Skip to content

Commit

Permalink
Merge pull request #14 from yangby-cryptape/feature/check-old-mmr-roo…
Browse files Browse the repository at this point in the history
…t-v0.2.x

backport #13 to `v0.2.x`
  • Loading branch information
yangby-cryptape authored Jul 21, 2023
2 parents 5e8549d + b262660 commit f080ddd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion verification/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/synapseweb3/eth-light-client-in-ckb"

[dependencies]
molecule = { version = "=0.7.3", default-features = false }
ckb-mmr = { version = "0.5.2", default-features = false, package = "ckb-merkle-mountain-range" }
ckb-mmr = { version = "0.6.0", default-features = false, package = "ckb-merkle-mountain-range" }
rlp = { version = "0.5.2", default-features = false }
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
eth2_hashing = { version = "0.3.0", default-features = false, git = "https://github.com/synapseweb3/lighthouse", rev = "2c246d6" }
Expand Down
37 changes: 27 additions & 10 deletions verification/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl core::Client {
let mut updates_iter = updates.iter();

let mut cached_finalized_headers = Vec::with_capacity(updates_len);
let mut digests_with_positions = Vec::with_capacity(updates_len);
let mut digests = Vec::with_capacity(updates_len);
let minimal_slot;
let mut header_mmr_index;

Expand Down Expand Up @@ -145,11 +145,10 @@ impl core::Client {

// TODO verify more, such as BLS

let position = leaf_index_to_pos(header_mmr_index);
trace!("previous header in MMR on index {header_mmr_index}, position {position}");
trace!("previous header in MMR on index {header_mmr_index}");
let digest = prev_cached_header.digest();
cached_finalized_headers.push(prev_cached_header);
digests_with_positions.push((position, digest));
digests.push(digest);

header_mmr_index += 1;
prev_cached_header = curr_cached_header;
Expand All @@ -160,11 +159,10 @@ impl core::Client {

// Handle the last update
{
let position = leaf_index_to_pos(header_mmr_index);
trace!("previous header in MMR on index {header_mmr_index}, position {position}");
trace!("previous header in MMR on index {header_mmr_index}");
let digest = prev_cached_header.digest();
cached_finalized_headers.push(prev_cached_header);
digests_with_positions.push((position, digest));
digests.push(digest);
}

// Check MMR Root
Expand All @@ -180,12 +178,31 @@ impl core::Client {
.collect::<Vec<_>>();
mmr::MMRProof::new(mmr_size, proof)
};
let result = proof
.verify(

let result = if let Some(ref client) = prev_client_opt {
debug!("check MMR root with prev client: {client}");
proof.verify_incremental(
packed_proof_update.new_headers_mmr_root().to_entity(),
client.headers_mmr_root.pack(),
digests,
)
} else {
debug!("check MMR root without prev client");
let digests_with_positions = digests
.into_iter()
.fold((0u64, Vec::new()), |(index, mut vec), digest| {
let position = leaf_index_to_pos(index);
vec.push((position, digest));
(index + 1, vec)
})
.1;
proof.verify(
packed_proof_update.new_headers_mmr_root().to_entity(),
digests_with_positions,
)
.map_err(|_| ProofUpdateError::Other)?;
}
.map_err(|_| ProofUpdateError::Other)?;

if !result {
return Err(ProofUpdateError::HeadersMmrProof);
}
Expand Down

0 comments on commit f080ddd

Please sign in to comment.