Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Refactor sp-mvm/tests/assets.rs #108

Merged
merged 1 commit into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pallets/sp-mvm/tests/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use move_core_types::language_storage::StructTag;
use move_core_types::account_address::AccountAddress;

mod common;
use common::assets::*;
use common::assets::{modules, transactions};
use common::mock::*;
use common::addr::*;
use common::utils::*;
Expand All @@ -31,7 +31,7 @@ where
};
let tag = StructTag {
address,
module: Identifier::new(UserMod::Store.name()).unwrap(),
module: Identifier::new(modules::user::STORE.name()).unwrap(),
name: Identifier::new("U128").unwrap(),
type_params: vec![],
};
Expand All @@ -47,7 +47,7 @@ where
};
let tag = StructTag {
address,
module: Identifier::new(UserMod::Store.name()).unwrap(),
module: Identifier::new(modules::user::STORE.name()).unwrap(),
name: Identifier::new("U64").unwrap(),
type_params: vec![],
};
Expand All @@ -60,10 +60,10 @@ fn execute_get_balance() {
let account = origin_ps_acc();

// publish user module:
publish_module(account, UserMod::Store, None).unwrap();
publish_module(account, &modules::user::STORE, None).unwrap();

// execute tx:
let result = execute_tx(account, UserTx::StoreGetBalance, None);
let result = execute_tx(account, &transactions::STORE_NATIVE_BALANCE, None);
assert_ok!(result);

// check storage:
Expand All @@ -84,10 +84,10 @@ fn execute_transfer() {
eprintln!("Bob balance: {}", bob_init_balance);

// publish user module
publish_module(bob, UserMod::Store, None).unwrap();
publish_module(bob, &modules::user::STORE, None).unwrap();

// execute tx:
let result = execute_tx(bob, UserTx::Transfer, None);
let result = execute_tx(bob, &transactions::TRANSFER, None);
assert_ok!(result);

// check storage balance:
Expand Down
291 changes: 98 additions & 193 deletions pallets/sp-mvm/tests/common/assets.rs
Original file line number Diff line number Diff line change
@@ -1,215 +1,120 @@
#![allow(dead_code)]

const ROOT_PACKAGES: &[&str] = &["Assets"];
const ROOT_PACKAGES_BYTECODE: &[&[u8]] = &[include_bytes!(
"../assets/root/artifacts/bundles/assets.pac"
)];
const ROOT_PACKAGES_MODULES: &[&[&str]] = &[&["Store", "EventProxy"]];

const USR_PACKAGES: &[&str] = &["Assets"];
const USR_PACKAGES_BYTECODE: &[&[u8]] = &[include_bytes!(
"../assets/user/artifacts/bundles/assets.pac"
)];
const USR_PACKAGES_MODULES: &[&[&str]] = &[&["Store", "EventProxy"]];

const USER_MODULES: &[&str] = &["Store", "EventProxy"];
const USER_BYTECODE: &[&[u8]] = &[
include_bytes!("../assets/user/artifacts/modules/2_Store.mv"),
include_bytes!("../assets/user/artifacts/modules/47_EventProxy.mv"),
];

const ROOT_MODULES: &[&str] = &["Store", "EventProxy"];
const ROOT_BYTECODE: &[&[u8]] = &[
include_bytes!("../assets/root/artifacts/modules/0_Store.mv"),
include_bytes!("../assets/root/artifacts/modules/1_EventProxy.mv"),
];

const TX_NAMES: &[&str] = &[
"store_u64",
"emit_event",
"store_system_block",
"store_system_timestamp",
"inf_loop",
"store_native_balance",
"transfer",
// "store_native_deposit",
// "store_native_deposit_reg",
// "store_native_withdraw",
// "store_native_withdraw_reg",
// "get_price_test",
// "missed_native_balance",
];
const TX_BYTECODE: &[&[u8]] = &[
include_bytes!("../assets/user/artifacts/transactions/store_u64.mvt"),
include_bytes!("../assets/user/artifacts/transactions/emit_event.mvt"),
include_bytes!("../assets/user/artifacts/transactions/store_system_block.mvt"),
include_bytes!("../assets/user/artifacts/transactions/store_system_timestamp.mvt"),
include_bytes!("../assets/user/artifacts/transactions/inf_loop.mvt"),
include_bytes!("../assets/user/artifacts/transactions/store_native_balance.mvt"),
include_bytes!("../assets/user/artifacts/transactions/transfer.mvt"),
include_bytes!("../assets/user/artifacts/transactions/multisig_test.mvt"),
// include_bytes!("../assets/user/artifacts/transactions/store_native_deposit.mvt"),
// include_bytes!("../assets/user/artifacts/transactions/store_native_deposit_reg.mvt"),
// include_bytes!("../assets/user/artifacts/transactions/store_native_withdraw.mvt"),
// include_bytes!("../assets/user/artifacts/transactions/store_native_withdraw_reg.mvt"),
// include_bytes!("../assets/user/artifacts/transactions/get_price_test.mvt"),
// include_bytes!("../assets/user/artifacts/transactions/missed_native_balance.mvt"),
];

pub trait BinAsset: Sized + Copy + Into<usize> {
const NAMES: &'static [&'static str];
const BYTES: &'static [&'static [u8]];

fn name(&self) -> &'static str {
Self::NAMES[(*self).into()]
use once_cell::sync::OnceCell;
use std::path::Path;

#[derive(Clone)]
pub struct Asset {
name: &'static str,
path: &'static str,
bytes: OnceCell<Vec<u8>>,
}

impl Asset {
pub const fn new(name: &'static str, path: &'static str) -> Self {
Self {
name,
path,
bytes: OnceCell::new(),
}
}
fn bc(&self) -> &'static [u8] {
Self::BYTES[(*self).into()]
}

fn all() -> &'static [Self];
}

