From 18466754aa7da47c1f2d161032db4ee76958a0e2 Mon Sep 17 00:00:00 2001 From: clangenb <37865735+clangenb@users.noreply.github.com> Date: Mon, 3 Jun 2024 06:44:53 +0200 Subject: [PATCH] polkadot v1.12.0 (#1003) * [tokens] update fungible implementation * [benchmarking] update benchmarks * [asset-registry] add `XcmRecorder` associated type to mocks * [tokens] add preservation arg to tests * [xtokens] add `XcmRecorder` associated type to mocks * fmt * [benchmarking] fix tests * add updated Cargo.dev.toml --- Cargo.dev.toml | 54 +++++++++--------- asset-registry/src/mock/para.rs | 1 + asset-registry/src/mock/relay.rs | 1 + benchmarking/Cargo.toml | 2 + benchmarking/src/lib.rs | 78 ++++++++++++-------------- benchmarking/src/tests.rs | 72 ++++++++++-------------- tokens/src/impls.rs | 7 ++- tokens/src/lib.rs | 12 +++- tokens/src/tests_events.rs | 1 + tokens/src/tests_fungibles.rs | 10 +++- xtokens/src/mock/para.rs | 1 + xtokens/src/mock/para_relative_view.rs | 1 + xtokens/src/mock/para_teleport.rs | 1 + xtokens/src/mock/relay.rs | 1 + 14 files changed, 127 insertions(+), 115 deletions(-) diff --git a/Cargo.dev.toml b/Cargo.dev.toml index 42f7e0933..6e7de60ee 100644 --- a/Cargo.dev.toml +++ b/Cargo.dev.toml @@ -37,35 +37,35 @@ scale-info = { version = "2.10.0", default-features = false, features = ["derive serde = { version = "1.0.189" } parity-scale-codec = { version = "3.6.5", default-features = false, features = ["max-encoded-len"] } -cumulus-pallet-xcm = { version = "0.11.0", default-features = false } -cumulus-primitives-core = { version = "0.11.0", default-features = false } -frame-benchmarking = { version = "32.0.0", default-features = false } -frame-support = { version = "32.0.0", default-features = false } -frame-system = { version = "32.0.0", default-features = false } -pallet-balances = { version = "33.0.0", default-features = false } -pallet-elections-phragmen = { version = "33.0.0", default-features = false } -pallet-message-queue = { version = "35.0.0", default-features = false } -pallet-preimage = { version = "32.0.0", default-features = false } -pallet-root-testing = { version = "8.0.0", default-features = false } -pallet-scheduler = { version = "33.0.0", default-features = false } -pallet-timestamp = { version = "31.0.0", default-features = false } -pallet-treasury = { version = "31.0.0", default-features = false } -pallet-xcm = { version = "11.0.0", default-features = false } -polkadot-parachain-primitives = { version = "10.0.0", default-features = false } -polkadot-runtime-common = { version = "11.0.0", default-features = false } -polkadot-runtime-parachains = { version = "11.0.0", default-features = false } -sp-api = { version = "30.0.0", default-features = false } -sp-application-crypto = { version = "34.0.0", default-features = false } +cumulus-pallet-xcm = { version = "0.13.0", default-features = false } +cumulus-primitives-core = { version = "0.13.0", default-features = false } +frame-benchmarking = { version = "34.0.0", default-features = false } +frame-support = { version = "34.0.0", default-features = false } +frame-system = { version = "34.0.1", default-features = false } +pallet-balances = { version = "35.0.0", default-features = false } +pallet-elections-phragmen = { version = "35.0.0", default-features = false } +pallet-message-queue = { version = "37.0.0", default-features = false } +pallet-preimage = { version = "34.0.0", default-features = false } +pallet-root-testing = { version = "10.0.0", default-features = false } +pallet-scheduler = { version = "35.0.0", default-features = false } +pallet-timestamp = { version = "33.0.0", default-features = false } +pallet-treasury = { version = "33.0.0", default-features = false } +pallet-xcm = { version = "13.0.0", default-features = false } +polkadot-parachain-primitives = { version = "12.0.0", default-features = false } +polkadot-runtime-common = { version = "13.0.0", default-features = false } +polkadot-runtime-parachains = { version = "13.0.0", default-features = false } +sp-api = { version = "32.0.0", default-features = false } +sp-application-crypto = { version = "36.0.0", default-features = false } sp-arithmetic = { version = "26.0.0", default-features = false } -sp-core = { version = "32.0.0", default-features = false } -sp-io = { version = "34.0.0", default-features = false } -sp-runtime = { version = "35.0.0", default-features = false } +sp-core = { version = "33.0.1", default-features = false } +sp-io = { version = "36.0.0", default-features = false } +sp-runtime = { version = "37.0.0", default-features = false } sp-runtime-interface = { version = "27.0.0", default-features = false } -sp-staking = { version = "30.0.0", default-features = false } +sp-staking = { version = "32.0.0", default-features = false } sp-std = { version = "14.0.0", default-features = false } sp-storage = { version = "21.0.0", default-features = false } -xcm = { version = "11.0.0", package = "staging-xcm", default-features = false } -xcm-builder = { version = "11.0.0", package = "staging-xcm-builder", default-features = false } -xcm-executor = { version = "11.0.0", package = "staging-xcm-executor", default-features = false } +xcm = { version = "13.0.1", package = "staging-xcm", default-features = false } +xcm-builder = { version = "13.0.0", package = "staging-xcm-builder", default-features = false } +xcm-executor = { version = "13.0.0", package = "staging-xcm-executor", default-features = false } -xcm-simulator = { version = "11.0.0" } +xcm-simulator = { version = "13.0.0" } diff --git a/asset-registry/src/mock/para.rs b/asset-registry/src/mock/para.rs index 6829e4225..2121a2153 100644 --- a/asset-registry/src/mock/para.rs +++ b/asset-registry/src/mock/para.rs @@ -236,6 +236,7 @@ impl Config for XcmConfig { type HrmpNewChannelOpenRequestHandler = (); type HrmpChannelAcceptedHandler = (); type HrmpChannelClosingHandler = (); + type XcmRecorder = (); } impl cumulus_pallet_xcm::Config for Runtime { diff --git a/asset-registry/src/mock/relay.rs b/asset-registry/src/mock/relay.rs index 05eedfd6b..c3f867111 100644 --- a/asset-registry/src/mock/relay.rs +++ b/asset-registry/src/mock/relay.rs @@ -113,6 +113,7 @@ impl Config for XcmConfig { type HrmpNewChannelOpenRequestHandler = (); type HrmpChannelAcceptedHandler = (); type HrmpChannelClosingHandler = (); + type XcmRecorder = (); } pub type LocalOriginToLocation = SignedToAccountId32; diff --git a/benchmarking/Cargo.toml b/benchmarking/Cargo.toml index 799064dac..e2f33701e 100644 --- a/benchmarking/Cargo.toml +++ b/benchmarking/Cargo.toml @@ -17,6 +17,7 @@ serde = { workspace = true, optional = true } frame-benchmarking = { workspace = true } frame-support = { workspace = true } sp-api = { workspace = true } +sp-core = { workspace = true } sp-io = { workspace = true } sp-runtime = { workspace = true } sp-runtime-interface = { workspace = true } @@ -38,6 +39,7 @@ std = [ "scale-info/std", "serde", "sp-api/std", + "sp-core/std", "sp-io/std", "sp-runtime-interface/std", "sp-runtime/std", diff --git a/benchmarking/src/lib.rs b/benchmarking/src/lib.rs index 53a306559..fd375120e 100644 --- a/benchmarking/src/lib.rs +++ b/benchmarking/src/lib.rs @@ -7,8 +7,10 @@ mod tests; pub use frame_benchmarking::{ benchmarking, whitelisted_caller, BenchmarkBatch, BenchmarkConfig, BenchmarkError, BenchmarkList, - BenchmarkMetadata, BenchmarkParameter, BenchmarkResult, Benchmarking, BenchmarkingSetup, + BenchmarkMetadata, BenchmarkParameter, BenchmarkRecording, BenchmarkResult, Benchmarking, BenchmarkingSetup, + Recording, }; + #[cfg(feature = "std")] pub use frame_benchmarking::{Analysis, BenchmarkSelector}; #[doc(hidden)] @@ -20,6 +22,8 @@ pub use parity_scale_codec; #[doc(hidden)] pub use paste; #[doc(hidden)] +pub use sp_core::defer; +#[doc(hidden)] pub use sp_io::storage::root as storage_root; #[doc(hidden)] pub use sp_runtime::traits::Zero; @@ -176,7 +180,6 @@ macro_rules! whitelist_account { /// }); /// } /// ``` - #[macro_export] macro_rules! runtime_benchmarks { ( @@ -626,9 +629,10 @@ macro_rules! benchmark_backend { fn instance( &self, + recording: &mut impl $crate::Recording, components: &[($crate::BenchmarkParameter, u32)], verify: bool - ) -> Result<$crate::Box Result<(), $crate::BenchmarkError>>, $crate::BenchmarkError> { + ) -> Result<(), $crate::BenchmarkError> { $( // Prepare instance let $param = components.iter() @@ -642,13 +646,14 @@ macro_rules! benchmark_backend { $( $param_instancer ; )* $( $post )* - Ok($crate::Box::new(move || -> Result<(), $crate::BenchmarkError> { - $eval; - if verify { - $postcode; - } - Ok(()) - })) + recording.start(); + $eval; + recording.stop(); + + if verify { + $postcode; + } + Ok(()) } } }; @@ -696,14 +701,15 @@ macro_rules! selected_benchmark { fn instance( &self, + recording: &mut impl $crate::Recording, components: &[($crate::BenchmarkParameter, u32)], verify: bool - ) -> Result<$crate::Box Result<(), $crate::BenchmarkError>>, $crate::BenchmarkError> { + ) -> Result<(), $crate::BenchmarkError> { match self { $( Self::$bench => < $bench as $crate::BenchmarkingSetup<$runtime $(, $bench_inst)? > - >::instance(&$bench, components, verify), + >::instance(&$bench, recording, components, verify), )* } } @@ -781,14 +787,7 @@ macro_rules! impl_benchmark { let mut results: $crate::Vec<$crate::BenchmarkResult> = $crate::Vec::new(); - // Always do at least one internal repeat... - for _ in 0 .. internal_repeats.max(1) { - // Set up the externalities environment for the setup we want to - // benchmark. - let closure_to_benchmark = < - SelectedBenchmark as $crate::BenchmarkingSetup<$runtime $(, $instance)?> - >::instance(&selected_benchmark, c, verify)?; - + let on_before_start = || { // Set the block number to at least 1 so events are deposited. if $crate::Zero::is_zero(&frame_system::Pallet::<$runtime>::block_number()) { frame_system::Pallet::<$runtime>::set_block_number(1u32.into()); @@ -800,6 +799,12 @@ macro_rules! impl_benchmark { // Reset the read/write counter so we don't count operations in the setup process. $crate::benchmarking::reset_read_write_count(); + }; + + // Always do at least one internal repeat... + for _ in 0 .. internal_repeats.max(1) { + // Always reset the state after the benchmark. + $crate::defer!($crate::benchmarking::wipe_db()); // Time the extrinsic logic. $crate::log::trace!( @@ -807,20 +812,12 @@ macro_rules! impl_benchmark { "Start Benchmark: {:?}", c ); - let start_pov = $crate::benchmarking::proof_size(); - let start_extrinsic = $crate::benchmarking::current_time(); - - closure_to_benchmark()?; - - let finish_extrinsic = $crate::benchmarking::current_time(); - let end_pov = $crate::benchmarking::proof_size(); + let mut recording = $crate::BenchmarkRecording::new(&on_before_start); + >::instance(&selected_benchmark, &mut recording, c, verify)?; // Calculate the diff caused by the benchmark. - let elapsed_extrinsic = finish_extrinsic.saturating_sub(start_extrinsic); - let diff_pov = match (start_pov, end_pov) { - (Some(start), Some(end)) => end.saturating_sub(start), - _ => Default::default(), - }; + let elapsed_extrinsic = recording.elapsed_extrinsic().expect("elapsed time should be recorded"); + let diff_pov = recording.diff_pov().unwrap_or_default(); // Commit the changes to get proper write count $crate::benchmarking::commit_db(); @@ -917,18 +914,16 @@ macro_rules! impl_benchmark_test { let execute_benchmark = | c: $crate::Vec<($crate::BenchmarkParameter, u32)> | -> Result<(), $crate::BenchmarkError> { - // Set up the benchmark, return execution + verification function. - let closure_to_verify = < - SelectedBenchmark as $crate::BenchmarkingSetup<$runtime, _> - >::instance(&selected_benchmark, &c, true)?; - // Set the block number to at least 1 so events are deposited. - if $crate::Zero::is_zero(&frame_system::Pallet::<$runtime>::block_number()) { - frame_system::Pallet::<$runtime>::set_block_number(1u32.into()); - } + let on_before_start = || { + // Set the block number to at least 1 so events are deposited. + if $crate::Zero::is_zero(&frame_system::Pallet::<$runtime>::block_number()) { + frame_system::Pallet::<$runtime>::set_block_number(1u32.into()); + } + }; // Run execution + verification - closure_to_verify()?; + >::test_instance(&selected_benchmark, &c, &on_before_start)?; // Reset the state $crate::benchmarking::wipe_db(); @@ -1386,7 +1381,6 @@ macro_rules! cb_add_benchmarks { /// ``` /// /// This should match what exists with the `add_benchmark!` macro. - #[macro_export] macro_rules! list_benchmark { ( $list:ident, $extra:ident, $name:path, $( $location:tt )* ) => ( diff --git a/benchmarking/src/tests.rs b/benchmarking/src/tests.rs index 4220badd7..2beb4df38 100644 --- a/benchmarking/src/tests.rs +++ b/benchmarking/src/tests.rs @@ -127,79 +127,67 @@ fn benchmarks_macro_works() { let components = >::components(&selected_benchmark); assert_eq!(components, vec![(BenchmarkParameter::b, 1, 1000)]); - let closure = >::instance( - &selected_benchmark, - &[(BenchmarkParameter::b, 1)], - true, - ) - .expect("failed to create closure"); - new_test_ext().execute_with(|| { - assert_eq!(closure(), Ok(())); + assert_ok!(>::unit_test_instance( + &selected_benchmark, + &[(BenchmarkParameter::b, 1)], + )); }); } #[test] fn benchmarks_macro_rename_works() { // Check benchmark creation for `other_dummy`. - let selected_benchmark = SelectedBenchmark::other_name; - let components = >::components(&selected_benchmark); + let selected = SelectedBenchmark::other_name; + let components = >::components(&selected); assert_eq!(components, vec![(BenchmarkParameter::b, 1, 1000)]); - let closure = >::instance( - &selected_benchmark, - &[(BenchmarkParameter::b, 1)], - true, - ) - .expect("failed to create closure"); - new_test_ext().execute_with(|| { - assert_ok!(closure()); + assert_ok!(>::unit_test_instance( + &selected, + &[(BenchmarkParameter::b, 1)], + )); }); } #[test] fn benchmarks_macro_works_for_non_dispatchable() { - let selected_benchmark = SelectedBenchmark::sort_vector; + let selected = SelectedBenchmark::sort_vector; - let components = >::components(&selected_benchmark); + let components = >::components(&selected); assert_eq!(components, vec![(BenchmarkParameter::x, 1, 10000)]); - let closure = >::instance( - &selected_benchmark, - &[(BenchmarkParameter::x, 1)], - true, - ) - .expect("failed to create closure"); - - assert_eq!(closure(), Ok(())); + new_test_ext().execute_with(|| { + assert_ok!(>::unit_test_instance( + &selected, + &[(BenchmarkParameter::x, 1)], + )); + }); } #[test] fn benchmarks_macro_verify_works() { // Check postcondition for benchmark `set_value` is valid. - let selected_benchmark = SelectedBenchmark::set_value; - - let closure = >::instance( - &selected_benchmark, - &[(BenchmarkParameter::b, 1)], - true, - ) - .expect("failed to create closure"); + let selected = SelectedBenchmark::set_value; new_test_ext().execute_with(|| { - assert_ok!(closure()); + assert_ok!(>::unit_test_instance( + &selected, + &[(BenchmarkParameter::b, 1)], + )); }); // Check postcondition for benchmark `bad_verify` is invalid. let selected = SelectedBenchmark::bad_verify; - let closure = - >::instance(&selected, &[(BenchmarkParameter::x, 10000)], true) - .expect("failed to create closure"); - new_test_ext().execute_with(|| { - assert_err!(closure(), "You forgot to sort!"); + assert_err!( + >::unit_test_instance( + &selected, + &[(BenchmarkParameter::x, 10000)], + ), + "You forgot to sort!" + ); }); } diff --git a/tokens/src/impls.rs b/tokens/src/impls.rs index 04ace3eda..83a65ad60 100644 --- a/tokens/src/impls.rs +++ b/tokens/src/impls.rs @@ -120,13 +120,14 @@ where asset: Self::AssetId, dest: &AccountId, amount: Self::Balance, + preservation: Preservation, precision: Precision, fortitude: Fortitude, ) -> Result { if TestKey::contains(&asset) { - A::burn_from(dest, amount, precision, fortitude) + A::burn_from(dest, amount, preservation, precision, fortitude) } else { - B::burn_from(asset, dest, amount, precision, fortitude) + B::burn_from(asset, dest, amount, preservation, precision, fortitude) } } @@ -295,6 +296,7 @@ where fn burn_from( dest: &AccountId, amount: Self::Balance, + preservation: Preservation, precision: Precision, fortitude: Fortitude, ) -> Result { @@ -302,6 +304,7 @@ where GetCurrencyId::get(), dest, C::convert_balance_back(amount, GetCurrencyId::get())?, + preservation, precision, fortitude, ) diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index a00435e3a..cc7dfb83e 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -1858,6 +1858,8 @@ impl fungibles::Mutate for Pallet { asset_id: Self::AssetId, who: &T::AccountId, amount: Self::Balance, + // TODO: Respect preservation + _preservation: Preservation, // TODO: Respect precision _precision: Precision, // TODO: Respect fortitude @@ -2492,10 +2494,18 @@ where fn burn_from( who: &T::AccountId, amount: Self::Balance, + preservation: Preservation, precision: Precision, fortitude: Fortitude, ) -> Result { - as fungibles::Mutate<_>>::burn_from(GetCurrencyId::get(), who, amount, precision, fortitude) + as fungibles::Mutate<_>>::burn_from( + GetCurrencyId::get(), + who, + amount, + preservation, + precision, + fortitude, + ) } fn transfer( diff --git a/tokens/src/tests_events.rs b/tokens/src/tests_events.rs index 1aff9861e..df79ed80f 100644 --- a/tokens/src/tests_events.rs +++ b/tokens/src/tests_events.rs @@ -174,6 +174,7 @@ fn pallet_fungibles_mutate_deposit_events() { DOT, &ALICE, 500, + Preservation::Expendable, Precision::Exact, Fortitude::Polite )); diff --git a/tokens/src/tests_fungibles.rs b/tokens/src/tests_fungibles.rs index 2d975c727..13c56c33a 100644 --- a/tokens/src/tests_fungibles.rs +++ b/tokens/src/tests_fungibles.rs @@ -51,7 +51,14 @@ fn fungibles_mutate_trait_should_work() { ExtBuilder::default().build().execute_with(|| { assert_ok!(>::mint_into(DOT, &ALICE, 10)); assert_eq!( - >::burn_from(DOT, &ALICE, 8, Precision::Exact, Fortitude::Polite), + >::burn_from( + DOT, + &ALICE, + 8, + Preservation::Expendable, + Precision::Exact, + Fortitude::Polite + ), Ok(8) ); }); @@ -718,6 +725,7 @@ fn fungibles_mutate_convert_should_work() { DOT, &BOB, 10000, + Preservation::Expendable, Precision::Exact, Fortitude::Polite )); diff --git a/xtokens/src/mock/para.rs b/xtokens/src/mock/para.rs index 159c8bf8c..b879e539c 100644 --- a/xtokens/src/mock/para.rs +++ b/xtokens/src/mock/para.rs @@ -147,6 +147,7 @@ impl Config for XcmConfig { type HrmpNewChannelOpenRequestHandler = (); type HrmpChannelAcceptedHandler = (); type HrmpChannelClosingHandler = (); + type XcmRecorder = (); } impl cumulus_pallet_xcm::Config for Runtime { diff --git a/xtokens/src/mock/para_relative_view.rs b/xtokens/src/mock/para_relative_view.rs index 8b3d79f0e..f912eaf58 100644 --- a/xtokens/src/mock/para_relative_view.rs +++ b/xtokens/src/mock/para_relative_view.rs @@ -145,6 +145,7 @@ impl Config for XcmConfig { type HrmpNewChannelOpenRequestHandler = (); type HrmpChannelAcceptedHandler = (); type HrmpChannelClosingHandler = (); + type XcmRecorder = (); } impl cumulus_pallet_xcm::Config for Runtime { diff --git a/xtokens/src/mock/para_teleport.rs b/xtokens/src/mock/para_teleport.rs index ea882fdaa..c0a8664f4 100644 --- a/xtokens/src/mock/para_teleport.rs +++ b/xtokens/src/mock/para_teleport.rs @@ -143,6 +143,7 @@ impl Config for XcmConfig { type HrmpNewChannelOpenRequestHandler = (); type HrmpChannelAcceptedHandler = (); type HrmpChannelClosingHandler = (); + type XcmRecorder = (); } impl cumulus_pallet_xcm::Config for Runtime { diff --git a/xtokens/src/mock/relay.rs b/xtokens/src/mock/relay.rs index a9d1d2912..47deaae4d 100644 --- a/xtokens/src/mock/relay.rs +++ b/xtokens/src/mock/relay.rs @@ -123,6 +123,7 @@ impl Config for XcmConfig { type HrmpNewChannelOpenRequestHandler = (); type HrmpChannelAcceptedHandler = (); type HrmpChannelClosingHandler = (); + type XcmRecorder = (); } pub type LocalOriginToLocation = SignedToAccountId32;