From 8c2ec8b0256a23ff749b9c9ee56f03f4e5a9f7c8 Mon Sep 17 00:00:00 2001 From: Michael Eberhardt <64731211+neutrinoks@users.noreply.github.com> Date: Thu, 18 Apr 2024 16:30:10 +0200 Subject: [PATCH] feat: optimise structuring and common used dev-utils --- pallet/src/assets.rs | 66 ---------- pallet/src/benchmarking.rs | 198 ++++++++++++------------------ pallet/src/lib.rs | 4 +- pallet/src/mock.rs | 28 +---- pallet/src/mock_utils.rs | 106 ++++++++++++++++ pallet/src/tests/balance.rs | 54 ++++---- pallet/src/tests/example.rs | 16 +-- pallet/src/tests/execute.rs | 38 +++--- pallet/src/tests/modules.rs | 12 +- pallet/src/tests/publish.rs | 54 ++++---- pallet/src/tests/signer.rs | 86 ++++++------- pallet/src/tests/update_stdlib.rs | 14 +-- 12 files changed, 326 insertions(+), 350 deletions(-) delete mode 100644 pallet/src/assets.rs create mode 100644 pallet/src/mock_utils.rs diff --git a/pallet/src/assets.rs b/pallet/src/assets.rs deleted file mode 100644 index 78ef72e..0000000 --- a/pallet/src/assets.rs +++ /dev/null @@ -1,66 +0,0 @@ -//! Helper methods to load move-project test assets. - -#[cfg(test)] -extern crate alloc; -#[cfg(test)] -extern crate std; - -#[cfg(test)] -use alloc::format; -#[cfg(test)] -use sp_std::vec::Vec; - -#[cfg(test)] -const MOVE_PROJECTS: &str = "src/assets/move-projects"; - -/// Reads bytes from a file for the given path. -/// Can panic if the file doesn't exist. -#[cfg(test)] -fn read_bytes(file_path: &str) -> Vec { - std::fs::read(file_path) - .unwrap_or_else(|e| panic!("Can't read {file_path}: {e} - make sure you run pallet-move/tests/assets/move-projects/smove-build-all.sh")) -} - -/// Reads a precompiled Move module from our assets directory. -#[cfg(test)] -pub fn read_module_from_project(project: &str, module_name: &str) -> Vec { - let path = - format!("{MOVE_PROJECTS}/{project}/build/{project}/bytecode_modules/{module_name}.mv"); - read_bytes(&path) -} - -/// Reads a precompiled Move bundle from our assets directory. -#[cfg(test)] -pub fn read_bundle_from_project(project: &str, bundle_name: &str) -> Vec { - let path = format!("{MOVE_PROJECTS}/{project}/build/{project}/bundles/{bundle_name}.mvb"); - read_bytes(&path) -} - -/// Reads a precompiled Move scripts from our assets directory. -#[cfg(test)] -pub fn read_script_from_project(project: &str, script_name: &str) -> Vec { - let path = - format!("{MOVE_PROJECTS}/{project}/build/{project}/bytecode_scripts/{script_name}.mv"); - read_bytes(&path) -} - -#[macro_export] -macro_rules! script_transaction { - ($bytecode:expr, $type_args:expr $(, $args:expr)*) => { - { - let transaction = ScriptTransaction { - bytecode: $bytecode, - type_args: $type_args, - args: vec![$(bcs::to_bytes($args).unwrap()),*], - }; - bcs::to_bytes(&transaction).unwrap() - } - } -} - -#[macro_export] -macro_rules! no_type_args { - () => { - vec![] - }; -} diff --git a/pallet/src/benchmarking.rs b/pallet/src/benchmarking.rs index b0ef5ad..00c8404 100644 --- a/pallet/src/benchmarking.rs +++ b/pallet/src/benchmarking.rs @@ -1,46 +1,40 @@ //! Benchmarking setup for pallet-move. -use frame_benchmarking::*; +use frame_benchmarking::benchmarks; +#[cfg(test)] +use frame_benchmarking::impl_benchmark_test_suite; use frame_system::{Config as SysConfig, RawOrigin}; -use move_core_types::account_address::AccountAddress; use move_vm_backend::types::MAX_GAS_AMOUNT; -pub use sp_core::{crypto::Ss58Codec, sr25519::Public}; -use sp_std::{vec, vec::Vec}; +use sp_core::crypto::Ss58Codec; -#[cfg(test)] -use crate::mock::*; -use crate::{balance::BalanceOf, *}; +use crate::{balance::BalanceOf, mock_utils::*, *}; -const LIMIT: u128 = 30_000_000_000_000; -const BOB_ADDR: &str = "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"; -const ALICE_ADDR: &str = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"; -const DAVE_ADDR: &str = "5DAAnrj7VHTznn2AWBemMuyBwZWs6FNFjdyVXUeYum3PTXFy"; -const EVE_ADDR: &str = "5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw"; +const LIMIT: u128 = 60_000_000_000_000; benchmarks! { where_clause { where T: Config + SysConfig, - T::AccountId: From, + T::AccountId: Ss58Codec, } execute { let n in 0 .. 7; - let (bob_32, bob_mv) = account_address::(BOB_ADDR); - let (alice_32, alice_mv) = account_address::(ALICE_ADDR); - let (dave_32, dave_mv) = account_address::(DAVE_ADDR); - let (eve_32, eve_mv) = account_address::(EVE_ADDR); + let (bob_32, bob_mv) = account_n_address::(BOB_ADDR); + let (alice_32, alice_mv) = account_n_address::(ALICE_ADDR); + let (dave_32, dave_mv) = account_n_address::(DAVE_ADDR); + let (eve_32, eve_mv) = account_n_address::(EVE_ADDR); // Our benchmark plan (each is a test scenario with different parameters). let script_bcs = [ - car_wash_initial_coin_miniting(&bob_mv), - multiple_signers_init_module(&bob_mv), - car_wash_register_new_user(&alice_mv), - car_wash_buy_coin(&alice_mv, 1), - car_wash_wash_car(&alice_mv), - multiple_signers_rent_apartment(&alice_mv, &dave_mv, &eve_mv, 1), - multiple_signers_rent_apartment(&alice_mv, &dave_mv, &eve_mv, 1), - multiple_signers_rent_apartment(&alice_mv, &dave_mv, &eve_mv, 1), + car_wash_initial_coin_miniting().to_vec(), + multiple_signers_init_module().to_vec(), + car_wash_register_new_user().to_vec(), + car_wash_buy_coin().to_vec(), + car_wash_wash_car().to_vec(), + multiple_signers_rent_apartment().to_vec(), + multiple_signers_rent_apartment().to_vec(), + multiple_signers_rent_apartment().to_vec(), ]; // Sequence of account-IDs who will execute each extrinsic call. let accounts = [ @@ -60,12 +54,12 @@ benchmarks! { // Publish both modules always. Pallet::::publish_module( RawOrigin::Signed(bob_32.clone()).into(), - car_wash_example_module(), + car_wash_example_module().to_vec(), MAX_GAS_AMOUNT ).unwrap(); Pallet::::publish_module( RawOrigin::Signed(bob_32.clone()).into(), - multiple_signers_module(), + multiple_signers_module().to_vec(), MAX_GAS_AMOUNT ).unwrap(); @@ -73,14 +67,14 @@ benchmarks! { if n > 1 && n < 5 { Pallet::::execute( RawOrigin::Signed(bob_32.clone()).into(), - car_wash_initial_coin_miniting(&bob_mv), + car_wash_initial_coin_miniting().to_vec(), MAX_GAS_AMOUNT, LIMIT.into() ).unwrap(); if n > 2 { Pallet::::execute( RawOrigin::Signed(alice_32.clone()).into(), - car_wash_register_new_user(&alice_mv), + car_wash_register_new_user().to_vec(), MAX_GAS_AMOUNT, LIMIT.into() ).unwrap(); @@ -88,7 +82,7 @@ benchmarks! { if n > 3 { Pallet::::execute( RawOrigin::Signed(alice_32.clone()).into(), - car_wash_buy_coin(&alice_mv, 1), + car_wash_buy_coin().to_vec(), MAX_GAS_AMOUNT, LIMIT.into() ).unwrap(); @@ -97,7 +91,7 @@ benchmarks! { if n > 4 { Pallet::::execute( RawOrigin::Signed(bob_32.clone()).into(), - multiple_signers_init_module(&bob_mv), + multiple_signers_init_module().to_vec(), MAX_GAS_AMOUNT, LIMIT.into() ).unwrap(); @@ -108,14 +102,13 @@ benchmarks! { publish_module { let n in 0 .. 3; - let bob_32: T::AccountId = Public::from_ss58check(BOB_ADDR).unwrap().into(); - let bob_mv = Pallet::::to_move_address(&bob_32).unwrap(); + let (bob_32, bob_mv) = account_n_address::(BOB_ADDR); let module_bcs = [ - move_basics_module(), - using_stdlib_natives_module(), - multiple_signers_module(), - car_wash_example_module(), + move_basics_module().to_vec(), + using_stdlib_natives_module().to_vec(), + multiple_signers_module().to_vec(), + car_wash_example_module().to_vec(), ]; let gas = [11, 33, 67, 74]; @@ -123,7 +116,7 @@ benchmarks! { }: _(RawOrigin::Signed(bob_32), module_bcs[n as usize].clone(), gas[n as usize]) publish_module_bundle { - let bob_32: T::AccountId = Public::from_ss58check(BOB_ADDR).unwrap().into(); + let bob_32 = account::(BOB_ADDR); let bundle = core::include_bytes!("assets/move-projects/using_stdlib_natives/build/using_stdlib_natives/bundles/using_stdlib_natives.mvb").to_vec(); }: _(RawOrigin::Signed(bob_32), bundle, 1_500_000) @@ -138,19 +131,19 @@ impl_benchmark_test_suite!( crate::mock::ExtBuilder::default() .with_balances(vec![ ( - crate::benchmarking::account::(crate::benchmarking::BOB_ADDR), + crate::mock_utils::account::(crate::mock_utils::BOB_ADDR), u128::MAX ), ( - crate::benchmarking::account::(crate::benchmarking::ALICE_ADDR), + crate::mock_utils::account::(crate::mock_utils::ALICE_ADDR), u128::MAX ), ( - crate::benchmarking::account::(crate::benchmarking::DAVE_ADDR), + crate::mock_utils::account::(crate::mock_utils::DAVE_ADDR), u128::MAX ), ( - crate::benchmarking::account::(crate::benchmarking::EVE_ADDR), + crate::mock_utils::account::(crate::mock_utils::EVE_ADDR), u128::MAX ), ]) @@ -158,89 +151,60 @@ impl_benchmark_test_suite!( crate::mock::Test ); -#[cfg(test)] -fn account(name: &str) -> T::AccountId -where - T::AccountId: From, -{ - Public::from_ss58check(name).unwrap().into() -} +use benchmark_only::*; -fn account_address(name: &str) -> (T::AccountId, AccountAddress) -where - T::AccountId: From, -{ - let account: T::AccountId = Public::from_ss58check(name).unwrap().into(); - let address = Pallet::::to_move_address(&account).unwrap(); - (account, address) -} - -// Move Basics Example -fn move_basics_module() -> Vec { - core::include_bytes!( - "assets/move-projects/move-basics/build/move-basics/bytecode_modules/EmptyBob.mv" - ) - .to_vec() -} +mod benchmark_only { + // Move Basics Example + pub fn move_basics_module() -> &'static [u8] { + core::include_bytes!( + "assets/move-projects/move-basics/build/move-basics/bytecode_modules/EmptyBob.mv" + ) + } -// Using Stdlib Natives Example -fn using_stdlib_natives_module() -> Vec { - core::include_bytes!("assets/move-projects/using_stdlib_natives/build/using_stdlib_natives/bytecode_modules/Vector.mv").to_vec() -} + // Using Stdlib Natives Example + pub fn using_stdlib_natives_module() -> &'static [u8] { + core::include_bytes!("assets/move-projects/using_stdlib_natives/build/using_stdlib_natives/bytecode_modules/Vector.mv") + } -// Car Wash Example -fn car_wash_example_module() -> Vec { - core::include_bytes!( - "assets/move-projects/car-wash-example/build/car-wash-example/bytecode_modules/CarWash.mv" - ) - .to_vec() -} + // Car Wash Example + pub fn car_wash_example_module() -> &'static [u8] { + core::include_bytes!( + "assets/move-projects/car-wash-example/build/car-wash-example/bytecode_modules/CarWash.mv" + ) + } -fn car_wash_initial_coin_miniting(addr: &AccountAddress) -> Vec { - let script = core::include_bytes!("assets/move-projects/car-wash-example/build/car-wash-example/bytecode_scripts/initial_coin_minting.mv").to_vec(); - script_transaction!(script, no_type_args!(), addr) -} + pub fn car_wash_initial_coin_miniting() -> &'static [u8] { + core::include_bytes!("assets/move-projects/car-wash-example/build/car-wash-example/script_transactions/initial_coin_minting.mvt") + } -fn car_wash_register_new_user(addr: &AccountAddress) -> Vec { - let script = core::include_bytes!("assets/move-projects/car-wash-example/build/car-wash-example/bytecode_scripts/register_new_user.mv").to_vec(); - script_transaction!(script, no_type_args!(), addr) -} + pub fn car_wash_register_new_user() -> &'static [u8] { + core::include_bytes!("assets/move-projects/car-wash-example/build/car-wash-example/script_transactions/register_new_user.mvt") + } -fn car_wash_buy_coin(addr: &AccountAddress, cnt: u8) -> Vec { - let script = core::include_bytes!( - "assets/move-projects/car-wash-example/build/car-wash-example/bytecode_scripts/buy_coin.mv" - ) - .to_vec(); - script_transaction!(script, no_type_args!(), addr, &cnt) -} + pub fn car_wash_buy_coin() -> &'static [u8] { + core::include_bytes!( + "assets/move-projects/car-wash-example/build/car-wash-example/script_transactions/buy_coin.mvt" + ) + } -fn car_wash_wash_car(addr: &AccountAddress) -> Vec { - let script = core::include_bytes!( - "assets/move-projects/car-wash-example/build/car-wash-example/bytecode_scripts/wash_car.mv" - ) - .to_vec(); - script_transaction!(script, no_type_args!(), addr) -} + pub fn car_wash_wash_car() -> &'static [u8] { + core::include_bytes!( + "assets/move-projects/car-wash-example/build/car-wash-example/script_transactions/wash_car.mvt" + ) + } -// Multiple Signers Example -fn multiple_signers_module() -> Vec { - core::include_bytes!( - "assets/move-projects/multiple-signers/build/multiple-signers/bytecode_modules/Dorm.mv" - ) - .to_vec() -} + // Multiple Signers Example + pub fn multiple_signers_module() -> &'static [u8] { + core::include_bytes!( + "assets/move-projects/multiple-signers/build/multiple-signers/bytecode_modules/Dorm.mv" + ) + } -fn multiple_signers_init_module(addr: &AccountAddress) -> Vec { - let script = core::include_bytes!("assets/move-projects/multiple-signers/build/multiple-signers/bytecode_scripts/init_module.mv").to_vec(); - script_transaction!(script, no_type_args!(), addr) -} + pub fn multiple_signers_init_module() -> &'static [u8] { + core::include_bytes!("assets/move-projects/multiple-signers/build/multiple-signers/script_transactions/init_module.mvt") + } -fn multiple_signers_rent_apartment( - addr1: &AccountAddress, - addr2: &AccountAddress, - addr3: &AccountAddress, - cnt: u8, -) -> Vec { - let script = core::include_bytes!("assets/move-projects/multiple-signers/build/multiple-signers/bytecode_scripts/rent_apartment.mv").to_vec(); - script_transaction!(script, no_type_args!(), addr1, addr2, addr3, &cnt) + pub fn multiple_signers_rent_apartment() -> &'static [u8] { + core::include_bytes!("assets/move-projects/multiple-signers/build/multiple-signers/script_transactions/rent_apartment.mvt") + } } diff --git a/pallet/src/lib.rs b/pallet/src/lib.rs index e72fbd4..482b9fe 100644 --- a/pallet/src/lib.rs +++ b/pallet/src/lib.rs @@ -2,13 +2,13 @@ #![recursion_limit = "256"] pub mod api; -#[cfg(any(test, feature = "runtime-benchmarks"))] -pub(crate) mod assets; pub mod balance; #[cfg(feature = "runtime-benchmarks")] pub mod benchmarking; #[cfg(test)] pub(crate) mod mock; +#[cfg(any(test, feature = "runtime-benchmarks"))] +pub(crate) mod mock_utils; mod result; mod signer; mod storage; diff --git a/pallet/src/mock.rs b/pallet/src/mock.rs index bd463df..ae7a569 100644 --- a/pallet/src/mock.rs +++ b/pallet/src/mock.rs @@ -8,12 +8,11 @@ use frame_support::{ traits::{ConstU128, ConstU16, ConstU32, ConstU64}, }; use frame_system::pallet_prelude::BlockNumberFor; -use sp_core::{crypto::Ss58Codec, sr25519::Public, H256}; +use sp_core::H256; use sp_runtime::traits::{BlakeTwo256, IdentityLookup}; use sp_runtime::BuildStorage; use crate as pallet_move; -use crate::Error; pub use move_core_types::account_address::AccountAddress; pub use move_vm_backend_common::types::ScriptTransaction; @@ -29,11 +28,6 @@ mod constants { // Key constants or frequently used ones. pub const EMPTY_CHEQUE: super::Balance = 0; pub const COIN_PRICE: u128 = 1_000_000_000_000; - pub const BOB_ADDR: &str = "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"; - pub const CAFE_ADDR: &str = "5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSv4fmh4G"; // == 0xCAFE - pub const ALICE_ADDR: &str = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"; - pub const DAVE_ADDR: &str = "5DAAnrj7VHTznn2AWBemMuyBwZWs6FNFjdyVXUeYum3PTXFy"; - pub const EVE_ADDR: &str = "5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw"; } pub use constants::*; @@ -160,26 +154,6 @@ impl ExtBuilder { } } -/// Creates a native 32-byte address from a given ss58 string. -pub(crate) fn addr32_from_ss58(ss58addr: &str) -> Result> { - let (pk, _) = Public::from_ss58check_with_version(ss58addr) - .map_err(|_| Error::::InvalidAccountSize)?; - let account: AccountId32 = pk.into(); - Ok(account) -} - -/// Converts a native 32-byte address into a Move memory address. -pub(crate) fn addr32_to_move(addr32: &AccountId32) -> Result> { - MoveModule::to_move_address(addr32) -} - -/// Creates a native 32-byte address and it's Move memory address by given ss58 string. -pub(crate) fn addrs_from_ss58(ss58: &str) -> Result<(AccountId32, AccountAddress), Error> { - let addr_32 = addr32_from_ss58(ss58)?; - let addr_mv = addr32_to_move(&addr_32)?; - Ok((addr_32, addr_mv)) -} - /// Rolls forward in future to the given block height. pub(crate) fn roll_to(n: BlockNumberFor) { let weight = Weight::from_parts(100_000_000_000, 1); diff --git a/pallet/src/mock_utils.rs b/pallet/src/mock_utils.rs new file mode 100644 index 0000000..1e542f0 --- /dev/null +++ b/pallet/src/mock_utils.rs @@ -0,0 +1,106 @@ +//! Helper methods to load move-project test assets. Constants and methods are sorted into +//! different modules for visibility reasons in depdency of enabled/disabled features. + +pub(crate) use always_used::*; +#[cfg(test)] +pub(crate) use tests_only::*; + +mod always_used { + extern crate alloc; + + use frame_system::Config as SysConfig; + use sp_core::crypto::Ss58Codec; + + pub use move_core_types::account_address::AccountAddress; + + use crate::{Config, Pallet}; + + // Reusable constants for test accounts. + pub const BOB_ADDR: &str = "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"; + pub const ALICE_ADDR: &str = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"; + pub const DAVE_ADDR: &str = "5DAAnrj7VHTznn2AWBemMuyBwZWs6FNFjdyVXUeYum3PTXFy"; + pub const EVE_ADDR: &str = "5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw"; + + /// Creates a native 32-byte address from a given ss58 string. + pub fn account(ss58addr: &str) -> T::AccountId + where + T::AccountId: Ss58Codec, + { + T::AccountId::from_ss58check_with_version(ss58addr) + .unwrap() + .0 + } + + /// Creates a native 32-byte address and it's Move memory address by given ss58 string. + pub fn account_n_address(ss58: &str) -> (T::AccountId, AccountAddress) + where + T::AccountId: Ss58Codec, + { + let addr_32 = account::(ss58); + let addr_mv = Pallet::::to_move_address(&addr_32).unwrap(); + (addr_32, addr_mv) + } + + #[macro_export] + macro_rules! script_transaction { + ($bytecode:expr, $type_args:expr $(, $args:expr)*) => { + { + let transaction = ScriptTransaction { + bytecode: $bytecode, + type_args: $type_args, + args: vec![$(bcs::to_bytes($args).unwrap()),*], + }; + bcs::to_bytes(&transaction).unwrap() + } + } + } + + #[macro_export] + macro_rules! no_type_args { + () => { + vec![] + }; + } +} + +#[cfg(test)] +mod tests_only { + extern crate alloc; + extern crate std; + + use alloc::format; + use sp_std::vec::Vec; + + // Reusable constants for test accounts. + pub const CAFE_ADDR: &str = "5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSv4fmh4G"; // == 0xCAFE + + // Location of our assets folder. + pub const MOVE_PROJECTS: &str = "src/assets/move-projects"; + + /// Reads bytes from a file for the given path. + /// Can panic if the file doesn't exist. + pub fn read_bytes(file_path: &str) -> Vec { + std::fs::read(file_path) + .unwrap_or_else(|e| panic!("Can't read {file_path}: {e} - make sure you run pallet-move/tests/assets/move-projects/smove-build-all.sh")) + } + + /// Reads a precompiled Move scripts from our assets directory. + pub fn read_script_from_project(project: &str, script_name: &str) -> Vec { + let path = + format!("{MOVE_PROJECTS}/{project}/build/{project}/bytecode_scripts/{script_name}.mv"); + read_bytes(&path) + } + + /// Reads a precompiled Move module from our assets directory. + pub fn read_module_from_project(project: &str, module_name: &str) -> Vec { + let path = + format!("{MOVE_PROJECTS}/{project}/build/{project}/bytecode_modules/{module_name}.mv"); + read_bytes(&path) + } + + /// Reads a precompiled Move bundle from our assets directory. + pub fn read_bundle_from_project(project: &str, bundle_name: &str) -> Vec { + let path = format!("{MOVE_PROJECTS}/{project}/build/{project}/bundles/{bundle_name}.mvb"); + read_bytes(&path) + } +} diff --git a/pallet/src/tests/balance.rs b/pallet/src/tests/balance.rs index 4f47bb4..216456b 100644 --- a/pallet/src/tests/balance.rs +++ b/pallet/src/tests/balance.rs @@ -1,8 +1,7 @@ use crate::{ - assets, balance::{BalanceAdapter, BalanceOf}, mock::*, - no_type_args, script_transaction, + mock_utils as utils, no_type_args, script_transaction, }; use frame_support::assert_ok; @@ -12,7 +11,7 @@ use move_vm_backend::{balance::BalanceHandler, types::MAX_GAS_AMOUNT}; fn verify_get_balance() { const AMOUNT: u128 = EXISTENTIAL_DEPOSIT + 100; - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default() .with_balances(vec![(bob_addr_32.clone(), AMOUNT)]) @@ -23,8 +22,7 @@ fn verify_get_balance() { assert_eq!(balance.total_amount(bob_addr_mv).unwrap(), AMOUNT); // Now check that it works from within the MoveVM. - let script = - assets::read_script_from_project("balance", "verify_preconfigured_balance"); + let script = utils::read_script_from_project("balance", "verify_preconfigured_balance"); let transaction_bc = script_transaction!(script, no_type_args!(), &bob_addr_mv, &AMOUNT); @@ -44,8 +42,8 @@ fn verify_get_balance() { fn verify_simple_transfer() { const AMOUNT: u128 = 100; - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default() .with_balances(vec![(alice_addr_32.clone(), 10_000)]) @@ -56,7 +54,7 @@ fn verify_simple_transfer() { let ini_blnc_bob = Balances::free_balance(&bob_addr_32); // Now check that it works from within the MoveVM. - let script = assets::read_script_from_project("balance", "single_transfer"); + let script = utils::read_script_from_project("balance", "single_transfer"); let transaction_bc = script_transaction!( script, @@ -84,9 +82,9 @@ fn verify_simple_transfer() { fn verify_multiple_transfers_different() { const AMOUNT: u128 = 100; - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); - let (dave_addr_32, dave_addr_mv) = addrs_from_ss58(DAVE_ADDR).unwrap(); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); + let (dave_addr_32, dave_addr_mv) = utils::account_n_address::(utils::DAVE_ADDR); ExtBuilder::default() .with_balances(vec![(alice_addr_32.clone(), 10_000)]) @@ -98,7 +96,7 @@ fn verify_multiple_transfers_different() { let ini_blnc_dave = Balances::free_balance(&dave_addr_32); // Now check that it works from within the MoveVM. - let script = assets::read_script_from_project("balance", "double_transfer"); + let script = utils::read_script_from_project("balance", "double_transfer"); let transaction_bc = script_transaction!( script, @@ -130,8 +128,8 @@ fn verify_multiple_transfers_different() { fn verify_multiple_transfers_same() { const AMOUNT: u128 = 100; - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default() .with_balances(vec![(alice_addr_32.clone(), 10_000)]) @@ -142,7 +140,7 @@ fn verify_multiple_transfers_same() { let ini_blnc_bob = Balances::free_balance(&bob_addr_32); // Now check that it works from within the MoveVM. - let script = assets::read_script_from_project("balance", "double_transfer"); + let script = utils::read_script_from_project("balance", "double_transfer"); let transaction_bc = script_transaction!( script, @@ -172,15 +170,15 @@ fn verify_multiple_transfers_same() { fn verify_balance_limit_too_low() { const AMOUNT: BalanceOf = 100; - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); - let (_, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); + let (_, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default() .with_balances(vec![(alice_addr_32.clone(), 10_000)]) .build() .execute_with(|| { // Now check that it works from within the MoveVM. - let script = assets::read_script_from_project("balance", "single_transfer"); + let script = utils::read_script_from_project("balance", "single_transfer"); let transaction_bc = script_transaction!( script, @@ -204,11 +202,11 @@ fn verify_balance_limit_too_low() { fn verify_insufficient_balance() { const AMOUNT: BalanceOf = 100; - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); - let (_, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); + let (_, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { - let script = assets::read_script_from_project("balance", "single_transfer"); + let script = utils::read_script_from_project("balance", "single_transfer"); let transaction_bc = script_transaction!( script, @@ -233,8 +231,8 @@ fn verify_move_script_fails_after_successful_transfer() { const BALANCE: BalanceOf = 1000; const AMOUNT: BalanceOf = 100; - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default() .with_balances(vec![ @@ -244,7 +242,7 @@ fn verify_move_script_fails_after_successful_transfer() { .build() .execute_with(|| { // Execute script with a successful transfer but which fails after transfer. - let script = assets::read_script_from_project("balance", "fail_at_the_end"); + let script = utils::read_script_from_project("balance", "fail_at_the_end"); let transaction_bc = script_transaction!( script, @@ -275,13 +273,13 @@ fn verify_move_script_fails_after_successful_transfer() { fn verify_self_transfer() { const AMOUNT: BalanceOf = 100; - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); ExtBuilder::default() .with_balances(vec![(alice_addr_32.clone(), AMOUNT * 2)]) .build() .execute_with(|| { - let script = assets::read_script_from_project("balance", "single_transfer"); + let script = utils::read_script_from_project("balance", "single_transfer"); let transaction_bc = script_transaction!( script, @@ -308,13 +306,13 @@ fn verify_self_transfer_trying_to_cheat() { const AMOUNT: BalanceOf = 1000; const BALANCE: BalanceOf = 100; - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); ExtBuilder::default() .with_balances(vec![(alice_addr_32.clone(), BALANCE)]) .build() .execute_with(|| { - let script = assets::read_script_from_project("balance", "single_transfer"); + let script = utils::read_script_from_project("balance", "single_transfer"); let transaction_bc = script_transaction!( script, diff --git a/pallet/src/tests/example.rs b/pallet/src/tests/example.rs index 4d51b08..1879089 100644 --- a/pallet/src/tests/example.rs +++ b/pallet/src/tests/example.rs @@ -1,4 +1,4 @@ -use crate::{assets, mock::*, no_type_args, script_transaction}; +use crate::{mock::*, mock_utils as utils, no_type_args, script_transaction}; use frame_support::assert_ok; use move_vm_backend::types::MAX_GAS_AMOUNT; @@ -8,8 +8,8 @@ const PROJECT: &str = "car-wash-example"; /// Test the regular, ideal flow of our example project. #[test] fn verify_normal_use_case() { - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default() .with_balances(vec![(alice_addr_32.clone(), 10_000_000_000_000)]) @@ -20,7 +20,7 @@ fn verify_normal_use_case() { let ini_blnc_bob = Balances::free_balance(&bob_addr_32); // Let's publish Bob's module CarWash. - let module_bc = assets::read_module_from_project(PROJECT, "CarWash"); + let module_bc = utils::read_module_from_project(PROJECT, "CarWash"); assert_ok!(MoveModule::publish_module( RuntimeOrigin::signed(bob_addr_32.clone()), module_bc, @@ -28,7 +28,7 @@ fn verify_normal_use_case() { )); // Now Bob initialises his module. - let script = assets::read_script_from_project(PROJECT, "initial_coin_minting"); + let script = utils::read_script_from_project(PROJECT, "initial_coin_minting"); let transaction_bc = script_transaction!(script, no_type_args!(), &bob_addr_mv); assert_ok!(MoveModule::execute( RuntimeOrigin::signed(bob_addr_32.clone()), @@ -38,7 +38,7 @@ fn verify_normal_use_case() { )); // Now Alice comes over to wash her car for the first time... - let script = assets::read_script_from_project(PROJECT, "register_new_user"); + let script = utils::read_script_from_project(PROJECT, "register_new_user"); let transaction_bc = script_transaction!(script, no_type_args!(), &alice_addr_mv); assert_ok!(MoveModule::execute( RuntimeOrigin::signed(alice_addr_32.clone()), @@ -47,7 +47,7 @@ fn verify_normal_use_case() { 0, )); - let script = assets::read_script_from_project(PROJECT, "buy_coin"); + let script = utils::read_script_from_project(PROJECT, "buy_coin"); let transaction_bc = script_transaction!(script, no_type_args!(), &alice_addr_mv, &1u8); assert_ok!(MoveModule::execute( RuntimeOrigin::signed(alice_addr_32.clone()), @@ -57,7 +57,7 @@ fn verify_normal_use_case() { )); // let script_bc = script_bytecode("wash_car", alice_addr_mv); - let script = assets::read_script_from_project(PROJECT, "wash_car"); + let script = utils::read_script_from_project(PROJECT, "wash_car"); let transaction_bc = script_transaction!(script, no_type_args!(), &alice_addr_mv); assert_ok!(MoveModule::execute( RuntimeOrigin::signed(alice_addr_32.clone()), diff --git a/pallet/src/tests/execute.rs b/pallet/src/tests/execute.rs index 33a6cb0..2f10357 100644 --- a/pallet/src/tests/execute.rs +++ b/pallet/src/tests/execute.rs @@ -1,4 +1,4 @@ -use crate::{assets, mock::*, no_type_args, script_transaction, Error}; +use crate::{mock::*, mock_utils as utils, no_type_args, script_transaction, Error}; use frame_support::{assert_err, assert_ok}; use move_core_types::{identifier::Identifier, language_storage::StructTag}; @@ -25,10 +25,10 @@ fn get_vm_resource( /// Test execution of a script. #[test] fn execute_script_empty() { - let addr_native = addr32_from_ss58(CAFE_ADDR).unwrap(); + let addr_native = utils::account::(utils::CAFE_ADDR); ExtBuilder::default().build().execute_with(|| { - let script = assets::read_script_from_project("move-basics", "empty_scr"); + let script = utils::read_script_from_project("move-basics", "empty_scr"); let transaction_bc = script_transaction!(script, no_type_args!()); @@ -41,7 +41,7 @@ fn execute_script_empty() { assert_ok!(res); - let script = assets::read_script_from_project("move-basics", "empty_loop"); + let script = utils::read_script_from_project("move-basics", "empty_loop"); let transaction_bc = script_transaction!(script, no_type_args!()); @@ -59,10 +59,10 @@ fn execute_script_empty() { /// Test execution of a script with parametrized function. #[test] fn execute_script_params() { - let addr_native = addr32_from_ss58(CAFE_ADDR).unwrap(); + let addr_native = utils::account::(utils::CAFE_ADDR); ExtBuilder::default().build().execute_with(|| { - let script = assets::read_script_from_project("move-basics", "empty_loop_param"); + let script = utils::read_script_from_project("move-basics", "empty_loop_param"); let transaction_bc = script_transaction!(script, no_type_args!(), &10u64); @@ -81,10 +81,10 @@ fn execute_script_params() { /// are not allowed. #[test] fn execute_script_generic_fails() { - let addr_native = addr32_from_ss58(CAFE_ADDR).unwrap(); + let addr_native = utils::account::(utils::CAFE_ADDR); ExtBuilder::default().build().execute_with(|| { - let script = assets::read_script_from_project("move-basics", "generic_1"); + let script = utils::read_script_from_project("move-basics", "generic_1"); let transaction_bc = script_transaction!(script, no_type_args!(), &100u64); @@ -102,12 +102,12 @@ fn execute_script_generic_fails() { /// Test execution of a script with correct parameters which stores something inside the storage. #[test] fn execute_script_storage_correct() { - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); ExtBuilder::default().build().execute_with(|| { // Bob publishes the move-module 'Counter', test preparation. - let module = assets::read_module_from_project("get-resource", "Counter"); + let module = utils::read_module_from_project("get-resource", "Counter"); assert_ok!(MoveModule::publish_module( RuntimeOrigin::signed(bob_addr_32.clone()), module, @@ -127,7 +127,7 @@ fn execute_script_storage_correct() { ); // Alice and Bob execute a script to create a counter using the move-module 'Counter'. - let script = assets::read_script_from_project("get-resource", "create_counter"); + let script = utils::read_script_from_project("get-resource", "create_counter"); let transaction_bc = script_transaction!(script.clone(), no_type_args!(), &alice_addr_mv); assert_ok!(MoveModule::execute( RuntimeOrigin::signed(alice_addr_32.clone()), @@ -156,7 +156,7 @@ fn execute_script_storage_correct() { assert_eq!(counter, vec![0, 0, 0, 0, 0, 0, 0, 0]); // Execute script that counts that created counter, but only for Alice. - let script = assets::read_script_from_project("get-resource", "count"); + let script = utils::read_script_from_project("get-resource", "count"); let transaction_bc = script_transaction!(script.clone(), no_type_args!(), &alice_addr_mv); assert_ok!(MoveModule::execute( RuntimeOrigin::signed(alice_addr_32.clone()), @@ -181,11 +181,11 @@ fn execute_script_storage_correct() { /// Test execution of a script with correct parameters but with insufficient gas. #[test] fn execute_script_insufficient_gas() { - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { // Bob publishes the move-module 'Counter', test preparation. - let module = assets::read_module_from_project("get-resource", "Counter"); + let module = utils::read_module_from_project("get-resource", "Counter"); assert_ok!(MoveModule::publish_module( RuntimeOrigin::signed(bob_addr_32.clone()), module, @@ -193,7 +193,7 @@ fn execute_script_insufficient_gas() { )); // Bob wants to execute a script which shall trigger that module but with too little gas. - let script = assets::read_script_from_project("get-resource", "create_counter"); + let script = utils::read_script_from_project("get-resource", "create_counter"); let transaction_bc = script_transaction!(script.clone(), no_type_args!(), &bob_addr_mv); assert!(MoveModule::execute( RuntimeOrigin::signed(bob_addr_32.clone()), @@ -208,11 +208,11 @@ fn execute_script_insufficient_gas() { /// Test execution of a script with corrupted bytecode. #[test] fn execute_script_corrupted_bytecode() { - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { // Bob publishes the move-module 'Counter', test preparation. - let module = assets::read_module_from_project("get-resource", "Counter"); + let module = utils::read_module_from_project("get-resource", "Counter"); assert_ok!(MoveModule::publish_module( RuntimeOrigin::signed(bob_addr_32.clone()), module, @@ -220,7 +220,7 @@ fn execute_script_corrupted_bytecode() { )); // Bob executes a corrupted script. - let script = assets::read_script_from_project("get-resource", "create_counter"); + let script = utils::read_script_from_project("get-resource", "create_counter"); let mut transaction_bc = script_transaction!(script.clone(), no_type_args!(), &bob_addr_mv); transaction_bc[10] += 1; assert!(MoveModule::execute( diff --git a/pallet/src/tests/modules.rs b/pallet/src/tests/modules.rs index f278216..876b20f 100644 --- a/pallet/src/tests/modules.rs +++ b/pallet/src/tests/modules.rs @@ -1,4 +1,4 @@ -use crate::{assets, mock::*}; +use crate::{mock::*, mock_utils as utils}; use frame_support::assert_ok; use move_core_types::{identifier::Identifier, language_storage::StructTag}; @@ -7,11 +7,11 @@ use move_vm_backend::types::MAX_GAS_AMOUNT; /// Test getting a module. #[test] fn get_module_correct() { - let addr_native = addr32_from_ss58(CAFE_ADDR).unwrap(); + let addr_native = utils::account::(utils::CAFE_ADDR); ExtBuilder::default().build().execute_with(|| { let module_name = "Empty"; - let module = assets::read_module_from_project("move-basics", module_name); + let module = utils::read_module_from_project("move-basics", module_name); let res = MoveModule::publish_module( RuntimeOrigin::signed(addr_native.clone()), @@ -30,7 +30,7 @@ fn get_module_correct() { /// Test getting a module that does not exist. #[test] fn get_module_nonexistent() { - let addr_native = addr32_from_ss58(CAFE_ADDR).unwrap(); + let addr_native = utils::account::(utils::CAFE_ADDR); ExtBuilder::default().build().execute_with(|| { let res = MoveModule::get_module(&addr_native, "Empty"); @@ -42,12 +42,12 @@ fn get_module_nonexistent() { /// Test getting resource from the module. #[test] fn get_resource_non_existent() { - let (_, addr) = addrs_from_ss58(CAFE_ADDR).unwrap(); + let (_, addr) = utils::account_n_address::(utils::CAFE_ADDR); ExtBuilder::default().build().execute_with(|| { let addr_native = MoveModule::to_native_account(&addr).unwrap(); - let module = assets::read_module_from_project("move-basics", "Empty"); + let module = utils::read_module_from_project("move-basics", "Empty"); let res = MoveModule::publish_module( RuntimeOrigin::signed(addr_native.clone()), diff --git a/pallet/src/tests/publish.rs b/pallet/src/tests/publish.rs index 0825117..9d9885d 100644 --- a/pallet/src/tests/publish.rs +++ b/pallet/src/tests/publish.rs @@ -1,4 +1,4 @@ -use crate::{assets, mock::*, GasStrategy}; +use crate::{mock::*, mock_utils as utils, GasStrategy}; use frame_support::assert_ok; use move_vm_backend::types::MAX_GAS_AMOUNT; @@ -6,11 +6,11 @@ use move_vm_backend::types::MAX_GAS_AMOUNT; /// Test that the module is published correctly. #[test] fn publish_module_as_user_correct() { - let cafe_addr_native = addr32_from_ss58(CAFE_ADDR).unwrap(); - let bob_addr_native = addr32_from_ss58(BOB_ADDR).unwrap(); + let cafe_addr_native = utils::account::(utils::CAFE_ADDR); + let bob_addr_native = utils::account::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { - let module = assets::read_module_from_project("move-basics", "Empty"); + let module = utils::read_module_from_project("move-basics", "Empty"); let res = MoveModule::publish_module( RuntimeOrigin::signed(cafe_addr_native), @@ -19,7 +19,7 @@ fn publish_module_as_user_correct() { ); assert_ok!(res); - let module = assets::read_module_from_project("move-basics", "EmptyBob"); + let module = utils::read_module_from_project("move-basics", "EmptyBob"); let res = MoveModule::publish_module( RuntimeOrigin::signed(bob_addr_native), @@ -33,10 +33,10 @@ fn publish_module_as_user_correct() { /// Test that the module is not published if the user is not the owner. #[test] fn publish_module_as_user_wrong_user() { - let bob_addr_native = addr32_from_ss58(BOB_ADDR).unwrap(); + let bob_addr_native = utils::account::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { - let module = assets::read_module_from_project("move-basics", "Empty"); + let module = utils::read_module_from_project("move-basics", "Empty"); let res = MoveModule::publish_module( RuntimeOrigin::signed(bob_addr_native), @@ -50,10 +50,10 @@ fn publish_module_as_user_wrong_user() { /// Test that the module is not published if the user does not have enough gas. #[test] fn publish_module_as_user_insufficient_gas() { - let cafe_addr_native = addr32_from_ss58(CAFE_ADDR).unwrap(); + let cafe_addr_native = utils::account::(utils::CAFE_ADDR); ExtBuilder::default().build().execute_with(|| { - let module = assets::read_module_from_project("move-basics", "Empty"); + let module = utils::read_module_from_project("move-basics", "Empty"); let gas_limit = 1; let res = @@ -65,10 +65,10 @@ fn publish_module_as_user_insufficient_gas() { /// Test that the module is not published if the bytecode is corrupted. #[test] fn publish_module_as_user_corrupted_bytecode() { - let cafe_addr_native = addr32_from_ss58(CAFE_ADDR).unwrap(); + let cafe_addr_native = utils::account::(utils::CAFE_ADDR); ExtBuilder::default().build().execute_with(|| { - let mut module = assets::read_module_from_project("move-basics", "Empty"); + let mut module = utils::read_module_from_project("move-basics", "Empty"); // This should be enough to corrupt the bytecode. for i in 0..(module.len() / 4) { @@ -87,11 +87,11 @@ fn publish_module_as_user_corrupted_bytecode() { /// Test that the bundle is published correctly. #[test] fn publish_bundle_as_user_correct() { - let bob_addr_native = addr32_from_ss58(BOB_ADDR).unwrap(); + let bob_addr_native = utils::account::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { let bundle = - assets::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); + utils::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); let res = MoveModule::publish_module_bundle( RuntimeOrigin::signed(bob_addr_native), @@ -106,11 +106,11 @@ fn publish_bundle_as_user_correct() { /// Test that the bundle is not published if the user is not the owner. #[test] fn publish_bundle_as_user_wrong_user() { - let cafe_addr_native = addr32_from_ss58(CAFE_ADDR).unwrap(); + let cafe_addr_native = utils::account::(utils::CAFE_ADDR); ExtBuilder::default().build().execute_with(|| { let bundle = - assets::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); + utils::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); let res = MoveModule::publish_module_bundle( RuntimeOrigin::signed(cafe_addr_native), @@ -125,11 +125,11 @@ fn publish_bundle_as_user_wrong_user() { /// Test that the bundle is not published if the user does not have enough gas. #[test] fn publish_bundle_as_user_insufficient_gas() { - let bob_addr_native = addr32_from_ss58(BOB_ADDR).unwrap(); + let bob_addr_native = utils::account::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { let bundle = - assets::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); + utils::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); let gas_limit = 1; let res = MoveModule::publish_module_bundle( @@ -145,11 +145,11 @@ fn publish_bundle_as_user_insufficient_gas() { /// Test that the bundle is not published if the bytecode is corrupted. #[test] fn publish_bundle_as_user_corrupted_bytecode() { - let cafe_addr_native = addr32_from_ss58(CAFE_ADDR).unwrap(); + let cafe_addr_native = utils::account::(utils::CAFE_ADDR); ExtBuilder::default().build().execute_with(|| { let mut bundle = - assets::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); + utils::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); // This should be enough to corrupt the bytecode. for i in 0..(bundle.len() / 4) { @@ -168,10 +168,10 @@ fn publish_bundle_as_user_corrupted_bytecode() { /// Test that the module is published correctly when the gas is estimated. #[test] fn raw_publish_module_dry_run() { - let (bob_addr_native, bob_addr_move) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_native, bob_addr_move) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { - let module = assets::read_module_from_project("using_stdlib_natives", "Vector"); + let module = utils::read_module_from_project("using_stdlib_natives", "Vector"); let estimation = MoveModule::raw_publish_module(&bob_addr_move, module.clone(), GasStrategy::DryRun) @@ -198,11 +198,11 @@ fn raw_publish_module_dry_run() { /// Test that the bundle is published correctly when the gas is estimated. #[test] fn raw_publish_bundle_dry_run() { - let (bob_addr_native, bob_addr_move) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_native, bob_addr_move) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { let bundle = - assets::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); + utils::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); let estimation = MoveModule::raw_publish_bundle(&bob_addr_move, bundle.clone(), GasStrategy::DryRun) @@ -233,10 +233,10 @@ fn raw_publish_bundle_dry_run() { /// Test that the module publishing fails when gas is exceeded. #[test] fn publish_module_will_fail_in_case_the_gas_limit_is_exceeded() { - let bob_addr_native = addr32_from_ss58(BOB_ADDR).unwrap(); + let bob_addr_native = utils::account::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { - let module = assets::read_module_from_project("using_stdlib_natives", "Vector"); + let module = utils::read_module_from_project("using_stdlib_natives", "Vector"); // Exceed the maximum gas limit by one. let result = MoveModule::publish_module( @@ -254,11 +254,11 @@ fn publish_module_will_fail_in_case_the_gas_limit_is_exceeded() { /// Test that the bundle publishing fails when gas is exceeded. #[test] fn publish_bundle_will_fail_in_case_the_gas_limit_is_exceeded() { - let bob_addr_native = addr32_from_ss58(BOB_ADDR).unwrap(); + let bob_addr_native = utils::account::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { let bundle = - assets::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); + utils::read_bundle_from_project("using_stdlib_natives", "using_stdlib_natives"); // Exceed the maximum gas limit by one. let result = MoveModule::publish_module_bundle( diff --git a/pallet/src/tests/signer.rs b/pallet/src/tests/signer.rs index acee7e2..2e519c0 100644 --- a/pallet/src/tests/signer.rs +++ b/pallet/src/tests/signer.rs @@ -1,4 +1,6 @@ -use crate::{assets, mock::*, no_type_args, script_transaction, Error, Event, MultisigStorage}; +use crate::{ + mock::*, mock_utils as utils, no_type_args, script_transaction, Error, Event, MultisigStorage, +}; use frame_support::{ assert_err, assert_ok, @@ -112,10 +114,10 @@ fn general_script_no_params_works() { // Roll to first block in case of block based event checkings and processes. roll_to(1); - let (bob_addr_32, _) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_32, _) = utils::account_n_address::(utils::BOB_ADDR); // no_param_at_all() - let script = assets::read_script_from_project("signer-scripts", "no_param_at_all"); + let script = utils::read_script_from_project("signer-scripts", "no_param_at_all"); let type_args: Vec = vec![]; let params: Vec<&[u8]> = vec![]; assert_ok!(execute_script(&bob_addr_32, script, params, type_args)); @@ -136,10 +138,10 @@ fn general_script_no_signers_param_at_all_works() { roll_to(1); let mut pg = ParamGenerator::new(); - let (bob_addr_32, _) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_32, _) = utils::account_n_address::(utils::BOB_ADDR); // no_signers_param_at_all(iterations: u64, _a: u32, _b: u8, _c: u256, _d: address, _e: vector, _f: bool) - let script = assets::read_script_from_project("signer-scripts", "no_signers_param_at_all"); + let script = utils::read_script_from_project("signer-scripts", "no_signers_param_at_all"); let type_args: Vec = vec![]; let iter = pg.rand::(); @@ -164,8 +166,8 @@ fn general_script_no_signers_param_at_all_works() { /// Script with a single signer fails if executed by the wrong user. #[test] fn script_with_single_signer_fails_if_executed_by_the_wrong_user() { - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); - let (eve_addr_32, _) = addrs_from_ss58(EVE_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); + let (eve_addr_32, _) = utils::account_n_address::(utils::EVE_ADDR); const BALANCE_UNUSED: Balance = 0; ExtBuilder::default().build().execute_with(|| { @@ -175,7 +177,7 @@ fn script_with_single_signer_fails_if_executed_by_the_wrong_user() { // We are using this script below here, but any script with a single signer could have been used here. // trying_with_signer_reference(_ref: &signer) let script = - assets::read_script_from_project("signer-scripts", "trying_with_signer_reference"); + utils::read_script_from_project("signer-scripts", "trying_with_signer_reference"); let transaction_bc = script_transaction!(script, no_type_args!(), &bob_addr_mv); // Eve cannot execute a script which requires signers, when Eve is not in the signer list. @@ -206,7 +208,7 @@ fn script_with_single_signer_fails_if_executed_by_the_wrong_user() { /// Script with many signers parameters executes correctly when all signers are signed by one account. #[test] fn general_script_eight_normal_signers_works() { - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default() .with_balances(vec![(bob_addr_32.clone(), EXISTENTIAL_DEPOSIT)]) @@ -217,7 +219,7 @@ fn general_script_eight_normal_signers_works() { // eight_normal_signers(_s1: signer, _s2: signer, _s3: &signer, _s4: signer, _s5: &signer, // _s6: signer, _s7: &signer, _s8: &signer, _extra: u32) - let script = assets::read_script_from_project("signer-scripts", "eight_normal_signers"); + let script = utils::read_script_from_project("signer-scripts", "eight_normal_signers"); let type_args: Vec = vec![]; let mut pg = ParamGenerator::new(); @@ -249,12 +251,12 @@ fn eve_cant_execute_multisig_script_without_other_signers_works() { let mut pg = ParamGenerator::new(); // Eve is basically Bob here, but since Bob is pretending to be bad, we'll rename him. - let (eve_addr_32, eve_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); + let (eve_addr_32, eve_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); // eight_normal_signers(_s1: signer, _s2: signer, _s3: &signer, _s4: signer, _s5: &signer, // _s6: signer, _s7: &signer, _s8: &signer, _extra: u32) - let script = assets::read_script_from_project("signer-scripts", "eight_normal_signers"); + let script = utils::read_script_from_project("signer-scripts", "eight_normal_signers"); let type_args: Vec = vec![]; let alice = pg.address(&alice_addr_mv); @@ -309,15 +311,13 @@ fn signer_before_all_possible_vectors_works() { roll_to(1); let mut pg = ParamGenerator::new(); - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); // signer_before_all_possible_vectors(_s: signer, _a: vector, _b: vector, _c: // vector, _e: vector, _f: vector, _g: vector, _h: vector
, // _i: vector) - let script = assets::read_script_from_project( - "signer-scripts", - "signer_before_all_possible_vectors", - ); + let script = + utils::read_script_from_project("signer-scripts", "signer_before_all_possible_vectors"); let type_args: Vec = vec![]; let bob = pg.address(&bob_addr_mv); @@ -348,13 +348,13 @@ fn signer_before_all_possible_vectors_works() { fn signer_after_all_possible_vectors_fails() { ExtBuilder::default().build().execute_with(|| { let mut pg = ParamGenerator::new(); - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); // signer_after_all_possible_vectors(_a: vector, _b: vector, _c: vector, _e: // vector, _f: vector, _g: vector, _h: vector
, _i: vector, // _s: &signer) let script = - assets::read_script_from_project("signer-scripts", "signer_after_all_possible_vectors"); + utils::read_script_from_project("signer-scripts", "signer_after_all_possible_vectors"); let type_args: Vec = vec![]; let v_u8 = pg.rand_vec_with_len::(1); @@ -380,11 +380,11 @@ fn signer_after_all_possible_vectors_fails() { fn script_with_vector_containing_signer_fails() { ExtBuilder::default().build().execute_with(|| { let pg = ParamGenerator::new(); - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); // trying_vector_containing_signer(_v: vector) let script = - assets::read_script_from_project("signer-scripts", "trying_vector_containing_signer"); + utils::read_script_from_project("signer-scripts", "trying_vector_containing_signer"); let type_args: Vec = vec![]; let v_addr = pg.address_vec(vec![&bob_addr_mv]); @@ -399,10 +399,10 @@ fn script_with_vector_containing_signer_fails() { fn multiple_signers_in_multisig_script_works() { const BALANCE: Balance = 80_000_000_000_000; const CHANGE: Balance = 20_000_000_000_000; - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); - let (dave_addr_32, dave_addr_mv) = addrs_from_ss58(DAVE_ADDR).unwrap(); - let (eve_addr_32, eve_addr_mv) = addrs_from_ss58(EVE_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); + let (dave_addr_32, dave_addr_mv) = utils::account_n_address::(utils::DAVE_ADDR); + let (eve_addr_32, eve_addr_mv) = utils::account_n_address::(utils::EVE_ADDR); ExtBuilder::default() .with_balances(vec![ @@ -418,7 +418,7 @@ fn multiple_signers_in_multisig_script_works() { roll_to(block_no_1); // Initialisation & Setup by developer Bob. - let module = assets::read_module_from_project("multiple-signers", "Dorm"); + let module = utils::read_module_from_project("multiple-signers", "Dorm"); assert_ok!(MoveModule::publish_module( RuntimeOrigin::signed(bob_addr_32.clone()), module, @@ -431,7 +431,7 @@ fn multiple_signers_in_multisig_script_works() { }) ); - let script = assets::read_script_from_project("multiple-signers", "init_module"); + let script = utils::read_script_from_project("multiple-signers", "init_module"); let transaction_bc = script_transaction!(script, no_type_args!(), &bob_addr_mv); assert_ok!(MoveModule::execute( RuntimeOrigin::signed(bob_addr_32.clone()), @@ -447,7 +447,7 @@ fn multiple_signers_in_multisig_script_works() { ); // Now our three tenants want to rent the 3-room apartment. - let script = assets::read_script_from_project("multiple-signers", "rent_apartment"); + let script = utils::read_script_from_project("multiple-signers", "rent_apartment"); let transaction_bc = script_transaction!( script, no_type_args!(), @@ -552,10 +552,10 @@ fn multiple_signers_in_multisig_script_works() { #[test] fn verify_old_multi_signer_requests_getting_removed() { const BALANCE: Balance = 80_000_000_000_000; - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); - let (dave_addr_32, dave_addr_mv) = addrs_from_ss58(DAVE_ADDR).unwrap(); - let (eve_addr_32, eve_addr_mv) = addrs_from_ss58(EVE_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); + let (dave_addr_32, dave_addr_mv) = utils::account_n_address::(utils::DAVE_ADDR); + let (eve_addr_32, eve_addr_mv) = utils::account_n_address::(utils::EVE_ADDR); ExtBuilder::default() .with_balances(vec![ @@ -570,13 +570,13 @@ fn verify_old_multi_signer_requests_getting_removed() { roll_to(1); // Initialisation & Setup by developer Bob. - let module = assets::read_module_from_project("multiple-signers", "Dorm"); + let module = utils::read_module_from_project("multiple-signers", "Dorm"); assert_ok!(MoveModule::publish_module( RuntimeOrigin::signed(bob_addr_32.clone()), module, MAX_GAS_AMOUNT )); - let script = assets::read_script_from_project("multiple-signers", "init_module"); + let script = utils::read_script_from_project("multiple-signers", "init_module"); let transaction_bc = script_transaction!(script, no_type_args!(), &bob_addr_mv); assert_ok!(MoveModule::execute( RuntimeOrigin::signed(bob_addr_32.clone()), @@ -586,7 +586,7 @@ fn verify_old_multi_signer_requests_getting_removed() { )); // Now only 2 of 3 planned signers will sign the script execution. - let script = assets::read_script_from_project("multiple-signers", "rent_apartment"); + let script = utils::read_script_from_project("multiple-signers", "rent_apartment"); let transaction_bc = script_transaction!( script, // script bytecode no_type_args!(), // no generic arguments @@ -646,10 +646,10 @@ fn verify_old_multi_signer_requests_getting_removed() { #[test] fn insufficient_cheque_limit_aborts_the_multisig_script_works() { const BALANCE: Balance = 80_000_000_000_000; - let (bob_addr_32, bob_addr_mv) = addrs_from_ss58(BOB_ADDR).unwrap(); - let (alice_addr_32, alice_addr_mv) = addrs_from_ss58(ALICE_ADDR).unwrap(); - let (dave_addr_32, dave_addr_mv) = addrs_from_ss58(DAVE_ADDR).unwrap(); - let (eve_addr_32, eve_addr_mv) = addrs_from_ss58(EVE_ADDR).unwrap(); + let (bob_addr_32, bob_addr_mv) = utils::account_n_address::(utils::BOB_ADDR); + let (alice_addr_32, alice_addr_mv) = utils::account_n_address::(utils::ALICE_ADDR); + let (dave_addr_32, dave_addr_mv) = utils::account_n_address::(utils::DAVE_ADDR); + let (eve_addr_32, eve_addr_mv) = utils::account_n_address::(utils::EVE_ADDR); ExtBuilder::default() .with_balances(vec![ @@ -664,13 +664,13 @@ fn insufficient_cheque_limit_aborts_the_multisig_script_works() { roll_to(1); // Initialisation & Setup by developer Bob. - let module = assets::read_module_from_project("multiple-signers", "Dorm"); + let module = utils::read_module_from_project("multiple-signers", "Dorm"); assert_ok!(MoveModule::publish_module( RuntimeOrigin::signed(bob_addr_32.clone()), module, MAX_GAS_AMOUNT )); - let script = assets::read_script_from_project("multiple-signers", "init_module"); + let script = utils::read_script_from_project("multiple-signers", "init_module"); let transaction_bc = script_transaction!(script, no_type_args!(), &bob_addr_mv); assert_ok!(MoveModule::execute( RuntimeOrigin::signed(bob_addr_32.clone()), @@ -680,7 +680,7 @@ fn insufficient_cheque_limit_aborts_the_multisig_script_works() { )); // Now only 2 of 3 planned signers will sign the script execution. - let script = assets::read_script_from_project("multiple-signers", "rent_apartment"); + let script = utils::read_script_from_project("multiple-signers", "rent_apartment"); let transaction_bc = script_transaction!( script, // script bytecode no_type_args!(), // no generic arguments diff --git a/pallet/src/tests/update_stdlib.rs b/pallet/src/tests/update_stdlib.rs index 755baf9..7f19491 100644 --- a/pallet/src/tests/update_stdlib.rs +++ b/pallet/src/tests/update_stdlib.rs @@ -1,22 +1,22 @@ //! Integration tests related to extrinsic call `update_stdlib`. -use crate::{assets, mock::*, no_type_args, script_transaction}; +use crate::{mock::*, mock_utils as utils, no_type_args, script_transaction}; use frame_support::{assert_err, assert_ok, pallet_prelude::*}; use move_stdlib::move_stdlib_bundle; use move_vm_backend::types::MAX_GAS_AMOUNT; fn mock_move_stdlib() -> Vec { - assets::read_bundle_from_project("testing-move-stdlib", "testing-move-stdlib") + utils::read_bundle_from_project("testing-move-stdlib", "testing-move-stdlib") } fn mock_substrate_stdlib() -> Vec { - assets::read_bundle_from_project("testing-substrate-stdlib", "testing-substrate-stdlib") + utils::read_bundle_from_project("testing-substrate-stdlib", "testing-substrate-stdlib") } #[test] fn regular_user_update_fail() { - let bob_addr_native = addr32_from_ss58(BOB_ADDR).unwrap(); + let bob_addr_native = utils::account::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { assert_err!( @@ -62,11 +62,11 @@ fn change_stdlib_api_remove_param_fail() { #[test] fn add_new_methods_or_update_methods_works() { - let (bob_addr_native, bob_addr_move) = addrs_from_ss58(BOB_ADDR).unwrap(); + let (bob_addr_native, bob_addr_move) = utils::account_n_address::(utils::BOB_ADDR); ExtBuilder::default().build().execute_with(|| { // Publish some module to fitting interface. - let car_wash_module = assets::read_module_from_project("car-wash-example", "CarWash"); + let car_wash_module = utils::read_module_from_project("car-wash-example", "CarWash"); assert_ok!(MoveModule::publish_module( RuntimeOrigin::signed(bob_addr_native.clone()), car_wash_module.clone(), @@ -80,7 +80,7 @@ fn add_new_methods_or_update_methods_works() { )); // Test module is still working in its bounds. - let script = assets::read_script_from_project("car-wash-example", "initial_coin_minting"); + let script = utils::read_script_from_project("car-wash-example", "initial_coin_minting"); let transaction_bc = script_transaction!(script, no_type_args!(), &bob_addr_move); assert_ok!(MoveModule::execute( RuntimeOrigin::signed(bob_addr_native),