Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Benchmarks for incentivized channel & constant time verify (parityt…
Browse files Browse the repository at this point in the history
…ech#328)

* Benchmarks for incentivized channel

* Make Verifier::verify constant time

* Add WeightInfo trait, generate weights

* Merge and update benchmarks
  • Loading branch information
Rizziepit authored Apr 13, 2021
1 parent a646b82 commit 1978dfb
Show file tree
Hide file tree
Showing 20 changed files with 443 additions and 80 deletions.
1 change: 1 addition & 0 deletions parachain/Cargo.lock

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

26 changes: 3 additions & 23 deletions parachain/pallets/basic-channel/src/inbound/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@ fn assert_last_event<T: Config>(system_event: <T as frame_system::Config>::Event
assert_eq!(event, &system_event);
}

fn make_header_chain(parent: Header, length: u32) -> Vec<Header> {
let mut chain = vec![parent.clone()];
(0..length).fold(parent, |ancestor, _| {
let mut header: Header = Default::default();
header.parent_hash = ancestor.compute_hash();
header.number = ancestor.number.saturating_add(1);
header.difficulty = 1.into();
chain.push(header.clone());
header
});

chain
}

// This collection of benchmarks should include a benchmark for each
// call dispatched by the channel, i.e. each "app" pallet function
// that can be invoked by MessageDispatch. The most expensive call
Expand All @@ -48,8 +34,6 @@ benchmarks! {
// * `submit` dispatches the DotApp::unlock call
// * `unlock` call successfully unlocks DOT
submit {
let h in 0 .. 20;

let caller: T::AccountId = whitelisted_caller();
let (header, message) = dot_unlock_data();
let envelope: envelope::Envelope = rlp::decode::<Log>(&message.data)
Expand All @@ -59,7 +43,7 @@ benchmarks! {
SourceChannel::put(envelope.channel);

T::Verifier::initialize_storage(
make_header_chain(header, h),
vec![header],
0.into(),
0, // forces all headers to be finalized
)?;
Expand All @@ -76,8 +60,6 @@ benchmarks! {

#[extra]
submit_eth_mint {
let h in 0 .. 20;

let caller: T::AccountId = whitelisted_caller();
let (header, message) = eth_mint_data();
let envelope: envelope::Envelope = rlp::decode::<Log>(&message.data)
Expand All @@ -87,7 +69,7 @@ benchmarks! {
SourceChannel::put(envelope.channel);

T::Verifier::initialize_storage(
make_header_chain(header, h),
vec![header],
0.into(),
0, // forces all headers to be finalized
)?;
Expand All @@ -104,8 +86,6 @@ benchmarks! {

#[extra]
submit_erc20_mint {
let h in 0 .. 20;

let caller: T::AccountId = whitelisted_caller();
let (header, message) = erc20_mint_data();
let envelope: envelope::Envelope = rlp::decode::<Log>(&message.data)
Expand All @@ -115,7 +95,7 @@ benchmarks! {
SourceChannel::put(envelope.channel);

T::Verifier::initialize_storage(
make_header_chain(header, h),
vec![header],
0.into(),
0, // forces all headers to be finalized
)?;
Expand Down
15 changes: 14 additions & 1 deletion parachain/pallets/basic-channel/src/inbound/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use frame_support::{
decl_error, decl_event, decl_module, decl_storage,
dispatch::DispatchResult,
weights::Weight,
};
use frame_system::{self as system, ensure_signed};
use sp_core::H160;
Expand All @@ -20,6 +21,15 @@ mod test;

mod envelope;

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

impl WeightInfo for () {
fn submit() -> Weight { 0 }
}

pub trait Config: system::Config {
type Event: From<Event> + Into<<Self as system::Config>::Event>;

Expand All @@ -28,6 +38,9 @@ pub trait Config: system::Config {

/// Verifier module for message verification.
type MessageDispatch: MessageDispatch<Self, MessageId>;

/// Weight information for extrinsics in this pallet
type WeightInfo: WeightInfo;
}

decl_storage! {
Expand Down Expand Up @@ -61,7 +74,7 @@ decl_module! {

fn deposit_event() = default;

#[weight = 0]
#[weight = T::WeightInfo::submit()]
pub fn submit(origin, message: Message) -> DispatchResult {
ensure_signed(origin)?;
// submit message to verifier for verification
Expand Down
1 change: 1 addition & 0 deletions parachain/pallets/basic-channel/src/inbound/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl basic_inbound_channel::Config for Test {
type Event = Event;
type Verifier = MockVerifier;
type MessageDispatch = MockMessageDispatch;
type WeightInfo = ();
}

pub fn new_tester(source_channel: H160) -> sp_io::TestExternalities {
Expand Down
8 changes: 8 additions & 0 deletions parachain/pallets/incentivized-channel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
serde = { version = "1.0.101", optional = true }
codec = { version = "2.0.0", package = "parity-scale-codec", default-features = false, features = ["derive"] }
hex-literal = { version = "0.3.1", optional = true }
rlp = { version = "0.5", default-features = false, optional = true }

frame-benchmarking = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false, optional = true }
frame-support = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
Expand Down Expand Up @@ -47,4 +50,9 @@ std = [
]
runtime-benchmarks = [
"artemis-core/runtime-benchmarks",
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"hex-literal",
"rlp",
]
172 changes: 172 additions & 0 deletions parachain/pallets/incentivized-channel/src/inbound/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
//! IncentivizedInboundChannel pallet benchmarking

#![cfg(feature = "runtime-benchmarks")]

use super::*;

use frame_system::{RawOrigin, self, EventRecord};
use frame_benchmarking::{benchmarks, whitelisted_caller, impl_benchmark_test_suite};
use hex_literal::hex;
use sp_std::convert::TryInto;

use artemis_core::{ChannelId, Message, MessageId, Proof};
use artemis_ethereum::{Log, Header};

#[allow(unused_imports)]
use crate::inbound::Module as IncentivizedInboundChannel;

fn assert_last_event<T: Config>(system_event: <T as frame_system::Config>::Event) {
let events = frame_system::Pallet::<T>::events();
// compare to the last event record
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}

// This collection of benchmarks should include a benchmark for each
// call dispatched by the channel, i.e. each "app" pallet function
// that can be invoked by MessageDispatch. The most expensive call
// should be used in the `submit` benchmark.
//
// We rely on configuration via chain spec of the app pallets because
// we don't have access to their storage here.
benchmarks! {
// Benchmark `submit` extrinsic under worst case conditions:
// * `submit` dispatches the DotApp::unlock call
// * `unlock` call successfully unlocks DOT
submit {
let caller: T::AccountId = whitelisted_caller();
let (header, message) = dot_unlock_data();
let envelope: envelope::Envelope<T> = rlp::decode::<Log>(&message.data)
.map(|log| log.try_into().unwrap())
.unwrap();
Nonce::put(envelope.nonce - 1);
SourceChannel::put(envelope.channel);

T::Verifier::initialize_storage(
vec![header],
0.into(),
0, // forces all headers to be finalized
)?;

}: _(RawOrigin::Signed(caller.clone()), message)
verify {
assert_eq!(envelope.nonce, Nonce::get());

let message_id = MessageId::new(ChannelId::Incentivized, envelope.nonce);
if let Some(event) = T::MessageDispatch::successful_dispatch_event(message_id) {
assert_last_event::<T>(event);
}
}

#[extra]
submit_eth_mint {
let caller: T::AccountId = whitelisted_caller();
let (header, message) = eth_mint_data();
let envelope: envelope::Envelope<T> = rlp::decode::<Log>(&message.data)
.map(|log| log.try_into().unwrap())
.unwrap();
Nonce::put(envelope.nonce - 1);
SourceChannel::put(envelope.channel);

T::Verifier::initialize_storage(
vec![header],
0.into(),
0, // forces all headers to be finalized
)?;

}: submit(RawOrigin::Signed(caller.clone()), message)
verify {
assert_eq!(envelope.nonce, Nonce::get());

let message_id = MessageId::new(ChannelId::Incentivized, envelope.nonce);
if let Some(event) = T::MessageDispatch::successful_dispatch_event(message_id) {
assert_last_event::<T>(event);
}
}

// TODO: add back ECR20 mint benchmark
}

// ETH mint
// Channel = 0xeda338e4dc46038493b885327842fd3e301cab39
// Nonce = 2
// Source = 0x774667629726ec1fabebcec0d9139bd1c8f72a23
fn eth_mint_data() -> (Header, Message) {
(
Header {
parent_hash: hex!("3f136282153539e5b4006c05d0c6ab033786d5793c7cc3b784ca3b274918181e").into(),
timestamp: 1618295862u64.into(),
number: 3584u64.into(),
author: hex!("0000000000000000000000000000000000000000").into(),
transactions_root: hex!("84ed6b4ad707994d7095eff6f5458d2c6e351fcab00669b084705229faa63840").into(),
ommers_hash: hex!("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347").into(),
extra_data: hex!("").into(),
state_root: hex!("ab5ce23d0914c05c74504cb797349fa8fca1b9b286f2f1ab11f0adb7f8c2d12c").into(),
receipts_root: hex!("ac1dbc12173e495453bf5277fb1cc507a092218adcb795e7743308615ed62c89").into(),
logs_bloom: (&hex!("00000000000040000000000000000200000000000000400010000000010080000000000000000000000000001000000010000000000000000000000000000080000000000000000400000008000000000000000000008000000000000000000000000000020000000000000000000800000001000400000000000010000000000000020000000000000000000400000000000000000000000000040000000000000000000004000000000000000000000220000000000001000000000200080000000002000000000000000000000000000000000000000000000000000020402000000000000000000000000000000000000000000000000000000000000000")).into(),
gas_used: 97210u64.into(),
gas_limit: 6721975u64.into(),
difficulty: 0u64.into(),
seal: vec![
hex!("a00000000000000000000000000000000000000000000000000000000000000000").to_vec(),
hex!("880000000000000000").to_vec(),
],
},
Message {
data: hex!("f9013a94eda338e4dc46038493b885327842fd3e301cab39e1a05e9ae1d7c484f74d554a503aa825e823725531d97e784dd9b1aacdb58d1f7076b90100000000000000000000000000774667629726ec1fabebcec0d9139bd1c8f72a2300000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000570c0189b4ab1ef20763630df9743acf155865600daff200d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0000c16ff2862300000000000000000000000000000000000000000000000000000000000000000000").to_vec(),
proof: Proof {
block_hash: hex!("fbd6740f975d184468373c65c1278051403867361c03ab8c245c5ff42e53c9e2").into(),
tx_index: 0,
data: (
vec![hex!("ac1dbc12173e495453bf5277fb1cc507a092218adcb795e7743308615ed62c89").to_vec()],
vec![hex!("f904a8822080b904a2f9049f0183017bbab9010000000000000040000000000000000200000000000000400010000000010080000000000000000000000000001000000010000000000000000000000000000080000000000000000400000008000000000000000000008000000000000000000000000000020000000000000000000800000001000400000000000010000000000000020000000000000000000400000000000000000000000000040000000000000000000004000000000000000000000220000000000001000000000200080000000002000000000000000000000000000000000000000000000000000020402000000000000000000000000000000000000000000000000000000000000000f90394f89994774667629726ec1fabebcec0d9139bd1c8f72a23e1a0caae0f5e72020d428da73a237d1f9bf162e158dda6d4908769b8b60c095b01f4b86000000000000000000000000089b4ab1ef20763630df9743acf155865600daff2d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d000000000000000000000000000000000000000000000000002386f26fc10000f9011c94672a95c8928c8450b594186cf7954ec269626a2df863a0a78a9be3a7b862d26933ad85fb11d80ef66b8f972d7cbba06621d583943a4098a0000000000000000000000000b1185ede04202fe62d38f5db72f71e38ff3e8305a000000000000000000000000089b4ab1ef20763630df9743acf155865600daff2b8a00000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f89b94672a95c8928c8450b594186cf7954ec269626a2df863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa000000000000000000000000089b4ab1ef20763630df9743acf155865600daff2a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000de0b6b3a7640000f9013a94eda338e4dc46038493b885327842fd3e301cab39e1a05e9ae1d7c484f74d554a503aa825e823725531d97e784dd9b1aacdb58d1f7076b90100000000000000000000000000774667629726ec1fabebcec0d9139bd1c8f72a2300000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000570c0189b4ab1ef20763630df9743acf155865600daff200d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0000c16ff2862300000000000000000000000000000000000000000000000000000000000000000000").to_vec()],
),
},
},
)
}

// DOT unlock
// Channel = 0xeda338e4dc46038493b885327842fd3e301cab39
// Nonce = 1
// Source = 0xb1185ede04202fe62d38f5db72f71e38ff3e8305
fn dot_unlock_data() -> (Header, Message) {
(
Header {
parent_hash: hex!("6e54738f4e58949361383e11d06cfe5b72c87410881230fae3289f1de8487927").into(),
timestamp: 1618291501u64.into(),
number: 2496u64.into(),
author: hex!("0000000000000000000000000000000000000000").into(),
transactions_root: hex!("9c53bbde6f3dfe2afd272cfd64a8639db23588d3ec63406b5225b38fa79a0490").into(),
ommers_hash: hex!("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347").into(),
extra_data: hex!("").into(),
state_root: hex!("b262afa00f5d920137d59f2061cb2075a9cece1d53775ba6f1b38f88423f6ff7").into(),
receipts_root: hex!("f19e53a299860306c5ad8201b28f78d505ea4a46f08739f2f58def37652cd386").into(),
logs_bloom: (&hex!("00000000000040000000000000000200000000000000000010000000000080000000000000000000000000001000000010000000000000000000000000000000000000000000000400000008000000000000000000008000000000000000000000000000020000000000000000000800000001000400000000000010000000000000020000000000000000000400000000000000000000000000040000000000000000000004000000000000000000000200000000000001000000000200000000000002000000000000000000000000000000000000000000000000000020400000000000000000000000000000000000000000000000000000000000000000")).into(),
gas_used: 107966u64.into(),
gas_limit: 6721975u64.into(),
difficulty: 0u64.into(),
seal: vec![
hex!("a00000000000000000000000000000000000000000000000000000000000000000").to_vec(),
hex!("880000000000000000").to_vec(),
],
},
Message {
data: hex!("f9013a94eda338e4dc46038493b885327842fd3e301cab39e1a05e9ae1d7c484f74d554a503aa825e823725531d97e784dd9b1aacdb58d1f7076b90100000000000000000000000000b1185ede04202fe62d38f5db72f71e38ff3e830500000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000570e0189b4ab1ef20763630df9743acf155865600daff200d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d000064a7b3b6e00d000000000000000000000000000000000000000000000000000000000000000000").to_vec(),
proof: Proof {
block_hash: hex!("2e2ac53ae0dcd35d23e004d105f21a07cfb841ee7f86b5edd957067684d61f85").into(),
tx_index: 0,
data: (
vec![hex!("f19e53a299860306c5ad8201b28f78d505ea4a46f08739f2f58def37652cd386").to_vec()],
vec![hex!("f905e9822080b905e3f905e0018301a5beb9010000000000000040000000000000000200000000000000000010000000000080000000000000000000000000001000000010000000000000000000000000000000000000000000000400000008000000000000000000008000000000000000000000000000020000000000000000000800000001000400000000000010000000000000020000000000000000000400000000000000000000000000040000000000000000000004000000000000000000000200000000000001000000000200000000000002000000000000000000000000000000000000000000000000000020400000000000000000000000000000000000000000000000000000000000000000f904d5f9013c94672a95c8928c8450b594186cf7954ec269626a2df863a0a78a9be3a7b862d26933ad85fb11d80ef66b8f972d7cbba06621d583943a4098a0000000000000000000000000b1185ede04202fe62d38f5db72f71e38ff3e8305a000000000000000000000000089b4ab1ef20763630df9743acf155865600daff2b8c00000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0000000000000000000000000000000000000000000000000000000000000000f89b94672a95c8928c8450b594186cf7954ec269626a2df863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa000000000000000000000000089b4ab1ef20763630df9743acf155865600daff2a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000de0b6b3a7640000f9011c94672a95c8928c8450b594186cf7954ec269626a2df863a0a78a9be3a7b862d26933ad85fb11d80ef66b8f972d7cbba06621d583943a4098a0000000000000000000000000b1185ede04202fe62d38f5db72f71e38ff3e8305a000000000000000000000000089b4ab1ef20763630df9743acf155865600daff2b8a00000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f89b94672a95c8928c8450b594186cf7954ec269626a2df863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa000000000000000000000000089b4ab1ef20763630df9743acf155865600daff2a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000de0b6b3a7640000f9013a94eda338e4dc46038493b885327842fd3e301cab39e1a05e9ae1d7c484f74d554a503aa825e823725531d97e784dd9b1aacdb58d1f7076b90100000000000000000000000000b1185ede04202fe62d38f5db72f71e38ff3e830500000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000570e0189b4ab1ef20763630df9743acf155865600daff200d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d000064a7b3b6e00d000000000000000000000000000000000000000000000000000000000000000000").to_vec()],
),
},
},
)
}

impl_benchmark_test_suite!(
IncentivizedInboundChannel,
crate::inbound::test::new_tester(Default::default()),
crate::inbound::test::Test,
);
18 changes: 17 additions & 1 deletion parachain/pallets/incentivized-channel/src/inbound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use frame_support::{
traits::{Currency, Get, ExistenceRequirement::KeepAlive, WithdrawReasons, Imbalance},
storage::StorageValue,
log,
weights::Weight,
};
use frame_system::{self as system, ensure_signed};
use sp_core::{U256, H160};
Expand All @@ -18,6 +19,8 @@ use envelope::Envelope;

use sp_runtime::{Perbill, traits::{Zero, Convert}};

mod benchmarking;

#[cfg(test)]
mod test;

Expand All @@ -26,6 +29,16 @@ mod envelope;
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type PositiveImbalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::PositiveImbalance;

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

impl WeightInfo for () {
fn submit() -> Weight { 0 }
}


pub trait Config: system::Config {
type Event: From<Event> + Into<<Self as system::Config>::Event>;

Expand All @@ -44,6 +57,9 @@ pub trait Config: system::Config {
type TreasuryAccount: Get<Self::AccountId>;

type FeeConverter: Convert<U256, BalanceOf<Self>>;

/// Weight information for extrinsics in this pallet
type WeightInfo: WeightInfo;
}

decl_storage! {
Expand Down Expand Up @@ -79,7 +95,7 @@ decl_module! {

fn deposit_event() = default;

#[weight = 0]
#[weight = T::WeightInfo::submit()]
pub fn submit(origin, message: Message) -> DispatchResult {
let relayer = ensure_signed(origin)?;
// submit message to verifier for verification
Expand Down
1 change: 1 addition & 0 deletions parachain/pallets/incentivized-channel/src/inbound/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ impl incentivized_inbound_channel::Config for Test {
type SourceAccount = SourceAccount;
type TreasuryAccount = TreasuryAccount;
type FeeConverter = FeeConverter<Self>;
type WeightInfo = ();
}

pub fn new_tester(source_channel: H160) -> sp_io::TestExternalities {
Expand Down
Loading

0 comments on commit 1978dfb

Please sign in to comment.