Skip to content

Commit

Permalink
primitives: replace primitive Withdrawals with alloy (#12119)
Browse files Browse the repository at this point in the history
Co-authored-by: joshieDo <[email protected]>
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2024
1 parent b5fce61 commit d2f494b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 92 deletions.
5 changes: 3 additions & 2 deletions crates/cli/commands/src/test_vectors/compact.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloy_eips::eip4895::Withdrawals;
use alloy_primitives::{hex, private::getrandom::getrandom, PrimitiveSignature, TxKind};
use arbitrary::Arbitrary;
use eyre::{Context, Result};
Expand All @@ -22,7 +23,7 @@ use reth_db::{
use reth_fs_util as fs;
use reth_primitives::{
Account, Log, LogData, Receipt, ReceiptWithBloom, StorageEntry, Transaction,
TransactionSignedNoHash, TxType, Withdrawals,
TransactionSignedNoHash, TxType,
};
use reth_prune_types::{PruneCheckpoint, PruneMode};
use reth_stages_types::{
Expand Down Expand Up @@ -75,14 +76,14 @@ compact_types!(
// reth-primitives
Account,
Receipt,
Withdrawals,
ReceiptWithBloom,
// reth_codecs::alloy
Authorization,
GenesisAccount,
Header,
HeaderExt,
Withdrawal,
Withdrawals,
TxEip2930,
TxEip1559,
TxEip4844,
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/state_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloy_eips::eip4895::Withdrawal;
use alloy_primitives::{map::HashMap, Address, U256};
use reth_chainspec::EthereumHardforks;
use reth_consensus_common::calc;
use reth_primitives::{Block, Withdrawals};
use reth_primitives::Block;

/// Collect all balance changes at the end of the block.
///
Expand Down Expand Up @@ -37,7 +37,7 @@ pub fn post_block_balance_increments<ChainSpec: EthereumHardforks>(
insert_post_block_withdrawals_balance_increments(
chain_spec,
block.timestamp,
block.body.withdrawals.as_ref().map(Withdrawals::as_ref),
block.body.withdrawals.as_ref().map(|w| w.as_slice()),
&mut balance_increments,
);

Expand Down
4 changes: 1 addition & 3 deletions crates/optimism/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod tests {
CompactClientVersion, CompactU256, CompactU64, StoredBlockBodyIndices, StoredBlockOmmers,
StoredBlockWithdrawals,
};
use reth_primitives::{Account, Receipt, ReceiptWithBloom, Withdrawals};
use reth_primitives::{Account, Receipt, ReceiptWithBloom};
use reth_prune_types::{PruneCheckpoint, PruneMode, PruneSegment};
use reth_stages_types::{
AccountHashingCheckpoint, CheckpointBlockRange, EntitiesCheckpoint, ExecutionCheckpoint,
Expand Down Expand Up @@ -47,7 +47,6 @@ mod tests {
assert_eq!(StoredBlockOmmers::bitflag_encoded_bytes(), 0);
assert_eq!(StoredBlockWithdrawals::bitflag_encoded_bytes(), 0);
assert_eq!(StorageHashingCheckpoint::bitflag_encoded_bytes(), 1);
assert_eq!(Withdrawals::bitflag_encoded_bytes(), 0);

// In case of failure, refer to the documentation of the
// [`validate_bitflag_backwards_compat`] macro for detailed instructions on handling
Expand All @@ -73,6 +72,5 @@ mod tests {
validate_bitflag_backwards_compat!(StoredBlockOmmers, UnusedBits::Zero);
validate_bitflag_backwards_compat!(StoredBlockWithdrawals, UnusedBits::Zero);
validate_bitflag_backwards_compat!(StorageHashingCheckpoint, UnusedBits::NotZero);
validate_bitflag_backwards_compat!(Withdrawals, UnusedBits::Zero);
}
}
2 changes: 1 addition & 1 deletion crates/primitives-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub mod block;
pub use block::{body::BlockBody, Block, FullBlock};

mod withdrawal;
pub use withdrawal::Withdrawals;
pub use withdrawal::{Withdrawal, Withdrawals};

mod error;
pub use error::{GotExpected, GotExpectedBoxed};
Expand Down
85 changes: 6 additions & 79 deletions crates/primitives-traits/src/withdrawal.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,11 @@
//! [EIP-4895](https://eips.ethereum.org/EIPS/eip-4895) Withdrawal types.
use alloc::vec::Vec;
use alloy_eips::eip4895::Withdrawal;
use alloy_rlp::{RlpDecodableWrapper, RlpEncodableWrapper};
use derive_more::{AsRef, Deref, DerefMut, From, IntoIterator};
use reth_codecs::{add_arbitrary_tests, Compact};
use serde::{Deserialize, Serialize};
/// Re-export from `alloy_eips`.
#[doc(inline)]
pub use alloy_eips::eip4895::Withdrawal;

/// Represents a collection of Withdrawals.
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Default,
Hash,
From,
AsRef,
Deref,
DerefMut,
IntoIterator,
RlpEncodableWrapper,
RlpDecodableWrapper,
Serialize,
Deserialize,
Compact,
)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[add_arbitrary_tests(compact)]
#[as_ref(forward)]
pub struct Withdrawals(Vec<Withdrawal>);

impl Withdrawals {
/// Create a new Withdrawals instance.
pub const fn new(withdrawals: Vec<Withdrawal>) -> Self {
Self(withdrawals)
}

/// Calculate the total size, including capacity, of the Withdrawals.
#[inline]
pub fn total_size(&self) -> usize {
self.capacity() * core::mem::size_of::<Withdrawal>()
}

/// Calculate a heuristic for the in-memory size of the [Withdrawals].
#[inline]
pub fn size(&self) -> usize {
self.len() * core::mem::size_of::<Withdrawal>()
}

/// Get an iterator over the Withdrawals.
pub fn iter(&self) -> core::slice::Iter<'_, Withdrawal> {
self.0.iter()
}

/// Get a mutable iterator over the Withdrawals.
pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, Withdrawal> {
self.0.iter_mut()
}

/// Convert [Self] into raw vec of withdrawals.
pub fn into_inner(self) -> Vec<Withdrawal> {
self.0
}
}

impl<'a> IntoIterator for &'a Withdrawals {
type Item = &'a Withdrawal;
type IntoIter = core::slice::Iter<'a, Withdrawal>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

impl<'a> IntoIterator for &'a mut Withdrawals {
type Item = &'a mut Withdrawal;
type IntoIter = core::slice::IterMut<'a, Withdrawal>;

fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
pub type Withdrawals = alloy_eips::eip4895::Withdrawals;

#[cfg(test)]
mod tests {
Expand All @@ -89,6 +14,8 @@ mod tests {
use alloy_rlp::{RlpDecodable, RlpEncodable};
use proptest::proptest;
use proptest_arbitrary_interop::arb;
use reth_codecs::{add_arbitrary_tests, Compact};
use serde::{Deserialize, Serialize};

/// This type is kept for compatibility tests after the codec support was added to alloy-eips
/// Withdrawal type natively
Expand Down
29 changes: 27 additions & 2 deletions crates/storage/codecs/src/alloy/withdrawal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Compact implementation for [`AlloyWithdrawal`]
use crate::Compact;
use alloy_eips::eip4895::Withdrawal as AlloyWithdrawal;
use alloc::vec::Vec;
use alloy_eips::eip4895::{Withdrawal as AlloyWithdrawal, Withdrawals};
use alloy_primitives::Address;
use reth_codecs_derive::add_arbitrary_tests;

Expand Down Expand Up @@ -53,6 +54,22 @@ impl Compact for AlloyWithdrawal {
}
}

impl Compact for Withdrawals {
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
self.as_ref().to_compact(buf)
}

fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) {
let (withdrawals, new_buf) = Vec::from_compact(buf, buf.len());
buf = new_buf;
let alloy_withdrawals = Self::new(withdrawals);
(alloy_withdrawals, buf)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -61,12 +78,20 @@ mod tests {

proptest! {
#[test]
fn roundtrip(withdrawal in arb::<AlloyWithdrawal>()) {
fn roundtrip_withdrawal(withdrawal in arb::<AlloyWithdrawal>()) {
let mut compacted_withdrawal = Vec::<u8>::new();
let len = withdrawal.to_compact(&mut compacted_withdrawal);
let (decoded, _) = AlloyWithdrawal::from_compact(&compacted_withdrawal, len);
assert_eq!(withdrawal, decoded)
}

#[test]
fn roundtrip_withdrawals(withdrawals in arb::<Withdrawals>()) {
let mut compacted_withdrawals = Vec::<u8>::new();
let len = withdrawals.to_compact(&mut compacted_withdrawals);
let (decoded, _) = Withdrawals::from_compact(&compacted_withdrawals, len);
assert_eq!(withdrawals, decoded);
}
}

// each value in the database has an extra field named flags that encodes metadata about other
Expand Down
4 changes: 1 addition & 3 deletions crates/storage/db-api/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ mod tests {
fn test_ensure_backwards_compatibility() {
use super::*;
use reth_codecs::{test_utils::UnusedBits, validate_bitflag_backwards_compat};
use reth_primitives::{Account, Receipt, ReceiptWithBloom, Withdrawals};
use reth_primitives::{Account, Receipt, ReceiptWithBloom};
use reth_prune_types::{PruneCheckpoint, PruneMode, PruneSegment};
use reth_stages_types::{
AccountHashingCheckpoint, CheckpointBlockRange, EntitiesCheckpoint,
Expand Down Expand Up @@ -341,7 +341,6 @@ mod tests {
assert_eq!(StoredBlockOmmers::bitflag_encoded_bytes(), 0);
assert_eq!(StoredBlockWithdrawals::bitflag_encoded_bytes(), 0);
assert_eq!(StorageHashingCheckpoint::bitflag_encoded_bytes(), 1);
assert_eq!(Withdrawals::bitflag_encoded_bytes(), 0);

validate_bitflag_backwards_compat!(Account, UnusedBits::NotZero);
validate_bitflag_backwards_compat!(AccountHashingCheckpoint, UnusedBits::NotZero);
Expand All @@ -364,6 +363,5 @@ mod tests {
validate_bitflag_backwards_compat!(StoredBlockOmmers, UnusedBits::Zero);
validate_bitflag_backwards_compat!(StoredBlockWithdrawals, UnusedBits::Zero);
validate_bitflag_backwards_compat!(StorageHashingCheckpoint, UnusedBits::NotZero);
validate_bitflag_backwards_compat!(Withdrawals, UnusedBits::Zero);
}
}

0 comments on commit d2f494b

Please sign in to comment.