From db0381f6363e0c8e781082b6f552c092b688fb1c Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Tue, 17 Jan 2023 13:37:06 +1300 Subject: [PATCH] Update polkadot v0.9.36 (#878) * Fix payments on-idle (#868) * fix payments on-idle * update comment * Switch to Rust-1.66 (#869) * feat: derive MEL for Value enum (#867) * feat: impl MEL for Value enum * fmt: fix * feat:Make the XcmTransfer trait support transfer_multiasset_with_fee (#870) * update bencher deps (#872) * add BenchmarkError::Weightless (#873) * add BenchmarkError::Weightless * Update benchmarking/src/lib.rs Co-authored-by: zjb0807 Co-authored-by: zjb0807 * Update jsonrpsee 0.16.2 (#876) * Update jsonrpsee 0.16.2 * fix clippy * fix clippy Co-authored-by: William Freudenberger Co-authored-by: NingBo Wang <2536935847@qq.com> Co-authored-by: Ermal Kaleci Co-authored-by: Andrew Plaza --- .github/workflows/coverage.yml | 2 +- .github/workflows/master.yml.disabled | 2 +- .github/workflows/test.yml | 2 +- Cargo.dev.toml | 5 +++ Makefile | 4 +- asset-registry/src/lib.rs | 2 +- auction/src/lib.rs | 18 ++++----- bencher/Cargo.toml | 10 ++--- bencher/src/bench_runner.rs | 8 +--- bencher/src/bencher.rs | 10 ++--- bencher/src/build_wasm/prerequisites.rs | 12 ++---- bencher/src/build_wasm/wasm_project.rs | 2 +- bencher/src/handler.rs | 11 +++--- bencher/src/lib.rs | 2 +- bencher/src/macros.rs | 2 +- bencher/src/utils.rs | 12 ++++++ bencher/test/Cargo.toml | 7 +--- bencher/test/src/benches.rs | 8 +--- bencher/test/src/lib.rs | 10 ++--- bencher/test/src/mock.rs | 2 +- bencher/test/src/weights.rs | 29 ++++++++++---- benchmarking/src/lib.rs | 19 +++++++++ oracle/rpc/Cargo.toml | 2 +- oracle/src/lib.rs | 2 +- payments/src/lib.rs | 15 +++---- payments/src/tests.rs | 52 ++++++++++++++++++++++++- tokens/rpc/Cargo.toml | 2 +- traits/src/lib.rs | 9 ++--- traits/src/xcm_transfer.rs | 9 +++++ vesting/src/lib.rs | 4 +- weight-gen/src/main.rs | 6 +-- weight-meter/Cargo.toml | 2 + xtokens/src/lib.rs | 15 +++++-- 33 files changed, 201 insertions(+), 96 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 5b6b34f7a..96a48dacf 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -34,7 +34,7 @@ jobs: - name: Install toolchain uses: dtolnay/rust-toolchain@nightly with: - toolchain: nightly-2022-08-05 + toolchain: nightly-2022-10-30 components: rustfmt target: wasm32-unknown-unknown - name: Generate code coverage diff --git a/.github/workflows/master.yml.disabled b/.github/workflows/master.yml.disabled index 1436a32f5..a1bdac623 100644 --- a/.github/workflows/master.yml.disabled +++ b/.github/workflows/master.yml.disabled @@ -15,7 +15,7 @@ jobs: - name: Install toolchain uses: dtolnay/rust-toolchain@nightly with: - toolchain: nightly-2022-08-05 + toolchain: nightly-2022-10-30 - name: Install cargo-unleash run: cargo install cargo-unleash --git https://github.com/xlc/cargo-unleash.git # https://github.com/paritytech/cargo-unleash/pull/38 - name: Prepare diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f6870a045..6b8f74e5f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,7 +34,7 @@ jobs: - name: Install toolchain uses: dtolnay/rust-toolchain@nightly with: - toolchain: nightly-2022-08-05 + toolchain: nightly-2022-10-30 components: rustfmt target: wasm32-unknown-unknown - name: Install Wasm toolchain diff --git a/Cargo.dev.toml b/Cargo.dev.toml index e8d806294..e2d51020f 100644 --- a/Cargo.dev.toml +++ b/Cargo.dev.toml @@ -4,6 +4,7 @@ members = [ "auction", "authority", "bencher", + "bencher/test", "benchmarking", "currencies", "gradually-update", @@ -11,6 +12,8 @@ members = [ "oracle/rpc", "oracle/rpc/runtime-api", "tokens", + "tokens/rpc", + "tokens/rpc/runtime-api", "traits", "utilities", "vesting", @@ -26,6 +29,8 @@ members = [ "payments" ] +exclude = ["bencher/test"] + resolver = "2" [profile.dev] diff --git a/Makefile b/Makefile index 51dfe8fe8..c57206900 100644 --- a/Makefile +++ b/Makefile @@ -44,9 +44,11 @@ dev-check: Cargo.toml check dev-check-tests: Cargo.toml cargo check --tests --all + cargo check --tests --features=bench --package=orml-weight-meter --package=orml-bencher-test dev-test: Cargo.toml - cargo test --all --features runtime-benchmarks + cargo test --all --features=runtime-benchmarks + cargo test --features=bench --package=orml-weight-meter --package=orml-bencher-test # run benchmarks via Acala node benchmark-all: diff --git a/asset-registry/src/lib.rs b/asset-registry/src/lib.rs index 2bea733e5..b9819566f 100644 --- a/asset-registry/src/lib.rs +++ b/asset-registry/src/lib.rs @@ -313,7 +313,7 @@ impl Pallet { fn do_insert_location(asset_id: T::AssetId, location: VersionedMultiLocation) -> DispatchResult { // if the metadata contains a location, set the LocationToAssetId let location: MultiLocation = location.try_into().map_err(|()| Error::::BadVersion)?; - LocationToAssetId::::try_mutate(&location, |maybe_asset_id| { + LocationToAssetId::::try_mutate(location, |maybe_asset_id| { ensure!(maybe_asset_id.is_none(), Error::::ConflictingLocation); *maybe_asset_id = Some(asset_id); Ok(()) diff --git a/auction/src/lib.rs b/auction/src/lib.rs index 2c9c3ec4b..5c4e00010 100644 --- a/auction/src/lib.rs +++ b/auction/src/lib.rs @@ -109,12 +109,12 @@ pub mod module { #[pallet::hooks] impl Hooks for Pallet { fn on_initialize(now: T::BlockNumber) -> Weight { - T::WeightInfo::on_finalize(AuctionEndTime::::iter_prefix(&now).count() as u32) + T::WeightInfo::on_finalize(AuctionEndTime::::iter_prefix(now).count() as u32) } fn on_finalize(now: T::BlockNumber) { - for (auction_id, _) in AuctionEndTime::::drain_prefix(&now) { - if let Some(auction) = Auctions::::take(&auction_id) { + for (auction_id, _) in AuctionEndTime::::drain_prefix(now) { + if let Some(auction) = Auctions::::take(auction_id) { T::Handler::on_auction_ended(auction_id, auction.bid); } } @@ -151,10 +151,10 @@ pub mod module { match bid_result.auction_end_change { Change::NewValue(new_end) => { if let Some(old_end_block) = auction.end { - AuctionEndTime::::remove(&old_end_block, id); + AuctionEndTime::::remove(old_end_block, id); } if let Some(new_end_block) = new_end { - AuctionEndTime::::insert(&new_end_block, id, ()); + AuctionEndTime::::insert(new_end_block, id, ()); } auction.end = new_end; } @@ -189,10 +189,10 @@ impl Auction for Pallet { ) -> DispatchResult { let auction = Auctions::::get(id).ok_or(Error::::AuctionNotExist)?; if let Some(old_end) = auction.end { - AuctionEndTime::::remove(&old_end, id); + AuctionEndTime::::remove(old_end, id); } if let Some(new_end) = info.end { - AuctionEndTime::::insert(&new_end, id, ()); + AuctionEndTime::::insert(new_end, id, ()); } Auctions::::insert(id, info); Ok(()) @@ -211,14 +211,14 @@ impl Auction for Pallet { })?; Auctions::::insert(auction_id, auction); if let Some(end_block) = end { - AuctionEndTime::::insert(&end_block, auction_id, ()); + AuctionEndTime::::insert(end_block, auction_id, ()); } Ok(auction_id) } fn remove_auction(id: Self::AuctionId) { - if let Some(auction) = Auctions::::take(&id) { + if let Some(auction) = Auctions::::take(id) { if let Some(end_block) = auction.end { AuctionEndTime::::remove(end_block, id); } diff --git a/bencher/Cargo.toml b/bencher/Cargo.toml index c18a02313..e61bf3fc1 100644 --- a/bencher/Cargo.toml +++ b/bencher/Cargo.toml @@ -10,15 +10,15 @@ edition = "2021" [dependencies] paste = "1.0.7" build-helper = { version = "0.1.1", optional = true } -cargo_metadata = { version = "0.14.1", optional = true } +cargo_metadata = { version = "0.15.2", optional = true } tempfile = { version = "3.2.0", optional = true } toml = { version = "0.5.8", optional = true } walkdir = { version = "2.3.1", optional = true } ansi_term = { version = "0.12.1", optional = true } wasm-gc-api = { version = "0.1.11", optional = true } rand = {version = "0.8.3", optional = true } -linregress = { version = "0.4.4", optional = true } -parking_lot = { version = "0.12.0", optional = true } +linregress = { version = "0.5.0", optional = true } +parking_lot = { version = "0.12.1", optional = true } thiserror = { version = "1.0", optional = true } serde = { version = "1.0.136", optional = true, features = ['derive'] } serde_json = {version = "1.0.68", optional = true } @@ -35,7 +35,7 @@ sc-executor = { git = "https://github.com/paritytech/substrate", default-feature sc-executor-common = { git = "https://github.com/paritytech/substrate", optional = true , branch = "polkadot-v0.9.36" } sc-client-db = { git = "https://github.com/paritytech/substrate", default-features = false, features = ["rocksdb"], optional = true , branch = "polkadot-v0.9.36" } sp-maybe-compressed-blob = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true , branch = "polkadot-v0.9.36" } -frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.36" } +frame-support = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.36" } sp-externalities = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.36" } sp-storage = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true , branch = "polkadot-v0.9.36" } @@ -67,7 +67,7 @@ std = [ "sc-executor-common", "sc-client-db", "sp-maybe-compressed-blob", - "frame-benchmarking/std", + "frame-support/std", "sp-externalities/std", "sp-storage/std", ] diff --git a/bencher/src/bench_runner.rs b/bencher/src/bench_runner.rs index b48246515..1906c1494 100644 --- a/bencher/src/bench_runner.rs +++ b/bencher/src/bench_runner.rs @@ -2,18 +2,14 @@ use super::{ bench_ext::BenchExt, tracker::{BenchTracker, BenchTrackerExt}, }; -use frame_benchmarking::frame_support::sp_runtime::traits::Block; +use frame_support::sp_runtime::traits::Block; use sc_executor::{WasmExecutionMethod, WasmExecutor, WasmtimeInstantiationStrategy}; use sc_executor_common::runtime_blob::RuntimeBlob; use sp_externalities::Extensions; use sp_state_machine::{Ext, OverlayedChanges, StorageTransactionCache}; use sp_std::sync::Arc; -type ComposeHostFunctions = ( - sp_io::SubstrateHostFunctions, - frame_benchmarking::benchmarking::HostFunctions, - super::bench::HostFunctions, -); +type ComposeHostFunctions = (sp_io::SubstrateHostFunctions, super::bench::HostFunctions); /// Run benches pub fn run(wasm_code: Vec) -> std::result::Result, sc_executor_common::error::Error> { diff --git a/bencher/src/bencher.rs b/bencher/src/bencher.rs index bfbc210a4..3ac197f2b 100644 --- a/bencher/src/bencher.rs +++ b/bencher/src/bencher.rs @@ -40,8 +40,8 @@ impl Bencher { pub fn before_run(&self) { #[cfg(not(feature = "std"))] { - frame_benchmarking::benchmarking::commit_db(); - frame_benchmarking::benchmarking::wipe_db(); + crate::bench::commit_db(); + crate::bench::wipe_db(); } } @@ -56,8 +56,8 @@ impl Bencher { { #[cfg(not(feature = "std"))] { - frame_benchmarking::benchmarking::commit_db(); - frame_benchmarking::benchmarking::reset_read_write_count(); + crate::bench::commit_db(); + crate::bench::reset_read_write_count(); crate::bench::start_timer(); } @@ -68,7 +68,7 @@ impl Bencher { let elapsed = crate::bench::end_timer().saturating_sub(crate::bench::redundant_time()); self.current.elapses.push(elapsed); - frame_benchmarking::benchmarking::commit_db(); + crate::bench::commit_db(); // changed keys self.current.keys = crate::bench::read_written_keys(); diff --git a/bencher/src/build_wasm/prerequisites.rs b/bencher/src/build_wasm/prerequisites.rs index 3074480df..8377af327 100644 --- a/bencher/src/build_wasm/prerequisites.rs +++ b/bencher/src/build_wasm/prerequisites.rs @@ -77,11 +77,7 @@ fn get_rustup_nightly(selected: Option) -> Option { let version = match selected { Some(selected) => selected, None => { - let output = Command::new("rustup") - .args(&["toolchain", "list"]) - .output() - .ok()? - .stdout; + let output = Command::new("rustup").args(["toolchain", "list"]).output().ok()?.stdout; let lines = output.as_slice().lines(); let mut latest_nightly = None; @@ -194,7 +190,7 @@ fn create_check_toolchain_project(project_dir: &Path) { let manifest_path = project_dir.join("Cargo.toml"); write_file_if_changed( - &manifest_path, + manifest_path, r#" [package] name = "wasm-test" @@ -260,7 +256,7 @@ fn check_wasm_toolchain_installed(cargo_command: CargoCommand) -> Result Result, storage_infos: Vec) { comments.sort(); + let intercepted_value = model.parameters()[0] as u64; + println!( "{} {:<40} {:>20} storage: {:<20}", green_bold("Bench"), cyan(&name), - green_bold(&format!( - "{:?}", - Duration::from_nanos(model.parameters.intercept_value as u64) - )), + green_bold(&format!("{:?}", Duration::from_nanos(intercepted_value))), green_bold(&format!( "[r: {}, w: {}]", &total_reads.to_string(), @@ -83,7 +82,7 @@ pub fn handle(output: Vec, storage_infos: Vec) { BenchData { name, - weight: model.parameters.intercept_value as u64 * 1_000, + weight: intercepted_value * 1_000, reads: total_reads, writes: total_writes, comments, diff --git a/bencher/src/lib.rs b/bencher/src/lib.rs index e7c18d486..824248fa6 100644 --- a/bencher/src/lib.rs +++ b/bencher/src/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] #[doc(hidden)] -pub extern crate frame_benchmarking; +pub extern crate frame_support; #[doc(hidden)] pub extern crate paste; #[doc(hidden)] diff --git a/bencher/src/macros.rs b/bencher/src/macros.rs index f307e7617..4945b0046 100644 --- a/bencher/src/macros.rs +++ b/bencher/src/macros.rs @@ -125,7 +125,7 @@ macro_rules! run_benches { ) => { #[cfg(all(feature = "std", feature = "bench"))] pub fn main() -> std::io::Result<()> { - use $crate::frame_benchmarking::frame_support::traits::StorageInfoTrait; + use $crate::frame_support::traits::StorageInfoTrait; let wasm = $crate::build_wasm::build()?; let storage_info = $all_pallets_with_system::storage_info(); match $crate::bench_runner::run::<$block>(wasm) { diff --git a/bencher/src/utils.rs b/bencher/src/utils.rs index 4b0ee5379..f46f58e17 100644 --- a/bencher/src/utils.rs +++ b/bencher/src/utils.rs @@ -36,6 +36,18 @@ pub trait Bench { println!("{}", msg); } + fn commit_db(&mut self) { + self.commit() + } + + fn wipe_db(&mut self) { + self.wipe() + } + + fn reset_read_write_count(&mut self) { + self.reset_read_write_count() + } + fn start_timer(&mut self) { let tracker = &***self .extension::() diff --git a/bencher/test/Cargo.toml b/bencher/test/Cargo.toml index af39d8675..f4705f3b6 100644 --- a/bencher/test/Cargo.toml +++ b/bencher/test/Cargo.toml @@ -32,18 +32,15 @@ std = [ "serde", "scale-info/std", "codec/std", - "orml-bencher/std", "frame-support/std", "frame-system/std", "sp-runtime/std", - "sp-std/std", "sp-core/std", + "sp-std/std", + "orml-bencher/std", "orml-weight-meter/std", ] bench = [ "orml-bencher/bench", "orml-weight-meter/bench", - "frame-support/runtime-benchmarks", - "sp-runtime/runtime-benchmarks", - "frame-system/runtime-benchmarks", ] diff --git a/bencher/test/src/benches.rs b/bencher/test/src/benches.rs index dbd98cc7e..62ac88231 100644 --- a/bencher/test/src/benches.rs +++ b/bencher/test/src/benches.rs @@ -42,10 +42,4 @@ fn whitelist(b: &mut Bencher) { }); } -benches!( - whitelist, - set_value, - set_foo, - remove_all_bar, - remove_all_bar_with_limit -); +benches!(whitelist, set_value, set_foo, remove_all_bar, remove_all_bar_with_limit); diff --git a/bencher/test/src/lib.rs b/bencher/test/src/lib.rs index 7881e2ea7..892e9231f 100644 --- a/bencher/test/src/lib.rs +++ b/bencher/test/src/lib.rs @@ -40,9 +40,9 @@ pub mod pallet { impl Pallet { #[pallet::call_index(0)] #[pallet::weight(0)] - #[orml_weight_meter::start(ModuleWeights::::set_value())] + #[orml_weight_meter::start(ModuleWeights::::set_value().ref_time())] pub fn set_value(origin: OriginFor, n: u32) -> DispatchResultWithPostInfo { - let _sender = frame_system::ensure_signed(origin)?; + frame_system::ensure_signed(origin)?; Value::::get(); Value::::put(n); Value::::put(n + 1); @@ -53,14 +53,14 @@ pub mod pallet { #[pallet::call_index(1)] #[pallet::weight(0)] pub fn dummy(origin: OriginFor, _n: u32) -> DispatchResult { - let _sender = frame_system::ensure_none(origin)?; + frame_system::ensure_none(origin)?; Foo::::put(1); Ok(()) } } impl Pallet { - #[orml_weight_meter::weight(ModuleWeights::::set_foo())] + #[orml_weight_meter::weight(ModuleWeights::::set_foo().ref_time())] pub(crate) fn set_foo() -> frame_support::dispatch::DispatchResult { Value::::put(2); @@ -87,4 +87,4 @@ pub mod pallet { _ = Bar::::clear(10, None); } } -} \ No newline at end of file +} diff --git a/bencher/test/src/mock.rs b/bencher/test/src/mock.rs index 54d84fcd2..af2ae2532 100644 --- a/bencher/test/src/mock.rs +++ b/bencher/test/src/mock.rs @@ -16,7 +16,7 @@ pub type Header = sp_runtime::generic::Header; pub type SignedExtra = (frame_system::CheckWeight,); -pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic; +pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic; pub type Block = sp_runtime::generic::Block; diff --git a/bencher/test/src/weights.rs b/bencher/test/src/weights.rs index bc946b082..8882be9cf 100644 --- a/bencher/test/src/weights.rs +++ b/bencher/test/src/weights.rs @@ -10,13 +10,21 @@ use sp_std::marker::PhantomData; pub struct ModuleWeights(PhantomData); impl ModuleWeights { + // Storage access info + // + // Test::Bar (r: 0, w: 1) + pub fn whitelist() -> Weight { + Weight::from_ref_time(5_356_000) + .saturating_add(T::DbWeight::get().writes(1)) + } // Storage access info // // Test::Value (r: 1, w: 1) + // Unknown 0x3a7472616e73616374696f6e5f6c6576656c3a (r: 1, w: 1) pub fn set_value() -> Weight { - (5_236_000 as Weight) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().writes(1 as Weight)) + Weight::from_ref_time(3_919_000) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(2)) } // Storage access info // @@ -24,13 +32,20 @@ impl ModuleWeights { // Test::Foo (r: 0, w: 1) // Test::Value (r: 0, w: 1) pub fn set_foo() -> Weight { - (13_274_000 as Weight) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().writes(4 as Weight)) + Weight::from_ref_time(5_133_000) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(4)) } // Storage access info // pub fn remove_all_bar() -> Weight { - (3_449_000 as Weight) + Weight::from_ref_time(1_533_000) + } + // Storage access info + // + // Test::Bar (r: 0, w: 10) + pub fn remove_all_bar_with_limit() -> Weight { + Weight::from_ref_time(1_600_000) + .saturating_add(T::DbWeight::get().writes(10)) } } diff --git a/benchmarking/src/lib.rs b/benchmarking/src/lib.rs index 4a4d8ec98..f165cb682 100644 --- a/benchmarking/src/lib.rs +++ b/benchmarking/src/lib.rs @@ -1180,6 +1180,14 @@ macro_rules! impl_benchmark_test_suite { $crate::str::from_utf8(benchmark_name) .expect("benchmark name is always a valid string!"), ); + }, + $crate::BenchmarkError::Weightless => { + // This is considered a success condition. + $crate::log::error!( + "WARNING: benchmark error weightless skipped - {}", + $crate::str::from_utf8(benchmark_name) + .expect("benchmark name is always a valid string!"), + ); } } }, @@ -1314,6 +1322,17 @@ macro_rules! add_benchmark { .expect("benchmark name is always a valid string!") ); None + }, + Err($crate::BenchmarkError::Weightless) => { + $crate::log::error!( + "WARNING: benchmark weightless skipped - {}", + $crate::str::from_utf8(benchmark) + .expect("benchmark name is always a valid string!") + ); + Some(vec![$crate::BenchmarkResult { + components: selected_components.clone(), + .. Default::default() + }]) } }; diff --git a/oracle/rpc/Cargo.toml b/oracle/rpc/Cargo.toml index 09100a4d8..32d66dabd 100644 --- a/oracle/rpc/Cargo.toml +++ b/oracle/rpc/Cargo.toml @@ -8,7 +8,7 @@ description = "RPC module for orml-oracle." [dependencies] codec = { package = "parity-scale-codec", version = "3.0.0" } -jsonrpsee = { version = "0.15.1", features = ["server", "macros"] } +jsonrpsee = { version = "0.16.2", features = ["client", "server", "macros"] } tracing = { version = "0.1.29" } tracing-core = { version = "0.1.28" } diff --git a/oracle/src/lib.rs b/oracle/src/lib.rs index 1d2513197..1ee385602 100644 --- a/oracle/src/lib.rs +++ b/oracle/src/lib.rs @@ -215,7 +215,7 @@ impl, I: 'static> Pallet { value: value.clone(), timestamp: now, }; - RawValues::::insert(&who, &key, timestamped); + RawValues::::insert(&who, key, timestamped); // Update `Values` storage if `combined` yielded result. if let Some(combined) = Self::combined(key) { diff --git a/payments/src/lib.rs b/payments/src/lib.rs index 8a66cbee6..9f14d1927 100644 --- a/payments/src/lib.rs +++ b/payments/src/lib.rs @@ -214,16 +214,17 @@ pub mod pallet { /// Hook that execute when there is leftover space in a block /// This function will look for any pending scheduled tasks that can /// be executed and will process them. - fn on_idle(now: T::BlockNumber, mut remaining_weight: Weight) -> Weight { + fn on_idle(now: T::BlockNumber, remaining_weight: Weight) -> Weight { const MAX_TASKS_TO_PROCESS: usize = 5; - // reduce the weight used to read the task list - remaining_weight = remaining_weight.saturating_sub(T::WeightInfo::remove_task()); + // used to read the task list + let mut used_weight = T::WeightInfo::remove_task(); let cancel_weight = T::WeightInfo::cancel(); // calculate count of tasks that can be processed with remaining weight let possible_task_count: usize = remaining_weight - .ref_time() + .saturating_sub(used_weight) .saturating_div(cancel_weight.ref_time()) + .ref_time() .try_into() .unwrap_or(MAX_TASKS_TO_PROCESS); @@ -239,9 +240,9 @@ pub mod pallet { // order by oldest task to process task_list.sort_by(|(_, t), (_, x)| x.when.cmp(&t.when)); - while !task_list.is_empty() && remaining_weight.all_gte(cancel_weight) { + while !task_list.is_empty() && used_weight.all_lte(remaining_weight) { if let Some((account_pair, _)) = task_list.pop() { - remaining_weight = remaining_weight.saturating_sub(cancel_weight); + used_weight = used_weight.saturating_add(cancel_weight); // remove the task form the tasks storage tasks.remove(&account_pair); @@ -268,7 +269,7 @@ pub mod pallet { } } }); - remaining_weight + used_weight } } diff --git a/payments/src/tests.rs b/payments/src/tests.rs index ddcc3fa79..ddd2bf7ab 100644 --- a/payments/src/tests.rs +++ b/payments/src/tests.rs @@ -1,9 +1,10 @@ use crate::{ mock::*, types::{PaymentDetail, PaymentState}, + weights::WeightInfo, Payment as PaymentStore, PaymentHandler, ScheduledTask, ScheduledTasks, Task, }; -use frame_support::{assert_noop, assert_ok, storage::with_transaction}; +use frame_support::{assert_noop, assert_ok, storage::with_transaction, traits::OnIdle, weights::Weight}; use orml_traits::MultiCurrency; use sp_runtime::{Percent, TransactionOutcome}; @@ -1398,3 +1399,52 @@ fn test_automatic_refund_works_for_multiple_payments() { assert_eq!(Tokens::free_balance(CURRENCY_ID, &PAYMENT_RECIPENT_TWO), 0); }); } + +#[test] +fn on_idle_works() { + new_test_ext().execute_with(|| { + assert_eq!( + Payment::on_idle(System::block_number(), Weight::MAX), + <()>::remove_task() + ); + + let payment_amount = 20; + let expected_cancel_block = CANCEL_BLOCK_BUFFER + 1; + + assert_ok!(Payment::pay( + RuntimeOrigin::signed(PAYMENT_CREATOR), + PAYMENT_RECIPENT, + CURRENCY_ID, + payment_amount, + None + )); + + // creator requests a refund + assert_ok!(Payment::request_refund( + RuntimeOrigin::signed(PAYMENT_CREATOR), + PAYMENT_RECIPENT + )); + // ensure the request is added to the refund queue + let scheduled_tasks_list = ScheduledTasks::::get(); + assert_eq!(scheduled_tasks_list.len(), 1); + assert_eq!( + scheduled_tasks_list.get(&(PAYMENT_CREATOR, PAYMENT_RECIPENT)).unwrap(), + &ScheduledTask { + task: Task::Cancel, + when: expected_cancel_block + } + ); + + assert_eq!(run_n_blocks(CANCEL_BLOCK_BUFFER - 1), 600); + assert_eq!( + Payment::on_idle(System::block_number(), Weight::MAX), + <()>::remove_task() + ); + + assert_eq!(run_n_blocks(1), 601); + assert_eq!( + Payment::on_idle(System::block_number(), Weight::MAX), + <()>::remove_task() + <()>::cancel() + ); + }); +} diff --git a/tokens/rpc/Cargo.toml b/tokens/rpc/Cargo.toml index b396f57bd..b873b2ebc 100644 --- a/tokens/rpc/Cargo.toml +++ b/tokens/rpc/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] codec = { package = "parity-scale-codec", version = "3.0.0" } -jsonrpsee = { version = "0.15.1", features = ["server", "macros"] } +jsonrpsee = { version = "0.16.2", features = ["client", "server", "macros"] } tracing = { version = "0.1.29" } tracing-core = { version = "0.1.28" } diff --git a/traits/src/lib.rs b/traits/src/lib.rs index a5894d218..ea5231dd9 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -1,6 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std)] -use codec::{Decode, Encode}; +use codec::{Decode, Encode, MaxEncodedLen}; use impl_trait_for_tuples::impl_for_tuples; use sp_runtime::{DispatchResult, RuntimeDebug}; use sp_std::{ @@ -8,9 +8,6 @@ use sp_std::{ prelude::Vec, }; -#[cfg(feature = "std")] -use serde::{Deserialize, Serialize}; - pub use asset_registry::{FixedConversionRateProvider, WeightToFeeConverter}; pub use auction::{Auction, AuctionHandler, AuctionInfo, OnNewBidResult}; pub use currency::{ @@ -25,6 +22,8 @@ pub use nft::InspectExtended; pub use price::{DefaultPriceProvider, PriceProvider}; pub use rewards::RewardHandler; use scale_info::TypeInfo; +#[cfg(feature = "std")] +use serde::{Deserialize, Serialize}; pub use xcm_transfer::XcmTransfer; pub mod arithmetic; @@ -58,7 +57,7 @@ pub trait CombineData { } /// Indicate if should change a value -#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)] +#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] pub enum Change { /// No change. NoChange, diff --git a/traits/src/xcm_transfer.rs b/traits/src/xcm_transfer.rs index cd671f5e6..c4dacb116 100644 --- a/traits/src/xcm_transfer.rs +++ b/traits/src/xcm_transfer.rs @@ -19,4 +19,13 @@ pub trait XcmTransfer { dest: MultiLocation, dest_weight_limit: WeightLimit, ) -> DispatchResult; + + /// Transfer `MultiAssetWithFee` + fn transfer_multiasset_with_fee( + who: AccountId, + asset: MultiAsset, + fee: MultiAsset, + dest: MultiLocation, + dest_weight_limit: WeightLimit, + ) -> DispatchResult; } diff --git a/vesting/src/lib.rs b/vesting/src/lib.rs index 8ca778c34..66edffc10 100644 --- a/vesting/src/lib.rs +++ b/vesting/src/lib.rs @@ -245,7 +245,7 @@ pub mod module { #[pallet::call] impl Pallet { #[pallet::call_index(0)] - #[pallet::weight(T::WeightInfo::claim((::MaxVestingSchedules::get() / 2) as u32))] + #[pallet::weight(T::WeightInfo::claim(::MaxVestingSchedules::get() / 2))] pub fn claim(origin: OriginFor) -> DispatchResult { let who = ensure_signed(origin)?; let locked_amount = Self::do_claim(&who); @@ -301,7 +301,7 @@ pub mod module { } #[pallet::call_index(3)] - #[pallet::weight(T::WeightInfo::claim((::MaxVestingSchedules::get() / 2) as u32))] + #[pallet::weight(T::WeightInfo::claim(::MaxVestingSchedules::get() / 2))] pub fn claim_for(origin: OriginFor, dest: ::Source) -> DispatchResult { let _ = ensure_signed(origin)?; let who = T::Lookup::lookup(dest)?; diff --git a/weight-gen/src/main.rs b/weight-gen/src/main.rs index 7fdf90211..b8f521ca5 100644 --- a/weight-gen/src/main.rs +++ b/weight-gen/src/main.rs @@ -162,7 +162,7 @@ fn main() { // Use empty header if a header path is not given. let header = { if let Some(path) = matches.get_one::("header") { - ::std::fs::read_to_string(&path).expect("Header file not found") + ::std::fs::read_to_string(path).expect("Header file not found") } else { String::from("") } @@ -175,7 +175,7 @@ fn main() { // Use default template if template path is not given. let template = { if let Some(path) = matches.get_one::("template") { - ::std::fs::read_to_string(&path).expect("Template file not found") + ::std::fs::read_to_string(path).expect("Template file not found") } else { String::from(DEFAULT_TEMPLATE) } @@ -183,7 +183,7 @@ fn main() { // Write benchmark to file or print to terminal if output path is not given. if let Some(path) = matches.get_one::("output") { - let mut output_file = ::std::fs::File::create(&path).expect("Could not create output file"); + let mut output_file = ::std::fs::File::create(path).expect("Could not create output file"); handlebars .render_template_to_write(&template, &hbs_data, &mut output_file) diff --git a/weight-meter/Cargo.toml b/weight-meter/Cargo.toml index a6b76278c..7f9d38ca7 100644 --- a/weight-meter/Cargo.toml +++ b/weight-meter/Cargo.toml @@ -24,6 +24,8 @@ frame-support = { git = "https://github.com/paritytech/substrate", branch = "pol frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } +orml-bencher = { path = "../bencher" } + [features] default = ["std"] std = [ diff --git a/xtokens/src/lib.rs b/xtokens/src/lib.rs index e75585d31..0cf6ab4bf 100644 --- a/xtokens/src/lib.rs +++ b/xtokens/src/lib.rs @@ -924,10 +924,8 @@ pub mod module { fn get_reserve_location(assets: &MultiAssets, fee_item: &u32) -> Option { let reserve_idx = if assets.len() == 1 { 0 - } else if *fee_item == 0 { - 1 } else { - 0 + (*fee_item == 0) as usize }; let asset = assets.get(reserve_idx); asset.and_then(T::ReserveProvider::reserve) @@ -955,6 +953,17 @@ pub mod module { ) -> DispatchResult { Self::do_transfer_multiasset(who, asset, dest, dest_weight_limit) } + + #[require_transactional] + fn transfer_multiasset_with_fee( + who: T::AccountId, + asset: MultiAsset, + fee: MultiAsset, + dest: MultiLocation, + dest_weight_limit: WeightLimit, + ) -> DispatchResult { + Self::do_transfer_multiasset_with_fee(who, asset, fee, dest, dest_weight_limit) + } } }