AccMap struct for HashMap<[u8;32], Account>, serialization

This commit is contained in:
Rostyslav Tyshko 2025-06-11 01:22:48 -04:00
parent 222b0136bf
commit 62f06582ef
2 changed files with 66 additions and 3 deletions

View File

@ -12,6 +12,8 @@ use log::error;
use storage::sc_db_utils::{DataBlob, DataBlobChangeVariant};
use storage::RocksDBIO;
use crate::chain_storage::AccMap;
pub struct NodeBlockStore {
dbio: RocksDBIO,
}
@ -69,7 +71,8 @@ impl NodeBlockStore {
}
pub fn get_snapshot_account(&self) -> Result<HashMap<[u8; 32], Account>> {
Ok(serde_json::from_slice(&self.dbio.get_snapshot_account()?)?)
let temp: AccMap = serde_json::from_slice(&self.dbio.get_snapshot_account()?)?;
Ok(temp.into())
}
pub fn get_snapshot_commitment(&self) -> Result<HashStorageMerkleTree<UTXOCommitment>> {

View File

@ -12,6 +12,7 @@ use common::{
use k256::AffinePoint;
use log::{info, warn};
use public_context::PublicSCContext;
use serde::{Deserialize, Serialize};
use utxo::utxo_core::UTXO;
use crate::{config::NodeConfig, ActionData};
@ -20,6 +21,32 @@ pub mod accounts_store;
pub mod block_store;
pub mod public_context;
#[derive(Deserialize, Serialize)]
pub struct AccMap {
pub acc_map: HashMap<String, Account>,
}
impl From<HashMap<[u8; 32], Account>> for AccMap {
fn from(value: HashMap<[u8; 32], Account>) -> Self {
AccMap {
acc_map: value
.into_iter()
.map(|(key, val)| (hex::encode(key), val))
.collect(),
}
}
}
impl From<AccMap> for HashMap<[u8; 32], Account> {
fn from(value: AccMap) -> Self {
value
.acc_map
.into_iter()
.map(|(key, val)| (hex::decode(key).unwrap().try_into().unwrap(), val))
.collect()
}
}
pub struct NodeChainStore {
pub acc_map: HashMap<AccountAddress, Account>,
pub block_store: NodeBlockStore,
@ -51,6 +78,38 @@ impl NodeChainStore {
block_id = temp_block_id;
}
Ok((
Self {
acc_map: From::from(acc_map),
block_store,
nullifier_store,
utxo_commitments_store,
pub_tx_store,
node_config: config,
},
block_id,
))
}
pub fn new_after_restart(config: NodeConfig, genesis_block: Block) -> Result<(Self, u64)> {
let mut acc_map = HashMap::new();
let mut nullifier_store = HashSet::new();
let mut utxo_commitments_store = UTXOCommitmentsMerkleTree::new(vec![]);
let mut pub_tx_store = PublicTransactionMerkleTree::new(vec![]);
let mut block_id = genesis_block.block_id;
//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_reload(&config.home.join("rocksdb")).unwrap();
if let Ok(temp_block_id) = block_store.get_snapshot_block_id() {
utxo_commitments_store = block_store.get_snapshot_commitment()?;
nullifier_store = block_store.get_snapshot_nullifier()?;
acc_map = block_store.get_snapshot_account()?;
pub_tx_store = block_store.get_snapshot_transaction()?;
block_id = temp_block_id;
}
Ok((
Self {
acc_map,
@ -125,7 +184,7 @@ impl NodeChainStore {
let nonce =
accounts::key_management::constants_types::Nonce::clone_from_slice(slice);
for (acc_id, acc) in self.acc_map.iter_mut() {
if acc_id[0] == tag {
if hex::decode(acc_id).unwrap()[0] == tag {
let decoded_data_curr_acc = acc.decrypt_data(
ephemeral_public_key_sender,
ciphertext.clone(),
@ -156,8 +215,9 @@ impl NodeChainStore {
//If we fail serialization, it is not the reason to stop running
//Logging on warn level in this cases
let acc_map: AccMap = self.acc_map.clone().into();
if let Ok(accounts_ser) = serde_json::to_vec(&self.acc_map).inspect_err(|err| {
if let Ok(accounts_ser) = serde_json::to_vec(&acc_map).inspect_err(|err| {
warn!("Failed to serialize accounts data {err:#?}");
}) {
if let Ok(comm_ser) =