Skip to content

Commit

Permalink
feat: optimise structuring and common used dev-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoks committed Apr 18, 2024
1 parent 3180918 commit 11c5f71
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 337 deletions.
66 changes: 0 additions & 66 deletions pallet/src/assets.rs

This file was deleted.

125 changes: 16 additions & 109 deletions pallet/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
//! 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};
pub 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";

benchmarks! {
where_clause { where
T: Config + SysConfig,
T::AccountId: From<Public>,
T::AccountId: Ss58Codec,
}

execute {
let n in 0 .. 7;

let (bob_32, bob_mv) = account_address::<T>(BOB_ADDR);
let (alice_32, alice_mv) = account_address::<T>(ALICE_ADDR);
let (dave_32, dave_mv) = account_address::<T>(DAVE_ADDR);
let (eve_32, eve_mv) = account_address::<T>(EVE_ADDR);
let (bob_32, bob_mv) = account_n_address::<T>(BOB_ADDR);
let (alice_32, alice_mv) = account_n_address::<T>(ALICE_ADDR);
let (dave_32, dave_mv) = account_n_address::<T>(DAVE_ADDR);
let (eve_32, eve_mv) = account_n_address::<T>(EVE_ADDR);

// Our benchmark plan (each is a test scenario with different parameters).
let script_bcs = [
Expand Down Expand Up @@ -108,8 +102,7 @@ benchmarks! {
publish_module {
let n in 0 .. 3;

let bob_32: T::AccountId = Public::from_ss58check(BOB_ADDR).unwrap().into();
let bob_mv = Pallet::<T>::to_move_address(&bob_32).unwrap();
let (bob_32, bob_mv) = account_n_address::<T>(BOB_ADDR);

let module_bcs = [
move_basics_module(),
Expand All @@ -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::<T>(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)

Expand All @@ -138,109 +131,23 @@ impl_benchmark_test_suite!(
crate::mock::ExtBuilder::default()
.with_balances(vec![
(
crate::benchmarking::account::<crate::mock::Test>(crate::benchmarking::BOB_ADDR),
crate::mock_utils::account::<crate::mock::Test>(crate::mock_utils::BOB_ADDR),
u128::MAX
),
(
crate::benchmarking::account::<crate::mock::Test>(crate::benchmarking::ALICE_ADDR),
crate::mock_utils::account::<crate::mock::Test>(crate::mock_utils::ALICE_ADDR),
u128::MAX
),
(
crate::benchmarking::account::<crate::mock::Test>(crate::benchmarking::DAVE_ADDR),
crate::mock_utils::account::<crate::mock::Test>(crate::mock_utils::DAVE_ADDR),
u128::MAX
),
(
crate::benchmarking::account::<crate::mock::Test>(crate::benchmarking::EVE_ADDR),
crate::mock_utils::account::<crate::mock::Test>(crate::mock_utils::EVE_ADDR),
u128::MAX
),
])
.build(),
crate::mock::Test
);

#[cfg(test)]
fn account<T: SysConfig + Config>(name: &str) -> T::AccountId
where
T::AccountId: From<Public>,
{
Public::from_ss58check(name).unwrap().into()
}

fn account_address<T: SysConfig + Config>(name: &str) -> (T::AccountId, AccountAddress)
where
T::AccountId: From<Public>,
{
let account: T::AccountId = Public::from_ss58check(name).unwrap().into();
let address = Pallet::<T>::to_move_address(&account).unwrap();
(account, address)
}

// Move Basics Example
fn move_basics_module() -> Vec<u8> {
core::include_bytes!(
"assets/move-projects/move-basics/build/move-basics/bytecode_modules/EmptyBob.mv"
)
.to_vec()
}

// Using Stdlib Natives Example
fn using_stdlib_natives_module() -> Vec<u8> {
core::include_bytes!("assets/move-projects/using_stdlib_natives/build/using_stdlib_natives/bytecode_modules/Vector.mv").to_vec()
}

// Car Wash Example
fn car_wash_example_module() -> Vec<u8> {
core::include_bytes!(
"assets/move-projects/car-wash-example/build/car-wash-example/bytecode_modules/CarWash.mv"
)
.to_vec()
}

fn car_wash_initial_coin_miniting(addr: &AccountAddress) -> Vec<u8> {
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)
}

fn car_wash_register_new_user(addr: &AccountAddress) -> Vec<u8> {
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)
}

fn car_wash_buy_coin(addr: &AccountAddress, cnt: u8) -> Vec<u8> {
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)
}

fn car_wash_wash_car(addr: &AccountAddress) -> Vec<u8> {
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)
}

// Multiple Signers Example
fn multiple_signers_module() -> Vec<u8> {
core::include_bytes!(
"assets/move-projects/multiple-signers/build/multiple-signers/bytecode_modules/Dorm.mv"
)
.to_vec()
}

fn multiple_signers_init_module(addr: &AccountAddress) -> Vec<u8> {
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)
}

fn multiple_signers_rent_apartment(
addr1: &AccountAddress,
addr2: &AccountAddress,
addr3: &AccountAddress,
cnt: u8,
) -> Vec<u8> {
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)
}
4 changes: 2 additions & 2 deletions pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 1 addition & 27 deletions pallet/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::*;
Expand Down Expand Up @@ -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<AccountId32, Error<Test>> {
let (pk, _) = Public::from_ss58check_with_version(ss58addr)
.map_err(|_| Error::<Test>::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<AccountAddress, Error<Test>> {
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<Test>> {
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<Test>) {
let weight = Weight::from_parts(100_000_000_000, 1);
Expand Down
Loading

0 comments on commit 11c5f71

Please sign in to comment.