From f1336970d09d9bc84b822b819506ce2e959cc49a Mon Sep 17 00:00:00 2001 From: Ashwin Sekar Date: Mon, 22 Apr 2024 16:03:18 -0700 Subject: [PATCH] blockstore: store first received coding shred index in ErasureMeta (#961) * blockstore: store first received coding shred index in ErasureMeta * pr feedback: use u64 instead of usize --- ledger/src/blockstore_meta.rs | 67 +++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/ledger/src/blockstore_meta.rs b/ledger/src/blockstore_meta.rs index c56453d88352bc..8e97b9ac801e63 100644 --- a/ledger/src/blockstore_meta.rs +++ b/ledger/src/blockstore_meta.rs @@ -125,9 +125,8 @@ pub struct ErasureMeta { set_index: u64, /// First coding index in the FEC set first_coding_index: u64, - /// Size of shards in this erasure set - #[serde(rename = "size")] - __unused_size: usize, + /// Index of the first received coding shred in the FEC set + first_received_coding_index: u64, /// Erasure configuration for this erasure set config: ErasureConfig, } @@ -348,11 +347,12 @@ impl ErasureMeta { num_coding: usize::from(shred.num_coding_shreds().ok()?), }; let first_coding_index = u64::from(shred.first_coding_index()?); + let first_received_coding_index = u64::from(shred.index()); let erasure_meta = ErasureMeta { set_index: u64::from(shred.fec_set_index()), config, first_coding_index, - __unused_size: 0, + first_received_coding_index, }; Some(erasure_meta) } @@ -365,7 +365,7 @@ impl ErasureMeta { let Some(mut other) = Self::from_coding_shred(shred) else { return false; }; - other.__unused_size = self.__unused_size; + other.first_received_coding_index = self.first_received_coding_index; self == &other } @@ -392,6 +392,11 @@ impl ErasureMeta { self.first_coding_index..self.first_coding_index + num_coding } + #[allow(dead_code)] + pub(crate) fn first_received_coding_shred_index(&self) -> Option { + u32::try_from(self.first_received_coding_index).ok() + } + pub(crate) fn next_fec_set_index(&self) -> Option { let num_data = u64::try_from(self.config.num_data).ok()?; self.set_index @@ -568,7 +573,7 @@ mod test { set_index, first_coding_index: set_index, config: erasure_config, - __unused_size: 0, + first_received_coding_index: 0, }; let mut rng = thread_rng(); let mut index = Index::new(0); @@ -721,4 +726,54 @@ mod test { assert_eq!(actual, expected); } + + #[test] + fn test_erasure_meta_transition() { + #[derive(Debug, Deserialize, PartialEq, Serialize)] + struct OldErasureMeta { + set_index: u64, + first_coding_index: u64, + #[serde(rename = "size")] + __unused_size: usize, + config: ErasureConfig, + } + + let set_index = 64; + let erasure_config = ErasureConfig { + num_data: 8, + num_coding: 16, + }; + let mut old_erasure_meta = OldErasureMeta { + set_index, + first_coding_index: set_index, + __unused_size: 0, + config: erasure_config, + }; + let mut new_erasure_meta = ErasureMeta { + set_index, + first_coding_index: set_index, + first_received_coding_index: 0, + config: erasure_config, + }; + + assert_eq!( + bincode::serialized_size(&old_erasure_meta).unwrap(), + bincode::serialized_size(&new_erasure_meta).unwrap(), + ); + + assert_eq!( + bincode::deserialize::(&bincode::serialize(&old_erasure_meta).unwrap()) + .unwrap(), + new_erasure_meta + ); + + new_erasure_meta.first_received_coding_index = u64::from(u32::max_value()); + old_erasure_meta.__unused_size = usize::try_from(u32::max_value()).unwrap(); + + assert_eq!( + bincode::deserialize::(&bincode::serialize(&new_erasure_meta).unwrap()) + .unwrap(), + old_erasure_meta + ); + } }