Merge pull request #29 from vacp2p/Pravdyvy/node-core-strucutures-and-account-management

Node core strucutures and account management
This commit is contained in:
tyshko-rostyslav
2024-12-08 18:45:07 -05:00
committed by GitHub
10 changed files with 448 additions and 16 deletions
+11
View File
@@ -0,0 +1,11 @@
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeConfig {
///Home dir of sequencer storage
pub home: PathBuf,
///Override rust log (env var logging level)
pub override_rust_log: Option<String>,
}
+14
View File
@@ -1 +1,15 @@
use accounts::account_core::AccountAddress;
use config::NodeConfig;
use storage::NodeChainStore;
pub mod config;
pub mod executions;
pub mod sequencer_client;
pub mod storage;
pub struct NodeCore {
pub storage: NodeChainStore,
pub curr_height: u64,
pub main_acc_addr: AccountAddress,
pub node_config: NodeConfig,
}
+1
View File
@@ -0,0 +1 @@
+8
View File
@@ -0,0 +1,8 @@
use crate::config::NodeConfig;
pub mod json;
pub struct SequencerClient {
pub client: reqwest::Client,
pub config: NodeConfig,
}
+28
View File
@@ -0,0 +1,28 @@
use accounts::account_core::{Account, AccountAddress};
use std::collections::HashMap;
pub struct NodeAccountsStore {
pub accounts: HashMap<AccountAddress, Account>,
}
impl NodeAccountsStore {
pub fn new() -> Self {
Self {
accounts: HashMap::new(),
}
}
pub fn register_account(&mut self, account: Account) {
self.accounts.insert(account.address, account);
}
pub fn unregister_account(&mut self, account_addr: AccountAddress) {
self.accounts.remove(&account_addr);
}
}
impl Default for NodeAccountsStore {
fn default() -> Self {
Self::new()
}
}
+33
View File
@@ -0,0 +1,33 @@
use std::path::Path;
use anyhow::Result;
use storage::{block::Block, RocksDBIO};
pub struct NodeBlockStore {
dbio: RocksDBIO,
}
impl NodeBlockStore {
///Starting database at the start of new chain.
/// Creates files if necessary.
///
/// ATTENTION: Will overwrite genesis block.
pub fn open_db_with_genesis(location: &Path, genesis_block: Option<Block>) -> Result<Self> {
Ok(Self {
dbio: RocksDBIO::new(location, genesis_block)?,
})
}
///Reopening existing database
pub fn open_db_restart(location: &Path) -> Result<Self> {
NodeBlockStore::open_db_with_genesis(location, None)
}
pub fn get_block_at_id(&self, id: u64) -> Result<Block> {
Ok(self.dbio.get_block(id)?)
}
pub fn put_block_at_id(&self, block: Block) -> Result<()> {
Ok(self.dbio.put_block(block)?)
}
}
+71
View File
@@ -0,0 +1,71 @@
use std::path::Path;
use accounts::account_core::{Account, AccountAddress};
use accounts_store::NodeAccountsStore;
use block_store::NodeBlockStore;
use rand::{rngs::OsRng, RngCore};
use storage::{
block::{Block, HashableBlockData},
merkle_tree_public::merkle_tree::{PublicTransactionMerkleTree, UTXOCommitmentsMerkleTree},
nullifier_sparse_merkle_tree::NullifierSparseMerkleTree,
};
pub mod accounts_store;
pub mod block_store;
pub struct NodeChainStore {
pub acc_store: NodeAccountsStore,
pub block_store: NodeBlockStore,
pub nullifier_store: NullifierSparseMerkleTree,
pub utxo_commitments_store: UTXOCommitmentsMerkleTree,
pub pub_tx_store: PublicTransactionMerkleTree,
///For simplicity, we will allow only one account per node.
/// ToDo: Change it in future
node_main_account_info: Account,
}
impl NodeChainStore {
pub fn new_with_genesis(home_dir: &Path, genesis_id: u64, is_genesis_random: bool) -> Self {
let acc_store = NodeAccountsStore::default();
let nullifier_store = NullifierSparseMerkleTree::default();
let utxo_commitments_store = UTXOCommitmentsMerkleTree::new(vec![]);
let pub_tx_store = PublicTransactionMerkleTree::new(vec![]);
let mut data = [0; 32];
let mut prev_block_hash = [0; 32];
if is_genesis_random {
OsRng.fill_bytes(&mut data);
OsRng.fill_bytes(&mut prev_block_hash);
}
let hashable_data = HashableBlockData {
block_id: genesis_id,
prev_block_id: genesis_id.saturating_sub(1),
transactions: vec![],
data: data.to_vec(),
prev_block_hash,
};
let genesis_block = Block::produce_block_from_hashable_data(hashable_data);
//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))
.unwrap();
Self {
acc_store,
block_store,
nullifier_store,
utxo_commitments_store,
pub_tx_store,
node_main_account_info: Account::new(),
}
}
pub fn get_main_account_addr(&self) -> AccountAddress {
self.node_main_account_info.address
}
}