Skip to content

Commit f51ab17

Browse files
committed
update: simplified codebase
1 parent 9ddce95 commit f51ab17

File tree

5 files changed

+27
-71
lines changed

5 files changed

+27
-71
lines changed

madara/crates/client/block_production/src/executor/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ pub enum ExecutorCommandError {
3434
pub enum ExecutorCommand {
3535
/// Force close the current block.
3636
CloseBlock(oneshot::Sender<Result<(), ExecutorCommandError>>),
37-
/// Reset executor state after a reorg. This reinitializes the state adapter from the current database state.
38-
ResetState(oneshot::Sender<Result<(), ExecutorCommandError>>),
3937
}
4038

4139
#[derive(Debug)]
@@ -52,8 +50,6 @@ pub enum ExecutorMessage {
5250
},
5351
BatchExecuted(BatchExecutionResult),
5452
EndBlock(Box<BlockExecutionSummary>),
55-
/// Notifies the task that executor state was reset after a reorg
56-
StateReset { latest_block_n: Option<u64> },
5753
}
5854

5955
#[derive(Default, Debug)]

madara/crates/client/block_production/src/executor/thread.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -281,45 +281,6 @@ impl ExecutorThread {
281281
let _ = callback.send(Ok(()));
282282
Default::default()
283283
}
284-
super::ExecutorCommand::ResetState(callback) => {
285-
tracing::info!("🔄 Resetting executor state after reorg");
286-
// Reinitialize the state adapter from the current database state
287-
let new_state_adaptor = match mc_exec::LayeredStateAdapter::new(self.backend.clone()) {
288-
Ok(adaptor) => adaptor,
289-
Err(e) => {
290-
tracing::error!("Failed to reinitialize state adapter after reorg: {:#}", e);
291-
let _ = callback.send(Err(super::ExecutorCommandError::ChannelClosed));
292-
return Err(e.into());
293-
}
294-
};
295-
296-
let latest_block_n = new_state_adaptor.previous_block_n();
297-
tracing::info!("✅ State adapter reinitialized to block_n={}", new_state_adaptor.block_n());
298-
299-
// Reset the executor state to NewBlock with the fresh state adapter
300-
state = ExecutorThreadState::NewBlock(ExecutorStateNewBlock {
301-
state_adaptor: new_state_adaptor,
302-
consumed_l1_to_l2_nonces: HashSet::new(),
303-
});
304-
305-
// Clear any pending transactions by clearing both fields
306-
to_exec.txs.clear();
307-
to_exec.additional_info.clear();
308-
309-
// Reset block state flags
310-
block_empty = true;
311-
force_close = false;
312-
l2_gas_consumed_block = 0;
313-
314-
// Notify the main task that state was reset
315-
if self.replies_sender.blocking_send(super::ExecutorMessage::StateReset { latest_block_n }).is_err() {
316-
tracing::error!("Failed to send StateReset message to block production task");
317-
}
318-
319-
let _ = callback.send(Ok(()));
320-
// Continue the loop to wait for new transactions - don't return a batch
321-
continue;
322-
}
323284
},
324285
// Channel closed. Exit gracefully.
325286
WaitTxBatchOutcome::Exit => return Ok(()),

madara/crates/client/block_production/src/handle.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,6 @@ impl BlockProductionHandle {
7070
recv.await.map_err(|_| ExecutorCommandError::ChannelClosed)?
7171
}
7272

