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

chore: De-duplicate code to create BlockHeader for genesis and view #12109

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

tayfunelmas
Copy link
Contributor

@tayfunelmas tayfunelmas commented Sep 18, 2024

We address a TODO item to use BlockHeader::new when creating a BlockHeader in two other places (today these places use similar logic to choose the right BlockHeader version and contruct the inner structs based on the protocol version).

  1. When generating BlockHeader from BlockHeaderView.
  2. When generating the genesis BlockHeader for the genesis block.

For this, we add BlockHeader::new_impl to contain the common code and have two other wrapper functions to call new_impl for the use cases above: BlockHeader::from_view and BlockHeader::genesis.

@tayfunelmas tayfunelmas marked this pull request as ready for review September 19, 2024 09:34
@tayfunelmas tayfunelmas requested a review from a team as a code owner September 19, 2024 09:34
@tayfunelmas tayfunelmas removed the request for review from akhi3030 September 19, 2024 10:25
@tayfunelmas tayfunelmas marked this pull request as draft September 19, 2024 10:26
Copy link

codecov bot commented Sep 19, 2024

Codecov Report

Attention: Patch coverage is 98.82812% with 3 lines in your changes missing coverage. Please review.

Project coverage is 71.59%. Comparing base (8ece1ac) to head (784a0f6).

Files with missing lines Patch % Lines
core/primitives/src/block_header.rs 99.10% 1 Missing and 1 partial ⚠️
core/primitives/src/views.rs 96.87% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12109      +/-   ##
==========================================
+ Coverage   71.53%   71.59%   +0.06%     
==========================================
  Files         818      818              
  Lines      164494   164367     -127     
  Branches   164494   164367     -127     
==========================================
+ Hits       117667   117679      +12     
+ Misses      41688    41549     -139     
  Partials     5139     5139              
Flag Coverage Δ
backward-compatibility 0.17% <0.00%> (+<0.01%) ⬆️
db-migration 0.17% <0.00%> (+<0.01%) ⬆️
genesis-check 1.26% <0.00%> (+<0.01%) ⬆️
integration-tests 38.71% <98.82%> (+0.07%) ⬆️
linux 71.39% <98.82%> (+0.06%) ⬆️
linux-nightly 71.14% <98.82%> (+0.02%) ⬆️
macos 54.31% <95.31%> (+1.37%) ⬆️
pytests 1.53% <0.00%> (+<0.01%) ⬆️
sanity-checks 1.33% <0.00%> (+<0.01%) ⬆️
unittests 65.44% <95.31%> (+0.18%) ⬆️
upgradability 0.22% <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.

@tayfunelmas tayfunelmas marked this pull request as ready for review September 19, 2024 14:20
@tayfunelmas tayfunelmas assigned akhi3030 and unassigned akhi3030 Sep 19, 2024
Copy link
Member

@Longarithm Longarithm left a comment

Choose a reason for hiding this comment

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

This is great, thank you!

Comment on lines +989 to +993
assert_ne!(
signer.is_some(),
signature.is_some(),
"Exactly one of signer and signature must be provided"
);
Copy link
Member

Choose a reason for hiding this comment

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

We should replace them with a single enum then, with two variants, e.g.

enum SignatureSource<'a> {
    Signer(&'a ValidatorSigner)
    Signature(Signature)
}

which documents itself.

}

/// Common logic for generating BlockHeader for different purposes, including new blocks, from views, and for genesis block
fn new_impl(
Copy link
Member

Choose a reason for hiding this comment

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

We could probably unite arguments in BlockHeaderInnerLite, BlockHeaderInnerRestV1 (?) and the rest earlier to reduce number of arguments.

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