feat: periodic snapshooting

This commit is contained in:
Oleksandr Pravdyvyi
2025-05-29 12:00:50 +03:00
parent 0d577da288
commit 66def746e6
8 changed files with 99 additions and 9 deletions
@@ -2,6 +2,7 @@ use std::path::Path;
use anyhow::{anyhow, Result};
use common::block::Block;
use log::warn;
use storage::sc_db_utils::{DataBlob, DataBlobChangeVariant};
use storage::RocksDBIO;
@@ -56,6 +57,35 @@ impl NodeBlockStore {
pub fn get_sc_sc_state(&self, sc_addr: &str) -> Result<Vec<DataBlob>> {
Ok(self.dbio.get_sc_sc_state(sc_addr)?)
}
pub fn put_snapshot_at_block_id(
&self,
id: u64,
accounts_ser: Vec<u8>,
comm_ser: Vec<u8>,
txs_ser: Vec<u8>,
nullifiers_ser: Vec<u8>,
) -> Result<()> {
self.dbio
.put_snapshot_block_id_db(id)
.inspect_err(|err| warn!("Failed to store snapshot block id with error {err:#?}"))?;
self.dbio
.put_snapshot_account_db(accounts_ser)
.inspect_err(|err| warn!("Failed to store snapshot accounts with error {err:#?}"))?;
self.dbio
.put_snapshot_commitement_db(comm_ser)
.inspect_err(|err| warn!("Failed to store snapshot commitments with error {err:#?}"))?;
self.dbio
.put_snapshot_transaction_db(txs_ser)
.inspect_err(|err| {
warn!("Failed to store snapshot transactions with error {err:#?}")
})?;
self.dbio
.put_snapshot_account_db(nullifiers_ser)
.inspect_err(|err| warn!("Failed to store snapshot nullifiers with error {err:#?}"))?;
Ok(())
}
}
#[cfg(test)]
+57 -3
View File
@@ -13,10 +13,11 @@ use common::{
utxo_commitment::UTXOCommitment,
};
use k256::AffinePoint;
use log::{info, warn};
use public_context::PublicSCContext;
use utxo::utxo_core::UTXO;
use crate::ActionData;
use crate::{config::NodeConfig, ActionData};
pub mod accounts_store;
pub mod block_store;
@@ -28,10 +29,11 @@ pub struct NodeChainStore {
pub nullifier_store: HashSet<UTXONullifier>,
pub utxo_commitments_store: UTXOCommitmentsMerkleTree,
pub pub_tx_store: PublicTransactionMerkleTree,
pub node_config: NodeConfig,
}
impl NodeChainStore {
pub fn new_with_genesis(home_dir: &Path, genesis_block: Block) -> Self {
pub fn new_with_genesis(config: NodeConfig, genesis_block: Block) -> Self {
let acc_map = HashMap::new();
let nullifier_store = HashSet::new();
let utxo_commitments_store = UTXOCommitmentsMerkleTree::new(vec![]);
@@ -40,7 +42,7 @@ impl NodeChainStore {
//Sequencer should panic if unable to open db,
//as fixing this issue may require actions non-native to program scope
let block_store =
NodeBlockStore::open_db_with_genesis(&home_dir.join("rocksdb"), Some(genesis_block))
NodeBlockStore::open_db_with_genesis(&config.home.join("rocksdb"), Some(genesis_block))
.unwrap();
Self {
@@ -49,10 +51,13 @@ impl NodeChainStore {
nullifier_store,
utxo_commitments_store,
pub_tx_store,
node_config: config,
}
}
pub fn dissect_insert_block(&mut self, block: Block) -> Result<()> {
let block_id = block.block_id;
for tx in &block.transactions {
if !tx.execution_input.is_empty() {
let public_action = serde_json::from_slice::<ActionData>(&tx.execution_input);
@@ -136,6 +141,55 @@ impl NodeChainStore {
self.block_store.put_block_at_id(block)?;
//Snapshot
if block_id % self.node_config.shapshot_frequency_in_blocks == 0 {
//Serializing all important data structures
//If we fail snapshot, it is not the reason to stop running
//Logging on warn level in this cases
let accounts_ser = serde_json::to_vec(&self.acc_map).inspect_err(|err| {
warn!("Failed to serialize accounts data {err:#?}");
});
let comm_tree_serialized = serde_json::to_vec(&self.utxo_commitments_store)
.inspect_err(|err| {
warn!("Failed to serialize commitments {err:#?}");
});
let tx_tree_serialized = serde_json::to_vec(&self.pub_tx_store).inspect_err(|err| {
warn!("Failed to serialize transactions {err:#?}");
});
let nullifiers_serialized =
serde_json::to_vec(&self.nullifier_store).inspect_err(|err| {
warn!("Failed to serialize nullifiers {err:#?}");
});
match (
accounts_ser,
comm_tree_serialized,
tx_tree_serialized,
nullifiers_serialized,
) {
(Ok(accounts_ser), Ok(comm_ser), Ok(txs_ser), Ok(nullifiers_ser)) => {
let snapshot_trace = self.block_store.put_snapshot_at_block_id(
block_id,
accounts_ser,
comm_ser,
txs_ser,
nullifiers_ser,
);
info!(
"Snapshot executed at {:?} with results {snapshot_trace:#?}",
block_id
);
}
_ => warn!("Failed to serialize node data for snapshot"),
}
}
Ok(())
}
+2
View File
@@ -49,4 +49,6 @@ pub struct NodeConfig {
pub port: u16,
///Gas config
pub gas_config: GasConfig,
///Frequency of snapshots
pub shapshot_frequency_in_blocks: u64,
}
+2 -2
View File
@@ -94,11 +94,11 @@ impl NodeCore {
let client = Arc::new(SequencerClient::new(config.clone())?);
let genesis_id = client.get_genesis_id().await?;
info!("Gesesis id is {genesis_id:?}");
info!("Genesis id is {genesis_id:?}");
let genesis_block = client.get_block(genesis_id.genesis_id).await?.block;
let mut storage = NodeChainStore::new_with_genesis(&config.home, genesis_block);
let mut storage = NodeChainStore::new_with_genesis(config.clone(), genesis_block);
pre_start::setup_empty_sc_states(&storage).await?;