pub trait BinAssetPackage: BinAsset {
const MODULES: &'static [&'static [&'static str]];

fn modules(&self) -> &[&'static str] {
Self::MODULES[(*self).into()]
pub fn name(&self) -> &'static str {
self.name
}
}

#[repr(usize)]
#[derive(Copy, Clone, Debug)]
pub enum UserMod {
Store = 0,
EventProxy = 1,
}

#[repr(usize)]
#[derive(Copy, Clone, Debug)]
pub enum RootMod {
Store = 0,
EventProxy = 1,
}

#[repr(usize)]
#[derive(Copy, Clone, Debug)]
pub enum RootPackages {
Assets = 0,
}

#[repr(usize)]
#[derive(Copy, Clone, Debug)]
pub enum UsrPackages {
Assets = 0,
}

#[repr(usize)]
#[derive(Copy, Clone, Debug)]
pub enum UserTx {
StoreU64 = 0,
EmitEvent = 1,
StoreSysBlock = 2,
StoreSysTime = 3,
InfLoop = 4,
StoreGetBalance = 5,
Transfer = 6,
MultisigTest = 7,
//StoreNativeDeposit = 6,
//StoreNativeDepositReg = 7,
//StoreNativeWithdraw = 8,
//StoreNativeWithdrawReg = 9,
//GetPriceTest = 10,
//MissedNativeBalance = 11,
}

impl Into<usize> for UserMod {
fn into(self) -> usize {
self as usize
pub fn bytes(&self) -> &[u8] {
self.bytes
.get_or_init(|| {
let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let path = Path::new(dir.as_str()).join(self.path);
std::fs::read(&path)
.unwrap_or_else(|_| panic!("Failed to load test asset: {:?}", path.display()))
})
.as_slice()
}
}

impl Into<usize> for RootMod {
fn into(self) -> usize {
self as usize
}
pub struct Package {
modules: &'static [&'static str],
package: Asset,
}

impl Into<usize> for RootPackages {
fn into(self) -> usize {
self as usize
impl Package {
pub const fn new(modules: &'static [&'static str], package: Asset) -> Self {
Self { modules, package }
}
}

impl Into<usize> for UsrPackages {
fn into(self) -> usize {
self as usize
pub fn modules(&self) -> &'static [&'static str] {
self.modules
}
}

impl Into<usize> for UserTx {
fn into(self) -> usize {
self as usize
pub fn bytes(&self) -> &[u8] {
self.package.bytes()
}
}

impl BinAsset for UserMod {
const NAMES: &'static [&'static str] = USER_MODULES;
const BYTES: &'static [&'static [u8]] = USER_BYTECODE;

fn all() -> &'static [Self] {
&[Self::Store, Self::EventProxy]
pub static ROOT_PACKAGE: Package = Package::new(
&["Store", "EventProxy"],
Asset::new("", "tests/assets/root/artifacts/bundles/assets.pac"),
);
pub static USER_PACKAGE: Package = Package::new(
&["Store", "EventProxy"],
Asset::new("", "tests/assets/user/artifacts/bundles/assets.pac"),
);

pub mod modules {
pub mod root {
use super::super::Asset;
pub static STORE: Asset =
Asset::new("Store", "tests/assets/root/artifacts/modules/0_Store.mv");
pub static EVENT_PROXY: Asset = Asset::new(
"EventProxy",
"tests/assets/root/artifacts/modules/1_EventProxy.mv",
);
}
}

impl BinAsset for RootMod {
const NAMES: &'static [&'static str] = ROOT_MODULES;
const BYTES: &'static [&'static [u8]] = ROOT_BYTECODE;

fn all() -> &'static [Self] {
&[Self::Store, Self::EventProxy]
}
}

