|
| 1 | +use crate::events::{ClosureReason, Event}; |
1 | 2 | use crate::ln::chan_utils::shared_anchor_script_pubkey;
|
2 | 3 | use crate::ln::functional_test_utils::*;
|
| 4 | +use crate::ln::msgs::BaseMessageHandler; |
3 | 5 |
|
4 | 6 | #[test]
|
5 | 7 | fn test_p2a_anchor_values_under_trims_and_rounds() {
|
@@ -92,3 +94,128 @@ fn test_p2a_anchor_values_under_trims_and_rounds() {
|
92 | 94 | p2a_value_test!([353_000], [353_001], 240);
|
93 | 95 | p2a_value_test!([353_001], [353_001], 240);
|
94 | 96 | }
|
| 97 | + |
| 98 | +#[test] |
| 99 | +fn test_htlc_claim_chunking() { |
| 100 | + let chanmon_cfgs = create_chanmon_cfgs(2); |
| 101 | + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); |
| 102 | + let mut user_cfg = test_default_channel_config(); |
| 103 | + user_cfg.channel_handshake_config.our_htlc_minimum_msat = 1; |
| 104 | + user_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true; |
| 105 | + user_cfg.manually_accept_inbound_channels = true; |
| 106 | + |
| 107 | + let configs = [Some(user_cfg.clone()), Some(user_cfg)]; |
| 108 | + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &configs); |
| 109 | + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); |
| 110 | + |
| 111 | + let coinbase_tx = provide_anchor_reserves(&nodes); |
| 112 | + |
| 113 | + let _node_a_id = nodes[0].node.get_our_node_id(); |
| 114 | + let _node_b_id = nodes[1].node.get_our_node_id(); |
| 115 | + |
| 116 | + const CHAN_CAPACITY: u64 = 10_000_000; |
| 117 | + let (_, _, chan_id, _funding_tx) = create_announced_chan_between_nodes_with_value( |
| 118 | + &nodes, |
| 119 | + 0, |
| 120 | + 1, |
| 121 | + CHAN_CAPACITY, |
| 122 | + (CHAN_CAPACITY / 2) * 1000, |
| 123 | + ); |
| 124 | + |
| 125 | + let mut node_1_preimages = Vec::new(); |
| 126 | + const NONDUST_HTLC_AMT_MSAT: u64 = 1_000_000; |
| 127 | + for _ in 0..26 { |
| 128 | + let (preimage, payment_hash, _, _) = |
| 129 | + route_payment(&nodes[0], &[&nodes[1]], NONDUST_HTLC_AMT_MSAT); |
| 130 | + node_1_preimages.push((preimage, payment_hash)); |
| 131 | + } |
| 132 | + let node_0_commit_tx = get_local_commitment_txn!(nodes[0], chan_id); |
| 133 | + assert_eq!(node_0_commit_tx.len(), 1); |
| 134 | + assert_eq!(node_0_commit_tx[0].output.len(), 26 + 2 + 1); |
| 135 | + let node_1_commit_tx = get_local_commitment_txn!(nodes[1], chan_id); |
| 136 | + assert_eq!(node_1_commit_tx.len(), 1); |
| 137 | + assert_eq!(node_1_commit_tx[0].output.len(), 26 + 2 + 1); |
| 138 | + |
| 139 | + for (preimage, payment_hash) in node_1_preimages { |
| 140 | + nodes[1].node.claim_funds(preimage); |
| 141 | + check_added_monitors!(nodes[1], 1); |
| 142 | + expect_payment_claimed!(nodes[1], payment_hash, NONDUST_HTLC_AMT_MSAT); |
| 143 | + } |
| 144 | + nodes[0].node.get_and_clear_pending_msg_events(); |
| 145 | + nodes[1].node.get_and_clear_pending_msg_events(); |
| 146 | + |
| 147 | + mine_transaction(&nodes[0], &node_1_commit_tx[0]); |
| 148 | + mine_transaction(&nodes[1], &node_1_commit_tx[0]); |
| 149 | + |
| 150 | + let mut events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events(); |
| 151 | + assert_eq!(events.len(), 1); |
| 152 | + match events.pop().unwrap() { |
| 153 | + Event::BumpTransaction(bump_event) => { |
| 154 | + nodes[1].bump_tx_handler.handle_event(&bump_event); |
| 155 | + }, |
| 156 | + _ => panic!("Unexpected event"), |
| 157 | + } |
| 158 | + |
| 159 | + let htlc_claims = nodes[1].tx_broadcaster.txn_broadcast(); |
| 160 | + assert_eq!(htlc_claims.len(), 2); |
| 161 | + |
| 162 | + check_spends!(htlc_claims[0], node_1_commit_tx[0], coinbase_tx); |
| 163 | + check_spends!(htlc_claims[1], node_1_commit_tx[0], coinbase_tx); |
| 164 | + |
| 165 | + assert_eq!(htlc_claims[0].input.len(), 26); |
| 166 | + assert_eq!(htlc_claims[0].output.len(), 26); |
| 167 | + assert_eq!(htlc_claims[1].input.len(), 2); |
| 168 | + assert_eq!(htlc_claims[1].output.len(), 2); |
| 169 | + |
| 170 | + check_closed_broadcast!(nodes[0], true); |
| 171 | + check_added_monitors!(nodes[0], 1); |
| 172 | + check_closed_event!( |
| 173 | + nodes[0], |
| 174 | + 1, |
| 175 | + ClosureReason::CommitmentTxConfirmed, |
| 176 | + [nodes[1].node.get_our_node_id()], |
| 177 | + CHAN_CAPACITY |
| 178 | + ); |
| 179 | + assert!(nodes[0].node.list_channels().is_empty()); |
| 180 | + check_closed_broadcast!(nodes[1], true); |
| 181 | + check_added_monitors!(nodes[1], 1); |
| 182 | + check_closed_event!( |
| 183 | + nodes[1], |
| 184 | + 1, |
| 185 | + ClosureReason::CommitmentTxConfirmed, |
| 186 | + [nodes[0].node.get_our_node_id()], |
| 187 | + CHAN_CAPACITY |
| 188 | + ); |
| 189 | + assert!(nodes[1].node.list_channels().is_empty()); |
| 190 | + assert!(nodes[0].node.get_and_clear_pending_events().is_empty()); |
| 191 | + assert!(nodes[1].node.get_and_clear_pending_events().is_empty()); |
| 192 | + |
| 193 | + mine_transaction(&nodes[1], &htlc_claims[0]); |
| 194 | + |
| 195 | + let mut events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events(); |
| 196 | + assert_eq!(events.len(), 1); |
| 197 | + match events.pop().unwrap() { |
| 198 | + Event::BumpTransaction(bump_event) => { |
| 199 | + nodes[1].bump_tx_handler.handle_event(&bump_event); |
| 200 | + }, |
| 201 | + _ => panic!("Unexpected event"), |
| 202 | + } |
| 203 | + |
| 204 | + let fresh_htlc_claims = nodes[1].tx_broadcaster.txn_broadcast(); |
| 205 | + assert_eq!(fresh_htlc_claims.len(), 1); |
| 206 | + check_spends!(fresh_htlc_claims[0], node_1_commit_tx[0], htlc_claims[0]); |
| 207 | + assert_eq!(fresh_htlc_claims[0].input.len(), 2); |
| 208 | + assert_eq!(fresh_htlc_claims[0].output.len(), 2); |
| 209 | + |
| 210 | + // Assert when we handled the second `BumpTransaction::HTLCResolution` event, we |
| 211 | + // assigned the same UTXO id to that set of HTLCs |
| 212 | + let log_entries = &nodes[1].logger.lines.lock().unwrap(); |
| 213 | + let keys: Vec<_> = log_entries |
| 214 | + .keys() |
| 215 | + .filter(|key| key.1.contains("Batch transaction assigned to UTXO id")) |
| 216 | + .collect(); |
| 217 | + assert_eq!(keys.len(), 2); |
| 218 | + let values = [*log_entries.get(keys[0]).unwrap(), *log_entries.get(keys[1]).unwrap()]; |
| 219 | + assert!(values.contains(&1)); |
| 220 | + assert!(values.contains(&2)); |
| 221 | +} |
0 commit comments