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

v1.17: shred: expose merkle root for use in blockstore (backport of #34063) #34291

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ledger/src/blockstore_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,35 @@ impl ErasureMeta {
}
}

#[allow(dead_code)]
impl MerkleRootMeta {
pub(crate) fn from_shred(shred: &Shred) -> Self {
Self {
// An error here after the shred has already sigverified
// can only indicate that the leader is sending
// 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.
merkle_root: shred.merkle_root().ok(),
first_received_shred_index: shred.index(),
first_received_shred_type: shred.shred_type(),
}
}

pub(crate) fn merkle_root(&self) -> Option<Hash> {
self.merkle_root
}

pub(crate) fn first_received_shred_index(&self) -> u32 {
self.first_received_shred_index
}

pub(crate) fn first_received_shred_type(&self) -> ShredType {
self.first_received_shred_type
}
}

impl DuplicateSlotProof {
pub(crate) fn new(shred1: Vec<u8>, shred2: Vec<u8>) -> Self {
DuplicateSlotProof { shred1, shred2 }
Expand Down
1 change: 1 addition & 0 deletions ledger/src/shred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ impl Shred {
dispatch!(pub(crate) fn erasure_shard_index(&self) -> Result<usize, Error>);

dispatch!(pub fn into_payload(self) -> Vec<u8>);
dispatch!(pub fn merkle_root(&self) -> Result<Hash, Error>);
dispatch!(pub fn payload(&self) -> &Vec<u8>);
dispatch!(pub fn sanitize(&self) -> Result<(), Error>);

Expand Down
4 changes: 2 additions & 2 deletions ledger/src/shred/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl ShredData {
Ok(Self::SIZE_OF_HEADERS + Self::capacity(proof_size)?)
}

fn merkle_root(&self) -> Result<Hash, Error> {
pub(super) fn merkle_root(&self) -> Result<Hash, Error> {
let proof_size = self.proof_size()?;
let index = self.erasure_shard_index()?;
let proof_offset = Self::proof_offset(proof_size)?;
Expand Down Expand Up @@ -266,7 +266,7 @@ impl ShredCode {
Ok(Self::SIZE_OF_HEADERS + Self::capacity(proof_size)?)
}

fn merkle_root(&self) -> Result<Hash, Error> {
pub(super) fn merkle_root(&self) -> Result<Hash, Error> {
let proof_size = self.proof_size()?;
let index = self.erasure_shard_index()?;
let proof_offset = Self::proof_offset(proof_size)?;
Expand Down
9 changes: 8 additions & 1 deletion ledger/src/shred/shred_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
CodingShredHeader, Error, ShredCommonHeader, ShredType, SignedData,
DATA_SHREDS_PER_FEC_BLOCK, MAX_DATA_SHREDS_PER_SLOT, SIZE_OF_NONCE,
},
solana_sdk::{clock::Slot, packet::PACKET_DATA_SIZE, signature::Signature},
solana_sdk::{clock::Slot, hash::Hash, packet::PACKET_DATA_SIZE, signature::Signature},
static_assertions::const_assert_eq,
};

Expand Down Expand Up @@ -47,6 +47,13 @@ impl ShredCode {
}
}

pub(super) fn merkle_root(&self) -> Result<Hash, Error> {
match self {
Self::Legacy(_) => Err(Error::InvalidShredType),
Self::Merkle(shred) => shred.merkle_root(),
}
}

pub(super) fn new_from_parity_shard(
slot: Slot,
index: u32,
Expand Down
9 changes: 8 additions & 1 deletion ledger/src/shred/shred_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
DataShredHeader, Error, ShredCommonHeader, ShredFlags, ShredType, ShredVariant, SignedData,
MAX_DATA_SHREDS_PER_SLOT,
},
solana_sdk::{clock::Slot, signature::Signature},
solana_sdk::{clock::Slot, hash::Hash, signature::Signature},
};

#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -41,6 +41,13 @@ impl ShredData {
}
}

pub(super) fn merkle_root(&self) -> Result<Hash, Error> {
match self {
Self::Legacy(_) => Err(Error::InvalidShredType),
Self::Merkle(shred) => shred.merkle_root(),
}
}

pub(super) fn new_from_data(
slot: Slot,
index: u32,
Expand Down