Skip to content

Commit e42fd9c

Browse files
committed
f Verify claimable balances exactly
1 parent 51516fb commit e42fd9c

File tree

1 file changed

+11
-122
lines changed

1 file changed

+11
-122
lines changed

lightning/src/ln/monitor_tests.rs

Lines changed: 11 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -346,122 +346,10 @@ fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
346346
v
347347
}
348348

349-
fn verify_claimable_balances(mut balances_1: Vec<Balance>, mut balances_2: Vec<Balance>, margin: u64) {
349+
fn verify_claimable_balances(mut balances_1: Vec<Balance>, mut balances_2: Vec<Balance>) {
350350
balances_1.sort();
351351
balances_2.sort();
352-
assert_eq!(balances_1.len(), balances_2.len());
353-
for i in 0..balances_1.len() {
354-
match (balances_1.get(i).unwrap(), balances_2.get(i).unwrap()) {
355-
(
356-
Balance::ClaimableOnChannelClose {
357-
amount_satoshis: amount_satoshis_1,
358-
transaction_fee_satoshis: transaction_fee_satoshis_1,
359-
outbound_payment_htlc_rounded_msat: outbound_payment_htlc_rounded_msat_1,
360-
outbound_forwarded_htlc_rounded_msat: outbound_forwarded_htlc_rounded_msat_1,
361-
inbound_claiming_htlc_rounded_msat: inbound_claiming_htlc_rounded_msat_1,
362-
inbound_htlc_rounded_msat: inbound_htlc_rounded_msat_1,
363-
},
364-
Balance::ClaimableOnChannelClose {
365-
amount_satoshis: amount_satoshis_2,
366-
transaction_fee_satoshis: transaction_fee_satoshis_2,
367-
outbound_payment_htlc_rounded_msat: outbound_payment_htlc_rounded_msat_2,
368-
outbound_forwarded_htlc_rounded_msat: outbound_forwarded_htlc_rounded_msat_2,
369-
inbound_claiming_htlc_rounded_msat: inbound_claiming_htlc_rounded_msat_2,
370-
inbound_htlc_rounded_msat: inbound_htlc_rounded_msat_2,
371-
},
372-
) => {
373-
assert!(amount_satoshis_1.abs_diff(*amount_satoshis_2) <= margin);
374-
assert!(transaction_fee_satoshis_1.abs_diff(*transaction_fee_satoshis_2) <= margin);
375-
assert!(outbound_payment_htlc_rounded_msat_1.abs_diff(*outbound_payment_htlc_rounded_msat_2) <= margin);
376-
assert!(outbound_forwarded_htlc_rounded_msat_1.abs_diff(*outbound_forwarded_htlc_rounded_msat_2) <= margin);
377-
assert!(inbound_claiming_htlc_rounded_msat_1.abs_diff(*inbound_claiming_htlc_rounded_msat_2) <= margin);
378-
assert!(inbound_htlc_rounded_msat_1.abs_diff(*inbound_htlc_rounded_msat_2) <= margin);
379-
},
380-
(
381-
Balance::ClaimableAwaitingConfirmations {
382-
amount_satoshis: amount_satoshis_1,
383-
confirmation_height: confirmation_height_1,
384-
source: source_1,
385-
},
386-
Balance::ClaimableAwaitingConfirmations {
387-
amount_satoshis: amount_satoshis_2,
388-
confirmation_height: confirmation_height_2,
389-
source: source_2,
390-
},
391-
) => {
392-
assert!(amount_satoshis_1.abs_diff(*amount_satoshis_2) <= margin);
393-
assert_eq!(confirmation_height_1, confirmation_height_2);
394-
assert_eq!(source_1, source_2);
395-
},
396-
(
397-
Balance::ContentiousClaimable {
398-
amount_satoshis: amount_satoshis_1,
399-
timeout_height: timeout_height_1,
400-
payment_hash: payment_hash_1,
401-
payment_preimage: payment_preimage_1,
402-
},
403-
Balance::ContentiousClaimable {
404-
amount_satoshis: amount_satoshis_2,
405-
timeout_height: timeout_height_2,
406-
payment_hash: payment_hash_2,
407-
payment_preimage: payment_preimage_2,
408-
},
409-
) => {
410-
assert!(amount_satoshis_1.abs_diff(*amount_satoshis_2) <= margin);
411-
assert_eq!(timeout_height_1, timeout_height_2);
412-
assert_eq!(payment_hash_1, payment_hash_2);
413-
assert_eq!(payment_preimage_1, payment_preimage_2);
414-
},
415-
(
416-
Balance::MaybeTimeoutClaimableHTLC {
417-
amount_satoshis: amount_satoshis_1,
418-
claimable_height: claimable_height_1,
419-
payment_hash: payment_hash_1,
420-
outbound_payment: outbound_payment_1,
421-
},
422-
Balance::MaybeTimeoutClaimableHTLC {
423-
amount_satoshis: amount_satoshis_2,
424-
claimable_height: claimable_height_2,
425-
payment_hash: payment_hash_2,
426-
outbound_payment: outbound_payment_2,
427-
},
428-
) => {
429-
assert!(amount_satoshis_1.abs_diff(*amount_satoshis_2) <= margin);
430-
assert_eq!(claimable_height_1, claimable_height_2);
431-
assert_eq!(payment_hash_1, payment_hash_2);
432-
assert_eq!(outbound_payment_1, outbound_payment_2);
433-
},
434-
(
435-
Balance::MaybePreimageClaimableHTLC {
436-
amount_satoshis: amount_satoshis_1,
437-
expiry_height: expiry_height_1,
438-
payment_hash: payment_hash_1,
439-
},
440-
Balance::MaybePreimageClaimableHTLC {
441-
amount_satoshis: amount_satoshis_2,
442-
expiry_height: expiry_height_2,
443-
payment_hash: payment_hash_2,
444-
},
445-
) => {
446-
assert!(amount_satoshis_1.abs_diff(*amount_satoshis_2) <= margin);
447-
assert_eq!(expiry_height_1, expiry_height_2);
448-
assert_eq!(payment_hash_1, payment_hash_2);
449-
},
450-
(
451-
Balance::CounterpartyRevokedOutputClaimable {
452-
amount_satoshis: amount_satoshis_1,
453-
},
454-
Balance::CounterpartyRevokedOutputClaimable {
455-
amount_satoshis: amount_satoshis_2,
456-
},
457-
) => {
458-
assert!(amount_satoshis_1.abs_diff(*amount_satoshis_2) <= margin);
459-
},
460-
_ => {
461-
panic!("unexpected balance type");
462-
}
463-
}
464-
}
352+
assert_eq!(balances_1, balances_2);
465353
}
466354

467355
/// Asserts that `a` and `b` are close, but maybe off by up to 5.
@@ -1493,7 +1381,6 @@ fn do_test_revoked_counterparty_commitment_balances(anchors: bool, confirm_htlc_
14931381
outbound_payment: true,
14941382
}],
14951383
nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(),
1496-
5,
14971384
);
14981385

