Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Sep 9, 2024
1 parent 207cc10 commit 241c644
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
3 changes: 1 addition & 2 deletions bridges/snowbridge/pallets/ethereum-client/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
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;
Expand Down
14 changes: 7 additions & 7 deletions bridges/snowbridge/pallets/ethereum-client/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Config> Verifier for Pallet<T> {
/// Verify a message by verifying the existence of the corresponding
Expand Down Expand Up @@ -118,7 +116,10 @@ impl<T: Config> Pallet<T> {
.hash_tree_root()
.map_err(|_| Error::<T>::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,
Expand Down Expand Up @@ -150,13 +151,12 @@ impl<T: Config> Pallet<T> {
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::<T>::InvalidAncestryMerkleProof
Expand Down
44 changes: 31 additions & 13 deletions bridges/snowbridge/pallets/ethereum-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -245,7 +245,10 @@ pub mod pallet {
.map_err(|_| Error::<T>::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(
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -398,7 +408,10 @@ pub mod pallet {
Error::<T>::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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit 241c644

Please sign in to comment.