Skip to content

Commit 34218cc

Browse files
committed
Add storage for ChannelMonitorUpdates in Channels
In order to support fully async `ChannelMonitor` updating, we need to ensure that we can replay `ChannelMonitorUpdate`s if we shut down after persisting a `ChannelManager` but without completing a `ChannelMonitorUpdate` persistence. In order to support that we (obviously) have to store the `ChannelMonitorUpdate`s in the `ChannelManager`, which we do here inside the `Channel`. We do so now because in the coming commits we will start using the async persistence flow for all updates, and while we won't yet support fully async monitor updating it's nice to get some of the foundational structures in place now.
1 parent 56f979b commit 34218cc

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lightning/src/ln/channel.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,12 @@ pub(super) struct Channel<Signer: ChannelSigner> {
743743
/// The unique identifier used to re-derive the private key material for the channel through
744744
/// [`SignerProvider::derive_channel_signer`].
745745
channel_keys_id: [u8; 32],
746+
747+
/// When we generate [`ChannelMonitorUpdate`]s to persist, they may not be persisted immediately.
748+
/// If we then persist the [`channelmanager::ChannelManager`] and crash before the persistence
749+
/// completes we still need to be able to complete the persistence. Thus, we have to keep a
750+
/// copy of the [`ChannelMonitorUpdate`] here until it is complete.
751+
pending_monitor_updates: Vec<ChannelMonitorUpdate>,
746752
}
747753

748754
#[cfg(any(test, fuzzing))]
@@ -1112,6 +1118,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
11121118

11131119
channel_type,
11141120
channel_keys_id,
1121+
1122+
pending_monitor_updates: Vec::new(),
11151123
})
11161124
}
11171125

@@ -1458,6 +1466,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
14581466

14591467
channel_type,
14601468
channel_keys_id,
1469+
1470+
pending_monitor_updates: Vec::new(),
14611471
};
14621472

14631473
Ok(chan)
@@ -4891,6 +4901,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
48914901
(self.channel_state & ChannelState::MonitorUpdateInProgress as u32) != 0
48924902
}
48934903

4904+
pub fn get_next_monitor_update(&self) -> Option<&ChannelMonitorUpdate> {
4905+
self.pending_monitor_updates.first()
4906+
}
4907+
48944908
/// Returns true if funding_created was sent/received.
48954909
pub fn is_funding_initiated(&self) -> bool {
48964910
self.channel_state >= ChannelState::FundingSent as u32
@@ -6891,6 +6905,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68916905

68926906
channel_type: channel_type.unwrap(),
68936907
channel_keys_id,
6908+
6909+
pending_monitor_updates: Vec::new(),
68946910
})
68956911
}
68966912
}

0 commit comments

Comments
 (0)