From 7f075fcdd3104d4138176651d6e542e265f032b1 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Sat, 25 Oct 2025 00:30:04 -0300 Subject: [PATCH] wip --- sequencer_core/src/lib.rs | 40 ++++++++++++++----- .../src/sequencer_store/block_store.rs | 6 +-- sequencer_core/src/sequencer_store/mod.rs | 19 +++------ sequencer_runner/Cargo.toml | 1 + storage/src/lib.rs | 3 +- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/sequencer_core/src/lib.rs b/sequencer_core/src/lib.rs index 7366471..6bd10de 100644 --- a/sequencer_core/src/lib.rs +++ b/sequencer_core/src/lib.rs @@ -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( diff --git a/sequencer_core/src/sequencer_store/block_store.rs b/sequencer_core/src/sequencer_store/block_store.rs index e1dbb2b..6fd9fb7 100644 --- a/sequencer_core/src/sequencer_store/block_store.rs +++ b/sequencer_core/src/sequencer_store/block_store.rs @@ -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, + pub tx_hash_to_block_map: HashMap, 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 { +pub(crate) fn block_to_transactions_map(block: &Block) -> HashMap { block .body .transactions diff --git a/sequencer_core/src/sequencer_store/mod.rs b/sequencer_core/src/sequencer_store/mod.rs index 4f18405..f6ca478 100644 --- a/sequencer_core/src/sequencer_store/mod.rs +++ b/sequencer_core/src/sequencer_store/mod.rs @@ -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 } } } diff --git a/sequencer_runner/Cargo.toml b/sequencer_runner/Cargo.toml index 4da788d..2f105ee 100644 --- a/sequencer_runner/Cargo.toml +++ b/sequencer_runner/Cargo.toml @@ -22,6 +22,7 @@ path = "../sequencer_rpc" [dependencies.sequencer_core] path = "../sequencer_core" +features = ["testnet"] [dependencies.common] path = "../common" diff --git a/storage/src/lib.rs b/storage/src/lib.rs index d69c521..cbce767 100644 --- a/storage/src/lib.rs +++ b/storage/src/lib.rs @@ -44,7 +44,7 @@ pub struct RocksDBIO { } impl RocksDBIO { - pub fn new(path: &Path, start_block: Option) -> DbResult { + pub fn open_or_create(path: &Path, start_block: Option) -> DbResult { 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)