|
| 1 | +use crate::tests::mock::{RuntimeBuilder, Accounts, Mvm, Origin, run_to_block, TIME_BLOCK_MULTIPLIER}; |
| 2 | +use frame_support::assert_ok; |
| 3 | +use move_core_types::{ |
| 4 | + language_storage::StructTag, identifier::Identifier, account_address::AccountAddress, |
| 5 | + resolver::ResourceResolver, |
| 6 | +}; |
| 7 | +use sp_mvm::storage::MoveVmStorage; |
| 8 | +use move_vm::io::state::State; |
| 9 | + |
| 10 | +pub mod modules { |
| 11 | + use assets::Asset; |
| 12 | + pub static STORE: Asset = Asset::new( |
| 13 | + "Store", |
| 14 | + "src/tests/assets/user/build/assets/bytecode_modules/Store.mv", |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +pub mod transactions { |
| 19 | + use assets::Asset; |
| 20 | + pub static STORE_SYSTEM_BLOCK: Asset = Asset::new( |
| 21 | + "store_system_block", |
| 22 | + "src/tests/assets/user/build/assets/transaction/store_system_block.mvt", |
| 23 | + ); |
| 24 | + pub static STORE_SYSTEM_TIMESTAMP: Asset = Asset::new( |
| 25 | + "store_system_timestamp", |
| 26 | + "src/tests/assets/user/build/assets/transaction/store_system_timestamp.mvt", |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +const GAS_LIMIT: u64 = 1_000_000; |
| 31 | + |
| 32 | +/// Check stored value (u64) inside storage. |
| 33 | +fn check_stored_value(expected: u64) { |
| 34 | + #[derive(serde::Deserialize, Debug, PartialEq)] |
| 35 | + struct StoreU64 { |
| 36 | + pub val: u64, |
| 37 | + } |
| 38 | + |
| 39 | + let expected = StoreU64 { val: expected }; |
| 40 | + |
| 41 | + let tag = StructTag { |
| 42 | + address: Accounts::BOB.addr(), |
| 43 | + module: Identifier::new(modules::STORE.name()).unwrap(), |
| 44 | + name: Identifier::new("U64").unwrap(), |
| 45 | + type_params: vec![], |
| 46 | + }; |
| 47 | + |
| 48 | + check_storage_res(Accounts::BOB.addr(), tag, expected); |
| 49 | +} |
| 50 | + |
| 51 | +/// Check resource value inside storage. |
| 52 | +pub fn check_storage_res<T>(owner: AccountAddress, ty: StructTag, expected: T) |
| 53 | +where |
| 54 | + T: for<'de> serde::Deserialize<'de>, |
| 55 | + T: std::cmp::PartialEq + std::fmt::Debug, |
| 56 | +{ |
| 57 | + let storage = Mvm::move_vm_storage(); |
| 58 | + let state = State::new(storage); |
| 59 | + let blob = state |
| 60 | + .get_resource(&owner, &ty) |
| 61 | + .expect("VM state read storage (resource)") |
| 62 | + .expect(&format!("Resource '{}' should exist", ty)); |
| 63 | + |
| 64 | + let tt_str = format!("{}", ty); |
| 65 | + println!("checking stored resource '{}'", tt_str); |
| 66 | + let stored: T = |
| 67 | + bcs::from_bytes(&blob).expect(&format!("Resource '{}' should exists", tt_str)); |
| 68 | + assert_eq!(expected, stored); |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +/// Execute storing of block height inside module by calling script. |
| 73 | +fn execute_store_block() { |
| 74 | + RuntimeBuilder::new().build().execute_with(|| { |
| 75 | + // Publish STORE module. |
| 76 | + assert_ok!(Mvm::publish_module( |
| 77 | + Origin::signed(Accounts::BOB.account()), |
| 78 | + modules::STORE.bytes().to_vec(), |
| 79 | + GAS_LIMIT |
| 80 | + )); |
| 81 | + |
| 82 | + const EXPECTED: u32 = 3; |
| 83 | + run_to_block(EXPECTED); |
| 84 | + assert_ok!(Mvm::execute( |
| 85 | + Origin::signed(Accounts::BOB.account()), |
| 86 | + transactions::STORE_SYSTEM_BLOCK.bytes().to_vec(), |
| 87 | + GAS_LIMIT |
| 88 | + )); |
| 89 | + check_stored_value(EXPECTED.into()); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +#[test] |
| 94 | +/// Execute storing of timestamp inside module by calling script. |
| 95 | +fn execute_store_time() { |
| 96 | + RuntimeBuilder::new().build().execute_with(|| { |
| 97 | + // Publish STORE module. |
| 98 | + assert_ok!(Mvm::publish_module( |
| 99 | + Origin::signed(Accounts::BOB.account()), |
| 100 | + modules::STORE.bytes().to_vec(), |
| 101 | + GAS_LIMIT |
| 102 | + )); |
| 103 | + |
| 104 | + const EXPECTED: u32 = 3; |
| 105 | + run_to_block(EXPECTED); |
| 106 | + assert_ok!(Mvm::execute( |
| 107 | + Origin::signed(Accounts::BOB.account()), |
| 108 | + transactions::STORE_SYSTEM_TIMESTAMP.bytes().to_vec(), |
| 109 | + GAS_LIMIT |
| 110 | + )); |
| 111 | + check_stored_value(EXPECTED as u64 * TIME_BLOCK_MULTIPLIER); |
| 112 | + }); |
| 113 | +} |
0 commit comments