Implement Compact for Withdrawal (#7604)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
jn
2024-04-23 02:15:53 -07:00
committed by GitHub
parent a2027c817f
commit b9db4cb61b
3 changed files with 69 additions and 3 deletions

View File

@ -11,18 +11,21 @@ repository.workspace = true
workspace = true workspace = true
[dependencies] [dependencies]
# reth
reth-codecs-derive = { path = "./derive", default-features = false } reth-codecs-derive = { path = "./derive", default-features = false }
# eth
alloy-eips = { workspace = true, optional = true } alloy-eips = { workspace = true, optional = true }
alloy-primitives.workspace = true alloy-primitives.workspace = true
# misc
bytes.workspace = true bytes.workspace = true
modular-bitfield = { workspace = true, optional = true }
serde.workspace = true
[dev-dependencies] [dev-dependencies]
alloy-eips = { workspace = true, default-features = false, features = ["arbitrary", "serde"] } alloy-eips = { workspace = true, default-features = false, features = ["arbitrary", "serde"] }
alloy-primitives = { workspace = true, features = ["arbitrary", "serde"] } alloy-primitives = { workspace = true, features = ["arbitrary", "serde"] }
serde.workspace = true
modular-bitfield.workspace = true
test-fuzz.workspace = true test-fuzz.workspace = true
serde_json.workspace = true serde_json.workspace = true
@ -33,5 +36,5 @@ proptest-derive.workspace = true
[features] [features]
default = ["std", "alloy"] default = ["std", "alloy"]
std = ["alloy-primitives/std", "bytes/std"] std = ["alloy-primitives/std", "bytes/std"]
alloy = ["alloy-eips"] alloy = ["dep:alloy-eips", "dep:modular-bitfield"]
optimism = ["reth-codecs-derive/optimism"] optimism = ["reth-codecs-derive/optimism"]

View File

@ -1,3 +1,4 @@
mod access_list; mod access_list;
mod log; mod log;
mod txkind; mod txkind;
mod withdrawal;

View File

@ -0,0 +1,62 @@
use crate::Compact;
use alloy_eips::eip4895::Withdrawal as AlloyWithdrawal;
use alloy_primitives::Address;
use reth_codecs_derive::main_codec;
/// Withdrawal acts as bridge which simplifies Compact implementation for AlloyWithdrawal.
///
/// Notice: Make sure this struct is 1:1 with `alloy_eips::eip4895::Withdrawal`
#[main_codec]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
struct Withdrawal {
/// Monotonically increasing identifier issued by consensus layer.
index: u64,
/// Index of validator associated with withdrawal.
validator_index: u64,
/// Target address for withdrawn ether.
address: Address,
/// Value of the withdrawal in gwei.
amount: u64,
}
impl Compact for AlloyWithdrawal {
fn to_compact<B>(self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
let withdrawal = Withdrawal {
index: self.index,
validator_index: self.validator_index,
address: self.address,
amount: self.amount,
};
withdrawal.to_compact(buf)
}
fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
let (withdrawal, _) = Withdrawal::from_compact(buf, len);
let alloy_withdrawal = AlloyWithdrawal {
index: withdrawal.index,
validator_index: withdrawal.validator_index,
address: withdrawal.address,
amount: withdrawal.amount,
};
(alloy_withdrawal, buf)
}
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::proptest;
proptest! {
#[test]
fn roundtrip(withdrawal: 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)
}
}
}