Skip to content

Commit

Permalink
feat: use derive more display (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell authored Oct 9, 2024
1 parent 19139b8 commit 0440b5f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions crates/mpt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ workspace = true
# General
tracing.workspace = true
thiserror.workspace = true
derive_more.workspace = true

# Revm + Alloy
revm.workspace = true
Expand Down
24 changes: 6 additions & 18 deletions crates/mpt/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,23 +61,26 @@ 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,
/// The value of the leaf node
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,
Expand All @@ -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<TrieNode>,
},
}

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.
///
Expand Down

0 comments on commit 0440b5f

Please sign in to comment.