Skip to content

Commit af29adc

Browse files
committed
Macro-out checking a tx validly spends another (and add one more)
1 parent 0983193 commit af29adc

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

src/ln/channelmanager.rs

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,17 @@ mod tests {
26062606
(chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
26072607
}
26082608

2609+
macro_rules! check_spends {
2610+
($tx: expr, $spends_tx: expr) => {
2611+
{
2612+
let mut funding_tx_map = HashMap::new();
2613+
let spends_tx = $spends_tx;
2614+
funding_tx_map.insert(spends_tx.txid(), spends_tx);
2615+
$tx.verify(&funding_tx_map).unwrap();
2616+
}
2617+
}
2618+
}
2619+
26092620
fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &[u8; 32], funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate) {
26102621
let (node_a, broadcaster_a) = if close_inbound_first { (&inbound_node.node, &inbound_node.tx_broadcaster) } else { (&outbound_node.node, &outbound_node.tx_broadcaster) };
26112622
let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
@@ -2649,9 +2660,7 @@ mod tests {
26492660
tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
26502661
}
26512662
assert_eq!(tx_a, tx_b);
2652-
let mut funding_tx_map = HashMap::new();
2653-
funding_tx_map.insert(funding_tx.txid(), funding_tx);
2654-
tx_a.verify(&funding_tx_map).unwrap();
2663+
check_spends!(tx_a, funding_tx);
26552664

26562665
let events_2 = node_a.get_and_clear_pending_events();
26572666
assert_eq!(events_2.len(), 1);
@@ -3189,9 +3198,7 @@ mod tests {
31893198
let mut res = Vec::with_capacity(2);
31903199
node_txn.retain(|tx| {
31913200
if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
3192-
let mut funding_tx_map = HashMap::new();
3193-
funding_tx_map.insert(chan.3.txid(), chan.3.clone());
3194-
tx.verify(&funding_tx_map).unwrap();
3201+
check_spends!(tx, chan.3.clone());
31953202
if commitment_tx.is_none() {
31963203
res.push(tx.clone());
31973204
}
@@ -3207,9 +3214,7 @@ mod tests {
32073214
if has_htlc_tx != HTLCType::NONE {
32083215
node_txn.retain(|tx| {
32093216
if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
3210-
let mut funding_tx_map = HashMap::new();
3211-
funding_tx_map.insert(res[0].txid(), res[0].clone());
3212-
tx.verify(&funding_tx_map).unwrap();
3217+
check_spends!(tx, res[0].clone());
32133218
if has_htlc_tx == HTLCType::TIMEOUT {
32143219
assert!(tx.lock_time != 0);
32153220
} else {
@@ -3233,9 +3238,7 @@ mod tests {
32333238
assert_eq!(node_txn.len(), 1);
32343239
node_txn.retain(|tx| {
32353240
if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
3236-
let mut funding_tx_map = HashMap::new();
3237-
funding_tx_map.insert(revoked_tx.txid(), revoked_tx.clone());
3238-
tx.verify(&funding_tx_map).unwrap();
3241+
check_spends!(tx, revoked_tx.clone());
32393242
false
32403243
} else { true }
32413244
});
@@ -3251,10 +3254,7 @@ mod tests {
32513254

32523255
for tx in prev_txn {
32533256
if node_txn[0].input[0].previous_output.txid == tx.txid() {
3254-
let mut funding_tx_map = HashMap::new();
3255-
funding_tx_map.insert(tx.txid(), tx.clone());
3256-
node_txn[0].verify(&funding_tx_map).unwrap();
3257-
3257+
check_spends!(node_txn[0], tx.clone());
32583258
assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
32593259
assert_eq!(tx.input.len(), 1); // must spend a commitment tx
32603260

@@ -3441,9 +3441,7 @@ mod tests {
34413441
assert_eq!(node_txn.pop().unwrap(), node_txn[0]); // An outpoint registration will result in a 2nd block_connected
34423442
assert_eq!(node_txn[0].input.len(), 2); // We should claim the revoked output and the HTLC output
34433443

3444-
let mut funding_tx_map = HashMap::new();
3445-
funding_tx_map.insert(revoked_local_txn[0].txid(), revoked_local_txn[0].clone());
3446-
node_txn[0].verify(&funding_tx_map).unwrap();
3444+
check_spends!(node_txn[0], revoked_local_txn[0].clone());
34473445
node_txn.swap_remove(0);
34483446
}
34493447
test_txn_broadcast(&nodes[1], &chan_5, None, HTLCType::NONE);
@@ -3481,13 +3479,8 @@ mod tests {
34813479

34823480
assert_eq!(node_txn[0], node_txn[2]);
34833481

3484-
let mut revoked_tx_map = HashMap::new();
3485-
revoked_tx_map.insert(revoked_local_txn[0].txid(), revoked_local_txn[0].clone());
3486-
node_txn[0].verify(&revoked_tx_map).unwrap();
3487-
3488-
revoked_tx_map.clear();
3489-
revoked_tx_map.insert(chan_1.3.txid(), chan_1.3.clone());
3490-
node_txn[1].verify(&revoked_tx_map).unwrap();
3482+
check_spends!(node_txn[0], revoked_local_txn[0].clone());
3483+
check_spends!(node_txn[1], chan_1.3.clone());
34913484

34923485
// Inform nodes[0] that a watchtower cheated on its behalf, so it will force-close the chan
34933486
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
@@ -3516,6 +3509,7 @@ mod tests {
35163509
assert_eq!(revoked_local_txn[1].input.len(), 1);
35173510
assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
35183511
assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), 133); // HTLC-Timeout
3512+
check_spends!(revoked_local_txn[1], revoked_local_txn[0].clone());
35193513

35203514
//Revoke the old state
35213515
claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
@@ -3529,11 +3523,9 @@ mod tests {
35293523
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
35303524
assert_eq!(node_txn.len(), 4);
35313525

3532-
let mut revoked_tx_map = HashMap::new();
3533-
revoked_tx_map.insert(revoked_local_txn[0].txid(), revoked_local_txn[0].clone());
3534-
35353526
assert_eq!(node_txn[0].input.len(), 3); // Claim the revoked output + both revoked HTLC outputs
3536-
node_txn[0].verify(&revoked_tx_map).unwrap();
3527+
check_spends!(node_txn[0], revoked_local_txn[0].clone());
3528+
35373529
assert_eq!(node_txn[0], node_txn[3]); // justice tx is duplicated due to block re-scanning
35383530

35393531
let mut witness_lens = BTreeSet::new();
@@ -3617,10 +3609,8 @@ mod tests {
36173609
assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), 133); // revoked offered HTLC
36183610
assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), 138); // revoked received HTLC
36193611

3620-
let mut funding_tx_map = HashMap::new();
3621-
funding_tx_map.insert(chan_1.3.txid(), chan_1.3.clone());
3622-
node_txn[3].verify(&funding_tx_map).unwrap();
36233612
assert_eq!(node_txn[3].input.len(), 1);
3613+
check_spends!(node_txn[3], chan_1.3.clone());
36243614

36253615
assert_eq!(node_txn[4].input.len(), 1);
36263616
let witness_script = node_txn[4].input[0].witness.last().unwrap();
@@ -3790,9 +3780,8 @@ mod tests {
37903780
assert_eq!(node_txn[0].input[0].previous_output.txid, tx.txid());
37913781
assert_eq!(node_txn[0].lock_time, 0); // Must be an HTLC-Success
37923782
assert_eq!(node_txn[0].input[0].witness.len(), 5); // Must be an HTLC-Success
3793-
let mut funding_tx_map = HashMap::new();
3794-
funding_tx_map.insert(tx.txid(), tx);
3795-
node_txn[0].verify(&funding_tx_map).unwrap();
3783+
3784+
check_spends!(node_txn[0], tx);
37963785
}
37973786

37983787
#[test]

0 commit comments

Comments
 (0)