Skip to content

Commit 00da3fc

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 336d815 commit 00da3fc

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lightning/src/ln/channel.rs

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

5540-
pub fn channel_update(&mut self, msg: &msgs::ChannelUpdate) -> Result<(), ChannelError> {
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> {
55415543
if msg.contents.htlc_minimum_msat >= self.context.channel_value_satoshis * 1000 {
55425544
return Err(ChannelError::Close("Minimum htlc value is greater than channel value".to_string()));
55435545
}
5546+
let prev_forwarding_info = self.context.counterparty_forwarding_info();
55445547
self.context.counterparty_forwarding_info = Some(CounterpartyForwardingInfo {
55455548
fee_base_msat: msg.contents.fee_base_msat,
55465549
fee_proportional_millionths: msg.contents.fee_proportional_millionths,
55475550
cltv_expiry_delta: msg.contents.cltv_expiry_delta
55485551
});
55495552

5550-
Ok(())
5553+
Ok(prev_forwarding_info != self.context.counterparty_forwarding_info)
55515554
}
55525555

55535556
/// Begins the shutdown process, getting a message for the remote peer and returning all
@@ -8143,7 +8146,7 @@ mod tests {
81438146
},
81448147
signature: Signature::from(unsafe { FFISignature::new() })
81458148
};
8146-
node_a_chan.channel_update(&update).unwrap();
8149+
assert!(node_a_chan.channel_update(&update).unwrap());
81478150

81488151
// The counterparty can send an update with a higher minimum HTLC, but that shouldn't
81498152
// change our official htlc_minimum_msat.
@@ -8156,6 +8159,8 @@ mod tests {
81568159
},
81578160
None => panic!("expected counterparty forwarding info to be Some")
81588161
}
8162+
8163+
assert!(!node_a_chan.channel_update(&update).unwrap());
81598164
}
81608165

81618166
#[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 {}.", 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)