Skip to content

Commit

Permalink
chore: remove unused WithdrawalsProvider::latest_withdrawal (#13671)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo authored Jan 6, 2025
1 parent 873009f commit bd34537
Show file tree
Hide file tree
Showing 12 changed files with 15 additions and 374 deletions.
73 changes: 0 additions & 73 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions crates/consensus/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@ alloy-eips.workspace = true

[dev-dependencies]
alloy-consensus.workspace = true
reth-storage-api.workspace = true
reth-ethereum-primitives.workspace = true
rand.workspace = true
mockall = "0.13"

210 changes: 4 additions & 206 deletions crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,123 +335,12 @@ pub fn validate_against_parent_4844<H: BlockHeader>(
#[cfg(test)]
mod tests {
use super::*;
use alloy_consensus::{Header, TxEip4844, EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH};
use alloy_eips::{
eip4895::{Withdrawal, Withdrawals},
BlockHashOrNumber,
};
use alloy_primitives::{
hex_literal::hex, Address, BlockHash, BlockNumber, Bytes, PrimitiveSignature as Signature,
U256,
};
use mockall::mock;
use alloy_consensus::{Header, TxEip4844};
use alloy_eips::eip4895::Withdrawals;
use alloy_primitives::{Address, Bytes, PrimitiveSignature as Signature, U256};
use rand::Rng;
use reth_chainspec::ChainSpecBuilder;
use reth_ethereum_primitives::{Transaction, TransactionSigned};
use reth_primitives::{proofs, Account, BlockBody};
use reth_storage_api::{
errors::provider::ProviderResult, AccountReader, HeaderProvider, WithdrawalsProvider,
};
use std::ops::RangeBounds;

mock! {
WithdrawalsProvider {}

impl WithdrawalsProvider for WithdrawalsProvider {
fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> ;

fn withdrawals_by_block(
&self,
_id: BlockHashOrNumber,
_timestamp: u64,
) -> ProviderResult<Option<Withdrawals>> ;
}
}

struct Provider {
is_known: bool,
parent: Option<Header>,
account: Option<Account>,
withdrawals_provider: MockWithdrawalsProvider,
}

impl Provider {
/// New provider with parent
fn new(parent: Option<Header>) -> Self {
Self {
is_known: false,
parent,
account: None,
withdrawals_provider: MockWithdrawalsProvider::new(),
}
}
}

impl AccountReader for Provider {
fn basic_account(&self, _address: &Address) -> ProviderResult<Option<Account>> {
Ok(self.account)
}
}

impl HeaderProvider for Provider {
type Header = Header;

fn is_known(&self, _block_hash: &BlockHash) -> ProviderResult<bool> {
Ok(self.is_known)
}

fn header(&self, _block_number: &BlockHash) -> ProviderResult<Option<Header>> {
Ok(self.parent.clone())
}

fn header_by_number(&self, _num: u64) -> ProviderResult<Option<Header>> {
Ok(self.parent.clone())
}

fn header_td(&self, _hash: &BlockHash) -> ProviderResult<Option<U256>> {
Ok(None)
}

fn header_td_by_number(&self, _number: BlockNumber) -> ProviderResult<Option<U256>> {
Ok(None)
}

fn headers_range(
&self,
_range: impl RangeBounds<BlockNumber>,
) -> ProviderResult<Vec<Header>> {
Ok(vec![])
}

fn sealed_header(
&self,
_block_number: BlockNumber,
) -> ProviderResult<Option<SealedHeader>> {
Ok(None)
}

fn sealed_headers_while(
&self,
_range: impl RangeBounds<BlockNumber>,
_predicate: impl FnMut(&SealedHeader) -> bool,
) -> ProviderResult<Vec<SealedHeader>> {
Ok(vec![])
}
}

impl WithdrawalsProvider for Provider {
fn withdrawals_by_block(
&self,
_id: BlockHashOrNumber,
_timestamp: u64,
) -> ProviderResult<Option<Withdrawals>> {
self.withdrawals_provider.withdrawals_by_block(_id, _timestamp)
}

fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> {
self.withdrawals_provider.latest_withdrawal()
}
}
use reth_primitives::{proofs, BlockBody, Transaction, TransactionSigned};

fn mock_blob_tx(nonce: u64, num_blobs: usize) -> TransactionSigned {
let mut rng = rand::thread_rng();
Expand All @@ -474,97 +363,6 @@ mod tests {
TransactionSigned::new_unhashed(request, signature)
}

/// got test block
fn mock_block() -> (SealedBlock, Header) {
// https://etherscan.io/block/15867168 where transaction root and receipts root are cleared
// empty merkle tree: 0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421

let header = Header {
parent_hash: hex!("859fad46e75d9be177c2584843501f2270c7e5231711e90848290d12d7c6dcdd").into(),
ommers_hash: EMPTY_OMMER_ROOT_HASH,
beneficiary: hex!("4675c7e5baafbffbca748158becba61ef3b0a263").into(),
state_root: hex!("8337403406e368b3e40411138f4868f79f6d835825d55fd0c2f6e17b1a3948e9").into(),
transactions_root: EMPTY_ROOT_HASH,
receipts_root: EMPTY_ROOT_HASH,
logs_bloom: hex!("002400000000004000220000800002000000000000000000000000000000100000000000000000100000000000000021020000000800000006000000002100040000000c0004000000000008000008200000000000000000000000008000000001040000020000020000002000000800000002000020000000022010000000000000010002001000000000020200000000000001000200880000004000000900020000000000020000000040000000000000000000000000000080000000000001000002000000000000012000200020000000000000001000000000000020000010321400000000100000000000000000000000000000400000000000000000").into(),
difficulty: U256::ZERO, // total difficulty: 0xc70d815d562d3cfa955).into(),
number: 0xf21d20,
gas_limit: 0x1c9c380,
gas_used: 0x6e813,
timestamp: 0x635f9657,
extra_data: hex!("")[..].into(),
mix_hash: hex!("0000000000000000000000000000000000000000000000000000000000000000").into(),
nonce: 0x0000000000000000u64.into(),
base_fee_per_gas: 0x28f0001df.into(),
withdrawals_root: None,
blob_gas_used: None,
excess_blob_gas: None,
parent_beacon_block_root: None,
requests_hash: None,
};
// size: 0x9b5

let mut parent = header.clone();
parent.gas_used = 17763076;
parent.gas_limit = 30000000;
parent.base_fee_per_gas = Some(0x28041f7f5);
parent.number -= 1;
parent.timestamp -= 1;

let ommers = Vec::new();
let transactions = Vec::new();

(
SealedBlock::new(
SealedHeader::seal(header),
BlockBody { transactions, ommers, withdrawals: None },
),
parent,
)
}

#[test]
fn valid_withdrawal_index() {
let chain_spec = ChainSpecBuilder::mainnet().shanghai_activated().build();

let create_block_with_withdrawals = |indexes: &[u64]| {
let withdrawals = Withdrawals::new(
indexes
.iter()
.map(|idx| Withdrawal { index: *idx, ..Default::default() })
.collect(),
);

let header = Header {
withdrawals_root: Some(proofs::calculate_withdrawals_root(&withdrawals)),
..Default::default()
};

SealedBlock::new(
SealedHeader::seal(header),
BlockBody { withdrawals: Some(withdrawals), ..Default::default() },
)
};

// Single withdrawal
let block: SealedBlock = create_block_with_withdrawals(&[1]);
assert_eq!(validate_block_pre_execution(&block, &chain_spec), Ok(()));

// Multiple increasing withdrawals
let block = create_block_with_withdrawals(&[1, 2, 3]);
assert_eq!(validate_block_pre_execution(&block, &chain_spec), Ok(()));
let block = create_block_with_withdrawals(&[5, 6, 7, 8, 9]);
assert_eq!(validate_block_pre_execution(&block, &chain_spec), Ok(()));
let (_, parent) = mock_block();

// Withdrawal index should be the last withdrawal index + 1
let mut provider = Provider::new(Some(parent));
provider
.withdrawals_provider
.expect_latest_withdrawal()
.return_const(Ok(Some(Withdrawal { index: 2, ..Default::default() })));
}

#[test]
fn cancun_block_incorrect_blob_gas_used() {
let chain_spec = ChainSpecBuilder::mainnet().cancun_activated().build();
Expand Down
15 changes: 1 addition & 14 deletions crates/storage/provider/src/providers/blockchain_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,6 @@ impl<N: ProviderNodeTypes> WithdrawalsProvider for BlockchainProvider2<N> {
) -> ProviderResult<Option<Withdrawals>> {
self.consistent_provider()?.withdrawals_by_block(id, timestamp)
}

fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> {
self.consistent_provider()?.latest_withdrawal()
}
}

impl<N: ProviderNodeTypes> OmmersProvider for BlockchainProvider2<N> {
Expand Down Expand Up @@ -1459,7 +1455,7 @@ mod tests {
"Expected withdrawals_by_block to return empty list if block does not exist"
);

for block in blocks.clone() {
for block in blocks {
assert_eq!(
provider
.withdrawals_by_block(
Expand All @@ -1472,15 +1468,6 @@ mod tests {
);
}

let canonical_block_num = provider.best_block_number().unwrap();
let canonical_block = blocks.get(canonical_block_num as usize).unwrap();

assert_eq!(
Some(provider.latest_withdrawal()?.unwrap()),
canonical_block.body().withdrawals.clone().unwrap().pop(),
"Expected latest withdrawal to be equal to last withdrawal entry in canonical block"
);

Ok(())
}

Expand Down
Loading

0 comments on commit bd34537

Please sign in to comment.