diff --git a/bridges/snowbridge/pallets/ethereum-client/src/config/mod.rs b/bridges/snowbridge/pallets/ethereum-client/src/config/mod.rs index 963b668ac2e0..bd6a1141c07a 100644 --- a/bridges/snowbridge/pallets/ethereum-client/src/config/mod.rs +++ b/bridges/snowbridge/pallets/ethereum-client/src/config/mod.rs @@ -1,10 +1,9 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2023 Snowfork -use snowbridge_beacon_primitives::merkle_proof::{generalized_index_length, subtree_index}; use static_assertions::const_assert; -pub mod electra; pub mod altair; +pub mod electra; /// Sizes related to SSZ encoding pub const MAX_EXTRA_DATA_BYTES: usize = 32; diff --git a/bridges/snowbridge/pallets/ethereum-client/src/impls.rs b/bridges/snowbridge/pallets/ethereum-client/src/impls.rs index 77cafecf64a4..deeea8709185 100644 --- a/bridges/snowbridge/pallets/ethereum-client/src/impls.rs +++ b/bridges/snowbridge/pallets/ethereum-client/src/impls.rs @@ -4,14 +4,12 @@ use super::*; use frame_support::ensure; use snowbridge_beacon_primitives::ExecutionProof; +use snowbridge_beacon_primitives::merkle_proof::{generalized_index_length, subtree_index}; use snowbridge_core::inbound::{ VerificationError::{self, *}, *, }; use snowbridge_ethereum::Receipt; -use snowbridge_beacon_primitives::merkle_proof::generalized_index_length; -use snowbridge_beacon_primitives::merkle_proof::subtree_index; - impl Verifier for Pallet { /// Verify a message by verifying the existence of the corresponding @@ -118,7 +116,10 @@ impl Pallet { .hash_tree_root() .map_err(|_| Error::::BlockBodyHashTreeRootFailed)?; - let execution_header_g_index = Self::execution_header_gindex_at_slot(execution_proof.header.slot, T::ForkVersions::get()); + let execution_header_g_index = Self::execution_header_gindex_at_slot( + execution_proof.header.slot, + T::ForkVersions::get(), + ); ensure!( verify_merkle_branch( execution_header_root, @@ -150,13 +151,12 @@ impl Pallet { let index_in_array = block_slot % (SLOTS_PER_HISTORICAL_ROOT as u64); let leaf_index = (SLOTS_PER_HISTORICAL_ROOT as u64) + index_in_array; - let block_roots_g_index = Self::block_roots_gindex_at_slot(block_slot, T::ForkVersions::get()); ensure!( verify_merkle_branch( block_root, block_root_proof, - subtree_index(block_roots_g_index), - generalized_index_length(block_roots_g_index), + leaf_index as usize, + config::BLOCK_ROOT_AT_INDEX_DEPTH, state.block_roots_root ), Error::::InvalidAncestryMerkleProof diff --git a/bridges/snowbridge/pallets/ethereum-client/src/lib.rs b/bridges/snowbridge/pallets/ethereum-client/src/lib.rs index 8ecac5354897..ab1c9c39d79c 100644 --- a/bridges/snowbridge/pallets/ethereum-client/src/lib.rs +++ b/bridges/snowbridge/pallets/ethereum-client/src/lib.rs @@ -41,11 +41,11 @@ use frame_support::{ }; use frame_system::ensure_signed; use snowbridge_beacon_primitives::{ - fast_aggregate_verify, verify_merkle_branch, verify_receipt_proof, BeaconHeader, BlsError, - CompactBeaconState, ForkData, ForkVersion, ForkVersions, PublicKeyPrepared, SigningData, + fast_aggregate_verify, + merkle_proof::{generalized_index_length, subtree_index}, + verify_merkle_branch, verify_receipt_proof, BeaconHeader, BlsError, CompactBeaconState, + ForkData, ForkVersion, ForkVersions, PublicKeyPrepared, SigningData, }; -use snowbridge_beacon_primitives::merkle_proof::generalized_index_length; -use snowbridge_beacon_primitives::merkle_proof::subtree_index; use snowbridge_core::{BasicOperatingMode, RingBufferMap}; use sp_core::H256; use sp_std::prelude::*; @@ -245,7 +245,10 @@ pub mod pallet { .map_err(|_| Error::::SyncCommitteeHashTreeRootFailed)?; let fork_versions = T::ForkVersions::get(); - let sync_committee_g_index = Self::current_sync_committee_gindex_at_slot(update.header.slot, fork_versions.clone()); + let sync_committee_g_index = Self::current_sync_committee_gindex_at_slot( + update.header.slot, + fork_versions.clone(), + ); // Verifies the sync committee in the Beacon state. ensure!( verify_merkle_branch( @@ -266,7 +269,8 @@ pub mod pallet { // This is used for ancestry proofs in ExecutionHeader updates. This verifies the // BeaconState: the beacon state root is the tree root; the `block_roots` hash is the // tree leaf. - let block_roots_g_index = Self::block_roots_gindex_at_slot(update.header.slot, fork_versions); + let block_roots_g_index = + Self::block_roots_gindex_at_slot(update.header.slot, fork_versions); ensure!( verify_merkle_branch( update.block_roots_root, @@ -351,9 +355,12 @@ pub mod pallet { ); let fork_versions = T::ForkVersions::get(); - let finalized_root_g_index = Self::finalized_root_gindex_at_slot(update.attested_header.slot, fork_versions.clone()); // TODO check attested / finalized header slot - // Verify that the `finality_branch`, if present, confirms `finalized_header` to match - // the finalized checkpoint root saved in the state of `attested_header`. + let finalized_root_g_index = Self::finalized_root_gindex_at_slot( + update.attested_header.slot, + fork_versions.clone(), + ); // TODO check attested / finalized header slot + // Verify that the `finality_branch`, if present, confirms `finalized_header` to match + // the finalized checkpoint root saved in the state of `attested_header`. let finalized_block_root: H256 = update .finalized_header .hash_tree_root() @@ -372,7 +379,10 @@ pub mod pallet { // Though following check does not belong to ALC spec we verify block_roots_root to // match the finalized checkpoint root saved in the state of `finalized_header` so to // cache it for later use in `verify_ancestry_proof`. - let block_roots_g_index = Self::block_roots_gindex_at_slot(update.finalized_header.slot, fork_versions.clone()); + let block_roots_g_index = Self::block_roots_gindex_at_slot( + update.finalized_header.slot, + fork_versions.clone(), + ); ensure!( verify_merkle_branch( update.block_roots_root, @@ -398,7 +408,10 @@ pub mod pallet { Error::::InvalidSyncCommitteeUpdate ); } - let next_sync_committee_g_index = Self::next_sync_committee_gindex_at_slot(update.attested_header.slot, fork_versions); + let next_sync_committee_g_index = Self::next_sync_committee_gindex_at_slot( + update.attested_header.slot, + fork_versions, + ); ensure!( verify_merkle_branch( sync_committee_root, @@ -693,7 +706,10 @@ pub mod pallet { config::altair::FINALIZED_ROOT_INDEX } - pub fn current_sync_committee_gindex_at_slot(slot: u64, fork_versions: ForkVersions) -> usize { + pub fn current_sync_committee_gindex_at_slot( + slot: u64, + fork_versions: ForkVersions, + ) -> usize { let epoch = compute_epoch(slot, config::SLOTS_PER_EPOCH as u64); if epoch >= fork_versions.electra.epoch { @@ -717,9 +733,11 @@ pub mod pallet { let epoch = compute_epoch(slot, config::SLOTS_PER_EPOCH as u64); if epoch >= fork_versions.electra.epoch { - return config::electra::BLOCK_ROOTS_INDEX; + println!("ELECTRA"); + return config::electra::BLOCK_ROOTS_INDEX; } + println!("ALTAIR"); config::altair::BLOCK_ROOTS_INDEX }