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

feat: get more last headers in the first proof of a newly connected peer #178

Conversation

yangby-cryptape
Copy link
Collaborator

Description

In the following scenario:

  • Let $N$ denote the parameter LAST_N_BLOCKS.
  • A light client L is shutdown with last state $s$.
  • Then, restart L, and it connects to a peer P whose tip is $s+t$ and $t < N$.
    So, the first proof that L received from P, will contains only $t$ last headers.

If $t$ is very small, and P changes to another fork chain, then the fork block will be hard to find.

let fork_number = reorg_last_headers.iter().rev().find_map(|reorg_header| {
let number = reorg_header.number();
old_last_headers
.get(&number)
.map(|hash| {
if &reorg_header.hash() == hash {
Some(number)
} else {
None
}
})
.unwrap_or_default()
});

As a result, it is easy to trigger "long fork detected".

Changes

  • Find the largest header from the last headers in the storage, which satisfies that $s - x > N$ ($x$ is the number of that header).
  • If not found any header in the above step, then choose the smallest header from the last headers in the storage.
  • Use the header which is found in above steps as the start header.

@yangby-cryptape yangby-cryptape force-pushed the feature/more-last-headers-for-a-newly-connected-peer branch from ce43a52 to 673bad9 Compare January 9, 2024 02:03
@codecov-commenter
Copy link

codecov-commenter commented Jan 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (0a8c34a) 71.42% compared to head (0c653da) 72.46%.
Report is 6 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #178      +/-   ##
===========================================
+ Coverage    71.42%   72.46%   +1.04%     
===========================================
  Files           25       25              
  Lines         6512     6523      +11     
===========================================
+ Hits          4651     4727      +76     
+ Misses        1861     1796      -65     
Flag Coverage Δ
unittests 72.46% <100.00%> (+1.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yangby-cryptape yangby-cryptape marked this pull request as ready for review January 9, 2024 04:02
quake
quake previously approved these changes Jan 9, 2024
src/protocols/light_client/mod.rs Outdated Show resolved Hide resolved
@quake quake merged commit 945b1dc into nervosnetwork:develop Jan 10, 2024
6 checks passed
@yangby-cryptape yangby-cryptape deleted the feature/more-last-headers-for-a-newly-connected-peer branch January 10, 2024 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants