|
| 1 | +// Run this test with: |
| 2 | +// cargo test --package mina-tree --test test_zkapp |
| 3 | + |
| 4 | +use ark_ff::Zero; |
| 5 | +use mina_curves::pasta::Fp; |
| 6 | +use mina_signer::Signature; |
| 7 | +use mina_tree::{ |
| 8 | + scan_state::{ |
| 9 | + currency::{Amount, Fee, Magnitude, Nonce, Sgn, Signed, Slot}, |
| 10 | + transaction_logic::{ |
| 11 | + zkapp_command::{ |
| 12 | + Account, AccountPreconditions, AccountUpdate, Actions, AuthorizationKind, Body, |
| 13 | + CallForest, Control, Events, FeePayer, FeePayerBody, MayUseToken, Numeric, |
| 14 | + Preconditions, Tree, Update, WithStackHash, ZkAppCommand, ZkAppPreconditions, |
| 15 | + }, |
| 16 | + Memo, |
| 17 | + }, |
| 18 | + }, |
| 19 | + MutableFp, TokenId, |
| 20 | +}; |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn test_zkapp_command_creation() { |
| 24 | + // Create fee payer account |
| 25 | + let fee_payer_pk = mina_signer::PubKey::from_address( |
| 26 | + "B62qmnY6m4c6bdgSPnQGZriSaj9vuSjsfh6qkveGTsFX3yGA5ywRaja", |
| 27 | + ) |
| 28 | + .unwrap() |
| 29 | + .into_compressed(); |
| 30 | + |
| 31 | + // Create fee payer body |
| 32 | + let fee_payer_body = FeePayerBody { |
| 33 | + public_key: fee_payer_pk.clone(), |
| 34 | + fee: Fee::from_u64(1000000), |
| 35 | + valid_until: Some(Slot::from_u32(100000)), |
| 36 | + nonce: Nonce::from_u32(0), |
| 37 | + }; |
| 38 | + |
| 39 | + // Create fee payer with a dummy signature |
| 40 | + let fee_payer = FeePayer { |
| 41 | + body: fee_payer_body, |
| 42 | + authorization: Signature::dummy(), |
| 43 | + }; |
| 44 | + |
| 45 | + // Create an account update body |
| 46 | + let account_update_pk = mina_signer::PubKey::from_address( |
| 47 | + "B62qjVQLxt9nYMWGn45mkgwYfcz8e8jvjNCBo11VKJb7vxDNwv5QLPS", |
| 48 | + ) |
| 49 | + .unwrap() |
| 50 | + .into_compressed(); |
| 51 | + |
| 52 | + let account_update_body = Body { |
| 53 | + public_key: account_update_pk.clone(), |
| 54 | + token_id: TokenId::default(), |
| 55 | + update: Update::noop(), |
| 56 | + balance_change: Signed { |
| 57 | + magnitude: Amount::zero(), |
| 58 | + sgn: Sgn::Pos, |
| 59 | + }, |
| 60 | + increment_nonce: false, |
| 61 | + events: Events::empty(), |
| 62 | + actions: Actions::empty(), |
| 63 | + call_data: Fp::zero(), |
| 64 | + preconditions: Preconditions { |
| 65 | + network: ZkAppPreconditions::accept(), |
| 66 | + account: AccountPreconditions(Account::accept()), |
| 67 | + valid_while: Numeric::Ignore, |
| 68 | + }, |
| 69 | + use_full_commitment: false, |
| 70 | + implicit_account_creation_fee: false, |
| 71 | + may_use_token: MayUseToken::No, |
| 72 | + authorization_kind: AuthorizationKind::NoneGiven, |
| 73 | + }; |
| 74 | + |
| 75 | + // Create account update |
| 76 | + let account_update = AccountUpdate { |
| 77 | + body: account_update_body, |
| 78 | + authorization: Control::NoneGiven, |
| 79 | + }; |
| 80 | + |
| 81 | + // Create a tree with the account update and empty calls |
| 82 | + let tree = Tree { |
| 83 | + account_update, |
| 84 | + account_update_digest: MutableFp::new(Fp::zero()), |
| 85 | + calls: CallForest::new(), |
| 86 | + }; |
| 87 | + |
| 88 | + // Wrap tree in WithStackHash |
| 89 | + let tree_with_hash = WithStackHash { |
| 90 | + elt: tree, |
| 91 | + stack_hash: MutableFp::new(Fp::zero()), |
| 92 | + }; |
| 93 | + |
| 94 | + // Create call forest with the tree |
| 95 | + let call_forest = CallForest(vec![tree_with_hash]); |
| 96 | + |
| 97 | + // Ensure hashes are computed |
| 98 | + call_forest.ensure_hashed(); |
| 99 | + |
| 100 | + // Create the zkApp command |
| 101 | + let zkapp_command = ZkAppCommand { |
| 102 | + fee_payer, |
| 103 | + account_updates: call_forest, |
| 104 | + memo: Memo::empty(), |
| 105 | + }; |
| 106 | + |
| 107 | + // Verify basic properties |
| 108 | + assert_eq!(zkapp_command.fee(), Fee::from_u64(1000000)); |
| 109 | + assert_eq!(zkapp_command.fee_payer().public_key, fee_payer_pk); |
| 110 | + assert_eq!(zkapp_command.fee_token(), TokenId::default()); |
| 111 | + |
| 112 | + // Verify account updates - access the inner vector directly |
| 113 | + assert_eq!(zkapp_command.account_updates.0.len(), 1); |
| 114 | + assert_eq!( |
| 115 | + zkapp_command.account_updates.0[0] |
| 116 | + .elt |
| 117 | + .account_update |
| 118 | + .body |
| 119 | + .public_key, |
| 120 | + account_update_pk |
| 121 | + ); |
| 122 | +} |
0 commit comments