Skip to content

Commit

Permalink
actual AccountId in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
35359595 committed Nov 3, 2023
1 parent bde49f8 commit 6cde75f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v1.0.0' }
sp-core = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v1.0.0' }
sp-runtime = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v1.0.0' }

# MoveVM dependencies
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod pallet {
use move_core_types::account_address::AccountAddress;
use move_vm_backend::Mvm;
use move_vm_types::gas::UnmeteredGasMeter;
use sp_core::crypto::AccountId32;
use sp_std::{default::Default, vec::Vec};

use super::*;
Expand Down Expand Up @@ -243,12 +244,11 @@ pub mod pallet {
}

// Transparent conversion native -> move
pub fn native_to_move(of: T::AccountId) -> Result<AccountAddress, Error<T>> {
let encoded = of.encode();
if encoded.len().ne(&32usize) {
return Err(Error::InvalidAccountSize);
}
Ok(AccountAddress::new(array_ref![encoded, 0, 32].to_owned()))
pub fn native_to_move(of: AccountId32) -> Result<AccountAddress, Error<T>> {
let account_bytes: [u8; 32] = of.into();
Ok(AccountAddress::new(
array_ref![account_bytes, 0, 32].to_owned(),
))
}
}
}
11 changes: 11 additions & 0 deletions tests/execute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod mock;
use mock::*;
use move_core_types::account_address::AccountAddress;

#[test]
#[ignore = "to be implemented"]
Expand Down Expand Up @@ -46,3 +47,13 @@ fn execute_script_corrupted_bytecode() {
assert_eq!(1, 0);
});
}

#[test]
fn round_conversion_native_move_works() {
new_test_ext().execute_with(|| {
const MOVE: &str = "0x90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22";
let native =
MoveModule::move_to_native(&AccountAddress::from_hex_literal(MOVE).unwrap()).unwrap();
let move_again = MoveModule::native_to_move(native).unwrap();
})
}
31 changes: 27 additions & 4 deletions tests/mock.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use frame_support::traits::{ConstU16, ConstU64};
use frame_support::traits::{ConstU128, ConstU16, ConstU32, ConstU64};
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
AccountId32, BuildStorage,
};

type Block = frame_system::mocking::MockBlock<Test>;
Expand All @@ -17,14 +17,14 @@ impl frame_system::Config for Test {
type Nonce = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type AccountId = AccountId32;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
Expand All @@ -33,7 +33,29 @@ impl frame_system::Config for Test {
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

pub const EXISTENTIAL_DEPOSIT: u128 = 500;
pub type Balance = u128;

impl pallet_balances::Config for Test {
type MaxLocks = ConstU32<50>;
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
/// The type for recording an account's balance.
type Balance = Balance;
/// The ubiquitous event type.
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ConstU128<EXISTENTIAL_DEPOSIT>;
type AccountStore = System;
type WeightInfo = pallet_balances::weights::SubstrateWeight<Test>;
type FreezeIdentifier = ();
type MaxFreezes = ();
type RuntimeHoldReason = ();
type MaxHolds = ();
}

impl pallet_move::Config for Test {
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}
Expand All @@ -52,6 +74,7 @@ frame_support::construct_runtime!(
pub enum Test
{
System: frame_system,
Balances: pallet_balances,
MoveModule: pallet_move,
}
);
12 changes: 10 additions & 2 deletions tests/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ use frame_support::assert_ok;
use mock::*;
use move_core_types::{identifier::Identifier, language_storage::StructTag};
use pallet_move::address;
use sp_runtime::AccountId32;

const EMPTY_ADDR: u64 = 0x000000000CAFE_u64.to_be();
const EMPTY_ADDR: AccountId32 = AccountId32::new([1u8; 32]);

#[test]
/// Test getting a module.
fn get_module_correct() {
new_test_ext().execute_with(|| {
let signed = RuntimeOrigin::signed(EMPTY_ADDR);
assert_ok!(Balances::force_set_balance(
RuntimeOrigin::root(),
EMPTY_ADDR,
1_000_000_000_000
));

let module = include_bytes!("assets/move/build/move/bytecode_modules/Empty.mv").to_vec();

let res = MoveModule::publish_module(RuntimeOrigin::signed(EMPTY_ADDR), module.clone(), 0);
let res = MoveModule::publish_module(signed, module.clone(), 0);

assert_ok!(res);

Expand Down
3 changes: 2 additions & 1 deletion tests/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod mock;

use frame_support::assert_ok;
use mock::*;
use sp_runtime::AccountId32;

#[test]
/// Test that the module is published correctly.
Expand All @@ -12,7 +13,7 @@ fn publish_module_as_user_correct() {
let res = MoveModule::publish_module(
// Just for now - as Move module address account is 0xCAFE, we need to sign it the same
// address. But in tests, AccountId is u64, so we need to convert it (0xCAFE -> 0xFECA000000000000 - endian welcome)
RuntimeOrigin::signed(0xFECA000000000000),
RuntimeOrigin::signed(AccountId32::new([1u8; 32])),
module,
0,
);
Expand Down

0 comments on commit 6cde75f

Please sign in to comment.