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

refactor: replace dependency on Block with BlockContext and Chunks on apply chunk #12746

Merged
merged 7 commits into from
Jan 17, 2025

Conversation

Longarithm
Copy link
Member

@Longarithm Longarithm commented Jan 16, 2025

Context

We want to improve chunk processing efficiency by applying chunks optimistically (#10584), when all partial chunks for the next height and block metadata (from an OptimisticBlock) are already available. This allows the results of chunk application to be reused when the actual block is received, enabling the next chunk to be produced immediately.

Currently, this work is a step towards supporting OptimisticBlock. While OptimisticBlock is not introduced yet, this refactor prepares the codebase for its implementation by reducing data dependency on the current block.

Change

I replaced the dependency on Block with ApplyChunkBlockContext and chunk_headers: &Chunks. We'll just need to add conversion from OptimisticBlock to ApplyChunkBlockContext later. Chunk headers are just taken from block, and for optimistic block, they must be supplied by ShardsManager.

Some APIs are refactored to reflect that change.

Next steps

  • Convert OptimisticBlock to ApplyChunkBlockContext.
  • Then, we call get_update_shard_job for OptimisticBlock and reuse result if it is called for the actual Block.

@Longarithm Longarithm marked this pull request as ready for review January 16, 2025 10:31
@Longarithm Longarithm requested a review from a team as a code owner January 16, 2025 10:31
@Longarithm Longarithm requested a review from miloserdow January 16, 2025 10:36
Copy link

codecov bot commented Jan 16, 2025

Codecov Report

Attention: Patch coverage is 80.11364% with 35 lines in your changes missing coverage. Please review.

Project coverage is 70.65%. Comparing base (be90285) to head (7caad04).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
chain/chain/src/chain.rs 76.38% 15 Missing and 19 partials ⚠️
chain/chain-primitives/src/error.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #12746       +/-   ##
===========================================
+ Coverage    1.35%   70.65%   +69.29%     
===========================================
  Files         670      849      +179     
  Lines      120927   174555    +53628     
  Branches   120927   174555    +53628     
===========================================
+ Hits         1637   123328   +121691     
+ Misses     119201    46062    -73139     
- Partials       89     5165     +5076     
Flag Coverage Δ
backward-compatibility 0.16% <0.00%> (?)
db-migration 0.16% <0.00%> (?)
genesis-check 1.35% <0.00%> (-0.01%) ⬇️
linux 69.15% <77.84%> (+67.80%) ⬆️
linux-nightly 70.25% <79.54%> (?)
pytests 1.65% <0.00%> (+0.29%) ⬆️
sanity-checks 1.46% <0.00%> (?)
unittests 70.48% <80.11%> (?)
upgradability 0.20% <0.00%> (-0.01%) ⬇️

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.

Copy link
Contributor

@VanBarbascu VanBarbascu left a comment

Choose a reason for hiding this comment

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

Kudos!
This is a huge step in plugging optimistic block in the chunk processing flow!

chain/chain/src/chain.rs Outdated Show resolved Hide resolved
@@ -3722,30 +3742,38 @@ impl Chain {
fn get_update_shard_job(
&self,
me: &Option<AccountId>,
block: &Block,
// TODO(#10584): introduce separate structure which can be derived from
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be an enum that either has Block or OptimisticBlock.

Copy link
Member Author

Choose a reason for hiding this comment

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

When looking into that, I realised I can just use ApplyChunkBlockContext because that's all what matters. It led to one more change - InvalidChunkProofs must be generated outside the method. But it's not an important error to support precisely, and we won't be able to return such errors while having optimistic block anyway.

let epoch_id = self.epoch_manager.get_epoch_id_from_prev_block(prev_hash)?;
let shard_layout = self.epoch_manager.get_shard_layout(&epoch_id)?;
let shard_id = shard_layout.get_shard_id(shard_index)?;
let chunk_header = &chunk_headers[shard_index];
Copy link
Contributor

Choose a reason for hiding this comment

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

In Chunks struct, they say that Index trait is deprecated and recommends using .get() instead. Just to align with the new resharding implementattion.

Suggested change
let chunk_header = &chunk_headers[shard_index];
let chunk_header = &chunk_headers.get(shard_index);

Longarithm and others added 2 commits January 17, 2025 01:58
Co-authored-by: Razvan Barbascu <r.barbascu@gmail.com>
@Longarithm
Copy link
Member Author

Please note I did more refactoring as response to #12746 (comment).

Copy link
Contributor

@VanBarbascu VanBarbascu left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@miloserdow miloserdow left a comment

Choose a reason for hiding this comment

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

LGTM, don't forget to run cargo fmt before merging

@Longarithm Longarithm enabled auto-merge January 17, 2025 13:34
@Longarithm Longarithm added this pull request to the merge queue Jan 17, 2025
@Longarithm Longarithm changed the title refactor: replace dependency on Block with BlockHeader and Chunks on apply chunk refactor: replace dependency on Block with BlockContext and Chunks on apply chunk Jan 17, 2025
auto-merge was automatically disabled January 17, 2025 13:53

Pull Request is not mergeable

@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 17, 2025
@Longarithm Longarithm added this pull request to the merge queue Jan 17, 2025
Merged via the queue into near:master with commit 43fc799 Jan 17, 2025
28 checks passed
@Longarithm Longarithm deleted the break-block branch January 17, 2025 14:44
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