@@ -43,6 +43,7 @@ use ln::chan_utils;
43
43
use ln:: chan_utils:: { CounterpartyCommitmentSecrets , HTLCOutputInCommitment , LocalCommitmentTransaction , HTLCType } ;
44
44
use ln:: channelmanager:: { HTLCSource , PaymentPreimage , PaymentHash } ;
45
45
use ln:: onchaintx:: { OnchainTxHandler , InputDescriptors } ;
46
+ use ln:: data_persister:: ChannelDataPersister ;
46
47
use chain;
47
48
use chain:: Notify ;
48
49
use chain:: chaininterface:: { BroadcasterInterface , FeeEstimator } ;
@@ -99,7 +100,7 @@ impl Readable for ChannelMonitorUpdate {
99
100
}
100
101
101
102
/// An error enum representing a failure to persist a channel monitor update.
102
- #[ derive( Clone ) ]
103
+ #[ derive( Clone , Debug ) ]
103
104
pub enum ChannelMonitorUpdateErr {
104
105
/// Used to indicate a temporary failure (eg connection to a watchtower or remote backup of
105
106
/// our state failed, but is expected to succeed at some point in the future).
@@ -182,25 +183,28 @@ impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source });
182
183
/// independently to monitor channels remotely.
183
184
///
184
185
/// [`chain::Watch`]: ../../chain/trait.Watch.html
185
- pub struct ChainMonitor < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref >
186
+ pub struct ChainMonitor < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref , D : Deref >
186
187
where C :: Target : chain:: Notify ,
187
188
T :: Target : BroadcasterInterface ,
188
189
F :: Target : FeeEstimator ,
189
190
L :: Target : Logger ,
191
+ D :: Target : ChannelDataPersister < Keys =ChanSigner > ,
190
192
{
191
193
/// The monitors
192
194
pub monitors : Mutex < HashMap < OutPoint , ChannelMonitor < ChanSigner > > > ,
193
195
chain_source : Option < C > ,
194
196
broadcaster : T ,
195
197
logger : L ,
196
- fee_estimator : F
198
+ fee_estimator : F ,
199
+ data_persister : D ,
197
200
}
198
201
199
- impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > ChainMonitor < ChanSigner , C , T , F , L >
202
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref , D : Deref > ChainMonitor < ChanSigner , C , T , F , L , D >
200
203
where C :: Target : chain:: Notify ,
201
204
T :: Target : BroadcasterInterface ,
202
205
F :: Target : FeeEstimator ,
203
206
L :: Target : Logger ,
207
+ D :: Target : ChannelDataPersister < Keys =ChanSigner > ,
204
208
{
205
209
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
206
210
/// of a channel and reacting accordingly based on transactions in the connected block. See
@@ -246,22 +250,29 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
246
250
}
247
251
}
248
252
249
- impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > ChainMonitor < ChanSigner , C , T , F , L >
253
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref , D : Deref > ChainMonitor < ChanSigner , C , T , F , L , D >
250
254
where C :: Target : chain:: Notify ,
251
255
T :: Target : BroadcasterInterface ,
252
256
F :: Target : FeeEstimator ,
253
257
L :: Target : Logger ,
258
+ D :: Target : ChannelDataPersister < Keys =ChanSigner > ,
254
259
{
255
260
/// Creates a new object which can be used to monitor several channels given the chain
256
261
/// interface with which to register to receive notifications.
257
- pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F ) -> Self {
258
- Self {
259
- monitors : Mutex :: new ( HashMap :: new ( ) ) ,
262
+ pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F , data_persister : D ) -> Result < Self , ChannelMonitorUpdateErr > {
263
+ let monitors = match data_persister. load_channel_data ( ) {
264
+ Ok ( mons) => mons,
265
+ Err ( e) => return Err ( e)
266
+ } ;
267
+
268
+ Ok ( Self {
269
+ monitors : Mutex :: new ( monitors) ,
260
270
chain_source,
261
271
broadcaster,
262
272
logger,
263
273
fee_estimator : feeest,
264
- }
274
+ data_persister,
275
+ } )
265
276
}
266
277
267
278
/// Adds or updates the monitor which monitors the channel referred to by the given outpoint.
@@ -275,6 +286,12 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
275
286
hash_map:: Entry :: Occupied ( _) => return Err ( MonitorUpdateError ( "Channel monitor for given outpoint is already present" ) ) ,
276
287
hash_map:: Entry :: Vacant ( e) => e,
277
288
} ;
289
+ {
290
+ match self . data_persister . persist_channel_data ( outpoint, & monitor) {
291
+ Err ( _) => return Err ( MonitorUpdateError ( "Unable to persist channel data" ) ) ,
292
+ _ => { }
293
+ }
294
+ }
278
295
{
279
296
let funding_txo = monitor. get_funding_txo ( ) ;
280
297
log_trace ! ( self . logger, "Got new Channel Monitor for channel {}" , log_bytes!( funding_txo. 0 . to_channel_id( ) [ ..] ) ) ;
@@ -295,21 +312,29 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
295
312
/// Updates the monitor which monitors the channel referred to by the given outpoint.
296
313
pub fn update_monitor ( & self , outpoint : OutPoint , update : ChannelMonitorUpdate ) -> Result < ( ) , MonitorUpdateError > {
297
314
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
298
- match monitors. get_mut ( & outpoint) {
299
- Some ( orig_monitor) => {
300
- log_trace ! ( self . logger, "Updating Channel Monitor for channel {}" , log_funding_info!( orig_monitor) ) ;
301
- orig_monitor. update_monitor ( update, & self . broadcaster , & self . logger )
302
- } ,
303
- None => Err ( MonitorUpdateError ( "No such monitor registered" ) )
315
+ if let Some ( orig_monitor) = monitors. get_mut ( & outpoint) {
316
+ log_trace ! ( self . logger, "Updating Channel Monitor for channel {}" , log_funding_info!( orig_monitor) ) ;
317
+ if let Err ( e) = orig_monitor. update_monitor ( & update, & self . broadcaster , & self . logger ) {
318
+ return Err ( e)
319
+ }
320
+ match self . data_persister . update_channel_data ( outpoint, & update, orig_monitor) {
321
+ Err ( _) => return Err ( MonitorUpdateError ( "Failed to persist monitor update" ) ) ,
322
+ _ => { }
323
+ }
324
+ } else {
325
+ return Err ( MonitorUpdateError ( "No such monitor registered" ) ) ;
304
326
}
327
+
328
+ Ok ( ( ) )
305
329
}
306
330
}
307
331
308
- impl < ChanSigner : ChannelKeys , C : Deref + Sync + Send , T : Deref + Sync + Send , F : Deref + Sync + Send , L : Deref + Sync + Send > chain:: Watch for ChainMonitor < ChanSigner , C , T , F , L >
332
+ impl < ChanSigner : ChannelKeys , C : Deref + Sync + Send , T : Deref + Sync + Send , F : Deref + Sync + Send , L : Deref + Sync + Send , D : Deref + Sync + Send > chain:: Watch for ChainMonitor < ChanSigner , C , T , F , L , D >
309
333
where C :: Target : chain:: Notify ,
310
334
T :: Target : BroadcasterInterface ,
311
335
F :: Target : FeeEstimator ,
312
336
L :: Target : Logger ,
337
+ D :: Target : ChannelDataPersister < Keys =ChanSigner > ,
313
338
{
314
339
type Keys = ChanSigner ;
315
340
@@ -336,11 +361,12 @@ impl<ChanSigner: ChannelKeys, C: Deref + Sync + Send, T: Deref + Sync + Send, F:
336
361
}
337
362
}
338
363
339
- impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > events:: EventsProvider for ChainMonitor < ChanSigner , C , T , F , L >
364
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref , D : Deref > events:: EventsProvider for ChainMonitor < ChanSigner , C , T , F , L , D >
340
365
where C :: Target : chain:: Notify ,
341
366
T :: Target : BroadcasterInterface ,
342
367
F :: Target : FeeEstimator ,
343
368
L :: Target : Logger ,
369
+ D :: Target : ChannelDataPersister < Keys =ChanSigner > ,
344
370
{
345
371
fn get_and_clear_pending_events ( & self ) -> Vec < Event > {
346
372
let mut pending_events = Vec :: new ( ) ;
@@ -1339,30 +1365,29 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1339
1365
/// itself.
1340
1366
///
1341
1367
/// panics if the given update is not the next update by update_id.
1342
- pub fn update_monitor < B : Deref , L : Deref > ( & mut self , mut updates : ChannelMonitorUpdate , broadcaster : & B , logger : & L ) -> Result < ( ) , MonitorUpdateError >
1368
+ pub fn update_monitor < B : Deref , L : Deref > ( & mut self , updates : & ChannelMonitorUpdate , broadcaster : & B , logger : & L ) -> Result < ( ) , MonitorUpdateError >
1343
1369
where B :: Target : BroadcasterInterface ,
1344
1370
L :: Target : Logger ,
1345
1371
{
1346
1372
if self . latest_update_id + 1 != updates. update_id {
1347
1373
panic ! ( "Attempted to apply ChannelMonitorUpdates out of order, check the update_id before passing an update to update_monitor!" ) ;
1348
1374
}
1349
- for update in updates. updates . drain ( .. ) {
1375
+ for update in updates. updates . iter ( ) {
1350
1376
match update {
1351
1377
ChannelMonitorUpdateStep :: LatestLocalCommitmentTXInfo { commitment_tx, htlc_outputs } => {
1352
1378
if self . lockdown_from_offchain { panic ! ( ) ; }
1353
- self . provide_latest_local_commitment_tx_info ( commitment_tx, htlc_outputs) ?
1379
+ self . provide_latest_local_commitment_tx_info ( commitment_tx. clone ( ) , htlc_outputs. clone ( ) ) ?
1354
1380
} ,
1355
1381
ChannelMonitorUpdateStep :: LatestRemoteCommitmentTXInfo { unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point } =>
1356
- self . provide_latest_remote_commitment_tx_info ( & unsigned_commitment_tx, htlc_outputs, commitment_number, their_revocation_point, logger) ,
1382
+ self . provide_latest_remote_commitment_tx_info ( & unsigned_commitment_tx, htlc_outputs. clone ( ) , * commitment_number, * their_revocation_point, logger) ,
1357
1383
ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage } =>
1358
1384
self . provide_payment_preimage ( & PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 [ ..] ) . into_inner ( ) ) , & payment_preimage) ,
1359
1385
ChannelMonitorUpdateStep :: CommitmentSecret { idx, secret } =>
1360
- self . provide_secret ( idx, secret) ?,
1386
+ self . provide_secret ( * idx, * secret) ?,
1361
1387
ChannelMonitorUpdateStep :: ChannelForceClosed { should_broadcast } => {
1362
1388
self . lockdown_from_offchain = true ;
1363
- if should_broadcast {
1364
- self . broadcast_latest_local_commitment_txn ( broadcaster, logger) ;
1365
- } else {
1389
+ if * should_broadcast {
1390
+ self . broadcast_latest_local_commitment_txn ( broadcaster, logger) ; } else {
1366
1391
log_error ! ( logger, "You have a toxic local commitment transaction avaible in channel monitor, read comment in ChannelMonitor::get_latest_local_commitment_txn to be informed of manual action to take" ) ;
1367
1392
}
1368
1393
}
0 commit comments