From 0ce665a90b189cc2c90255106c67d6f832dcb5f1 Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Thu, 23 Jan 2025 10:06:12 -0800 Subject: [PATCH] Add legacy deserialization blockstore test --- ledger/src/blockstore.rs | 24 +++++++++++++++++++++++- ledger/src/blockstore_meta.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/ledger/src/blockstore.rs b/ledger/src/blockstore.rs index 697751f1558f51..b5d003a84aabaf 100644 --- a/ledger/src/blockstore.rs +++ b/ledger/src/blockstore.rs @@ -5364,7 +5364,7 @@ pub mod tests { shred::{max_ticks_per_n_shreds, ShredFlags, LEGACY_SHRED_DATA_CAPACITY}, }, assert_matches::assert_matches, - bincode::serialize, + bincode::{serialize, Options}, crossbeam_channel::unbounded, rand::{seq::SliceRandom, thread_rng}, solana_account_decoder::parse_token::UiTokenAmount, @@ -5832,6 +5832,28 @@ pub mod tests { test_insert_data_shreds_slots(true); } + #[test] + fn test_index_fallback_deserialize() { + use rand::Rng; + let ledger_path = get_tmp_ledger_path_auto_delete!(); + let blockstore = Blockstore::open(ledger_path.path()).unwrap(); + let slot = rand::thread_rng().gen_range(0..100); + let bincode = bincode::DefaultOptions::new() + .reject_trailing_bytes() + .with_fixint_encoding(); + + let mut index_v1 = IndexV1::new(slot); + index_v1.data_mut().insert(10); + + blockstore + .index_cf + .put_bytes(slot, &bincode.serialize(&index_v1).unwrap()) + .unwrap(); + + let index_v2 = blockstore.index_cf.get(slot).unwrap().unwrap(); + assert!(index_v2.data().contains(10)) + } + /* #[test] pub fn test_iteration_order() { diff --git a/ledger/src/blockstore_meta.rs b/ledger/src/blockstore_meta.rs index 24e80e3401b638..05cd66f3b746ad 100644 --- a/ledger/src/blockstore_meta.rs +++ b/ledger/src/blockstore_meta.rs @@ -110,6 +110,7 @@ mod serde_compat { pub type Index = IndexV2; pub type ShredIndex = ShredIndexV2; pub type IndexFallback = IndexV1; +pub type ShredIndexFallback = ShredIndexV1; #[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] /// Index recording presence/absence of shreds @@ -277,6 +278,32 @@ impl Index { } } +#[cfg(test)] +#[allow(unused)] +impl IndexFallback { + pub(crate) fn new(slot: Slot) -> Self { + Self { + slot, + data: ShredIndexFallback::default(), + coding: ShredIndexFallback::default(), + } + } + + pub fn data(&self) -> &ShredIndexFallback { + &self.data + } + pub fn coding(&self) -> &ShredIndexFallback { + &self.coding + } + + pub(crate) fn data_mut(&mut self) -> &mut ShredIndexFallback { + &mut self.data + } + pub(crate) fn coding_mut(&mut self) -> &mut ShredIndexFallback { + &mut self.coding + } +} + /// Superseded by [`ShredIndexV2`]. /// /// TODO: Remove this once new [`ShredIndexV2`] is fully rolled out