Skip to content

Commit 511b580

Browse files
committed
Avoid persisting on same counterparty's ChannelUpdate
Some nodes may rebroadcast their `ChannelUpdate` to their counterparty on every connection establishment, which leads to us doing an additional persist most of the time when nothing has changed. Now, we'll only persist if we receive an update that changes anything.
1 parent 082a19b commit 511b580

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

lightning/src/ln/channel.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5537,14 +5537,25 @@ impl<SP: Deref> Channel<SP> where
55375537
}
55385538
}
55395539

5540-
pub fn channel_update(&mut self, msg: &msgs::ChannelUpdate) -> Result<(), ChannelError> {
5541-
self.context.counterparty_forwarding_info = Some(CounterpartyForwardingInfo {
5542-
fee_base_msat: msg.contents.fee_base_msat,
5543-
fee_proportional_millionths: msg.contents.fee_proportional_millionths,
5544-
cltv_expiry_delta: msg.contents.cltv_expiry_delta
5545-
});
5540+
/// Applies the `ChannelUpdate` and returns a boolean indicating whether a change actually
5541+
/// happened.
5542+
pub fn channel_update(&mut self, msg: &msgs::ChannelUpdate) -> Result<bool, ChannelError> {
5543+
let did_change = self.context.counterparty_forwarding_info.as_ref()
5544+
.map(|fwd_info|
5545+
fwd_info.fee_base_msat != msg.contents.fee_base_msat ||
5546+
fwd_info.fee_proportional_millionths != msg.contents.fee_proportional_millionths ||
5547+
fwd_info.cltv_expiry_delta != msg.contents.cltv_expiry_delta
5548+
).unwrap_or(true);
5549+
5550+
if did_change {
5551+
self.context.counterparty_forwarding_info = Some(CounterpartyForwardingInfo {
5552+
fee_base_msat: msg.contents.fee_base_msat,
5553+
fee_proportional_millionths: msg.contents.fee_proportional_millionths,
5554+
cltv_expiry_delta: msg.contents.cltv_expiry_delta
5555+
});
5556+
}
55465557

5547-
Ok(())
5558+
Ok(did_change)
55485559
}
55495560

55505561
/// Begins the shutdown process, getting a message for the remote peer and returning all
@@ -8140,7 +8151,7 @@ mod tests {
81408151
},
81418152
signature: Signature::from(unsafe { FFISignature::new() })
81428153
};
8143-
node_a_chan.channel_update(&update).unwrap();
8154+
assert!(node_a_chan.channel_update(&update).unwrap());
81448155

81458156
// The counterparty can send an update with a higher minimum HTLC, but that shouldn't
81468157
// change our official htlc_minimum_msat.
@@ -8153,6 +8164,8 @@ mod tests {
81538164
},
81548165
None => panic!("expected counterparty forwarding info to be Some")
81558166
}
8167+
8168+
assert!(!node_a_chan.channel_update(&update).unwrap());
81568169
}
81578170

81588171
#[cfg(feature = "_test_vectors")]

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6757,7 +6757,12 @@ where
67576757
return Ok(NotifyOption::SkipPersistNoEvents);
67586758
} else {
67596759
log_debug!(self.logger, "Received channel_update {:?} for channel {}.", msg, chan_id);
6760-
try_chan_phase_entry!(self, chan.channel_update(&msg), chan_phase_entry);
6760+
let did_change = try_chan_phase_entry!(self, chan.channel_update(&msg), chan_phase_entry);
6761+
// If nothing changed after applying their update, we don't need to bother
6762+
// persisting.
6763+
if !did_change {
6764+
return Ok(NotifyOption::SkipPersistNoEvents);
6765+
}
67616766
}
67626767
} else {
67636768
return try_chan_phase_entry!(self, Err(ChannelError::Close(

0 commit comments

Comments
 (0)