1- //! WorkerContext implementation for ASM worker
1+ //! WorkerContext implementation for ASM worker.
2+ //!
3+ //! This is the glue between the worker (which uses `strata_asm_worker::AsmState`)
4+ //! and the storage layer (which uses `strata_state::asm_state::AsmState`).
5+ //! The two types are structurally identical — we convert between them at the
6+ //! persistence boundary.
27
38use std:: sync:: Arc ;
49
510use bitcoin:: { Block , BlockHash , Network } ;
611use bitcoind_async_client:: { Client , traits:: Reader } ;
712use strata_asm_common:: { AsmManifest , AuxData } ;
8- use strata_asm_worker:: { WorkerContext , WorkerError , WorkerResult } ;
13+ use strata_asm_worker:: { AsmState , WorkerContext , WorkerError , WorkerResult } ;
914use strata_btc_types:: { BitcoinTxid , L1BlockIdBitcoinExt , RawBitcoinTx } ;
1015use strata_identifiers:: { Hash , L1BlockCommitment , L1BlockId } ;
11- use strata_state:: asm_state:: AsmState ;
1216use strata_storage:: { AsmStateManager , MmrIndexHandle } ;
1317use tokio:: runtime:: Handle ;
1418
15- /// ASM [`WorkerContext`] implementation
19+ /// Convert storage-layer AsmState to worker AsmState.
20+ fn from_storage ( s : strata_state:: asm_state:: AsmState ) -> AsmState {
21+ AsmState :: new ( s. state ( ) . clone ( ) , s. logs ( ) . clone ( ) )
22+ }
23+
24+ /// Convert worker AsmState to storage-layer AsmState.
25+ fn to_storage ( s : & AsmState ) -> strata_state:: asm_state:: AsmState {
26+ strata_state:: asm_state:: AsmState :: new ( s. state ( ) . clone ( ) , s. logs ( ) . clone ( ) )
27+ }
28+
29+ /// ASM [`WorkerContext`] implementation.
1630///
17- /// This implementation fetches L1 blocks directly from a Bitcoin node
18- /// and uses SledDB for state storage.
31+ /// Fetches L1 blocks from a Bitcoin node and persists state via SledDB.
1932pub ( crate ) struct AsmWorkerContext {
2033 runtime_handle : Handle ,
2134 bitcoin_client : Arc < Client > ,
@@ -24,7 +37,6 @@ pub(crate) struct AsmWorkerContext {
2437}
2538
2639impl AsmWorkerContext {
27- /// Create a new BridgeWorkerContext
2840 pub ( crate ) const fn new (
2941 runtime_handle : Handle ,
3042 bitcoin_client : Arc < Client > ,
@@ -42,7 +54,6 @@ impl AsmWorkerContext {
4254
4355impl WorkerContext for AsmWorkerContext {
4456 fn get_l1_block ( & self , blockid : & L1BlockId ) -> WorkerResult < Block > {
45- // Fetch block directly from Bitcoin node by hash
4657 let block_hash: BlockHash = blockid. to_block_hash ( ) ;
4758 self . runtime_handle
4859 . block_on ( self . bitcoin_client . get_block ( & block_hash) )
@@ -52,12 +63,14 @@ impl WorkerContext for AsmWorkerContext {
5263 fn get_latest_asm_state ( & self ) -> WorkerResult < Option < ( L1BlockCommitment , AsmState ) > > {
5364 self . asm_manager
5465 . fetch_most_recent_state ( )
66+ . map ( |opt| opt. map ( |( id, s) | ( id, from_storage ( s) ) ) )
5567 . map_err ( |_| WorkerError :: DbError )
5668 }
5769
5870 fn get_anchor_state ( & self , blockid : & L1BlockCommitment ) -> WorkerResult < AsmState > {
5971 self . asm_manager
6072 . get_state ( * blockid)
73+ . map ( |opt| opt. map ( from_storage) )
6174 . map_err ( |_| WorkerError :: DbError ) ?
6275 . ok_or ( WorkerError :: MissingAsmState ( * blockid. blkid ( ) ) )
6376 }
@@ -68,13 +81,11 @@ impl WorkerContext for AsmWorkerContext {
6881 state : & AsmState ,
6982 ) -> WorkerResult < ( ) > {
7083 self . asm_manager
71- . put_state ( * blockid, state . clone ( ) )
84+ . put_state ( * blockid, to_storage ( state ) )
7285 . map_err ( |_| WorkerError :: DbError )
7386 }
7487
7588 fn store_l1_manifest ( & self , _manifest : AsmManifest ) -> WorkerResult < ( ) > {
76- // Manifests are already stored with AsmState in AsmStateManager
77- // No separate storage needed for now
7889 Ok ( ( ) )
7990 }
8091
@@ -130,7 +141,6 @@ impl WorkerContext for AsmWorkerContext {
130141 }
131142
132143 fn has_l1_manifest ( & self , _blockid : & L1BlockId ) -> WorkerResult < bool > {
133- // Each block generates a valid manifest
134144 Ok ( true )
135145 }
136146}
0 commit comments