From 37d067c0e0e17afe501d615e4bead13bb6720264 Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Mon, 7 Jun 2021 17:56:13 +0300 Subject: [PATCH] Update submit finality proof weight formula (#981) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * updated weight formula for submit_finality_proof * remove common prefix traces * update docs * single benchmark * Re-generate weights. * Update delivery transaction limits Co-authored-by: Tomasz Drwięga Co-authored-by: Hernando Castano --- bridges/modules/grandpa/src/benchmarking.rs | 231 +++--------------- bridges/modules/grandpa/src/lib.rs | 5 +- bridges/modules/grandpa/src/weights.rs | 62 +---- bridges/modules/messages/src/weights.rs | 90 +++---- .../header-chain/tests/justification.rs | 10 +- bridges/primitives/test-utils/Cargo.toml | 4 +- bridges/primitives/test-utils/src/keyring.rs | 2 +- bridges/primitives/test-utils/src/lib.rs | 27 +- .../relays/bin-substrate/src/messages_lane.rs | 2 +- 9 files changed, 113 insertions(+), 320 deletions(-) diff --git a/bridges/modules/grandpa/src/benchmarking.rs b/bridges/modules/grandpa/src/benchmarking.rs index b7294e9180..bc027e86a4 100644 --- a/bridges/modules/grandpa/src/benchmarking.rs +++ b/bridges/modules/grandpa/src/benchmarking.rs @@ -38,24 +38,18 @@ //! //! Note that the worst case scenario here would be a justification where each validator has it's //! own fork which is `SESSION_LENGTH` blocks long. -//! -//! As far as benchmarking results go, the only benchmark that should be used in -//! `pallet-bridge-grandpa` to annotate weights is the `submit_finality_proof` one. The others are -//! looking at the effects of specific code paths and do not actually reflect the overall worst case -//! scenario. use crate::*; use bp_test_utils::{ - accounts, authority_list, make_justification_for_header, test_keyring, JustificationGeneratorParams, ALICE, - TEST_GRANDPA_ROUND, TEST_GRANDPA_SET_ID, + accounts, make_justification_for_header, JustificationGeneratorParams, TEST_GRANDPA_ROUND, TEST_GRANDPA_SET_ID, }; use frame_benchmarking::{benchmarks_instance_pallet, whitelisted_caller}; use frame_support::traits::Get; use frame_system::RawOrigin; use sp_finality_grandpa::AuthorityId; use sp_runtime::traits::Zero; -use sp_std::{vec, vec::Vec}; +use sp_std::vec::Vec; // The maximum number of vote ancestries to include in a justification. // @@ -75,81 +69,46 @@ fn header_number, I: 'static, N: From>() -> N { (T::HeadersToKeep::get() + 1).into() } +/// Prepare header and its justification to submit using `submit_finality_proof`. +fn prepare_benchmark_data, I: 'static>( + precommits: u32, + ancestors: u32, +) -> (BridgedHeader, GrandpaJustification>) { + let authority_list = accounts(precommits as u16) + .iter() + .map(|id| (AuthorityId::from(*id), 1)) + .collect::>(); + + let init_data = InitializationData { + header: bp_test_utils::test_header(Zero::zero()), + authority_list, + set_id: TEST_GRANDPA_SET_ID, + is_halted: false, + }; + + bootstrap_bridge::(init_data); + + let header: BridgedHeader = bp_test_utils::test_header(header_number::()); + let params = JustificationGeneratorParams { + header: header.clone(), + round: TEST_GRANDPA_ROUND, + set_id: TEST_GRANDPA_SET_ID, + authorities: accounts(precommits as u16).iter().map(|k| (*k, 1)).collect::>(), + ancestors, + forks: 1, + }; + let justification = make_justification_for_header(params); + (header, justification) +} + benchmarks_instance_pallet! { // This is the "gold standard" benchmark for this extrinsic, and it's what should be used to // annotate the weight in the pallet. - // - // The other benchmarks related to `submit_finality_proof` are looking at the effect of specific - // parameters and are there mostly for seeing how specific codepaths behave. submit_finality_proof { - let v in 1..MAX_VOTE_ANCESTRIES; let p in 1..MAX_VALIDATOR_SET_SIZE; - - let caller: T::AccountId = whitelisted_caller(); - - let authority_list = accounts(p as u16) - .iter() - .map(|id| (AuthorityId::from(*id), 1)) - .collect::>(); - - let init_data = InitializationData { - header: bp_test_utils::test_header(Zero::zero()), - authority_list, - set_id: TEST_GRANDPA_SET_ID, - is_halted: false, - }; - - bootstrap_bridge::(init_data); - - let header: BridgedHeader = bp_test_utils::test_header(header_number::()); - let params = JustificationGeneratorParams { - header: header.clone(), - round: TEST_GRANDPA_ROUND, - set_id: TEST_GRANDPA_SET_ID, - authorities: accounts(p as u16).iter().map(|k| (*k, 1)).collect::>(), - votes: v, - forks: 1, - }; - - let justification = make_justification_for_header(params); - - }: _(RawOrigin::Signed(caller), header, justification) - verify { - let header: BridgedHeader = bp_test_utils::test_header(header_number::()); - let expected_hash = header.hash(); - - assert_eq!(>::get(), expected_hash); - assert!(>::contains_key(expected_hash)); - } - - // What we want to check here is the effect of vote ancestries on justification verification - // do this by varying the number of headers between `finality_target` and `header_of_chain`. - submit_finality_proof_on_single_fork { let v in 1..MAX_VOTE_ANCESTRIES; - let caller: T::AccountId = whitelisted_caller(); - - let init_data = InitializationData { - header: bp_test_utils::test_header(Zero::zero()), - authority_list: authority_list(), - set_id: TEST_GRANDPA_SET_ID, - is_halted: false, - }; - - bootstrap_bridge::(init_data); - let header: BridgedHeader = bp_test_utils::test_header(header_number::()); - - let params = JustificationGeneratorParams { - header: header.clone(), - round: TEST_GRANDPA_ROUND, - set_id: TEST_GRANDPA_SET_ID, - authorities: test_keyring(), - votes: v, - forks: 1, - }; - - let justification = make_justification_for_header(params); - + let (header, justification) = prepare_benchmark_data::(p, v); }: submit_finality_proof(RawOrigin::Signed(caller), header, justification) verify { let header: BridgedHeader = bp_test_utils::test_header(header_number::()); @@ -158,124 +117,4 @@ benchmarks_instance_pallet! { assert_eq!(>::get(), expected_hash); assert!(>::contains_key(expected_hash)); } - - // What we want to check here is the effect of many pre-commits on justification verification. - // We do this by creating many forks, whose head will be used as a signed pre-commit in the - // final justification. - submit_finality_proof_on_many_forks { - let p in 1..MAX_VALIDATOR_SET_SIZE; - - let caller: T::AccountId = whitelisted_caller(); - - let authority_list = accounts(p as u16) - .iter() - .map(|id| (AuthorityId::from(*id), 1)) - .collect::>(); - - let init_data = InitializationData { - header: bp_test_utils::test_header(Zero::zero()), - authority_list, - set_id: TEST_GRANDPA_SET_ID, - is_halted: false, - }; - - bootstrap_bridge::(init_data); - let header: BridgedHeader = bp_test_utils::test_header(header_number::()); - - let params = JustificationGeneratorParams { - header: header.clone(), - round: TEST_GRANDPA_ROUND, - set_id: TEST_GRANDPA_SET_ID, - authorities: accounts(p as u16).iter().map(|k| (*k, 1)).collect::>(), - votes: p, - forks: p, - }; - - let justification = make_justification_for_header(params); - - }: submit_finality_proof(RawOrigin::Signed(caller), header, justification) - verify { - let header: BridgedHeader = bp_test_utils::test_header(header_number::()); - let expected_hash = header.hash(); - - assert_eq!(>::get(), expected_hash); - assert!(>::contains_key(expected_hash)); - } - - // Here we want to find out the overheaded of looking through consensus digests found in a - // header. As the number of logs in a header grows, how much more work do we require to look - // through them? - // - // Note that this should be the same for looking through scheduled changes and forces changes, - // which is why we only have one benchmark for this. - find_scheduled_change { - // Not really sure what a good bound for this is. - let n in 1..1000; - - let mut logs = vec![]; - for i in 0..n { - // We chose a non-consensus log on purpose since that way we have to look through all - // the logs in the header - logs.push(sp_runtime::DigestItem::Other(vec![])); - } - - let mut header: BridgedHeader = bp_test_utils::test_header(Zero::zero()); - let digest = header.digest_mut(); - *digest = sp_runtime::Digest { - logs, - }; - - }: { - crate::find_scheduled_change(&header) - } - - // What we want to check here is how long it takes to read and write the authority set tracked - // by the pallet as the number of authorities grows. - read_write_authority_sets { - // The current max target number of validators on Polkadot/Kusama - let n in 1..1000; - - let mut authorities = vec![]; - for i in 0..n { - authorities.push((ALICE, 1)); - } - - let authority_set = bp_header_chain::AuthoritySet { - authorities: authorities.iter().map(|(id, w)| (AuthorityId::from(*id), *w)).collect(), - set_id: 0 - }; - - >::put(&authority_set); - - }: { - let authority_set = >::get(); - >::put(&authority_set); - } -} - -#[cfg(test)] -mod tests { - use super::*; - use frame_support::assert_ok; - - #[test] - fn finality_proof_is_valid() { - mock::run_test(|| { - assert_ok!(test_benchmark_submit_finality_proof::()); - }); - } - - #[test] - fn single_fork_finality_proof_is_valid() { - mock::run_test(|| { - assert_ok!(test_benchmark_submit_finality_proof_on_single_fork::()); - }); - } - - #[test] - fn multi_fork_finality_proof_is_valid() { - mock::run_test(|| { - assert_ok!(test_benchmark_submit_finality_proof_on_many_forks::()); - }); - } } diff --git a/bridges/modules/grandpa/src/lib.rs b/bridges/modules/grandpa/src/lib.rs index f89950772a..1d77bf9861 100644 --- a/bridges/modules/grandpa/src/lib.rs +++ b/bridges/modules/grandpa/src/lib.rs @@ -46,6 +46,7 @@ use frame_support::{ensure, fail}; use frame_system::{ensure_signed, RawOrigin}; use sp_finality_grandpa::{ConsensusLog, GRANDPA_ENGINE_ID}; use sp_runtime::traits::{BadOrigin, Header as HeaderT, Zero}; +use sp_std::convert::TryInto; #[cfg(test)] mod mock; @@ -124,8 +125,8 @@ pub mod pallet { /// If successful in verification, it will write the target header to the underlying storage /// pallet. #[pallet::weight(T::WeightInfo::submit_finality_proof( - justification.votes_ancestries.len() as u32, - justification.commit.precommits.len() as u32, + justification.commit.precommits.len().try_into().unwrap_or(u32::MAX), + justification.votes_ancestries.len().try_into().unwrap_or(u32::MAX), ))] pub fn submit_finality_proof( origin: OriginFor, diff --git a/bridges/modules/grandpa/src/weights.rs b/bridges/modules/grandpa/src/weights.rs index 9e7c2ebc08..18d88049f1 100644 --- a/bridges/modules/grandpa/src/weights.rs +++ b/bridges/modules/grandpa/src/weights.rs @@ -17,7 +17,7 @@ //! Autogenerated weights for pallet_bridge_grandpa //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-04-21, STEPS: [50, ], REPEAT: 20 +//! DATE: 2021-06-03, STEPS: [50, ], REPEAT: 20 //! LOW RANGE: [], HIGH RANGE: [] //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled //! CHAIN: Some("dev"), DB CACHE: 128 @@ -48,74 +48,28 @@ use sp_std::marker::PhantomData; /// Weight functions needed for pallet_bridge_grandpa. pub trait WeightInfo { - fn submit_finality_proof(v: u32, p: u32) -> Weight; - fn submit_finality_proof_on_single_fork(v: u32) -> Weight; - fn submit_finality_proof_on_many_forks(p: u32) -> Weight; - fn find_scheduled_change(n: u32) -> Weight; - fn read_write_authority_sets(n: u32) -> Weight; + 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 { - fn submit_finality_proof(v: u32, p: u32) -> Weight { + fn submit_finality_proof(p: u32, v: u32) -> Weight { (0 as Weight) - .saturating_add((756_462_000 as Weight).saturating_mul(v as Weight)) - .saturating_add((791_236_000 as Weight).saturating_mul(p 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)) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(6 as Weight)) } - fn submit_finality_proof_on_single_fork(v: u32) -> Weight { - (280_121_000 as Weight) - .saturating_add((14_098_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)) - } - fn submit_finality_proof_on_many_forks(p: u32) -> Weight { - (10_370_940_000 as Weight) - .saturating_add((96_902_000 as Weight).saturating_mul(p as Weight)) - .saturating_add(T::DbWeight::get().reads(7 as Weight)) - .saturating_add(T::DbWeight::get().writes(6 as Weight)) - } - fn find_scheduled_change(n: u32) -> Weight { - (479_000 as Weight).saturating_add((11_000 as Weight).saturating_mul(n as Weight)) - } - fn read_write_authority_sets(n: u32) -> Weight { - (8_030_000 as Weight) - .saturating_add((232_000 as Weight).saturating_mul(n as Weight)) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().writes(1 as Weight)) - } } // For backwards compatibility and tests impl WeightInfo for () { - fn submit_finality_proof(v: u32, p: u32) -> Weight { + fn submit_finality_proof(p: u32, v: u32) -> Weight { (0 as Weight) - .saturating_add((756_462_000 as Weight).saturating_mul(v as Weight)) - .saturating_add((791_236_000 as Weight).saturating_mul(p 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)) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(6 as Weight)) } - fn submit_finality_proof_on_single_fork(v: u32) -> Weight { - (280_121_000 as Weight) - .saturating_add((14_098_000 as Weight).saturating_mul(v as Weight)) - .saturating_add(RocksDbWeight::get().reads(7 as Weight)) - .saturating_add(RocksDbWeight::get().writes(6 as Weight)) - } - fn submit_finality_proof_on_many_forks(p: u32) -> Weight { - (10_370_940_000 as Weight) - .saturating_add((96_902_000 as Weight).saturating_mul(p as Weight)) - .saturating_add(RocksDbWeight::get().reads(7 as Weight)) - .saturating_add(RocksDbWeight::get().writes(6 as Weight)) - } - fn find_scheduled_change(n: u32) -> Weight { - (479_000 as Weight).saturating_add((11_000 as Weight).saturating_mul(n as Weight)) - } - fn read_write_authority_sets(n: u32) -> Weight { - (8_030_000 as Weight) - .saturating_add((232_000 as Weight).saturating_mul(n as Weight)) - .saturating_add(RocksDbWeight::get().reads(1 as Weight)) - .saturating_add(RocksDbWeight::get().writes(1 as Weight)) - } } diff --git a/bridges/modules/messages/src/weights.rs b/bridges/modules/messages/src/weights.rs index f86a21e3ed..aea0e44239 100644 --- a/bridges/modules/messages/src/weights.rs +++ b/bridges/modules/messages/src/weights.rs @@ -17,7 +17,7 @@ //! Autogenerated weights for pallet_bridge_messages //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-04-21, STEPS: [50, ], REPEAT: 20 +//! DATE: 2021-06-03, STEPS: [50, ], REPEAT: 20 //! LOW RANGE: [], HIGH RANGE: [] //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled //! CHAIN: Some("dev"), DB CACHE: 128 @@ -73,105 +73,105 @@ pub trait WeightInfo { pub struct RialtoWeight(PhantomData); impl WeightInfo for RialtoWeight { fn send_minimal_message_worst_case() -> Weight { - (149_643_000 as Weight) + (140_457_000 as Weight) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(12 as Weight)) } fn send_1_kb_message_worst_case() -> Weight { - (153_329_000 as Weight) + (138_097_000 as Weight) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(12 as Weight)) } fn send_16_kb_message_worst_case() -> Weight { - (200_113_000 as Weight) + (196_192_000 as Weight) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(12 as Weight)) } fn increase_message_fee() -> Weight { - (6_407_252_000 as Weight) + (6_244_063_000 as Weight) .saturating_add(T::DbWeight::get().reads(4 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn receive_single_message_proof() -> Weight { - (141_256_000 as Weight) + (135_633_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn receive_two_messages_proof() -> Weight { - (247_723_000 as Weight) + (229_415_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn receive_single_message_proof_with_outbound_lane_state() -> Weight { - (159_731_000 as Weight) + (147_408_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn receive_single_message_proof_1_kb() -> Weight { - (168_546_000 as Weight) + (160_092_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn receive_single_message_proof_16_kb() -> Weight { - (450_087_000 as Weight) + (452_140_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 { - (164_519_000 as Weight) + (123_704_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 { - (173_300_000 as Weight) + (127_844_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 { - (246_205_000 as Weight) + (183_119_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 { - (149_551_000 as Weight) + (162_249_000 as Weight) .saturating_add((3_000 as Weight).saturating_mul(i as Weight)) .saturating_add(T::DbWeight::get().reads(5 as Weight)) .saturating_add(T::DbWeight::get().writes(12 as Weight)) } fn receive_multiple_messages_proof(i: u32) -> Weight { (0 as Weight) - .saturating_add((114_817_000 as Weight).saturating_mul(i as Weight)) + .saturating_add((107_235_000 as Weight).saturating_mul(i as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn receive_message_proofs_with_extra_nodes(i: u32) -> Weight { - (437_797_000 as Weight) - .saturating_add((10_000 as Weight).saturating_mul(i as Weight)) + (450_232_000 as Weight) + .saturating_add((9_000 as Weight).saturating_mul(i as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn receive_message_proofs_with_large_leaf(i: u32) -> Weight { - (137_633_000 as Weight) + (181_851_000 as Weight) .saturating_add((7_000 as Weight).saturating_mul(i as Weight)) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn receive_multiple_messages_proof_with_outbound_lane_state(i: u32) -> Weight { (0 as Weight) - .saturating_add((118_482_000 as Weight).saturating_mul(i as Weight)) + .saturating_add((114_622_000 as Weight).saturating_mul(i 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_multiple_messages_by_single_relayer(i: u32) -> Weight { - (116_036_000 as Weight) - .saturating_add((7_118_000 as Weight).saturating_mul(i as Weight)) + (103_133_000 as Weight) + .saturating_add((6_676_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 { - (172_780_000 as Weight) - .saturating_add((63_718_000 as Weight).saturating_mul(i as Weight)) + (100_321_000 as Weight) + .saturating_add((59_736_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)) @@ -182,105 +182,105 @@ impl WeightInfo for RialtoWeight { // For backwards compatibility and tests impl WeightInfo for () { fn send_minimal_message_worst_case() -> Weight { - (149_643_000 as Weight) + (140_457_000 as Weight) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(12 as Weight)) } fn send_1_kb_message_worst_case() -> Weight { - (153_329_000 as Weight) + (138_097_000 as Weight) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(12 as Weight)) } fn send_16_kb_message_worst_case() -> Weight { - (200_113_000 as Weight) + (196_192_000 as Weight) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(12 as Weight)) } fn increase_message_fee() -> Weight { - (6_407_252_000 as Weight) + (6_244_063_000 as Weight) .saturating_add(RocksDbWeight::get().reads(4 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } fn receive_single_message_proof() -> Weight { - (141_256_000 as Weight) + (135_633_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn receive_two_messages_proof() -> Weight { - (247_723_000 as Weight) + (229_415_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn receive_single_message_proof_with_outbound_lane_state() -> Weight { - (159_731_000 as Weight) + (147_408_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn receive_single_message_proof_1_kb() -> Weight { - (168_546_000 as Weight) + (160_092_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn receive_single_message_proof_16_kb() -> Weight { - (450_087_000 as Weight) + (452_140_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 { - (164_519_000 as Weight) + (123_704_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 { - (173_300_000 as Weight) + (127_844_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 { - (246_205_000 as Weight) + (183_119_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 { - (149_551_000 as Weight) + (162_249_000 as Weight) .saturating_add((3_000 as Weight).saturating_mul(i as Weight)) .saturating_add(RocksDbWeight::get().reads(5 as Weight)) .saturating_add(RocksDbWeight::get().writes(12 as Weight)) } fn receive_multiple_messages_proof(i: u32) -> Weight { (0 as Weight) - .saturating_add((114_817_000 as Weight).saturating_mul(i as Weight)) + .saturating_add((107_235_000 as Weight).saturating_mul(i as Weight)) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn receive_message_proofs_with_extra_nodes(i: u32) -> Weight { - (437_797_000 as Weight) - .saturating_add((10_000 as Weight).saturating_mul(i as Weight)) + (450_232_000 as Weight) + .saturating_add((9_000 as Weight).saturating_mul(i as Weight)) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn receive_message_proofs_with_large_leaf(i: u32) -> Weight { - (137_633_000 as Weight) + (181_851_000 as Weight) .saturating_add((7_000 as Weight).saturating_mul(i as Weight)) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn receive_multiple_messages_proof_with_outbound_lane_state(i: u32) -> Weight { (0 as Weight) - .saturating_add((118_482_000 as Weight).saturating_mul(i as Weight)) + .saturating_add((114_622_000 as Weight).saturating_mul(i as Weight)) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn receive_delivery_proof_for_multiple_messages_by_single_relayer(i: u32) -> Weight { - (116_036_000 as Weight) - .saturating_add((7_118_000 as Weight).saturating_mul(i as Weight)) + (103_133_000 as Weight) + .saturating_add((6_676_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 { - (172_780_000 as Weight) - .saturating_add((63_718_000 as Weight).saturating_mul(i as Weight)) + (100_321_000 as Weight) + .saturating_add((59_736_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/bridges/primitives/header-chain/tests/justification.rs b/bridges/primitives/header-chain/tests/justification.rs index ab8d9c4905..85846c6d50 100644 --- a/bridges/primitives/header-chain/tests/justification.rs +++ b/bridges/primitives/header-chain/tests/justification.rs @@ -29,7 +29,7 @@ fn valid_justification_accepted() { round: TEST_GRANDPA_ROUND, set_id: TEST_GRANDPA_SET_ID, authorities: authorities.clone(), - votes: 7, + ancestors: 7, forks: 3, }; @@ -45,7 +45,7 @@ fn valid_justification_accepted() { ); assert_eq!(justification.commit.precommits.len(), authorities.len()); - assert_eq!(justification.votes_ancestries.len(), params.votes as usize); + assert_eq!(justification.votes_ancestries.len(), params.ancestors as usize); } #[test] @@ -55,7 +55,7 @@ fn valid_justification_accepted_with_single_fork() { round: TEST_GRANDPA_ROUND, set_id: TEST_GRANDPA_SET_ID, authorities: vec![(ALICE, 1), (BOB, 1), (CHARLIE, 1), (DAVE, 1), (EVE, 1)], - votes: 5, + ancestors: 5, forks: 1, }; @@ -83,7 +83,7 @@ fn valid_justification_accepted_with_arbitrary_number_of_authorities() { round: TEST_GRANDPA_ROUND, set_id: TEST_GRANDPA_SET_ID, authorities: authorities.clone(), - votes: n.into(), + ancestors: n.into(), forks: n.into(), }; @@ -175,7 +175,7 @@ fn justification_is_invalid_if_we_dont_meet_threshold() { round: TEST_GRANDPA_ROUND, set_id: TEST_GRANDPA_SET_ID, authorities: authorities.clone(), - votes: 2 * authorities.len() as u32, + ancestors: 2 * authorities.len() as u32, forks: 2, }; diff --git a/bridges/primitives/test-utils/Cargo.toml b/bridges/primitives/test-utils/Cargo.toml index 5adb2c2b55..461e0ffe93 100644 --- a/bridges/primitives/test-utils/Cargo.toml +++ b/bridges/primitives/test-utils/Cargo.toml @@ -7,9 +7,9 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] bp-header-chain = { path = "../header-chain", default-features = false } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } ed25519-dalek = { version = "1.0", default-features = false, features = ["u64_backend"] } finality-grandpa = { version = "0.14.0", default-features = false } -parity-scale-codec = { version = "2.0.0", default-features = false } sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } @@ -19,9 +19,9 @@ sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", d default = ["std"] std = [ "bp-header-chain/std", + "codec/std", "ed25519-dalek/std", "finality-grandpa/std", - "parity-scale-codec/std", "sp-application-crypto/std", "sp-finality-grandpa/std", "sp-runtime/std", diff --git a/bridges/primitives/test-utils/src/keyring.rs b/bridges/primitives/test-utils/src/keyring.rs index 6c5b1cae91..b83678cae5 100644 --- a/bridges/primitives/test-utils/src/keyring.rs +++ b/bridges/primitives/test-utils/src/keyring.rs @@ -16,9 +16,9 @@ //! Utilities for working with test accounts. +use codec::Encode; use ed25519_dalek::{Keypair, PublicKey, SecretKey, Signature}; use finality_grandpa::voter_set::VoterSet; -use parity_scale_codec::Encode; use sp_application_crypto::Public; use sp_finality_grandpa::{AuthorityId, AuthorityList, AuthorityWeight}; use sp_runtime::RuntimeDebug; diff --git a/bridges/primitives/test-utils/src/lib.rs b/bridges/primitives/test-utils/src/lib.rs index 0eff3fe585..14260e9b7b 100644 --- a/bridges/primitives/test-utils/src/lib.rs +++ b/bridges/primitives/test-utils/src/lib.rs @@ -19,6 +19,7 @@ #![cfg_attr(not(feature = "std"), no_std)] use bp_header_chain::justification::GrandpaJustification; +use codec::Encode; use sp_application_crypto::TryFrom; use sp_finality_grandpa::{AuthorityId, AuthorityWeight}; use sp_finality_grandpa::{AuthoritySignature, SetId}; @@ -46,10 +47,10 @@ pub struct JustificationGeneratorParams { /// /// The size of the set will determine the number of pre-commits in our justification. pub authorities: Vec<(Account, AuthorityWeight)>, - /// The total number of vote ancestries in our justification. + /// The total number of precommit ancestors in the `votes_ancestries` field our justification. /// /// These may be distributed among many different forks. - pub votes: u32, + pub ancestors: u32, /// The number of forks. /// /// Useful for creating a "worst-case" scenario in which each authority is on its own fork. @@ -63,7 +64,7 @@ impl Default for JustificationGeneratorParams { round: TEST_GRANDPA_ROUND, set_id: TEST_GRANDPA_SET_ID, authorities: test_keyring(), - votes: 2, + ancestors: 2, forks: 1, } } @@ -94,35 +95,33 @@ pub fn make_justification_for_header(params: JustificationGeneratorP round, set_id, authorities, - mut votes, + mut ancestors, forks, } = params; - let (target_hash, target_number) = (header.hash(), *header.number()); - let mut precommits = vec![]; let mut votes_ancestries = vec![]; + let mut precommits = vec![]; assert!(forks != 0, "Need at least one fork to have a chain.."); - assert!(votes >= forks, "Need at least one header per fork."); assert!( forks as usize <= authorities.len(), "If we have more forks than authorities we can't create valid pre-commits for all the forks." ); // Roughly, how many vote ancestries do we want per fork - let target_depth = (votes + forks - 1) / forks; + let target_depth = (ancestors + forks - 1) / forks; let mut unsigned_precommits = vec![]; for i in 0..forks { - let depth = if votes >= target_depth { - votes -= target_depth; + let depth = if ancestors >= target_depth { + ancestors -= target_depth; target_depth } else { - votes + ancestors }; // Note: Adding 1 to account for the target header - let chain = generate_chain(i as u8, depth + 1, &header); + let chain = generate_chain(i as u32, depth + 1, &header); // We don't include our finality target header in the vote ancestries for child in &chain[1..] { @@ -154,7 +153,7 @@ pub fn make_justification_for_header(params: JustificationGeneratorP } } -fn generate_chain(fork_id: u8, depth: u32, ancestor: &H) -> Vec { +fn generate_chain(fork_id: u32, depth: u32, ancestor: &H) -> Vec { let mut headers = vec![ancestor.clone()]; for i in 1..depth { @@ -169,7 +168,7 @@ fn generate_chain(fork_id: u8, depth: u32, ancestor: &H) -> Vec { header .digest_mut() .logs - .push(sp_runtime::DigestItem::Other(vec![fork_id])); + .push(sp_runtime::DigestItem::Other(fork_id.encode())); headers.push(header); } diff --git a/bridges/relays/bin-substrate/src/messages_lane.rs b/bridges/relays/bin-substrate/src/messages_lane.rs index 616e2253a6..bfca980a07 100644 --- a/bridges/relays/bin-substrate/src/messages_lane.rs +++ b/bridges/relays/bin-substrate/src/messages_lane.rs @@ -203,7 +203,7 @@ mod tests { // reserved for messages dispatch allows dispatch of non-trivial messages. // // Any significant change in this values should attract additional attention. - (1013, 216_583_333_334), + (1024, 216_583_333_334), ); } }