Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

shred: expose merkle root for use in blockstore #34063

Merged
merged 3 commits into from
Nov 15, 2023

Conversation

AshwinSekar
Copy link
Contributor

@AshwinSekar AshwinSekar commented Nov 14, 2023

Problem

We need to access the merkle_root for merkle shreds in blockstore.

Summary of Changes

Dispatch to it, return None for legacy shreds.

Split from #33889
Contributes to #33644

@AshwinSekar AshwinSekar force-pushed the merkle-root-getter branch 2 times, most recently from 0a32372 to b741505 Compare November 14, 2023 20:47
impl MerkleRootMeta {
pub(crate) fn from_shred(shred: &Shred) -> Self {
Self {
merkle_root: shred.merkle_root().unwrap_or_default(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Now I am thinking that MerkleRootMeta.merkle_root should have been Option<Hash> to explicitly distinguish legacy shreds.
This unwrap_or_default here is a bit hacky, and it seems like we will have to special case merkle_root_meta.merkle_root == Hash::default() later.

Copy link
Contributor Author

@AshwinSekar AshwinSekar Nov 14, 2023

Choose a reason for hiding this comment

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

definitely possible, however I was under the impression that legacy shreds are no longer being broadcasted.
I assume at some point you will delete shred/legacy.rs at which point I can remove unwrap_or_default() and any Hash::default() checks, and return Option<Self> for legitimate errors

Copy link
Contributor

Choose a reason for hiding this comment

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

They are no longer broadcasted but still totally valid to send legacy shreds.
We might also need to keep shred/legacy.rs for backward compatibility.
I am not sure for how long we will need to preserve legacy shreds, so I am leaning to use Option<Hash> to be consistent with current state of the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Is it supported to send a mixture of legacy and merkle shreds in a single FEC set?
If not I think we can just avoid storing the merkle root meta for legacy FEC sets. There's no benefit to inserting the meta into blockstore if the FEC set only has legacy shreds. For the next pr that writes this column, I can filter out FEC sets that are legacy.

Copy link
Contributor

Choose a reason for hiding this comment

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

no, but if you receive two shreds with the same fec_set_index and one is merkle and the other legacy, then that is a proof of duplicate block.
So I think we need to record that we have received a legacy shred for this fec_set_index, and I guess the easiest way to do that is to set merkle_root to None

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense i'll do that first.

@@ -337,6 +337,8 @@ impl Shred {
dispatch!(pub fn payload(&self) -> &Vec<u8>);
dispatch!(pub fn sanitize(&self) -> Result<(), Error>);

dispatch!(pub fn merkle_root(&self) -> Option<Hash>);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please keep lines 336-340 alphabetically sorted?
also, the blank like 339 is not needed.

also, the return type should be Result<Hash, Error> to preserve the error.

@@ -47,6 +47,13 @@ impl ShredCode {
}
}

pub(super) fn merkle_root(&self) -> Option<Hash> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we return -> Result<Hash, Error> to preserve the error?
Self::Legacy(_) case would then return Err(Error::InvalidShredType).

Comment on lines 44 to 46
pub(super) fn merkle_root(&self) -> Option<Hash> {
match self {
Self::Legacy(_) => None,
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, about preserving the Error.

Copy link

codecov bot commented Nov 14, 2023

Codecov Report

Merging #34063 (28b6626) into master (7ba9fbc) will increase coverage by 0.0%.
Report is 1333 commits behind head on master.
The diff coverage is 91.1%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #34063      +/-   ##
==========================================
  Coverage    81.8%    81.9%              
==========================================
  Files         766      812      +46     
  Lines      209154   219707   +10553     
==========================================
+ Hits       171206   180014    +8808     
- Misses      37948    39693    +1745     

Copy link
Contributor

@behzadnouri behzadnouri left a comment

Choose a reason for hiding this comment

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

lgtm, but can we first update MerkleRootMeta.merkle_root to an explicit Option<Hash>.
that might need to be done in a separate PR because that needs to be backported.

// legacy or malformed shreds. We should still store
// `None` for those cases in blockstore, as a later
// shred that contains a proper merkle root would constitute
// a valid duplicate shred proof.
Copy link
Contributor

Choose a reason for hiding this comment

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

duplicate "block" proof, no?
we might not actually observe any duplicate shreds.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure about the terminology. We're still calling them duplicate shred proofs in gossip even though it has evolved beyond shreds with the same index. If you prefer I can do a separate refactor PR afterwards renaming all instances of "duplicate shred proof" to "duplicate block proof"?

Copy link
Contributor

Choose a reason for hiding this comment

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

never mind then,
we can revisit once the code is shipped and stable.

@AshwinSekar AshwinSekar added the automerge Merge this Pull Request automatically once CI passes label Nov 15, 2023
@mergify mergify bot merged commit 6a5b8e8 into solana-labs:master Nov 15, 2023
@AshwinSekar AshwinSekar deleted the merkle-root-getter branch November 15, 2023 20:13
@AshwinSekar AshwinSekar self-assigned this Nov 29, 2023
@AshwinSekar AshwinSekar requested a review from t-nelson November 30, 2023 23:29
@AshwinSekar AshwinSekar added the v1.17 PRs that should be backported to v1.17 label Nov 30, 2023
Copy link
Contributor

mergify bot commented Nov 30, 2023

Backports to the beta branch are to be avoided unless absolutely necessary for fixing bugs, security issues, and perf regressions. Changes intended for backport should be structured such that a minimum effective diff can be committed separately from any refactoring, plumbing, cleanup, etc that are not strictly necessary to achieve the goal. Any of the latter should go only into master and ride the normal stabilization schedule. Exceptions include CI/metrics changes, CLI improvements and documentation updates on a case by case basis.

mergify bot pushed a commit that referenced this pull request Nov 30, 2023
* shred: expose merkle root for use in blockstore

* pr feedback: sorted, keep Result return type

* convert Result<Hash> -> Option<Hash>

(cherry picked from commit 6a5b8e8)
AshwinSekar added a commit that referenced this pull request Dec 1, 2023
* shred: expose merkle root for use in blockstore

* pr feedback: sorted, keep Result return type

* convert Result<Hash> -> Option<Hash>

(cherry picked from commit 6a5b8e8)
AshwinSekar added a commit that referenced this pull request Dec 6, 2023
* shred: expose merkle root for use in blockstore

* pr feedback: sorted, keep Result return type

* convert Result<Hash> -> Option<Hash>

(cherry picked from commit 6a5b8e8)
AshwinSekar added a commit that referenced this pull request Dec 7, 2023
…34063) (#34291)

shred: expose merkle root for use in blockstore (#34063)

* shred: expose merkle root for use in blockstore

* pr feedback: sorted, keep Result return type

* convert Result<Hash> -> Option<Hash>

(cherry picked from commit 6a5b8e8)

Co-authored-by: Ashwin Sekar <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
automerge Merge this Pull Request automatically once CI passes v1.17 PRs that should be backported to v1.17
Projects
Development

Successfully merging this pull request may close these issues.

2 participants