@@ -34,7 +34,8 @@ use bitcoin::blockdata::block::BlockHeader;
34
34
use chain;
35
35
use chain:: Filter ;
36
36
use chain:: chaininterface:: { BroadcasterInterface , FeeEstimator } ;
37
- use chain:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdate , ChannelMonitorUpdateErr , MonitorEvent , MonitorUpdateError } ;
37
+ use chain:: channelmonitor;
38
+ use chain:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdate , ChannelMonitorUpdateErr , MonitorEvent , Persist } ;
38
39
use chain:: transaction:: { OutPoint , TransactionData } ;
39
40
use chain:: keysinterface:: ChannelKeys ;
40
41
use util:: logger:: Logger ;
@@ -55,25 +56,28 @@ use std::ops::Deref;
55
56
/// [`chain::Watch`]: ../trait.Watch.html
56
57
/// [`ChannelManager`]: ../../ln/channelmanager/struct.ChannelManager.html
57
58
/// [module-level documentation]: index.html
58
- pub struct ChainMonitor < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref >
59
+ pub struct ChainMonitor < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref , P : Deref >
59
60
where C :: Target : chain:: Filter ,
60
61
T :: Target : BroadcasterInterface ,
61
62
F :: Target : FeeEstimator ,
62
63
L :: Target : Logger ,
64
+ P :: Target : channelmonitor:: Persist < ChanSigner > ,
63
65
{
64
66
/// The monitors
65
67
pub monitors : Mutex < HashMap < OutPoint , ChannelMonitor < ChanSigner > > > ,
66
68
chain_source : Option < C > ,
67
69
broadcaster : T ,
68
70
logger : L ,
69
- fee_estimator : F
71
+ fee_estimator : F ,
72
+ persister : P ,
70
73
}
71
74
72
- impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > ChainMonitor < ChanSigner , C , T , F , L >
73
- where C :: Target : chain:: Filter ,
74
- T :: Target : BroadcasterInterface ,
75
- F :: Target : FeeEstimator ,
76
- L :: Target : Logger ,
75
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref , P : Deref > ChainMonitor < ChanSigner , C , T , F , L , P >
76
+ where C :: Target : chain:: Filter ,
77
+ T :: Target : BroadcasterInterface ,
78
+ F :: Target : FeeEstimator ,
79
+ L :: Target : Logger ,
80
+ P :: Target : channelmonitor:: Persist < ChanSigner > ,
77
81
{
78
82
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
79
83
/// of a channel and reacting accordingly based on transactions in the connected block. See
@@ -124,27 +128,47 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
124
128
/// transactions relevant to the watched channels.
125
129
///
126
130
/// [`chain::Filter`]: ../trait.Filter.html
127
- pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F ) -> Self {
131
+ pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F , persister : P ) -> Self {
128
132
Self {
129
133
monitors : Mutex :: new ( HashMap :: new ( ) ) ,
130
134
chain_source,
131
135
broadcaster,
132
136
logger,
133
137
fee_estimator : feeest,
138
+ persister,
134
139
}
135
140
}
141
+ }
142
+
143
+ impl < ChanSigner : ChannelKeys , C : Deref + Sync + Send , T : Deref + Sync + Send , F : Deref + Sync + Send , L : Deref + Sync + Send , P : Deref + Sync + Send > chain:: Watch for ChainMonitor < ChanSigner , C , T , F , L , P >
144
+ where C :: Target : chain:: Filter ,
145
+ T :: Target : BroadcasterInterface ,
146
+ F :: Target : FeeEstimator ,
147
+ L :: Target : Logger ,
148
+ P :: Target : channelmonitor:: Persist < ChanSigner > ,
149
+ {
150
+ type Keys = ChanSigner ;
136
151
137
152
/// Adds the monitor that watches the channel referred to by the given outpoint.
138
153
///
139
154
/// Calls back to [`chain::Filter`] with the funding transaction and outputs to watch.
140
155
///
156
+ /// Note that we persist the given `ChannelMonitor` while holding the `ChainMonitor`
157
+ /// monitors lock.
158
+ ///
141
159
/// [`chain::Filter`]: ../trait.Filter.html
142
- fn add_monitor ( & self , outpoint : OutPoint , monitor : ChannelMonitor < ChanSigner > ) -> Result < ( ) , MonitorUpdateError > {
160
+ fn watch_channel ( & self , funding_outpoint : OutPoint , monitor : ChannelMonitor < ChanSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > {
143
161
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
144
- let entry = match monitors. entry ( outpoint) {
145
- hash_map:: Entry :: Occupied ( _) => return Err ( MonitorUpdateError ( "Channel monitor for given outpoint is already present" ) ) ,
162
+ let entry = match monitors. entry ( funding_outpoint) {
163
+ hash_map:: Entry :: Occupied ( _) => {
164
+ log_error ! ( self . logger, "Failed to add new channel data: channel monitor for given outpoint is already present" ) ;
165
+ return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) } ,
146
166
hash_map:: Entry :: Vacant ( e) => e,
147
167
} ;
168
+ if let Err ( e) = self . persister . persist_new_channel ( funding_outpoint, & monitor) {
169
+ log_error ! ( self . logger, "Failed to persist new channel data" ) ;
170
+ return Err ( e) ;
171
+ }
148
172
{
149
173
let funding_txo = monitor. get_funding_txo ( ) ;
150
174
log_trace ! ( self . logger, "Got new Channel Monitor for channel {}" , log_bytes!( funding_txo. 0 . to_channel_id( ) [ ..] ) ) ;
@@ -162,38 +186,34 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
162
186
Ok ( ( ) )
163
187
}
164
188
165
- /// Updates the monitor that watches the channel referred to by the given outpoint.
166
- fn update_monitor ( & self , outpoint : OutPoint , update : ChannelMonitorUpdate ) -> Result < ( ) , MonitorUpdateError > {
189
+ /// Note that we persist the given `ChannelMonitor` update while holding the
190
+ /// `ChainMonitor` monitors lock.
191
+ fn update_channel ( & self , funding_txo : OutPoint , update : ChannelMonitorUpdate ) -> Result < ( ) , ChannelMonitorUpdateErr > {
192
+ // Update the monitor that watches the channel referred to by the given outpoint.
167
193
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
168
- match monitors. get_mut ( & outpoint) {
194
+ match monitors. get_mut ( & funding_txo) {
195
+ None => {
196
+ log_error ! ( self . logger, "Failed to update channel monitor: no such monitor registered" ) ;
197
+ Err ( ChannelMonitorUpdateErr :: PermanentFailure )
198
+ } ,
169
199
Some ( orig_monitor) => {
170
200
log_trace ! ( self . logger, "Updating Channel Monitor for channel {}" , log_funding_info!( orig_monitor) ) ;
171
- orig_monitor. update_monitor ( update, & self . broadcaster , & self . logger )
172
- } ,
173
- None => Err ( MonitorUpdateError ( "No such monitor registered" ) )
174
- }
175
- }
176
- }
177
-
178
- 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 >
179
- where C :: Target : chain:: Filter ,
180
- T :: Target : BroadcasterInterface ,
181
- F :: Target : FeeEstimator ,
182
- L :: Target : Logger ,
183
- {
184
- type Keys = ChanSigner ;
185
-
186
- fn watch_channel ( & self , funding_txo : OutPoint , monitor : ChannelMonitor < ChanSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > {
187
- match self . add_monitor ( funding_txo, monitor) {
188
- Ok ( _) => Ok ( ( ) ) ,
189
- Err ( _) => Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ,
190
- }
191
- }
192
-
193
- fn update_channel ( & self , funding_txo : OutPoint , update : ChannelMonitorUpdate ) -> Result < ( ) , ChannelMonitorUpdateErr > {
194
- match self . update_monitor ( funding_txo, update) {
195
- Ok ( _) => Ok ( ( ) ) ,
196
- Err ( _) => Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ,
201
+ let update_res = orig_monitor. update_monitor ( & update, & self . broadcaster , & self . logger ) ;
202
+ if let Err ( e) = & update_res {
203
+ log_error ! ( self . logger, "Failed to update channel monitor: {:?}" , e) ;
204
+ }
205
+ // Even if updating the monitor returns an error, the monitor's state will
206
+ // still be changed. So, persist the updated monitor despite the error.
207
+ let persist_res = self . persister . update_persisted_channel ( funding_txo, & update, orig_monitor) ;
208
+ if let Err ( ref e) = persist_res {
209
+ log_error ! ( self . logger, "Failed to persist channel monitor update: {:?}" , e) ;
210
+ }
211
+ if update_res. is_err ( ) {
212
+ Err ( ChannelMonitorUpdateErr :: PermanentFailure )
213
+ } else {
214
+ persist_res
215
+ }
216
+ }
197
217
}
198
218
}
199
219
@@ -206,11 +226,12 @@ impl<ChanSigner: ChannelKeys, C: Deref + Sync + Send, T: Deref + Sync + Send, F:
206
226
}
207
227
}
208
228
209
- impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > events:: EventsProvider for ChainMonitor < ChanSigner , C , T , F , L >
229
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref , P : Deref > events:: EventsProvider for ChainMonitor < ChanSigner , C , T , F , L , P >
210
230
where C :: Target : chain:: Filter ,
211
231
T :: Target : BroadcasterInterface ,
212
232
F :: Target : FeeEstimator ,
213
233
L :: Target : Logger ,
234
+ P :: Target : channelmonitor:: Persist < ChanSigner > ,
214
235
{
215
236
fn get_and_clear_pending_events ( & self ) -> Vec < Event > {
216
237
let mut pending_events = Vec :: new ( ) ;
0 commit comments