Skip to content

Commit 8f89371

Browse files
committed
Update ChannelUpdate::timestamp when channels are dis-/en-abled
We update the `Channel::update_time_counter` field (which is copied into `ChannelUpdate::timestamp`) only when the channel is initialized or closes, and when a new block is connected. However, if a peer disconnects or reconnects, we may wish to generate `ChannelUpdate` updates in between new blocks. In such a case, we need to make sure the `timestamp` field is newer than any previous updates' `timestamp` fields, which we do here by simply incrementing it when the channel status is changed. As a side effect of this we have to update `test_background_processor` to ensure it eventually succeeds even if the serialization of the `ChannelManager` changes after the test begins.
1 parent 74828d2 commit 8f89371

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,10 @@ mod tests {
493493

494494
macro_rules! check_persisted_data {
495495
($node: expr, $filepath: expr, $expected_bytes: expr) => {
496-
match $node.write(&mut $expected_bytes) {
497-
Ok(()) => {
498-
loop {
496+
loop {
497+
$expected_bytes.clear();
498+
match $node.write(&mut $expected_bytes) {
499+
Ok(()) => {
499500
match std::fs::read($filepath) {
500501
Ok(bytes) => {
501502
if bytes == $expected_bytes {
@@ -506,9 +507,9 @@ mod tests {
506507
},
507508
Err(_) => continue
508509
}
509-
}
510-
},
511-
Err(e) => panic!("Unexpected error: {}", e)
510+
},
511+
Err(e) => panic!("Unexpected error: {}", e)
512+
}
512513
}
513514
}
514515
}

lightning/src/ln/channel.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4167,6 +4167,7 @@ impl<Signer: Sign> Channel<Signer> {
41674167
}
41684168

41694169
pub fn set_channel_update_status(&mut self, status: ChannelUpdateStatus) {
4170+
self.update_time_counter += 1;
41704171
self.channel_update_status = status;
41714172
}
41724173

lightning/src/ln/functional_tests.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7313,9 +7313,9 @@ fn test_announce_disable_channels() {
73137313
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
73147314
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
73157315

7316-
let short_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
7317-
let short_id_2 = create_announced_chan_between_nodes(&nodes, 1, 0, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
7318-
let short_id_3 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
7316+
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7317+
create_announced_chan_between_nodes(&nodes, 1, 0, InitFeatures::known(), InitFeatures::known());
7318+
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
73197319

73207320
// Disconnect peers
73217321
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
@@ -7325,13 +7325,13 @@ fn test_announce_disable_channels() {
73257325
nodes[0].node.timer_tick_occurred(); // DisabledStaged -> Disabled
73267326
let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
73277327
assert_eq!(msg_events.len(), 3);
7328-
let mut chans_disabled: HashSet<u64> = [short_id_1, short_id_2, short_id_3].iter().map(|a| *a).collect();
7328+
let mut chans_disabled = HashMap::new();
73297329
for e in msg_events {
73307330
match e {
73317331
MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
73327332
assert_eq!(msg.contents.flags & (1<<1), 1<<1); // The "channel disabled" bit should be set
73337333
// Check that each channel gets updated exactly once
7334-
if !chans_disabled.remove(&msg.contents.short_channel_id) {
7334+
if chans_disabled.insert(msg.contents.short_channel_id, msg.contents.timestamp).is_some() {
73357335
panic!("Generated ChannelUpdate for wrong chan!");
73367336
}
73377337
},
@@ -7367,19 +7367,22 @@ fn test_announce_disable_channels() {
73677367
nodes[0].node.timer_tick_occurred();
73687368
let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
73697369
assert_eq!(msg_events.len(), 3);
7370-
chans_disabled = [short_id_1, short_id_2, short_id_3].iter().map(|a| *a).collect();
73717370
for e in msg_events {
73727371
match e {
73737372
MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
73747373
assert_eq!(msg.contents.flags & (1<<1), 0); // The "channel disabled" bit should be off
7375-
// Check that each channel gets updated exactly once
7376-
if !chans_disabled.remove(&msg.contents.short_channel_id) {
7377-
panic!("Generated ChannelUpdate for wrong chan!");
7374+
match chans_disabled.remove(&msg.contents.short_channel_id) {
7375+
// Each update should have a higher timestamp than the previous one, replacing
7376+
// the old one.
7377+
Some(prev_timestamp) => assert!(msg.contents.timestamp > prev_timestamp),
7378+
None => panic!("Generated ChannelUpdate for wrong chan!"),
73787379
}
73797380
},
73807381
_ => panic!("Unexpected event"),
73817382
}
73827383
}
7384+
// Check that each channel gets updated exactly once
7385+
assert!(chans_disabled.is_empty());
73837386
}
73847387

73857388
#[test]

0 commit comments

Comments
 (0)