This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split implementation over several files (paritytech#837)
- Loading branch information
Showing
11 changed files
with
205 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}, | ||
} | ||
} | ||
} |
Oops, something went wrong.