Skip to content

Commit

Permalink
adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Jan 16, 2025
1 parent 97f2bc8 commit 42430f2
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 156 deletions.
2 changes: 2 additions & 0 deletions bridges/snowbridge/pallets/ethereum-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,12 @@ pub mod pallet {
pub(super) fn select_fork_version(fork_versions: &ForkVersions, epoch: u64) -> ForkVersion {
if epoch >= fork_versions.electra.epoch {
log::info!(target: LOG_TARGET,"💫 Found Electra fork.");
println!("ELECTRA");
return fork_versions.electra.version
}
if epoch >= fork_versions.deneb.epoch {
log::info!(target: LOG_TARGET,"💫 Found Deneb fork.");
println!("DENEB");
return fork_versions.deneb.version
}
if epoch >= fork_versions.capella.epoch {
Expand Down
1 change: 1 addition & 0 deletions bridges/snowbridge/pallets/ethereum-client/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl ethereum_beacon_client::Config for Test {

// Build genesis storage according to the mock runtime.
pub fn new_tester() -> sp_io::TestExternalities {
println!("DENEB storage");
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let ext = sp_io::TestExternalities::new(t);
ext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ pub fn load_next_finalized_header_update_fixture() -> snowbridge_beacon_primitiv
load_fixture("next-finalized-header-update.json".to_string()).unwrap()
}

pub fn load_other_finalized_header_update_fixture() -> snowbridge_beacon_primitives::Update<
{ config::SYNC_COMMITTEE_SIZE },
{ config::SYNC_COMMITTEE_BITS_SIZE },
> {
load_fixture("other-finalized-header-update.json".to_string()).unwrap()
}

pub fn get_message_verification_payload() -> (Log, Proof) {
let inbound_fixture = snowbridge_pallet_ethereum_client_fixtures::make_inbound_fixture();
(inbound_fixture.message.event_log, inbound_fixture.message.proof)
Expand Down Expand Up @@ -124,6 +131,7 @@ impl ethereum_beacon_client::Config for Test {

// Build genesis storage according to the mock runtime.
pub fn new_tester() -> sp_io::TestExternalities {
println!("ELECTRA storage");
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let ext = sp_io::TestExternalities::new(t);
ext
Expand Down
31 changes: 11 additions & 20 deletions bridges/snowbridge/pallets/ethereum-client/src/tests_electra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ pub use crate::mock_electra::*;
use crate::{
config::{EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH, SLOTS_PER_HISTORICAL_ROOT},
functions::compute_period,
mock::{
mock_electra::{
get_message_verification_payload, load_checkpoint_update_fixture,
load_finalized_header_update_fixture, load_next_finalized_header_update_fixture,
load_next_sync_committee_update_fixture, load_sync_committee_update_fixture,
},
sync_committee_sum, verify_merkle_branch, BeaconHeader, CompactBeaconState, Error,
FinalizedBeaconState, LatestFinalizedBlockRoot, LatestSyncCommitteeUpdatePeriod,
NextSyncCommittee, SyncCommitteePrepared,
FinalizedBeaconState, LatestFinalizedBlockRoot, NextSyncCommittee, SyncCommitteePrepared,
};
use frame_support::{assert_err, assert_noop, assert_ok, pallet_prelude::Pays};
use hex_literal::hex;
Expand Down Expand Up @@ -412,33 +411,26 @@ fn submit_update_with_sync_committee_in_current_period() {
fn reject_submit_update_in_next_period() {
let checkpoint = Box::new(load_checkpoint_update_fixture());
let sync_committee_update = Box::new(load_sync_committee_update_fixture());
let finalized_update = Box::new(load_finalized_header_update_fixture());
let update = Box::new(load_next_finalized_header_update_fixture());
let sync_committee_period = compute_period(sync_committee_update.finalized_header.slot);
let next_sync_committee_period = compute_period(update.finalized_header.slot);
assert_eq!(sync_committee_period + 1, next_sync_committee_period);
let next_sync_committee_update = Box::new(load_next_sync_committee_update_fixture());

new_tester().execute_with(|| {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
let result =
EthereumBeaconClient::submit(RuntimeOrigin::signed(1), sync_committee_update.clone());
let result = EthereumBeaconClient::submit(RuntimeOrigin::signed(1), sync_committee_update);
assert_ok!(result);
assert_eq!(result.unwrap().pays_fee, Pays::No);

// interim update required so the header gap is not too large
let other_result = EthereumBeaconClient::submit(RuntimeOrigin::signed(1), finalized_update);
assert_ok!(other_result);

// check an update in the next period is rejected
let second_result = EthereumBeaconClient::submit(RuntimeOrigin::signed(1), update.clone());
let second_result = EthereumBeaconClient::submit(RuntimeOrigin::signed(1), update);
assert_err!(second_result, Error::<Test>::SyncCommitteeUpdateRequired);
assert_eq!(second_result.unwrap_err().post_info.pays_fee, Pays::Yes);

// submit update with next sync committee
let third_result =
EthereumBeaconClient::submit(RuntimeOrigin::signed(1), next_sync_committee_update);
assert_ok!(third_result);
assert_eq!(third_result.unwrap().pays_fee, Pays::No);
// check same header in the next period can now be submitted successfully
assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), update.clone()));
let block_root: H256 = update.finalized_header.clone().hash_tree_root().unwrap();
assert!(<FinalizedBeaconState<Test>>::contains_key(block_root));
});
}

Expand Down Expand Up @@ -532,13 +524,12 @@ fn submit_update_with_sync_committee_in_next_period() {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
assert!(!<NextSyncCommittee<Test>>::exists());

let result = EthereumBeaconClient::submit(RuntimeOrigin::signed(1), update.clone());
let result = EthereumBeaconClient::submit(RuntimeOrigin::signed(1), update);
assert_ok!(result);
assert_eq!(result.unwrap().pays_fee, Pays::No);
assert!(<NextSyncCommittee<Test>>::exists());

let second_result =
EthereumBeaconClient::submit(RuntimeOrigin::signed(1), next_update.clone());
let second_result = EthereumBeaconClient::submit(RuntimeOrigin::signed(1), next_update);
assert_ok!(second_result);
assert_eq!(second_result.unwrap().pays_fee, Pays::No);
let last_finalized_state =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
{
"header": {
"slot": 1301,
"proposer_index": 6,
"parent_root": "0x5f82401d1c73d0a0adcc7870dccc45475d1435c5f24f8a5446f2f9e8bbe79b6b",
"state_root": "0x29061e45b205fdce59af55870d6d8e2a1924bb6ed172d452de93ba2b66b13e60",
"body_root": "0x584d6bc148501a77c62fff96148590f1af43144ddb0dd3f8525e7b064c82926d"
"slot": 1471,
"proposer_index": 0,
"parent_root": "0xee2c2d983ca291f9836bb2686e27110df7083d6ffe8592135e6f5682e5872865",
"state_root": "0xc91dde0b460345d59632abc0c97f850ca7865db10ad503b5ad0735491ab1f591",
"body_root": "0x1078188f82a3b186b59eabc9ad9dbf78678fc712f25839b54eae0de6745b66aa"
},
"ancestry_proof": {
"header_branch": [
"0x5f82401d1c73d0a0adcc7870dccc45475d1435c5f24f8a5446f2f9e8bbe79b6b",
"0x112ee9d45cdde51125d66e80d1927a257ccb9a057be6782ed8d584f430ae31c7",
"0xa7db3faec19240fdf1bd65ac0749a58c7d0d067ec22de809b722f4fa6f1eeb93",
"0xdcecf843d6a3496f1720dba541ecaf381604ddc0da9bcb18498ee422d0d3e8ad",
"0xf0561fa63aa1a284ca5f9298df9df9fc665a308e06b2bc91c549083a7be5c1fa",
"0xe7db86a113a15a850864cb481a49e6939f034d6a4162933af0a90456a0b3e652",
"0xddf4b03cd5895642d849dd858a9786e96988ec4da6de0f3997fa359d127ab327",
"0x6f9435042ae999e0c56b490ed83971db93c0deb29d0ca4a06db3fce513f24347",
"0xdd5c61f48f74b42fc4a6cdf960677e7ca57b132c48066b6c91d22e14905c3c27",
"0xae15cb7f5e2ffd780ccb19532a8103fc23dd4da8bc8448582b0875284383300b",
"0x8d6f44d201b62436ec2979d9a88de06b1142b58347e5c21970c236e53220ae4c",
"0xee2c2d983ca291f9836bb2686e27110df7083d6ffe8592135e6f5682e5872865",
"0xf72c17e4b0335c0ff944ca33455734793315afe7eec60294370ce4449e62ddeb",
"0xbefa57233af9add021a971ba8f13f24fed9bb9041360795e037b60b28b4fe93c",
"0xde5943a938b973bf2cf2ba7b05dc8ed6113e9d1659e2b40e0de0c91de5cd67da",
"0x401318565c107ddd5b2de4671a798589088c080c26bcbeb6701773c3f25f97d6",
"0xb3555c1cf5898f9d83580757a6d31e603f723b19f4b4bbcfeca701222e8b560e",
"0xab7c47c9f8e4b6439647bcd53e717fb1069e7fbce7ed548e724c0a8ef364d6d2",
"0xe787c98181293e74b6973fb75f92b2e323119c095485178c1516df69fbfac2cb",
"0xc2a7138ee8ce76654281b297707b67229e9334a1d4fdcc792c87691e4f18e2c5",
"0xd23f0d2ca62ee6840fdbce17673c4ef6a29266a07b3fe504d54be0ed6c30ed53",
"0x01462e461c6fe4865b751c1a55f1bf21cacb611dbe795b1562bc76c1c36baf64",
"0x6cf04127db05441cd833107a52be852868890e4317e6a02ab47683aa75964220",
"0xb7d05f875f140027ef5118a2247bbb84ce8f2f0f1123623085daf7960c329f5f"
],
"finalized_block_root": "0x6fde76df79a31f17e5b221501f3109878a2a06cc6bba2caad42e1ee6e31c279f"
"finalized_block_root": "0xeadd60f47f74c01dc12c7dc892490f549e0664e18df3dd27b9a80dd7df2b1f23"
},
"execution_header": {
"Deneb": {
"parent_hash": "0x36ab7c387fa4d4a12f011935924ecefe7ec2778920b1f8685d5d92418cb449b7",
"parent_hash": "0xbddc8ab20335a9d9448ac21b56ada15b59974da43d25f5a299784f3b9cac31a4",
"fee_recipient": "0x0000000000000000000000000000000000000000",
"state_root": "0xc6197a8ce963657e1f00feeae93b50667b863bdbacde9a60788a7e8b8102099c",
"state_root": "0x466f0a0b0437f2f30d2417914ae88d0aec5b823ef99e6dec8000a65030e5ded1",
"receipts_root": "0xf87cce309dcadb263aa9dda92bf74e6218b5198b9cb315c98d9be72376c203ac",
"logs_bloom": "0x00000000000000000000000000000000000000000000004000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000080000000000000000000000000000004000000000080000000000000000000000000000000000010100000000000000000000000000000000000000000000000000000000000000001000000000080000000000000000000000040000000000000000002000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000200000000000010",
"prev_randao": "0x8bec524f0f9c4203fbae90c1277dfa80b9bcb564e9d316b43dd8c5b834e36776",
"block_number": 1301,
"prev_randao": "0xd4e5c2b7df821e8cf57b0fdc1fda4658ec898f87dbd8554e2379423daabfd953",
"block_number": 1471,
"gas_limit": 30000000,
"gas_used": 123189,
"timestamp": 1736792763,
"timestamp": 1736963157,
"extra_data": "0xd883010e0c846765746888676f312e32332e33856c696e7578",
"base_fee_per_gas": 7,
"block_hash": "0x5f770c95f8ffa248e0313fb2d5fb021fc76029783a33a57cd0b7a222d6ca6fb6",
"block_hash": "0x8501525d685416e05beb2d5dca1ad7702e1e55c9f1c715a6c451e4538c900ddf",
"transactions_root": "0xc3a1efbc96ee4b3700a0674ea64bcbc3e3818144a7a297a1f7abc51826186a1d",
"withdrawals_root": "0x792930bbd5baac43bcc798ee49aa8185ef76bb3b44ba62b91d86ae569e4bb535",
"blob_gas_used": 0,
"excess_blob_gas": 0
}
},
"execution_branch": [
"0xaafe1dffbb88291bf56bb67984ace5111e94675f922c63fdabd4318811606ef8",
"0xcc390acbae705724d483d45371bf243e5ca7698e8d333be9eb4f1e37dc65919a",
"0xb46f0c01805fe212e15907981b757e6c496b0cb06664224655613dcec82505bb",
"0xdb56114e00fdd4c1f85c892bf35ac9a89289aaecb1ebd0a96cde606a748b5d71",
"0x7e8ffac41d1d7fd3211d6c3a4ed576dd1617aee846ad9c1c89268e3dfa72600b"
"0x6dd3b9955d892d92338b19976fd07084bfe88a76c3063482b7f30ee60feb2a58",
"0x940522efb5e2581496c5e254e388423781dd70d9ce959cc5ff65382471de0b16"
]
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
{
"attested_header": {
"slot": 2022,
"proposer_index": 5,
"parent_root": "0xa54a4858696b1077e91b3f0ca4ce862ef690f56ccaebe3e522ce96b0c5aae005",
"state_root": "0x16e2927fa1632a1752a41b23546eac2859edb070dd2af1dd7798c114725d3373",
"body_root": "0x80f5a3cf38ae5a441adbf68046de8784652f2a19f2310bc05fb534b976be6496"
"slot": 2113,
"proposer_index": 6,
"parent_root": "0x87f243c86526060dca1f67b38f3fe98221d54bb9a0678df94e72476061e1bc62",
"state_root": "0xebb9f9b03dcc53361bb7a78c62dcf0b40fb32e7dd1375c23f525b5918bcdba9c",
"body_root": "0x09ba225e6d9edfada6599e6373e7efd7587cf3a1184da5fae29912cf668f29be"
},
"sync_aggregate": {
"sync_committee_bits": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"sync_committee_signature": "0x81049cf063a2a4b135330f03caff34131a8dd6f7e83b64ba2eed32a35a05ebe926e0be835756825d550057e28237b9fa036e925a8a84d7238de086bfa42a259de7922473fa75b923bf1f6398eb0453ea2b333b0bd8902fc6e8add9b14a03cbc0"
"sync_committee_signature": "0x950129e89341d6a05766b6fbc015b2cbe35325fb971c25cbe27dca644d4ed04be7946d8a8a11ad774a117159195aa98303134d6cd11549b92cbfd6212429e36e62063bd61cc53c8119e638974fb121ca73c174d8b19fb4d026f555e1c32fc890"
},
"signature_slot": 2023,
"signature_slot": 2114,
"next_sync_committee_update": null,
"finalized_header": {
"slot": 1952,
"proposer_index": 2,
"parent_root": "0x9406ae94a094bb5c69e93c2505bad4d4590839b995d9e2e9d5102a70342dc7c8",
"state_root": "0x2401698df613b0d096d469b34d88cb59264aa8c9501f0add586fb2d2de9a2452",
"body_root": "0x88c46d97eaa836efd7654d6e34db726985509fdfb1db99d5c76693048c0e1948"
"slot": 2048,
"proposer_index": 7,
"parent_root": "0x3ca76212e0701ac4e8a30be6c8cbe5f2e58b47f75d8148ee65d9c98650d6be68",
"state_root": "0x4e84f2beb1b34777d724cfd5b2a7b1f3c3cc74bdfc6b200c09958d980d3573cb",
"body_root": "0x840fea91c48e8040fcabc748274526111a4e9aebeda76a21ddb9b0f9d3bdd9af"
},
"finality_branch": [
"0x3d00000000000000000000000000000000000000000000000000000000000000",
"0x4000000000000000000000000000000000000000000000000000000000000000",
"0x10c726fac935bf9657cc7476d3cfa7bedec5983dcfb59e8a7df6d0a619e108d7",
"0x285982f071ac45a6a036d5af08cf5d42f749964e75c37332f9cd98d1f3f9ac69",
"0x9f3dbe3bdbffe5a633e3069f304bab5305c3e358516f4fe93da9b364321675e1",
"0x18011c3a3c64671819eb1c6f9468519e62fb94a9652669967ee8528c496eb212",
"0x1e4caee0475e802132a6baed6c39a38ac21a2f1986b50c88abb43787515f7b7b",
"0x95f9c7c621db355a489222cfb199b9722bcd1e8342e3e857b084225d755019d5",
"0xf9be8ddda9eeb5ae40168cdaff99c7a2c6c35f15e18e0d5bd6a41035d3fa6085",
"0x7bd256962edf47e0eb9c1e31aaa01821baddeeaa296efc95c00d2c509dfa6a4c",
"0xa1381fdc64967103fe79c0705727851ce61e7f91bee7e3e7759f9283c91ff7ff"
],
"block_roots_root": "0xb90c7dae5f977bc22c8de21888ee1e990a79d1f8dff804af42e38c39b3cdc980",
"block_roots_root": "0x6822b5d90a970e43565b7178870260ab7d083c0ac20b870c3c2a66183b7b5591",
"block_roots_branch": [
"0x65980422287343748ca583d66511e54c04382b31534863f6161031c94812528d",
"0x1c854f8d8c560273542a1e08a853c65fb6c662dcbb91a72401fc790998ff071a",
"0x0cde55fd5475d2ef7173ce66af33873d8e114b1497dd641b62bd69fdfc99290c",
"0xa384ec3513e01f983049282b720e0965571f3e0b15d84898e73168d91ba45f9a",
"0xb986f5cd87bb90a48118894685d071dbcb3692e6b29002c948f31d435f619d77",
"0x1c6c3259152e52a08621fd83e7b65dfcc24982081b28d18864acc798cdcb239b",
"0x90f4d586a68d56905eaa8d5ae88fc84bcc31bf57345d6c489d7cf8aadaac1cbb",
"0xd94fa501277bd0a9db542e964ec86793d45d2e4a982ddba26e676fe9cc886775",
"0x475d15486647c49ae55007b8b3aa1d9491fb262618a8392871dc7efd6d2c304d",
"0x10bcbc53cc02814a5dcf3a740df1d6e09563180c1199d86d67d7f3dfcfe5f41c",
"0xa1381fdc64967103fe79c0705727851ce61e7f91bee7e3e7759f9283c91ff7ff"
],
"execution_header": null,
Expand Down
Loading

0 comments on commit 42430f2

Please sign in to comment.