Skip to content

Commit ba4856e

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 0f9d9c5 commit ba4856e

File tree

5 files changed

+275
-12
lines changed

5 files changed

+275
-12
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: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,103 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
17751775
PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key)
17761776
}
17771777

1778+
/// Restores a single, given channel to normal operation after a
1779+
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1780+
/// operation.
1781+
///
1782+
/// All ChannelMonitor updates up to and including highest_applied_update_id must have been
1783+
/// fully committed in every copy of the given channels' ChannelMonitors.
1784+
///
1785+
/// Note that there is no effect to calling with a highest_applied_update_id other than the
1786+
/// current latest ChannelMonitorUpdate and one call to this function after multiple
1787+
/// ChannelMonitorUpdateErr::TemporaryFailures is fine. The highest_applied_update_id field
1788+
/// exists largely only to prevent races between this and concurrent update_monitor calls.
1789+
pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
1790+
let _ = self.total_consistency_lock.read().unwrap();
1791+
1792+
let mut close_results = Vec::new();
1793+
let mut htlc_forwards = Vec::new();
1794+
let mut htlc_failures = Vec::new();
1795+
let mut pending_events = Vec::new();
1796+
1797+
{
1798+
let mut channel_lock = self.channel_state.lock().unwrap();
1799+
let channel_state = &mut *channel_lock;
1800+
let short_to_id = &mut channel_state.short_to_id;
1801+
let pending_msg_events = &mut channel_state.pending_msg_events;
1802+
let channel = match channel_state.by_id.get_mut(&funding_txo.to_channel_id()) {
1803+
Some(chan) => chan,
1804+
None => return,
1805+
};
1806+
if !channel.is_awaiting_monitor_update() || channel.get_latest_monitor_update_id() != highest_applied_update_id {
1807+
return;
1808+
}
1809+
1810+
let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.monitor_updating_restored();
1811+
if !pending_forwards.is_empty() {
1812+
htlc_forwards.push((channel.get_short_channel_id().expect("We can't have pending forwards before funding confirmation"), pending_forwards));
1813+
}
1814+
htlc_failures.append(&mut pending_failures);
1815+
1816+
macro_rules! handle_cs { () => {
1817+
if let Some(update) = commitment_update {
1818+
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
1819+
node_id: channel.get_their_node_id(),
1820+
updates: update,
1821+
});
1822+
}
1823+
} }
1824+
macro_rules! handle_raa { () => {
1825+
if let Some(revoke_and_ack) = raa {
1826+
pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
1827+
node_id: channel.get_their_node_id(),
1828+
msg: revoke_and_ack,
1829+
});
1830+
}
1831+
} }
1832+
match order {
1833+
RAACommitmentOrder::CommitmentFirst => {
1834+
handle_cs!();
1835+
handle_raa!();
1836+
},
1837+
RAACommitmentOrder::RevokeAndACKFirst => {
1838+
handle_raa!();
1839+
handle_cs!();
1840+
},
1841+
}
1842+
if needs_broadcast_safe {
1843+
pending_events.push(events::Event::FundingBroadcastSafe {
1844+
funding_txo: channel.get_funding_txo().unwrap(),
1845+
user_channel_id: channel.get_user_id(),
1846+
});
1847+
}
1848+
if let Some(msg) = funding_locked {
1849+
pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
1850+
node_id: channel.get_their_node_id(),
1851+
msg,
1852+
});
1853+
if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
1854+
pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
1855+
node_id: channel.get_their_node_id(),
1856+
msg: announcement_sigs,
1857+
});
1858+
}
1859+
short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
1860+
}
1861+
}
1862+
1863+
self.pending_events.lock().unwrap().append(&mut pending_events);
1864+
1865+
for failure in htlc_failures.drain(..) {
1866+
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
1867+
}
1868+
self.forward_htlcs(&mut htlc_forwards[..]);
1869+
1870+
for res in close_results.drain(..) {
1871+
self.finish_force_close_channel(res);
1872+
}
1873+
}
1874+
17781875
/// Used to restore channels to normal operation after a
17791876
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
17801877
/// operation.
@@ -3323,7 +3420,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> R
33233420
if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
33243421
if channel.get_cur_local_commitment_transaction_number() != monitor.get_cur_local_commitment_number() ||
33253422
channel.get_revoked_remote_commitment_transaction_number() != monitor.get_min_seen_secret() ||
3326-
channel.get_cur_remote_commitment_transaction_number() != monitor.get_cur_remote_commitment_number() {
3423+
channel.get_cur_remote_commitment_transaction_number() != monitor.get_cur_remote_commitment_number() ||
3424+
channel.get_latest_monitor_update_id() != monitor.get_latest_update_id() {
33273425
let mut force_close_res = channel.force_shutdown();
33283426
force_close_res.0 = monitor.get_latest_local_commitment_txn();
33293427
closed_channels.push(force_close_res);

0 commit comments

Comments
 (0)