14991386
mine_transaction(&nodes[1], &as_revoked_txn[0]);
@@ -1513,8 +1400,14 @@ fn do_test_revoked_counterparty_commitment_balances(anchors: bool, confirm_htlc_
15131400
// somewhat so it may not always be exact.
15141401
fuzzy_assert_eq(claim_txn[0].weight().to_wu(), pinnable_weight);
15151402
fuzzy_assert_eq(claim_txn[1].weight().to_wu(), unpinnable_weight);
1516-
let pinnable_fee = chan_feerate * claim_txn[0].weight().to_wu() / 1000;
1517-
let unpinnable_fee = chan_feerate * claim_txn[1].weight().to_wu() / 1000;
1403+
let pinnable_fee = claim_txn[0].input.iter().map(|txin| {
1404+
assert_eq!(txin.previous_output.txid, as_revoked_txn[0].compute_txid());
1405+
as_revoked_txn[0].output[txin.previous_output.vout as usize].value.to_sat()
1406+
}).sum::<u64>() - claim_txn[0].output.iter().map(|txout| txout.value.to_sat()).sum::<u64>();
1407+
let unpinnable_fee = claim_txn[1].input.iter().map(|txin| {
1408+
assert_eq!(txin.previous_output.txid, as_revoked_txn[0].compute_txid());
1409+
as_revoked_txn[0].output[txin.previous_output.vout as usize].value.to_sat()
1410+
}).sum::<u64>() - claim_txn[1].output.iter().map(|txout| txout.value.to_sat()).sum::<u64>();
15181411

15191412
// The expected balances for the next three checks.
15201413
let to_remote_balance = Balance::ClaimableAwaitingConfirmations {
@@ -1543,7 +1436,6 @@ fn do_test_revoked_counterparty_commitment_balances(anchors: bool, confirm_htlc_
15431436
htlc_unclaimed_balance(5_000),
15441437
],
15451438
nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(),
1546-
5,
15471439
);
15481440

15491441
if confirm_htlc_spend_first {
@@ -1558,7 +1450,7 @@ fn do_test_revoked_counterparty_commitment_balances(anchors: bool, confirm_htlc_
15581450
}
15591451

15601452
let pinnable_claimed_balance = Balance::ClaimableAwaitingConfirmations {
1561-
amount_satoshis: 5_000 + 3_000 + 4_000 - pinnable_fee - if anchors { 3 } else { 2 },
1453+
amount_satoshis: 5_000 + 3_000 + 4_000 - pinnable_fee,
15621454
confirmation_height: largest_htlc_claimed_avail_height,
15631455
source: BalanceSource::CounterpartyForceClosed,
15641456
};
@@ -1576,7 +1468,6 @@ fn do_test_revoked_counterparty_commitment_balances(anchors: bool, confirm_htlc_
15761468
to_self_unclaimed_balance.clone(),
15771469
],
15781470
nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(),
1579-
5,
15801471
);
15811472
} else {
15821473
verify_claimable_balances(
@@ -1588,7 +1479,6 @@ fn do_test_revoked_counterparty_commitment_balances(anchors: bool, confirm_htlc_
15881479
htlc_unclaimed_balance(5_000),
15891480
],
15901481
nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(),
1591-
5,
15921482
);
15931483
}
15941484

@@ -1604,7 +1494,6 @@ fn do_test_revoked_counterparty_commitment_balances(anchors: bool, confirm_htlc_
16041494
unpinnable_claimed_balance.clone(),
16051495
],
16061496
nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances(),
1607-
5,
16081497
);
16091498

16101499
connect_blocks(&nodes[1], 3);

0 commit comments

Comments
 (0)