Skip to content

Commit 546bd37

Browse files
Antoine RiardTheBlueMatt
authored andcommitted
Refactor check spendable outputs macros into one
In consequence, harden spendable outputs tests Fix vocabulary abuse
1 parent 7687e02 commit 546bd37

File tree

1 file changed

+79
-107
lines changed

1 file changed

+79
-107
lines changed

src/ln/channelmanager.rs

Lines changed: 79 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -7588,8 +7588,8 @@ mod tests {
75887588
} else { panic!("Unexpected result"); }
75897589
}
75907590

7591-
macro_rules! check_dynamic_output_p2wsh {
7592-
($node: expr) => {
7591+
macro_rules! check_spendable_outputs {
7592+
($node: expr, $der_idx: expr) => {
75937593
{
75947594
let events = $node.chan_monitor.simple_monitor.get_and_clear_pending_events();
75957595
let mut txn = Vec::new();
@@ -7598,6 +7598,33 @@ mod tests {
75987598
Event::SpendableOutputs { ref outputs } => {
75997599
for outp in outputs {
76007600
match *outp {
7601+
SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
7602+
let input = TxIn {
7603+
previous_output: outpoint.clone(),
7604+
script_sig: Script::new(),
7605+
sequence: 0,
7606+
witness: Vec::new(),
7607+
};
7608+
let outp = TxOut {
7609+
script_pubkey: Builder::new().push_opcode(opcodes::All::OP_RETURN).into_script(),
7610+
value: output.value,
7611+
};
7612+
let mut spend_tx = Transaction {
7613+
version: 2,
7614+
lock_time: 0,
7615+
input: vec![input],
7616+
output: vec![outp],
7617+
};
7618+
let secp_ctx = Secp256k1::new();
7619+
let remotepubkey = PublicKey::from_secret_key(&secp_ctx, &key);
7620+
let witness_script = Address::p2pkh(&remotepubkey, Network::Testnet).script_pubkey();
7621+
let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
7622+
let remotesig = secp_ctx.sign(&sighash, key);
7623+
spend_tx.input[0].witness.push(remotesig.serialize_der(&secp_ctx).to_vec());
7624+
spend_tx.input[0].witness[0].push(SigHashType::All as u8);
7625+
spend_tx.input[0].witness.push(remotepubkey.serialize().to_vec());
7626+
txn.push(spend_tx);
7627+
},
76017628
SpendableOutputDescriptor::DynamicOutputP2WSH { ref outpoint, ref key, ref witness_script, ref to_self_delay, ref output } => {
76027629
let input = TxIn {
76037630
previous_output: outpoint.clone(),
@@ -7624,29 +7651,8 @@ mod tests {
76247651
spend_tx.input[0].witness.push(witness_script.clone().into_bytes());
76257652
txn.push(spend_tx);
76267653
},
7627-
_ => panic!("Unexpected event"),
7628-
}
7629-
}
7630-
},
7631-
_ => panic!("Unexpected event"),
7632-
};
7633-
}
7634-
txn
7635-
}
7636-
}
7637-
}
7638-
7639-
macro_rules! check_dynamic_output_p2wpkh {
7640-
($node: expr) => {
7641-
{
7642-
let events = $node.chan_monitor.simple_monitor.get_and_clear_pending_events();
7643-
let mut txn = Vec::new();
7644-
for event in events {
7645-
match event {
7646-
Event::SpendableOutputs { ref outputs } => {
7647-
for outp in outputs {
7648-
match *outp {
7649-
SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
7654+
SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output } => {
7655+
let secp_ctx = Secp256k1::new();
76507656
let input = TxIn {
76517657
previous_output: outpoint.clone(),
76527658
script_sig: Script::new(),
@@ -7661,19 +7667,28 @@ mod tests {
76617667
version: 2,
76627668
lock_time: 0,
76637669
input: vec![input],
7664-
output: vec![outp],
7670+
output: vec![outp.clone()],
76657671
};
7666-
let secp_ctx = Secp256k1::new();
7667-
let remotepubkey = PublicKey::from_secret_key(&secp_ctx, &key);
7668-
let witness_script = Address::p2pkh(&remotepubkey, Network::Testnet).script_pubkey();
7672+
let secret = {
7673+
match ExtendedPrivKey::new_master(&secp_ctx, Network::Testnet, &$node.node_seed) {
7674+
Ok(master_key) => {
7675+
match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx($der_idx)) {
7676+
Ok(key) => key,
7677+
Err(_) => panic!("Your RNG is busted"),
7678+
}
7679+
}
7680+
Err(_) => panic!("Your rng is busted"),
7681+
}
7682+
};
7683+
let pubkey = ExtendedPubKey::from_private(&secp_ctx, &secret).public_key;
7684+
let witness_script = Address::p2pkh(&pubkey, Network::Testnet).script_pubkey();
76697685
let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
7670-
let remotesig = secp_ctx.sign(&sighash, key);
7671-
spend_tx.input[0].witness.push(remotesig.serialize_der(&secp_ctx).to_vec());
7686+
let sig = secp_ctx.sign(&sighash, &secret.secret_key);
7687+
spend_tx.input[0].witness.push(sig.serialize_der(&secp_ctx).to_vec());
76727688
spend_tx.input[0].witness[0].push(SigHashType::All as u8);
7673-
spend_tx.input[0].witness.push(remotepubkey.serialize().to_vec());
7689+
spend_tx.input[0].witness.push(pubkey.serialize().to_vec());
76747690
txn.push(spend_tx);
76757691
},
7676-
_ => panic!("Unexpected event"),
76777692
}
76787693
}
76797694
},
@@ -7685,57 +7700,6 @@ mod tests {
76857700
}
76867701
}
76877702

7688-
macro_rules! check_static_output {
7689-
($event: expr, $node: expr, $event_idx: expr, $output_idx: expr, $der_idx: expr, $idx_node: expr) => {
7690-
match $event[$event_idx] {
7691-
Event::SpendableOutputs { ref outputs } => {
7692-
match outputs[$output_idx] {
7693-
SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output } => {
7694-
let secp_ctx = Secp256k1::new();
7695-
let input = TxIn {
7696-
previous_output: outpoint.clone(),
7697-
script_sig: Script::new(),
7698-
sequence: 0,
7699-
witness: Vec::new(),
7700-
};
7701-
let outp = TxOut {
7702-
script_pubkey: Builder::new().push_opcode(opcodes::All::OP_RETURN).into_script(),
7703-
value: output.value,
7704-
};
7705-
let mut spend_tx = Transaction {
7706-
version: 2,
7707-
lock_time: 0,
7708-
input: vec![input],
7709-
output: vec![outp.clone()],
7710-
};
7711-
let secret = {
7712-
match ExtendedPrivKey::new_master(&secp_ctx, Network::Testnet, &$node[$idx_node].node_seed) {
7713-
Ok(master_key) => {
7714-
match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx($der_idx)) {
7715-
Ok(key) => key,
7716-
Err(_) => panic!("Your RNG is busted"),
7717-
}
7718-
}
7719-
Err(_) => panic!("Your rng is busted"),
7720-
}
7721-
};
7722-
let pubkey = ExtendedPubKey::from_private(&secp_ctx, &secret).public_key;
7723-
let witness_script = Address::p2pkh(&pubkey, Network::Testnet).script_pubkey();
7724-
let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
7725-
let sig = secp_ctx.sign(&sighash, &secret.secret_key);
7726-
spend_tx.input[0].witness.push(sig.serialize_der(&secp_ctx).to_vec());
7727-
spend_tx.input[0].witness[0].push(SigHashType::All as u8);
7728-
spend_tx.input[0].witness.push(pubkey.serialize().to_vec());
7729-
spend_tx
7730-
},
7731-
_ => panic!("Unexpected event !"),
7732-
}
7733-
},
7734-
_ => panic!("Unexpected event !"),
7735-
};
7736-
}
7737-
}
7738-
77397703
#[test]
77407704
fn test_claim_sizeable_push_msat() {
77417705
// Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
@@ -7755,14 +7719,14 @@ mod tests {
77557719

77567720
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
77577721
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 0);
7758-
let spend_txn = check_dynamic_output_p2wsh!(nodes[1]);
7722+
let spend_txn = check_spendable_outputs!(nodes[1], 1);
77597723
assert_eq!(spend_txn.len(), 1);
77607724
check_spends!(spend_txn[0], node_txn[0].clone());
77617725
}
77627726

77637727
#[test]
77647728
fn test_claim_on_remote_sizeable_push_msat() {
7765-
// Same test as precedent, just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
7729+
// Same test as previous, just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
77667730
// to_remote output is encumbered by a P2WPKH
77677731

77687732
let nodes = create_network(2);
@@ -7786,7 +7750,7 @@ mod tests {
77867750
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
77877751
_ => panic!("Unexpected event"),
77887752
}
7789-
let spend_txn = check_dynamic_output_p2wpkh!(nodes[1]);
7753+
let spend_txn = check_spendable_outputs!(nodes[1], 1);
77907754
assert_eq!(spend_txn.len(), 2);
77917755
assert_eq!(spend_txn[0], spend_txn[1]);
77927756
check_spends!(spend_txn[0], node_txn[0].clone());
@@ -7827,9 +7791,10 @@ mod tests {
78277791
assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 133);
78287792
check_spends!(node_txn[1], chan_1.3.clone());
78297793

7830-
let events = nodes[1].chan_monitor.simple_monitor.get_and_clear_pending_events();
7831-
let spend_tx = check_static_output!(events, nodes, 0, 0, 1, 1);
7832-
check_spends!(spend_tx, node_txn[0].clone());
7794+
let spend_txn = check_spendable_outputs!(nodes[1], 1); // , 0, 0, 1, 1);
7795+
assert_eq!(spend_txn.len(), 2);
7796+
assert_eq!(spend_txn[0], spend_txn[1]);
7797+
check_spends!(spend_txn[0], node_txn[0].clone());
78337798
}
78347799

78357800
#[test]
@@ -7859,9 +7824,10 @@ mod tests {
78597824
assert_eq!(node_txn[0].input.len(), 2);
78607825
check_spends!(node_txn[0], revoked_local_txn[0].clone());
78617826

7862-
let events = nodes[1].chan_monitor.simple_monitor.get_and_clear_pending_events();
7863-
let spend_tx = check_static_output!(events, nodes, 0, 0, 1, 1);
7864-
check_spends!(spend_tx, node_txn[0].clone());
7827+
let spend_txn = check_spendable_outputs!(nodes[1], 1);
7828+
assert_eq!(spend_txn.len(), 2);
7829+
assert_eq!(spend_txn[0], spend_txn[1]);
7830+
check_spends!(spend_txn[0], node_txn[0].clone());
78657831
}
78667832

78677833
#[test]
@@ -7905,10 +7871,12 @@ mod tests {
79057871
assert_eq!(node_txn[3].input.len(), 1);
79067872
check_spends!(node_txn[3], revoked_htlc_txn[0].clone());
79077873

7908-
let events = nodes[1].chan_monitor.simple_monitor.get_and_clear_pending_events();
79097874
// Check B's ChannelMonitor was able to generate the right spendable output descriptor
7910-
let spend_tx = check_static_output!(events, nodes, 1, 1, 1, 1);
7911-
check_spends!(spend_tx, node_txn[3].clone());
7875+
let spend_txn = check_spendable_outputs!(nodes[1], 1);
7876+
assert_eq!(spend_txn.len(), 3);
7877+
assert_eq!(spend_txn[0], spend_txn[1]);
7878+
check_spends!(spend_txn[0], node_txn[0].clone());
7879+
check_spends!(spend_txn[2], node_txn[3].clone());
79127880
}
79137881

79147882
#[test]
@@ -7953,10 +7921,14 @@ mod tests {
79537921
assert_eq!(node_txn[3].input.len(), 1);
79547922
check_spends!(node_txn[3], revoked_htlc_txn[0].clone());
79557923

7956-
let events = nodes[0].chan_monitor.simple_monitor.get_and_clear_pending_events();
79577924
// Check A's ChannelMonitor was able to generate the right spendable output descriptor
7958-
let spend_tx = check_static_output!(events, nodes, 1, 2, 1, 0);
7959-
check_spends!(spend_tx, node_txn[3].clone());
7925+
let spend_txn = check_spendable_outputs!(nodes[0], 1);
7926+
assert_eq!(spend_txn.len(), 5);
7927+
assert_eq!(spend_txn[0], spend_txn[2]);
7928+
assert_eq!(spend_txn[1], spend_txn[3]);
7929+
check_spends!(spend_txn[0], revoked_local_txn[0].clone()); // spending to_remote output from revoked local tx
7930+
check_spends!(spend_txn[1], node_txn[2].clone()); // spending justice tx output from revoked local tx htlc received output
7931+
check_spends!(spend_txn[4], node_txn[3].clone()); // spending justice tx output on htlc success tx
79607932
}
79617933

79627934
#[test]
@@ -7991,7 +7963,7 @@ mod tests {
79917963
check_spends!(node_txn[0], local_txn[0].clone());
79927964

79937965
// Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
7994-
let spend_txn = check_dynamic_output_p2wsh!(nodes[1]);
7966+
let spend_txn = check_spendable_outputs!(nodes[1], 1);
79957967
assert_eq!(spend_txn.len(), 1);
79967968
check_spends!(spend_txn[0], node_txn[0].clone());
79977969
}
@@ -8022,7 +7994,7 @@ mod tests {
80227994
check_spends!(node_txn[0], local_txn[0].clone());
80237995

80247996
// Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
8025-
let spend_txn = check_dynamic_output_p2wsh!(nodes[0]);
7997+
let spend_txn = check_spendable_outputs!(nodes[0], 1);
80267998
assert_eq!(spend_txn.len(), 4);
80277999
assert_eq!(spend_txn[0], spend_txn[2]);
80288000
assert_eq!(spend_txn[1], spend_txn[3]);
@@ -8041,13 +8013,13 @@ mod tests {
80418013

80428014
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
80438015
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![closing_tx.clone()] }, 1);
8044-
let events = nodes[0].chan_monitor.simple_monitor.get_and_clear_pending_events();
8045-
let spend_tx = check_static_output!(events, nodes, 0, 0, 2, 0);
8046-
check_spends!(spend_tx, closing_tx.clone());
8016+
let spend_txn = check_spendable_outputs!(nodes[0], 2);
8017+
assert_eq!(spend_txn.len(), 1);
8018+
check_spends!(spend_txn[0], closing_tx.clone());
80478019

80488020
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![closing_tx.clone()] }, 1);
8049-
let events = nodes[1].chan_monitor.simple_monitor.get_and_clear_pending_events();
8050-
let spend_tx = check_static_output!(events, nodes, 0, 0, 2, 1);
8051-
check_spends!(spend_tx, closing_tx);
8021+
let spend_txn = check_spendable_outputs!(nodes[1], 2);
8022+
assert_eq!(spend_txn.len(), 1);
8023+
check_spends!(spend_txn[0], closing_tx);
80528024
}
80538025
}

0 commit comments

Comments
 (0)