Skip to content

Commit

Permalink
Updates frozen abi test for bank snapshot serialization (#1544)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored May 30, 2024
1 parent f954fba commit 97d36e6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 39 deletions.
1 change: 1 addition & 0 deletions accounts-db/src/epoch_accounts_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod manager;
pub use manager::Manager as EpochAccountsHashManager;

/// The EpochAccountsHash holds the result after calculating the accounts hash once per epoch
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Debug, Serialize, Deserialize, Hash, PartialEq, Eq, Clone, Copy)]
pub struct EpochAccountsHash(Hash);

Expand Down
34 changes: 23 additions & 11 deletions runtime/src/bank/serde_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ mod tests {
let mut buf = Vec::new();
let cursor = Cursor::new(&mut buf);
let mut writer = BufWriter::new(cursor);
serde_snapshot::serialize_bank_snapshot(
serde_snapshot::serialize_bank_snapshot_into(
&mut writer,
bank2.get_fields_to_serialize(),
&bank2.rc.accounts.accounts_db,
Expand Down Expand Up @@ -718,22 +718,22 @@ mod tests {

#[cfg(RUSTC_WITH_SPECIALIZATION)]
mod test_bank_serialize {
use {super::*, crate::serde_snapshot::serialize_test_bank_and_storage};
use {super::*, solana_sdk::clock::Slot};

// This some what long test harness is required to freeze the ABI of
// Bank's serialization due to versioned nature
#[cfg_attr(
feature = "frozen-abi",
derive(AbiExample),
frozen_abi(digest = "7K1xfUkoCwhxssszgoSpbeMCcX3KEyjycGyLXrpFaJNe")
frozen_abi(digest = "6riNuebfnAUpS2e3GYb5G8udH5PoEtep48ULchLjRDCB")
)]
#[derive(Serialize)]
pub struct BankAbiTestWrapperNewer {
pub struct BankAbiTestWrapper {
#[serde(serialize_with = "wrapper")]
bank: Bank,
}

pub fn wrapper<S>(bank: &Bank, s: S) -> std::result::Result<S::Ok, S::Error>
pub fn wrapper<S>(bank: &Bank, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
Expand All @@ -745,14 +745,26 @@ mod tests {
bank.slot(),
(AccountsHash(Hash::new_unique()), u64::default()),
);
let snapshot_storages = bank.rc.accounts.accounts_db.get_snapshot_storages(..=0).0;
// ensure there is a single snapshot storage example for ABI digesting
assert_eq!(snapshot_storages.len(), 1);

serialize_test_bank_and_storage::<S>(
bank,
let snapshot_storages = bank.get_snapshot_storages(None);
// ensure there is at least one snapshot storage example for ABI digesting
assert!(!snapshot_storages.is_empty());

let incremental_snapshot_persistence = BankIncrementalSnapshotPersistence {
full_slot: Slot::default(),
full_hash: SerdeAccountsHash(Hash::new_unique()),
full_capitalization: u64::default(),
incremental_hash: SerdeIncrementalAccountsHash(Hash::new_unique()),
incremental_capitalization: u64::default(),
};

serde_snapshot::serialize_bank_snapshot_with(
serializer,
bank.get_fields_to_serialize(),
&bank.rc.accounts.accounts_db,
&get_storages_to_serialize(&snapshot_storages),
s,
Some(&incremental_snapshot_persistence),
Some(EpochAccountsHash::new(Hash::new_unique())),
)
}
}
Expand Down
63 changes: 36 additions & 27 deletions runtime/src/serde_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,8 @@ where
)
}

/// Serializes bank snapshot into `stream`
pub fn serialize_bank_snapshot<W>(
/// Serializes bank snapshot into `stream` with bincode
pub fn serialize_bank_snapshot_into<W>(
stream: &mut BufWriter<W>,
bank_fields: BankFieldsToSerialize,
accounts_db: &AccountsDb,
Expand All @@ -591,6 +591,32 @@ pub fn serialize_bank_snapshot<W>(
) -> Result<(), Error>
where
W: Write,
{
let mut serializer = bincode::Serializer::new(
stream,
bincode::DefaultOptions::new().with_fixint_encoding(),
);
serialize_bank_snapshot_with(
&mut serializer,
bank_fields,
accounts_db,
account_storage_entries,
incremental_snapshot_persistence,
epoch_accounts_hash,
)
}

/// Serializes bank snapshot with `serializer`
pub fn serialize_bank_snapshot_with<S>(
serializer: S,
bank_fields: BankFieldsToSerialize,
accounts_db: &AccountsDb,
account_storage_entries: &[Vec<Arc<AccountStorageEntry>>],
incremental_snapshot_persistence: Option<&BankIncrementalSnapshotPersistence>,
epoch_accounts_hash: Option<EpochAccountsHash>,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let slot = bank_fields.slot;
let lamports_per_signature = bank_fields.fee_rate_governor.lamports_per_signature;
Expand All @@ -600,16 +626,15 @@ where
slot,
account_storage_entries,
};
bincode::serialize_into(
stream,
&(
serializable_bank,
serializable_accounts_db,
lamports_per_signature,
incremental_snapshot_persistence,
epoch_accounts_hash,
),

(
serializable_bank,
serializable_accounts_db,
lamports_per_signature,
incremental_snapshot_persistence,
epoch_accounts_hash,
)
.serialize(serializer)
}

/// deserialize the bank from 'stream_reader'
Expand Down Expand Up @@ -749,22 +774,6 @@ impl<'a> Serialize for SerializableBankAndStorage<'a> {
}
}

#[cfg(test)]
pub fn serialize_test_bank_and_storage<S>(
bank: &Bank,
storage: &[Vec<Arc<AccountStorageEntry>>],
s: S,
) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
SerializableBankAndStorage {
bank,
snapshot_storages: storage,
}
.serialize(s)
}

#[cfg(test)]
struct SerializableBankAndStorageNoExtra<'a> {
bank: &'a Bank,
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/snapshot_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ fn serialize_snapshot(
);

let bank_snapshot_serializer = move |stream: &mut BufWriter<fs::File>| -> Result<()> {
serde_snapshot::serialize_bank_snapshot(
serde_snapshot::serialize_bank_snapshot_into(
stream,
bank_fields,
accounts_db,
Expand Down

0 comments on commit 97d36e6

Please sign in to comment.