Skip to content

Commit

Permalink
Inbound queue benchmarks (#876)
Browse files Browse the repository at this point in the history
* Start with inbound channel benchmarks.

* Add method to set execution header storage for benchmark test.

* Working on benchmarks

* Basic working version

* Cleanup

* Removes cleanup.

* Adds some comments for Alistair.

* Adds branch name.

* Makes note

* Test transactions

* Cleaning up beacon client deps.

* Clean up comments.

* Tests cleanup.

* Fixes non-benchmark test runs.

* Cleanup.

* Update fixtures and generates benchmarks.

* Revert relayer logs.

* Cleanup BenchmarkHelper impl and inbound queue dependencies.

* fmt

* Cleanup imports.

* Cleanup imports.

* Touch

* Adds weights in inbound queue pallet.

* Fix tests.

* Update cumulus.

---------

Co-authored-by: claravanstaden <Cats 4 life!>
  • Loading branch information
claravanstaden committed Jul 18, 2023
1 parent 4947f1e commit a99fc50
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 13 deletions.
3 changes: 3 additions & 0 deletions parachain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions parachain/pallets/ethereum-beacon-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ use snowbridge_core::Proof;
use functions::{
compute_epoch, compute_period, decompress_sync_committee_bits, sync_committee_sum,
};
pub use types::ExecutionHeaderBuffer;
use types::{
CheckpointUpdate, ExecutionHeaderBuffer, ExecutionHeaderUpdate, FinalizedBeaconStateBuffer,
SyncCommitteePrepared, Update,
CheckpointUpdate, ExecutionHeaderUpdate, FinalizedBeaconStateBuffer, SyncCommitteePrepared,
Update,
};

pub use pallet::*;
Expand Down Expand Up @@ -165,17 +166,16 @@ pub mod pallet {

/// Execution Headers
#[pallet::storage]
pub(super) type ExecutionHeaders<T: Config> =
pub type ExecutionHeaders<T: Config> =
StorageMap<_, Identity, H256, CompactExecutionHeader, OptionQuery>;

/// Execution Headers: Current position in ring buffer
#[pallet::storage]
pub(crate) type ExecutionHeaderIndex<T: Config> = StorageValue<_, u32, ValueQuery>;
pub type ExecutionHeaderIndex<T: Config> = StorageValue<_, u32, ValueQuery>;

/// Execution Headers: Mapping of ring buffer index to a pruning candidate
#[pallet::storage]
pub(crate) type ExecutionHeaderMapping<T: Config> =
StorageMap<_, Identity, u32, H256, ValueQuery>;
pub type ExecutionHeaderMapping<T: Config> = StorageMap<_, Identity, u32, H256, ValueQuery>;

#[pallet::call]
impl<T: Config> Pallet<T> {
Expand Down
2 changes: 1 addition & 1 deletion parachain/pallets/ethereum-beacon-client/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub type NextSyncCommitteeUpdate = primitives::NextSyncCommitteeUpdate<SC_SIZE>;
pub use primitives::ExecutionHeaderUpdate;

/// ExecutionHeader ring buffer implementation
pub(crate) type ExecutionHeaderBuffer<T> = RingBufferMapImpl<
pub type ExecutionHeaderBuffer<T> = RingBufferMapImpl<
u32,
<T as crate::Config>::MaxExecutionHeadersToKeep,
crate::ExecutionHeaderIndex<T>,
Expand Down
6 changes: 5 additions & 1 deletion parachain/pallets/inbound-queue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ xcm = { git = "https://github.com/paritytech/polkadot.git", branch = "master", d
snowbridge-core = { path = "../../primitives/core", default-features = false }
snowbridge-ethereum = { path = "../../primitives/ethereum", default-features = false }
snowbridge-router-primitives = { path = "../../primitives/router", default-features = false }
snowbridge-beacon-primitives = { path = "../../primitives/beacon", default-features = false }
ethabi = { git = "https://github.com/Snowfork/ethabi-decode.git", package = "ethabi-decode", branch = "master", default-features = false }

[dev-dependencies]
frame-benchmarking = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-keyring = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
snowbridge-ethereum-beacon-client = { path = "../../pallets/ethereum-beacon-client", default-features = false }

hex-literal = { version = "0.4.1" }
rlp = { version = "0.5" }
Expand All @@ -57,7 +59,9 @@ std = [
"snowbridge-ethereum/std",
"snowbridge-router-primitives/std",
"ethabi/std",
"xcm/std"
"xcm/std",
"snowbridge-beacon-primitives/std",
"snowbridge-ethereum-beacon-client/std",
]
runtime-benchmarks = [
"snowbridge-core/runtime-benchmarks",
Expand Down
2 changes: 0 additions & 2 deletions parachain/pallets/inbound-queue/src/benchmarking.rs

This file was deleted.

36 changes: 36 additions & 0 deletions parachain/pallets/inbound-queue/src/benchmarking/fixtures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use super::*;
use hex_literal::hex;
use snowbridge_beacon_primitives::CompactExecutionHeader;
use snowbridge_core::{Message, Proof};

pub struct InboundQueueTest {
pub execution_header: CompactExecutionHeader,
pub message: Message,
}

pub fn make_create_message() -> InboundQueueTest {
InboundQueueTest{
execution_header: CompactExecutionHeader{
parent_hash: hex!("04a7f6ab8282203562c62f38b0ab41d32aaebe2c7ea687702b463148a6429e04").into(),
block_number: 55,
state_root: hex!("894d968712976d613519f973a317cb0781c7b039c89f27ea2b7ca193f7befdb3").into(),
receipts_root: hex!("cf0d1c1ba57d1e0edfb59786c7e30c2b7e12bd54612b00cd21c4eaeecedf44fb").into(),
},
message: Message {
data: hex!("f9011c94ee9170abfbf9421ad6dd07f6bdec9d89f2b581e0f863a01b11dcf133cc240f682dab2d3a8e4cd35c5da8c9cf99adac4336f8512584c5ada000000000000000000000000000000000000000000000000000000000000003e8a00000000000000000000000000000000000000000000000000000000000000001b8a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004b000f000000000000000100d184c103f7acc340847eee82a0b909e3358bc28d440edffa1352b13227e8ee646f3ea37456dec701345772617070656420457468657210574554481235003511000000000000000000000000000000000000000000").to_vec(),
proof: Proof {
block_hash: hex!("05aaa60b0f27cce9e71909508527264b77ee14da7b5bf915fcc4e32715333213").into(),
tx_index: 0,
data: (vec![
hex!("cf0d1c1ba57d1e0edfb59786c7e30c2b7e12bd54612b00cd21c4eaeecedf44fb").to_vec(),
hex!("d21fc4f68ab05bc4dcb23c67008e92c4d466437cdd6ed7aad0c008944c185510").to_vec(),
hex!("b9890f91ca0d77aa2a4adfaf9b9e40c94cac9e638b6d9797923865872944b646").to_vec(),
], vec![
hex!("f90131a0b601337b3aa10a671caa724eba641e759399979856141d3aea6b6b4ac59b889ba00c7d5dd48be9060221a02fb8fa213860b4c50d47046c8fa65ffaba5737d569e0a094601b62a1086cd9c9cb71a7ebff9e718f3217fd6e837efe4246733c0a196f63a06a4b0dd0aefc37b3c77828c8f07d1b7a2455ceb5dbfd3c77d7d6aeeddc2f7e8ca0d6e8e23142cdd8ec219e1f5d8b56aa18e456702b195deeaa210327284d42ade4a08a313d4c87023005d1ab631bbfe3f5de1e405d0e66d0bef3e033f1e5711b5521a0bf09a5d9a48b10ade82b8d6a5362a15921c8b5228a3487479b467db97411d82fa0f95cccae2a7c572ef3c566503e30bac2b2feb2d2f26eebf6d870dcf7f8cf59cea0d21fc4f68ab05bc4dcb23c67008e92c4d466437cdd6ed7aad0c008944c1855108080808080808080").to_vec(),
hex!("f851a0b9890f91ca0d77aa2a4adfaf9b9e40c94cac9e638b6d9797923865872944b646a060a634b9280e3a23fb63375e7bbdd9ab07fd379ab6a67e2312bbc112195fa358808080808080808080808080808080").to_vec(),
hex!("f9030820b9030402f90300018301d6e2b9010000000000000800000000000020040008000000000000000000000000400000008000000000000000000000000000000000000000000000000000000000042010000000001000000000000000000000000000000000040000000000000000000000000000000000000000000000008000000000000000002000000000000000000000000200000000000000200000000000100000000040000001000200008000000000000200000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000f901f5f87a942ffa5ecdbe006d30397c7636d3e015eee251369ff842a0c965575a00553e094ca7c5d14f02e107c258dda06867cbf9e0e69f80e71bbcc1a000000000000000000000000000000000000000000000000000000000000003e8a000000000000000000000000000000000000000000000000000000000000003e8f9011c94ee9170abfbf9421ad6dd07f6bdec9d89f2b581e0f863a01b11dcf133cc240f682dab2d3a8e4cd35c5da8c9cf99adac4336f8512584c5ada000000000000000000000000000000000000000000000000000000000000003e8a00000000000000000000000000000000000000000000000000000000000000001b8a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004b000f000000000000000100d184c103f7acc340847eee82a0b909e3358bc28d440edffa1352b13227e8ee646f3ea37456dec701345772617070656420457468657210574554481235003511000000000000000000000000000000000000000000f858948cf6147918a5cbb672703f879f385036f8793a24e1a01449abf21e49fd025f33495e77f7b1461caefdd3d4bb646424a3f445c4576a5ba0000000000000000000000000440edffa1352b13227e8ee646f3ea37456dec701").to_vec(),
]),
},
},
}
}
69 changes: 69 additions & 0 deletions parachain/pallets/inbound-queue/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
mod fixtures;

// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
use super::*;

use crate::Pallet as InboundQueue;
use frame_benchmarking::v2::*;
use frame_system::RawOrigin;

#[benchmarks]
mod benchmarks {
use super::*;
use crate::benchmarking::fixtures::make_create_message;
use hex_literal::hex;

const OUTBOUND_QUEUE_ADDRESS: [u8; 20] = hex!["ee9170abfbf9421ad6dd07f6bdec9d89f2b581e0"];

#[benchmark]
fn submit() -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();

let create_message = make_create_message();

T::Helper::initialize_storage(
create_message.message.proof.block_hash,
create_message.execution_header,
);

<AllowList<T>>::put(create_allowlist::<T>());

let dest_para: ParaId = 1000u32.into();
let sovereign_account = dest_para.into_account_truncating();

// So that the receiving account exists
let _ = T::Token::mint_into(&caller, T::Token::minimum_balance().into());
// Fund the sovereign account (parachain sovereign account) so it can transfer a reward
// fee to the caller account
let _ = T::Token::mint_into(&sovereign_account, 10000u32.into());

#[block]
{
let _ = InboundQueue::<T>::submit(
RawOrigin::Signed(caller.clone()).into(),
create_message.message,
)?;
}

Ok(())
}

impl_benchmark_test_suite!(
InboundQueue,
crate::test::new_tester(crate::H160::default()),
crate::test::Test
);

fn create_allowlist<T>() -> BoundedBTreeSet<H160, T::AllowListLength>
where
T: Config,
{
let allowlist: BoundedBTreeSet<H160, T::AllowListLength> =
BTreeSet::from_iter(vec![OUTBOUND_QUEUE_ADDRESS.into()].into_iter())
.try_into()
.expect("exceeded bound");

allowlist
}
}
16 changes: 15 additions & 1 deletion parachain/pallets/inbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ mod envelope;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

#[cfg(feature = "runtime-benchmarks")]
use snowbridge_beacon_primitives::CompactExecutionHeader;
#[cfg(feature = "runtime-benchmarks")]
use snowbridge_ethereum::H256;

pub mod weights;

#[cfg(test)]
Expand Down Expand Up @@ -57,9 +62,15 @@ pub mod pallet {
use frame_support::{pallet_prelude::*, traits::tokens::Preservation};
use frame_system::pallet_prelude::*;
use xcm::v3::SendXcm;

#[pallet::pallet]
pub struct Pallet<T>(_);

#[cfg(feature = "runtime-benchmarks")]
pub trait BenchmarkHelper<T> {
fn initialize_storage(block_hash: H256, header: CompactExecutionHeader);
}

#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
Expand All @@ -75,6 +86,9 @@ pub mod pallet {
type WeightInfo: WeightInfo;

type AllowListLength: Get<u32>;

#[cfg(feature = "runtime-benchmarks")]
type Helper: BenchmarkHelper<Self>;
}

#[pallet::hooks]
Expand Down Expand Up @@ -135,7 +149,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight({100_000_000})]
#[pallet::weight(T::WeightInfo::submit())]
pub fn submit(origin: OriginFor<T>, message: Message) -> DispatchResult {
let who = ensure_signed(origin)?;
// submit message to verifier for verification
Expand Down
39 changes: 39 additions & 0 deletions parachain/pallets/inbound-queue/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use sp_runtime::{
};
use sp_std::convert::From;

use snowbridge_beacon_primitives::{Fork, ForkVersions};
use snowbridge_core::{Message, Proof};
use snowbridge_ethereum::Log;

Expand All @@ -35,6 +36,7 @@ frame_support::construct_runtime!(
{
System: frame_system::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
EthereumBeaconClient: snowbridge_ethereum_beacon_client::{Pallet, Call, Storage, Event<T>},
InboundQueue: inbound_queue::{Pallet, Call, Storage, Event<T>},
}
);
Expand Down Expand Up @@ -89,6 +91,35 @@ impl pallet_balances::Config for Test {
type MaxHolds = ();
}

parameter_types! {
pub const ExecutionHeadersPruneThreshold: u32 = 10;
pub const ChainForkVersions: ForkVersions = ForkVersions{
genesis: Fork {
version: [0, 0, 0, 1], // 0x00000001
epoch: 0,
},
altair: Fork {
version: [1, 0, 0, 1], // 0x01000001
epoch: 0,
},
bellatrix: Fork {
version: [2, 0, 0, 1], // 0x02000001
epoch: 0,
},
capella: Fork {
version: [3, 0, 0, 1], // 0x03000001
epoch: 0,
},
};
}

impl snowbridge_ethereum_beacon_client::Config for Test {
type RuntimeEvent = RuntimeEvent;
type ForkVersions = ChainForkVersions;
type MaxExecutionHeadersToKeep = ExecutionHeadersPruneThreshold;
type WeightInfo = ();
}

// Mock verifier
pub struct MockVerifier;

Expand All @@ -103,6 +134,12 @@ parameter_types! {
pub const EthereumNetwork: xcm::v3::NetworkId = xcm::v3::NetworkId::Ethereum { chain_id: 15};
}

#[cfg(feature = "runtime-benchmarks")]
impl<T: snowbridge_ethereum_beacon_client::Config> BenchmarkHelper<T> for Test {
// not implemented since the MockVerifier is used for tests
fn initialize_storage(_: H256, _: CompactExecutionHeader) {}
}

impl inbound_queue::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Verifier = MockVerifier;
Expand All @@ -111,6 +148,8 @@ impl inbound_queue::Config for Test {
type XcmSender = ();
type WeightInfo = ();
type AllowListLength = ConstU32<2>;
#[cfg(feature = "runtime-benchmarks")]
type Helper = Test;
}

fn last_events(n: usize) -> Vec<RuntimeEvent> {
Expand Down
30 changes: 28 additions & 2 deletions parachain/pallets/inbound-queue/src/weights.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
pub trait WeightInfo {}
//! Autogenerated weights for `snowbridge_inbound_queue`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-07-14, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `Claras-MacBook-Pro-2.local`, CPU: `<UNKNOWN>`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("bridge-hub-rococo-dev"), DB CACHE: 1024

impl WeightInfo for () {}
#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use sp_std::marker::PhantomData;

/// Weight functions needed for ethereum_beacon_client.
pub trait WeightInfo {
fn submit() -> Weight;
}

// For backwards compatibility and tests
impl WeightInfo for () {
fn submit() -> Weight {
Weight::from_parts(125_000_000, 0)
.saturating_add(Weight::from_parts(0, 4042))
.saturating_add(RocksDbWeight::get().reads(9))
.saturating_add(RocksDbWeight::get().writes(4))
}
}
2 changes: 2 additions & 0 deletions parachain/primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false }
snowbridge-beacon-primitives = { path = "../../primitives/beacon", default-features = false }

[dev-dependencies]
hex = { package = "rustc-hex", version = "2.1.0", default-features = false }
Expand All @@ -35,6 +36,7 @@ std = [
"sp-core/std",
"sp-runtime/std",
"snowbridge-ethereum/std",
"snowbridge-beacon-primitives/std",
"xcm/std"
]
runtime-benchmarks = []

0 comments on commit a99fc50

Please sign in to comment.