Add Withdrawals struct (#6267)

This commit is contained in:
Thomas Coratger
2024-02-01 18:57:50 +01:00
committed by GitHub
parent cac6a5275a
commit 55fae2ca9c
28 changed files with 175 additions and 113 deletions

View File

@ -1,6 +1,6 @@
use crate::{
Address, GotExpected, Header, SealedHeader, TransactionSigned, TransactionSignedEcRecovered,
Withdrawal, B256,
Withdrawal, Withdrawals, B256,
};
use alloy_rlp::{RlpDecodable, RlpEncodable};
use reth_codecs::derive_arbitrary;
@ -27,7 +27,7 @@ pub struct Block {
/// Ommers/uncles header.
pub ommers: Vec<Header>,
/// Block withdrawals.
pub withdrawals: Option<Vec<Withdrawal>>,
pub withdrawals: Option<Withdrawals>,
}
impl Block {
@ -119,7 +119,7 @@ impl Block {
// take into account capacity
self.body.iter().map(TransactionSigned::size).sum::<usize>() + self.body.capacity() * std::mem::size_of::<TransactionSigned>() +
self.ommers.iter().map(Header::size).sum::<usize>() + self.ommers.capacity() * std::mem::size_of::<Header>() +
self.withdrawals.as_ref().map(|w| w.iter().map(Withdrawal::size).sum::<usize>() + w.capacity() * std::mem::size_of::<Withdrawal>()).unwrap_or(std::mem::size_of::<Option<Vec<Withdrawal>>>())
self.withdrawals.as_ref().map(|w| w.size() + w.capacity() * std::mem::size_of::<Withdrawal>()).unwrap_or(std::mem::size_of::<Option<Withdrawals>>())
}
}
@ -219,7 +219,7 @@ pub struct SealedBlock {
/// Ommer/uncle headers
pub ommers: Vec<Header>,
/// Block withdrawals.
pub withdrawals: Option<Vec<Withdrawal>>,
pub withdrawals: Option<Withdrawals>,
}
impl SealedBlock {
@ -316,7 +316,7 @@ impl SealedBlock {
// take into account capacity
self.body.iter().map(TransactionSigned::size).sum::<usize>() + self.body.capacity() * std::mem::size_of::<TransactionSigned>() +
self.ommers.iter().map(Header::size).sum::<usize>() + self.ommers.capacity() * std::mem::size_of::<Header>() +
self.withdrawals.as_ref().map(|w| w.iter().map(Withdrawal::size).sum::<usize>() + w.capacity() * std::mem::size_of::<Withdrawal>()).unwrap_or(std::mem::size_of::<Option<Vec<Withdrawal>>>())
self.withdrawals.as_ref().map(|w| w.size() + w.capacity() * std::mem::size_of::<Withdrawal>()).unwrap_or(std::mem::size_of::<Option<Withdrawals>>())
}
/// Calculates the total gas used by blob transactions in the sealed block.
@ -469,11 +469,9 @@ pub struct BlockBody {
/// Withdrawals in the block.
#[cfg_attr(
any(test, feature = "arbitrary"),
proptest(
strategy = "proptest::option::of(proptest::collection::vec(proptest::arbitrary::any::<Withdrawal>(), 0..=16))"
)
proptest(strategy = "proptest::option::of(proptest::arbitrary::any::<Withdrawals>())")
)]
pub withdrawals: Option<Vec<Withdrawal>>,
pub withdrawals: Option<Withdrawals>,
}
impl BlockBody {
@ -512,11 +510,8 @@ impl BlockBody {
self.ommers.capacity() * std::mem::size_of::<Header>() +
self.withdrawals
.as_ref()
.map(|w| {
w.iter().map(Withdrawal::size).sum::<usize>() +
w.capacity() * std::mem::size_of::<Withdrawal>()
})
.unwrap_or(std::mem::size_of::<Option<Vec<Withdrawal>>>())
.map(|w| w.size() + w.capacity() * std::mem::size_of::<Withdrawal>())
.unwrap_or(std::mem::size_of::<Option<Withdrawals>>())
}
}