73-
/// Reset the executor state after a reorg. This reinitializes the state adapter from the current database state.
74-
pub async fn reset_state(&self) -> Result<(), ExecutorCommandError> {
75-
let (sender, recv) = oneshot::channel();
76-
self.executor_commands
77-
.send(ExecutorCommand::ResetState(sender))
78-
.map_err(|_| ExecutorCommandError::ChannelClosed)?;
79-
recv.await.map_err(|_| ExecutorCommandError::ChannelClosed)?
80-
}
81-
8273
/// Send a transaction through the bypass channel to bypass mempool and validation.
8374
pub async fn send_tx_raw(&self, tx: ValidatedTransaction) -> Result<(), ExecutorCommandError> {
8475
self.bypass_input.send(tx).await.map_err(|_| ExecutorCommandError::ChannelClosed)

madara/crates/client/block_production/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ pub use handle::BlockProductionHandle;
115115
pub enum BlockProductionStateNotification {
116116
ClosedBlock,
117117
BatchExecuted,
118-
StateReset,
119118
}
120119

121120
#[derive(Debug)]
@@ -626,11 +625,6 @@ impl BlockProductionTask {
626625
/// Handles the state machine and its transitions.
627626
async fn process_reply(&mut self, reply: ExecutorMessage) -> anyhow::Result<()> {
628627
match reply {
629-
ExecutorMessage::StateReset { latest_block_n } => {
630-
tracing::info!("🔄 Resetting block production task state after reorg to latest_block_n={:?}", latest_block_n);
631-
self.current_state = Some(TaskState::NotExecuting { latest_block_n });
632-
self.send_state_notification(BlockProductionStateNotification::StateReset);
633-
}
634628
ExecutorMessage::StartNewBlock { exec_ctx } => {
635629
tracing::debug!("Received ExecutorMessage::StartNewBlock block_n={}", exec_ctx.block_number);
636630
let current_state = self.current_state.take().context("No current state")?;

madara/crates/client/rpc/src/versions/admin/v0_1_0/methods/write.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use mp_rpc::v0_9_0::{
1111
ClassAndTxnHash, ContractAndTxnHash,
1212
};
1313
use mp_transactions::{L1HandlerTransactionResult, L1HandlerTransactionWithFee};
14-
use mp_utils::service::MadaraServiceId;
14+
use mp_utils::service::{MadaraServiceId, MadaraServiceStatus};
15+
use std::time::Duration;
1516
#[async_trait]
1617
impl MadaraWriteRpcApiV0_1_0Server for Starknet {
1718
/// Submit a new class v0 declaration transaction, bypassing mempool and all validation.
@@ -126,7 +127,26 @@ impl MadaraWriteRpcApiV0_1_0Server for Starknet {
126127
}
127128
}
128129

130+
// Check if block production is currently running (sequencer mode)
131+
let bp_was_running = matches!(
132+
self.ctx.service_status(MadaraServiceId::BlockProduction),
133+
MadaraServiceStatus::On
134+
);
135+
136+
// If running, shut down block production cleanly before reorg
137+
if bp_was_running {
138+
tracing::info!("🔌 Stopping block production service for reorg...");
139+
self.ctx.service_remove(MadaraServiceId::BlockProduction);
140+
// Allow time for clean shutdown of executor threads and channels
141+
tokio::time::sleep(Duration::from_millis(100)).await;
142+
tracing::info!("✅ Block production service stopped");
143+
}
144+
145+
// Perform the blockchain reorg
146+
tracing::info!("🔄 Reverting blockchain to block {}", target_block_n);
129147
self.backend.revert_to(&block_hash).map_err(StarknetRpcApiError::from)?;
148+
149+
// Update chain tip cache
130150
let fresh_chain_tip = self
131151
.backend
132152
.db
@@ -135,19 +155,13 @@ impl MadaraWriteRpcApiV0_1_0Server for Starknet {
135155
.map_err(StarknetRpcApiError::from)?;
136156
let backend_chain_tip = mc_db::ChainTip::from_storage(fresh_chain_tip);
137157
self.backend.chain_tip.send_replace(backend_chain_tip);
158+
tracing::info!("✅ Blockchain reverted successfully");
138159

139-
// Reset block production executor state if block production is enabled
140-
let bp_status = self.ctx.service_status(MadaraServiceId::BlockProduction);
141-
if matches!(bp_status, mp_utils::service::MadaraServiceStatus::On) {
142-
if let Some(block_prod_handle) = &self.block_prod_handle {
143-
tracing::debug!("revertTo: resetting block production state");
144-
block_prod_handle
145-
.reset_state()
146-
.await
147-
.context("Resetting block production executor state after reorg")
148-
.map_err(StarknetRpcApiError::from)?;
149-
tracing::debug!("revertTo: block production state reset complete");
150-
}
160+
// Restart block production if it was running before
161+
if bp_was_running {
162+
tracing::info!("🔌 Restarting block production service after reorg...");
163+
self.ctx.service_add(MadaraServiceId::BlockProduction);
164+
tracing::info!("✅ Block production service restarted with fresh state");
151165
}
152166

153167
Ok(())

0 commit comments

Comments
 (0)