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

fix(sync): return control from HeaderStage back to pipeline #609

Merged
merged 4 commits into from
Dec 29, 2022

Conversation

TechieBoy
Copy link
Contributor

We only write commit_threshold blocks after fetching them with the downloader.

Closes #607

@TechieBoy
Copy link
Contributor Author

I think we can make this more efficient by only streaming head + commit_threshold blocks instead of head + fork choice tip, or by downloading from head onwards instead of downloading in reverse order from tip??

@rkrasiuk
Copy link
Member

thanks for the PR @TechieBoy! unfortunately, we cannot download headers onwards, because of long-range attack (refer to the headers section of the book for more details). it's better to trust the CL and walk the canonical chain from the tip to prevent peers from advertising bad blocks to us. we are planning to add some onward logic for chains that don't have EL/CL separation and possibly for pre-merge headers

@rkrasiuk rkrasiuk added the A-staged-sync Related to staged sync (pipelines and stages) label Dec 26, 2022
crates/stages/src/stages/headers.rs Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Dec 26, 2022

Codecov Report

Merging #609 (7db0d8c) into main (5bb14ec) will increase coverage by 0.09%.
The diff coverage is 96.22%.

@@            Coverage Diff             @@
##             main     #609      +/-   ##
==========================================
+ Coverage   72.22%   72.32%   +0.09%     
==========================================
  Files         255      255              
  Lines       25783    25792       +9     
==========================================
+ Hits        18623    18654      +31     
+ Misses       7160     7138      -22     
Flag Coverage Δ
unit-tests 72.32% <96.22%> (+0.09%) ⬆️

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

Impacted Files Coverage Δ
crates/stages/src/stages/headers.rs 97.20% <96.22%> (-0.46%) ⬇️
crates/net/network/src/manager.rs 46.24% <0.00%> (-0.25%) ⬇️
crates/stages/src/stages/bodies.rs 93.69% <0.00%> (+0.38%) ⬆️
crates/transaction-pool/src/pool/txpool.rs 58.73% <0.00%> (+0.52%) ⬆️
crates/net/eth-wire/src/types/version.rs 80.00% <0.00%> (+2.22%) ⬆️
crates/transaction-pool/src/test_util/mock.rs 60.37% <0.00%> (+6.66%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@TechieBoy
Copy link
Contributor Author

TechieBoy commented Dec 26, 2022

we cannot download headers onwards, because of long-range attack

Makes sense, but we do want to write the headers to db from head onwards, correct? Otherwise the stage_progress value in ExecOutput will stay the same until the entire stage is done. That is what I am doing here:

// Write a maximum of `commit_threshold` headers, starting from local head onwards
let write_progress = self
.write_headers::<DB>(tx, res, self.commit_threshold)
.await?
.unwrap_or_default();

Or should it write to db from tip downwards?

@rkrasiuk
Copy link
Member

rkrasiuk commented Dec 26, 2022

we do want to write the headers to db from head onwards

Since we are not appending anymore, it shouldn't matter really.

stage_progress value in ExecOutput will stay the same until the entire stage is done

that's the desired behavior. otherwise we wouldn't know the local "head"

crates/stages/src/stages/headers.rs Outdated Show resolved Hide resolved
crates/stages/src/stages/headers.rs Outdated Show resolved Hide resolved
@TechieBoy TechieBoy force-pushed the header-commit branch 2 times, most recently from 26b2d68 to 76bb72c Compare December 27, 2022 13:33
@TechieBoy TechieBoy marked this pull request as ready for review December 27, 2022 13:36
@TechieBoy
Copy link
Contributor Author

TechieBoy commented Dec 27, 2022

  • Take and write only commit_threshold headers from stream to db.
  • Use headers stored in local db + fork choice state to check if we haved finished the stage.

crates/stages/src/stages/headers.rs Outdated Show resolved Hide resolved
crates/stages/src/stages/headers.rs Outdated Show resolved Hide resolved
Copy link
Member

@rkrasiuk rkrasiuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, great job!

@rkrasiuk rkrasiuk changed the title fix(sync): Headerstage now gives control back to pipeline after commit_threshold blocks have been synced fix(sync): return control from HeaderStage back to pipeline Dec 27, 2022
@TechieBoy
Copy link
Contributor Author

lgtm, great job!

👍🏻 Thanks for being so patient and helpful and answering all my questions!

Copy link
Member

@gakonst gakonst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good from me - pending @onbjerg approval

current_progress = current_progress.max(write_progress);
// The downloader returns the headers in descending order starting from the tip
// down to the local head (latest block in db)
let downloaded_headers: Result<Vec<SealedHeader>, DownloadError> = self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bjerg Ptal - looks good from my side

@@ -166,6 +169,19 @@ impl<D: HeaderDownloader, C: Consensus, H: HeadersClient, S: StatusUpdater>
Ok(())
}

async fn is_stage_done<DB: Database>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@onbjerg PTAL

@rkrasiuk
Copy link
Member

lgtm, merging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-staged-sync Related to staged sync (pipelines and stages)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Headers stage commits when the full range is synced
4 participants