chore: remove unused WithdrawalsProvider::latest_withdrawal (#13671)

This commit is contained in:
joshieDo
2025-01-06 16:15:09 +00:00
committed by GitHub
parent 873009fd68
commit bd345378b6
12 changed files with 15 additions and 374 deletions

View File

@ -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"

View File

@ -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();
@ -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();

View File

@ -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> {
@ -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(
@ -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(())
}

View File

@ -8,9 +8,8 @@ use crate::{
};
use alloy_consensus::{transaction::TransactionMeta, BlockHeader};
use alloy_eips::{
eip2718::Encodable2718,
eip4895::{Withdrawal, Withdrawals},
BlockHashOrNumber, BlockId, BlockNumHash, BlockNumberOrTag, HashOrNumber,
eip2718::Encodable2718, eip4895::Withdrawals, BlockHashOrNumber, BlockId, BlockNumHash,
BlockNumberOrTag, HashOrNumber,
};
use alloy_primitives::{
map::{hash_map, HashMap},
@ -1132,24 +1131,6 @@ impl<N: ProviderNodeTypes> WithdrawalsProvider for ConsistentProvider<N> {
|block_state| Ok(block_state.block_ref().block().body().withdrawals().cloned()),
)
}
fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> {
let best_block_num = self.best_block_number()?;
self.get_in_memory_or_storage_by_block(
best_block_num.into(),
|db_provider| db_provider.latest_withdrawal(),
|block_state| {
Ok(block_state
.block_ref()
.block()
.body()
.withdrawals()
.cloned()
.and_then(|mut w| w.pop()))
},
)
}
}
impl<N: ProviderNodeTypes> OmmersProvider for ConsistentProvider<N> {

View File

@ -8,10 +8,7 @@ use crate::{
TransactionVariant, TransactionsProvider, WithdrawalsProvider,
};
use alloy_consensus::transaction::TransactionMeta;
use alloy_eips::{
eip4895::{Withdrawal, Withdrawals},
BlockHashOrNumber,
};
use alloy_eips::{eip4895::Withdrawals, BlockHashOrNumber};
use alloy_primitives::{Address, BlockHash, BlockNumber, TxHash, TxNumber, B256, U256};
use core::fmt;
use reth_chainspec::{ChainInfo, EthereumHardforks};
@ -556,10 +553,6 @@ impl<N: ProviderNodeTypes> WithdrawalsProvider for ProviderFactory<N> {
) -> ProviderResult<Option<Withdrawals>> {
self.provider()?.withdrawals_by_block(id, timestamp)
}
fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> {
self.provider()?.latest_withdrawal()
}
}
impl<N: ProviderNodeTypes> OmmersProvider for ProviderFactory<N> {

View File

@ -20,11 +20,7 @@ use crate::{
TransactionsProviderExt, TrieWriter, WithdrawalsProvider,
};
use alloy_consensus::{transaction::TransactionMeta, BlockHeader, Header};
use alloy_eips::{
eip2718::Encodable2718,
eip4895::{Withdrawal, Withdrawals},
BlockHashOrNumber,
};
use alloy_eips::{eip2718::Encodable2718, eip4895::Withdrawals, BlockHashOrNumber};
use alloy_primitives::{
keccak256,
map::{hash_map, B256HashMap, HashMap, HashSet},
@ -1593,12 +1589,6 @@ impl<TX: DbTx + 'static, N: NodeTypes<ChainSpec: EthereumHardforks>> Withdrawals
}
Ok(None)
}
fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> {
let latest_block_withdrawal = self.tx.cursor_read::<tables::BlockWithdrawals>()?.last()?;
Ok(latest_block_withdrawal
.and_then(|(_, mut block_withdrawal)| block_withdrawal.withdrawals.pop()))
}
}
impl<TX: DbTx + 'static, N: NodeTypesForProvider> OmmersProvider for DatabaseProvider<TX, N> {

View File

