diff --git a/Cargo.lock b/Cargo.lock index 243c7e6..b68586c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4178,6 +4178,7 @@ dependencies = [ "scale-info", "sp-core", "sp-genesis-builder", + "sp-io", "sp-runtime", "substrate-wasm-builder", ] diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 647c74f..1973b5a 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -27,6 +27,7 @@ frame-system = { git = "https://github.com/paritytech/polkadot-sdk", branch = "r ## premitivies sp-core = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0" , default-features = false } sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0" , default-features = false } +sp-io = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } # pallets that we want to use pallet-balances = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0" , default-features = false } diff --git a/runtime/src/frame_config.rs b/runtime/src/frame_config.rs deleted file mode 100644 index 61289b8..0000000 --- a/runtime/src/frame_config.rs +++ /dev/null @@ -1,95 +0,0 @@ -use frame::deps::frame_support; -use frame::deps::frame_system; -use frame::prelude::inject_runtime_type; - - -pub struct OpmChainDefaultConfig; - -type Block = frame::runtime::types_common::BlockOf; - -#[frame_support::register_default_impl(OpmChainDefaultConfig)] -impl frame_system::DefaultConfig for OpmChainDefaultConfig { - type Block = Block; - type BlockHashCount = ConstU32<1024>; - - /// The default type for storing how many extrinsics an account has signed. - type Nonce = u32; - - /// The default type for hashing blocks and tries. - type Hash = sp_core::hash::H256; - - /// The default hashing algorithm used. - type Hashing = sp_runtime::traits::BlakeTwo256; - - /// The default identifier used to distinguish between accounts. - type AccountId = sp_runtime::AccountId32; - - /// The lookup mechanism to get account ID from whatever is passed in dispatchers. - type Lookup = sp_runtime::traits::AccountIdLookup; - - /// The maximum number of consumers allowed on a single account. Using 128 as default. - type MaxConsumers = frame_support::traits::ConstU32<128>; - - /// The default data to be stored in an account. - type AccountData = frame_system::AccountInfo; - - /// What to do if a new account is created. - type OnNewAccount = (); - - /// What to do if an account is fully reaped from the system. - type OnKilledAccount = (); - - /// Weight information for the extrinsics of this pallet. - type SystemWeightInfo = (); - - /// This is used as an identifier of the chain. - type SS58Prefix = (); - - /// Version of the runtime. - type Version = (); - - /// Block & extrinsics weights: base values and limits. - type BlockWeights = (); - - /// The maximum length of a block (in bytes). - type BlockLength = (); - - /// The weight of database operations that the runtime can invoke. - type DbWeight = (); - - /// The ubiquitous event type injected by `construct_runtime!`. - #[inject_runtime_type] - type RuntimeEvent = (); - - /// The ubiquitous origin type injected by `construct_runtime!`. - #[inject_runtime_type] - type RuntimeOrigin = (); - - /// The aggregated dispatch type available for extrinsics, injected by - /// `construct_runtime!`. - #[inject_runtime_type] - type RuntimeCall = (); - - /// The aggregated Task type, injected by `construct_runtime!`. - #[inject_runtime_type] - type RuntimeTask = (); - - /// Converts a module to the index of the module, injected by `construct_runtime!`. - #[inject_runtime_type] - type PalletInfo = (); - - /// The basic call filter to use in dispatchable. Supports everything as the default. - type BaseCallFilter = frame_support::traits::Everything; - - /// Maximum number of block number to block hash mappings to keep (oldest pruned first). - /// Using 256 as default. - type BlockHashCount = frame_support::traits::ConstU32<256>; - - /// The set code logic, just the default since we're not a parachain. - type OnSetCode = (); - type SingleBlockMigrations = (); - type MultiBlockMigrator = (); - type PreInherents = (); - type PostInherents = (); - type PostTransactions = (); -} diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 8698a23..d612275 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -24,8 +24,8 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); use frame::{ deps::frame_support::{ genesis_builder_helper::{build_config, create_default_config}, - weights::{FixedFee, NoFee}, traits::Everything, + weights::{FixedFee, NoFee}, }, prelude::*, runtime::{ @@ -37,9 +37,7 @@ use frame::{ }, }; -use frame_support::{ - weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, IdentityFee, Weight}, -}; +use frame_support::weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, IdentityFee, Weight}; // Substrate FRAME #[cfg(feature = "with-paritydb-weights")] @@ -47,9 +45,7 @@ use frame_support::weights::constants::ParityDbWeight as RuntimeDbWeight; #[cfg(feature = "with-rocksdb-weights")] use frame_support::weights::constants::RocksDbWeight as RuntimeDbWeight; -use sp_runtime::generic; -use sp_runtime::Perbill; -use sp_runtime::traits::BlakeTwo256; +use sp_runtime::{generic, traits::BlakeTwo256, Perbill}; /// Type of block number. pub type BlockNumber = u32; @@ -113,10 +109,8 @@ construct_runtime!( const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); /// We allow for 2000ms of compute with a 6 second average block time. pub const WEIGHT_MILLISECS_PER_BLOCK: u64 = 2000; -pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts( - WEIGHT_MILLISECS_PER_BLOCK * WEIGHT_REF_TIME_PER_MILLIS, - u64::MAX, -); +pub const MAXIMUM_BLOCK_WEIGHT: Weight = + Weight::from_parts(WEIGHT_MILLISECS_PER_BLOCK * WEIGHT_REF_TIME_PER_MILLIS, u64::MAX); pub const MAXIMUM_BLOCK_LENGTH: u32 = 5 * 1024 * 1024; parameter_types! { @@ -365,3 +359,65 @@ pub mod interface { pub type Balance = ::Balance; pub type MinimumBalance = ::ExistentialDeposit; } + +#[cfg(test)] +mod tests { + use super::{interface, Balances, Runtime, RuntimeOrigin, System}; + use sp_io; + use sp_runtime::BuildStorage; + + use sp_core::{crypto::DEV_PHRASE, sr25519, Pair}; + use sp_runtime::AccountId32; + + use sp_runtime::MultiAddress::Id; + + fn get_account_id(name: &str) -> AccountId32 { + let pair = sr25519::Pair::from_string(&format!("{}//{}", DEV_PHRASE, name), None).unwrap(); + let pub_key = pair.public(); + let account_id = interface::AccountId::from(pub_key); + account_id + } + + // Function to set up the externalities + pub fn new_test_ext() -> sp_io::TestExternalities { + let mut storage = + frame_system::GenesisConfig::::default().build_storage().unwrap(); + + // Here you must include the balances in your genesis configuration. + pallet_balances::GenesisConfig:: { + // Assuming Alice's account ID is correct and she should have some balance. + balances: vec![(get_account_id("Alice"), 1000), (get_account_id("Bob"), 1000)], + } + .assimilate_storage(&mut storage) + .unwrap(); + + sp_io::TestExternalities::from(storage) + } + + #[test] + fn test_block_number() { + new_test_ext().execute_with(|| { + assert_eq!(System::block_number(), 0, "Block Number should be 0"); + System::set_block_number(5); + assert_eq!(System::block_number(), 5, "Block Number should be 5"); + }); + } + + #[test] + fn test_balance_transfer() { + let ALICE: AccountId32 = get_account_id(&"Alice"); + let BOB: AccountId32 = get_account_id(&"Bob"); + new_test_ext().execute_with(|| { + assert_eq!(Balances::usable_balance(ALICE.clone()), 1000); + assert_eq!(Balances::usable_balance(BOB.clone()), 1000); + let _ = Balances::transfer_keep_alive( + RuntimeOrigin::signed(ALICE.clone()), + Id(BOB.clone()), + 10, + ) + .unwrap(); + assert_eq!(Balances::usable_balance(ALICE.clone()), 990); + assert_eq!(Balances::usable_balance(BOB.clone()), 1010); + }) + } +}