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:
Thomas Coratger
2024-11-09 08:55:06 +01:00
committed by GitHub
parent b5fce61738
commit d2f494bd88
7 changed files with 41 additions and 92 deletions

View File

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

View File

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