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

backport #13 to v0.2.x #14

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 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