From 2a1c5eaaa7079463afeeef125d0fee735b5f5ab1 Mon Sep 17 00:00:00 2001 From: refcell Date: Wed, 9 Oct 2024 14:07:18 -0400 Subject: [PATCH] feat: use derive more display --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 1 + crates/mpt/Cargo.toml | 1 + crates/mpt/src/node.rs | 24 ++++++------------------ 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8dc42ceb8..3487a95b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1104,6 +1104,15 @@ version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -1297,6 +1306,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" dependencies = [ + "convert_case", "proc-macro2", "quote", "syn 2.0.79", @@ -2175,6 +2185,7 @@ dependencies = [ "alloy-trie", "anyhow", "criterion", + "derive_more", "futures", "pprof", "proptest", @@ -4384,6 +4395,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + [[package]] name = "unicode-xid" version = "0.2.6" diff --git a/Cargo.toml b/Cargo.toml index d38ff4c90..3f0649558 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,6 +88,7 @@ c-kzg = { version = "1.0.3", default-features = false } alloc-no-stdlib = "2.0.4" linked_list_allocator = "0.10.5" command-fds = { version = "0.3", features = ["tokio"] } +derive_more = { version = "1.0.0", default-features = false, features = ["full"] } # Tracing tracing = { version = "0.1.40", default-features = false } diff --git a/crates/mpt/Cargo.toml b/crates/mpt/Cargo.toml index ba11ca730..e518db7f3 100644 --- a/crates/mpt/Cargo.toml +++ b/crates/mpt/Cargo.toml @@ -15,6 +15,7 @@ workspace = true # General tracing.workspace = true thiserror.workspace = true +derive_more.workspace = true # Revm + Alloy revm.workspace = true diff --git a/crates/mpt/src/node.rs b/crates/mpt/src/node.rs index 4bf487107..6e4ad49d5 100644 --- a/crates/mpt/src/node.rs +++ b/crates/mpt/src/node.rs @@ -10,7 +10,7 @@ use alloc::{boxed::Box, string::ToString, vec, vec::Vec}; use alloy_primitives::{hex, keccak256, Bytes, B256}; use alloy_rlp::{length_of_length, Buf, Decodable, Encodable, Header, EMPTY_STRING_CODE}; use alloy_trie::{Nibbles, EMPTY_ROOT_HASH}; -use core::fmt::Display; +use derive_more::Display; /// The length of the branch list when RLP encoded const BRANCH_LIST_LENGTH: usize = 17; @@ -61,16 +61,18 @@ const NIBBLE_WIDTH: usize = 4; /// As this implementation only supports uniform key sizes, the [TrieNode] data structure will fail /// to behave correctly if confronted with keys of varying lengths. Namely, this is because it does /// not support the `value` field in branch nodes, just like the Ethereum Merkle Patricia Trie. -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq, Display)] pub enum TrieNode { /// An empty [TrieNode] is represented as an [EMPTY_STRING_CODE] (0x80). Empty, /// A blinded node is a node that has been blinded by a [keccak256] commitment. + #[display("Blinded({commitment})")] Blinded { /// The commitment that blinds the node. commitment: B256, }, /// A leaf node is a 2-item node with the encoding `rlp([encoded_path, value])` + #[display("Leaf({}, {})", hex::encode(prefix.as_ref()), hex::encode(value.as_ref()))] Leaf { /// The key of the leaf node prefix: Nibbles, @@ -78,6 +80,7 @@ pub enum TrieNode { value: Bytes, }, /// An extension node is a 2-item pointer node with the encoding `rlp([encoded_path, key])` + #[display("Extension({}, {})", hex::encode(prefix.as_ref()), node)] Extension { /// The path prefix of the extension prefix: Nibbles, @@ -86,28 +89,13 @@ pub enum TrieNode { }, /// A branch node refers to up to 16 child nodes with the encoding /// `rlp([ v0, ..., v15, value ])` + #[display("Branch")] Branch { /// The 16 child nodes and value of the branch. stack: Vec, }, } -impl Display for TrieNode { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self { - Self::Empty => write!(f, "Empty"), - Self::Blinded { commitment } => write!(f, "Blinded({})", commitment), - Self::Leaf { prefix, value } => { - write!(f, "Leaf({}, {})", hex::encode(prefix.as_ref()), hex::encode(value.as_ref())) - } - Self::Extension { prefix, node } => { - write!(f, "Extension({}, {})", hex::encode(prefix.as_ref()), node) - } - Self::Branch { .. } => write!(f, "Branch"), - } - } -} - impl TrieNode { /// Creates a new [TrieNode::Blinded] node. ///