From ab2b16956ead1120e81c825a7fa1d6ce99bf734a Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Wed, 28 Jan 2026 15:53:29 -0300 Subject: [PATCH] minor refactor --- sequencer_core/src/block_store.rs | 2 +- sequencer_core/src/lib.rs | 57 +++++++++++++++++++------------ sequencer_runner/src/lib.rs | 4 ++- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/sequencer_core/src/block_store.rs b/sequencer_core/src/block_store.rs index 3f1eb3b8..3aed2143 100644 --- a/sequencer_core/src/block_store.rs +++ b/sequencer_core/src/block_store.rs @@ -80,7 +80,7 @@ impl SequencerStore { &self.signing_key } - pub fn get_pending_blocks(&self) -> impl Iterator> { + pub fn get_all_blocks(&self) -> impl Iterator> { self.dbio.get_all_blocks().map(|res| Ok(res?)) } diff --git a/sequencer_core/src/lib.rs b/sequencer_core/src/lib.rs index 02a05231..2caa99ed 100644 --- a/sequencer_core/src/lib.rs +++ b/sequencer_core/src/lib.rs @@ -9,7 +9,7 @@ use common::{ transaction::{EncodedTransaction, NSSATransaction}, }; use config::SequencerConfig; -use log::warn; +use log::{info, warn}; use logos_blockchain_core::mantle::ops::channel::MsgId; use mempool::{MemPool, MemPoolHandle}; use serde::{Deserialize, Serialize}; @@ -66,29 +66,40 @@ impl SequencerCore { signing_key, ) .unwrap(); - let mut initial_commitments = vec![]; - for init_comm_data in config.initial_commitments.clone() { - let npk = init_comm_data.npk; + let mut state = match store.get_nssa_state() { + Some(state) => { + info!("Found local database. Loading state and pending blocks from it."); + state + }, + None => { + info!( + "No database found when starting the sequencer. Creating a fresh new with the initial data in config" + ); + let mut initial_commitments = vec![]; - let mut acc = init_comm_data.account; + for init_comm_data in config.initial_commitments.clone() { + let npk = init_comm_data.npk; - acc.program_owner = nssa::program::Program::authenticated_transfer_program().id(); + let mut acc = init_comm_data.account; - let comm = nssa_core::Commitment::new(&npk, &acc); + acc.program_owner = + nssa::program::Program::authenticated_transfer_program().id(); - initial_commitments.push(comm); - } + let comm = nssa_core::Commitment::new(&npk, &acc); - let init_accs: Vec<(nssa::AccountId, u128)> = config - .initial_accounts - .iter() - .map(|acc_data| (acc_data.account_id.parse().unwrap(), acc_data.balance)) - .collect(); + initial_commitments.push(comm); + } - let mut state = store.get_nssa_state().unwrap_or_else(|| { - nssa::V02State::new_with_genesis_accounts(&init_accs, &initial_commitments) - }); + let init_accs: Vec<(nssa::AccountId, u128)> = config + .initial_accounts + .iter() + .map(|acc_data| (acc_data.account_id.parse().unwrap(), acc_data.balance)) + .collect(); + + nssa::V02State::new_with_genesis_accounts(&init_accs, &initial_commitments) + } + }; #[cfg(feature = "testnet")] state.add_pinata_program(PINATA_BASE58.parse().unwrap()); @@ -227,12 +238,14 @@ impl SequencerCore { .try_for_each(|&id| self.store.delete_block_at_id(id)) } - pub fn get_pending_blocks(&self) -> Vec { - self.store - .get_pending_blocks() - .flatten() + pub fn get_pending_blocks(&self) -> Result> { + Ok(self + .store + .get_all_blocks() + .collect::>>()? + .into_iter() .filter(|block| matches!(block.bedrock_status, BedrockStatus::Pending)) - .collect() + .collect()) } pub fn block_settlement_client(&self) -> Option { diff --git a/sequencer_runner/src/lib.rs b/sequencer_runner/src/lib.rs index 3387c524..488b755f 100644 --- a/sequencer_runner/src/lib.rs +++ b/sequencer_runner/src/lib.rs @@ -57,7 +57,9 @@ pub async fn startup_sequencer( let (pending_blocks, block_settlement_client) = { let sequencer_core = seq_core_wrapped_for_block_retry.lock().await; let client = sequencer_core.block_settlement_client(); - let pending_blocks = sequencer_core.get_pending_blocks(); + let pending_blocks = sequencer_core + .get_pending_blocks() + .expect("Sequencer should be able to retrieve pending blocks"); (pending_blocks, client) };