Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(trie): introduce serde feature on reth-trie-common #12864

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/storage/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ reth-storage-errors.workspace = true
reth-nippy-jar.workspace = true
reth-prune-types.workspace = true
reth-stages-types.workspace = true
reth-trie-common = { workspace = true, features = ["serde"] }
reth-tracing.workspace = true
reth-trie-common.workspace = true

# ethereum
alloy-primitives.workspace = true
Expand Down
26 changes: 20 additions & 6 deletions crates/trie/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@ description = "Commonly used types for trie usage in reth."
workspace = true

[dependencies]
reth-primitives-traits = { workspace = true, features = ["serde"] }
reth-codecs.workspace = true

# alloy
alloy-primitives.workspace = true
alloy-rlp = { workspace = true, features = ["arrayvec"] }
alloy-trie = { workspace = true, features = ["serde"] }
alloy-trie.workspace = true
alloy-consensus.workspace = true
alloy-genesis.workspace = true

reth-primitives-traits.workspace = true
reth-codecs.workspace = true
revm-primitives.workspace = true

bytes.workspace = true
derive_more.workspace = true
serde.workspace = true
itertools.workspace = true
nybbles = { workspace = true, features = ["serde", "rlp"] }
nybbles = { workspace = true, features = ["rlp"] }

# `serde` feature
serde = { workspace = true, optional = true }

# `test-utils` feature
hash-db = { version = "=0.15.2", optional = true }
Expand All @@ -43,6 +46,17 @@ plain_hasher = "0.2"
serde_json.workspace = true

[features]
serde = [
"dep:serde",
"bytes/serde",
"nybbles/serde",
"alloy-primitives/serde",
"alloy-consensus/serde",
"alloy-trie/serde",
"revm-primitives/serde",
"reth-primitives-traits/serde",
"reth-codecs/serde"
]
test-utils = [
"dep:plain_hasher",
"dep:hash-db",
Expand Down
4 changes: 2 additions & 2 deletions crates/trie/common/src/hash_builder/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use alloy_trie::{hash_builder::HashBuilderValue, nodes::RlpNode, HashBuilder};
use bytes::Buf;
use nybbles::Nibbles;
use reth_codecs::Compact;
use serde::{Deserialize, Serialize};

/// The hash builder state for storing in the database.
/// Check the `reth-trie` crate for more info on hash builder.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "arbitrary",
derive(arbitrary::Arbitrary),
Expand Down
19 changes: 4 additions & 15 deletions crates/trie/common/src/nibbles.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
use bytes::Buf;
use derive_more::Deref;
use reth_codecs::Compact;
use serde::{Deserialize, Serialize};

pub use nybbles::Nibbles;

/// The representation of nibbles of the merkle trie stored in the database.
#[derive(
Clone,
Debug,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize,
derive_more::Index,
)]
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, derive_more::Index)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "test-utils", derive(arbitrary::Arbitrary))]
pub struct StoredNibbles(pub Nibbles);

Expand Down Expand Up @@ -74,7 +62,8 @@ impl Compact for StoredNibbles {
}

/// The representation of nibbles of the merkle trie stored in the database.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord, Hash, Deref)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deref)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "test-utils", derive(arbitrary::Arbitrary))]
pub struct StoredNibblesSubKey(pub Nibbles);

Expand Down
9 changes: 5 additions & 4 deletions crates/trie/common/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use alloy_trie::{
};
use itertools::Itertools;
use reth_primitives_traits::Account;
use serde::{Deserialize, Serialize};
use std::collections::{hash_map, HashMap};

/// The state multiproof of target accounts and multiproofs of their storage tries.
Expand Down Expand Up @@ -171,8 +170,9 @@ impl StorageMultiProof {
}

/// The merkle proof with the relevant account info.
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct AccountProof {
/// The address associated with the account.
pub address: Address,
Expand Down Expand Up @@ -227,7 +227,8 @@ impl AccountProof {
}

/// The merkle proof of the storage entry.
#[derive(Clone, PartialEq, Eq, Default, Debug, Serialize, Deserialize)]
#[derive(Clone, PartialEq, Eq, Default, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StorageProof {
/// The raw storage key.
pub key: B256,
Expand Down
4 changes: 2 additions & 2 deletions crates/trie/common/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::{BranchNodeCompact, StoredNibblesSubKey};
use reth_codecs::Compact;
use serde::{Deserialize, Serialize};

/// Account storage trie node.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StorageTrieEntry {
/// The nibbles of the intermediate node
pub nibbles: StoredNibblesSubKey,
Expand Down
2 changes: 1 addition & 1 deletion crates/trie/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ reth-execution-errors.workspace = true
reth-db.workspace = true
reth-db-api.workspace = true
reth-storage-errors.workspace = true
reth-trie-common.workspace = true
reth-trie.workspace = true

revm.workspace = true
Expand Down Expand Up @@ -70,6 +69,7 @@ serde = [
"dep:serde",
"reth-provider/serde",
"reth-trie/serde",
"reth-trie-common/serde",
"alloy-consensus/serde",
"alloy-primitives/serde",
"revm/serde",
Expand Down
6 changes: 4 additions & 2 deletions crates/trie/db/src/prefix_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use reth_db_api::{
DatabaseError,
};
use reth_primitives::StorageEntry;
use reth_trie::prefix_set::{PrefixSetMut, TriePrefixSets};
use reth_trie_common::Nibbles;
use reth_trie::{
prefix_set::{PrefixSetMut, TriePrefixSets},
Nibbles,
};
use std::{
collections::{HashMap, HashSet},
ops::RangeInclusive,
Expand Down
7 changes: 3 additions & 4 deletions crates/trie/db/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use reth_trie::{
hashed_cursor::HashedPostStateCursorFactory,
proof::{Proof, StorageProof},
trie_cursor::InMemoryTrieCursorFactory,
HashedPostStateSorted, HashedStorage, MultiProof, TrieInput,
AccountProof, HashedPostStateSorted, HashedStorage, MultiProof, TrieInput,
};
use reth_trie_common::AccountProof;

/// Extends [`Proof`] with operations specific for working with a database transaction.
pub trait DatabaseProof<'a, TX> {
Expand Down Expand Up @@ -96,7 +95,7 @@ pub trait DatabaseStorageProof<'a, TX> {
address: Address,
slot: B256,
storage: HashedStorage,
) -> Result<reth_trie_common::StorageProof, StateProofError>;
) -> Result<reth_trie::StorageProof, StateProofError>;
}

impl<'a, TX: DbTx> DatabaseStorageProof<'a, TX>
Expand All @@ -111,7 +110,7 @@ impl<'a, TX: DbTx> DatabaseStorageProof<'a, TX>
address: Address,
slot: B256,
storage: HashedStorage,
) -> Result<reth_trie_common::StorageProof, StateProofError> {
) -> Result<reth_trie::StorageProof, StateProofError> {
let hashed_address = keccak256(address);
let prefix_set = storage.construct_prefix_set();
let state_sorted = HashedPostStateSorted::new(
Expand Down
3 changes: 1 addition & 2 deletions crates/trie/db/src/trie_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use reth_storage_errors::db::DatabaseError;
use reth_trie::{
trie_cursor::{TrieCursor, TrieCursorFactory},
updates::StorageTrieUpdates,
BranchNodeCompact, Nibbles, StoredNibbles, StoredNibblesSubKey,
BranchNodeCompact, Nibbles, StorageTrieEntry, StoredNibbles, StoredNibblesSubKey,
};
use reth_trie_common::StorageTrieEntry;

/// Wrapper struct for database transaction implementing trie cursor factory trait.
#[derive(Debug)]
Expand Down
3 changes: 1 addition & 2 deletions crates/trie/db/tests/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use alloy_rlp::EMPTY_STRING_CODE;
use reth_chainspec::{Chain, ChainSpec, HOLESKY, MAINNET};
use reth_primitives::Account;
use reth_provider::test_utils::{create_test_provider_factory, insert_genesis};
use reth_trie::{proof::Proof, Nibbles};
use reth_trie_common::{AccountProof, StorageProof};
use reth_trie::{proof::Proof, AccountProof, Nibbles, StorageProof};
use reth_trie_db::DatabaseProof;
use std::{
str::FromStr,
Expand Down
2 changes: 1 addition & 1 deletion crates/trie/db/tests/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use reth_provider::{
use reth_trie::{
prefix_set::PrefixSetMut,
test_utils::{state_root, state_root_prehashed, storage_root, storage_root_prehashed},
triehash::KeccakHasher,
BranchNodeCompact, StateRoot, StorageRoot, TrieMask,
};
use reth_trie_common::triehash::KeccakHasher;
use reth_trie_db::{DatabaseStateRoot, DatabaseStorageRoot};
use std::{
collections::{BTreeMap, HashMap},
Expand Down
4 changes: 2 additions & 2 deletions crates/trie/db/tests/walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use reth_db::tables;
use reth_db_api::{cursor::DbCursorRW, transaction::DbTxMut};
use reth_provider::test_utils::create_test_provider_factory;
use reth_trie::{
prefix_set::PrefixSetMut, trie_cursor::TrieCursor, walker::TrieWalker, StorageTrieEntry,
prefix_set::PrefixSetMut, trie_cursor::TrieCursor, walker::TrieWalker, BranchNodeCompact,
Nibbles, StorageTrieEntry,
};
use reth_trie_common::{BranchNodeCompact, Nibbles};
use reth_trie_db::{DatabaseAccountTrieCursor, DatabaseStorageTrieCursor};

#[test]
Expand Down
3 changes: 2 additions & 1 deletion crates/trie/trie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ serde = [
"alloy-consensus/serde",
"alloy-primitives/serde",
"revm/serde",
"alloy-trie/serde"
"alloy-trie/serde",
"reth-trie-common/serde"
]
serde-bincode-compat = [
"serde_with",
Expand Down
Loading