Skip to content

Commit

Permalink
blockstore: store first received coding shred index in ErasureMeta (a…
Browse files Browse the repository at this point in the history
…nza-xyz#961)

* blockstore: store first received coding shred index in ErasureMeta

* pr feedback: use u64 instead of usize
  • Loading branch information
AshwinSekar authored Apr 22, 2024
1 parent af4759e commit f133697
Showing 1 changed file with 61 additions and 6 deletions.
67 changes: 61 additions & 6 deletions ledger/src/blockstore_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}

Expand All @@ -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> {
u32::try_from(self.first_received_coding_index).ok()
}

pub(crate) fn next_fec_set_index(&self) -> Option<u32> {
let num_data = u64::try_from(self.config.num_data).ok()?;
self.set_index
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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::<ErasureMeta>(&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::<OldErasureMeta>(&bincode::serialize(&new_erasure_meta).unwrap())
.unwrap(),
old_erasure_meta
);
}
}

0 comments on commit f133697

Please sign in to comment.