Skip to content

Commit cd7bdd0

Browse files
committed
Expand test coverage in monitor_tests to incl local prev commitment
1 parent 641b159 commit cd7bdd0

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

Diff for: lightning/src/ln/monitor_tests.rs

+53-14
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,18 @@ fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
182182
v
183183
}
184184

185-
#[test]
186-
fn test_claim_value_force_close() {
185+
fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
187186
// Tests `get_claimable_balances` with an HTLC across a force-close.
188187
// We build a channel with an HTLC pending, then force close the channel and check that the
189188
// `get_claimable_balances` return value is correct as transactions confirm on-chain.
190-
let chanmon_cfgs = create_chanmon_cfgs(2);
189+
let mut chanmon_cfgs = create_chanmon_cfgs(2);
190+
if prev_commitment_tx {
191+
// We broadcast a second-to-latest commitment transaction, without providing the revocation
192+
// secret to the counterparty. However, because we always immediately take the revocation
193+
// secret from the keys_manager, we would panic at broadcast as we're trying to sign a
194+
// transaction which, from the point of view of our keys_manager, is revoked.
195+
chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
196+
}
191197
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
192198
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
193199
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
@@ -230,31 +236,52 @@ fn test_claim_value_force_close() {
230236

231237
nodes[1].node.claim_funds(payment_preimage);
232238
check_added_monitors!(nodes[1], 1);
233-
get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
239+
let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
234240
// We claim the dust payment here as well, but it won't impact our claimable balances as its
235241
// dust and thus doesn't appear on chain at all.
236242
nodes[1].node.claim_funds(dust_payment_preimage);
237243
check_added_monitors!(nodes[1], 1);
238244
nodes[1].node.claim_funds(timeout_payment_preimage);
239245
check_added_monitors!(nodes[1], 1);
240246

247+
if prev_commitment_tx {
248+
// To build a previous commitment transaction, deliver one round of commitment messages.
249+
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.update_fulfill_htlcs[0]);
250+
expect_payment_sent!(nodes[0], payment_preimage);
251+
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &b_htlc_msgs.commitment_signed);
252+
check_added_monitors!(nodes[0], 1);
253+
let (as_raa, as_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
254+
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
255+
let _htlc_updates = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
256+
check_added_monitors!(nodes[1], 1);
257+
nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs);
258+
let _bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
259+
check_added_monitors!(nodes[1], 1);
260+
}
261+
241262
// Once B has received the payment preimage, it includes the value of the HTLC in its
242263
// "claimable if you were to close the channel" balance.
243-
assert_eq!(sorted_vec(vec![ClaimableBalance::ClaimableOnChannelClose {
264+
let mut a_expected_balances = vec![ClaimableBalance::ClaimableOnChannelClose {
244265
claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
245266
4_000 - // The to-be-failed HTLC value in satoshis
246267
3_000 - // The claimed HTLC value in satoshis
247268
1_000 - // The push_msat value in satoshis
248269
3 - // The dust HTLC value in satoshis
249270
// The commitment transaction fee with two HTLC outputs:
250-
chan_feerate * (channel::COMMITMENT_TX_BASE_WEIGHT + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
251-
}, ClaimableBalance::MaybeClaimableHTLCAwaitingTimeout {
252-
claimable_amount_satoshis: 3_000,
253-
claimable_height: htlc_cltv_timeout,
271+
chan_feerate * (channel::COMMITMENT_TX_BASE_WEIGHT +
272+
if prev_commitment_tx { 1 } else { 2 } *
273+
channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
254274
}, ClaimableBalance::MaybeClaimableHTLCAwaitingTimeout {
255275
claimable_amount_satoshis: 4_000,
256276
claimable_height: htlc_cltv_timeout,
257-
}]),
277+
}];
278+
if !prev_commitment_tx {
279+
a_expected_balances.push(ClaimableBalance::MaybeClaimableHTLCAwaitingTimeout {
280+
claimable_amount_satoshis: 3_000,
281+
claimable_height: htlc_cltv_timeout,
282+
});
283+
}
284+
assert_eq!(sorted_vec(a_expected_balances),
258285
sorted_vec(nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances()));
259286
assert_eq!(vec![ClaimableBalance::ClaimableOnChannelClose {
260287
claimable_amount_satoshis: 1_000 + 3_000 + 4_000,
@@ -268,9 +295,13 @@ fn test_claim_value_force_close() {
268295
mine_transaction(&nodes[1], &remote_txn[0]);
269296

270297
let b_broadcast_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
271-
assert_eq!(b_broadcast_txn.len(), 5);
272-
assert_eq!(b_broadcast_txn[0], b_broadcast_txn[3]);
273-
assert_eq!(b_broadcast_txn[1], b_broadcast_txn[4]);
298+
assert_eq!(b_broadcast_txn.len(), if prev_commitment_tx { 4 } else { 5 });
299+
if prev_commitment_tx {
300+
check_spends!(b_broadcast_txn[3], b_broadcast_txn[2]);
301+
} else {
302+
assert_eq!(b_broadcast_txn[0], b_broadcast_txn[3]);
303+
assert_eq!(b_broadcast_txn[1], b_broadcast_txn[4]);
304+
}
274305
// b_broadcast_txn[0] should spend the HTLC output of the commitment tx for 3_000 sats
275306
check_spends!(b_broadcast_txn[0], remote_txn[0]);
276307
check_spends!(b_broadcast_txn[1], remote_txn[0]);
@@ -365,7 +396,9 @@ fn test_claim_value_force_close() {
365396
// After broadcasting the HTLC claim transaction, node A will still consider the HTLC
366397
// possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
367398
mine_transaction(&nodes[0], &b_broadcast_txn[0]);
368-
expect_payment_sent!(nodes[0], payment_preimage);
399+
if !prev_commitment_tx {
400+
expect_payment_sent!(nodes[0], payment_preimage);
401+
}
369402
assert_eq!(sorted_vec(vec![ClaimableBalance::MaybeClaimableHTLCAwaitingTimeout {
370403
claimable_amount_satoshis: 3_000,
371404
claimable_height: htlc_cltv_timeout,
@@ -493,3 +526,9 @@ fn test_claim_value_force_close() {
493526
assert_eq!(Vec::<ClaimableBalance>::new(),
494527
nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap().get(&funding_outpoint).unwrap().get_claimable_balances());
495528
}
529+
530+
#[test]
531+
fn test_claim_value_force_close() {
532+
do_test_claim_value_force_close(true);
533+
do_test_claim_value_force_close(false);
534+
}

0 commit comments

Comments
 (0)