Skip to content

Commit

Permalink
Fix Ethereum pallet benchmarks (paritytech#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik authored and serban300 committed Apr 10, 2024
1 parent fb1761e commit 0d5b0ef
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
4 changes: 2 additions & 2 deletions bridges/bin/rialto/runtime/src/exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub(crate) fn prepare_environment_for_claim<T: pallet_bridge_eth_poa::Config<I>,
) -> bp_eth_poa::H256 {
use bp_eth_poa::compute_merkle_root;
use pallet_bridge_eth_poa::{
test_utils::{insert_header, validator_utils::validator, HeaderBuilder},
test_utils::{insert_dummy_header, validator_utils::validator, HeaderBuilder},
BridgeStorage, Storage,
};

Expand All @@ -143,7 +143,7 @@ pub(crate) fn prepare_environment_for_claim<T: pallet_bridge_eth_poa::Config<I>,
.receipts_root(compute_merkle_root(transactions.iter().map(|(_, receipt)| receipt)))
.sign_by(&validator(0));
let header_id = header.compute_id();
insert_header(&mut storage, header);
insert_dummy_header(&mut storage, header);
storage.finalize_and_prune_headers(Some(header_id), 0);

header_id.hash
Expand Down
2 changes: 1 addition & 1 deletion bridges/modules/ethereum/src/finality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn is_finalized(
}

/// Prepare 'votes' of header and its ancestors' signers.
fn prepare_votes<Submitter>(
pub(crate) fn prepare_votes<Submitter>(
mut cached_votes: CachedFinalityVotes<Submitter>,
best_finalized: HeaderId,
validators: &BTreeSet<&Address>,
Expand Down
39 changes: 14 additions & 25 deletions bridges/modules/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,7 @@ pub(crate) mod tests {
genesis, insert_header, run_test, run_test_with_genesis, validators_addresses, HeaderBuilder, TestRuntime,
GAS_LIMIT,
};
use crate::test_utils::validator_utils::*;
use bp_eth_poa::compute_merkle_root;

const TOTAL_VALIDATORS: usize = 3;
Expand All @@ -1069,33 +1070,24 @@ pub(crate) mod tests {
}

fn example_header_with_failed_receipt() -> AuraHeader {
AuraHeader {
number: 3,
transactions_root: compute_merkle_root(vec![example_tx()].into_iter()),
receipts_root: compute_merkle_root(vec![example_tx_receipt(false)].into_iter()),
parent_hash: example_header().compute_hash(),
..Default::default()
}
HeaderBuilder::with_parent(&example_header())
.transactions_root(compute_merkle_root(vec![example_tx()].into_iter()))
.receipts_root(compute_merkle_root(vec![example_tx_receipt(false)].into_iter()))
.sign_by(&validator(0))
}

fn example_header() -> AuraHeader {
AuraHeader {
number: 2,
transactions_root: compute_merkle_root(vec![example_tx()].into_iter()),
receipts_root: compute_merkle_root(vec![example_tx_receipt(true)].into_iter()),
parent_hash: example_header_parent().compute_hash(),
..Default::default()
}
HeaderBuilder::with_parent(&example_header_parent())
.transactions_root(compute_merkle_root(vec![example_tx()].into_iter()))
.receipts_root(compute_merkle_root(vec![example_tx_receipt(true)].into_iter()))
.sign_by(&validator(0))
}

fn example_header_parent() -> AuraHeader {
AuraHeader {
number: 1,
transactions_root: compute_merkle_root(vec![example_tx()].into_iter()),
receipts_root: compute_merkle_root(vec![example_tx_receipt(true)].into_iter()),
parent_hash: genesis().compute_hash(),
..Default::default()
}
HeaderBuilder::with_parent(&genesis())
.transactions_root(compute_merkle_root(vec![example_tx()].into_iter()))
.receipts_root(compute_merkle_root(vec![example_tx_receipt(true)].into_iter()))
.sign_by(&validator(0))
}

fn with_headers_to_prune<T>(f: impl Fn(BridgeStorage<TestRuntime>) -> T) -> T {
Expand Down Expand Up @@ -1277,10 +1269,7 @@ pub(crate) mod tests {
let header_with_entry = HeaderBuilder::with_parent_number(interval - 1).sign_by_set(&ctx.validators);
let header_with_entry_hash = header_with_entry.compute_hash();
insert_header(&mut storage, header_with_entry);
assert_eq!(
FinalityCache::<TestRuntime>::get(&header_with_entry_hash),
Some(Default::default()),
);
assert!(FinalityCache::<TestRuntime>::get(&header_with_entry_hash).is_some());

// when we later prune this header, cache entry is removed
BlocksToPrune::<DefaultInstance>::put(PruningRange {
Expand Down
34 changes: 34 additions & 0 deletions bridges/modules/ethereum/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,41 @@ where
}

/// Insert unverified header into storage.
///
/// This function assumes that the header is signed by validator from the current set.
pub fn insert_header<S: Storage>(storage: &mut S, header: AuraHeader) {
let id = header.compute_id();
let best_finalized = storage.finalized_block();
let import_context = storage.import_context(None, &header.parent_hash).unwrap();
let parent_finality_votes = storage.cached_finality_votes(&header.parent_id().unwrap(), &best_finalized, |_| false);
let finality_votes = crate::finality::prepare_votes(
parent_finality_votes,
best_finalized,
&import_context.validators_set().validators.iter().collect(),
id,
&header,
None,
)
.unwrap();

storage.insert_header(HeaderToImport {
context: storage.import_context(None, &header.parent_hash).unwrap(),
is_best: true,
id,
header,
total_difficulty: 0.into(),
enacted_change: None,
scheduled_change: None,
finality_votes,
});
}

/// Insert unverified header into storage.
///
/// No assumptions about header author are made. The cost is that finality votes cache
/// is filled incorrectly, so this function shall not be used if you're going to insert
/// (or import) header descendants.
pub fn insert_dummy_header<S: Storage>(storage: &mut S, header: AuraHeader) {
storage.insert_header(HeaderToImport {
context: storage.import_context(None, &header.parent_hash).unwrap(),
is_best: true,
Expand Down

0 comments on commit 0d5b0ef

Please sign in to comment.