diff --git a/bridges/bin/rialto/runtime/src/exchange.rs b/bridges/bin/rialto/runtime/src/exchange.rs index 12fbadf012dd..926d9595372a 100644 --- a/bridges/bin/rialto/runtime/src/exchange.rs +++ b/bridges/bin/rialto/runtime/src/exchange.rs @@ -133,7 +133,7 @@ pub(crate) fn prepare_environment_for_claim, ) -> 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, }; @@ -143,7 +143,7 @@ pub(crate) fn prepare_environment_for_claim, .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 diff --git a/bridges/modules/ethereum/src/finality.rs b/bridges/modules/ethereum/src/finality.rs index e53ce91ed3cf..608708a0c7ba 100644 --- a/bridges/modules/ethereum/src/finality.rs +++ b/bridges/modules/ethereum/src/finality.rs @@ -140,7 +140,7 @@ fn is_finalized( } /// Prepare 'votes' of header and its ancestors' signers. -fn prepare_votes( +pub(crate) fn prepare_votes( mut cached_votes: CachedFinalityVotes, best_finalized: HeaderId, validators: &BTreeSet<&Address>, diff --git a/bridges/modules/ethereum/src/lib.rs b/bridges/modules/ethereum/src/lib.rs index 70ebbe624ce5..05beb279a926 100644 --- a/bridges/modules/ethereum/src/lib.rs +++ b/bridges/modules/ethereum/src/lib.rs @@ -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; @@ -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(f: impl Fn(BridgeStorage) -> T) -> T { @@ -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::::get(&header_with_entry_hash), - Some(Default::default()), - ); + assert!(FinalityCache::::get(&header_with_entry_hash).is_some()); // when we later prune this header, cache entry is removed BlocksToPrune::::put(PruningRange { diff --git a/bridges/modules/ethereum/src/test_utils.rs b/bridges/modules/ethereum/src/test_utils.rs index e1e145f3d123..f41652f57081 100644 --- a/bridges/modules/ethereum/src/test_utils.rs +++ b/bridges/modules/ethereum/src/test_utils.rs @@ -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(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(storage: &mut S, header: AuraHeader) { storage.insert_header(HeaderToImport { context: storage.import_context(None, &header.parent_hash).unwrap(), is_best: true,