Skip to content

Commit 4985587

Browse files
committed
Add types for updating ChannelMonitors without copying them.
This is the first step in migrating ChannelMonitor updating logic to use incremental Update objects instead of copying the ChannelMonitors themselves and insert_combine()ing them. This adds most of the scaffolding and updates relevant comments to refer to the new architecture, without changing how any actual updates occur.
1 parent d271d74 commit 4985587

File tree

5 files changed

+284
-13
lines changed

5 files changed

+284
-13
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ impl channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMon
121121
ret
122122
}
123123

124+
fn update_monitor(&self, funding_txo: OutPoint, update: channelmonitor::ChannelMonitorUpdate) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
125+
unimplemented!(); //TODO
126+
}
127+
124128
fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate> {
125129
return self.simple_monitor.get_and_clear_pending_htlcs_updated();
126130
}

lightning/src/ln/channel.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
240240
secp_ctx: Secp256k1<secp256k1::All>,
241241
channel_value_satoshis: u64,
242242

243+
latest_monitor_update_id: u64,
244+
243245
#[cfg(not(test))]
244246
local_keys: ChanSigner,
245247
#[cfg(test)]
@@ -470,6 +472,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
470472
secp_ctx: secp_ctx,
471473
channel_value_satoshis: channel_value_satoshis,
472474

475+
latest_monitor_update_id: 0,
476+
473477
local_keys: chan_keys,
474478
shutdown_pubkey: keys_provider.get_shutdown_pubkey(),
475479
cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
@@ -691,6 +695,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
691695
channel_outbound: false,
692696
secp_ctx: secp_ctx,
693697

698+
latest_monitor_update_id: 0,
699+
694700
local_keys: chan_keys,
695701
shutdown_pubkey: keys_provider.get_shutdown_pubkey(),
696702
cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
@@ -2908,6 +2914,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
29082914
self.channel_update_count
29092915
}
29102916

2917+
pub fn get_latest_monitor_update_id(&self) -> u64 {
2918+
self.latest_monitor_update_id
2919+
}
2920+
29112921
pub fn should_announce(&self) -> bool {
29122922
self.config.announced_channel
29132923
}
@@ -3673,6 +3683,8 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
36733683
self.channel_outbound.write(writer)?;
36743684
self.channel_value_satoshis.write(writer)?;
36753685

3686+
self.latest_monitor_update_id.write(writer)?;
3687+
36763688
self.local_keys.write(writer)?;
36773689
self.shutdown_pubkey.write(writer)?;
36783690

@@ -3870,6 +3882,8 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
38703882
let channel_outbound = Readable::read(reader)?;
38713883
let channel_value_satoshis = Readable::read(reader)?;
38723884

3885+
let latest_monitor_update_id = Readable::read(reader)?;
3886+
38733887
let local_keys = Readable::read(reader)?;
38743888
let shutdown_pubkey = Readable::read(reader)?;
38753889

@@ -4018,6 +4032,8 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
40184032
secp_ctx: Secp256k1::new(),
40194033
channel_value_satoshis,
40204034

4035+
latest_monitor_update_id,
4036+
40214037
local_keys,
40224038
shutdown_pubkey,
40234039

lightning/src/ln/channelmanager.rs

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,111 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref> ChannelManager<ChanSigner, M,
17811781
PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key)
17821782
}
17831783

