From f2d1a65123bd3ba76dc4cc6144ae8aa6f2707777 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Sat, 26 Aug 2023 00:01:13 +0200 Subject: [PATCH 1/7] Added short-benchmarks for cumulus --- .gitlab/pipeline/build.yml | 13 ++++++ .gitlab/pipeline/short-benchmarks.yml | 58 +++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/.gitlab/pipeline/build.yml b/.gitlab/pipeline/build.yml index 290ef8c8f72d1..b31a5761f75df 100644 --- a/.gitlab/pipeline/build.yml +++ b/.gitlab/pipeline/build.yml @@ -258,6 +258,19 @@ build-runtime-testing: - job: build-runtime-starters artifacts: false +build-short-benchmark-cumulus: + stage: build + extends: + - .docker-env + - .common-refs + - .run-immediately + - .collect-artifacts + script: + - cargo build --profile release --locked --features=runtime-benchmarks + - mkdir -p artifacts + - target/release/polkadot-parachain --version + - cp ./target/release/polkadot-parachain ./artifacts/ + # substrate build-linux-substrate: diff --git a/.gitlab/pipeline/short-benchmarks.yml b/.gitlab/pipeline/short-benchmarks.yml index 2993338cabb15..8d2d699814851 100644 --- a/.gitlab/pipeline/short-benchmarks.yml +++ b/.gitlab/pipeline/short-benchmarks.yml @@ -2,6 +2,9 @@ # Here are all jobs that are executed during "short-benchmarks" stage # Run all pallet benchmarks only once to check if there are any errors + +# run short-benchmarks for relay chain runtimes from polkadot + short-benchmark-polkadot: &short-bench stage: short-benchmarks extends: @@ -24,3 +27,58 @@ short-benchmark-westend: <<: *short-bench variables: RUNTIME: westend + +# run short-benchmarks for system parachain runtimes from cumulus + +.short-benchmark-cumulus: &short-bench-cumulus + stage: short-benchmarks + extends: + - .common-refs + - .docker-env + needs: + - job: build-short-benchmark + artifacts: true + variables: + RUNTIME_CHAIN: benchmarked-runtime-chain + script: + - ./artifacts/polkadot-parachain benchmark pallet --wasm-execution compiled --chain $RUNTIME_CHAIN --pallet "*" --extrinsic "*" --steps 2 --repeat 1 + +short-benchmark-asset-hub-polkadot: + <<: *short-bench-cumulus + variables: + RUNTIME_CHAIN: asset-hub-polkadot-dev + +short-benchmark-asset-hub-kusama: + <<: *short-bench-cumulus + variables: + RUNTIME_CHAIN: asset-hub-kusama-dev + +short-benchmark-asset-hub-westend: + <<: *short-bench-cumulus + variables: + RUNTIME_CHAIN: asset-hub-westend-dev + +short-benchmark-bridge-hub-polkadot: + <<: *short-bench-cumulus + variables: + RUNTIME_CHAIN: bridge-hub-polkadot-dev + +short-benchmark-bridge-hub-kusama: + <<: *short-bench-cumulus + variables: + RUNTIME_CHAIN: bridge-hub-kusama-dev + +short-benchmark-bridge-hub-rococo: + <<: *short-bench-cumulus + variables: + RUNTIME_CHAIN: bridge-hub-rococo-dev + +short-benchmark-collectives-polkadot : + <<: *short-bench-cumulus + variables: + RUNTIME_CHAIN: collectives-polkadot-dev + +short-benchmark-glutton-kusama : + <<: *short-bench-cumulus + variables: + RUNTIME_CHAIN: glutton-kusama-dev-1300 \ No newline at end of file From c1d3417084479ee2b785ed6158cc42ae62437dd1 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Sat, 26 Aug 2023 00:16:10 +0200 Subject: [PATCH 2/7] Added `--bin` flag for short-benchmarks --- .gitlab/pipeline/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab/pipeline/build.yml b/.gitlab/pipeline/build.yml index b31a5761f75df..120d96a9993b8 100644 --- a/.gitlab/pipeline/build.yml +++ b/.gitlab/pipeline/build.yml @@ -139,7 +139,7 @@ build-short-benchmark: - .run-immediately - .collect-artifacts script: - - cargo build --profile release --locked --features=runtime-benchmarks + - cargo build --profile release --locked --features=runtime-benchmarks --bin polkadot - mkdir -p artifacts - target/release/polkadot --version - cp ./target/release/polkadot ./artifacts/ @@ -266,7 +266,7 @@ build-short-benchmark-cumulus: - .run-immediately - .collect-artifacts script: - - cargo build --profile release --locked --features=runtime-benchmarks + - cargo build --profile release --locked --features=runtime-benchmarks --bin polkadot-parachain - mkdir -p artifacts - target/release/polkadot-parachain --version - cp ./target/release/polkadot-parachain ./artifacts/ From a2b4e1cfa6915dfd6c23cb0009cfa1d038cf83e2 Mon Sep 17 00:00:00 2001 From: alvicsam Date: Tue, 29 Aug 2023 14:06:11 +0200 Subject: [PATCH 3/7] fix dependency for short-benchmark-cumulus --- .gitlab/pipeline/short-benchmarks.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitlab/pipeline/short-benchmarks.yml b/.gitlab/pipeline/short-benchmarks.yml index 8d2d699814851..baf4c310b3a52 100644 --- a/.gitlab/pipeline/short-benchmarks.yml +++ b/.gitlab/pipeline/short-benchmarks.yml @@ -36,7 +36,7 @@ short-benchmark-westend: - .common-refs - .docker-env needs: - - job: build-short-benchmark + - job: build-short-benchmark-cumulus artifacts: true variables: RUNTIME_CHAIN: benchmarked-runtime-chain @@ -73,12 +73,12 @@ short-benchmark-bridge-hub-rococo: variables: RUNTIME_CHAIN: bridge-hub-rococo-dev -short-benchmark-collectives-polkadot : +short-benchmark-collectives-polkadot: <<: *short-bench-cumulus variables: RUNTIME_CHAIN: collectives-polkadot-dev -short-benchmark-glutton-kusama : +short-benchmark-glutton-kusama: <<: *short-bench-cumulus variables: - RUNTIME_CHAIN: glutton-kusama-dev-1300 \ No newline at end of file + RUNTIME_CHAIN: glutton-kusama-dev-1300 From 8f19fd95d45eb1af6681837b30001cd81719de21 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Thu, 31 Aug 2023 10:53:04 +0200 Subject: [PATCH 4/7] Fixed benchmark with new XCM::V3 `MAX_INSTRUCTIONS_TO_DECODE` --- .../bridges/bin/runtime-common/src/messages_benchmarking.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cumulus/bridges/bin/runtime-common/src/messages_benchmarking.rs b/cumulus/bridges/bin/runtime-common/src/messages_benchmarking.rs index 60f4bb17858c6..5d25fabe58da4 100644 --- a/cumulus/bridges/bin/runtime-common/src/messages_benchmarking.rs +++ b/cumulus/bridges/bin/runtime-common/src/messages_benchmarking.rs @@ -65,6 +65,11 @@ fn prepare_inbound_message( // we don't need to be super-precise with `expected_size` here let xcm_size = expected_size.saturating_sub(location_encoded_size); + + // XCM V3 introduced limit for max instructions + const MAX_INSTRUCTIONS_TO_DECODE: u32 = 100; + let xcm_size = sp_std::cmp::min(xcm_size, MAX_INSTRUCTIONS_TO_DECODE as usize); + let xcm = xcm::VersionedXcm::<()>::V3(vec![Instruction::ClearOrigin; xcm_size].into()); // this is the `BridgeMessage` from polkadot xcm builder, but it has no constructor From 27d84d854d56d2c168fd4e4f05057c2cdada14a4 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Fri, 1 Sep 2023 11:33:02 +0200 Subject: [PATCH 5/7] Fixed benchmark for bridge messages pallets --- .../src/messages_benchmarking.rs | 92 +++++++++++++------ .../modules/messages/src/weights_ext.rs | 4 +- .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 10 +- 3 files changed, 75 insertions(+), 31 deletions(-) diff --git a/cumulus/bridges/bin/runtime-common/src/messages_benchmarking.rs b/cumulus/bridges/bin/runtime-common/src/messages_benchmarking.rs index 5d25fabe58da4..d80a88f1068c8 100644 --- a/cumulus/bridges/bin/runtime-common/src/messages_benchmarking.rs +++ b/cumulus/bridges/bin/runtime-common/src/messages_benchmarking.rs @@ -29,7 +29,7 @@ use crate::{ }, }; -use bp_messages::storage_keys; +use bp_messages::{storage_keys, MessagePayload}; use bp_polkadot_core::parachains::ParaHash; use bp_runtime::{ record_all_trie_keys, Chain, Parachain, RawStorageProof, StorageProofSize, UnderlyingChainOf, @@ -45,8 +45,8 @@ use xcm::v3::prelude::*; /// Prepare inbound bridge message according to given message proof parameters. fn prepare_inbound_message( params: &MessageProofParams, - destination: InteriorMultiLocation, -) -> Vec { + successful_dispatch_message_generator: impl Fn(usize) -> MessagePayload, +) -> MessagePayload { // we only care about **this** message size when message proof needs to be `Minimal` let expected_size = match params.size { StorageProofSize::Minimal(size) => size as usize, @@ -58,25 +58,15 @@ fn prepare_inbound_message( return vec![0u8; expected_size] } - // else let's prepare successful message. For XCM bridge hubs, it is the message that - // will be pushed further to some XCM queue (XCMP/UMP) - let location = xcm::VersionedInteriorMultiLocation::V3(destination); - let location_encoded_size = location.encoded_size(); - - // we don't need to be super-precise with `expected_size` here - let xcm_size = expected_size.saturating_sub(location_encoded_size); - - // XCM V3 introduced limit for max instructions - const MAX_INSTRUCTIONS_TO_DECODE: u32 = 100; - let xcm_size = sp_std::cmp::min(xcm_size, MAX_INSTRUCTIONS_TO_DECODE as usize); - - let xcm = xcm::VersionedXcm::<()>::V3(vec![Instruction::ClearOrigin; xcm_size].into()); - - // this is the `BridgeMessage` from polkadot xcm builder, but it has no constructor - // or public fields, so just tuple - // (double encoding, because `.encode()` is called on original Xcm BLOB when it is pushed - // to the storage) - (location, xcm).encode().encode() + // else let's prepare successful message. + let msg = successful_dispatch_message_generator(expected_size); + assert!( + msg.len() >= expected_size, + "msg.len(): {} does not match expected_size: {}", + expected_size, + msg.len() + ); + msg } /// Prepare proof of messages for the `receive_messages_proof` call. @@ -89,7 +79,7 @@ fn prepare_inbound_message( /// function. pub fn prepare_message_proof_from_grandpa_chain( params: MessageProofParams, - message_destination: InteriorMultiLocation, + message_generator: impl Fn(usize) -> MessagePayload, ) -> (FromBridgedChainMessagesProof>>, Weight) where R: pallet_bridge_grandpa::Config>>, @@ -102,7 +92,7 @@ where params.message_nonces.clone(), params.outbound_lane_data.clone(), params.size, - prepare_inbound_message(¶ms, message_destination), + prepare_inbound_message(¶ms, message_generator), encode_all_messages, encode_lane_data, ); @@ -132,7 +122,7 @@ where /// `prepare_message_proof_from_grandpa_chain` function. pub fn prepare_message_proof_from_parachain( params: MessageProofParams, - message_destination: InteriorMultiLocation, + message_generator: impl Fn(usize) -> MessagePayload, ) -> (FromBridgedChainMessagesProof>>, Weight) where R: pallet_bridge_parachains::Config, @@ -146,7 +136,7 @@ where params.message_nonces.clone(), params.outbound_lane_data.clone(), params.size, - prepare_inbound_message(¶ms, message_destination), + prepare_inbound_message(¶ms, message_generator), encode_all_messages, encode_lane_data, ); @@ -296,3 +286,53 @@ where pallet_bridge_parachains::initialize_for_benchmarks::(bridged_header); (bridged_block_number, bridged_header_hash) } + +/// Returns callback which generates `BridgeMessage` from Polkadot XCM builder based on +/// `expected_message_size` for benchmark. +pub fn generate_xcm_builder_bridge_message_sample( + destination: InteriorMultiLocation, +) -> impl Fn(usize) -> MessagePayload { + move |expected_message_size| -> MessagePayload { + // For XCM bridge hubs, it is the message that + // will be pushed further to some XCM queue (XCMP/UMP) + let location = xcm::VersionedInteriorMultiLocation::V3(destination); + let location_encoded_size = location.encoded_size(); + + // we don't need to be super-precise with `expected_size` here + let xcm_size = expected_message_size.saturating_sub(location_encoded_size); + let xcm_data_size = xcm_size.saturating_sub( + // minus empty instruction size + xcm::v3::Instruction::<()>::ExpectPallet { + index: 0, + name: vec![], + module_name: vec![], + crate_major: 0, + min_crate_minor: 0, + } + .encoded_size(), + ); + + log::trace!( + target: "runtime::bridge-benchmarks", + "generate_xcm_builder_bridge_message_sample with expected_message_size: {}, location_encoded_size: {}, xcm_size: {}, xcm_data_size: {}", + expected_message_size, location_encoded_size, xcm_size, xcm_data_size, + ); + + let xcm = xcm::VersionedXcm::<()>::V3( + vec![xcm::v3::Instruction::<()>::ExpectPallet { + index: 0, + name: vec![42; xcm_data_size], + module_name: vec![], + crate_major: 0, + min_crate_minor: 0, + }] + .into(), + ); + + // this is the `BridgeMessage` from polkadot xcm builder, but it has no constructor + // or public fields, so just tuple + // (double encoding, because `.encode()` is called on original Xcm BLOB when it is pushed + // to the storage) + (location, xcm).encode().encode() + } +} diff --git a/cumulus/bridges/modules/messages/src/weights_ext.rs b/cumulus/bridges/modules/messages/src/weights_ext.rs index 1be24a738a495..aeb3a581a69ee 100644 --- a/cumulus/bridges/modules/messages/src/weights_ext.rs +++ b/cumulus/bridges/modules/messages/src/weights_ext.rs @@ -29,8 +29,8 @@ pub const EXPECTED_DEFAULT_MESSAGE_LENGTH: u32 = 128; /// calls we're checking here would fit 1KB. const SIGNED_EXTENSIONS_SIZE: u32 = 1024; -/// Number of extra bytes (excluding size of storage value itself) of storage proof, built at -/// Rialto chain. This mostly depends on number of entries (and their density) in the storage trie. +/// Number of extra bytes (excluding size of storage value itself) of storage proof. +/// This mostly depends on number of entries (and their density) in the storage trie. /// Some reserve is reserved to account future chain growth. pub const EXTRA_STORAGE_PROOF_SIZE: u32 = 1024; diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index db53867f09440..a872e2327303c 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -1040,7 +1040,11 @@ impl_runtime_apis! { type XcmBalances = pallet_xcm_benchmarks::fungible::Pallet::; type XcmGeneric = pallet_xcm_benchmarks::generic::Pallet::; - use bridge_runtime_common::messages_benchmarking::{prepare_message_delivery_proof_from_parachain, prepare_message_proof_from_parachain}; + use bridge_runtime_common::messages_benchmarking::{ + prepare_message_delivery_proof_from_parachain, + prepare_message_proof_from_parachain, + generate_xcm_builder_bridge_message_sample, + }; use pallet_bridge_messages::benchmarking::{ Config as BridgeMessagesConfig, Pallet as BridgeMessagesBench, @@ -1072,7 +1076,7 @@ impl_runtime_apis! { Runtime, BridgeGrandpaWococoInstance, bridge_hub_rococo_config::WithBridgeHubWococoMessageBridge, - >(params, X2(GlobalConsensus(Rococo), Parachain(42))) + >(params, generate_xcm_builder_bridge_message_sample(X2(GlobalConsensus(Rococo), Parachain(42)))) } fn prepare_message_delivery_proof( @@ -1115,7 +1119,7 @@ impl_runtime_apis! { Runtime, BridgeGrandpaRococoInstance, bridge_hub_wococo_config::WithBridgeHubRococoMessageBridge, - >(params, X2(GlobalConsensus(Wococo), Parachain(42))) + >(params, generate_xcm_builder_bridge_message_sample(X2(GlobalConsensus(Wococo), Parachain(42)))) } fn prepare_message_delivery_proof( From 14db00757f33a78f38699c85713c05169a7a2240 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Fri, 1 Sep 2023 12:34:48 +0200 Subject: [PATCH 6/7] Update .gitlab/pipeline/short-benchmarks.yml Co-authored-by: Alexander Samusev <41779041+alvicsam@users.noreply.github.com> --- .gitlab/pipeline/short-benchmarks.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitlab/pipeline/short-benchmarks.yml b/.gitlab/pipeline/short-benchmarks.yml index baf4c310b3a52..3723cfab21ff7 100644 --- a/.gitlab/pipeline/short-benchmarks.yml +++ b/.gitlab/pipeline/short-benchmarks.yml @@ -40,6 +40,8 @@ short-benchmark-westend: artifacts: true variables: RUNTIME_CHAIN: benchmarked-runtime-chain + tags: + - benchmark script: - ./artifacts/polkadot-parachain benchmark pallet --wasm-execution compiled --chain $RUNTIME_CHAIN --pallet "*" --extrinsic "*" --steps 2 --repeat 1 From e0c662b6f7b39e527efda28c66cc5c0a9e64b656 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Fri, 1 Sep 2023 12:37:29 +0200 Subject: [PATCH 7/7] Added tag/benchmarks also to the short-benchmark-polkadot --- .gitlab/pipeline/short-benchmarks.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitlab/pipeline/short-benchmarks.yml b/.gitlab/pipeline/short-benchmarks.yml index 3723cfab21ff7..81601fba32acf 100644 --- a/.gitlab/pipeline/short-benchmarks.yml +++ b/.gitlab/pipeline/short-benchmarks.yml @@ -15,6 +15,8 @@ short-benchmark-polkadot: &short-bench artifacts: true variables: RUNTIME: polkadot + tags: + - benchmark script: - ./artifacts/polkadot benchmark pallet --execution wasm --wasm-execution compiled --chain $RUNTIME-dev --pallet "*" --extrinsic "*" --steps 2 --repeat 1