Skip to content

Commit

Permalink
Merge pull request #8 from yangby-cryptape/bugfix/update-multiple-hea…
Browse files Browse the repository at this point in the history
…ders

fix: mmr error when update multiple headers at once
  • Loading branch information
yangby-cryptape authored Mar 16, 2024
2 parents de2a438 + e8f1cfb commit 29a8710
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions prover/src/dummy_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ impl DummyService {

let mut positions = Vec::new();
let mut block_hash = core::Hash::all_zeros();
let mut height = 0;
let mut height = self.client.headers_mmr_root.max_height;

for header in &headers {
height = self.client.headers_mmr_root.max_height + 1;
height += 1;

let index = height - self.client.headers_mmr_root.min_height;
let position = mmr::lib::leaf_index_to_pos(u64::from(index));
Expand Down
17 changes: 15 additions & 2 deletions prover/src/tests/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{format, fs::File, io::Read as _};
use std::{format, fs::File, io::Read as _, mem};

use bitcoin::consensus::serialize;
use ckb_bitcoin_spv_verifier::types::{core, packed, prelude::*};
Expand Down Expand Up @@ -47,6 +47,8 @@ fn test_spv_client(

// Update
let mut old_client: packed::SpvClient = service.tip_client().pack();
let mut headers = Vec::new();
let mut headers_group_size = 1;
for header_bin in header_bins_iter {
let header: core::Header = utilities::decode_from_bin_file(&header_bin).unwrap();
let height: u32 = header_bin
Expand All @@ -58,7 +60,18 @@ fn test_spv_client(
.unwrap();
log::trace!("process header-{height} from file {}", header_bin.display());

let update = service.update(vec![header]).unwrap();
headers.push(header);
if height + 1 != verify_tx_range.0 && headers.len() < headers_group_size {
continue;
}

log::trace!("process {} headers at one time", headers.len());
let update = service.update(mem::take(&mut headers)).unwrap();
if verify_tx_range.0 <= height + 1 && height <= verify_tx_range.1 {
headers_group_size = 1;
} else {
headers_group_size += 1;
}
let new_client: packed::SpvClient = service.tip_client().pack();

old_client
Expand Down

0 comments on commit 29a8710

Please sign in to comment.