From fb5cf987ee9162152bb3bc7aabea9e67bc114424 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:55:09 +0100 Subject: [PATCH] feat: rlp --- Cargo.toml | 8 ++++++-- src/chain.rs | 31 +++++++++++-------------------- src/named.rs | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a61c95d..cabb355 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,9 +16,12 @@ strum = { version = "0.25", default-features = false, features = ["derive"] } # serde serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } +# rlp +alloy-rlp = { version = "0.3", default-features = false, features = ["derive"], optional = true } + # arbitrary -arbitrary = { version = "1.3.2", optional = true } -proptest = { version = "1.4", optional = true } +arbitrary = { version = "1.3.2", default-features = false, optional = true } +proptest = { version = "1.4", default-features = false, features = ["alloc"], optional = true } [dev-dependencies] serde_json = { version = "1.0", default-features = false, features = ["alloc"] } @@ -27,4 +30,5 @@ serde_json = { version = "1.0", default-features = false, features = ["alloc"] } default = ["std"] std = ["strum/std", "serde?/std"] serde = ["dep:serde"] +rlp = ["dep:alloy-rlp"] arbitrary = ["dep:arbitrary", "dep:proptest"] diff --git a/src/chain.rs b/src/chain.rs index c5a3495..f29f5e2 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -176,24 +176,24 @@ impl fmt::Display for Chain { } } -#[cfg(TODO)] +#[cfg(feature = "rlp")] impl alloy_rlp::Encodable for Chain { fn encode(&self, out: &mut dyn alloy_rlp::BufMut) { match self { - Self::Named(chain) => u64::from(*chain).encode(out), + Self::Named(chain) => chain.encode(out), Self::Id(id) => id.encode(out), } } fn length(&self) -> usize { match self { - Self::Named(chain) => u64::from(*chain).length(), + Self::Named(chain) => chain.length(), Self::Id(id) => id.length(), } } } -#[cfg(TODO)] +#[cfg(feature = "rlp")] impl alloy_rlp::Decodable for Chain { fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { Ok(u64::decode(buf)?.into()) @@ -236,41 +236,34 @@ mod tests { #[test] fn test_id() { - let chain = Chain::Id(1234); - assert_eq!(chain.id(), 1234); + assert_eq!(Chain::Id(1234).id(), 1234); } #[test] fn test_named_id() { - let chain = Chain::Named(NamedChain::Goerli); - assert_eq!(chain.id(), 5); + assert_eq!(Chain::Named(NamedChain::Goerli).id(), 5); } #[test] fn test_display_named_chain() { - let chain = Chain::Named(NamedChain::Mainnet); - assert_eq!(format!("{chain}"), "mainnet"); + assert_eq!(Chain::Named(NamedChain::Mainnet).to_string(), "mainnet"); } #[test] fn test_display_id_chain() { - let chain = Chain::Id(1234); - assert_eq!(format!("{chain}"), "1234"); + assert_eq!(Chain::Id(1234).to_string(), "1234"); } #[test] fn test_from_str_named_chain() { let result = Chain::from_str("mainnet"); let expected = Chain::Named(NamedChain::Mainnet); - - assert!(result.is_ok()); assert_eq!(result.unwrap(), expected); } #[test] fn test_from_str_named_chain_error() { let result = Chain::from_str("chain"); - assert!(result.is_err()); } @@ -278,8 +271,6 @@ mod tests { fn test_from_str_id_chain() { let result = Chain::from_str("1234"); let expected = Chain::Id(1234); - - assert!(result.is_ok()); assert_eq!(result.unwrap(), expected); } @@ -287,15 +278,15 @@ mod tests { fn test_default() { let default = Chain::default(); let expected = Chain::Named(NamedChain::Mainnet); - assert_eq!(default, expected); } - #[cfg(TODO)] + #[cfg(feature = "rlp")] #[test] fn test_id_chain_encodable_length() { - let chain = Chain::Id(1234); + use alloy_rlp::Encodable; + let chain = Chain::Id(1234); assert_eq!(chain.length(), 3); } diff --git a/src/named.rs b/src/named.rs index 763a3bf..6ff20f2 100644 --- a/src/named.rs +++ b/src/named.rs @@ -199,6 +199,28 @@ impl serde::Serialize for NamedChain { } } +#[cfg(feature = "rlp")] +impl alloy_rlp::Encodable for NamedChain { + #[inline] + fn encode(&self, out: &mut dyn alloy_rlp::BufMut) { + (*self as u64).encode(out) + } + + #[inline] + fn length(&self) -> usize { + (*self as u64).length() + } +} + +#[cfg(feature = "rlp")] +impl alloy_rlp::Decodable for NamedChain { + #[inline] + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + let n = u64::decode(buf)?; + Self::try_from(n).map_err(|_| alloy_rlp::Error::Overflow) + } +} + // NB: all utility functions *should* be explicitly exhaustive (not use `_` matcher) so we don't // forget to update them when adding a new `NamedChain` variant. #[allow(clippy::match_like_matches_macro)]