Skip to content

Commit

Permalink
chore: hash_block replaced with is_hash_valid in network spec to impl…
Browse files Browse the repository at this point in the history
…ement extra checks on the block
  • Loading branch information
Dhruv-2003 committed Jan 22, 2025
1 parent d88ec2b commit 733ea9b
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/src/execution/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl<N: NetworkSpec, R: ExecutionRpc<N>> Inner<N, R> {
if self.blocks.get(&prev).is_none() {
let backfilled = self.rpc.get_block(block.header().parent_hash()).await?;

if N::hash_block(&backfilled) == backfilled.header().hash()
if N::is_hash_valid(&backfilled)
&& block.header().parent_hash() == backfilled.header().hash()
{
info!("backfilled: block={}", backfilled.header().number());
Expand Down
4 changes: 2 additions & 2 deletions core/src/network_spec.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use alloy::{network::Network, primitives::B256, rpc::types::Log};
use alloy::{network::Network, rpc::types::Log};
use revm::primitives::{BlockEnv, TxEnv};

pub trait NetworkSpec: Network {
fn encode_receipt(receipt: &Self::ReceiptResponse) -> Vec<u8>;
fn hash_block(block: &Self::BlockResponse) -> B256;
fn is_hash_valid(block: &Self::BlockResponse) -> bool;
fn receipt_contains(list: &[Self::ReceiptResponse], elem: &Self::ReceiptResponse) -> bool;
fn receipt_logs(receipt: &Self::ReceiptResponse) -> Vec<Log>;
fn tx_env(request: &Self::TransactionRequest) -> TxEnv;
Expand Down
11 changes: 11 additions & 0 deletions ethereum/consensus-core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ pub struct Withdrawal {
amount: u64,
}

impl Into<alloy::eips::eip4895::Withdrawal> for Withdrawal {

Check failure on line 221 in ethereum/consensus-core/src/types/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
fn into(self) -> alloy::eips::eip4895::Withdrawal {
alloy::eips::eip4895::Withdrawal {
index: self.index,
validator_index: self.validator_index,
address: self.address,
amount: self.amount,
}
}
}

#[derive(Deserialize, Debug, Default, Encode, TreeHash, Clone)]
pub struct ProposerSlashing {
signed_header_1: SignedBeaconBlockHeader,
Expand Down
14 changes: 12 additions & 2 deletions ethereum/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::process;
use std::sync::Arc;

use alloy::consensus::{Header as ConsensusHeader, Transaction as TxTrait, TxEnvelope};
use alloy::eips::eip4895::{Withdrawal, Withdrawals};
use alloy::primitives::{b256, fixed_bytes, Bloom, BloomInput, B256, U256};
use alloy::rlp::{encode, Decodable};
use alloy::rpc::types::{Block, BlockTransactions, Header, Transaction};
Expand Down Expand Up @@ -602,11 +603,19 @@ fn payload_to_block<S: ConsensusSpec>(value: ExecutionPayload<S>) -> Block<Trans
})
.collect::<Vec<Transaction>>();

let withdrawals: Vec<Withdrawal> = value
.withdrawals()
.unwrap()
.into_iter()
.map(|w| w.clone().into())
.collect();

let raw_txs = value.transactions().iter().map(|tx| tx.inner.to_vec());
let txs_root = ordered_trie_root(raw_txs);

let withdrawals = value.withdrawals().unwrap().iter().map(encode);
let withdrawals_root = ordered_trie_root(withdrawals);
let raw_withdrawals = value.withdrawals().unwrap().iter().map(encode);
let withdrawals_root = ordered_trie_root(raw_withdrawals);

let logs_bloom: Bloom =
Bloom::from(BloomInput::Raw(&value.logs_bloom().clone().inner.to_vec()));

Expand Down Expand Up @@ -642,6 +651,7 @@ fn payload_to_block<S: ConsensusSpec>(value: ExecutionPayload<S>) -> Block<Trans
};

Block::new(header, BlockTransactions::Full(txs))
.with_withdrawals(Some(Withdrawals::new(withdrawals)))
}

#[cfg(test)]
Expand Down
31 changes: 28 additions & 3 deletions ethereum/src/spec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use alloy::{
consensus::{BlockHeader, Receipt, ReceiptWithBloom, TxReceipt, TxType, TypedTransaction},
consensus::{
proofs::{calculate_transaction_root, calculate_withdrawals_root},
BlockHeader, Receipt, ReceiptWithBloom, TxReceipt, TxType, TypedTransaction,
},
network::{BuildResult, Network, NetworkWallet, TransactionBuilder, TransactionBuilderError},
primitives::{Address, Bytes, ChainId, TxKind, U256},
rpc::types::{AccessList, Log, TransactionRequest},
Expand Down Expand Up @@ -36,8 +39,30 @@ impl NetworkSpec for Ethereum {
}
}

fn hash_block(block: &Self::BlockResponse) -> revm::primitives::B256 {
block.header.hash_slow()
fn is_hash_valid(block: &Self::BlockResponse) -> bool {
if block.header.hash_slow() != block.header.hash {
return false;
}

if let Some(txs) = block.transactions.as_transactions() {
let txs_root = calculate_transaction_root(
&txs.iter().map(|t| t.clone().inner).collect::<Vec<_>>(),
);
if txs_root != block.header.transactions_root {
return false;
}
}

if let Some(withdrawals) = &block.withdrawals {
let withdrawals_root = calculate_withdrawals_root(
&withdrawals.iter().map(|w| w.clone()).collect::<Vec<_>>(),
);
if Some(withdrawals_root) != block.header.withdrawals_root {
return false;
}
}

true
}

fn receipt_contains(list: &[Self::ReceiptResponse], elem: &Self::ReceiptResponse) -> bool {
Expand Down
18 changes: 14 additions & 4 deletions opstack/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use alloy::consensus::{Header as ConsensusHeader, Transaction as TxTrait};
use alloy::primitives::{b256, fixed_bytes, keccak256, Address, Bloom, BloomInput, B256, U256};
use alloy::rlp::Decodable;
use alloy::rpc::types::{
Block, EIP1186AccountProofResponse, Header, Transaction as EthTransaction,
Block, EIP1186AccountProofResponse, Header, Transaction as EthTransaction, Withdrawal,
Withdrawals,
};
use alloy_rlp::encode;
use eyre::{eyre, OptionExt, Result};
Expand Down Expand Up @@ -339,11 +340,19 @@ fn payload_to_block(value: ExecutionPayload) -> Result<Block<Transaction>> {
})
.collect::<Result<Vec<Transaction>>>()?;

let withdrawals: Vec<Withdrawal> = value
.withdrawals
.clone()
.into_iter()
.map(|w| w.clone().into())
.collect();

let raw_txs = value.transactions.iter().map(|tx| tx.to_vec());
let txs_root = ordered_trie_root(raw_txs);

let withdrawals = value.withdrawals.iter().map(|v| encode(v));
let withdrawals_root = ordered_trie_root(withdrawals);
let raw_withdrawals = value.withdrawals.iter().map(|v| encode(v));
let withdrawals_root = ordered_trie_root(raw_withdrawals);

let logs_bloom: Bloom = Bloom::from(BloomInput::Raw(&value.logs_bloom.to_vec()));

let consensus_header = ConsensusHeader {
Expand Down Expand Up @@ -377,5 +386,6 @@ fn payload_to_block(value: ExecutionPayload) -> Result<Block<Transaction>> {
size: Some(U256::ZERO),
};

Ok(Block::new(header, BlockTransactions::Full(txs)))
Ok(Block::new(header, BlockTransactions::Full(txs))
.with_withdrawals(Some(Withdrawals::new(withdrawals))))
}
29 changes: 27 additions & 2 deletions opstack/src/spec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloy::{
consensus::{
proofs::{calculate_transaction_root, calculate_withdrawals_root},
BlockHeader, Receipt, ReceiptWithBloom, Transaction as TxTrait, TxReceipt, TxType,
},
primitives::{Address, Bytes, ChainId, TxKind, U256},
Expand Down Expand Up @@ -69,8 +70,32 @@ impl NetworkSpec for OpStack {
}
}

fn hash_block(block: &Self::BlockResponse) -> revm::primitives::B256 {
block.header.hash_slow()
fn is_hash_valid(block: &Self::BlockResponse) -> bool {
if block.header.hash_slow() != block.header.hash {
return false;
}

if let Some(txs) = block.transactions.as_transactions() {
let txs_root = calculate_transaction_root(
&txs.iter()
.map(|t| t.clone().inner.inner)
.collect::<Vec<_>>(),
);
if txs_root != block.header.transactions_root {
return false;
}
}

if let Some(withdrawals) = &block.withdrawals {
let withdrawals_root = calculate_withdrawals_root(
&withdrawals.iter().map(|w| w.clone()).collect::<Vec<_>>(),
);
if Some(withdrawals_root) != block.header.withdrawals_root {
return false;
}
}

true
}

fn receipt_contains(list: &[Self::ReceiptResponse], elem: &Self::ReceiptResponse) -> bool {
Expand Down
11 changes: 11 additions & 0 deletions opstack/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ pub struct Withdrawal {
address: Address,
amount: u64,
}

impl Into<alloy::eips::eip4895::Withdrawal> for Withdrawal {
fn into(self) -> alloy::eips::eip4895::Withdrawal {
alloy::eips::eip4895::Withdrawal {
index: self.index,
validator_index: self.validator_index,
address: self.address,
amount: self.amount,
}
}
}

0 comments on commit 733ea9b

Please sign in to comment.