@ -11,8 +11,7 @@ use crate::{
};
use alloy_consensus::{transaction::TransactionMeta, Header};
use alloy_eips::{
eip4895::{Withdrawal, Withdrawals},
BlockHashOrNumber, BlockId, BlockNumHash, BlockNumberOrTag,
eip4895::Withdrawals, BlockHashOrNumber, BlockId, BlockNumHash, BlockNumberOrTag,
};
use alloy_primitives::{Address, BlockHash, BlockNumber, TxHash, TxNumber, B256, U256};
use alloy_rpc_types_engine::ForkchoiceState;
@ -570,10 +569,6 @@ impl<N: ProviderNodeTypes> WithdrawalsProvider for BlockchainProvider<N> {
) -> ProviderResult<Option<Withdrawals>> {
self.database.withdrawals_by_block(id, timestamp)
}
fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> {
self.database.latest_withdrawal()
}
}
impl<N: TreeNodeTypes> OmmersProvider for BlockchainProvider<N> {

View File

@ -8,11 +8,7 @@ use crate::{
TransactionsProviderExt, WithdrawalsProvider,
};
use alloy_consensus::{transaction::TransactionMeta, Header};
use alloy_eips::{
eip2718::Encodable2718,
eip4895::{Withdrawal, Withdrawals},
BlockHashOrNumber,
};
use alloy_eips::{eip2718::Encodable2718, eip4895::Withdrawals, BlockHashOrNumber};
use alloy_primitives::{keccak256, Address, BlockHash, BlockNumber, TxHash, TxNumber, B256, U256};
use dashmap::DashMap;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
@ -1675,11 +1671,6 @@ impl<N: NodePrimitives> WithdrawalsProvider for StaticFileProvider<N> {
// Required data not present in static_files
Err(ProviderError::UnsupportedProvider)
}
fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> {
// Required data not present in static_files
Err(ProviderError::UnsupportedProvider)
}
}
impl<N: FullNodePrimitives<BlockHeader: Value>> OmmersProvider for StaticFileProvider<N> {

View File

@ -6,10 +6,7 @@ use crate::{
StateRootProvider, TransactionVariant, TransactionsProvider, WithdrawalsProvider,
};
use alloy_consensus::{constants::EMPTY_ROOT_HASH, transaction::TransactionMeta, Header};
use alloy_eips::{
eip4895::{Withdrawal, Withdrawals},
BlockHashOrNumber, BlockId, BlockNumberOrTag,
};
use alloy_eips::{eip4895::Withdrawals, BlockHashOrNumber, BlockId, BlockNumberOrTag};
use alloy_primitives::{
keccak256,
map::{B256HashMap, HashMap},
@ -766,9 +763,6 @@ impl WithdrawalsProvider for MockEthProvider {
) -> ProviderResult<Option<Withdrawals>> {
Ok(None)
}
fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> {
Ok(None)
}
}
impl OmmersProvider for MockEthProvider {

View File

@ -9,10 +9,7 @@ use crate::{
TransactionVariant, TransactionsProvider, WithdrawalsProvider,
};
use alloy_consensus::transaction::TransactionMeta;
use alloy_eips::{
eip4895::{Withdrawal, Withdrawals},
BlockHashOrNumber, BlockId, BlockNumberOrTag,
};
use alloy_eips::{eip4895::Withdrawals, BlockHashOrNumber, BlockId, BlockNumberOrTag};
use alloy_primitives::{
map::{B256HashMap, HashMap},
Address, BlockHash, BlockNumber, Bytes, StorageKey, StorageValue, TxHash, TxNumber, B256, U256,
@ -543,9 +540,6 @@ impl<C: Send + Sync, N: NodePrimitives> WithdrawalsProvider for NoopProvider<C,
) -> ProviderResult<Option<Withdrawals>> {
Ok(None)
}
fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>> {
Ok(None)
}
}
impl<C: Send + Sync, N: NodePrimitives> OmmersProvider for NoopProvider<C, N> {

View File

@ -1,10 +1,7 @@
use alloy_eips::{
eip4895::{Withdrawal, Withdrawals},
BlockHashOrNumber,
};
use alloy_eips::{eip4895::Withdrawals, BlockHashOrNumber};
use reth_storage_errors::provider::ProviderResult;
/// Client trait for fetching [Withdrawal] related data.
/// Client trait for fetching [`alloy_eips::eip4895::Withdrawal`] related data.
#[auto_impl::auto_impl(&, Arc)]
pub trait WithdrawalsProvider: Send + Sync {
/// Get withdrawals by block id.
@ -13,7 +10,4 @@ pub trait WithdrawalsProvider: Send + Sync {
id: BlockHashOrNumber,
timestamp: u64,
) -> ProviderResult<Option<Withdrawals>>;
/// Get latest withdrawal from this block or earlier .
fn latest_withdrawal(&self) -> ProviderResult<Option<Withdrawal>>;
}