-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
feat: introduce StateCommitment type #11842
Merged
joshieDo
merged 1 commit into
paradigmxyz:main
from
scroll-tech:feat/state-commitment-trait
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use alloy_primitives::B256; | ||
use revm_primitives::keccak256; | ||
|
||
/// Trait for hashing keys in state. | ||
pub trait KeyHasher: Default + Clone + Send + Sync + 'static { | ||
/// Hashes the given bytes into a 256-bit hash. | ||
fn hash_key<T: AsRef<[u8]>>(bytes: T) -> B256; | ||
} | ||
|
||
/// A key hasher that uses the Keccak-256 hash function. | ||
#[derive(Clone, Debug, Default)] | ||
pub struct KeccakKeyHasher; | ||
|
||
impl KeyHasher for KeccakKeyHasher { | ||
fn hash_key<T: AsRef<[u8]>>(bytes: T) -> B256 { | ||
keccak256(bytes) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::{ | ||
DatabaseHashedCursorFactory, DatabaseProof, DatabaseStateRoot, DatabaseStorageRoot, | ||
DatabaseTrieCursorFactory, DatabaseTrieWitness, | ||
}; | ||
use reth_db::transaction::DbTx; | ||
use reth_trie::{ | ||
proof::Proof, witness::TrieWitness, KeccakKeyHasher, KeyHasher, StateRoot, StorageRoot, | ||
}; | ||
|
||
/// The `StateCommitment` trait provides associated types for state commitment operations. | ||
pub trait StateCommitment: std::fmt::Debug + Send + Sync + Unpin + 'static { | ||
/// The state root type. | ||
type StateRoot<'a, TX: DbTx + 'a>: DatabaseStateRoot<'a, TX>; | ||
/// The storage root type. | ||
type StorageRoot<'a, TX: DbTx + 'a>: DatabaseStorageRoot<'a, TX>; | ||
/// The state proof type. | ||
type StateProof<'a, TX: DbTx + 'a>: DatabaseProof<'a, TX>; | ||
/// The state witness type. | ||
type StateWitness<'a, TX: DbTx + 'a>: DatabaseTrieWitness<'a, TX>; | ||
/// The key hasher type. | ||
type KeyHasher: KeyHasher; | ||
} | ||
|
||
/// The state commitment type for Ethereum's Merkle Patricia Trie. | ||
#[derive(Debug)] | ||
#[non_exhaustive] | ||
pub struct MerklePatriciaTrie; | ||
joshieDo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
impl StateCommitment for MerklePatriciaTrie { | ||
type StateRoot<'a, TX: DbTx + 'a> = | ||
StateRoot<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>; | ||
type StorageRoot<'a, TX: DbTx + 'a> = | ||
StorageRoot<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>; | ||
type StateProof<'a, TX: DbTx + 'a> = | ||
Proof<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>; | ||
type StateWitness<'a, TX: DbTx + 'a> = | ||
TrieWitness<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>; | ||
type KeyHasher = KeccakKeyHasher; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused as to why these require
DbTx
bounds? Is it just because these must implement theDatabase*
traits?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a consequence of the impl of
DatabaseStateRoot
onStateRoot
. As we see below we have aDbTx
bound onTX
which is required for certain methods used in the implementation.reth/crates/trie/db/src/state.rs
Lines 132 to 133 in 51594c9
As a consequence of this in the implementation of
StateCommitment
for theMerklePatriciaTrie
we must also have aDbTx
bound on theStateRoot
type as seen below:Rust does not allow for an impl of a trait to have more strict bounds than the trait itself and therefore me must propagate the bound to the trait itself. The above rationale is also applicable for the other types in
StateCommitment
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just added this issue: #12216
note that it should not block any of this work, but eventually these traits could be simplified