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

Commit

Permalink
Split implementation over several files (paritytech#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes authored May 19, 2023
1 parent 54ab138 commit 28df768
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ benchmarks! {
let initial_sync_data = initial_sync();
let sync_committee_update = initialize_sync_committee::<T>()?;

let period = EthereumBeaconClient::<T>::compute_current_sync_period(sync_committee_update.attested_header.slot);
let period = compute_period(sync_committee_update.attested_header.slot);

// initialize LatestFinalizedHeaderState with parent slot of finalized_header_update
LatestFinalizedHeader::<T>::set(FinalizedHeaderState {
Expand All @@ -45,7 +45,7 @@ benchmarks! {

let finalized_header_update = finalized_header_update();

let current_period = EthereumBeaconClient::<T>::compute_current_sync_period(
let current_period = compute_period(
finalized_header_update.attested_header.slot,
);

Expand Down Expand Up @@ -74,7 +74,7 @@ benchmarks! {

let header_update = header_update();

let current_period = EthereumBeaconClient::<T>::compute_current_sync_period(
let current_period = compute_period(
header_update.header.slot,
);

Expand Down
17 changes: 6 additions & 11 deletions parachain/pallets/ethereum-beacon-client/src/benchmarking/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
decompress_sync_committee_bits, Config, LatestSyncCommitteePeriod,
Pallet as EthereumBeaconClient, SyncCommitteeUpdate, SyncCommittees, ValidatorsRoot, Vec,
SYNC_COMMITTEE_SIZE,
compute_period, config::SYNC_COMMITTEE_SIZE, decompress_sync_committee_bits, Config,
LatestSyncCommitteePeriod, Pallet as EthereumBeaconClient, SyncCommitteeUpdate, SyncCommittees,
ValidatorsRoot, Vec,
};
use primitives::{PublicKeyPrepared, SyncCommitteePrepared};
use sp_core::H256;
Expand All @@ -16,12 +16,8 @@ pub fn initialize_sync_committee<T: Config>() -> Result<SyncCommitteeUpdate, &'s
let sync_committee_update = sync_committee_update();

// initialize SyncCommittees with period in sync_committee_update
LatestSyncCommitteePeriod::<T>::set(EthereumBeaconClient::<T>::compute_current_sync_period(
sync_committee_update.attested_header.slot,
));
let current_period = EthereumBeaconClient::<T>::compute_current_sync_period(
sync_committee_update.attested_header.slot,
);
LatestSyncCommitteePeriod::<T>::set(compute_period(sync_committee_update.attested_header.slot));
let current_period = compute_period(sync_committee_update.attested_header.slot);
EthereumBeaconClient::<T>::store_sync_committee(
current_period,
&initial_sync_data.current_sync_committee,
Expand All @@ -32,8 +28,7 @@ pub fn initialize_sync_committee<T: Config>() -> Result<SyncCommitteeUpdate, &'s
pub fn sync_committee<T: Config>(
update: &SyncCommitteeUpdate,
) -> Result<SyncCommitteePrepared<SYNC_COMMITTEE_SIZE>, &'static str> {
let current_period =
EthereumBeaconClient::<T>::compute_current_sync_period(update.attested_header.slot);
let current_period = compute_period(update.attested_header.slot);
let sync_committee = SyncCommittees::<T>::get(current_period).ok_or("no sync committee")?;
Ok(sync_committee)
}
Expand Down
29 changes: 29 additions & 0 deletions parachain/pallets/ethereum-beacon-client/src/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::config::{
EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH, SYNC_COMMITTEE_BITS_SIZE,
SYNC_COMMITTEE_SIZE,
};

/// Decompress packed bitvector into byte vector according to SSZ deserialization rules. Each byte
/// in the decompressed vector is either 0 or 1.
pub fn decompress_sync_committee_bits(
input: [u8; SYNC_COMMITTEE_BITS_SIZE],
) -> [u8; SYNC_COMMITTEE_SIZE] {
primitives::decompress_sync_committee_bits::<SYNC_COMMITTEE_SIZE, SYNC_COMMITTEE_BITS_SIZE>(
input,
)
}

/// Compute the sync committee period in which a slot is contained
pub fn compute_period(slot: u64) -> u64 {
slot / SLOTS_PER_EPOCH as u64 / EPOCHS_PER_SYNC_COMMITTEE_PERIOD as u64
}

/// Compute epoch in which a slot is contained
pub fn compute_epoch(slot: u64, slots_per_epoch: u64) -> u64 {
slot / slots_per_epoch
}

/// Sums the bit vector of sync committee particpation.
pub fn sync_committee_sum(sync_committee_bits: &[u8]) -> u32 {
sync_committee_bits.iter().fold(0, |acc: u32, x| acc + *x as u32)
}
93 changes: 93 additions & 0 deletions parachain/pallets/ethereum-beacon-client/src/impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use super::*;

use frame_support::dispatch::DispatchError;
use snowbridge_ethereum::{Log, Receipt};

impl<T: Config> Verifier for Pallet<T> {
/// Verify a message by verifying the existence of the corresponding
/// Ethereum log in a block. Returns the log if successful.
fn verify(message: &Message) -> Result<Log, DispatchError> {
log::info!(
target: "ethereum-beacon-client",
"💫 Verifying message with block hash {}",
message.proof.block_hash,
);

let header = <ExecutionHeaderBuffer<T>>::get(message.proof.block_hash)
.ok_or(Error::<T>::MissingHeader)?;

let receipt = match Self::verify_receipt_inclusion(header.receipts_root, &message.proof) {
Ok(receipt) => receipt,
Err(err) => {
log::error!(
target: "ethereum-beacon-client",
"💫 Verify receipt inclusion failed for block {}: {:?}",
message.proof.block_hash,
err
);
return Err(err)
},
};

log::trace!(
target: "ethereum-beacon-client",
"💫 Verified receipt inclusion for transaction at index {} in block {}",
message.proof.tx_index, message.proof.block_hash,
);

let log = match rlp::decode(&message.data) {
Ok(log) => log,
Err(err) => {
log::error!(
target: "ethereum-beacon-client",
"💫 RLP log decoded failed {}: {:?}",
message.proof.block_hash,
err
);
return Err(Error::<T>::DecodeFailed.into())
},
};

if !receipt.contains_log(&log) {
log::error!(
target: "ethereum-beacon-client",
"💫 Event log not found in receipt for transaction at index {} in block {}",
message.proof.tx_index, message.proof.block_hash,
);
return Err(Error::<T>::InvalidProof.into())
}

log::info!(
target: "ethereum-beacon-client",
"💫 Receipt verification successful for {}",
message.proof.block_hash,
);

Ok(log)
}
}

impl<T: Config> Pallet<T> {
// Verifies that the receipt encoded in proof.data is included
// in the block given by proof.block_hash. Inclusion is only
// recognized if the block has been finalized.
pub fn verify_receipt_inclusion(
receipts_root: H256,
proof: &Proof,
) -> Result<Receipt, DispatchError> {
let result =
verify_receipt_proof(receipts_root, &proof.data.1).ok_or(Error::<T>::InvalidProof)?;

match result {
Ok(receipt) => Ok(receipt),
Err(err) => {
log::trace!(
target: "ethereum-beacon-client",
"💫 Failed to decode transaction receipt: {}",
err
);
Err(Error::<T>::InvalidProof.into())
},
}
}
}
Loading

0 comments on commit 28df768

Please sign in to comment.