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

Commit 9fbd65b

Browse files
AshwinSekarmergify[bot]
authored andcommitted
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)
1 parent fca44b7 commit 9fbd65b

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

ledger/src/blockstore_meta.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,35 @@ impl ErasureMeta {
396396
}
397397
}
398398

399+
#[allow(dead_code)]
400+
impl MerkleRootMeta {
401+
pub(crate) fn from_shred(shred: &Shred) -> Self {
402+
Self {
403+
// An error here after the shred has already sigverified
404+
// can only indicate that the leader is sending
405+
// legacy or malformed shreds. We should still store
406+
// `None` for those cases in blockstore, as a later
407+
// shred that contains a proper merkle root would constitute
408+
// a valid duplicate shred proof.
409+
merkle_root: shred.merkle_root().ok(),
410+
first_received_shred_index: shred.index(),
411+
first_received_shred_type: shred.shred_type(),
412+
}
413+
}
414+
415+
pub(crate) fn merkle_root(&self) -> Option<Hash> {
416+
self.merkle_root
417+
}
418+
419+
pub(crate) fn first_received_shred_index(&self) -> u32 {
420+
self.first_received_shred_index
421+
}
422+
423+
pub(crate) fn first_received_shred_type(&self) -> ShredType {
424+
self.first_received_shred_type
425+
}
426+
}
427+
399428
impl DuplicateSlotProof {
400429
pub(crate) fn new(shred1: Vec<u8>, shred2: Vec<u8>) -> Self {
401430
DuplicateSlotProof { shred1, shred2 }

ledger/src/shred.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ impl Shred {
334334
dispatch!(pub(crate) fn erasure_shard_index(&self) -> Result<usize, Error>);
335335

336336
dispatch!(pub fn into_payload(self) -> Vec<u8>);
337+
dispatch!(pub fn merkle_root(&self) -> Result<Hash, Error>);
337338
dispatch!(pub fn payload(&self) -> &Vec<u8>);
338339
dispatch!(pub fn sanitize(&self) -> Result<(), Error>);
339340

ledger/src/shred/merkle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl ShredData {
154154
Ok(Self::SIZE_OF_HEADERS + Self::capacity(proof_size)?)
155155
}
156156

157-
fn merkle_root(&self) -> Result<Hash, Error> {
157+
pub(super) fn merkle_root(&self) -> Result<Hash, Error> {
158158
let proof_size = self.proof_size()?;
159159
let index = self.erasure_shard_index()?;
160160
let proof_offset = Self::proof_offset(proof_size)?;
@@ -266,7 +266,7 @@ impl ShredCode {
266266
Ok(Self::SIZE_OF_HEADERS + Self::capacity(proof_size)?)
267267
}
268268

269-
fn merkle_root(&self) -> Result<Hash, Error> {
269+
pub(super) fn merkle_root(&self) -> Result<Hash, Error> {
270270
let proof_size = self.proof_size()?;
271271
let index = self.erasure_shard_index()?;
272272
let proof_offset = Self::proof_offset(proof_size)?;

ledger/src/shred/shred_code.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use {
66
CodingShredHeader, Error, ShredCommonHeader, ShredType, SignedData,
77
DATA_SHREDS_PER_FEC_BLOCK, MAX_DATA_SHREDS_PER_SLOT, SIZE_OF_NONCE,
88
},
9-
solana_sdk::{clock::Slot, packet::PACKET_DATA_SIZE, signature::Signature},
9+
solana_sdk::{clock::Slot, hash::Hash, packet::PACKET_DATA_SIZE, signature::Signature},
1010
static_assertions::const_assert_eq,
1111
};
1212

@@ -47,6 +47,13 @@ impl ShredCode {
4747
}
4848
}
4949

50+
pub(super) fn merkle_root(&self) -> Result<Hash, Error> {
51+
match self {
52+
Self::Legacy(_) => Err(Error::InvalidShredType),
53+
Self::Merkle(shred) => shred.merkle_root(),
54+
}
55+
}
56+
5057
pub(super) fn new_from_parity_shard(
5158
slot: Slot,
5259
index: u32,

ledger/src/shred/shred_data.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use {
77
DataShredHeader, Error, ShredCommonHeader, ShredFlags, ShredType, ShredVariant, SignedData,
88
MAX_DATA_SHREDS_PER_SLOT,
99
},
10-
solana_sdk::{clock::Slot, signature::Signature},
10+
solana_sdk::{clock::Slot, hash::Hash, signature::Signature},
1111
};
1212

1313
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -41,6 +41,13 @@ impl ShredData {
4141
}
4242
}
4343

44+
pub(super) fn merkle_root(&self) -> Result<Hash, Error> {
45+
match self {
46+
Self::Legacy(_) => Err(Error::InvalidShredType),
47+
Self::Merkle(shred) => shred.merkle_root(),
48+
}
49+
}
50+
4451
pub(super) fn new_from_data(
4552
slot: Slot,
4653
index: u32,

0 commit comments

Comments
 (0)