Skip to content

Commit 9d543bf

Browse files
committed
Test pending payments when duplicatively resolved on chain
1 parent b6474b6 commit 9d543bf

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4302,6 +4302,108 @@ fn test_no_txn_manager_serialize_deserialize() {
43024302
send_payment(&nodes[0], &[&nodes[1]], 1000000);
43034303
}
43044304

4305+
#[test]
4306+
fn test_dup_htlc_onchain_fails_on_reload() {
4307+
// When a Channel is closed, any outbound HTLCs which were relayed through it are simply
4308+
// dropped when the Channel is. From there, the ChannelManager relies on the ChannelMonitor
4309+
// having a copy of the relevant fail-/claim-back data and processes the HTLC fail/claim when
4310+
// the ChannelMonitor tells it to.
4311+
//
4312+
// If, due to an on-chain event, an HTLC is failed/claimed, and then we serialize the
4313+
// ChannelManager, we generally expect there not to be a duplicate HTLC fail/claim (eg via a
4314+
// PaymentFailed event appearing). However, because we may not serialize the relevant
4315+
// ChannelMonitor at the same time, this isn't strictly guaranteed. In order to provide this
4316+
// consistency, the ChannelManager explicitly tracks pending-onchain-resolution outbound HTLCs
4317+
// and de-duplicates ChannelMonitor events.
4318+
//
4319+
// This tests that explicit tracking behavior.
4320+
let chanmon_cfgs = create_chanmon_cfgs(2);
4321+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4322+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4323+
let persister: test_utils::TestPersister;
4324+
let new_chain_monitor: test_utils::TestChainMonitor;
4325+
let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
4326+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4327+
4328+
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
4329+
4330+
// Route a payment, but force-close the channel before the HTLC fulfill message arrives at
4331+
// nodes[0].
4332+
let (payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1]], 10000000);
4333+
nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id).unwrap();
4334+
check_closed_broadcast!(nodes[0], true);
4335+
check_added_monitors!(nodes[0], 1);
4336+
4337+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
4338+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
4339+
4340+
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4341+
assert_eq!(node_txn.len(), 2);
4342+
4343+
assert!(nodes[1].node.claim_funds(payment_preimage));
4344+
check_added_monitors!(nodes[1], 1);
4345+
4346+
let mut header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
4347+
connect_block(&nodes[1], &Block { header, txdata: vec![node_txn[0].clone(), node_txn[1].clone()]});
4348+
check_closed_broadcast!(nodes[1], true);
4349+
check_added_monitors!(nodes[1], 1);
4350+
let claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4351+
4352+
connect_block(&nodes[0], &Block { header, txdata: node_txn});
4353+
4354+
// Serialize out the ChannelMonitor before connecting the on-chain claim transactions. This is
4355+
// fairly normal behavior as ChannelMonitor(s) are often not re-serialized when on-chain events
4356+
// happen, unlike ChannelManager which tends to be re-serialized after any relevant event(s).
4357+
let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
4358+
nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
4359+
4360+
header.prev_blockhash = header.block_hash();
4361+
let claim_block = Block { header, txdata: claim_txn};
4362+
connect_block(&nodes[0], &claim_block);
4363+
expect_payment_sent!(nodes[0], payment_preimage);
4364+
4365+
// ChannelManagers generally get re-serialized after any relevant event(s). Since we just
4366+
// connected a highly-relevant block, it likely gets serialized out now.
4367+
let mut chan_manager_serialized = test_utils::TestVecWriter(Vec::new());
4368+
nodes[0].node.write(&mut chan_manager_serialized).unwrap();
4369+
4370+
// Now reload nodes[0]...
4371+
persister = test_utils::TestPersister::new();
4372+
let keys_manager = &chanmon_cfgs[0].keys_manager;
4373+
new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), nodes[0].logger, node_cfgs[0].fee_estimator, &persister, keys_manager);
4374+
nodes[0].chain_monitor = &new_chain_monitor;
4375+
let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
4376+
let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
4377+
&mut chan_0_monitor_read, keys_manager).unwrap();
4378+
assert!(chan_0_monitor_read.is_empty());
4379+
4380+
let (_, nodes_0_deserialized_tmp) = {
4381+
let mut channel_monitors = HashMap::new();
4382+
channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
4383+
<(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>
4384+
::read(&mut std::io::Cursor::new(&chan_manager_serialized.0[..]), ChannelManagerReadArgs {
4385+
default_config: Default::default(),
4386+
keys_manager,
4387+
fee_estimator: node_cfgs[0].fee_estimator,
4388+
chain_monitor: nodes[0].chain_monitor,
4389+
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
4390+
logger: nodes[0].logger,
4391+
channel_monitors,
4392+
}).unwrap()
4393+
};
4394+
nodes_0_deserialized = nodes_0_deserialized_tmp;
4395+
4396+
assert!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor).is_ok());
4397+
check_added_monitors!(nodes[0], 1);
4398+
nodes[0].node = &nodes_0_deserialized;
4399+
4400+
// Note that if we re-connect the block which exposed nodes[0] to the payment preimage (but
4401+
// which the current ChannelMonitor has not seen), the ChannelManager's de-duplication of
4402+
// payment events should kick in, leaving us with no pending events here.
4403+
nodes[0].chain_monitor.chain_monitor.block_connected(&claim_block, nodes[0].blocks.borrow().len() as u32 - 1);
4404+
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
4405+
}
4406+
43054407
#[test]
43064408
fn test_manager_serialize_deserialize_events() {
43074409
// This test makes sure the events field in ChannelManager survives de/serialization

0 commit comments

Comments
 (0)