Skip to content

Commit c084930

Browse files
committed
return err on ASM STF instead of panicking
1 parent e1cb212 commit c084930

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

crates/asm/common/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use strata_primitives::l1::L1VerificationError;
12
use thiserror::Error;
23

34
use crate::SubprotocolId;
@@ -24,4 +25,8 @@ pub enum AsmError {
2425
/// Failed to serialize the state of the given subprotocol.
2526
#[error("failed to serialize subprotocol {0} state: {1}")]
2627
Serialization(SubprotocolId, #[source] borsh::io::Error),
28+
29+
/// L1Header do not follow consensus rules.
30+
#[error("L1Header do not follow consensus rules")]
31+
InvalidL1Header(#[source] L1VerificationError),
2732
}

crates/asm/common/src/subprotocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub trait SubprotoHandler {
8383

8484
/// Manages the lifecycle and execution of subprotocol handlers in the Anchor State Machine (ASM).
8585
///
86-
/// Implementors of this trait maintain a collection of subprotocol handlers and
86+
/// Implementers of this trait maintain a collection of subprotocol handlers and
8787
/// provide methods to insert, remove, lookup, and drive execution (transactions and messages),
8888
/// as well as extract the final `SectionState`.
8989
pub trait SubprotocolManager: MsgRelayer + Sized {

crates/asm/stf/src/stage.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ impl FinishStage {
6666
Self { sections }
6767
}
6868

69-
pub(crate) fn into_sections(self) -> Vec<SectionState> {
69+
pub(crate) fn into_sorted_sections(mut self) -> Vec<SectionState> {
70+
self.sections.sort_by_key(|state| state.id);
7071
self.sections
7172
}
7273
}

crates/asm/stf/src/transition.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
//! view into a single deterministic state transition.
44
55
use bitcoin::{block::Block, params::Params};
6-
use strata_asm_common::{AnchorState, AsmSpec, ChainViewState, Stage, SubprotocolManager};
6+
use strata_asm_common::{
7+
AnchorState, AsmError, AsmSpec, ChainViewState, Stage, SubprotocolManager,
8+
};
79
use strata_asm_proto_bridge_v1::BridgeV1Subproto;
810
use strata_asm_proto_core::OLCoreSubproto;
911

@@ -26,13 +28,13 @@ impl AsmSpec for StrataAsmSpec {
2628

2729
/// Computes the next AnchorState by applying the Anchor State Machine (ASM) state transition
2830
/// function (STF) to the given previous state and new L1 block.
29-
pub fn asm_stf<S: AsmSpec>(pre_state: AnchorState, block: Block) -> AnchorState {
31+
pub fn asm_stf<S: AsmSpec>(pre_state: AnchorState, block: Block) -> Result<AnchorState, AsmError> {
3032
let mut pow_state = pre_state.chain_view.pow_state.clone();
3133

3234
// 1. Validate and update PoW header continuity for the new block
3335
pow_state
3436
.check_and_update_continuity(&block.header, &Params::MAINNET)
35-
.expect("header doesn't follow the consensus rules");
37+
.map_err(AsmError::InvalidL1Header)?;
3638

3739
// 2. Filter the relevant transactions
3840
let all_relevant_transactions = group_txs_by_subprotocol(&block.txdata);
@@ -51,10 +53,10 @@ pub fn asm_stf<S: AsmSpec>(pre_state: AnchorState, block: Block) -> AnchorState
5153
let mut finish_stage = FinishStage::new();
5254
S::call_subprotocols(&mut finish_stage, &mut manager);
5355

54-
let sections = finish_stage.into_sections();
56+
let sections = finish_stage.into_sorted_sections();
5557
let chain_view = ChainViewState { pow_state };
56-
AnchorState {
58+
Ok(AnchorState {
5759
chain_view,
5860
sections,
59-
}
61+
})
6062
}

0 commit comments

Comments
 (0)