1784+
/// Restores a single, given channel to normal operation after a
1785+
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1786+
/// operation.
1787+
///
1788+
/// All ChannelMonitor updates up to and including highest_applied_update_id must have been
1789+
/// fully committed in every copy of the given channels' ChannelMonitors.
1790+
///
1791+
/// Note that there is no effect to calling with a highest_applied_update_id other than the
1792+
/// current latest ChannelMonitorUpdate and one call to this function after multiple
1793+
/// ChannelMonitorUpdateErr::TemporaryFailures is fine. The highest_applied_update_id field
1794+
/// exists largely only to prevent races between this and concurrent update_monitor calls.
1795+
///
1796+
/// Thus, the anticipated use is, at a high level:
1797+
/// 1) You register a ManyChannelMonitor with this ChannelManager.
1798+
/// 2) it stores each update to disk, and begins updating any remote (eg watchtower) copies of
1799+
/// said ChannelMonitors as it can, returning ChannelMonitorUpdateErr::TemporaryFailures
1800+
/// any time it cannot do so instantly,
1801+
/// 3) once all remote copies are updated, you call this function with the update_id that
1802+
/// completed, and once it is the latest the Channel will be re-enabled.
1803+
pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
1804+
let _ = self.total_consistency_lock.read().unwrap();
1805+
1806+
let mut close_results = Vec::new();
1807+
let mut htlc_forwards = Vec::new();
1808+
let mut htlc_failures = Vec::new();
1809+
let mut pending_events = Vec::new();
1810+
1811+
{
1812+
let mut channel_lock = self.channel_state.lock().unwrap();
1813+
let channel_state = &mut *channel_lock;
1814+
let short_to_id = &mut channel_state.short_to_id;
1815+
let pending_msg_events = &mut channel_state.pending_msg_events;
1816+
let channel = match channel_state.by_id.get_mut(&funding_txo.to_channel_id()) {
1817+
Some(chan) => chan,
1818+
None => return,
1819+
};
1820+
if !channel.is_awaiting_monitor_update() || channel.get_latest_monitor_update_id() != highest_applied_update_id {
1821+
return;
1822+
}
1823+
1824+
let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.monitor_updating_restored();
1825+
if !pending_forwards.is_empty() {
1826+
htlc_forwards.push((channel.get_short_channel_id().expect("We can't have pending forwards before funding confirmation"), pending_forwards));
1827+
}
1828+
htlc_failures.append(&mut pending_failures);
1829+
1830+
macro_rules! handle_cs { () => {
1831+
if let Some(update) = commitment_update {
1832+
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
1833+
node_id: channel.get_their_node_id(),
1834+
updates: update,
1835+
});
1836+
}
1837+
} }
1838+
macro_rules! handle_raa { () => {
1839+
if let Some(revoke_and_ack) = raa {
1840+
pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
1841+
node_id: channel.get_their_node_id(),
1842+
msg: revoke_and_ack,
1843+
});
1844+
}
1845+
} }
1846+
match order {
1847+
RAACommitmentOrder::CommitmentFirst => {
1848+
handle_cs!();
1849+
handle_raa!();
1850+
},
1851+
RAACommitmentOrder::RevokeAndACKFirst => {
1852+
handle_raa!();
1853+
handle_cs!();
1854+
},
1855+
}
1856+
if needs_broadcast_safe {
1857+
pending_events.push(events::Event::FundingBroadcastSafe {
1858+
funding_txo: channel.get_funding_txo().unwrap(),
1859+
user_channel_id: channel.get_user_id(),
1860+
});
1861+
}
1862+
if let Some(msg) = funding_locked {
1863+
pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
1864+
node_id: channel.get_their_node_id(),
1865+
msg,
1866+
});
1867+
if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
1868+
pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
1869+
node_id: channel.get_their_node_id(),
1870+
msg: announcement_sigs,
1871+
});
1872+
}
1873+
short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
1874+
}
1875+
}
1876+
1877+
self.pending_events.lock().unwrap().append(&mut pending_events);
1878+
1879+
for failure in htlc_failures.drain(..) {
1880+
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
1881+
}
1882+
self.forward_htlcs(&mut htlc_forwards[..]);
1883+
1884+
for res in close_results.drain(..) {
1885+
self.finish_force_close_channel(res);
1886+
}
1887+
}
1888+
17841889
/// Used to restore channels to normal operation after a
17851890
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
17861891
/// operation.
@@ -3351,7 +3456,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref, T
33513456
if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
33523457
if channel.get_cur_local_commitment_transaction_number() != monitor.get_cur_local_commitment_number() ||
33533458
channel.get_revoked_remote_commitment_transaction_number() != monitor.get_min_seen_secret() ||
3354-
channel.get_cur_remote_commitment_transaction_number() != monitor.get_cur_remote_commitment_number() {
3459+
channel.get_cur_remote_commitment_transaction_number() != monitor.get_cur_remote_commitment_number() ||
3460+
channel.get_latest_monitor_update_id() != monitor.get_latest_update_id() {
33553461
let mut force_close_res = channel.force_shutdown();
33563462
force_close_res.0 = monitor.get_latest_local_commitment_txn();
33573463
closed_channels.push(force_close_res);

0 commit comments

Comments
 (0)