@@ -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,44 @@ 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
///
141
156
/// [`chain::Filter`]: ../trait.Filter.html
142
- fn add_monitor ( & self , outpoint : OutPoint , monitor : ChannelMonitor < ChanSigner > ) -> Result < ( ) , MonitorUpdateError > {
157
+ fn watch_channel ( & self , funding_outpoint : OutPoint , monitor : ChannelMonitor < ChanSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > {
143
158
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" ) ) ,
159
+ let entry = match monitors. entry ( funding_outpoint) {
160
+ hash_map:: Entry :: Occupied ( _) => {
161
+ log_error ! ( self . logger, "Failed to add new channel data: channel monitor for given outpoint is already present" ) ;
162
+ return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) } ,
146
163
hash_map:: Entry :: Vacant ( e) => e,
147
164
} ;
165
+ if let Err ( e) = self . persister . persist_new_channel ( funding_outpoint, & monitor) {
166
+ log_error ! ( self . logger, "Failed to persist new channel data" ) ;
167
+ return Err ( e) ;
168
+ }
148
169
{
149
170
let funding_txo = monitor. get_funding_txo ( ) ;
150
171
log_trace ! ( self . logger, "Got new Channel Monitor for channel {}" , log_bytes!( funding_txo. 0 . to_channel_id( ) [ ..] ) ) ;
@@ -162,38 +183,32 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
162
183
Ok ( ( ) )
163
184
}
164
185
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 > {
186
+ fn update_channel ( & self , funding_txo : OutPoint , update : ChannelMonitorUpdate ) -> Result < ( ) , ChannelMonitorUpdateErr > {
187
+ // Update the monitor that watches the channel referred to by the given outpoint.
167
188
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
168
- match monitors. get_mut ( & outpoint) {
189
+ match monitors. get_mut ( & funding_txo) {
190
+ None => {
191
+ log_error ! ( self . logger, "Failed to update channel monitor: no such monitor registered" ) ;
192
+ Err ( ChannelMonitorUpdateErr :: PermanentFailure )
193
+ } ,
169
194
Some ( orig_monitor) => {
170
195
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 ) ,
196
+ let update_res = orig_monitor. update_monitor ( & update, & self . broadcaster , & self . logger ) ;
197
+ if let Err ( e) = & update_res {
198
+ log_error ! ( self . logger, "Failed to update channel monitor: {:?}" , e) ;
199
+ }
200
+ // Even if updating the monitor returns an error, the monitor's state will
201
+ // still be changed. So, persist the updated monitor despite the error.
202
+ let persist_res = self . persister . update_persisted_channel ( funding_txo, & update, orig_monitor) ;
203
+ if let Err ( ref e) = persist_res {
204
+ log_error ! ( self . logger, "Failed to persist channel monitor update: {:?}" , e) ;
205
+ }
206
+ if update_res. is_err ( ) {
207
+ Err ( ChannelMonitorUpdateErr :: PermanentFailure )
208
+ } else {
209
+ persist_res
210
+ }
211
+ }
197
212
}
198
213
}
199
214
@@ -206,11 +221,12 @@ impl<ChanSigner: ChannelKeys, C: Deref + Sync + Send, T: Deref + Sync + Send, F:
206
221
}
207
222
}
208
223
209
- impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > events:: EventsProvider for ChainMonitor < ChanSigner , C , T , F , L >
224
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref , P : Deref > events:: EventsProvider for ChainMonitor < ChanSigner , C , T , F , L , P >
210
225
where C :: Target : chain:: Filter ,
211
226
T :: Target : BroadcasterInterface ,
212
227
F :: Target : FeeEstimator ,
213
228
L :: Target : Logger ,
229
+ P :: Target : channelmonitor:: Persist < ChanSigner > ,
214
230
{
215
231
fn get_and_clear_pending_events ( & self ) -> Vec < Event > {
216
232
let mut pending_events = Vec :: new ( ) ;
0 commit comments