This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move some tests from sp-mvm to runtime (#190)
* Move some tests from sp-mvm to runtime * Add ensure_root runtime tests
- Loading branch information
1 parent
ca7fdd5
commit 7c716a2
Showing
11 changed files
with
279 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
script { | ||
use PontemFramework::PontBlock; | ||
use RuntimeTests::Store; | ||
|
||
fun store_system_block(account: signer) { | ||
Store::store_u64(&account, PontBlock::get_current_block_height()); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
script { | ||
use PontemFramework::PontTimestamp; | ||
use RuntimeTests::Store; | ||
|
||
fun store_system_timestamp(account: signer) { | ||
Store::store_u64(&account, PontTimestamp::now_microseconds()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module RuntimeTests::Store { | ||
struct U64 has key { val: u64 } | ||
|
||
struct U128 has key { val: u128 } | ||
|
||
struct Address has key { val: address } | ||
|
||
struct VectorU8 has key { val: vector<u8> } | ||
|
||
public fun store_u64(account: &signer, val: u64) { | ||
let foo = U64 { val: val }; | ||
move_to<U64>(account, foo); | ||
} | ||
|
||
public fun store_u128(account: &signer, val: u128) { | ||
let foo = U128 { val: val }; | ||
move_to<U128>(account, foo); | ||
} | ||
|
||
public fun store_address(account: &signer, val: address) { | ||
let addr = Address { val: val }; | ||
move_to<Address>(account, addr); | ||
} | ||
|
||
public fun store_vector_u8(account: &signer, val: vector<u8>) { | ||
let vec = VectorU8 { val: val }; | ||
move_to<VectorU8>(account, vec); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use crate::tests::mock::{ | ||
RuntimeBuilder, Accounts, Origin, TransactionPause, CurrencyId, Currencies, ParachainStaking, | ||
Perbill, | ||
}; | ||
use frame_support::{assert_ok, assert_err, error::BadOrigin}; | ||
|
||
#[test] | ||
fn ensure_root_in_transaction_pause() { | ||
RuntimeBuilder::new().build().execute_with(|| { | ||
assert_ok!(TransactionPause::pause_transaction( | ||
Origin::root(), | ||
b"Balances".to_vec(), | ||
b"transfer".to_vec() | ||
)); | ||
assert_err!( | ||
TransactionPause::pause_transaction( | ||
Origin::signed(Accounts::BOB.account()), | ||
b"Balances".to_vec(), | ||
b"transfer".to_vec() | ||
), | ||
BadOrigin | ||
); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn ensure_root_in_update_balance() { | ||
RuntimeBuilder::new().build().execute_with(|| { | ||
assert_err!( | ||
Currencies::update_balance( | ||
Origin::signed(Accounts::ALICE.account()), | ||
Accounts::ALICE.account().into(), | ||
CurrencyId::NATIVE, | ||
100 | ||
), | ||
BadOrigin | ||
); | ||
assert_ok!(Currencies::update_balance( | ||
Origin::root(), | ||
Accounts::ALICE.account().into(), | ||
CurrencyId::NATIVE, | ||
100 | ||
)); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn ensure_root_in_parachain_staking() { | ||
RuntimeBuilder::new().build().execute_with(|| { | ||
assert_err!( | ||
ParachainStaking::set_blocks_per_round( | ||
Origin::signed(Accounts::ALICE.account()), | ||
20u32 | ||
), | ||
BadOrigin | ||
); | ||
assert_ok!(ParachainStaking::set_blocks_per_round( | ||
Origin::root(), | ||
42u32 | ||
)); | ||
assert_err!( | ||
ParachainStaking::set_collator_commission( | ||
Origin::signed(Accounts::ALICE.account()), | ||
Perbill::from_percent(5) | ||
), | ||
BadOrigin | ||
); | ||
assert_ok!(ParachainStaking::set_collator_commission( | ||
Origin::root(), | ||
Perbill::from_percent(5) | ||
)); | ||
assert_err!( | ||
ParachainStaking::set_total_selected( | ||
Origin::signed(Accounts::ALICE.account()), | ||
42u32 | ||
), | ||
BadOrigin | ||
); | ||
assert_ok!(ParachainStaking::set_total_selected(Origin::root(), 42u32)); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use crate::tests::mock::{RuntimeBuilder, Accounts, Mvm, Origin, run_to_block, TIME_BLOCK_MULTIPLIER}; | ||
use frame_support::assert_ok; | ||
use move_core_types::{ | ||
language_storage::StructTag, identifier::Identifier, account_address::AccountAddress, | ||
resolver::ResourceResolver, | ||
}; | ||
use sp_mvm::storage::MoveVmStorage; | ||
use move_vm::io::state::State; | ||
|
||
pub mod modules { | ||
use assets::Asset; | ||
pub static STORE: Asset = Asset::new( | ||
"Store", | ||
"src/tests/assets/user/build/assets/bytecode_modules/Store.mv", | ||
); | ||
} | ||
|
||
pub mod transactions { | ||
use assets::Asset; | ||
pub static STORE_SYSTEM_BLOCK: Asset = Asset::new( | ||
"store_system_block", | ||
"src/tests/assets/user/build/assets/transaction/store_system_block.mvt", | ||
); | ||
pub static STORE_SYSTEM_TIMESTAMP: Asset = Asset::new( | ||
"store_system_timestamp", | ||
"src/tests/assets/user/build/assets/transaction/store_system_timestamp.mvt", | ||
); | ||
} | ||
|
||
const GAS_LIMIT: u64 = 1_000_000; | ||
|
||
/// Check stored value (u64) inside storage. | ||
fn check_stored_value(expected: u64) { | ||
#[derive(serde::Deserialize, Debug, PartialEq)] | ||
struct StoreU64 { | ||
pub val: u64, | ||
} | ||
|
||
let expected = StoreU64 { val: expected }; | ||
|
||
let tag = StructTag { | ||
address: Accounts::BOB.addr(), | ||
module: Identifier::new(modules::STORE.name()).unwrap(), | ||
name: Identifier::new("U64").unwrap(), | ||
type_params: vec![], | ||
}; | ||
|
||
check_storage_res(Accounts::BOB.addr(), tag, expected); | ||
} | ||
|
||
/// Check resource value inside storage. | ||
pub fn check_storage_res<T>(owner: AccountAddress, ty: StructTag, expected: T) | ||
where | ||
T: for<'de> serde::Deserialize<'de>, | ||
T: std::cmp::PartialEq + std::fmt::Debug, | ||
{ | ||
let storage = Mvm::move_vm_storage(); | ||
let state = State::new(storage); | ||
let blob = state | ||
.get_resource(&owner, &ty) | ||
.expect("VM state read storage (resource)") | ||
.expect(&format!("Resource '{}' should exist", ty)); | ||
|
||
let tt_str = format!("{}", ty); | ||
println!("checking stored resource '{}'", tt_str); | ||
let stored: T = | ||
bcs::from_bytes(&blob).expect(&format!("Resource '{}' should exists", tt_str)); | ||
assert_eq!(expected, stored); | ||
} | ||
|
||
#[test] | ||
/// Execute storing of block height inside module by calling script. | ||
fn execute_store_block() { | ||
RuntimeBuilder::new().build().execute_with(|| { | ||
// Publish STORE module. | ||
assert_ok!(Mvm::publish_module( | ||
Origin::signed(Accounts::BOB.account()), | ||
modules::STORE.bytes().to_vec(), | ||
GAS_LIMIT | ||
)); | ||
|
||
const EXPECTED: u32 = 3; | ||
run_to_block(EXPECTED); | ||
assert_ok!(Mvm::execute( | ||
Origin::signed(Accounts::BOB.account()), | ||
transactions::STORE_SYSTEM_BLOCK.bytes().to_vec(), | ||
GAS_LIMIT | ||
)); | ||
check_stored_value(EXPECTED.into()); | ||
}); | ||
} | ||
|
||
#[test] | ||
/// Execute storing of timestamp inside module by calling script. | ||
fn execute_store_time() { | ||
RuntimeBuilder::new().build().execute_with(|| { | ||
// Publish STORE module. | ||
assert_ok!(Mvm::publish_module( | ||
Origin::signed(Accounts::BOB.account()), | ||
modules::STORE.bytes().to_vec(), | ||
GAS_LIMIT | ||
)); | ||
|
||
const EXPECTED: u32 = 3; | ||
run_to_block(EXPECTED); | ||
assert_ok!(Mvm::execute( | ||
Origin::signed(Accounts::BOB.account()), | ||
transactions::STORE_SYSTEM_TIMESTAMP.bytes().to_vec(), | ||
GAS_LIMIT | ||
)); | ||
check_stored_value(EXPECTED as u64 * TIME_BLOCK_MULTIPLIER); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[toolchain] | ||
channel = "nightly-2021-11-07" | ||
profile = "complete" | ||
profile = "default" | ||
targets = [ "wasm32-unknown-unknown"] |