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 ;
16+ use strata_state:: asm_state:: AsmState as StorageAsmState ;
1217use strata_storage:: { AsmStateManager , MmrIndexHandle } ;
1318use tokio:: runtime:: Handle ;
1419
15- /// ASM [`WorkerContext`] implementation
20+ /// Convert storage-layer AsmState to worker AsmState.
21+ fn from_storage ( s : StorageAsmState ) -> AsmState {
22+ AsmState :: new ( s. state ( ) . clone ( ) , s. logs ( ) . clone ( ) )
23+ }
24+
25+ /// Convert worker AsmState to storage-layer AsmState.
26+ fn to_storage ( s : & AsmState ) -> StorageAsmState {
27+ StorageAsmState :: new ( s. state ( ) . clone ( ) , s. logs ( ) . clone ( ) )
28+ }
29+
30+ /// ASM [`WorkerContext`] implementation.
1631///
17- /// This implementation fetches L1 blocks directly from a Bitcoin node
18- /// and uses SledDB for state storage.
32+ /// Fetches L1 blocks from a Bitcoin node and persists state via SledDB.
1933pub ( crate ) struct AsmWorkerContext {
2034 runtime_handle : Handle ,
2135 bitcoin_client : Arc < Client > ,
@@ -24,7 +38,6 @@ pub(crate) struct AsmWorkerContext {
2438}
2539
2640impl AsmWorkerContext {
27- /// Create a new BridgeWorkerContext
2841 pub ( crate ) const fn new (
2942 runtime_handle : Handle ,
3043 bitcoin_client : Arc < Client > ,
@@ -42,7 +55,6 @@ impl AsmWorkerContext {
4255
4356impl WorkerContext for AsmWorkerContext {
4457 fn get_l1_block ( & self , blockid : & L1BlockId ) -> WorkerResult < Block > {
45- // Fetch block directly from Bitcoin node by hash
4658 let block_hash: BlockHash = blockid. to_block_hash ( ) ;
4759 self . runtime_handle
4860 . block_on ( self . bitcoin_client . get_block ( & block_hash) )
@@ -52,12 +64,14 @@ impl WorkerContext for AsmWorkerContext {
5264 fn get_latest_asm_state ( & self ) -> WorkerResult < Option < ( L1BlockCommitment , AsmState ) > > {
5365 self . asm_manager
5466 . fetch_most_recent_state ( )
67+ . map ( |opt| opt. map ( |( id, s) | ( id, from_storage ( s) ) ) )
5568 . map_err ( |_| WorkerError :: DbError )
5669 }
5770
5871 fn get_anchor_state ( & self , blockid : & L1BlockCommitment ) -> WorkerResult < AsmState > {
5972 self . asm_manager
6073 . get_state ( * blockid)
74+ . map ( |opt| opt. map ( from_storage) )
6175 . map_err ( |_| WorkerError :: DbError ) ?
6276 . ok_or ( WorkerError :: MissingAsmState ( * blockid. blkid ( ) ) )
6377 }
@@ -68,13 +82,11 @@ impl WorkerContext for AsmWorkerContext {
6882 state : & AsmState ,
6983 ) -> WorkerResult < ( ) > {
7084 self . asm_manager
71- . put_state ( * blockid, state . clone ( ) )
85+ . put_state ( * blockid, to_storage ( state ) )
7286 . map_err ( |_| WorkerError :: DbError )
7387 }
7488
7589 fn store_l1_manifest ( & self , _manifest : AsmManifest ) -> WorkerResult < ( ) > {
76- // Manifests are already stored with AsmState in AsmStateManager
77- // No separate storage needed for now
7890 Ok ( ( ) )
7991 }
8092
@@ -130,7 +142,6 @@ impl WorkerContext for AsmWorkerContext {
130142 }
131143
132144 fn has_l1_manifest ( & self , _blockid : & L1BlockId ) -> WorkerResult < bool > {
133- // Each block generates a valid manifest
134145 Ok ( true )
135146 }
136147}
0 commit comments