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

P2ID script and test #187

Closed
wants to merge 13 commits into from
Closed

P2ID script and test #187

wants to merge 13 commits into from

Conversation

Dominik1999
Copy link
Collaborator

@Dominik1999 Dominik1999 commented Aug 4, 2023

I want to create a P2ID MASM script, see #12

I had to create a test in miden-tx/src/test.rs that creates two Accounts and a Note. The Note is created using the P2ID script. The ID that is set in the script is the AccountID of the target_account.

Unfortunately, we don't have an account procedure for account::add_asset yet. So I created an artificial procedure that is being called. Only the target_account can successfully consume the note and execute the note script.

I will squash all commits into one single commit after the review.

I will add the script to miden-lib after the initial review.

@Dominik1999 Dominik1999 force-pushed the dominik_pay_2_id_script branch from 3566794 to aea0e5d Compare August 4, 2023 10:08
@frisitano frisitano force-pushed the dominik_pay_2_id_script branch 2 times, most recently from a2dca59 to 86904dc Compare August 7, 2023 11:55
@Dominik1999 Dominik1999 force-pushed the dominik_pay_2_id_script branch from ded3dae to 4c00b4b Compare August 7, 2023 15:56
@Dominik1999 Dominik1999 force-pushed the dominik_pay_2_id_script branch 2 times, most recently from 087f712 to c05f3b9 Compare August 8, 2023 09:00
@Dominik1999 Dominik1999 force-pushed the dominik_pay_2_id_script branch from c05f3b9 to 02c186b Compare August 8, 2023 10:07
@Dominik1999 Dominik1999 requested a review from frisitano August 8, 2023 11:57
@Dominik1999 Dominik1999 changed the title WIP: p2id script testcase P2ID script and test Aug 8, 2023
@Dominik1999 Dominik1999 force-pushed the dominik_pay_2_id_script branch from d392cdb to a33dc8a Compare August 8, 2023 12:32
Copy link
Contributor

@frisitano frisitano 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 so far! A few minor comments left inline.

@Dominik1999 Dominik1999 mentioned this pull request Aug 8, 2023
@Dominik1999 Dominik1999 requested a review from frisitano August 8, 2023 15:52
@Dominik1999 Dominik1999 force-pushed the dominik_pay_2_id_script branch from 5d0c8a0 to 48b0860 Compare August 8, 2023 17:02
@Dominik1999 Dominik1999 requested a review from bobbinth August 8, 2023 17:03
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 great! Thank you! I left some comments inline. Some of them can be applied now, but some would need to be addressed after we implement #22.

Another comment (not for this PR, but would should create an issue for this): it would be good to set up structure for standard note scripts - something similar to how we handle miden library and SatKernel.

One approach would be to put note scripts under miden-lib/asm/scripts directory. This would also mean that the current content of miden-lib/asm would need to be moved under miden-lib/asm/miden or something like that.

We would also need to modify the build script to compile these scripts and save them into miden-lib/assets directory. One issue here is that we don't have a way to serialize ProgramAst into files. This should be fairly easy to do and the extension we can use is something like .masb (Miden Assembly binary). This is something we'll need to implement in Miden VM - so, let's create an issue for this.

Lastly, we may want to provide some convenience interface from miden-lib crate to create notes for standard scripts. We could have an enum to identify each script. For example:

pub enum Script {
    P2ID { target: AccountId },
    P2IDR {
        target: AccountId,
        recall_height: u32,
    },
}

And then we could have a function which could look something like this:

pub fn create_note(
    script: Script,
    assets: Vec<Asset>,
    sender: AccountId,
    tag: Option<Felt>,
) -> Result<Note, SomeError>

Comment on lines +278 to +282
pub fn mock_inputs(
account_status: AccountStatus,
account: Option<Account>,
consumed_notes: Option<Vec<Note>>,
) -> (Account, BlockHeader, ChainMmr, Vec<Note>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should just create a separate method for handling this case - e.g., something like:

pub fn mock_inputs_with_account(
    account: Acount,
    consumed_notes: Vec<Note>,
) -> (Account, BlockHeader, ChainMmr, Vec<Note>)

An then mock_inputs() would create an account and then just call this function.

Comment on lines 370 to 372
begin # [note_inputs = target_account_id, ...]
exec.account::get_id # [account_id, target_account_id, ...]
assert_eq # [] if account_id == target_account_id, fails if not
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be good to use the same commenting style as we use in other places (e.g., see here). For example, it could start something like this:

# Deposits all assets of this note into an account. Fails if the current account ID does 
# not match the target account ID.
#
# Input:  [target_account_id, ...]
# Output: [...]
being
    # get the current account ID and make sure it is the same as the target account ID
    exec.account::get_id
    assert_eq
    # => [...]

exec.account::get_id # [account_id, target_account_id, ...]
assert_eq # [] if account_id == target_account_id, fails if not

push.1000000000 # [1000000000, ...] memory pointer to store assets
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to use 1000000000 rather than just 0?

Comment on lines +377 to +382
dup neq.0 # [1 || 0, num_of_assets, 1000000000, ...]
while.true # [num_of_assets, 1000000000, ...]
add sub.1 # [num_of_assets + 1000000000 - (i), ...] -> points to last asset in memory
exec.account::get_nonce drop # TODO: Should call add_asset but we don't have it yet
dup neq.1000000000 # [1 || 0, num_of_assets + 1000000000 - (i), ...], if end of memory reached, break
end
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't be calling add_asset directly here as the call to add_asset should be done from within account code (if it is done from the note, it won't pass the access check). So, what we need is a procedure on the target account - something like receive_asset. This procedure could look as follows:

# Input:  [ASSET, ...]
# Output: [...]
export.recieve_asset
    # call account::add_asset from here
end

Ideally, this procedure would be a part of basic wallet and then we'd be able to call it from here. For now, we can probably just load the asset onto the stack and then drop it using dropw procedure. We can update this later when basic wallet interface is implemented.

Another thing, we would need to use call instruction to invoke the account procedure. Calling convention for such procedures is still an open question (see #179). This is also something to figure out as we work through the basic wallet interface.

Lastly, computing the end pointer and then incrementing current pointer within the loop would be more efficient because add.1 just takes a single VM cycle.


// check that we got the expected result - TransactionResult and not TransactionExecutorError
match transaction_result {
Ok(_) => {} // expected result, we do nothing
Copy link
Contributor

Choose a reason for hiding this comment

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

Once everything is implemented, we probably want to confirm that transaction result contains the expected data. Maybe leave a TODO here to revisit this later?

@Dominik1999 Dominik1999 force-pushed the dominik_pay_2_id_script branch from 48b0860 to 8512eb8 Compare August 10, 2023 15:19
@frisitano
Copy link
Contributor

Heads up that the account vault has been implemented in masm - see here. I believe this should unblock this PR.

@Dominik1999
Copy link
Collaborator Author

Closing in favor of #244

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.

3 participants