Skip to content

Commit

Permalink
feat(verkle): extract state root from the proof
Browse files Browse the repository at this point in the history
  • Loading branch information
morph-dev committed Aug 7, 2024
1 parent a28cc44 commit 5d53166
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion ethportal-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ keccak-hash = "0.10.0"
lazy_static = "1.4.0"
nanotemplate = "0.3.0"
once_cell = "1.17"
portal-verkle-primitives = { git = "https://github.com/morph-dev/portal-verkle-primitives.git", rev = "65ce73011fd2636733271219057c9dc194800ded" }
portal-verkle-primitives = { git = "https://github.com/morph-dev/portal-verkle-primitives.git", rev = "244a975baca2af42d4a596f7f6f83bc26c35223b" }
quickcheck = "1.0.3"
rand = "0.8.5"
reth-rpc-types = { rev = "8d1d13ef89cf19459adc37ba0c45e7aac6270dc1", git = "https://github.com/paradigmxyz/reth.git"}
Expand Down
2 changes: 1 addition & 1 deletion trin-verkle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ discv5 = { version = "0.4.1", features = ["serde"] }
ethportal-api = { path = "../ethportal-api" }
parking_lot = "0.11.2"
portalnet = { path = "../portalnet" }
portal-verkle-primitives = { git = "https://github.com/morph-dev/portal-verkle-primitives.git", rev = "65ce73011fd2636733271219057c9dc194800ded" }
portal-verkle-primitives = { git = "https://github.com/morph-dev/portal-verkle-primitives.git", rev = "244a975baca2af42d4a596f7f6f83bc26c35223b" }
serde_json = "1.0.89"
thiserror = "1.0.57"
tokio = {version = "1.14.0", features = ["full"]}
Expand Down
35 changes: 29 additions & 6 deletions trin-verkle/src/validation/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ impl Validator<VerkleContentKey> for VerkleValidator {
Ok(ValidationResult::new(/* valid_for_storing= */ false))
}
VerkleContentValue::NodeWithProof(node_with_proof) => {
let state_root = self.get_state_root(&node_with_proof.block_hash()).await;
let mut state_root = self.get_state_root(&node_with_proof.block_hash()).await;
if state_root.is_zero() {
state_root = self.get_state_root_from_proof(node_with_proof);
}

match content_key {
VerkleContentKey::Bundle(commitment) => match node_with_proof {
PortalVerkleNodeWithProof::BranchBundle(node_with_proof) => node_with_proof
.verify(
commitment,
&self.get_state_root(&node_with_proof.block_hash).await,
)?,
PortalVerkleNodeWithProof::BranchBundle(node_with_proof) => {
node_with_proof.verify(commitment, &state_root)?
}
PortalVerkleNodeWithProof::LeafBundle(node_with_proof) => {
node_with_proof.verify(commitment, &state_root)?
}
Expand Down Expand Up @@ -119,4 +121,25 @@ impl VerkleValidator {
debug!("Fetching state root is not yet implemented");
B256::ZERO
}

/// TODO: This is wrong! We shouldn't extract root from the proof.
fn get_state_root_from_proof(&self, node_with_proof: &PortalVerkleNodeWithProof) -> B256 {
let root_commitment = match node_with_proof {
PortalVerkleNodeWithProof::BranchBundle(node) => {
node.trie_path.root().unwrap_or(node.node.commitment())
}
PortalVerkleNodeWithProof::BranchFragment(node) => {
node.trie_path.root().unwrap_or(&node.bundle_commitment)
}
PortalVerkleNodeWithProof::LeafBundle(node) => node
.trie_path
.root()
.expect("bundle leaf proof should have root"),
PortalVerkleNodeWithProof::LeafFragment(node) => node
.trie_path
.root()
.expect("fragment leaf proof should have root"),
};
root_commitment.into()
}
}

0 comments on commit 5d53166

Please sign in to comment.