Skip to content

Commit d926be2

Browse files
committed
Verify channel-monitor processes blocks with skipped best_block
This can happen due to races b/w client's call to block_connect and adding newly created channel-monitor to chain-monitor using watch_channel in funding_created.
1 parent b5e9594 commit d926be2

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

lightning/src/ln/functional_tests.rs

+67
Original file line numberDiff line numberDiff line change
@@ -4080,6 +4080,73 @@ fn test_channel_ready_without_best_block_updated() {
40804080
nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
40814081
}
40824082

4083+
#[test]
4084+
fn test_channel_monitor_skipping_block_when_channel_manager_is_leading() {
4085+
let chanmon_cfgs = create_chanmon_cfgs(2);
4086+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4087+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4088+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4089+
4090+
// Let channel_manager get ahead of chain_monitor by 1 block.
4091+
// This is to emulate race-condition where newly added channel_monitor skips processing 1 block,
4092+
// in case where client calls block_connect on channel_manager first and then on chain_monitor.
4093+
let height_1 = nodes[0].best_block_info().1 + 1;
4094+
let mut block_1 = create_dummy_block(nodes[0].best_block_hash(), height_1, Vec::new());
4095+
4096+
nodes[0].blocks.lock().unwrap().push((block_1.clone(), height_1));
4097+
nodes[0].node.block_connected(&block_1, height_1);
4098+
4099+
// Create channel, and it gets added to chain_monitor in funding_created.
4100+
let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
4101+
4102+
// Now, newly added channel_monitor in chain_monitor hasn't processed block_1,
4103+
// but it's best_block is block_1, since that was populated by channel_manager, and channel_manager
4104+
// was running ahead of chain_monitor at the time of funding_created.
4105+
// Later on, subsequent blocks are connected to both channel_manager and chain_monitor.
4106+
// Hence, this channel's channel_monitor skipped block_1, directly tries to process subsequent blocks.
4107+
confirm_transaction_at(&nodes[0], &funding_tx, nodes[0].best_block_info().1 + 1);
4108+
connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
4109+
4110+
// Ensure nodes[0] generates a channel_ready after the transactions_confirmed
4111+
let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
4112+
nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
4113+
}
4114+
4115+
#[test]
4116+
fn test_channel_monitor_skipping_block_when_channel_manager_is_lagging() {
4117+
let chanmon_cfgs = create_chanmon_cfgs(2);
4118+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4119+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4120+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4121+
4122+
// Let chain_monitor get ahead of channel_manager by 1 block.
4123+
// This is to emulate race-condition where newly added channel_monitor skips processing 1 block,
4124+
// in case where client calls block_connect on chain_monitor first and then on channel_manager.
4125+
let height_1 = nodes[0].best_block_info().1 + 1;
4126+
let mut block_1 = create_dummy_block(nodes[0].best_block_hash(), height_1, Vec::new());
4127+
4128+
nodes[0].blocks.lock().unwrap().push((block_1.clone(), height_1));
4129+
nodes[0].chain_monitor.chain_monitor.block_connected(&block_1, height_1);
4130+
4131+
// Create channel, and it gets added to chain_monitor in funding_created.
4132+
let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
4133+
4134+
// channel_manager can't really skip block_1, it should get it eventually.
4135+
nodes[0].node.block_connected(&block_1, height_1);
4136+
4137+
// Now, newly added channel_monitor in chain_monitor hasn't processed block_1, it's best_block is
4138+
// the block before block_1, since that was populated by channel_manager, and channel_manager was
4139+
// running behind at the time of funding_created.
4140+
// Later on, subsequent blocks are connected to both channel_manager and chain_monitor.
4141+
// Hence, this channel's channel_monitor skipped block_1, directly tries to process subsequent blocks.
4142+
confirm_transaction_at(&nodes[0], &funding_tx, nodes[0].best_block_info().1 + 1);
4143+
connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
4144+
4145+
// Ensure nodes[0] generates a channel_ready after the transactions_confirmed
4146+
let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
4147+
nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
4148+
}
4149+
40834150
#[test]
40844151
fn test_drop_messages_peer_disconnect_dual_htlc() {
40854152
// Test that we can handle reconnecting when both sides of a channel have pending

0 commit comments

Comments
 (0)