-
Notifications
You must be signed in to change notification settings - Fork 54
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
Conversation
During debugging I noticed that we have a mismatch in the description of how 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. |
Current version of the test for the |
52a8a9d
to
34492d8
Compare
There was a problem hiding this 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.
I agree, once the procedure needs an internal delimiter comments, it is the sign to divide it. I'll implement the |
720f6c9
to
2728521
Compare
There was a problem hiding this 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)?
da44180
to
52df279
Compare
There was a problem hiding this 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.
bb05603
to
61e419d
Compare
There was a problem hiding this 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.
miden-lib/asm/miden/tx.masm
Outdated
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 |
There was a problem hiding this comment.
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.
# 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, ...] |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
#! 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. |
There was a problem hiding this comment.
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/miden/tx.masm
Outdated
#! 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 |
There was a problem hiding this comment.
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.
df2d1c7
to
3b264d6
Compare
For now I applied the ABI rules just for the procedures that I used in the tests ( |
3b264d6
to
197be54
Compare
There was a problem hiding this 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 # |
There was a problem hiding this comment.
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 syscall
s only.
miden-lib/asm/miden/tx.masm
Outdated
push.0 movdn.7 padw padw movdnw.3 movdnw.3 | ||
# => [tag, aux, note_type, RECIPIENT, PAD(9)] |
There was a problem hiding this comment.
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.
miden-lib/asm/miden/tx.masm
Outdated
# 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 |
There was a problem hiding this comment.
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
miden-lib/asm/miden/tx.masm
Outdated
# 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 |
There was a problem hiding this comment.
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.
197be54
to
9e3849c
Compare
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:
send_asset
wallet procedure