Skip to content

Commit d20b1ae

Browse files
committed
Add types for updating ChannelMonitors without copying them.
1 parent ccbf997 commit d20b1ae

File tree

5 files changed

+272
-12
lines changed

5 files changed

+272
-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
@@ -243,6 +243,8 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
243243
secp_ctx: Secp256k1<secp256k1::All>,
244244
channel_value_satoshis: u64,
245245

246+
latest_monitor_update_id: u64,
247+
246248
#[cfg(not(test))]
247249
local_keys: ChanSigner,
248250
#[cfg(test)]
@@ -473,6 +475,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
473475
secp_ctx: secp_ctx,
474476
channel_value_satoshis: channel_value_satoshis,
475477

478+
latest_monitor_update_id: 0,
479+
476480
local_keys: chan_keys,
477481
shutdown_pubkey: keys_provider.get_shutdown_pubkey(),
478482
cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
@@ -694,6 +698,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
694698
channel_outbound: false,
695699
secp_ctx: secp_ctx,
696700

701+
latest_monitor_update_id: 0,
702+
697703
local_keys: chan_keys,
698704
shutdown_pubkey: keys_provider.get_shutdown_pubkey(),
699705
cur_local_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
@@ -2912,6 +2918,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
29122918
self.channel_update_count
29132919
}
29142920

2921+
pub fn get_latest_monitor_update_id(&self) -> u64 {
2922+
self.latest_monitor_update_id
2923+
}
2924+
29152925
pub fn should_announce(&self) -> bool {
29162926
self.config.announced_channel
29172927
}
@@ -3689,6 +3699,8 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
36893699
self.channel_outbound.write(writer)?;
36903700
self.channel_value_satoshis.write(writer)?;
36913701

3702+
self.latest_monitor_update_id.write(writer)?;
3703+
36923704
self.local_keys.write(writer)?;
36933705
self.shutdown_pubkey.write(writer)?;
36943706

@@ -3883,6 +3895,8 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
38833895
let channel_outbound = Readable::read(reader)?;
38843896
let channel_value_satoshis = Readable::read(reader)?;
38853897

3898+
let latest_monitor_update_id = Readable::read(reader)?;
3899+
38863900
let local_keys = Readable::read(reader)?;
38873901
let shutdown_pubkey = Readable::read(reader)?;
38883902

@@ -4031,6 +4045,8 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
40314045
secp_ctx: Secp256k1::new(),
40324046
channel_value_satoshis,
40334047

4048+
latest_monitor_update_id,
4049+
40344050
local_keys,
40354051
shutdown_pubkey,
40364052

lightning/src/ln/channelmanager.rs

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,103 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
19861986
PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key)
19871987
}
19881988

1989+
/// Restores a single, given channel to normal operation after a
1990+
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1991+
/// operation.
1992+
///
1993+
/// All ChannelMonitor updates up to and including highest_applied_update_id must have been
1994+
/// fully committed in every copy of the given channels' ChannelMonitors.
1995+
///
1996+
/// Note that there is no effect to calling with a highest_applied_update_id other than the
1997+
/// current latest ChannelMonitorUpdate and one call to this function after multiple
1998+
/// ChannelMonitorUpdateErr::TemporaryFailures is fine. The highest_applied_update_id field
1999+
/// exists largely only to prevent races between this and concurrent update_monitor calls.
2000+
pub fn channel_monitor_updated(&self, funding_txo: &OutPoint, highest_applied_update_id: u64) {
2001+
let _ = self.total_consistency_lock.read().unwrap();
2002+
2003+
let mut close_results = Vec::new();
2004+
let mut htlc_forwards = Vec::new();
2005+
let mut htlc_failures = Vec::new();
2006+
let mut pending_events = Vec::new();
2007+
2008+
{
2009+
let mut channel_lock = self.channel_state.lock().unwrap();
2010+
let channel_state = &mut *channel_lock;
2011+
let short_to_id = &mut channel_state.short_to_id;
2012+
let pending_msg_events = &mut channel_state.pending_msg_events;
2013+
let channel = match channel_state.by_id.get_mut(&funding_txo.to_channel_id()) {
2014+
Some(chan) => chan,
2015+
None => return,
2016+
};
2017+
if channel.get_latest_monitor_update_id() != highest_applied_update_id {
2018+
return;
2019+
}
2020+
2021+
let (raa, commitment_update, order, pending_forwards, mut pending_failures, needs_broadcast_safe, funding_locked) = channel.monitor_updating_restored();
2022+
if !pending_forwards.is_empty() {
2023+
htlc_forwards.push((channel.get_short_channel_id().expect("We can't have pending forwards before funding confirmation"), pending_forwards));
2024+
}
2025+
htlc_failures.append(&mut pending_failures);
2026+
2027+
macro_rules! handle_cs { () => {
2028+
if let Some(update) = commitment_update {
2029+
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2030+
node_id: channel.get_their_node_id(),
2031+
updates: update,
2032+
});
2033+
}
2034+
} }
2035+
macro_rules! handle_raa { () => {
2036+
if let Some(revoke_and_ack) = raa {
2037+
pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
2038+
node_id: channel.get_their_node_id(),
2039+
msg: revoke_and_ack,
2040+
});
2041+
}
2042+
} }
2043+
match order {
2044+
RAACommitmentOrder::CommitmentFirst => {
2045+
handle_cs!();
2046+
handle_raa!();
2047+
},
2048+
RAACommitmentOrder::RevokeAndACKFirst => {
2049+
handle_raa!();
2050+
handle_cs!();
2051+
},
2052+
}
2053+
if needs_broadcast_safe {
2054+
pending_events.push(events::Event::FundingBroadcastSafe {
2055+
funding_txo: channel.get_funding_txo().unwrap(),
2056+
user_channel_id: channel.get_user_id(),
2057+
});
2058+
}
2059+
if let Some(msg) = funding_locked {
2060+
pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
2061+
node_id: channel.get_their_node_id(),
2062+
msg,
2063+
});
2064+
if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
2065+
pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
2066+
node_id: channel.get_their_node_id(),
2067+
msg: announcement_sigs,
2068+
});
2069+
}
2070+
short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
2071+
}
2072+
}
2073+
2074+
self.pending_events.lock().unwrap().append(&mut pending_events);
2075+
2076+
for failure in htlc_failures.drain(..) {
2077+
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
2078+
}
2079+
self.forward_htlcs(&mut htlc_forwards[..]);
2080+
2081+
for res in close_results.drain(..) {
2082+
self.finish_force_close_channel(res);
2083+
}
2084+
}
2085+
19892086
/// Used to restore channels to normal operation after a
19902087
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
19912088
/// operation.
@@ -3532,7 +3629,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> R
35323629
if let Some(ref mut monitor) = args.channel_monitors.get_mut(&funding_txo) {
35333630
if channel.get_cur_local_commitment_transaction_number() != monitor.get_cur_local_commitment_number() ||
35343631
channel.get_revoked_remote_commitment_transaction_number() != monitor.get_min_seen_secret() ||
3535-
channel.get_cur_remote_commitment_transaction_number() != monitor.get_cur_remote_commitment_number() {
3632+
channel.get_cur_remote_commitment_transaction_number() != monitor.get_cur_remote_commitment_number() ||
3633+
channel.get_latest_monitor_update_id() != monitor.get_latest_update_id() {
35363634
let mut force_close_res = channel.force_shutdown();
35373635
force_close_res.0 = monitor.get_latest_local_commitment_txn();
35383636
closed_channels.push(force_close_res);

0 commit comments

Comments
 (0)