View File

@ -92,7 +92,7 @@ pub use transaction::{
TxEip4844, TxHashOrNumber, TxLegacy, TxType, TxValue, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID,
EIP4844_TX_TYPE_ID, LEGACY_TX_TYPE_ID,
};
pub use withdrawal::Withdrawal;
pub use withdrawal::{Withdrawal, Withdrawals};
// Re-exports
pub use self::ruint::UintTryTo;

View File

@ -1,30 +1,27 @@
#![cfg(feature = "c-kzg")]
#![cfg_attr(docsrs, doc(cfg(feature = "c-kzg")))]
use crate::{
keccak256, Signature, Transaction, TransactionSigned, TxEip4844, TxHash, EIP4844_TX_TYPE_ID,
};
use crate::kzg::{
self, Blob, Bytes48, KzgSettings, BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_PROOF,
};
use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header};
use bytes::BufMut;
use serde::{Deserialize, Serialize};
#[cfg(any(test, feature = "arbitrary"))]
use proptest::{
arbitrary::{any as proptest_any, ParamsFor},
collection::vec as proptest_vec,
strategy::{BoxedStrategy, Strategy},
};
#[cfg(any(test, feature = "arbitrary"))]
use crate::{
constants::eip4844::{FIELD_ELEMENTS_PER_BLOB, MAINNET_KZG_TRUSTED_SETUP},
kzg::{KzgCommitment, KzgProof, BYTES_PER_FIELD_ELEMENT},
};
use crate::{
keccak256,
kzg::{
self, Blob, Bytes48, KzgSettings, BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_PROOF,
},
Signature, Transaction, TransactionSigned, TxEip4844, TxHash, EIP4844_TX_TYPE_ID,
};
use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header};
use bytes::BufMut;
#[cfg(any(test, feature = "arbitrary"))]
use proptest::{
arbitrary::{any as proptest_any, ParamsFor},
collection::vec as proptest_vec,
strategy::{BoxedStrategy, Strategy},
};
use serde::{Deserialize, Serialize};
/// An error that can occur when validating a [BlobTransaction].
#[derive(Debug, thiserror::Error)]

View File

@ -1,7 +1,10 @@
use crate::{constants::GWEI_TO_WEI, serde_helper::u64_hex, Address};
use alloy_rlp::{RlpDecodable, RlpEncodable};
use alloy_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper};
use reth_codecs::{main_codec, Compact};
use std::mem;
use std::{
mem,
ops::{Deref, DerefMut},
};
/// Withdrawal represents a validator withdrawal from the consensus layer.
#[main_codec]
@ -33,6 +36,57 @@ impl Withdrawal {
}
}
/// Represents a collection of Withdrawals.
#[main_codec]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, RlpEncodableWrapper, RlpDecodableWrapper)]
pub struct Withdrawals(Vec<Withdrawal>);
impl Withdrawals {
/// Create a new Withdrawals instance.
pub fn new(withdrawals: Vec<Withdrawal>) -> Self {
Withdrawals(withdrawals)
}
/// Calculate a heuristic for the in-memory size of the [Withdrawals].
#[inline]
pub fn size(&self) -> usize {
self.iter().map(Withdrawal::size).sum()
}
/// Get an iterator over the Withdrawals.
pub fn iter(&self) -> std::slice::Iter<'_, Withdrawal> {
self.0.iter()
}
/// Get a mutable iterator over the Withdrawals.
pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, Withdrawal> {
self.0.iter_mut()
}
}
impl IntoIterator for Withdrawals {
type Item = Withdrawal;
type IntoIter = std::vec::IntoIter<Withdrawal>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl Deref for Withdrawals {
type Target = Vec<Withdrawal>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Withdrawals {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[cfg(test)]
mod tests {
use super::*;