From c730e25b61c6e5aa9ee3e59952fca84fd6caf84c Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Mon, 10 Jan 2022 18:34:38 +0300 Subject: [PATCH 1/3] Move benchmarks from Rialto to Millau (#1277) * move brnchmarks from Rialto to Millau * fix limits test --- .maintain/rialto-weight-template.hbs | 103 -------- Cargo.lock | 2 + bin/millau/runtime/Cargo.toml | 7 + bin/millau/runtime/src/lib.rs | 212 +++++++++++++++- bin/millau/runtime/src/rialto_messages.rs | 4 +- bin/rialto/runtime/src/lib.rs | 240 +----------------- modules/grandpa/src/weights.rs | 26 +- modules/messages/src/weights.rs | 182 ++++++------- modules/messages/src/weights_ext.rs | 2 +- modules/token-swap/src/benchmarking.rs | 2 +- modules/token-swap/src/weights.rs | 14 +- primitives/chain-millau/src/lib.rs | 2 +- primitives/chain-rialto/src/lib.rs | 2 +- .../src/chains/kusama_headers_to_polkadot.rs | 2 +- .../lib-substrate-relay/src/messages_lane.rs | 4 +- scripts/update-weights.sh | 8 +- 16 files changed, 335 insertions(+), 477 deletions(-) delete mode 100644 .maintain/rialto-weight-template.hbs diff --git a/.maintain/rialto-weight-template.hbs b/.maintain/rialto-weight-template.hbs deleted file mode 100644 index 297b58e0d3..0000000000 --- a/.maintain/rialto-weight-template.hbs +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright 2019-2021 Parity Technologies (UK) Ltd. -// This file is part of Parity Bridges Common. - -// Parity Bridges Common is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Parity Bridges Common is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Parity Bridges Common. If not, see . - -//! Autogenerated weights for `{{pallet}}` -//! -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}} -//! DATE: {{date}}, STEPS: {{cmd.steps}}, REPEAT: {{cmd.repeat}} -//! LOW RANGE: {{cmd.lowest_range_values}}, HIGH RANGE: {{cmd.highest_range_values}} -//! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}} -//! CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}} - -// Executed Command: -{{#each args as |arg|~}} -// {{arg}} -{{/each}} - -#![allow(clippy::all)] -#![allow(unused_parens)] -#![allow(unused_imports)] - -use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; -use sp_std::marker::PhantomData; - -/// Weight functions needed for `{{pallet}}`. -pub trait WeightInfo { - {{~#each benchmarks as |benchmark|}} - fn {{benchmark.name~}} - ( - {{~#each benchmark.components as |c| ~}} - {{c.name}}: u32, {{/each~}} - ) -> Weight; - {{~/each}} -} - -/// Weights for `{{pallet}}` using the Rialto node and recommended hardware. -pub struct RialtoWeight(PhantomData); -impl WeightInfo for RialtoWeight { - {{~#each benchmarks as |benchmark|}} - fn {{benchmark.name~}} - ( - {{~#each benchmark.components as |c| ~}} - {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} - ) -> Weight { - ({{underscore benchmark.base_weight}} as Weight) - {{~#each benchmark.component_weight as |cw|}} - .saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)) - {{~/each}} - {{~#if (ne benchmark.base_reads "0")}} - .saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight)) - {{~/if}} - {{~#each benchmark.component_reads as |cr|}} - .saturating_add(T::DbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight))) - {{~/each}} - {{~#if (ne benchmark.base_writes "0")}} - .saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as Weight)) - {{~/if}} - {{~#each benchmark.component_writes as |cw|}} - .saturating_add(T::DbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))) - {{~/each}} - } - {{~/each}} -} - -// For backwards compatibility and tests -impl WeightInfo for () { - {{~#each benchmarks as |benchmark|}} - fn {{benchmark.name~}} - ( - {{~#each benchmark.components as |c| ~}} - {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} - ) -> Weight { - ({{underscore benchmark.base_weight}} as Weight) - {{~#each benchmark.component_weight as |cw|}} - .saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)) - {{~/each}} - {{~#if (ne benchmark.base_reads "0")}} - .saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight)) - {{~/if}} - {{~#each benchmark.component_reads as |cr|}} - .saturating_add(RocksDbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight))) - {{~/each}} - {{~#if (ne benchmark.base_writes "0")}} - .saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}} as Weight)) - {{~/if}} - {{~#each benchmark.component_writes as |cw|}} - .saturating_add(RocksDbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))) - {{~/each}} - } - {{~/each}} -} diff --git a/Cargo.lock b/Cargo.lock index 4c03f27a4e..4b988640ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4635,6 +4635,7 @@ version = "0.1.0" dependencies = [ "beefy-primitives", "bp-header-chain", + "bp-message-dispatch", "bp-messages", "bp-millau", "bp-rialto", @@ -4647,6 +4648,7 @@ dependencies = [ "frame-system", "frame-system-rpc-runtime-api", "hex-literal", + "libsecp256k1", "pallet-aura", "pallet-balances", "pallet-beefy", diff --git a/bin/millau/runtime/Cargo.toml b/bin/millau/runtime/Cargo.toml index 13195b9519..4fe2f72a96 100644 --- a/bin/millau/runtime/Cargo.toml +++ b/bin/millau/runtime/Cargo.toml @@ -10,12 +10,14 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] hex-literal = "0.3" codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false, features = ["derive"] } +libsecp256k1 = { version = "0.7", optional = true, default-features = false, features = ["hmac"] } scale-info = { version = "1.0", default-features = false, features = ["derive"] } serde = { version = "1.0", optional = true, features = ["derive"] } # Bridge dependencies bp-header-chain = { path = "../../../primitives/header-chain", default-features = false } +bp-message-dispatch = { path = "../../../primitives/message-dispatch", default-features = false } bp-messages = { path = "../../../primitives/messages", default-features = false } bp-millau = { path = "../../../primitives/chain-millau", default-features = false } bp-rialto = { path = "../../../primitives/chain-rialto", default-features = false } @@ -71,6 +73,7 @@ default = ["std"] std = [ "beefy-primitives/std", "bp-header-chain/std", + "bp-message-dispatch/std", "bp-messages/std", "bp-millau/std", "bp-rialto/std", @@ -116,8 +119,12 @@ std = [ "sp-version/std", ] runtime-benchmarks = [ + "bridge-runtime-common/runtime-benchmarks", "frame-benchmarking", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", + "libsecp256k1", + "pallet-bridge-messages/runtime-benchmarks", "pallet-bridge-token-swap/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", ] diff --git a/bin/millau/runtime/src/lib.rs b/bin/millau/runtime/src/lib.rs index b4a80659ab..4d6ecd8ba3 100644 --- a/bin/millau/runtime/src/lib.rs +++ b/bin/millau/runtime/src/lib.rs @@ -374,12 +374,26 @@ parameter_types! { // Note that once this is hit the pallet will essentially throttle incoming requests down to one // call per block. pub const MaxRequests: u32 = 50; +} - // Number of headers to keep. - // - // Assuming the worst case of every header being finalized, we will keep headers for at least a - // week. - pub const HeadersToKeep: u32 = 7 * bp_millau::DAYS as u32; +#[cfg(feature = "runtime-benchmarks")] +parameter_types! { + /// Number of headers to keep in benchmarks. + /// + /// In benchmarks we always populate with full number of `HeadersToKeep` to make sure that + /// pruning is taken into account. + /// + /// Note: This is lower than regular value, to speed up benchmarking setup. + pub const HeadersToKeep: u32 = 1024; +} + +#[cfg(not(feature = "runtime-benchmarks"))] +parameter_types! { + /// Number of headers to keep. + /// + /// Assuming the worst case of every header being finalized, we will keep headers at least for a + /// week. + pub const HeadersToKeep: u32 = 7 * bp_rialto::DAYS as u32; } pub type RialtoGrandpaInstance = (); @@ -388,8 +402,7 @@ impl pallet_bridge_grandpa::Config for Runtime { type MaxRequests = MaxRequests; type HeadersToKeep = HeadersToKeep; - // TODO [#391]: Use weights generated for the Millau runtime instead of Rialto ones. - type WeightInfo = pallet_bridge_grandpa::weights::RialtoWeight; + type WeightInfo = pallet_bridge_grandpa::weights::MillauWeight; } pub type WestendGrandpaInstance = pallet_bridge_grandpa::Instance1; @@ -398,8 +411,7 @@ impl pallet_bridge_grandpa::Config for Runtime { type MaxRequests = MaxRequests; type HeadersToKeep = HeadersToKeep; - // TODO [#391]: Use weights generated for the Millau runtime instead of Rialto ones. - type WeightInfo = pallet_bridge_grandpa::weights::RialtoWeight; + type WeightInfo = pallet_bridge_grandpa::weights::MillauWeight; } impl pallet_shift_session_manager::Config for Runtime {} @@ -422,8 +434,7 @@ pub type WithRialtoMessagesInstance = (); impl pallet_bridge_messages::Config for Runtime { type Event = Event; - // TODO: https://github.com/paritytech/parity-bridges-common/issues/390 - type WeightInfo = pallet_bridge_messages::weights::RialtoWeight; + type WeightInfo = pallet_bridge_messages::weights::MillauWeight; type Parameter = rialto_messages::MillauToRialtoMessagesParameter; type MaxMessagesToPruneAtOnce = MaxMessagesToPruneAtOnce; type MaxUnrewardedRelayerEntriesAtInboundLane = MaxUnrewardedRelayerEntriesAtInboundLane; @@ -783,9 +794,13 @@ impl_runtime_apis! { use frame_benchmarking::{list_benchmark, Benchmarking, BenchmarkList}; use frame_support::traits::StorageInfoTrait; + use pallet_bridge_messages::benchmarking::Pallet as MessagesBench; + let mut list = Vec::::new(); list_benchmark!(list, extra, pallet_bridge_token_swap, BridgeRialtoTokenSwap); + list_benchmark!(list, extra, pallet_bridge_messages, MessagesBench::); + list_benchmark!(list, extra, pallet_bridge_grandpa, BridgeRialtoGrandpa); let storage_info = AllPalletsWithSystem::storage_info(); @@ -813,6 +828,171 @@ impl_runtime_apis! { let mut batches = Vec::::new(); let params = (&config, &whitelist); + use bp_runtime::messages::DispatchFeePayment; + use bridge_runtime_common::messages; + use pallet_bridge_messages::benchmarking::{ + Pallet as MessagesBench, + Config as MessagesConfig, + MessageDeliveryProofParams, + MessageParams, + MessageProofParams, + ProofSize as MessagesProofSize, + }; + use rialto_messages::{ToRialtoMessagePayload, WithRialtoMessageBridge}; + + impl MessagesConfig for Runtime { + fn maximal_message_size() -> u32 { + messages::source::maximal_message_size::() + } + + fn bridged_relayer_id() -> Self::InboundRelayer { + Default::default() + } + + fn account_balance(account: &Self::AccountId) -> Self::OutboundMessageFee { + pallet_balances::Pallet::::free_balance(account) + } + + fn endow_account(account: &Self::AccountId) { + pallet_balances::Pallet::::make_free_balance_be( + account, + Balance::MAX / 100, + ); + } + + fn prepare_outbound_message( + params: MessageParams, + ) -> (rialto_messages::ToRialtoMessagePayload, Balance) { + let message_payload = vec![0; params.size as usize]; + let dispatch_origin = bp_message_dispatch::CallOrigin::SourceAccount( + params.sender_account, + ); + + let message = ToRialtoMessagePayload { + spec_version: 0, + weight: params.size as _, + origin: dispatch_origin, + call: message_payload, + dispatch_fee_payment: DispatchFeePayment::AtSourceChain, + }; + (message, pallet_bridge_messages::benchmarking::MESSAGE_FEE.into()) + } + + fn prepare_message_proof( + params: MessageProofParams, + ) -> (rialto_messages::FromRialtoMessagesProof, Weight) { + use bp_messages::{MessageKey, storage_keys}; + use bridge_runtime_common::{ + messages::MessageBridge, + messages_benchmarking::{ed25519_sign, prepare_message_proof}, + }; + use codec::Encode; + use frame_support::weights::GetDispatchInfo; + use rialto_messages::WithRialtoMessageBridge; + use sp_runtime::traits::{Header, IdentifyAccount}; + + let remark = match params.size { + MessagesProofSize::Minimal(ref size) => vec![0u8; *size as _], + _ => vec![], + }; + let call = Call::System(SystemCall::remark { remark }); + let call_weight = call.get_dispatch_info().weight; + + let rialto_account_id: bp_rialto::AccountId = Default::default(); + let (millau_raw_public, millau_raw_signature) = ed25519_sign( + &call, + &rialto_account_id, + VERSION.spec_version, + bp_runtime::RIALTO_CHAIN_ID, + bp_runtime::MILLAU_CHAIN_ID, + ); + let millau_public = MultiSigner::Ed25519(sp_core::ed25519::Public::from_raw(millau_raw_public)); + let millau_signature = MultiSignature::Ed25519(sp_core::ed25519::Signature::from_raw( + millau_raw_signature, + )); + + if params.dispatch_fee_payment == DispatchFeePayment::AtTargetChain { + Self::endow_account(&millau_public.clone().into_account()); + } + + let make_rialto_message_key = |message_key: MessageKey| storage_keys::message_key( + ::BRIDGED_MESSAGES_PALLET_NAME, + &message_key.lane_id, message_key.nonce, + ).0; + let make_rialto_outbound_lane_data_key = |lane_id| storage_keys::outbound_lane_data_key( + ::BRIDGED_MESSAGES_PALLET_NAME, + &lane_id, + ).0; + + let make_rialto_header = |state_root| bp_rialto::Header::new( + 0, + Default::default(), + state_root, + Default::default(), + Default::default(), + ); + + let dispatch_fee_payment = params.dispatch_fee_payment.clone(); + prepare_message_proof::( + params, + make_rialto_message_key, + make_rialto_outbound_lane_data_key, + make_rialto_header, + call_weight, + bp_message_dispatch::MessagePayload { + spec_version: VERSION.spec_version, + weight: call_weight, + origin: bp_message_dispatch::CallOrigin::< + bp_rialto::AccountId, + MultiSigner, + Signature, + >::TargetAccount( + rialto_account_id, + millau_public, + millau_signature, + ), + dispatch_fee_payment, + call: call.encode(), + }.encode(), + ) + } + + fn prepare_message_delivery_proof( + params: MessageDeliveryProofParams, + ) -> rialto_messages::ToRialtoMessagesDeliveryProof { + use bridge_runtime_common::messages_benchmarking::prepare_message_delivery_proof; + use rialto_messages::WithRialtoMessageBridge; + use sp_runtime::traits::Header; + + prepare_message_delivery_proof::( + params, + |lane_id| bp_messages::storage_keys::inbound_lane_data_key( + ::BRIDGED_MESSAGES_PALLET_NAME, + &lane_id, + ).0, + |state_root| bp_rialto::Header::new( + 0, + Default::default(), + state_root, + Default::default(), + Default::default(), + ), + ) + } + + fn is_message_dispatched(nonce: bp_messages::MessageNonce) -> bool { + frame_system::Pallet::::events() + .into_iter() + .map(|event_record| event_record.event) + .any(|event| matches!( + event, + Event::BridgeDispatch(pallet_bridge_dispatch::Event::::MessageDispatched( + _, ([0, 0, 0, 0], nonce_from_event), _, + )) if nonce_from_event == nonce + )) + } + } + use pallet_bridge_token_swap::benchmarking::Config as TokenSwapConfig; impl TokenSwapConfig for Runtime { @@ -828,6 +1008,13 @@ impl_runtime_apis! { } } + add_benchmark!( + params, + batches, + pallet_bridge_messages, + MessagesBench:: + ); + add_benchmark!(params, batches, pallet_bridge_grandpa, BridgeRialtoGrandpa); add_benchmark!(params, batches, pallet_bridge_token_swap, BridgeRialtoTokenSwap); if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) } @@ -868,8 +1055,7 @@ mod tests { #[test] fn ensure_millau_message_lane_weights_are_correct() { - // TODO: https://github.com/paritytech/parity-bridges-common/issues/390 - type Weights = pallet_bridge_messages::weights::RialtoWeight; + type Weights = pallet_bridge_messages::weights::MillauWeight; pallet_bridge_messages::ensure_weights_are_correct::( bp_millau::DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT, diff --git a/bin/millau/runtime/src/rialto_messages.rs b/bin/millau/runtime/src/rialto_messages.rs index d46ab4ca04..4403c423c2 100644 --- a/bin/millau/runtime/src/rialto_messages.rs +++ b/bin/millau/runtime/src/rialto_messages.rs @@ -64,10 +64,10 @@ pub type FromRialtoMessagePayload = pub type FromRialtoEncodedCall = messages::target::FromBridgedChainEncodedMessageCall; /// Messages proof for Rialto -> Millau messages. -type FromRialtoMessagesProof = messages::target::FromBridgedChainMessagesProof; +pub type FromRialtoMessagesProof = messages::target::FromBridgedChainMessagesProof; /// Messages delivery proof for Millau -> Rialto messages. -type ToRialtoMessagesDeliveryProof = +pub type ToRialtoMessagesDeliveryProof = messages::source::FromBridgedChainMessagesDeliveryProof; /// Call-dispatch based message dispatch for Rialto -> Millau messages. diff --git a/bin/rialto/runtime/src/lib.rs b/bin/rialto/runtime/src/lib.rs index ab5318a4f7..a68de93069 100644 --- a/bin/rialto/runtime/src/lib.rs +++ b/bin/rialto/runtime/src/lib.rs @@ -399,21 +399,7 @@ parameter_types! { /// Note that once this is hit the pallet will essentially throttle incoming requests down to one /// call per block. pub const MaxRequests: u32 = 50; -} - -#[cfg(feature = "runtime-benchmarks")] -parameter_types! { - /// Number of headers to keep in benchmarks. - /// - /// In benchmarks we always populate with full number of `HeadersToKeep` to make sure that - /// pruning is taken into account. - /// - /// Note: This is lower than regular value, to speed up benchmarking setup. - pub const HeadersToKeep: u32 = 1024; -} -#[cfg(not(feature = "runtime-benchmarks"))] -parameter_types! { /// Number of headers to keep. /// /// Assuming the worst case of every header being finalized, we will keep headers at least for a @@ -426,7 +412,7 @@ impl pallet_bridge_grandpa::Config for Runtime { type BridgedChain = bp_millau::Millau; type MaxRequests = MaxRequests; type HeadersToKeep = HeadersToKeep; - type WeightInfo = pallet_bridge_grandpa::weights::RialtoWeight; + type WeightInfo = pallet_bridge_grandpa::weights::MillauWeight; } impl pallet_shift_session_manager::Config for Runtime {} @@ -449,7 +435,7 @@ pub type WithMillauMessagesInstance = (); impl pallet_bridge_messages::Config for Runtime { type Event = Event; - type WeightInfo = pallet_bridge_messages::weights::RialtoWeight; + type WeightInfo = pallet_bridge_messages::weights::MillauWeight; type Parameter = millau_messages::RialtoToMillauMessagesParameter; type MaxMessagesToPruneAtOnce = MaxMessagesToPruneAtOnce; type MaxUnrewardedRelayerEntriesAtInboundLane = MaxUnrewardedRelayerEntriesAtInboundLane; @@ -921,226 +907,6 @@ impl_runtime_apis! { BridgeMillauMessages::inbound_unrewarded_relayers_state(lane) } } - - #[cfg(feature = "runtime-benchmarks")] - impl frame_benchmarking::Benchmark for Runtime { - fn benchmark_metadata(extra: bool) -> ( - Vec, - Vec, - ) { - use frame_benchmarking::{list_benchmark, Benchmarking, BenchmarkList}; - use frame_support::traits::StorageInfoTrait; - - use pallet_bridge_messages::benchmarking::Pallet as MessagesBench; - - let mut list = Vec::::new(); - - list_benchmark!(list, extra, pallet_bridge_messages, MessagesBench::); - list_benchmark!(list, extra, pallet_bridge_grandpa, BridgeMillauGrandpa); - - let storage_info = AllPalletsWithSystem::storage_info(); - - return (list, storage_info) - } - - fn dispatch_benchmark( - config: frame_benchmarking::BenchmarkConfig, - ) -> Result, sp_runtime::RuntimeString> { - use frame_benchmarking::{Benchmarking, BenchmarkBatch, TrackedStorageKey, add_benchmark}; - - let whitelist: Vec = vec![ - // Block Number - hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(), - // Execution Phase - hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec().into(), - // Event Count - hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec().into(), - // System Events - hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7").to_vec().into(), - // Caller 0 Account - hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da946c154ffd9992e395af90b5b13cc6f295c77033fce8a9045824a6690bbf99c6db269502f0a8d1d2a008542d5690a0749").to_vec().into(), - ]; - - let mut batches = Vec::::new(); - let params = (&config, &whitelist); - - use crate::millau_messages::{ToMillauMessagePayload, WithMillauMessageBridge}; - use bp_runtime::messages::DispatchFeePayment; - use bridge_runtime_common::messages; - use pallet_bridge_messages::benchmarking::{ - Pallet as MessagesBench, - Config as MessagesConfig, - MessageDeliveryProofParams, - MessageParams, - MessageProofParams, - ProofSize as MessagesProofSize, - }; - - impl MessagesConfig for Runtime { - fn maximal_message_size() -> u32 { - messages::source::maximal_message_size::() - } - - fn bridged_relayer_id() -> Self::InboundRelayer { - Default::default() - } - - fn account_balance(account: &Self::AccountId) -> Self::OutboundMessageFee { - pallet_balances::Pallet::::free_balance(account) - } - - fn endow_account(account: &Self::AccountId) { - pallet_balances::Pallet::::make_free_balance_be( - account, - Balance::MAX / 100, - ); - } - - fn prepare_outbound_message( - params: MessageParams, - ) -> (millau_messages::ToMillauMessagePayload, Balance) { - let message_payload = vec![0; params.size as usize]; - let dispatch_origin = bp_message_dispatch::CallOrigin::SourceAccount( - params.sender_account, - ); - - let message = ToMillauMessagePayload { - spec_version: 0, - weight: params.size as _, - origin: dispatch_origin, - call: message_payload, - dispatch_fee_payment: DispatchFeePayment::AtSourceChain, - }; - (message, pallet_bridge_messages::benchmarking::MESSAGE_FEE.into()) - } - - fn prepare_message_proof( - params: MessageProofParams, - ) -> (millau_messages::FromMillauMessagesProof, Weight) { - use crate::millau_messages::WithMillauMessageBridge; - use bp_messages::{MessageKey, storage_keys}; - use bridge_runtime_common::{ - messages::MessageBridge, - messages_benchmarking::{ed25519_sign, prepare_message_proof}, - }; - use codec::Encode; - use frame_support::weights::GetDispatchInfo; - use sp_runtime::traits::{Header, IdentifyAccount}; - - let remark = match params.size { - MessagesProofSize::Minimal(ref size) => vec![0u8; *size as _], - _ => vec![], - }; - let call = Call::System(SystemCall::remark { remark }); - let call_weight = call.get_dispatch_info().weight; - - let millau_account_id: bp_millau::AccountId = Default::default(); - let (rialto_raw_public, rialto_raw_signature) = ed25519_sign( - &call, - &millau_account_id, - VERSION.spec_version, - bp_runtime::MILLAU_CHAIN_ID, - bp_runtime::RIALTO_CHAIN_ID, - ); - let rialto_public = MultiSigner::Ed25519(sp_core::ed25519::Public::from_raw(rialto_raw_public)); - let rialto_signature = MultiSignature::Ed25519(sp_core::ed25519::Signature::from_raw( - rialto_raw_signature, - )); - - if params.dispatch_fee_payment == DispatchFeePayment::AtTargetChain { - Self::endow_account(&rialto_public.clone().into_account()); - } - - let make_millau_message_key = |message_key: MessageKey| storage_keys::message_key( - ::BRIDGED_MESSAGES_PALLET_NAME, - &message_key.lane_id, message_key.nonce, - ).0; - let make_millau_outbound_lane_data_key = |lane_id| storage_keys::outbound_lane_data_key( - ::BRIDGED_MESSAGES_PALLET_NAME, - &lane_id, - ).0; - - let make_millau_header = |state_root| bp_millau::Header::new( - 0, - Default::default(), - state_root, - Default::default(), - Default::default(), - ); - - let dispatch_fee_payment = params.dispatch_fee_payment.clone(); - prepare_message_proof::( - params, - make_millau_message_key, - make_millau_outbound_lane_data_key, - make_millau_header, - call_weight, - bp_message_dispatch::MessagePayload { - spec_version: VERSION.spec_version, - weight: call_weight, - origin: bp_message_dispatch::CallOrigin::< - bp_millau::AccountId, - MultiSigner, - Signature, - >::TargetAccount( - millau_account_id, - rialto_public, - rialto_signature, - ), - dispatch_fee_payment, - call: call.encode(), - }.encode(), - ) - } - - fn prepare_message_delivery_proof( - params: MessageDeliveryProofParams, - ) -> millau_messages::ToMillauMessagesDeliveryProof { - use crate::millau_messages::WithMillauMessageBridge; - use bridge_runtime_common::{messages_benchmarking::prepare_message_delivery_proof}; - use sp_runtime::traits::Header; - - prepare_message_delivery_proof::( - params, - |lane_id| bp_messages::storage_keys::inbound_lane_data_key( - ::BRIDGED_MESSAGES_PALLET_NAME, - &lane_id, - ).0, - |state_root| bp_millau::Header::new( - 0, - Default::default(), - state_root, - Default::default(), - Default::default(), - ), - ) - } - - fn is_message_dispatched(nonce: bp_messages::MessageNonce) -> bool { - frame_system::Pallet::::events() - .into_iter() - .map(|event_record| event_record.event) - .any(|event| matches!( - event, - Event::BridgeDispatch(pallet_bridge_dispatch::Event::::MessageDispatched( - _, ([0, 0, 0, 0], nonce_from_event), _, - )) if nonce_from_event == nonce - )) - } - } - - add_benchmark!( - params, - batches, - pallet_bridge_messages, - MessagesBench:: - ); - add_benchmark!(params, batches, pallet_bridge_grandpa, BridgeMillauGrandpa); - - if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) } - Ok(batches) - } - } } /// Millau account ownership digest from Rialto. @@ -1175,7 +941,7 @@ mod tests { #[test] fn ensure_rialto_message_lane_weights_are_correct() { - type Weights = pallet_bridge_messages::weights::RialtoWeight; + type Weights = pallet_bridge_messages::weights::MillauWeight; pallet_bridge_messages::ensure_weights_are_correct::( bp_rialto::DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT, diff --git a/modules/grandpa/src/weights.rs b/modules/grandpa/src/weights.rs index c0cce2c525..2c4660160a 100644 --- a/modules/grandpa/src/weights.rs +++ b/modules/grandpa/src/weights.rs @@ -16,14 +16,14 @@ //! Autogenerated weights for `pallet_bridge_grandpa` //! -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-06-03, STEPS: [50, ], REPEAT: 20 +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2021-12-28, STEPS: 50, REPEAT: 20 //! LOW RANGE: [], HIGH RANGE: [] //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled //! CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: -// target/release/rialto-bridge-node +// target/release/millau-bridge-node // benchmark // --chain=dev // --steps=50 @@ -34,7 +34,7 @@ // --wasm-execution=Compiled // --heap-pages=4096 // --output=./modules/grandpa/src/weights.rs -// --template=./.maintain/rialto-weight-template.hbs +// --template=./.maintain/millau-weight-template.hbs #![allow(clippy::all)] #![allow(unused_parens)] @@ -51,13 +51,13 @@ pub trait WeightInfo { fn submit_finality_proof(p: u32, v: u32) -> Weight; } -/// Weights for `pallet_bridge_grandpa` using the Rialto node and recommended hardware. -pub struct RialtoWeight(PhantomData); -impl WeightInfo for RialtoWeight { +/// Weights for `pallet_bridge_grandpa` using the Millau node and recommended hardware. +pub struct MillauWeight(PhantomData); +impl WeightInfo for MillauWeight { fn submit_finality_proof(p: u32, v: u32) -> Weight { - (0 as Weight) - .saturating_add((59_692_000 as Weight).saturating_mul(p as Weight)) - .saturating_add((6_876_000 as Weight).saturating_mul(v as Weight)) + (115_651_000 as Weight) + .saturating_add((61_465_000 as Weight).saturating_mul(p as Weight)) + .saturating_add((3_438_000 as Weight).saturating_mul(v as Weight)) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(6 as Weight)) } @@ -66,9 +66,9 @@ impl WeightInfo for RialtoWeight { // For backwards compatibility and tests impl WeightInfo for () { fn submit_finality_proof(p: u32, v: u32) -> Weight { - (0 as Weight) - .saturating_add((59_692_000 as Weight).saturating_mul(p as Weight)) - .saturating_add((6_876_000 as Weight).saturating_mul(v as Weight)) + (115_651_000 as Weight) + .saturating_add((61_465_000 as Weight).saturating_mul(p as Weight)) + .saturating_add((3_438_000 as Weight).saturating_mul(v as Weight)) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(6 as Weight)) } diff --git a/modules/messages/src/weights.rs b/modules/messages/src/weights.rs index 9dce11168f..7c5df201f9 100644 --- a/modules/messages/src/weights.rs +++ b/modules/messages/src/weights.rs @@ -16,14 +16,14 @@ //! Autogenerated weights for `pallet_bridge_messages` //! -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-06-18, STEPS: [50, ], REPEAT: 20 +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2021-12-28, STEPS: 50, REPEAT: 20 //! LOW RANGE: [], HIGH RANGE: [] //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled //! CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: -// target/release/rialto-bridge-node +// target/release/millau-bridge-node // benchmark // --chain=dev // --steps=50 @@ -34,7 +34,7 @@ // --wasm-execution=Compiled // --heap-pages=4096 // --output=./modules/messages/src/weights.rs -// --template=./.maintain/rialto-weight-template.hbs +// --template=./.maintain/millau-weight-template.hbs #![allow(clippy::all)] #![allow(unused_parens)] @@ -71,120 +71,120 @@ pub trait WeightInfo { fn receive_delivery_proof_for_multiple_messages_by_multiple_relayers(i: u32) -> Weight; } -/// Weights for `pallet_bridge_messages` using the Rialto node and recommended hardware. -pub struct RialtoWeight(PhantomData); -impl WeightInfo for RialtoWeight { +/// Weights for `pallet_bridge_messages` using the Millau node and recommended hardware. +pub struct MillauWeight(PhantomData); +impl WeightInfo for MillauWeight { fn send_minimal_message_worst_case() -> Weight { - (159_305_000 as Weight) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (117_480_000 as Weight) + .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(12 as Weight)) } fn send_1_kb_message_worst_case() -> Weight { - (164_394_000 as Weight) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (128_391_000 as Weight) + .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(12 as Weight)) } fn send_16_kb_message_worst_case() -> Weight { - (223_521_000 as Weight) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (149_149_000 as Weight) + .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(12 as Weight)) } fn maximal_increase_message_fee() -> Weight { - (6_781_470_000 as Weight) + (6_015_058_000 as Weight) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn increase_message_fee(i: u32) -> Weight { - (114_963_000 as Weight) - .saturating_add((6_000 as Weight).saturating_mul(i as Weight)) + (0 as Weight) + .saturating_add((2_000 as Weight).saturating_mul(i as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_single_message_proof() -> Weight { - (206_769_000 as Weight) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (179_892_000 as Weight) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_two_messages_proof() -> Weight { - (343_982_000 as Weight) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (291_793_000 as Weight) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_single_message_proof_with_outbound_lane_state() -> Weight { - (223_738_000 as Weight) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (192_191_000 as Weight) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_single_message_proof_1_kb() -> Weight { - (235_369_000 as Weight) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (202_104_000 as Weight) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_single_message_proof_16_kb() -> Weight { - (510_338_000 as Weight) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (357_144_000 as Weight) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_single_prepaid_message_proof() -> Weight { - (141_536_000 as Weight) + (122_648_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn receive_delivery_proof_for_single_message() -> Weight { - (128_805_000 as Weight) + (107_631_000 as Weight) .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_delivery_proof_for_two_messages_by_single_relayer() -> Weight { - (137_143_000 as Weight) + (113_885_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_delivery_proof_for_two_messages_by_two_relayers() -> Weight { - (193_108_000 as Weight) + (155_151_000 as Weight) .saturating_add(T::DbWeight::get().reads(8 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn send_messages_of_various_lengths(i: u32) -> Weight { - (133_632_000 as Weight) - .saturating_add((4_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (0 as Weight) + .saturating_add((2_000 as Weight).saturating_mul(i as Weight)) + .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(12 as Weight)) } fn receive_multiple_messages_proof(i: u32) -> Weight { - (0 as Weight) - .saturating_add((145_006_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (56_209_000 as Weight) + .saturating_add((109_502_000 as Weight).saturating_mul(i as Weight)) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_message_proofs_with_extra_nodes(i: u32) -> Weight { - (486_301_000 as Weight) - .saturating_add((10_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (0 as Weight) + .saturating_add((5_000 as Weight).saturating_mul(i as Weight)) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_message_proofs_with_large_leaf(i: u32) -> Weight { - (178_139_000 as Weight) - .saturating_add((7_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (50_281_000 as Weight) + .saturating_add((2_000 as Weight).saturating_mul(i as Weight)) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_multiple_messages_proof_with_outbound_lane_state(i: u32) -> Weight { - (0 as Weight) - .saturating_add((150_844_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) + (70_297_000 as Weight) + .saturating_add((110_892_000 as Weight).saturating_mul(i as Weight)) + .saturating_add(T::DbWeight::get().reads(6 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_delivery_proof_for_multiple_messages_by_single_relayer(i: u32) -> Weight { - (113_140_000 as Weight) - .saturating_add((7_656_000 as Weight).saturating_mul(i as Weight)) + (0 as Weight) + .saturating_add((8_016_000 as Weight).saturating_mul(i as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(i as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_delivery_proof_for_multiple_messages_by_multiple_relayers(i: u32) -> Weight { - (97_424_000 as Weight) - .saturating_add((63_128_000 as Weight).saturating_mul(i as Weight)) + (0 as Weight) + .saturating_add((50_981_000 as Weight).saturating_mul(i as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(i as Weight))) .saturating_add(T::DbWeight::get().writes(3 as Weight)) @@ -195,116 +195,116 @@ impl WeightInfo for RialtoWeight { // For backwards compatibility and tests impl WeightInfo for () { fn send_minimal_message_worst_case() -> Weight { - (159_305_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (117_480_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(12 as Weight)) } fn send_1_kb_message_worst_case() -> Weight { - (164_394_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (128_391_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(12 as Weight)) } fn send_16_kb_message_worst_case() -> Weight { - (223_521_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (149_149_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(12 as Weight)) } fn maximal_increase_message_fee() -> Weight { - (6_781_470_000 as Weight) + (6_015_058_000 as Weight) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn increase_message_fee(i: u32) -> Weight { - (114_963_000 as Weight) - .saturating_add((6_000 as Weight).saturating_mul(i as Weight)) + (0 as Weight) + .saturating_add((2_000 as Weight).saturating_mul(i as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_single_message_proof() -> Weight { - (206_769_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (179_892_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_two_messages_proof() -> Weight { - (343_982_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (291_793_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_single_message_proof_with_outbound_lane_state() -> Weight { - (223_738_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (192_191_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_single_message_proof_1_kb() -> Weight { - (235_369_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (202_104_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_single_message_proof_16_kb() -> Weight { - (510_338_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (357_144_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_single_prepaid_message_proof() -> Weight { - (141_536_000 as Weight) + (122_648_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn receive_delivery_proof_for_single_message() -> Weight { - (128_805_000 as Weight) + (107_631_000 as Weight) .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_delivery_proof_for_two_messages_by_single_relayer() -> Weight { - (137_143_000 as Weight) + (113_885_000 as Weight) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_delivery_proof_for_two_messages_by_two_relayers() -> Weight { - (193_108_000 as Weight) + (155_151_000 as Weight) .saturating_add(RocksDbWeight::get().reads(8 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn send_messages_of_various_lengths(i: u32) -> Weight { - (133_632_000 as Weight) - .saturating_add((4_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (0 as Weight) + .saturating_add((2_000 as Weight).saturating_mul(i as Weight)) + .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(12 as Weight)) } fn receive_multiple_messages_proof(i: u32) -> Weight { - (0 as Weight) - .saturating_add((145_006_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (56_209_000 as Weight) + .saturating_add((109_502_000 as Weight).saturating_mul(i as Weight)) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_message_proofs_with_extra_nodes(i: u32) -> Weight { - (486_301_000 as Weight) - .saturating_add((10_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (0 as Weight) + .saturating_add((5_000 as Weight).saturating_mul(i as Weight)) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_message_proofs_with_large_leaf(i: u32) -> Weight { - (178_139_000 as Weight) - .saturating_add((7_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (50_281_000 as Weight) + .saturating_add((2_000 as Weight).saturating_mul(i as Weight)) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_multiple_messages_proof_with_outbound_lane_state(i: u32) -> Weight { - (0 as Weight) - .saturating_add((150_844_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + (70_297_000 as Weight) + .saturating_add((110_892_000 as Weight).saturating_mul(i as Weight)) + .saturating_add(RocksDbWeight::get().reads(6 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_delivery_proof_for_multiple_messages_by_single_relayer(i: u32) -> Weight { - (113_140_000 as Weight) - .saturating_add((7_656_000 as Weight).saturating_mul(i as Weight)) + (0 as Weight) + .saturating_add((8_016_000 as Weight).saturating_mul(i as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(i as Weight))) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_delivery_proof_for_multiple_messages_by_multiple_relayers(i: u32) -> Weight { - (97_424_000 as Weight) - .saturating_add((63_128_000 as Weight).saturating_mul(i as Weight)) + (0 as Weight) + .saturating_add((50_981_000 as Weight).saturating_mul(i as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().reads((2 as Weight).saturating_mul(i as Weight))) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) diff --git a/modules/messages/src/weights_ext.rs b/modules/messages/src/weights_ext.rs index fef09c6ceb..483a22eda1 100644 --- a/modules/messages/src/weights_ext.rs +++ b/modules/messages/src/weights_ext.rs @@ -390,7 +390,7 @@ impl WeightInfoExt for () { } } -impl WeightInfoExt for crate::weights::RialtoWeight { +impl WeightInfoExt for crate::weights::MillauWeight { fn expected_extra_storage_proof_size() -> u32 { EXTRA_STORAGE_PROOF_SIZE } diff --git a/modules/token-swap/src/benchmarking.rs b/modules/token-swap/src/benchmarking.rs index bbc544a8b9..6ca35f5f39 100644 --- a/modules/token-swap/src/benchmarking.rs +++ b/modules/token-swap/src/benchmarking.rs @@ -30,7 +30,7 @@ use frame_system::RawOrigin; use sp_core::H256; use sp_io::hashing::blake2_256; use sp_runtime::traits::Bounded; -use sp_std::vec::Vec; +use sp_std::{boxed::Box, vec::Vec}; const SEED: u32 = 0; diff --git a/modules/token-swap/src/weights.rs b/modules/token-swap/src/weights.rs index 06cb6b85cf..51c5d99de9 100644 --- a/modules/token-swap/src/weights.rs +++ b/modules/token-swap/src/weights.rs @@ -17,7 +17,7 @@ //! Autogenerated weights for `pallet_bridge_token_swap` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-06, STEPS: 50, REPEAT: 20 +//! DATE: 2021-12-28, STEPS: 50, REPEAT: 20 //! LOW RANGE: [], HIGH RANGE: [] //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled //! CHAIN: Some("dev"), DB CACHE: 128 @@ -57,17 +57,17 @@ pub trait WeightInfo { pub struct MillauWeight(PhantomData); impl WeightInfo for MillauWeight { fn create_swap() -> Weight { - (116_040_000 as Weight) + (90_368_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn claim_swap() -> Weight { - (102_882_000 as Weight) + (88_397_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn cancel_swap() -> Weight { - (99_434_000 as Weight) + (91_253_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } @@ -76,17 +76,17 @@ impl WeightInfo for MillauWeight { // For backwards compatibility and tests impl WeightInfo for () { fn create_swap() -> Weight { - (116_040_000 as Weight) + (90_368_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn claim_swap() -> Weight { - (102_882_000 as Weight) + (88_397_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn cancel_swap() -> Weight { - (99_434_000 as Weight) + (91_253_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } diff --git a/primitives/chain-millau/src/lib.rs b/primitives/chain-millau/src/lib.rs index 2f51320dc8..6a8bbd3e33 100644 --- a/primitives/chain-millau/src/lib.rs +++ b/primitives/chain-millau/src/lib.rs @@ -103,7 +103,7 @@ pub const MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT: Weight = 2_000_000 /// chain. Don't put too much reserve there, because it is used to **decrease** /// `DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT` cost. So putting large reserve would make delivery /// transactions cheaper. -pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 600_000_000; +pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 700_000_000; /// The target length of a session (how often authorities change) on Millau measured in of number of /// blocks. diff --git a/primitives/chain-rialto/src/lib.rs b/primitives/chain-rialto/src/lib.rs index 4fd7cbb463..b995410a65 100644 --- a/primitives/chain-rialto/src/lib.rs +++ b/primitives/chain-rialto/src/lib.rs @@ -94,7 +94,7 @@ pub const MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT: Weight = 2_000_000 /// chain. Don't put too much reserve there, because it is used to **decrease** /// `DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT` cost. So putting large reserve would make delivery /// transactions cheaper. -pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 600_000_000; +pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 700_000_000; /// The target length of a session (how often authorities change) on Rialto measured in of number of /// blocks. diff --git a/relays/bin-substrate/src/chains/kusama_headers_to_polkadot.rs b/relays/bin-substrate/src/chains/kusama_headers_to_polkadot.rs index c36c336718..13d9f12141 100644 --- a/relays/bin-substrate/src/chains/kusama_headers_to_polkadot.rs +++ b/relays/bin-substrate/src/chains/kusama_headers_to_polkadot.rs @@ -83,7 +83,7 @@ pub(crate) mod tests { // differ from the `DbWeight` of Rialto runtime. But now (and most probably forever) it is // the same. type GrandpaPalletWeights = - pallet_bridge_grandpa::weights::RialtoWeight; + pallet_bridge_grandpa::weights::MillauWeight; // The following formula shall not be treated as super-accurate - guard is to protect from // mad relays, not to protect from over-average loses. diff --git a/relays/lib-substrate-relay/src/messages_lane.rs b/relays/lib-substrate-relay/src/messages_lane.rs index 4742ce6e48..87146be489 100644 --- a/relays/lib-substrate-relay/src/messages_lane.rs +++ b/relays/lib-substrate-relay/src/messages_lane.rs @@ -453,7 +453,7 @@ mod tests { use bp_runtime::Chain; type RialtoToMillauMessagesWeights = - pallet_bridge_messages::weights::RialtoWeight; + pallet_bridge_messages::weights::MillauWeight; #[test] fn select_delivery_transaction_limits_works() { @@ -469,7 +469,7 @@ mod tests { // i.e. weight reserved for messages dispatch allows dispatch of non-trivial messages. // // Any significant change in this values should attract additional attention. - (782, 216_583_333_334), + (958, 216_583_333_334), ); } } diff --git a/scripts/update-weights.sh b/scripts/update-weights.sh index 5ee7bb9e8d..b772386e75 100755 --- a/scripts/update-weights.sh +++ b/scripts/update-weights.sh @@ -6,7 +6,7 @@ set -eux -time cargo run --release -p rialto-bridge-node --features=runtime-benchmarks -- benchmark \ +time cargo run --release -p millau-bridge-node --features=runtime-benchmarks -- benchmark \ --chain=dev \ --steps=50 \ --repeat=20 \ @@ -16,9 +16,9 @@ time cargo run --release -p rialto-bridge-node --features=runtime-benchmarks -- --wasm-execution=Compiled \ --heap-pages=4096 \ --output=./modules/messages/src/weights.rs \ - --template=./.maintain/rialto-weight-template.hbs + --template=./.maintain/millau-weight-template.hbs -time cargo run --release -p rialto-bridge-node --features=runtime-benchmarks -- benchmark \ +time cargo run --release -p millau-bridge-node --features=runtime-benchmarks -- benchmark \ --chain=dev \ --steps=50 \ --repeat=20 \ @@ -28,7 +28,7 @@ time cargo run --release -p rialto-bridge-node --features=runtime-benchmarks -- --wasm-execution=Compiled \ --heap-pages=4096 \ --output=./modules/grandpa/src/weights.rs \ - --template=./.maintain/rialto-weight-template.hbs + --template=./.maintain/millau-weight-template.hbs time cargo run --release -p millau-bridge-node --features=runtime-benchmarks -- benchmark \ --chain=dev \ From e98d682de2c37cd99d6ba54cd1afdd322429ba6c Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Tue, 11 Jan 2022 16:00:26 +0300 Subject: [PATCH 2/3] removed extra messages benchmarks (#1279) --- modules/messages/README.md | 5 +- modules/messages/src/benchmarking.rs | 301 +-------------------------- modules/messages/src/weights.rs | 97 --------- 3 files changed, 3 insertions(+), 400 deletions(-) diff --git a/modules/messages/README.md b/modules/messages/README.md index 062a966fad..2dc5629684 100644 --- a/modules/messages/README.md +++ b/modules/messages/README.md @@ -294,9 +294,8 @@ Where: *\* - In all benchmarks all received messages are dispatched and their dispatch cost is near to zero* *\*\* - Trie leafs are assumed to have minimal values. The proof is derived from the minimal proof -by including more trie nodes. That's because according to `receive_message_proofs_with_large_leaf` -and `receive_message_proofs_with_extra_nodes` benchmarks, increasing proof by including more nodes -has slightly larger impact on performance than increasing values stored in leafs*. +by including more trie nodes. That's because according to our additioal benchmarks, increasing proof +by including more nodes has slightly larger impact on performance than increasing values stored in leafs*. #### Weight formula diff --git a/modules/messages/src/benchmarking.rs b/modules/messages/src/benchmarking.rs index 2074c55980..f396365699 100644 --- a/modules/messages/src/benchmarking.rs +++ b/modules/messages/src/benchmarking.rs @@ -30,12 +30,7 @@ use bp_runtime::messages::DispatchFeePayment; use frame_benchmarking::{account, benchmarks_instance_pallet}; use frame_support::{traits::Get, weights::Weight}; use frame_system::RawOrigin; -use sp_std::{ - collections::{btree_map::BTreeMap, vec_deque::VecDeque}, - convert::TryInto, - ops::RangeInclusive, - prelude::*, -}; +use sp_std::{collections::vec_deque::VecDeque, convert::TryInto, ops::RangeInclusive, prelude::*}; /// Fee paid by submitter for single message delivery. pub const MESSAGE_FEE: u64 = 100_000_000_000; @@ -602,300 +597,6 @@ benchmarks_instance_pallet! { ensure_relayer_rewarded::(&relayer1_id, &relayer1_balance); ensure_relayer_rewarded::(&relayer2_id, &relayer2_balance); } - - // - // Benchmarks for manual checks. - // - - // Benchmark `send_message` extrinsic with following conditions: - // * outbound lane already has state, so it needs to be read and decoded; - // * relayers fund account does not exists (in practice it needs to exist in production environment); - // * maximal number of messages is being pruned during the call; - // * message size varies from minimal to maximal for the target chain. - // - // Results of this benchmark may be used to check how message size affects `send_message` performance. - send_messages_of_various_lengths { - let i in 0..T::maximal_message_size().try_into().unwrap_or_default(); - - let lane_id = T::bench_lane_id(); - let sender = account("sender", 0, SEED); - T::endow_account(&sender); - - // 'send' messages that are to be pruned when our message is sent - for _nonce in 1..=T::MaxMessagesToPruneAtOnce::get() { - send_regular_message::(); - } - confirm_message_delivery::(T::MaxMessagesToPruneAtOnce::get()); - - let (payload, fee) = T::prepare_outbound_message(MessageParams { - size: i as _, - sender_account: sender.clone(), - }); - }: send_message(RawOrigin::Signed(sender), lane_id, payload, fee) - verify { - assert_eq!( - crate::OutboundLanes::::get(&T::bench_lane_id()).latest_generated_nonce, - T::MaxMessagesToPruneAtOnce::get() + 1, - ); - } - - // Benchmark `receive_messages_proof` extrinsic with multiple minimal-weight messages and following conditions: - // * proof does not include outbound lane state proof; - // * inbound lane already has state, so it needs to be read and decoded; - // * message is successfully dispatched; - // * message requires all heavy checks done by dispatcher. - // - // This benchmarks gives us an approximation of single message delivery weight. It is similar to the - // `weight(receive_two_messages_proof) - weight(receive_single_message_proof)`. So it may be used - // to verify that the other approximation is correct. - receive_multiple_messages_proof { - let i in 1..64; - - let relayer_id_on_source = T::bridged_relayer_id(); - let relayer_id_on_target = account("relayer", 0, SEED); - let messages_count = i as _; - - // mark messages 1..=20 as delivered - receive_messages::(20); - - let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams { - lane: T::bench_lane_id(), - message_nonces: 21..=(20 + i as MessageNonce), - outbound_lane_data: None, - size: ProofSize::Minimal(EXPECTED_DEFAULT_MESSAGE_LENGTH), - dispatch_fee_payment: DispatchFeePayment::AtTargetChain, - }); - }: receive_messages_proof( - RawOrigin::Signed(relayer_id_on_target), - relayer_id_on_source, - proof, - messages_count, - dispatch_weight - ) - verify { - assert_eq!( - crate::InboundLanes::::get(&T::bench_lane_id()).last_delivered_nonce(), - 20 + i as MessageNonce, - ); - } - - // Benchmark `receive_messages_proof` extrinsic with single minimal-weight message and following conditions: - // * proof does not include outbound lane state proof; - // * inbound lane already has state, so it needs to be read and decoded; - // * message is successfully dispatched; - // * message requires all heavy checks done by dispatcher. - // - // Results of this benchmark may be used to check how proof size affects `receive_message_proof` performance. - receive_message_proofs_with_extra_nodes { - let i in 0..T::maximal_message_size(); - - let relayer_id_on_source = T::bridged_relayer_id(); - let relayer_id_on_target = account("relayer", 0, SEED); - let messages_count = 1u32; - - // mark messages 1..=20 as delivered - receive_messages::(20); - - let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams { - lane: T::bench_lane_id(), - message_nonces: 21..=21, - outbound_lane_data: None, - size: ProofSize::HasExtraNodes(i as _), - dispatch_fee_payment: DispatchFeePayment::AtTargetChain, - }); - }: receive_messages_proof( - RawOrigin::Signed(relayer_id_on_target), - relayer_id_on_source, - proof, - messages_count, - dispatch_weight - ) - verify { - assert_eq!( - crate::InboundLanes::::get(&T::bench_lane_id()).last_delivered_nonce(), - 21, - ); - } - - // Benchmark `receive_messages_proof` extrinsic with single minimal-weight message and following conditions: - // * proof does not include outbound lane state proof; - // * inbound lane already has state, so it needs to be read and decoded; - // * message is successfully dispatched; - // * message requires all heavy checks done by dispatcher. - // - // Results of this benchmark may be used to check how message size affects `receive_message_proof` performance. - receive_message_proofs_with_large_leaf { - let i in 0..T::maximal_message_size(); - - let relayer_id_on_source = T::bridged_relayer_id(); - let relayer_id_on_target = account("relayer", 0, SEED); - let messages_count = 1u32; - - // mark messages 1..=20 as delivered - receive_messages::(20); - - let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams { - lane: T::bench_lane_id(), - message_nonces: 21..=21, - outbound_lane_data: None, - size: ProofSize::HasLargeLeaf(i as _), - dispatch_fee_payment: DispatchFeePayment::AtTargetChain, - }); - }: receive_messages_proof( - RawOrigin::Signed(relayer_id_on_target), - relayer_id_on_source, - proof, - messages_count, - dispatch_weight - ) - verify { - assert_eq!( - crate::InboundLanes::::get(&T::bench_lane_id()).last_delivered_nonce(), - 21, - ); - } - - // Benchmark `receive_messages_proof` extrinsic with multiple minimal-weight messages and following conditions: - // * proof includes outbound lane state proof; - // * inbound lane already has state, so it needs to be read and decoded; - // * message is successfully dispatched; - // * message requires all heavy checks done by dispatcher. - // - // This benchmarks gives us an approximation of outbound lane state delivery weight. It is similar to the - // `weight(receive_single_message_proof_with_outbound_lane_state) - weight(receive_single_message_proof)`. - // So it may be used to verify that the other approximation is correct. - receive_multiple_messages_proof_with_outbound_lane_state { - let i in 1..128; - - let relayer_id_on_source = T::bridged_relayer_id(); - let relayer_id_on_target = account("relayer", 0, SEED); - let messages_count = i as _; - - // mark messages 1..=20 as delivered - receive_messages::(20); - - let (proof, dispatch_weight) = T::prepare_message_proof(MessageProofParams { - lane: T::bench_lane_id(), - message_nonces: 21..=20 + i as MessageNonce, - outbound_lane_data: Some(OutboundLaneData { - oldest_unpruned_nonce: 21, - latest_received_nonce: 20, - latest_generated_nonce: 21, - }), - size: ProofSize::Minimal(0), - dispatch_fee_payment: DispatchFeePayment::AtTargetChain, - }); - }: receive_messages_proof( - RawOrigin::Signed(relayer_id_on_target), - relayer_id_on_source, - proof, - messages_count, - dispatch_weight - ) - verify { - assert_eq!( - crate::InboundLanes::::get(&T::bench_lane_id()).last_delivered_nonce(), - 20 + i as MessageNonce, - ); - assert_eq!( - crate::Pallet::::inbound_latest_confirmed_nonce(T::bench_lane_id()), - 20, - ); - } - - // Benchmark `receive_messages_delivery_proof` extrinsic where single relayer delivers multiple messages. - receive_delivery_proof_for_multiple_messages_by_single_relayer { - // there actually should be used value of `MaxUnrewardedRelayerEntriesAtInboundLane` from the bridged - // chain, but we're more interested in additional weight/message than in max weight - let i in 1..T::MaxUnrewardedRelayerEntriesAtInboundLane::get() - .try_into() - .expect("Value of MaxUnrewardedRelayerEntriesAtInboundLane is too large"); - - let relayers_fund_id = crate::relayer_fund_account_id::(); - let relayer_id: T::AccountId = account("relayer", 0, SEED); - let relayer_balance = T::account_balance(&relayer_id); - T::endow_account(&relayers_fund_id); - - // send messages that we're going to confirm - for _ in 1..=i { - send_regular_message::(); - } - - let relayers_state = UnrewardedRelayersState { - unrewarded_relayer_entries: 1, - messages_in_oldest_entry: 1, - total_messages: i as MessageNonce, - }; - let mut delivered_messages = DeliveredMessages::new(1, true); - for nonce in 2..=i { - delivered_messages.note_dispatched_message(true); - } - let proof = T::prepare_message_delivery_proof(MessageDeliveryProofParams { - lane: T::bench_lane_id(), - inbound_lane_data: InboundLaneData { - relayers: vec![UnrewardedRelayer { - relayer: relayer_id.clone(), - messages: delivered_messages, - }].into_iter().collect(), - last_confirmed_nonce: 0, - }, - size: ProofSize::Minimal(0), - }); - }: receive_messages_delivery_proof(RawOrigin::Signed(relayer_id.clone()), proof, relayers_state) - verify { - ensure_relayer_rewarded::(&relayer_id, &relayer_balance); - } - - // Benchmark `receive_messages_delivery_proof` extrinsic where every relayer delivers single messages. - receive_delivery_proof_for_multiple_messages_by_multiple_relayers { - // there actually should be used value of `MaxUnconfirmedMessagesAtInboundLane` from the bridged - // chain, but we're more interested in additional weight/message than in max weight - let i in 1..T::MaxUnconfirmedMessagesAtInboundLane::get() - .try_into() - .expect("Value of MaxUnconfirmedMessagesAtInboundLane is too large "); - - let relayers_fund_id = crate::relayer_fund_account_id::(); - let confirmation_relayer_id = account("relayer", 0, SEED); - let relayers: BTreeMap = (1..=i) - .map(|j| { - let relayer_id = account("relayer", j + 1, SEED); - let relayer_balance = T::account_balance(&relayer_id); - (relayer_id, relayer_balance) - }) - .collect(); - T::endow_account(&relayers_fund_id); - - // send messages that we're going to confirm - for _ in 1..=i { - send_regular_message::(); - } - - let relayers_state = UnrewardedRelayersState { - unrewarded_relayer_entries: i as MessageNonce, - messages_in_oldest_entry: 1, - total_messages: i as MessageNonce, - }; - let proof = T::prepare_message_delivery_proof(MessageDeliveryProofParams { - lane: T::bench_lane_id(), - inbound_lane_data: InboundLaneData { - relayers: relayers - .keys() - .enumerate() - .map(|(j, relayer)| UnrewardedRelayer { - relayer: relayer.clone(), - messages: DeliveredMessages::new(j as MessageNonce + 1, true), - }) - .collect(), - last_confirmed_nonce: 0, - }, - size: ProofSize::Minimal(0), - }); - }: receive_messages_delivery_proof(RawOrigin::Signed(confirmation_relayer_id), proof, relayers_state) - verify { - for (relayer_id, prev_balance) in relayers { - ensure_relayer_rewarded::(&relayer_id, &prev_balance); - } - } } fn send_regular_message, I: 'static>() { diff --git a/modules/messages/src/weights.rs b/modules/messages/src/weights.rs index 7c5df201f9..462f768a08 100644 --- a/modules/messages/src/weights.rs +++ b/modules/messages/src/weights.rs @@ -62,13 +62,6 @@ pub trait WeightInfo { fn receive_delivery_proof_for_single_message() -> Weight; fn receive_delivery_proof_for_two_messages_by_single_relayer() -> Weight; fn receive_delivery_proof_for_two_messages_by_two_relayers() -> Weight; - fn send_messages_of_various_lengths(i: u32) -> Weight; - fn receive_multiple_messages_proof(i: u32) -> Weight; - fn receive_message_proofs_with_extra_nodes(i: u32) -> Weight; - fn receive_message_proofs_with_large_leaf(i: u32) -> Weight; - fn receive_multiple_messages_proof_with_outbound_lane_state(i: u32) -> Weight; - fn receive_delivery_proof_for_multiple_messages_by_single_relayer(i: u32) -> Weight; - fn receive_delivery_proof_for_multiple_messages_by_multiple_relayers(i: u32) -> Weight; } /// Weights for `pallet_bridge_messages` using the Millau node and recommended hardware. @@ -145,51 +138,6 @@ impl WeightInfo for MillauWeight { .saturating_add(T::DbWeight::get().reads(8 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } - fn send_messages_of_various_lengths(i: u32) -> Weight { - (0 as Weight) - .saturating_add((2_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(7 as Weight)) - .saturating_add(T::DbWeight::get().writes(12 as Weight)) - } - fn receive_multiple_messages_proof(i: u32) -> Weight { - (56_209_000 as Weight) - .saturating_add((109_502_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(6 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) - } - fn receive_message_proofs_with_extra_nodes(i: u32) -> Weight { - (0 as Weight) - .saturating_add((5_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(6 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) - } - fn receive_message_proofs_with_large_leaf(i: u32) -> Weight { - (50_281_000 as Weight) - .saturating_add((2_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(6 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) - } - fn receive_multiple_messages_proof_with_outbound_lane_state(i: u32) -> Weight { - (70_297_000 as Weight) - .saturating_add((110_892_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(6 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) - } - fn receive_delivery_proof_for_multiple_messages_by_single_relayer(i: u32) -> Weight { - (0 as Weight) - .saturating_add((8_016_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) - .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(i as Weight))) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) - } - fn receive_delivery_proof_for_multiple_messages_by_multiple_relayers(i: u32) -> Weight { - (0 as Weight) - .saturating_add((50_981_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(T::DbWeight::get().reads(5 as Weight)) - .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(i as Weight))) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) - .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(i as Weight))) - } } // For backwards compatibility and tests @@ -265,49 +213,4 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(8 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } - fn send_messages_of_various_lengths(i: u32) -> Weight { - (0 as Weight) - .saturating_add((2_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(7 as Weight)) - .saturating_add(RocksDbWeight::get().writes(12 as Weight)) - } - fn receive_multiple_messages_proof(i: u32) -> Weight { - (56_209_000 as Weight) - .saturating_add((109_502_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(6 as Weight)) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) - } - fn receive_message_proofs_with_extra_nodes(i: u32) -> Weight { - (0 as Weight) - .saturating_add((5_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(6 as Weight)) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) - } - fn receive_message_proofs_with_large_leaf(i: u32) -> Weight { - (50_281_000 as Weight) - .saturating_add((2_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(6 as Weight)) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) - } - fn receive_multiple_messages_proof_with_outbound_lane_state(i: u32) -> Weight { - (70_297_000 as Weight) - .saturating_add((110_892_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(6 as Weight)) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) - } - fn receive_delivery_proof_for_multiple_messages_by_single_relayer(i: u32) -> Weight { - (0 as Weight) - .saturating_add((8_016_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) - .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(i as Weight))) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) - } - fn receive_delivery_proof_for_multiple_messages_by_multiple_relayers(i: u32) -> Weight { - (0 as Weight) - .saturating_add((50_981_000 as Weight).saturating_mul(i as Weight)) - .saturating_add(RocksDbWeight::get().reads(5 as Weight)) - .saturating_add(RocksDbWeight::get().reads((2 as Weight).saturating_mul(i as Weight))) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) - .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(i as Weight))) - } } From 8b9c4ec16d1c2bee4855cc1b18721e330dd3cee5 Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Wed, 12 Jan 2022 10:19:46 +0300 Subject: [PATCH 3/3] do not start spec_version guard when version mode is set to auto (#1278) --- Cargo.lock | 1 + relays/bin-substrate/Cargo.toml | 1 + .../src/chains/kusama_headers_to_polkadot.rs | 29 +++++------ .../src/chains/polkadot_headers_to_kusama.rs | 29 +++++------ .../src/chains/rococo_headers_to_wococo.rs | 29 +++++------ .../src/chains/wococo_headers_to_rococo.rs | 29 +++++------ relays/bin-substrate/src/cli/mod.rs | 10 ++++ relays/bin-substrate/src/cli/relay_headers.rs | 7 ++- .../src/cli/relay_headers_and_messages.rs | 8 +++- relays/client-substrate/src/guard.rs | 15 ++++++ .../src/finality_guards.rs | 48 +++++++++++++++++++ .../src/finality_pipeline.rs | 8 +++- relays/lib-substrate-relay/src/lib.rs | 1 + 13 files changed, 154 insertions(+), 61 deletions(-) create mode 100644 relays/lib-substrate-relay/src/finality_guards.rs diff --git a/Cargo.lock b/Cargo.lock index 4b988640ae..1f0ec406ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10969,6 +10969,7 @@ version = "1.0.0" dependencies = [ "anyhow", "async-std", + "async-trait", "bp-header-chain", "bp-kusama", "bp-message-dispatch", diff --git a/relays/bin-substrate/Cargo.toml b/relays/bin-substrate/Cargo.toml index 843340817e..86c1cedf37 100644 --- a/relays/bin-substrate/Cargo.toml +++ b/relays/bin-substrate/Cargo.toml @@ -8,6 +8,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] anyhow = "1.0" async-std = "1.9.0" +async-trait = "0.1.42" codec = { package = "parity-scale-codec", version = "2.2.0" } futures = "0.3.12" hex = "0.4" diff --git a/relays/bin-substrate/src/chains/kusama_headers_to_polkadot.rs b/relays/bin-substrate/src/chains/kusama_headers_to_polkadot.rs index 13d9f12141..f3a2d55304 100644 --- a/relays/bin-substrate/src/chains/kusama_headers_to_polkadot.rs +++ b/relays/bin-substrate/src/chains/kusama_headers_to_polkadot.rs @@ -16,7 +16,8 @@ //! Kusama-to-Polkadot headers sync entrypoint. -use sp_core::Pair; +use async_trait::async_trait; +use relay_polkadot_client::Polkadot; use substrate_relay_helper::{finality_pipeline::SubstrateFinalitySyncPipeline, TransactionParams}; /// Maximal saturating difference between `balance(now)` and `balance(now-24h)` to treat @@ -36,26 +37,26 @@ substrate_relay_helper::generate_mocked_submit_finality_proof_call_builder!( relay_polkadot_client::runtime::BridgeKusamaGrandpaCall::submit_finality_proof ); +#[async_trait] impl SubstrateFinalitySyncPipeline for KusamaFinalityToPolkadot { type SourceChain = relay_kusama_client::Kusama; - type TargetChain = relay_polkadot_client::Polkadot; + type TargetChain = Polkadot; type SubmitFinalityProofCallBuilder = KusamaFinalityToPolkadotCallBuilder; - type TransactionSignScheme = relay_polkadot_client::Polkadot; + type TransactionSignScheme = Polkadot; - fn start_relay_guards( - target_client: &relay_substrate_client::Client, + async fn start_relay_guards( + target_client: &relay_substrate_client::Client, transaction_params: &TransactionParams, - ) { - relay_substrate_client::guard::abort_on_spec_version_change( - target_client.clone(), - bp_polkadot::VERSION.spec_version, - ); - relay_substrate_client::guard::abort_when_account_balance_decreased( - target_client.clone(), - transaction_params.signer.public().into(), + enable_version_guard: bool, + ) -> relay_substrate_client::Result<()> { + substrate_relay_helper::finality_guards::start::( + target_client, + transaction_params, + enable_version_guard, MAXIMAL_BALANCE_DECREASE_PER_DAY, - ); + ) + .await } } diff --git a/relays/bin-substrate/src/chains/polkadot_headers_to_kusama.rs b/relays/bin-substrate/src/chains/polkadot_headers_to_kusama.rs index 5f65d658b9..27bcb24578 100644 --- a/relays/bin-substrate/src/chains/polkadot_headers_to_kusama.rs +++ b/relays/bin-substrate/src/chains/polkadot_headers_to_kusama.rs @@ -16,7 +16,8 @@ //! Polkadot-to-Kusama headers sync entrypoint. -use sp_core::Pair; +use async_trait::async_trait; +use relay_kusama_client::Kusama; use substrate_relay_helper::{finality_pipeline::SubstrateFinalitySyncPipeline, TransactionParams}; /// Maximal saturating difference between `balance(now)` and `balance(now-24h)` to treat @@ -36,26 +37,26 @@ substrate_relay_helper::generate_mocked_submit_finality_proof_call_builder!( relay_kusama_client::runtime::BridgePolkadotGrandpaCall::submit_finality_proof ); +#[async_trait] impl SubstrateFinalitySyncPipeline for PolkadotFinalityToKusama { type SourceChain = relay_polkadot_client::Polkadot; - type TargetChain = relay_kusama_client::Kusama; + type TargetChain = Kusama; type SubmitFinalityProofCallBuilder = PolkadotFinalityToKusamaCallBuilder; - type TransactionSignScheme = relay_kusama_client::Kusama; + type TransactionSignScheme = Kusama; - fn start_relay_guards( - target_client: &relay_substrate_client::Client, + async fn start_relay_guards( + target_client: &relay_substrate_client::Client, transaction_params: &TransactionParams, - ) { - relay_substrate_client::guard::abort_on_spec_version_change( - target_client.clone(), - bp_kusama::VERSION.spec_version, - ); - relay_substrate_client::guard::abort_when_account_balance_decreased( - target_client.clone(), - transaction_params.signer.public().into(), + enable_version_guard: bool, + ) -> relay_substrate_client::Result<()> { + substrate_relay_helper::finality_guards::start::( + target_client, + transaction_params, + enable_version_guard, MAXIMAL_BALANCE_DECREASE_PER_DAY, - ); + ) + .await } } diff --git a/relays/bin-substrate/src/chains/rococo_headers_to_wococo.rs b/relays/bin-substrate/src/chains/rococo_headers_to_wococo.rs index d0f1896db8..bb66a7422d 100644 --- a/relays/bin-substrate/src/chains/rococo_headers_to_wococo.rs +++ b/relays/bin-substrate/src/chains/rococo_headers_to_wococo.rs @@ -18,7 +18,8 @@ use crate::chains::wococo_headers_to_rococo::MAXIMAL_BALANCE_DECREASE_PER_DAY; -use sp_core::Pair; +use async_trait::async_trait; +use relay_wococo_client::Wococo; use substrate_relay_helper::{finality_pipeline::SubstrateFinalitySyncPipeline, TransactionParams}; /// Description of Rococo -> Wococo finalized headers bridge. @@ -31,25 +32,25 @@ substrate_relay_helper::generate_mocked_submit_finality_proof_call_builder!( relay_wococo_client::runtime::BridgeGrandpaRococoCall::submit_finality_proof ); +#[async_trait] impl SubstrateFinalitySyncPipeline for RococoFinalityToWococo { type SourceChain = relay_rococo_client::Rococo; - type TargetChain = relay_wococo_client::Wococo; + type TargetChain = Wococo; type SubmitFinalityProofCallBuilder = RococoFinalityToWococoCallBuilder; - type TransactionSignScheme = relay_wococo_client::Wococo; + type TransactionSignScheme = Wococo; - fn start_relay_guards( - target_client: &relay_substrate_client::Client, + async fn start_relay_guards( + target_client: &relay_substrate_client::Client, transaction_params: &TransactionParams, - ) { - relay_substrate_client::guard::abort_on_spec_version_change( - target_client.clone(), - bp_wococo::VERSION.spec_version, - ); - relay_substrate_client::guard::abort_when_account_balance_decreased( - target_client.clone(), - transaction_params.signer.public().into(), + enable_version_guard: bool, + ) -> relay_substrate_client::Result<()> { + substrate_relay_helper::finality_guards::start::( + target_client, + transaction_params, + enable_version_guard, MAXIMAL_BALANCE_DECREASE_PER_DAY, - ); + ) + .await } } diff --git a/relays/bin-substrate/src/chains/wococo_headers_to_rococo.rs b/relays/bin-substrate/src/chains/wococo_headers_to_rococo.rs index 2d8795fc83..a7bff59518 100644 --- a/relays/bin-substrate/src/chains/wococo_headers_to_rococo.rs +++ b/relays/bin-substrate/src/chains/wococo_headers_to_rococo.rs @@ -16,7 +16,8 @@ //! Wococo-to-Rococo headers sync entrypoint. -use sp_core::Pair; +use async_trait::async_trait; +use relay_rococo_client::Rococo; use substrate_relay_helper::{finality_pipeline::SubstrateFinalitySyncPipeline, TransactionParams}; /// Maximal saturating difference between `balance(now)` and `balance(now-24h)` to treat @@ -36,26 +37,26 @@ substrate_relay_helper::generate_mocked_submit_finality_proof_call_builder!( relay_rococo_client::runtime::BridgeGrandpaWococoCall::submit_finality_proof ); +#[async_trait] impl SubstrateFinalitySyncPipeline for WococoFinalityToRococo { type SourceChain = relay_wococo_client::Wococo; - type TargetChain = relay_rococo_client::Rococo; + type TargetChain = Rococo; type SubmitFinalityProofCallBuilder = WococoFinalityToRococoCallBuilder; - type TransactionSignScheme = relay_rococo_client::Rococo; + type TransactionSignScheme = Rococo; - fn start_relay_guards( - target_client: &relay_substrate_client::Client, + async fn start_relay_guards( + target_client: &relay_substrate_client::Client, transaction_params: &TransactionParams, - ) { - relay_substrate_client::guard::abort_on_spec_version_change( - target_client.clone(), - bp_rococo::VERSION.spec_version, - ); - relay_substrate_client::guard::abort_when_account_balance_decreased( - target_client.clone(), - transaction_params.signer.public().into(), + enable_version_guard: bool, + ) -> relay_substrate_client::Result<()> { + substrate_relay_helper::finality_guards::start::( + target_client, + transaction_params, + enable_version_guard, MAXIMAL_BALANCE_DECREASE_PER_DAY, - ); + ) + .await } } diff --git a/relays/bin-substrate/src/cli/mod.rs b/relays/bin-substrate/src/cli/mod.rs index 2a087e59b6..1ff46fdbb8 100644 --- a/relays/bin-substrate/src/cli/mod.rs +++ b/relays/bin-substrate/src/cli/mod.rs @@ -528,6 +528,16 @@ macro_rules! declare_chain_options { } impl [<$chain ConnectionParams>] { + /// Returns `true` if version guard can be started. + /// + /// There's no reason to run version guard when version mode is set to `Auto`. It can + /// lead to relay shutdown when chain is upgraded, even though we have explicitly + /// said that we don't want to shutdown. + #[allow(dead_code)] + pub fn can_start_version_guard(&self) -> bool { + self.[<$chain_prefix _runtime_version>].[<$chain_prefix _version_mode>] != RuntimeVersionType::Auto + } + /// Convert connection params into Substrate client. pub async fn to_client( &self, diff --git a/relays/bin-substrate/src/cli/relay_headers.rs b/relays/bin-substrate/src/cli/relay_headers.rs index 4b17a56819..e3c7fe76d3 100644 --- a/relays/bin-substrate/src/cli/relay_headers.rs +++ b/relays/bin-substrate/src/cli/relay_headers.rs @@ -157,7 +157,12 @@ impl RelayHeaders { signer: target_sign, mortality: target_transactions_mortality, }; - Finality::start_relay_guards(&target_client, &target_transactions_params); + Finality::start_relay_guards( + &target_client, + &target_transactions_params, + self.target.can_start_version_guard(), + ) + .await?; substrate_relay_helper::finality_pipeline::run::( source_client, diff --git a/relays/bin-substrate/src/cli/relay_headers_and_messages.rs b/relays/bin-substrate/src/cli/relay_headers_and_messages.rs index 3804513acd..212eb9c6a0 100644 --- a/relays/bin-substrate/src/cli/relay_headers_and_messages.rs +++ b/relays/bin-substrate/src/cli/relay_headers_and_messages.rs @@ -512,11 +512,15 @@ impl RelayHeadersAndMessages { LeftToRightFinality::start_relay_guards( &right_client, &left_to_right_transaction_params, - ); + params.right.can_start_version_guard(), + ) + .await?; RightToLeftFinality::start_relay_guards( &left_client, &right_to_left_transaction_params, - ); + params.left.can_start_version_guard(), + ) + .await?; let left_to_right_on_demand_headers = OnDemandHeadersRelay::new::( left_client.clone(), right_client.clone(), diff --git a/relays/client-substrate/src/guard.rs b/relays/client-substrate/src/guard.rs index c31482eec3..359a3f69d8 100644 --- a/relays/client-substrate/src/guard.rs +++ b/relays/client-substrate/src/guard.rs @@ -64,6 +64,13 @@ pub fn abort_on_spec_version_change( expected_spec_version: u32, ) { async_std::task::spawn(async move { + log::info!( + target: "bridge-guard", + "Starting spec_version guard for {}. Expected spec_version: {}", + C::NAME, + expected_spec_version, + ); + loop { let actual_spec_version = env.runtime_version().await; match actual_spec_version { @@ -103,6 +110,14 @@ pub fn abort_when_account_balance_decreased( const DAY: Duration = Duration::from_secs(60 * 60 * 24); async_std::task::spawn(async move { + log::info!( + target: "bridge-guard", + "Starting balance guard for {}/{:?}. Maximal decrease: {:?}", + C::NAME, + account_id, + maximal_decrease, + ); + let mut balances = VecDeque::new(); loop { diff --git a/relays/lib-substrate-relay/src/finality_guards.rs b/relays/lib-substrate-relay/src/finality_guards.rs new file mode 100644 index 0000000000..a3e69afe1b --- /dev/null +++ b/relays/lib-substrate-relay/src/finality_guards.rs @@ -0,0 +1,48 @@ +// Copyright 2019-2021 Parity Technologies (UK) Ltd. +// This file is part of Parity Bridges Common. + +// Parity Bridges Common is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity Bridges Common is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity Bridges Common. If not, see . + +//! Tools for starting guards of finality relays. + +use crate::TransactionParams; + +use relay_substrate_client::{ + AccountIdOf, AccountKeyPairOf, ChainWithBalances, TransactionSignScheme, +}; +use sp_core::Pair; + +/// Start finality relay guards. +pub async fn start>( + target_client: &relay_substrate_client::Client, + transaction_params: &TransactionParams, + enable_version_guard: bool, + maximal_balance_decrease_per_day: C::Balance, +) -> relay_substrate_client::Result<()> +where + AccountIdOf: From< as Pair>::Public>, +{ + if enable_version_guard { + relay_substrate_client::guard::abort_on_spec_version_change( + target_client.clone(), + target_client.simple_runtime_version().await?.0, + ); + } + relay_substrate_client::guard::abort_when_account_balance_decreased( + target_client.clone(), + transaction_params.signer.public().into(), + maximal_balance_decrease_per_day, + ); + Ok(()) +} diff --git a/relays/lib-substrate-relay/src/finality_pipeline.rs b/relays/lib-substrate-relay/src/finality_pipeline.rs index 87e50be039..07a1279ef4 100644 --- a/relays/lib-substrate-relay/src/finality_pipeline.rs +++ b/relays/lib-substrate-relay/src/finality_pipeline.rs @@ -22,6 +22,7 @@ use crate::{ TransactionParams, }; +use async_trait::async_trait; use bp_header_chain::justification::GrandpaJustification; use finality_relay::FinalitySyncPipeline; use pallet_bridge_grandpa::{Call as BridgeGrandpaCall, Config as BridgeGrandpaConfig}; @@ -40,6 +41,7 @@ use std::{fmt::Debug, marker::PhantomData}; pub(crate) const RECENT_FINALITY_PROOFS_LIMIT: usize = 4096; /// Substrate -> Substrate finality proofs synchronization pipeline. +#[async_trait] pub trait SubstrateFinalitySyncPipeline: 'static + Clone + Debug + Send + Sync { /// Headers of this chain are submitted to the `TargetChain`. type SourceChain: Chain; @@ -52,10 +54,12 @@ pub trait SubstrateFinalitySyncPipeline: 'static + Clone + Debug + Send + Sync { type TransactionSignScheme: TransactionSignScheme; /// Add relay guards if required. - fn start_relay_guards( + async fn start_relay_guards( _target_client: &Client, _transaction_params: &TransactionParams>, - ) { + _enable_version_guard: bool, + ) -> relay_substrate_client::Result<()> { + Ok(()) } } diff --git a/relays/lib-substrate-relay/src/lib.rs b/relays/lib-substrate-relay/src/lib.rs index c27aa7b708..27d91147c2 100644 --- a/relays/lib-substrate-relay/src/lib.rs +++ b/relays/lib-substrate-relay/src/lib.rs @@ -22,6 +22,7 @@ use std::time::Duration; pub mod conversion_rate_update; pub mod error; +pub mod finality_guards; pub mod finality_pipeline; pub mod finality_source; pub mod finality_target;