@@ -142,9 +142,20 @@ impl Writer for VecWriter {
142
142
}
143
143
}
144
144
145
+ /// The LDK API requires that any time we tell it we're done persisting a `ChannelMonitor[Update]`
146
+ /// we never pass it in as the "latest" `ChannelMonitor` on startup. However, we can pass
147
+ /// out-of-date monitors as long as we never told LDK we finished persisting them, which we do by
148
+ /// storing both old `ChannelMonitor`s and ones that are "being persisted" here.
149
+ ///
150
+ /// Note that such "being persisted" `ChannelMonitor`s are stored in `ChannelManager` and will
151
+ /// simply be replayed on startup.
145
152
struct LatestMonitorState {
153
+ /// The latest monitor id which we told LDK we've persisted
146
154
persisted_monitor_id : u64 ,
155
+ /// The latest serialized `ChannelMonitor` that we told LDK we persisted.
147
156
persisted_monitor : Vec < u8 > ,
157
+ /// A set of (monitor id, serialized `ChannelMonitor`)s which we're currently "persisting",
158
+ /// from LDK's perspective.
148
159
pending_monitor_updates : Vec < ( u64 , Vec < u8 > ) > ,
149
160
}
150
161
@@ -153,14 +164,6 @@ struct TestChainMonitor {
153
164
pub keys : Arc < KeyProvider > ,
154
165
pub persister : Arc < TestPersister > ,
155
166
pub chain_monitor : Arc < chainmonitor:: ChainMonitor < TestChannelSigner , Arc < dyn chain:: Filter > , Arc < TestBroadcaster > , Arc < FuzzEstimator > , Arc < dyn Logger > , Arc < TestPersister > > > ,
156
- // If we reload a node with an old copy of ChannelMonitors, the ChannelManager deserialization
157
- // logic will automatically force-close our channels for us (as we don't have an up-to-date
158
- // monitor implying we are not able to punish misbehaving counterparties). Because this test
159
- // "fails" if we ever force-close a channel, we avoid doing so, always saving the latest
160
- // fully-serialized monitor state here, as well as the corresponding update_id.
161
- //
162
- // Note that this doesn't apply to monitors which are pending persistence, so we store the
163
- // latest pending monitor separately.
164
167
pub latest_monitors : Mutex < HashMap < OutPoint , LatestMonitorState > > ,
165
168
}
166
169
impl TestChainMonitor {
@@ -549,6 +552,9 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
549
552
monitors. insert( outpoint, <( BlockHash , ChannelMonitor <TestChannelSigner >) >:: read(
550
553
& mut Cursor :: new( & prev_state. persisted_monitor) , ( & * $keys_manager, & * $keys_manager)
551
554
) . expect( "Failed to read monitor" ) . 1 ) ;
555
+ // Wipe any `ChannelMonitor`s which we never told LDK we finished persisting,
556
+ // considering them discarded. LDK should replay these for us as they're stored in
557
+ // the `ChannelManager`.
552
558
prev_state. pending_monitor_updates. clear( ) ;
553
559
chain_monitor. latest_monitors. lock( ) . unwrap( ) . insert( outpoint, prev_state) ;
554
560
}
0 commit comments