Skip to content

Commit 6969fc9

Browse files
authored
Merge pull request #259 from TheBlueMatt/2018-11-256-redux
Add test_claim_on_remote_revoked_sizeable_push_msat
2 parents dae97a4 + ab9a98f commit 6969fc9

File tree

1 file changed

+109
-107
lines changed

1 file changed

+109
-107
lines changed

src/ln/channelmanager.rs

Lines changed: 109 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -7598,8 +7598,8 @@ mod tests {
75987598
} else { panic!("Unexpected result"); }
75997599
}
76007600

7601-
macro_rules! check_dynamic_output_p2wsh {
7602-
($node: expr) => {
7601+
macro_rules! check_spendable_outputs {
7602+
($node: expr, $der_idx: expr) => {
76037603
{
76047604
let events = $node.chan_monitor.simple_monitor.get_and_clear_pending_events();
76057605
let mut txn = Vec::new();
@@ -7608,6 +7608,33 @@ mod tests {
76087608
Event::SpendableOutputs { ref outputs } => {
76097609
for outp in outputs {
76107610
match *outp {
7611+
SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
7612+
let input = TxIn {
7613+
previous_output: outpoint.clone(),
7614+
script_sig: Script::new(),
7615+
sequence: 0,
7616+
witness: Vec::new(),
7617+
};
7618+
let outp = TxOut {
7619+
script_pubkey: Builder::new().push_opcode(opcodes::All::OP_RETURN).into_script(),
7620+
value: output.value,
7621+
};
7622+
let mut spend_tx = Transaction {
7623+
version: 2,
7624+
lock_time: 0,
7625+
input: vec![input],
7626+
output: vec![outp],
7627+
};
7628+
let secp_ctx = Secp256k1::new();
7629+
let remotepubkey = PublicKey::from_secret_key(&secp_ctx, &key);
7630+
let witness_script = Address::p2pkh(&remotepubkey, Network::Testnet).script_pubkey();
7631+
let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
7632+
let remotesig = secp_ctx.sign(&sighash, key);
7633+
spend_tx.input[0].witness.push(remotesig.serialize_der(&secp_ctx).to_vec());
7634+
spend_tx.input[0].witness[0].push(SigHashType::All as u8);
7635+
spend_tx.input[0].witness.push(remotepubkey.serialize().to_vec());
7636+
txn.push(spend_tx);
7637+
},
76117638
SpendableOutputDescriptor::DynamicOutputP2WSH { ref outpoint, ref key, ref witness_script, ref to_self_delay, ref output } => {
76127639
let input = TxIn {
76137640
previous_output: outpoint.clone(),
@@ -7634,29 +7661,8 @@ mod tests {
76347661
spend_tx.input[0].witness.push(witness_script.clone().into_bytes());
76357662
txn.push(spend_tx);
76367663
},
7637-
_ => panic!("Unexpected event"),
7638-
}
7639-
}
7640-
},
7641-
_ => panic!("Unexpected event"),
7642-
};
7643-
}
7644-
txn
7645-
}
7646-
}
7647-
}
7648-
7649-
macro_rules! check_dynamic_output_p2wpkh {
7650-
($node: expr) => {
7651-
{
7652-
let events = $node.chan_monitor.simple_monitor.get_and_clear_pending_events();
7653-
let mut txn = Vec::new();
7654-
for event in events {
7655-
match event {
7656-
Event::SpendableOutputs { ref outputs } => {
7657-
for outp in outputs {
7658-
match *outp {
7659-
SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
7664+
SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output } => {
7665+
let secp_ctx = Secp256k1::new();
76607666
let input = TxIn {
76617667
previous_output: outpoint.clone(),
76627668
script_sig: Script::new(),
@@ -7671,19 +7677,28 @@ mod tests {
76717677
version: 2,
76727678
lock_time: 0,
76737679
input: vec![input],
7674-
output: vec![outp],
7680+
output: vec![outp.clone()],
76757681
};
7676-
let secp_ctx = Secp256k1::new();
7677-
let remotepubkey = PublicKey::from_secret_key(&secp_ctx, &key);
7678-
let witness_script = Address::p2pkh(&remotepubkey, Network::Testnet).script_pubkey();
7682+
let secret = {
7683+
match ExtendedPrivKey::new_master(&secp_ctx, Network::Testnet, &$node.node_seed) {
7684+
Ok(master_key) => {
7685+
match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx($der_idx)) {
7686+
Ok(key) => key,
7687+
Err(_) => panic!("Your RNG is busted"),
7688+
}
7689+
}
7690+
Err(_) => panic!("Your rng is busted"),
7691+
}
7692+
};
7693+
let pubkey = ExtendedPubKey::from_private(&secp_ctx, &secret).public_key;
7694+
let witness_script = Address::p2pkh(&pubkey, Network::Testnet).script_pubkey();
76797695
let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
7680-
let remotesig = secp_ctx.sign(&sighash, key);
7681-
spend_tx.input[0].witness.push(remotesig.serialize_der(&secp_ctx).to_vec());
7696+
let sig = secp_ctx.sign(&sighash, &secret.secret_key);
7697+
spend_tx.input[0].witness.push(sig.serialize_der(&secp_ctx).to_vec());
76827698
spend_tx.input[0].witness[0].push(SigHashType::All as u8);
7683-
spend_tx.input[0].witness.push(remotepubkey.serialize().to_vec());
7699+
spend_tx.input[0].witness.push(pubkey.serialize().to_vec());
76847700
txn.push(spend_tx);
76857701
},
7686-
_ => panic!("Unexpected event"),
76877702
}
76887703
}
76897704
},
@@ -7695,57 +7710,6 @@ mod tests {
76957710
}
76967711
}
76977712

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

77667730
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
77677731
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 0);
7768-
let spend_txn = check_dynamic_output_p2wsh!(nodes[1]);
7732+
let spend_txn = check_spendable_outputs!(nodes[1], 1);
77697733
assert_eq!(spend_txn.len(), 1);
77707734
check_spends!(spend_txn[0], node_txn[0].clone());
77717735
}
77727736

77737737
#[test]
77747738
fn test_claim_on_remote_sizeable_push_msat() {
7775-
// Same test as precedent, just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
7739+
// Same test as previous, just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
77767740
// to_remote output is encumbered by a P2WPKH
77777741

77787742
let nodes = create_network(2);
@@ -7796,12 +7760,42 @@ mod tests {
77967760
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
77977761
_ => panic!("Unexpected event"),
77987762
}
7799-
let spend_txn = check_dynamic_output_p2wpkh!(nodes[1]);
7763+
let spend_txn = check_spendable_outputs!(nodes[1], 1);
78007764
assert_eq!(spend_txn.len(), 2);
78017765
assert_eq!(spend_txn[0], spend_txn[1]);
78027766
check_spends!(spend_txn[0], node_txn[0].clone());
78037767
}
78047768

7769+
#[test]
7770+
fn test_claim_on_remote_revoked_sizeable_push_msat() {
7771+
// Same test as previous, just test on remote revoked commitment tx, as per_commitment_point registration changes following you're funder/fundee and
7772+
// to_remote output is encumbered by a P2WPKH
7773+
7774+
let nodes = create_network(2);
7775+
7776+
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 59000000);
7777+
let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
7778+
let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
7779+
assert_eq!(revoked_local_txn[0].input.len(), 1);
7780+
assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
7781+
7782+
claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
7783+
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
7784+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
7785+
let events = nodes[1].node.get_and_clear_pending_msg_events();
7786+
match events[0] {
7787+
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
7788+
_ => panic!("Unexpected event"),
7789+
}
7790+
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7791+
let spend_txn = check_spendable_outputs!(nodes[1], 1);
7792+
assert_eq!(spend_txn.len(), 4);
7793+
assert_eq!(spend_txn[0], spend_txn[2]); // to_remote output on revoked remote commitment_tx
7794+
check_spends!(spend_txn[0], revoked_local_txn[0].clone());
7795+
assert_eq!(spend_txn[1], spend_txn[3]); // to_local output on local commitment tx
7796+
check_spends!(spend_txn[1], node_txn[0].clone());
7797+
}
7798+
78057799
#[test]
78067800
fn test_static_spendable_outputs_preimage_tx() {
78077801
let nodes = create_network(2);
@@ -7837,9 +7831,10 @@ mod tests {
78377831
assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 133);
78387832
check_spends!(node_txn[1], chan_1.3.clone());
78397833

7840-
let events = nodes[1].chan_monitor.simple_monitor.get_and_clear_pending_events();
7841-
let spend_tx = check_static_output!(events, nodes, 0, 0, 1, 1);
7842-
check_spends!(spend_tx, node_txn[0].clone());
7834+
let spend_txn = check_spendable_outputs!(nodes[1], 1); // , 0, 0, 1, 1);
7835+
assert_eq!(spend_txn.len(), 2);
7836+
assert_eq!(spend_txn[0], spend_txn[1]);
7837+
check_spends!(spend_txn[0], node_txn[0].clone());
78437838
}
78447839

78457840
#[test]
@@ -7869,9 +7864,10 @@ mod tests {
78697864
assert_eq!(node_txn[0].input.len(), 2);
78707865
check_spends!(node_txn[0], revoked_local_txn[0].clone());
78717866

7872-
let events = nodes[1].chan_monitor.simple_monitor.get_and_clear_pending_events();
7873-
let spend_tx = check_static_output!(events, nodes, 0, 0, 1, 1);
7874-
check_spends!(spend_tx, node_txn[0].clone());
7867+
let spend_txn = check_spendable_outputs!(nodes[1], 1);
7868+
assert_eq!(spend_txn.len(), 2);
7869+
assert_eq!(spend_txn[0], spend_txn[1]);
7870+
check_spends!(spend_txn[0], node_txn[0].clone());
78757871
}
78767872

78777873
#[test]
@@ -7915,10 +7911,12 @@ mod tests {
79157911
assert_eq!(node_txn[3].input.len(), 1);
79167912
check_spends!(node_txn[3], revoked_htlc_txn[0].clone());
79177913

7918-
let events = nodes[1].chan_monitor.simple_monitor.get_and_clear_pending_events();
79197914
// Check B's ChannelMonitor was able to generate the right spendable output descriptor
7920-
let spend_tx = check_static_output!(events, nodes, 1, 1, 1, 1);
7921-
check_spends!(spend_tx, node_txn[3].clone());
7915+
let spend_txn = check_spendable_outputs!(nodes[1], 1);
7916+
assert_eq!(spend_txn.len(), 3);
7917+
assert_eq!(spend_txn[0], spend_txn[1]);
7918+
check_spends!(spend_txn[0], node_txn[0].clone());
7919+
check_spends!(spend_txn[2], node_txn[3].clone());
79227920
}
79237921

79247922
#[test]
@@ -7963,10 +7961,14 @@ mod tests {
79637961
assert_eq!(node_txn[3].input.len(), 1);
79647962
check_spends!(node_txn[3], revoked_htlc_txn[0].clone());
79657963

7966-
let events = nodes[0].chan_monitor.simple_monitor.get_and_clear_pending_events();
79677964
// Check A's ChannelMonitor was able to generate the right spendable output descriptor
7968-
let spend_tx = check_static_output!(events, nodes, 1, 2, 1, 0);
7969-
check_spends!(spend_tx, node_txn[3].clone());
7965+
let spend_txn = check_spendable_outputs!(nodes[0], 1);
7966+
assert_eq!(spend_txn.len(), 5);
7967+
assert_eq!(spend_txn[0], spend_txn[2]);
7968+
assert_eq!(spend_txn[1], spend_txn[3]);
7969+
check_spends!(spend_txn[0], revoked_local_txn[0].clone()); // spending to_remote output from revoked local tx
7970+
check_spends!(spend_txn[1], node_txn[2].clone()); // spending justice tx output from revoked local tx htlc received output
7971+
check_spends!(spend_txn[4], node_txn[3].clone()); // spending justice tx output on htlc success tx
79707972
}
79717973

79727974
#[test]
@@ -8001,7 +8003,7 @@ mod tests {
80018003
check_spends!(node_txn[0], local_txn[0].clone());
80028004

80038005
// Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
8004-
let spend_txn = check_dynamic_output_p2wsh!(nodes[1]);
8006+
let spend_txn = check_spendable_outputs!(nodes[1], 1);
80058007
assert_eq!(spend_txn.len(), 1);
80068008
check_spends!(spend_txn[0], node_txn[0].clone());
80078009
}
@@ -8032,7 +8034,7 @@ mod tests {
80328034
check_spends!(node_txn[0], local_txn[0].clone());
80338035

80348036
// Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
8035-
let spend_txn = check_dynamic_output_p2wsh!(nodes[0]);
8037+
let spend_txn = check_spendable_outputs!(nodes[0], 1);
80368038
assert_eq!(spend_txn.len(), 4);
80378039
assert_eq!(spend_txn[0], spend_txn[2]);
80388040
assert_eq!(spend_txn[1], spend_txn[3]);
@@ -8051,13 +8053,13 @@ mod tests {
80518053

80528054
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
80538055
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![closing_tx.clone()] }, 1);
8054-
let events = nodes[0].chan_monitor.simple_monitor.get_and_clear_pending_events();
8055-
let spend_tx = check_static_output!(events, nodes, 0, 0, 2, 0);
8056-
check_spends!(spend_tx, closing_tx.clone());
8056+
let spend_txn = check_spendable_outputs!(nodes[0], 2);
8057+
assert_eq!(spend_txn.len(), 1);
8058+
check_spends!(spend_txn[0], closing_tx.clone());
80578059

80588060
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![closing_tx.clone()] }, 1);
8059-
let events = nodes[1].chan_monitor.simple_monitor.get_and_clear_pending_events();
8060-
let spend_tx = check_static_output!(events, nodes, 0, 0, 2, 1);
8061-
check_spends!(spend_tx, closing_tx);
8061+
let spend_txn = check_spendable_outputs!(nodes[1], 2);
8062+
assert_eq!(spend_txn.len(), 1);
8063+
check_spends!(spend_txn[0], closing_tx);
80628064
}
80638065
}

0 commit comments

Comments
 (0)