This commit is contained in:
Sergio Chouhy 2025-10-25 00:30:04 -03:00
parent f96f4d9117
commit 7f075fcdd3
5 changed files with 41 additions and 28 deletions

View File

@ -12,6 +12,8 @@ use mempool::MemPool;
use sequencer_store::SequecerChainStore;
use serde::{Deserialize, Serialize};
use crate::sequencer_store::block_store::block_to_transactions_map;
pub mod config;
pub mod sequencer_store;
@ -53,19 +55,39 @@ impl SequencerCore {
initial_commitments.push(comm);
}
Self {
store: SequecerChainStore::new_with_genesis(
&config.home,
config.genesis_id,
config.is_genesis_random,
&config.initial_accounts,
&initial_commitments,
nssa::PrivateKey::try_new(config.signing_key).unwrap(),
),
let store = SequecerChainStore::new_with_genesis(
&config.home,
config.genesis_id,
config.is_genesis_random,
&config.initial_accounts,
&initial_commitments,
nssa::PrivateKey::try_new(config.signing_key).unwrap(),
);
let mut block_id = config.genesis_id;
let mut this = Self {
store,
mempool: MemPool::default(),
chain_height: config.genesis_id,
sequencer_config: config,
};
loop {
let Ok(block) = this.store.block_store.get_block_at_id(block_id) else {
break;
};
for encoded_transaction in block.body.transactions {
let transaction = NSSATransaction::try_from(&encoded_transaction).unwrap();
let transaction = this.transaction_pre_check(transaction).unwrap();
this.execute_check_transaction_on_state(transaction).unwrap();
this.store.block_store.tx_hash_to_block_map.insert(encoded_transaction.hash(), block_id);
}
block_id +=1;
}
this
}
pub fn transaction_pre_check(

View File

@ -7,7 +7,7 @@ use storage::RocksDBIO;
pub struct SequecerBlockStore {
dbio: RocksDBIO,
// TODO: Consider adding the hashmap to the database for faster recovery.
tx_hash_to_block_map: HashMap<HashType, u64>,
pub tx_hash_to_block_map: HashMap<HashType, u64>,
pub genesis_id: u64,
pub signing_key: nssa::PrivateKey,
}
@ -28,7 +28,7 @@ impl SequecerBlockStore {
HashMap::new()
};
let dbio = RocksDBIO::new(location, genesis_block)?;
let dbio = RocksDBIO::open_or_create(location, genesis_block)?;
let genesis_id = dbio.get_meta_first_block_in_db()?;
@ -71,7 +71,7 @@ impl SequecerBlockStore {
}
}
fn block_to_transactions_map(block: &Block) -> HashMap<HashType, u64> {
pub(crate) fn block_to_transactions_map(block: &Block) -> HashMap<HashType, u64> {
block
.body
.transactions

View File

@ -1,11 +1,11 @@
use std::path::Path;
use block_store::SequecerBlockStore;
use common::block::HashableBlockData;
use common::{block::HashableBlockData, transaction::{self, NSSATransaction}};
use nssa::{self, Address};
use rand::{RngCore, rngs::OsRng};
use crate::config::AccountInitialData;
use crate::{config::AccountInitialData, sequencer_store::block_store::block_to_transactions_map};
pub mod block_store;
@ -39,21 +39,11 @@ impl SequecerChainStore {
this
};
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 curr_time = chrono::Utc::now().timestamp_millis() as u64;
let hashable_data = HashableBlockData {
block_id: genesis_id,
transactions: vec![],
prev_block_hash,
timestamp: curr_time,
prev_block_hash: [0; 32],
timestamp: 0,
};
let genesis_block = hashable_data.into_block(&signing_key);
@ -67,6 +57,7 @@ impl SequecerChainStore {
)
.unwrap();
Self { state, block_store }
}
}

View File

@ -22,6 +22,7 @@ path = "../sequencer_rpc"
[dependencies.sequencer_core]
path = "../sequencer_core"
features = ["testnet"]
[dependencies.common]
path = "../common"

View File

@ -44,7 +44,7 @@ pub struct RocksDBIO {
}
impl RocksDBIO {
pub fn new(path: &Path, start_block: Option<Block>) -> DbResult<Self> {
pub fn open_or_create(path: &Path, start_block: Option<Block>) -> DbResult<Self> {
let mut cf_opts = Options::default();
cf_opts.set_max_write_buffer_number(16);
//ToDo: Add more column families for different data
@ -74,7 +74,6 @@ impl RocksDBIO {
let block_id = block.header.block_id;
dbio.put_meta_first_block_in_db(block)?;
dbio.put_meta_is_first_block_set()?;
dbio.put_meta_last_block_in_db(block_id)?;
Ok(dbio)