mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
primitives: replace primitive Withdrawals with alloy (#12119)
Co-authored-by: joshieDo <93316087+joshieDo@users.noreply.github.com> Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
@ -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};
|
||||
@ -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::{
|
||||
@ -75,7 +76,6 @@ compact_types!(
|
||||
// reth-primitives
|
||||
Account,
|
||||
Receipt,
|
||||
Withdrawals,
|
||||
ReceiptWithBloom,
|
||||
// reth_codecs::alloy
|
||||
Authorization,
|
||||
@ -83,6 +83,7 @@ compact_types!(
|
||||
Header,
|
||||
HeaderExt,
|
||||
Withdrawal,
|
||||
Withdrawals,
|
||||
TxEip2930,
|
||||
TxEip1559,
|
||||
TxEip4844,
|
||||
|
||||
@ -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.
|
||||
///
|
||||
@ -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,
|
||||
);
|
||||
|
||||
|
||||
@ -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,
|
||||
@ -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
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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};
|
||||
|
||||
@ -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 {
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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::*;
|
||||
@ -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
|
||||
|
||||
@ -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,
|
||||
@ -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);
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user