From 38256db04f795e6e19d66893d90085db6b4d5828 Mon Sep 17 00:00:00 2001 From: bear Date: Sun, 8 Oct 2023 14:59:37 +0800 Subject: [PATCH 1/7] Add some basics cases --- runtime/pangolin/tests/tests.rs | 181 +++++++++++++++++++++++++++++++- 1 file changed, 180 insertions(+), 1 deletion(-) diff --git a/runtime/pangolin/tests/tests.rs b/runtime/pangolin/tests/tests.rs index b220641fb..18fa01a2f 100644 --- a/runtime/pangolin/tests/tests.rs +++ b/runtime/pangolin/tests/tests.rs @@ -20,6 +20,185 @@ pub mod mock; darwinia_common_runtime::impl_weight_tests! {} darwinia_common_runtime::impl_fee_tests! {} -darwinia_common_runtime::impl_evm_tests! {} +// darwinia_common_runtime::impl_evm_tests! {} darwinia_common_runtime::impl_account_migration_tests! {} darwinia_common_runtime::impl_messages_bridge_tests! {} + +#[test] +fn precompile_address() { + use crate::mock::*; + + assert_eq!( + PangolinPrecompiles::::used_addresses() + .iter() + .map(|a| a.to_low_u64_be()) + .collect::>(), + vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 1027, 1536, 1537, 2048] + ); +} + +mod message_transact { + // darwinia + use super::mock::*; + use frame_support::traits::EnsureOrigin; + use sp_core::H160; + use static_assertions::assert_type_eq_all; + + #[test] + fn ensure_origin_correctly() { + assert_type_eq_all!( + <::LcmpEthOrigin as EnsureOrigin< + RuntimeOrigin, + >>::Success, + H160 + ); + } +} + +mod balances { + // darwinia + use super::mock::*; + use frame_support::traits::Get; + + #[test] + fn ensure_constants_correctly() { + assert_eq!(::ExistentialDeposit::get(), 0); + assert_eq!(<::MaxLocks as Get>::get(), 50); + assert_eq!(<::MaxReserves as Get>::get(), 50); + } +} + +mod evm { + // darwinia + use super::mock::*; + // frontier + use pallet_ethereum::PostLogContent; + use pallet_evm_precompile_dispatch::DispatchValidateT; + // substrate + use frame_support::{assert_err, traits::Get}; + use sp_core::{H160, U256}; + use sp_runtime::{DispatchError, ModuleError}; + + #[test] + fn configured_base_extrinsic_weight_is_evm_compatible() { + let min_ethereum_transaction_weight = WeightPerGas::get() * 21_000; + let base_extrinsic = ::BlockWeights::get() + .get(frame_support::dispatch::DispatchClass::Normal) + .base_extrinsic; + + assert!(base_extrinsic.ref_time() <= min_ethereum_transaction_weight.ref_time()); + } + + fn ethereum_constants_are_correctly() { + assert_eq!(<::ExtraDataLength as Get>::get(), 64); + assert_eq!( + ::PostLogContent::get() as u8, + PostLogContent::BlockAndTxnHashes as u8 + ); + } + + #[test] + fn evm_constants_are_correctly() { + assert_eq!(BlockGasLimit::get(), U256::from(20_000_000)); + assert_eq!(WeightPerGas::get().ref_time(), 18750); + assert_eq!(GasLimitPovSizeRatio::get(), 6); + } + + #[test] + fn pallet_evm_calls_only_callable_by_root() { + ExtBuilder::default().build().execute_with(|| { + // https://github.com/darwinia-network/darwinia/blob/5923b2e0526b67fe05cee6e4e592ceca80e8f2ff/runtime/darwinia/src/pallets/evm.rs#L136 + assert_err!( + EVM::call( + RuntimeOrigin::signed(H160::default().into()), + H160::default(), + H160::default(), + vec![], + U256::default(), + 1000000, + U256::from(1_000_000), + None, + None, + vec![], + ), + DispatchError::BadOrigin + ); + + if let Err(dispatch_info_with_err) = EVM::call( + RuntimeOrigin::root(), + H160::default(), + H160::default(), + vec![], + U256::default(), + 1000000, + U256::from(1_000_000), + None, + None, + vec![], + ) { + assert_eq!( + dispatch_info_with_err.error, + DispatchError::Module(ModuleError { + index: 37, + error: [4, 0, 0, 0], + message: Some("GasPriceTooLow") + }) + ); + } + }); + } + + #[test] + fn dispatch_validator_filter_runtime_calls() { + ExtBuilder::default().build().execute_with(|| { + assert!(DarwiniaDispatchValidator::validate_before_dispatch( + &H160::default().into(), + &RuntimeCall::System(frame_system::Call::remark { remark: vec![] }) + ) + .is_none()); + + assert!(DarwiniaDispatchValidator::validate_before_dispatch( + &H160::default().into(), + // forbidden call + &RuntimeCall::EVM(pallet_evm::Call::call { + source: H160::default(), + target: H160::default(), + input: vec![], + value: U256::default(), + gas_limit: 1000000, + max_fee_per_gas: U256::from(1_000_000), + max_priority_fee_per_gas: None, + nonce: None, + access_list: vec![], + }) + ) + .is_some()); + }); + } + + #[test] + fn dispatch_validator_filter_dispatch_class() { + ExtBuilder::default().build().execute_with(|| { + // Default class + assert!(DarwiniaDispatchValidator::validate_before_dispatch( + &H160::default().into(), + &RuntimeCall::System(frame_system::Call::remark { remark: vec![] }) + ) + .is_none()); + + // Operational class + assert!(DarwiniaDispatchValidator::validate_before_dispatch( + &H160::default().into(), + &RuntimeCall::System(frame_system::Call::set_heap_pages { pages: 20 }) + ) + .is_none()); + + // Mandatory class + assert!(DarwiniaDispatchValidator::validate_before_dispatch( + &H160::default().into(), + &RuntimeCall::Timestamp(pallet_timestamp::Call::set { now: 100 }) + ) + .is_some()); + }); + } +} From b7ffe0546dec7fb788b2997f098f23dfd0f9055d Mon Sep 17 00:00:00 2001 From: bear Date: Sun, 8 Oct 2023 15:59:19 +0800 Subject: [PATCH 2/7] More pallets --- runtime/pangolin/tests/tests.rs | 132 ++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/runtime/pangolin/tests/tests.rs b/runtime/pangolin/tests/tests.rs index 18fa01a2f..255e44c35 100644 --- a/runtime/pangolin/tests/tests.rs +++ b/runtime/pangolin/tests/tests.rs @@ -68,6 +68,138 @@ mod balances { } } +mod assets { + // darwinia + use super::mock::*; + use frame_support::traits::{AsEnsureOriginWithArg, Get, IsInVec}; + use static_assertions::assert_type_eq_all; + + #[test] + fn ensure_constants_and_origin_correctly() { + assert_type_eq_all!(::ForceOrigin, Root); + assert_type_eq_all!( + ::CreateOrigin, + AsEnsureOriginWithArg, AccountId>> + ); + + assert_eq!( + <::AssetAccountDeposit as Get>::get(), + 0 + ); + assert_eq!( + <::MetadataDepositPerByte as Get>::get(), + 0 + ); + assert_eq!( + <::MetadataDepositBase as Get>::get(), + 0 + ); + assert_eq!(<::AssetDeposit as Get>::get(), 0); + assert_eq!(<::RemoveItemsLimit as Get>::get(), 1000); + assert_eq!(<::StringLimit as Get>::get(), 50); + } +} + +mod governance { + // darwinia + use super::mock::*; + use frame_support::traits::{AsEnsureOriginWithArg, Get, IsInVec}; + use static_assertions::assert_type_eq_all; + + #[test] + fn preimages_setting_correctly() { + assert_eq!( + <::BaseDeposit as Get>::get(), + 500 * UNIT + ); + assert_eq!( + <::ByteDeposit as Get>::get(), + darwinia_deposit(0, 1) + ); + assert_type_eq_all!(::ManagerOrigin, Root); + } + + #[test] + fn scheduler_setting_correctly() { + assert_eq!( + <::MaxScheduledPerBlock as Get>::get(), + 50 + ); + assert_type_eq_all!(::ScheduleOrigin, Root); + } + + #[test] + fn democracy_setting_correctly() { + assert_type_eq_all!(::BlacklistOrigin, Root); + assert_type_eq_all!( + ::CancelProposalOrigin, + RootOrAll + ); + assert_type_eq_all!( + ::CancellationOrigin, + RootOrAtLeastTwoThird + ); + assert_type_eq_all!( + ::ExternalDefaultOrigin, + RootOrAll + ); + assert_type_eq_all!( + ::ExternalMajorityOrigin, + RootOrAtLeastHalf + ); + assert_type_eq_all!( + ::ExternalOrigin, + RootOrAtLeastHalf + ); + assert_type_eq_all!( + ::FastTrackOrigin, + RootOrAtLeastTwoThird + ); + assert_type_eq_all!( + ::InstantOrigin, + RootOrAll + ); + assert_type_eq_all!(::VetoOrigin, pallet_collective::EnsureMember); + + assert_eq!( + <::CooloffPeriod as Get>::get(), + 5 * MINUTES + ); + assert_eq!( + <::EnactmentPeriod as Get>::get(), + 10 * MINUTES + ); + assert_eq!( + <::FastTrackVotingPeriod as Get>::get(), + MINUTES + ); + assert_eq!( + <::InstantAllowed as Get>::get(), + true + ); + assert_eq!( + <::LaunchPeriod as Get>::get(), + 10 * MINUTES + ); + assert_eq!(<::MaxBlacklisted as Get>::get(), 100); + assert_eq!(<::MaxDeposits as Get>::get(), 100); + assert_eq!(<::MaxProposals as Get>::get(), 100); + assert_eq!(<::MaxVotes as Get>::get(), 100); + assert_eq!( + <::MinimumDeposit as Get>::get(), + DARWINIA_PROPOSAL_REQUIREMENT + ); + assert_eq!( + <::VoteLockingPeriod as Get>::get(), + 10 * MINUTES + ); + assert_eq!( + <::VotingPeriod as Get>::get(), + 10 * MINUTES + ); + } +} + mod evm { // darwinia use super::mock::*; From 9aa95915cdb7b0a45a5bb3adaa38170f1e56b1c5 Mon Sep 17 00:00:00 2001 From: bear Date: Sun, 8 Oct 2023 16:29:45 +0800 Subject: [PATCH 3/7] Move to macros --- runtime/common/src/test.rs | 195 +++++++++++++++++++- runtime/crab/tests/tests.rs | 2 +- runtime/darwinia/tests/tests.rs | 2 +- runtime/pangolin/tests/tests.rs | 304 +------------------------------- runtime/pangoro/tests/tests.rs | 2 +- 5 files changed, 200 insertions(+), 305 deletions(-) diff --git a/runtime/common/src/test.rs b/runtime/common/src/test.rs index 874106c7a..2f5856121 100644 --- a/runtime/common/src/test.rs +++ b/runtime/common/src/test.rs @@ -413,15 +413,16 @@ macro_rules! impl_account_migration_tests { } #[macro_export] -macro_rules! impl_evm_tests { +macro_rules! impl_ethereum_tests { () => { - mod evm { + mod ethereum { // darwinia use super::mock::*; // frontier + use pallet_ethereum::PostLogContent; use pallet_evm_precompile_dispatch::DispatchValidateT; // substrate - use frame_support::assert_err; + use frame_support::{assert_err, traits::Get}; use sp_core::{H160, U256}; use sp_runtime::{DispatchError, ModuleError}; @@ -435,10 +436,22 @@ macro_rules! impl_evm_tests { assert!(base_extrinsic.ref_time() <= min_ethereum_transaction_weight.ref_time()); } + fn ethereum_constants_are_correctly() { + assert_eq!( + <::ExtraDataLength as Get>::get(), + 64 + ); + assert_eq!( + ::PostLogContent::get() as u8, + PostLogContent::BlockAndTxnHashes as u8 + ); + } + #[test] fn evm_constants_are_correctly() { assert_eq!(BlockGasLimit::get(), U256::from(20_000_000)); assert_eq!(WeightPerGas::get().ref_time(), 18750); + assert_eq!(GasLimitPovSizeRatio::get(), 6); } #[test] @@ -886,3 +899,179 @@ macro_rules! impl_messages_bridge_tests { } }; } + +#[macro_export] +macro_rules! impl_governance_tests { + () => { + mod governance { + // darwinia + use super::mock::*; + // crates.io + use static_assertions::assert_type_eq_all; + // substrate + use frame_support::traits::{AsEnsureOriginWithArg, Get, IsInVec}; + + #[test] + fn preimages_setting_correctly() { + // Origins + assert_type_eq_all!(::ManagerOrigin, Root); + // Values + assert_eq!(<::BaseDeposit as Get>::get(), 500 * UNIT); + assert_eq!(<::ByteDeposit as Get>::get(), darwinia_deposit(0, 1)); + } + + #[test] + fn scheduler_setting_correctly() { + // Origins + assert_type_eq_all!(::ScheduleOrigin, Root); + // Values + assert_eq!(<::MaxScheduledPerBlock as Get>::get(), 50); + } + + #[test] + fn democracy_setting_correctly() { + // Origins + assert_type_eq_all!(::BlacklistOrigin, Root); + assert_type_eq_all!(::CancelProposalOrigin, RootOrAll); + assert_type_eq_all!(::CancellationOrigin, RootOrAtLeastTwoThird); + assert_type_eq_all!(::ExternalDefaultOrigin, RootOrAll); + assert_type_eq_all!(::ExternalMajorityOrigin, RootOrAtLeastHalf); + assert_type_eq_all!(::ExternalOrigin, RootOrAtLeastHalf); + assert_type_eq_all!(::FastTrackOrigin, RootOrAtLeastTwoThird); + assert_type_eq_all!(::InstantOrigin, RootOrAll); + assert_type_eq_all!(::VetoOrigin, pallet_collective::EnsureMember); + + // Values + assert_eq!(<::CooloffPeriod as Get>::get(), 5 * MINUTES); + assert_eq!(<::EnactmentPeriod as Get>::get(), 10 * MINUTES); + assert_eq!(<::FastTrackVotingPeriod as Get>::get(), MINUTES); + assert_eq!(<::InstantAllowed as Get>::get(), true); + assert_eq!(<::LaunchPeriod as Get>::get(), 10 * MINUTES); + assert_eq!(<::MaxBlacklisted as Get>::get(), 100); + assert_eq!(<::MaxDeposits as Get>::get(), 100); + assert_eq!(<::MaxProposals as Get>::get(), 100); + assert_eq!(<::MaxVotes as Get>::get(), 100); + assert_eq!(<::MinimumDeposit as Get>::get(), DARWINIA_PROPOSAL_REQUIREMENT); + assert_eq!(<::VoteLockingPeriod as Get>::get(), 10 * MINUTES); + assert_eq!(<::VotingPeriod as Get>::get(), 10 * MINUTES); + } + + #[test] + fn council_setting_correctly() { + // Origins + assert_type_eq_all!(>::SetMembersOrigin, Root); + // Values + assert_eq!(<>::MaxMembers as Get>::get(), 100); + assert_eq!(<>::MaxProposals as Get>::get(), 100); + assert_eq!(<>::MotionDuration as Get>::get(), 10 * MINUTES); + } + + #[test] + fn technical_setting_correctly() { + // Origins + assert_type_eq_all!(>::SetMembersOrigin, Root); + // Values + assert_eq!(<>::MaxMembers as Get>::get(), 100); + assert_eq!(<>::MaxProposals as Get>::get(), 100); + assert_eq!(<>::MotionDuration as Get>::get(), 10 * MINUTES); + } + } + + } +} + +#[macro_export] +macro_rules! impl_balances_tests { + () => { + mod balances { + // darwinia + use super::mock::*; + // substrate + use frame_support::traits::Get; + + #[test] + fn ensure_constants_correctly() { + assert_eq!(::ExistentialDeposit::get(), 0); + assert_eq!(<::MaxLocks as Get>::get(), 50); + assert_eq!( + <::MaxReserves as Get>::get(), + 50 + ); + } + } + }; +} + +#[macro_export] +macro_rules! impl_assets_tests { + () => { + mod assets { + // darwinia + use super::mock::*; + // crates.io + use static_assertions::assert_type_eq_all; + // substrate + use frame_support::traits::{AsEnsureOriginWithArg, Get, IsInVec}; + + #[test] + fn assets_setting_correctly() { + // Origins + assert_type_eq_all!(::ForceOrigin, Root); + assert_type_eq_all!( + ::CreateOrigin, + AsEnsureOriginWithArg< + frame_system::EnsureSignedBy, AccountId>, + > + ); + + // Values + assert_eq!( + <::AssetAccountDeposit as Get>::get(), + 0 + ); + assert_eq!( + <::MetadataDepositPerByte as Get>::get( + ), + 0 + ); + assert_eq!( + <::MetadataDepositBase as Get>::get(), + 0 + ); + assert_eq!( + <::AssetDeposit as Get>::get(), + 0 + ); + assert_eq!( + <::RemoveItemsLimit as Get>::get(), + 1000 + ); + assert_eq!( + <::StringLimit as Get>::get(), + 50 + ); + } + } + }; +} + +#[macro_export] +macro_rules! impl_message_transact_tests { + () => { + mod message_transact { + // darwinia + use super::mock::*; + use darwinia_message_transact::EnsureLcmpEthOrigin; + // crates.io + use static_assertions::assert_type_eq_all; + + #[test] + fn ensure_origin_correctly() { + assert_type_eq_all!( + ::LcmpEthOrigin, + EnsureLcmpEthOrigin + ); + } + } + }; +} diff --git a/runtime/crab/tests/tests.rs b/runtime/crab/tests/tests.rs index b220641fb..be1f9537f 100644 --- a/runtime/crab/tests/tests.rs +++ b/runtime/crab/tests/tests.rs @@ -20,6 +20,6 @@ pub mod mock; darwinia_common_runtime::impl_weight_tests! {} darwinia_common_runtime::impl_fee_tests! {} -darwinia_common_runtime::impl_evm_tests! {} +darwinia_common_runtime::impl_ethereum_tests! {} darwinia_common_runtime::impl_account_migration_tests! {} darwinia_common_runtime::impl_messages_bridge_tests! {} diff --git a/runtime/darwinia/tests/tests.rs b/runtime/darwinia/tests/tests.rs index b220641fb..be1f9537f 100644 --- a/runtime/darwinia/tests/tests.rs +++ b/runtime/darwinia/tests/tests.rs @@ -20,6 +20,6 @@ pub mod mock; darwinia_common_runtime::impl_weight_tests! {} darwinia_common_runtime::impl_fee_tests! {} -darwinia_common_runtime::impl_evm_tests! {} +darwinia_common_runtime::impl_ethereum_tests! {} darwinia_common_runtime::impl_account_migration_tests! {} darwinia_common_runtime::impl_messages_bridge_tests! {} diff --git a/runtime/pangolin/tests/tests.rs b/runtime/pangolin/tests/tests.rs index 255e44c35..e3e4cf732 100644 --- a/runtime/pangolin/tests/tests.rs +++ b/runtime/pangolin/tests/tests.rs @@ -20,9 +20,13 @@ pub mod mock; darwinia_common_runtime::impl_weight_tests! {} darwinia_common_runtime::impl_fee_tests! {} -// darwinia_common_runtime::impl_evm_tests! {} +darwinia_common_runtime::impl_ethereum_tests! {} darwinia_common_runtime::impl_account_migration_tests! {} darwinia_common_runtime::impl_messages_bridge_tests! {} +darwinia_common_runtime::impl_governance_tests! {} +darwinia_common_runtime::impl_balances_tests! {} +darwinia_common_runtime::impl_assets_tests! {} +darwinia_common_runtime::impl_message_transact_tests! {} #[test] fn precompile_address() { @@ -36,301 +40,3 @@ fn precompile_address() { vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 1027, 1536, 1537, 2048] ); } - -mod message_transact { - // darwinia - use super::mock::*; - use frame_support::traits::EnsureOrigin; - use sp_core::H160; - use static_assertions::assert_type_eq_all; - - #[test] - fn ensure_origin_correctly() { - assert_type_eq_all!( - <::LcmpEthOrigin as EnsureOrigin< - RuntimeOrigin, - >>::Success, - H160 - ); - } -} - -mod balances { - // darwinia - use super::mock::*; - use frame_support::traits::Get; - - #[test] - fn ensure_constants_correctly() { - assert_eq!(::ExistentialDeposit::get(), 0); - assert_eq!(<::MaxLocks as Get>::get(), 50); - assert_eq!(<::MaxReserves as Get>::get(), 50); - } -} - -mod assets { - // darwinia - use super::mock::*; - use frame_support::traits::{AsEnsureOriginWithArg, Get, IsInVec}; - use static_assertions::assert_type_eq_all; - - #[test] - fn ensure_constants_and_origin_correctly() { - assert_type_eq_all!(::ForceOrigin, Root); - assert_type_eq_all!( - ::CreateOrigin, - AsEnsureOriginWithArg, AccountId>> - ); - - assert_eq!( - <::AssetAccountDeposit as Get>::get(), - 0 - ); - assert_eq!( - <::MetadataDepositPerByte as Get>::get(), - 0 - ); - assert_eq!( - <::MetadataDepositBase as Get>::get(), - 0 - ); - assert_eq!(<::AssetDeposit as Get>::get(), 0); - assert_eq!(<::RemoveItemsLimit as Get>::get(), 1000); - assert_eq!(<::StringLimit as Get>::get(), 50); - } -} - -mod governance { - // darwinia - use super::mock::*; - use frame_support::traits::{AsEnsureOriginWithArg, Get, IsInVec}; - use static_assertions::assert_type_eq_all; - - #[test] - fn preimages_setting_correctly() { - assert_eq!( - <::BaseDeposit as Get>::get(), - 500 * UNIT - ); - assert_eq!( - <::ByteDeposit as Get>::get(), - darwinia_deposit(0, 1) - ); - assert_type_eq_all!(::ManagerOrigin, Root); - } - - #[test] - fn scheduler_setting_correctly() { - assert_eq!( - <::MaxScheduledPerBlock as Get>::get(), - 50 - ); - assert_type_eq_all!(::ScheduleOrigin, Root); - } - - #[test] - fn democracy_setting_correctly() { - assert_type_eq_all!(::BlacklistOrigin, Root); - assert_type_eq_all!( - ::CancelProposalOrigin, - RootOrAll - ); - assert_type_eq_all!( - ::CancellationOrigin, - RootOrAtLeastTwoThird - ); - assert_type_eq_all!( - ::ExternalDefaultOrigin, - RootOrAll - ); - assert_type_eq_all!( - ::ExternalMajorityOrigin, - RootOrAtLeastHalf - ); - assert_type_eq_all!( - ::ExternalOrigin, - RootOrAtLeastHalf - ); - assert_type_eq_all!( - ::FastTrackOrigin, - RootOrAtLeastTwoThird - ); - assert_type_eq_all!( - ::InstantOrigin, - RootOrAll - ); - assert_type_eq_all!(::VetoOrigin, pallet_collective::EnsureMember); - - assert_eq!( - <::CooloffPeriod as Get>::get(), - 5 * MINUTES - ); - assert_eq!( - <::EnactmentPeriod as Get>::get(), - 10 * MINUTES - ); - assert_eq!( - <::FastTrackVotingPeriod as Get>::get(), - MINUTES - ); - assert_eq!( - <::InstantAllowed as Get>::get(), - true - ); - assert_eq!( - <::LaunchPeriod as Get>::get(), - 10 * MINUTES - ); - assert_eq!(<::MaxBlacklisted as Get>::get(), 100); - assert_eq!(<::MaxDeposits as Get>::get(), 100); - assert_eq!(<::MaxProposals as Get>::get(), 100); - assert_eq!(<::MaxVotes as Get>::get(), 100); - assert_eq!( - <::MinimumDeposit as Get>::get(), - DARWINIA_PROPOSAL_REQUIREMENT - ); - assert_eq!( - <::VoteLockingPeriod as Get>::get(), - 10 * MINUTES - ); - assert_eq!( - <::VotingPeriod as Get>::get(), - 10 * MINUTES - ); - } -} - -mod evm { - // darwinia - use super::mock::*; - // frontier - use pallet_ethereum::PostLogContent; - use pallet_evm_precompile_dispatch::DispatchValidateT; - // substrate - use frame_support::{assert_err, traits::Get}; - use sp_core::{H160, U256}; - use sp_runtime::{DispatchError, ModuleError}; - - #[test] - fn configured_base_extrinsic_weight_is_evm_compatible() { - let min_ethereum_transaction_weight = WeightPerGas::get() * 21_000; - let base_extrinsic = ::BlockWeights::get() - .get(frame_support::dispatch::DispatchClass::Normal) - .base_extrinsic; - - assert!(base_extrinsic.ref_time() <= min_ethereum_transaction_weight.ref_time()); - } - - fn ethereum_constants_are_correctly() { - assert_eq!(<::ExtraDataLength as Get>::get(), 64); - assert_eq!( - ::PostLogContent::get() as u8, - PostLogContent::BlockAndTxnHashes as u8 - ); - } - - #[test] - fn evm_constants_are_correctly() { - assert_eq!(BlockGasLimit::get(), U256::from(20_000_000)); - assert_eq!(WeightPerGas::get().ref_time(), 18750); - assert_eq!(GasLimitPovSizeRatio::get(), 6); - } - - #[test] - fn pallet_evm_calls_only_callable_by_root() { - ExtBuilder::default().build().execute_with(|| { - // https://github.com/darwinia-network/darwinia/blob/5923b2e0526b67fe05cee6e4e592ceca80e8f2ff/runtime/darwinia/src/pallets/evm.rs#L136 - assert_err!( - EVM::call( - RuntimeOrigin::signed(H160::default().into()), - H160::default(), - H160::default(), - vec![], - U256::default(), - 1000000, - U256::from(1_000_000), - None, - None, - vec![], - ), - DispatchError::BadOrigin - ); - - if let Err(dispatch_info_with_err) = EVM::call( - RuntimeOrigin::root(), - H160::default(), - H160::default(), - vec![], - U256::default(), - 1000000, - U256::from(1_000_000), - None, - None, - vec![], - ) { - assert_eq!( - dispatch_info_with_err.error, - DispatchError::Module(ModuleError { - index: 37, - error: [4, 0, 0, 0], - message: Some("GasPriceTooLow") - }) - ); - } - }); - } - - #[test] - fn dispatch_validator_filter_runtime_calls() { - ExtBuilder::default().build().execute_with(|| { - assert!(DarwiniaDispatchValidator::validate_before_dispatch( - &H160::default().into(), - &RuntimeCall::System(frame_system::Call::remark { remark: vec![] }) - ) - .is_none()); - - assert!(DarwiniaDispatchValidator::validate_before_dispatch( - &H160::default().into(), - // forbidden call - &RuntimeCall::EVM(pallet_evm::Call::call { - source: H160::default(), - target: H160::default(), - input: vec![], - value: U256::default(), - gas_limit: 1000000, - max_fee_per_gas: U256::from(1_000_000), - max_priority_fee_per_gas: None, - nonce: None, - access_list: vec![], - }) - ) - .is_some()); - }); - } - - #[test] - fn dispatch_validator_filter_dispatch_class() { - ExtBuilder::default().build().execute_with(|| { - // Default class - assert!(DarwiniaDispatchValidator::validate_before_dispatch( - &H160::default().into(), - &RuntimeCall::System(frame_system::Call::remark { remark: vec![] }) - ) - .is_none()); - - // Operational class - assert!(DarwiniaDispatchValidator::validate_before_dispatch( - &H160::default().into(), - &RuntimeCall::System(frame_system::Call::set_heap_pages { pages: 20 }) - ) - .is_none()); - - // Mandatory class - assert!(DarwiniaDispatchValidator::validate_before_dispatch( - &H160::default().into(), - &RuntimeCall::Timestamp(pallet_timestamp::Call::set { now: 100 }) - ) - .is_some()); - }); - } -} diff --git a/runtime/pangoro/tests/tests.rs b/runtime/pangoro/tests/tests.rs index b220641fb..be1f9537f 100644 --- a/runtime/pangoro/tests/tests.rs +++ b/runtime/pangoro/tests/tests.rs @@ -20,6 +20,6 @@ pub mod mock; darwinia_common_runtime::impl_weight_tests! {} darwinia_common_runtime::impl_fee_tests! {} -darwinia_common_runtime::impl_evm_tests! {} +darwinia_common_runtime::impl_ethereum_tests! {} darwinia_common_runtime::impl_account_migration_tests! {} darwinia_common_runtime::impl_messages_bridge_tests! {} From 39eb1ce674347e02dc36f85e94e046a539aee1d3 Mon Sep 17 00:00:00 2001 From: bear Date: Sun, 8 Oct 2023 17:39:44 +0800 Subject: [PATCH 4/7] Spread to other runtimes --- runtime/common/src/test.rs | 8 --- runtime/crab/tests/tests.rs | 66 ++++++++++++++++++++++ runtime/darwinia/src/pallets/democracy.rs | 4 +- runtime/darwinia/tests/tests.rs | 66 ++++++++++++++++++++++ runtime/pangolin/tests/tests.rs | 67 ++++++++++++++++++++--- runtime/pangoro/tests/tests.rs | 66 ++++++++++++++++++++++ 6 files changed, 258 insertions(+), 19 deletions(-) diff --git a/runtime/common/src/test.rs b/runtime/common/src/test.rs index 2f5856121..43bfec3eb 100644 --- a/runtime/common/src/test.rs +++ b/runtime/common/src/test.rs @@ -942,18 +942,12 @@ macro_rules! impl_governance_tests { assert_type_eq_all!(::VetoOrigin, pallet_collective::EnsureMember); // Values - assert_eq!(<::CooloffPeriod as Get>::get(), 5 * MINUTES); - assert_eq!(<::EnactmentPeriod as Get>::get(), 10 * MINUTES); - assert_eq!(<::FastTrackVotingPeriod as Get>::get(), MINUTES); assert_eq!(<::InstantAllowed as Get>::get(), true); - assert_eq!(<::LaunchPeriod as Get>::get(), 10 * MINUTES); assert_eq!(<::MaxBlacklisted as Get>::get(), 100); assert_eq!(<::MaxDeposits as Get>::get(), 100); assert_eq!(<::MaxProposals as Get>::get(), 100); assert_eq!(<::MaxVotes as Get>::get(), 100); assert_eq!(<::MinimumDeposit as Get>::get(), DARWINIA_PROPOSAL_REQUIREMENT); - assert_eq!(<::VoteLockingPeriod as Get>::get(), 10 * MINUTES); - assert_eq!(<::VotingPeriod as Get>::get(), 10 * MINUTES); } #[test] @@ -963,7 +957,6 @@ macro_rules! impl_governance_tests { // Values assert_eq!(<>::MaxMembers as Get>::get(), 100); assert_eq!(<>::MaxProposals as Get>::get(), 100); - assert_eq!(<>::MotionDuration as Get>::get(), 10 * MINUTES); } #[test] @@ -973,7 +966,6 @@ macro_rules! impl_governance_tests { // Values assert_eq!(<>::MaxMembers as Get>::get(), 100); assert_eq!(<>::MaxProposals as Get>::get(), 100); - assert_eq!(<>::MotionDuration as Get>::get(), 10 * MINUTES); } } diff --git a/runtime/crab/tests/tests.rs b/runtime/crab/tests/tests.rs index be1f9537f..9b6e042aa 100644 --- a/runtime/crab/tests/tests.rs +++ b/runtime/crab/tests/tests.rs @@ -23,3 +23,69 @@ darwinia_common_runtime::impl_fee_tests! {} darwinia_common_runtime::impl_ethereum_tests! {} darwinia_common_runtime::impl_account_migration_tests! {} darwinia_common_runtime::impl_messages_bridge_tests! {} +darwinia_common_runtime::impl_governance_tests! {} +darwinia_common_runtime::impl_balances_tests! {} +darwinia_common_runtime::impl_assets_tests! {} +darwinia_common_runtime::impl_message_transact_tests! {} + +mod specific_setting { + // darwinia + use crate::mock::*; + // substrate + use frame_support::traits::Get; + + #[test] + fn precompile_address() { + assert_eq!( + CrabPrecompiles::::used_addresses() + .iter() + .map(|a| a.to_low_u64_be()) + .collect::>(), + vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 1536, 1537, 2048] + ); + } + + #[test] + fn democracy_mod() { + assert_eq!( + <::CooloffPeriod as Get>::get(), + 7 * DAYS + ); + assert_eq!( + <::EnactmentPeriod as Get>::get(), + 8 * DAYS + ); + assert_eq!( + <::FastTrackVotingPeriod as Get>::get(), + 3 * HOURS + ); + assert_eq!( + <::LaunchPeriod as Get>::get(), + 7 * DAYS + ); + assert_eq!( + <::VoteLockingPeriod as Get>::get(), + 8 * DAYS + ); + assert_eq!( + <::VotingPeriod as Get>::get(), + 7 * DAYS + ); + } + + #[test] + fn collective_mod() { + assert_eq!( + <>::MotionDuration as Get< + u32, + >>::get(), + 3 * DAYS + ); + assert_eq!( + <>::MotionDuration as Get< + u32, + >>::get(), + 3 * DAYS + ); + } +} diff --git a/runtime/darwinia/src/pallets/democracy.rs b/runtime/darwinia/src/pallets/democracy.rs index 206137e13..9909d162f 100644 --- a/runtime/darwinia/src/pallets/democracy.rs +++ b/runtime/darwinia/src/pallets/democracy.rs @@ -44,7 +44,7 @@ impl pallet_democracy::Config for Runtime { type FastTrackVotingPeriod = ConstU32<{ 3 * HOURS }>; type InstantAllowed = ConstBool; type InstantOrigin = RootOrAll; - type LaunchPeriod = ConstU32<{ 28 * DAYS }>; + type LaunchPeriod = ConstU32<{ ENACTMENT_PERIOD }>; type MaxBlacklisted = ConstU32<100>; type MaxDeposits = ConstU32<100>; type MaxProposals = ConstU32<100>; @@ -60,6 +60,6 @@ impl pallet_democracy::Config for Runtime { // only do it once and it lasts only for the cool-off period. type VetoOrigin = pallet_collective::EnsureMember; type VoteLockingPeriod = ConstU32; - type VotingPeriod = ConstU32<{ 28 * DAYS }>; + type VotingPeriod = ConstU32<{ ENACTMENT_PERIOD }>; type WeightInfo = weights::pallet_democracy::WeightInfo; } diff --git a/runtime/darwinia/tests/tests.rs b/runtime/darwinia/tests/tests.rs index be1f9537f..75458676b 100644 --- a/runtime/darwinia/tests/tests.rs +++ b/runtime/darwinia/tests/tests.rs @@ -23,3 +23,69 @@ darwinia_common_runtime::impl_fee_tests! {} darwinia_common_runtime::impl_ethereum_tests! {} darwinia_common_runtime::impl_account_migration_tests! {} darwinia_common_runtime::impl_messages_bridge_tests! {} +darwinia_common_runtime::impl_governance_tests! {} +darwinia_common_runtime::impl_balances_tests! {} +darwinia_common_runtime::impl_assets_tests! {} +darwinia_common_runtime::impl_message_transact_tests! {} + +mod specific_setting { + // darwinia + use crate::mock::*; + // substrate + use frame_support::traits::Get; + + #[test] + fn precompile_address() { + assert_eq!( + DarwiniaPrecompiles::::used_addresses() + .iter() + .map(|a| a.to_low_u64_be()) + .collect::>(), + vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 1536, 1537, 2048] + ); + } + + #[test] + fn democracy_mod() { + assert_eq!( + <::CooloffPeriod as Get>::get(), + 7 * DAYS + ); + assert_eq!( + <::EnactmentPeriod as Get>::get(), + 28 * DAYS + ); + assert_eq!( + <::FastTrackVotingPeriod as Get>::get(), + 3 * HOURS + ); + assert_eq!( + <::LaunchPeriod as Get>::get(), + 28 * DAYS + ); + assert_eq!( + <::VoteLockingPeriod as Get>::get(), + 28 * DAYS + ); + assert_eq!( + <::VotingPeriod as Get>::get(), + 28 * DAYS + ); + } + + #[test] + fn collective_mod() { + assert_eq!( + <>::MotionDuration as Get< + u32, + >>::get(), + 7 * DAYS + ); + assert_eq!( + <>::MotionDuration as Get< + u32, + >>::get(), + 7 * DAYS + ); + } +} diff --git a/runtime/pangolin/tests/tests.rs b/runtime/pangolin/tests/tests.rs index e3e4cf732..682ac17df 100644 --- a/runtime/pangolin/tests/tests.rs +++ b/runtime/pangolin/tests/tests.rs @@ -28,15 +28,64 @@ darwinia_common_runtime::impl_balances_tests! {} darwinia_common_runtime::impl_assets_tests! {} darwinia_common_runtime::impl_message_transact_tests! {} -#[test] -fn precompile_address() { +mod specific_setting { + // darwinia use crate::mock::*; + // substrate + use frame_support::traits::Get; - assert_eq!( - PangolinPrecompiles::::used_addresses() - .iter() - .map(|a| a.to_low_u64_be()) - .collect::>(), - vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 1027, 1536, 1537, 2048] - ); + #[test] + fn precompile_address() { + assert_eq!( + PangolinPrecompiles::::used_addresses() + .iter() + .map(|a| a.to_low_u64_be()) + .collect::>(), + vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 1027, 1536, 1537, 2048] + ); + } + + #[test] + fn democracy_mod() { + assert_eq!( + <::CooloffPeriod as Get>::get(), + 5 * MINUTES + ); + assert_eq!( + <::EnactmentPeriod as Get>::get(), + 10 * MINUTES + ); + assert_eq!( + <::FastTrackVotingPeriod as Get>::get(), + MINUTES + ); + assert_eq!( + <::LaunchPeriod as Get>::get(), + 10 * MINUTES + ); + assert_eq!( + <::VoteLockingPeriod as Get>::get(), + 10 * MINUTES + ); + assert_eq!( + <::VotingPeriod as Get>::get(), + 10 * MINUTES + ); + } + + #[test] + fn collective_mod() { + assert_eq!( + <>::MotionDuration as Get< + u32, + >>::get(), + 10 * MINUTES + ); + assert_eq!( + <>::MotionDuration as Get< + u32, + >>::get(), + 10 * MINUTES + ); + } } diff --git a/runtime/pangoro/tests/tests.rs b/runtime/pangoro/tests/tests.rs index be1f9537f..db76c3363 100644 --- a/runtime/pangoro/tests/tests.rs +++ b/runtime/pangoro/tests/tests.rs @@ -23,3 +23,69 @@ darwinia_common_runtime::impl_fee_tests! {} darwinia_common_runtime::impl_ethereum_tests! {} darwinia_common_runtime::impl_account_migration_tests! {} darwinia_common_runtime::impl_messages_bridge_tests! {} +darwinia_common_runtime::impl_governance_tests! {} +darwinia_common_runtime::impl_balances_tests! {} +darwinia_common_runtime::impl_assets_tests! {} +darwinia_common_runtime::impl_message_transact_tests! {} + +mod specific_setting { + // darwinia + use crate::mock::*; + // substrate + use frame_support::traits::Get; + + #[test] + fn precompile_address() { + assert_eq!( + PangoroPrecompiles::::used_addresses() + .iter() + .map(|a| a.to_low_u64_be()) + .collect::>(), + vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 1536, 1537, 2048] + ); + } + + #[test] + fn democracy_mod() { + assert_eq!( + <::CooloffPeriod as Get>::get(), + 5 * MINUTES + ); + assert_eq!( + <::EnactmentPeriod as Get>::get(), + 10 * MINUTES + ); + assert_eq!( + <::FastTrackVotingPeriod as Get>::get(), + MINUTES + ); + assert_eq!( + <::LaunchPeriod as Get>::get(), + 10 * MINUTES + ); + assert_eq!( + <::VoteLockingPeriod as Get>::get(), + 10 * MINUTES + ); + assert_eq!( + <::VotingPeriod as Get>::get(), + 10 * MINUTES + ); + } + + #[test] + fn collective_mod() { + assert_eq!( + <>::MotionDuration as Get< + u32, + >>::get(), + 10 * MINUTES + ); + assert_eq!( + <>::MotionDuration as Get< + u32, + >>::get(), + 10 * MINUTES + ); + } +} From 43eba04f7b983d66d4bcbc7e1baffcda1a16ef88 Mon Sep 17 00:00:00 2001 From: bear Date: Mon, 9 Oct 2023 10:23:42 +0800 Subject: [PATCH 5/7] Add treasury test cases --- runtime/common/src/test.rs | 13 +++++++++++-- runtime/crab/tests/tests.rs | 8 ++++++++ runtime/darwinia/tests/tests.rs | 8 ++++++++ runtime/pangolin/tests/tests.rs | 8 ++++++++ runtime/pangoro/tests/tests.rs | 8 ++++++++ 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/runtime/common/src/test.rs b/runtime/common/src/test.rs index 43bfec3eb..4dfcc9b8f 100644 --- a/runtime/common/src/test.rs +++ b/runtime/common/src/test.rs @@ -909,7 +909,7 @@ macro_rules! impl_governance_tests { // crates.io use static_assertions::assert_type_eq_all; // substrate - use frame_support::traits::{AsEnsureOriginWithArg, Get, IsInVec}; + use frame_support::traits::{AsEnsureOriginWithArg, Get, IsInVec, NeverEnsureOrigin}; #[test] fn preimages_setting_correctly() { @@ -967,8 +967,17 @@ macro_rules! impl_governance_tests { assert_eq!(<>::MaxMembers as Get>::get(), 100); assert_eq!(<>::MaxProposals as Get>::get(), 100); } - } + #[test] + fn treasury_setting_correctly() { + // Origins + assert_type_eq_all!(::ApproveOrigin, Root); + assert_type_eq_all!(::RejectOrigin, RootOrAll); + assert_type_eq_all!(::SpendOrigin, NeverEnsureOrigin); + // Values + assert_eq!(<::MaxApprovals as Get>::get(), 100); + } + } } } diff --git a/runtime/crab/tests/tests.rs b/runtime/crab/tests/tests.rs index 9b6e042aa..8015d1f29 100644 --- a/runtime/crab/tests/tests.rs +++ b/runtime/crab/tests/tests.rs @@ -88,4 +88,12 @@ mod specific_setting { 3 * DAYS ); } + + #[test] + fn treasury_mod() { + assert_eq!( + <::SpendPeriod as Get>::get(), + 6 * DAYS + ); + } } diff --git a/runtime/darwinia/tests/tests.rs b/runtime/darwinia/tests/tests.rs index 75458676b..2bf7c583a 100644 --- a/runtime/darwinia/tests/tests.rs +++ b/runtime/darwinia/tests/tests.rs @@ -88,4 +88,12 @@ mod specific_setting { 7 * DAYS ); } + + #[test] + fn treasury_mod() { + assert_eq!( + <::SpendPeriod as Get>::get(), + 24 * DAYS + ); + } } diff --git a/runtime/pangolin/tests/tests.rs b/runtime/pangolin/tests/tests.rs index 682ac17df..5f7a84db0 100644 --- a/runtime/pangolin/tests/tests.rs +++ b/runtime/pangolin/tests/tests.rs @@ -88,4 +88,12 @@ mod specific_setting { 10 * MINUTES ); } + + #[test] + fn treasury_mod() { + assert_eq!( + <::SpendPeriod as Get>::get(), + 10 * MINUTES + ); + } } diff --git a/runtime/pangoro/tests/tests.rs b/runtime/pangoro/tests/tests.rs index db76c3363..dedd50560 100644 --- a/runtime/pangoro/tests/tests.rs +++ b/runtime/pangoro/tests/tests.rs @@ -88,4 +88,12 @@ mod specific_setting { 10 * MINUTES ); } + + #[test] + fn treasury_mod() { + assert_eq!( + <::SpendPeriod as Get>::get(), + 10 * MINUTES + ); + } } From a143d32027dbaad97000e36512e43e9e1c38fa8e Mon Sep 17 00:00:00 2001 From: bear Date: Mon, 9 Oct 2023 10:51:35 +0800 Subject: [PATCH 6/7] Add membership test cases --- runtime/common/src/test.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/runtime/common/src/test.rs b/runtime/common/src/test.rs index 4dfcc9b8f..93fe471b8 100644 --- a/runtime/common/src/test.rs +++ b/runtime/common/src/test.rs @@ -977,6 +977,20 @@ macro_rules! impl_governance_tests { // Values assert_eq!(<::MaxApprovals as Get>::get(), 100); } + + #[test] + fn membership_setting_correctly() { + // Origins + assert_type_eq_all!(>::AddOrigin, Root); + assert_type_eq_all!(>::MembershipChanged, TechnicalCommittee); + assert_type_eq_all!(>::MembershipInitialized, TechnicalCommittee); + assert_type_eq_all!(>::PrimeOrigin, Root); + assert_type_eq_all!(>::RemoveOrigin, Root); + assert_type_eq_all!(>::ResetOrigin, Root); + assert_type_eq_all!(>::SwapOrigin, Root); + // Values + assert_eq!(<>::MaxMembers as Get>::get(), COLLECTIVE_MAX_MEMBERS); + } } } } From 65b8ed946fba068bdcdfafda2fa9538301400239 Mon Sep 17 00:00:00 2001 From: bear Date: Mon, 9 Oct 2023 13:33:36 +0800 Subject: [PATCH 7/7] Fix CI --- runtime/common/src/test.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/runtime/common/src/test.rs b/runtime/common/src/test.rs index 93fe471b8..424e8bace 100644 --- a/runtime/common/src/test.rs +++ b/runtime/common/src/test.rs @@ -1006,7 +1006,8 @@ macro_rules! impl_balances_tests { #[test] fn ensure_constants_correctly() { - assert_eq!(::ExistentialDeposit::get(), 0); + // the value is 1 under runtime-benchmarks feature + assert_eq!(::ExistentialDeposit::get(), 1); assert_eq!(<::MaxLocks as Get>::get(), 50); assert_eq!( <::MaxReserves as Get>::get(), @@ -1040,15 +1041,16 @@ macro_rules! impl_assets_tests { ); // Values - assert_eq!( - <::AssetAccountDeposit as Get>::get(), - 0 - ); assert_eq!( <::MetadataDepositPerByte as Get>::get( ), 0 ); + // the value is 1 under runtime-benchmarks feature + assert_eq!( + <::AssetAccountDeposit as Get>::get(), + 1 + ); assert_eq!( <::MetadataDepositBase as Get>::get(), 0