@@ -10,23 +10,13 @@ use tokio::sync::mpsc;
1010use tracing:: { debug, error, info, info_span, Instrument } ;
1111
1212use crate :: {
13- alias:: LexeChainMonitorType , traits:: LexePersister , BoxedAnyhowFuture ,
13+ alias:: LexeChainMonitorType ,
14+ traits:: { LexeInnerPersister , LexePersister } ,
1415} ;
1516
16- // `api_call_fut` is a future which makes an api call (typically with
17- // retries) to the backend to persist the channel monitor state, returning
18- // an `anyhow::Result<()>` once either (1) persistence succeeds or (2)
19- // there were too many failures to keep trying. We take this future as
20- // input (instead of e.g. a `VfsFile`) because it is the cleanest and
21- // easiest way to abstract over the user node and LSP's differing api
22- // clients, vfs structures, and expected error types.
23- //
24- // TODO(max): Add a required `upsert_monitor` method to the `LexePersister`
25- // trait to avoid this.
26- pub type MonitorChannelItem = ( LxChannelMonitorUpdate , BoxedAnyhowFuture ) ;
27-
2817/// Represents a channel monitor update. See docs on each field for details.
2918pub struct LxChannelMonitorUpdate {
19+ #[ allow( dead_code) ] // Conceptually part of the update.
3020 kind : ChannelMonitorUpdateKind ,
3121 funding_txo : LxOutPoint ,
3222 /// The ID of the channel monitor update, given by
@@ -85,27 +75,24 @@ impl Display for ChannelMonitorUpdateKind {
8575/// This prevent a race conditions where two monitor updates come in quick
8676/// succession and the newer channel monitor state is overwritten by the older
8777/// channel monitor state.
88- pub fn spawn_channel_monitor_persister_task < PS > (
78+ pub fn spawn_channel_monitor_persister_task < PS : LexePersister > (
79+ persister : PS ,
8980 chain_monitor : Arc < LexeChainMonitorType < PS > > ,
90- mut channel_monitor_persister_rx : mpsc:: Receiver < MonitorChannelItem > ,
81+ mut channel_monitor_persister_rx : mpsc:: Receiver < LxChannelMonitorUpdate > ,
9182 mut shutdown : NotifyOnce ,
92- ) -> LxTask < ( ) >
93- where
94- PS : LexePersister ,
95- {
83+ ) -> LxTask < ( ) > {
9684 debug ! ( "Starting channel monitor persister task" ) ;
9785 const SPAN_NAME : & str = "(chan-monitor-persister)" ;
9886 LxTask :: spawn_with_span ( SPAN_NAME , info_span ! ( SPAN_NAME ) , async move {
9987 loop {
10088 tokio:: select! {
101- Some ( ( update, api_call_fut) )
102- = channel_monitor_persister_rx. recv( ) => {
89+ Some ( update) = channel_monitor_persister_rx. recv( ) => {
10390 let update_span = update. span( ) ;
10491
10592 let handle_result = handle_update(
93+ & persister,
10694 chain_monitor. as_ref( ) ,
10795 update,
108- api_call_fut,
10996 )
11097 . instrument( update_span. clone( ) )
11198 . await ;
@@ -137,23 +124,18 @@ where
137124/// Since channel monitor persistence is very important, all [`Err`]s are
138125/// considered fatal; the caller should send a shutdown signal and exit.
139126async fn handle_update < PS : LexePersister > (
127+ persister : & PS ,
140128 chain_monitor : & LexeChainMonitorType < PS > ,
141129 update : LxChannelMonitorUpdate ,
142- api_call_fut : BoxedAnyhowFuture ,
143130) -> anyhow:: Result < ( ) > {
144- let LxChannelMonitorUpdate {
145- funding_txo,
146- update_id,
147- kind : _,
148- span : _,
149- } = update;
150-
151131 debug ! ( "Handling channel monitor update" ) ;
152132
153- // Run the persist future.
154- api_call_fut
133+ // Persist the monitor.
134+ let funding_txo = OutPoint :: from ( update. funding_txo ) ;
135+ persister
136+ . persist_monitor ( chain_monitor, & update. funding_txo )
155137 . await
156- . context ( "Channel monitor persist API call failed" ) ?;
138+ . context ( "persist_monitor failed" ) ?;
157139
158140 // Notify the chain monitor that the monitor update has been persisted.
159141 // - This should trigger a log like "Completed off-chain monitor update ..."
@@ -164,7 +146,7 @@ async fn handle_update<PS: LexePersister>(
164146 // channel be reenabled and the BGP woken to process events via the chain
165147 // monitor future.
166148 chain_monitor
167- . channel_monitor_updated ( OutPoint :: from ( funding_txo) , update_id)
149+ . channel_monitor_updated ( funding_txo, update . update_id )
168150 . map_err ( |e| anyhow ! ( "channel_monitor_updated returned Err: {e:?}" ) ) ?;
169151
170152 info ! ( "Success: persisted monitor" ) ;
0 commit comments