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