impl BinAsset for UserTx {
const NAMES: &'static [&'static str] = TX_NAMES;
const BYTES: &'static [&'static [u8]] = TX_BYTECODE;

fn all() -> &'static [Self] {
&[
Self::StoreU64,
Self::EmitEvent,
Self::StoreSysBlock,
Self::StoreSysTime,
Self::InfLoop,
Self::StoreGetBalance,
Self::Transfer,
]
}
}

impl BinAsset for RootPackages {
const NAMES: &'static [&'static str] = ROOT_PACKAGES;
const BYTES: &'static [&'static [u8]] = ROOT_PACKAGES_BYTECODE;

fn all() -> &'static [Self] {
&[Self::Assets]
}
}

impl BinAssetPackage for RootPackages {
const MODULES: &'static [&'static [&'static str]] = ROOT_PACKAGES_MODULES;
}

impl BinAsset for UsrPackages {
const NAMES: &'static [&'static str] = USR_PACKAGES;
const BYTES: &'static [&'static [u8]] = USR_PACKAGES_BYTECODE;

fn all() -> &'static [Self] {
&[Self::Assets]
pub mod user {
use super::super::Asset;
pub static STORE: Asset =
Asset::new("Store", "tests/assets/user/artifacts/modules/2_Store.mv");
pub static EVENT_PROXY: Asset = Asset::new(
"EventProxy",
"tests/assets/user/artifacts/modules/47_EventProxy.mv",
);
}
}

impl BinAssetPackage for UsrPackages {
const MODULES: &'static [&'static [&'static str]] = USR_PACKAGES_MODULES;
pub mod transactions {
use super::Asset;
pub static STORE_U64: Asset = Asset::new(
"store_u64",
"tests/assets/user/artifacts/transactions/store_u64.mvt",
);
pub static EMIT_EVENT: Asset = Asset::new(
"emit_event",
"tests/assets/user/artifacts/transactions/emit_event.mvt",
);
pub static STORE_SYSTEM_BLOCK: Asset = Asset::new(
"store_system_block",
"tests/assets/user/artifacts/transactions/store_system_block.mvt",
);
pub static STORE_SYSTEM_TIMESTAMP: Asset = Asset::new(
"store_system_timestamp",
"tests/assets/user/artifacts/transactions/store_system_timestamp.mvt",
);
pub static INF_LOOP: Asset = Asset::new(
"inf_loop",
"tests/assets/user/artifacts/transactions/inf_loop.mvt",
);
pub static STORE_NATIVE_BALANCE: Asset = Asset::new(
"store_native_balance",
"tests/assets/user/artifacts/transactions/store_native_balance.mvt",
);
pub static TRANSFER: Asset = Asset::new(
"transfer",
"tests/assets/user/artifacts/transactions/transfer.mvt",
);
pub static MULTISIG_TEST: Asset = Asset::new(
"multisig_test",
"tests/assets/user/artifacts/transactions/multisig_test.mvt",
);
}
Loading