Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement send_note for basic wallet #808

Merged
merged 12 commits into from
Aug 9, 2024
Merged

Implement send_note for basic wallet #808

merged 12 commits into from
Aug 9, 2024

Conversation

Fumuran
Copy link
Contributor

@Fumuran Fumuran commented Jul 30, 2024

This PR implements a new send_note procedure for basic wallet.
This procedure created a new note and adds an arbitrary number of assets to it. Assets are provided to the procedure by their number and a memory address where they are stored.

TODO:

  • Update docs
  • Update CHANGELOG
  • Create an issue: remove send_asset wallet procedure

@Fumuran
Copy link
Contributor Author

Fumuran commented Jul 30, 2024

During debugging I noticed that we have a mismatch in the description of how create_note kernel procedure should work. Instead of returning note_idx and 5 zeros, as it specified in the description, this procedure returns note_idx and 6 zeros.

I can't see it causing any problems in our implementations of whatever uses this procedure, but in my case I had to remove excess zero to make my procedure work. Probably we should create an issue to investigate further and at least update the docs.

@Fumuran
Copy link
Contributor Author

Fumuran commented Jul 30, 2024

Current version of the test for the send_note procedure doesn't work. To make it pass I needed to somehow provide the values representing the assets to the new context where send_note is executed, which, for now, I don't know how to do.

@Fumuran Fumuran requested a review from bobbinth July 30, 2024 16:00
@Fumuran Fumuran force-pushed the andrew-impl-send-note branch 2 times, most recently from 52a8a9d to 34492d8 Compare July 31, 2024 19:16
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you! I left a few comments inline. But also, I'm wondering if the approach w'eve taken here is the right one. An alternative approach could be:

  • We add a procedure send_note which creates a note without assets (i.e., no need to pass assets into this function).
  • We add a procedure move_asset_into_note. This procedure takes a note index and an asset, removes the specified asset from the account and adds it to the note.

Then the caller could use these two procedures to create notes with however many assets they need, and i would say that it shouldn't be any more complicated than the current approach (though, it may be a bit less efficient).

We could also potentially get rid of the send_asset procedure as these two procedures should be able to replace it.

miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
@Fumuran
Copy link
Contributor Author

Fumuran commented Aug 2, 2024

An alternative approach could be:

  • We add a procedure send_note which creates a note without assets (i.e., no need to pass assets into this function).
  • We add a procedure move_asset_into_note. This procedure takes a note index and an asset, removes the specified asset from the account and adds it to the note.

I agree, once the procedure needs an internal delimiter comments, it is the sign to divide it. I'll implement the move_asset_into_note to lighten the send_note.

@Fumuran Fumuran force-pushed the andrew-impl-send-note branch from 720f6c9 to 2728521 Compare August 2, 2024 18:33
@Fumuran Fumuran marked this pull request as ready for review August 2, 2024 18:42
@Fumuran Fumuran requested a review from bobbinth August 2, 2024 18:42
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks good. I left a coupe more comments inline - overall, I think we should simplify this quite a bit and also try to introduce ABI described in #685.

Also, should we try to fix the inconsistency mentioned in #808 (comment)?

miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
@Fumuran Fumuran force-pushed the andrew-impl-send-note branch 2 times, most recently from da44180 to 52df279 Compare August 6, 2024 07:12
@Fumuran Fumuran requested a review from bobbinth August 7, 2024 08:18
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks good. Not a full review yet - but I left some more comments inline.

miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
@Fumuran Fumuran force-pushed the andrew-impl-send-note branch from bb05603 to 61e419d Compare August 8, 2024 13:08
@Fumuran Fumuran requested a review from bobbinth August 8, 2024 13:16
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you! I reviewed pretty much everything except for the new test and left some more comments inline.

Comment on lines 78 to 99
export.add_asset_to_note
dupw movup.8
# => [note_idx, ASSET, ASSET]

syscall.add_asset_to_note
# => [note_idx, EMPTY_WORD]
# => [note_idx, EMPTY_WORD, ASSET]

# clear the padding from the kernel response
movdn.4 dropw
# => [note_idx]
movdn.8 dropw
# => [ASSET, note_idx]
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also update add_asset_to_note in api.masm to have the same signature as here? This way, we won't need to do extra dupw and dropw here.

Comment on lines 84 to 86
# load the ASSET and add it to the note
padw loc_loadw.0 movup.4 exec.tx::add_asset_to_note
padw loc_loadw.0 exec.tx::add_asset_to_note dropw
# => [note_idx, ASSET, EMPTY_WORD, ...]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to first do padw and then dropw here? Could we not do something like:

movdn.4 loc_loadw.0 movup.4 exec.tx::add_asset_to_note

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think I made a mistake here, it slipped my mind that tx::add_asset_to_note have a [ASSET, note_idx] -> [ASSET, note_idx] stack transition, so we don't need to drop the word at the end. So something like this would work:

movdn.4 loc_loadw.0 exec.tx::add_asset_to_note movup.4

It is a problem though that this procedure was returning a wrong result, and no test was failing, although we have tests for distribute procedure. I'll check why this happened.

miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
miden-lib/asm/miden/contracts/wallets/basic.masm Outdated Show resolved Hide resolved
@Fumuran Fumuran requested a review from bobbinth August 8, 2024 21:04
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I left a few more comments inline.

Comment on lines 526 to 528
#! This procedure is expected to be invoked using a `call` instruction. It makes no guarantees about
#! the contents of the `PAD` elements shown below. It is the caller's responsibility to make sure
#! these elements do not contain any meaningful data.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All exported procedures in this file are expected to be invoked using a syscall instruction. So, I'd update this comment and move it to the top of the file.

miden-lib/asm/kernels/transaction/api.masm Show resolved Hide resolved
Comment on lines 71 to 87
#! Adds the ASSET to the note specified by the index.
#!
#! Inputs: [note_idx, ASSET]
#! Outputs: [note_idx]
#! This procedure is expected to be invoked using a `call` instruction. It makes no guarantees about
#! the contents of the `PAD` elements shown below. It is the caller's responsibility to make sure
#! these elements do not contain any meaningful data.
#!
#! Inputs: [ASSET, note_idx, PAD(11)]
#! Outputs: [ASSET, note_idx, PAD(11)]
#!
#! note_idx is the index of the note to which the asset is added.
#! ASSET can be a fungible or non-fungible asset.
export.add_asset_to_note
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This procedure is invoked via exec instruction - not call instruction. So, the comment is not accurate here. And the signature for this procedure should probably be:

#! Inputs:  [ASSET, note_idx, ...]
#! Outputs: [ASSET, note_idx, ...]

Within this procedure, we should ensure that we pad and "unpad" the stack appropriately before making a syscall. That is, the stack before the syscall should look like this:

# => [ASSET, note_idx, ZERO(11)]

Unless, of course, we change the methodology to guarantee that add_asset_to_note in api.masm does not modify the PAD elements.

@Fumuran Fumuran force-pushed the andrew-impl-send-note branch from df2d1c7 to 3b264d6 Compare August 9, 2024 09:08
@Fumuran
Copy link
Contributor Author

Fumuran commented Aug 9, 2024

For now I applied the ABI rules just for the procedures that I used in the tests (tx::create_note, tx::add_asset_to_note, some procs in the tx script), I think it will be better to move the refactoring of the other procedures to the different (specific) PR.

@Fumuran Fumuran force-pushed the andrew-impl-send-note branch from 3b264d6 to 197be54 Compare August 9, 2024 09:15
miden-tx/src/tests/mod.rs Outdated Show resolved Hide resolved
@Fumuran Fumuran requested a review from bobbinth August 9, 2024 09:22
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you! I left a few comments inline. After these are addressed, we can merged (though if #816 gets merged first, we'll need to update this PR).

For now I applied the ABI rules just for the procedures that I used in the tests (tx::create_note, tx::add_asset_to_note, some procs in the tx script), I think it will be better to move the refactoring of the other procedures to the different (specific) PR.

Agreed. I'm actually not 100% sure yet if we should adapt this ABI for kernel procedures as it results in a bit more stack manipulation than I'd like. The main problem is that without doing this, we need to guarantee that these procedures don't modify the elements they are not supposed to. If we had a good testing framework for that, this could be good enough to avoid these ABI changes.

@@ -8,6 +8,13 @@ use.miden::kernels::tx::memory
use.miden::kernels::tx::note
use.miden::kernels::tx::tx

# NOTE
# =================================================================================================
# Procedures in this module are expected to be invoked using a `call` instruction. It makes no #
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change call to syscall as all procedures in this module are be accessible via syscalls only.

Comment on lines 69 to 70
push.0 movdn.7 padw padw movdnw.3 movdnw.3
# => [tag, aux, note_type, RECIPIENT, PAD(9)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be able to do something like this here:

push.0 movdn.7 padw padw swapdw

(movdnw instructions are more expensive than swapdw).

Though, it will become just padw padw swapdw after #816 is merged.

# clear the padding from the kernel response
movdn.4 dropw swap drop
# remove excess PADs from the stack
movdnw.3 dropw dropw dropw movdn.3 drop drop drop
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too I think we can use swapdw to make things a bit more efficient:

swapdw dropw dropw movdn.8 dropw drop drop drop

Comment on lines 89 to 97
# elements
push.0.0.0 padw padw movupw.3 movup.15 movup.4
# => [note_idx, ASSET, PAD(11)]

# clear the padding from the kernel response
movdn.4 dropw
# => [note_idx]
syscall.add_asset_to_note
# => [note_idx, ASSET, PAD(11)]

# remove excess PADs from the stack
movdn.15 movdnw.3 dropw dropw drop drop drop movdn.4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comments here about potentially making things a bit more efficient via swapdw instruction.

@Fumuran Fumuran force-pushed the andrew-impl-send-note branch from 197be54 to 9e3849c Compare August 9, 2024 20:38
@Fumuran Fumuran merged commit 77f0f74 into next Aug 9, 2024
13 checks passed
@Fumuran Fumuran deleted the andrew-impl-send-note branch August 9, 2024 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants