Skip to content

Commit 129f3ba

Browse files
authored
feat: introduce StateCommitment type (#11842)
1 parent 734c78f commit 129f3ba

File tree

18 files changed

+120
-15
lines changed

18 files changed

+120
-15
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ethereum/node/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ reth-node-api.workspace = true
3131
reth-chainspec.workspace = true
3232
reth-primitives.workspace = true
3333
reth-revm = { workspace = true, features = ["std"] }
34+
reth-trie-db.workspace = true
3435

3536
# revm with required ethereum features
3637
revm = { workspace = true, features = ["secp256k1", "blst", "c-kzg"] }
@@ -72,6 +73,7 @@ test-utils = [
7273
"reth-db/test-utils",
7374
"reth-provider/test-utils",
7475
"reth-transaction-pool/test-utils",
76+
"reth-trie-db/test-utils",
7577
"revm/test-utils",
7678
"reth-evm/test-utils"
7779
]

crates/ethereum/node/src/node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use reth_transaction_pool::{
3434
blobstore::DiskFileBlobStore, EthTransactionPool, TransactionPool,
3535
TransactionValidationTaskExecutor,
3636
};
37+
use reth_trie_db::MerklePatriciaTrie;
3738

3839
use crate::{EthEngineTypes, EthEvmConfig};
3940

@@ -81,6 +82,7 @@ impl EthereumNode {
8182
impl NodeTypes for EthereumNode {
8283
type Primitives = EthPrimitives;
8384
type ChainSpec = ChainSpec;
85+
type StateCommitment = MerklePatriciaTrie;
8486
}
8587

8688
impl NodeTypesWithEngine for EthereumNode {

crates/exex/test-utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ reth-primitives.workspace = true
3131
reth-provider = { workspace = true, features = ["test-utils"] }
3232
reth-tasks.workspace = true
3333
reth-transaction-pool = { workspace = true, features = ["test-utils"] }
34+
reth-trie-db.workspace = true
3435

3536
## async
3637
futures-util.workspace = true

crates/exex/test-utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub struct TestNode;
119119
impl NodeTypes for TestNode {
120120
type Primitives = ();
121121
type ChainSpec = ChainSpec;
122+
type StateCommitment = reth_trie_db::MerklePatriciaTrie;
122123
}
123124

124125
impl NodeTypesWithEngine for TestNode {

crates/node/builder/src/node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ where
6969
type Primitives = <N::Types as NodeTypes>::Primitives;
7070

7171
type ChainSpec = <N::Types as NodeTypes>::ChainSpec;
72+
73+
type StateCommitment = <N::Types as NodeTypes>::StateCommitment;
7274
}
7375

7476
impl<N, C, AO> NodeTypesWithEngine for AnyNode<N, C, AO>

crates/node/types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ reth-db-api.workspace = true
1717
reth-engine-primitives.workspace = true
1818
reth-primitives.workspace = true
1919
reth-primitives-traits.workspace = true
20+
reth-trie-db.workspace = true

crates/node/types/src/lib.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use reth_db_api::{
1818
Database,
1919
};
2020
use reth_engine_primitives::EngineTypes;
21+
use reth_trie_db::StateCommitment;
2122

2223
/// Configures all the primitive types of the node.
2324
pub trait NodePrimitives {
@@ -39,6 +40,8 @@ pub trait NodeTypes: Send + Sync + Unpin + 'static {
3940
type Primitives: NodePrimitives;
4041
/// The type used for configuration of the EVM.
4142
type ChainSpec: EthChainSpec;
43+
/// The type used to perform state commitment operations.
44+
type StateCommitment: StateCommitment;
4245
}
4346

4447
/// The type that configures an Ethereum-like node with an engine for consensus.
@@ -89,6 +92,7 @@ where
8992
{
9093
type Primitives = Types::Primitives;
9194
type ChainSpec = Types::ChainSpec;
95+
type StateCommitment = Types::StateCommitment;
9296
}
9397

9498
impl<Types, DB> NodeTypesWithEngine for NodeTypesWithDBAdapter<Types, DB>
@@ -109,70 +113,85 @@ where
109113

110114
/// A [`NodeTypes`] type builder.
111115
#[derive(Default, Debug)]
112-
pub struct AnyNodeTypes<P = (), C = ()>(PhantomData<P>, PhantomData<C>);
116+
pub struct AnyNodeTypes<P = (), C = (), S = ()>(PhantomData<P>, PhantomData<C>, PhantomData<S>);
113117

114-
impl<P, C> AnyNodeTypes<P, C> {
118+
impl<P, C, S> AnyNodeTypes<P, C, S> {
115119
/// Sets the `Primitives` associated type.
116-
pub const fn primitives<T>(self) -> AnyNodeTypes<T, C> {
117-
AnyNodeTypes::<T, C>(PhantomData::<T>, PhantomData::<C>)
120+
pub const fn primitives<T>(self) -> AnyNodeTypes<T, C, S> {
121+
AnyNodeTypes::<T, C, S>(PhantomData::<T>, PhantomData::<C>, PhantomData::<S>)
118122
}
119123

120124
/// Sets the `ChainSpec` associated type.
121-
pub const fn chain_spec<T>(self) -> AnyNodeTypes<P, T> {
122-
AnyNodeTypes::<P, T>(PhantomData::<P>, PhantomData::<T>)
125+
pub const fn chain_spec<T>(self) -> AnyNodeTypes<P, T, S> {
126+
AnyNodeTypes::<P, T, S>(PhantomData::<P>, PhantomData::<T>, PhantomData::<S>)
127+
}
128+
129+
/// Sets the `StateCommitment` associated type.
130+
pub const fn state_commitment<T>(self) -> AnyNodeTypes<P, C, T> {
131+
AnyNodeTypes::<P, C, T>(PhantomData::<P>, PhantomData::<C>, PhantomData::<T>)
123132
}
124133
}
125134

126-
impl<P, C> NodeTypes for AnyNodeTypes<P, C>
135+
impl<P, C, S> NodeTypes for AnyNodeTypes<P, C, S>
127136
where
128137
P: NodePrimitives + Send + Sync + Unpin + 'static,
129138
C: EthChainSpec + 'static,
139+
S: StateCommitment,
130140
{
131141
type Primitives = P;
132142
type ChainSpec = C;
143+
type StateCommitment = S;
133144
}
134145

135146
/// A [`NodeTypesWithEngine`] type builder.
136147
#[derive(Default, Debug)]
137-
pub struct AnyNodeTypesWithEngine<P = (), E = (), C = ()> {
148+
pub struct AnyNodeTypesWithEngine<P = (), E = (), C = (), S = ()> {
138149
/// Embedding the basic node types.
139-
base: AnyNodeTypes<P, C>,
150+
base: AnyNodeTypes<P, C, S>,
140151
/// Phantom data for the engine.
141152
_engine: PhantomData<E>,
142153
}
143154

144-
impl<P, E, C> AnyNodeTypesWithEngine<P, E, C> {
155+
impl<P, E, C, S> AnyNodeTypesWithEngine<P, E, C, S> {
145156
/// Sets the `Primitives` associated type.
146-
pub const fn primitives<T>(self) -> AnyNodeTypesWithEngine<T, E, C> {
157+
pub const fn primitives<T>(self) -> AnyNodeTypesWithEngine<T, E, C, S> {
147158
AnyNodeTypesWithEngine { base: self.base.primitives::<T>(), _engine: PhantomData }
148159
}
149160

150161
/// Sets the `Engine` associated type.
151-
pub const fn engine<T>(self) -> AnyNodeTypesWithEngine<P, T, C> {
162+
pub const fn engine<T>(self) -> AnyNodeTypesWithEngine<P, T, C, S> {
152163
AnyNodeTypesWithEngine { base: self.base, _engine: PhantomData::<T> }
153164
}
154165

155166
/// Sets the `ChainSpec` associated type.
156-
pub const fn chain_spec<T>(self) -> AnyNodeTypesWithEngine<P, E, T> {
167+
pub const fn chain_spec<T>(self) -> AnyNodeTypesWithEngine<P, E, T, S> {
157168
AnyNodeTypesWithEngine { base: self.base.chain_spec::<T>(), _engine: PhantomData }
158169
}
170+
171+
/// Sets the `StateCommitment` associated type.
172+
pub const fn state_commitment<T>(self) -> AnyNodeTypesWithEngine<P, E, C, T> {
173+
AnyNodeTypesWithEngine { base: self.base.state_commitment::<T>(), _engine: PhantomData }
174+
}
159175
}
160176

161-
impl<P, E, C> NodeTypes for AnyNodeTypesWithEngine<P, E, C>
177+
impl<P, E, C, S> NodeTypes for AnyNodeTypesWithEngine<P, E, C, S>
162178
where
163179
P: NodePrimitives + Send + Sync + Unpin + 'static,
164180
E: EngineTypes + Send + Sync + Unpin,
165181
C: EthChainSpec + 'static,
182+
S: StateCommitment,
166183
{
167184
type Primitives = P;
168185
type ChainSpec = C;
186+
type StateCommitment = S;
169187
}
170188

171-
impl<P, E, C> NodeTypesWithEngine for AnyNodeTypesWithEngine<P, E, C>
189+
impl<P, E, C, S> NodeTypesWithEngine for AnyNodeTypesWithEngine<P, E, C, S>
172190
where
173191
P: NodePrimitives + Send + Sync + Unpin + 'static,
174192
E: EngineTypes + Send + Sync + Unpin,
175193
C: EthChainSpec + 'static,
194+
S: StateCommitment,
176195
{
177196
type Engine = E;
178197
}

crates/optimism/node/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ reth-network.workspace = true
2828
reth-evm.workspace = true
2929
reth-revm = { workspace = true, features = ["std"] }
3030
reth-beacon-consensus.workspace = true
31+
reth-trie-db.workspace = true
3132

3233
# op-reth
3334
reth-optimism-payload-builder.workspace = true
@@ -99,5 +100,6 @@ test-utils = [
99100
"reth-db/test-utils",
100101
"reth-provider/test-utils",
101102
"reth-transaction-pool/test-utils",
103+
"reth-trie-db/test-utils",
102104
"revm/test-utils"
103105
]

crates/optimism/node/src/node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use reth_transaction_pool::{
3030
blobstore::DiskFileBlobStore, CoinbaseTipOrdering, TransactionPool,
3131
TransactionValidationTaskExecutor,
3232
};
33+
use reth_trie_db::MerklePatriciaTrie;
3334

3435
use crate::{
3536
args::RollupArgs,
@@ -122,6 +123,7 @@ where
122123
impl NodeTypes for OptimismNode {
123124
type Primitives = OpPrimitives;
124125
type ChainSpec = OpChainSpec;
126+
type StateCommitment = MerklePatriciaTrie;
125127
}
126128

127129
impl NodeTypesWithEngine for OptimismNode {

crates/storage/provider/src/test_utils/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use reth_trie::{
3434
updates::TrieUpdates, AccountProof, HashedPostState, HashedStorage, MultiProof, StorageProof,
3535
TrieInput,
3636
};
37+
use reth_trie_db::MerklePatriciaTrie;
3738
use revm::primitives::{BlockEnv, CfgEnvWithHandlerCfg};
3839
use std::{
3940
collections::BTreeMap,
@@ -157,6 +158,7 @@ pub struct MockNode;
157158
impl NodeTypes for MockNode {
158159
type Primitives = ();
159160
type ChainSpec = ChainSpec;
161+
type StateCommitment = MerklePatriciaTrie;
160162
}
161163

162164
impl DatabaseProviderFactory for MockEthProvider {

crates/storage/provider/src/test_utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub type MockNodeTypes = reth_node_types::AnyNodeTypesWithEngine<
2525
(),
2626
reth_ethereum_engine_primitives::EthEngineTypes,
2727
reth_chainspec::ChainSpec,
28+
reth_trie_db::MerklePatriciaTrie,
2829
>;
2930

3031
/// Mock [`reth_node_types::NodeTypesWithDB`] for testing.

crates/trie/common/src/key.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use alloy_primitives::B256;
2+
use revm_primitives::keccak256;
3+
4+
/// Trait for hashing keys in state.
5+
pub trait KeyHasher: Default + Clone + Send + Sync + 'static {
6+
/// Hashes the given bytes into a 256-bit hash.
7+
fn hash_key<T: AsRef<[u8]>>(bytes: T) -> B256;
8+
}
9+
10+
/// A key hasher that uses the Keccak-256 hash function.
11+
#[derive(Clone, Debug, Default)]
12+
pub struct KeccakKeyHasher;
13+
14+
impl KeyHasher for KeccakKeyHasher {
15+
fn hash_key<T: AsRef<[u8]>>(bytes: T) -> B256 {
16+
keccak256(bytes)
17+
}
18+
}

crates/trie/common/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub mod hash_builder;
1414
mod account;
1515
pub use account::TrieAccount;
1616

17+
mod key;
18+
pub use key::{KeccakKeyHasher, KeyHasher};
19+
1720
mod nibbles;
1821
pub use nibbles::{Nibbles, StoredNibbles, StoredNibblesSubKey};
1922

crates/trie/db/src/commitment.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use crate::{
2+
DatabaseHashedCursorFactory, DatabaseProof, DatabaseStateRoot, DatabaseStorageRoot,
3+
DatabaseTrieCursorFactory, DatabaseTrieWitness,
4+
};
5+
use reth_db::transaction::DbTx;
6+
use reth_trie::{
7+
proof::Proof, witness::TrieWitness, KeccakKeyHasher, KeyHasher, StateRoot, StorageRoot,
8+
};
9+
10+
/// The `StateCommitment` trait provides associated types for state commitment operations.
11+
pub trait StateCommitment: std::fmt::Debug + Send + Sync + Unpin + 'static {
12+
/// The state root type.
13+
type StateRoot<'a, TX: DbTx + 'a>: DatabaseStateRoot<'a, TX>;
14+
/// The storage root type.
15+
type StorageRoot<'a, TX: DbTx + 'a>: DatabaseStorageRoot<'a, TX>;
16+
/// The state proof type.
17+
type StateProof<'a, TX: DbTx + 'a>: DatabaseProof<'a, TX>;
18+
/// The state witness type.
19+
type StateWitness<'a, TX: DbTx + 'a>: DatabaseTrieWitness<'a, TX>;
20+
/// The key hasher type.
21+
type KeyHasher: KeyHasher;
22+
}
23+
24+
/// The state commitment type for Ethereum's Merkle Patricia Trie.
25+
#[derive(Debug)]
26+
#[non_exhaustive]
27+
pub struct MerklePatriciaTrie;
28+
29+
impl StateCommitment for MerklePatriciaTrie {
30+
type StateRoot<'a, TX: DbTx + 'a> =
31+
StateRoot<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>;
32+
type StorageRoot<'a, TX: DbTx + 'a> =
33+
StorageRoot<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>;
34+
type StateProof<'a, TX: DbTx + 'a> =
35+
Proof<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>;
36+
type StateWitness<'a, TX: DbTx + 'a> =
37+
TrieWitness<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>;
38+
type KeyHasher = KeccakKeyHasher;
39+
}

crates/trie/db/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! An integration of [`reth-trie`] with [`reth-db`].
22
3+
mod commitment;
34
mod hashed_cursor;
45
mod prefix_set;
56
mod proof;
@@ -8,6 +9,7 @@ mod storage;
89
mod trie_cursor;
910
mod witness;
1011

12+
pub use commitment::{MerklePatriciaTrie, StateCommitment};
1113
pub use hashed_cursor::{
1214
DatabaseHashedAccountCursor, DatabaseHashedCursorFactory, DatabaseHashedStorageCursor,
1315
};

examples/custom-engine-types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ reth-basic-payload-builder.workspace = true
1616
reth-ethereum-payload-builder.workspace = true
1717
reth-node-ethereum = { workspace = true, features = ["test-utils"] }
1818
reth-tracing.workspace = true
19+
reth-trie-db.workspace = true
1920
alloy-genesis.workspace = true
2021
alloy-rpc-types = { workspace = true, features = ["engine"] }
2122
alloy-primitives.workspace = true

examples/custom-engine-types/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ use reth_payload_builder::{
7070
};
7171
use reth_primitives::Withdrawals;
7272
use reth_tracing::{RethTracer, Tracer};
73+
use reth_trie_db::MerklePatriciaTrie;
7374

7475
/// A custom payload attributes type.
7576
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -228,6 +229,7 @@ struct MyCustomNode;
228229
impl NodeTypes for MyCustomNode {
229230
type Primitives = ();
230231
type ChainSpec = ChainSpec;
232+
type StateCommitment = MerklePatriciaTrie;
231233
}
232234

233235
/// Configure the node types with the custom engine types

0 commit comments

Comments
 (0)