X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;ds=sidebyside;f=lightning%2Fsrc%2Fln%2Freorg_tests.rs;h=c9573a67c13244cc73e1298ca43a9e1d66537e33;hb=eee1c30ea6592463d41eefad8306223ec5548014;hp=316e165ab96b1cbeb1addf3c6bb931e8f3eef091;hpb=561f0e22ac6dc3ff664a3698a12c5c920d35ae4d;p=rust-lightning diff --git a/lightning/src/ln/reorg_tests.rs b/lightning/src/ln/reorg_tests.rs index 316e165a..c9573a67 100644 --- a/lightning/src/ln/reorg_tests.rs +++ b/lightning/src/ln/reorg_tests.rs @@ -53,9 +53,9 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known()); // Make sure all nodes are at the same starting height - connect_blocks(&nodes[0], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1, nodes[0].best_block_info().1, false, Default::default()); - connect_blocks(&nodes[1], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1, nodes[1].best_block_info().1, false, Default::default()); - connect_blocks(&nodes[2], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1, nodes[2].best_block_info().1, false, Default::default()); + connect_blocks(&nodes[0], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1); + connect_blocks(&nodes[1], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1); + connect_blocks(&nodes[2], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1); let (our_payment_preimage, our_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000); @@ -74,9 +74,9 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { check_spends!(node_1_commitment_txn[1], node_1_commitment_txn[0]); // Give node 2 node 1's transactions and get its response (claiming the HTLC instead). - connect_block(&nodes[2], &Block { header, txdata: node_1_commitment_txn.clone() }, CHAN_CONFIRM_DEPTH*2 + 2); + connect_block(&nodes[2], &Block { header, txdata: node_1_commitment_txn.clone() }); check_added_monitors!(nodes[2], 1); - check_closed_broadcast!(nodes[2], false); // We should get a BroadcastChannelUpdate (and *only* a BroadcstChannelUpdate) + check_closed_broadcast!(nodes[2], true); // We should get a BroadcastChannelUpdate (and *only* a BroadcstChannelUpdate) let node_2_commitment_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_2_commitment_txn.len(), 3); // ChannelMonitor: 1 offered HTLC-Claim, ChannelManger: 1 local commitment tx, 1 Received HTLC-Claim assert_eq!(node_2_commitment_txn[1].output.len(), 2); // to-remote and Received HTLC (to-self is dust) @@ -85,10 +85,10 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { check_spends!(node_2_commitment_txn[0], node_1_commitment_txn[0]); // Make sure node 1's height is the same as the !local_commitment case - connect_blocks(&nodes[1], 1, CHAN_CONFIRM_DEPTH*2 + 1, false, Default::default()); + connect_blocks(&nodes[1], 1); // Confirm node 1's commitment txn (and HTLC-Timeout) on node 1 header.prev_blockhash = nodes[1].best_block_hash(); - connect_block(&nodes[1], &Block { header, txdata: node_1_commitment_txn.clone() }, CHAN_CONFIRM_DEPTH*2 + 3); + connect_block(&nodes[1], &Block { header, txdata: node_1_commitment_txn.clone() }); // ...but return node 1's commitment tx in case claim is set and we're preparing to reorg vec![node_1_commitment_txn[0].clone(), node_2_commitment_txn[0].clone()] @@ -111,14 +111,14 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { // Confirm node 2's commitment txn (and node 1's HTLC-Timeout) on node 1 header.prev_blockhash = nodes[1].best_block_hash(); - connect_block(&nodes[1], &Block { header, txdata: vec![node_2_commitment_txn[0].clone(), node_1_commitment_txn[0].clone()] }, CHAN_CONFIRM_DEPTH*2 + 3); + connect_block(&nodes[1], &Block { header, txdata: vec![node_2_commitment_txn[0].clone(), node_1_commitment_txn[0].clone()] }); // ...but return node 2's commitment tx (and claim) in case claim is set and we're preparing to reorg node_2_commitment_txn }; check_added_monitors!(nodes[1], 1); - check_closed_broadcast!(nodes[1], false); // We should get a BroadcastChannelUpdate (and *only* a BroadcstChannelUpdate) + check_closed_broadcast!(nodes[1], true); // We should get a BroadcastChannelUpdate (and *only* a BroadcstChannelUpdate) // Connect ANTI_REORG_DELAY - 2 blocks, giving us a confirmation count of ANTI_REORG_DELAY - 1. - connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2, nodes[1].best_block_info().1, false, Default::default()); + connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2); check_added_monitors!(nodes[1], 0); assert_eq!(nodes[1].node.get_and_clear_pending_events().len(), 0); @@ -129,7 +129,7 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { header: BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }, txdata: claim_txn, }; - connect_block(&nodes[1], &block, CHAN_CONFIRM_DEPTH*2 + 4); + connect_block(&nodes[1], &block); // ChannelManager only polls chain::Watch::release_pending_monitor_events when we // probe it for events, so we probe non-message events here (which should still end up empty): @@ -140,7 +140,7 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { header: BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }, txdata: vec![], }; - connect_block(&nodes[1], &block, CHAN_CONFIRM_DEPTH*2 + 3 + ANTI_REORG_DELAY - 1); + connect_block(&nodes[1], &block); expect_pending_htlcs_forwardable!(nodes[1]); } @@ -184,7 +184,7 @@ fn test_onchain_htlc_timeout_delay_remote_commitment() { do_test_onchain_htlc_reorg(false, false); } -fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool) { +fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, connect_style: ConnectStyle) { // After creating a chan between nodes, we disconnect all blocks previously seen to force a // channel close on nodes[0] side. We also use this to provide very basic testing of logic // around freeing background events which store monitor updates during block_[dis]connected. @@ -195,6 +195,8 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool) { let new_chain_monitor: test_utils::TestChainMonitor; let nodes_0_deserialized: ChannelManager; let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); + *nodes[0].connect_style.borrow_mut() = connect_style; + let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; let channel_state = nodes[0].node.channel_state.lock().unwrap(); @@ -204,7 +206,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool) { if !reorg_after_reload { disconnect_all_blocks(&nodes[0]); - check_closed_broadcast!(nodes[0], false); + check_closed_broadcast!(nodes[0], true); { let channel_state = nodes[0].node.channel_state.lock().unwrap(); assert_eq!(channel_state.by_id.len(), 0); @@ -256,7 +258,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool) { if reorg_after_reload { disconnect_all_blocks(&nodes[0]); - check_closed_broadcast!(nodes[0], false); + check_closed_broadcast!(nodes[0], true); { let channel_state = nodes[0].node.channel_state.lock().unwrap(); assert_eq!(channel_state.by_id.len(), 0); @@ -272,10 +274,18 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool) { #[test] fn test_unconf_chan() { - do_test_unconf_chan(true, true); - do_test_unconf_chan(false, true); - do_test_unconf_chan(true, false); - do_test_unconf_chan(false, false); + do_test_unconf_chan(true, true, ConnectStyle::BestBlockFirstSkippingBlocks); + do_test_unconf_chan(false, true, ConnectStyle::BestBlockFirstSkippingBlocks); + do_test_unconf_chan(true, false, ConnectStyle::BestBlockFirstSkippingBlocks); + do_test_unconf_chan(false, false, ConnectStyle::BestBlockFirstSkippingBlocks); +} + +#[test] +fn test_unconf_chan_via_listen() { + do_test_unconf_chan(true, true, ConnectStyle::FullBlockViaListen); + do_test_unconf_chan(false, true, ConnectStyle::FullBlockViaListen); + do_test_unconf_chan(true, false, ConnectStyle::FullBlockViaListen); + do_test_unconf_chan(false, false, ConnectStyle::FullBlockViaListen); } #[test] @@ -311,7 +321,7 @@ fn test_set_outpoints_partial_claiming() { // Connect blocks on node A commitment transaction mine_transaction(&nodes[0], &remote_txn[0]); - check_closed_broadcast!(nodes[0], false); + check_closed_broadcast!(nodes[0], true); check_added_monitors!(nodes[0], 1); // Verify node A broadcast tx claiming both HTLCs { @@ -327,8 +337,8 @@ fn test_set_outpoints_partial_claiming() { } // Connect blocks on node B - connect_blocks(&nodes[1], 135, nodes[1].best_block_info().1, false, Default::default()); - check_closed_broadcast!(nodes[1], false); + connect_blocks(&nodes[1], 135); + check_closed_broadcast!(nodes[1], true); check_added_monitors!(nodes[1], 1); // Verify node B broadcast 2 HTLC-timeout txn let partial_claim_tx = { @@ -364,7 +374,7 @@ fn test_set_outpoints_partial_claiming() { //// Disconnect one more block and then reconnect multiple no transaction should be generated disconnect_blocks(&nodes[0], 1); - connect_blocks(&nodes[0], 15, nodes[0].best_block_info().1, false, Default::default()); + connect_blocks(&nodes[0], 15); { let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_txn.len(), 0);