Skip to content

Commit

Permalink
feat: get more last headers in the first proof of a newly connected peer
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape committed Jan 9, 2024
1 parent 0a8c34a commit 673bad9
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/protocols/light_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,35 @@ impl LightClientProtocol {
}
let builder = packed::GetLastStateProof::new_builder()
.last_hash(last_header.header().hash())
.start_hash(start_hash)
.start_number(start_number.pack())
.last_n_blocks(last_n_blocks.pack());
let content = if last_number - start_number <= last_n_blocks {
builder.difficulty_boundary(start_total_difficulty.pack())
let last_n_headers = self.storage.get_last_n_headers();
let mut real_start_hash = start_hash;
let mut real_start_number = start_number;
// Find the number which satisfies that `last_number - number > last_n_blocks`.
// If not found, then choose the smallest number.
for (num, hash) in last_n_headers
.into_iter()
.rev()
// `skip_while` should do nothing, just for safety.
.skip_while(|(n, _)| *n >= start_number)
{
if real_start_number <= num {
continue;
}
if last_number > num + last_n_blocks {
break;
}
real_start_hash = hash;
real_start_number = num;
if last_number == num + last_n_blocks {
break;
}
}
builder
.start_hash(real_start_hash)
.start_number(real_start_number.pack())
.difficulty_boundary(start_total_difficulty.pack())
} else {
let (difficulty_boundary, difficulties) = sampling::sample_blocks(
start_number,
Expand All @@ -866,6 +890,8 @@ impl LightClientProtocol {
last_n_blocks,
);
builder
.start_hash(start_hash)
.start_number(start_number.pack())
.difficulty_boundary(difficulty_boundary.pack())
.difficulties(difficulties.into_iter().map(|inner| inner.pack()).pack())
}
Expand Down

0 comments on commit 673bad9

Please sign in to comment.