Skip to content

Commit f52f666

Browse files
author
Antoine Riard
committed
Add test of claiming HTLC-Timeout outputs based on a revoked commitment
tx
1 parent 83fd719 commit f52f666

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

src/ln/channelmanager.rs

+33-16
Original file line numberDiff line numberDiff line change
@@ -3108,9 +3108,11 @@ mod tests {
31083108

31093109
#[derive(PartialEq)]
31103110
enum HTLCType { NONE, TIMEOUT, SUCCESS }
3111-
fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction), commitment_tx: Option<Transaction>, has_htlc_tx: HTLCType) -> Vec<Transaction> {
3111+
#[derive(PartialEq)]
3112+
enum PenaltyType { NONE, COMMIT, HTLC }
3113+
fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction), commitment_tx: Option<Transaction>, revoked_tx: Option<Transaction>, has_htlc_tx: HTLCType, has_penalty_tx: PenaltyType) -> Vec<Transaction> {
31123114
let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
3113-
assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
3115+
assert!(node_txn.len() >= if has_htlc_tx == HTLCType::NONE { 0 } else { 1 } + if has_penalty_tx == PenaltyType::NONE { 0 } else { 1 });
31143116

31153117
let mut res = Vec::with_capacity(2);
31163118

@@ -3126,7 +3128,9 @@ mod tests {
31263128
}
31273129
}
31283130
}
3129-
assert_eq!(res.len(), 1);
3131+
if revoked_tx.is_none() {
3132+
assert_eq!(res.len(), 1);
3133+
}
31303134

31313135
if has_htlc_tx != HTLCType::NONE {
31323136
for tx in node_txn.iter() {
@@ -3145,6 +3149,21 @@ mod tests {
31453149
}
31463150
assert_eq!(res.len(), 2);
31473151
}
3152+
3153+
if has_penalty_tx == PenaltyType::HTLC {
3154+
if let Some(revoked_tx) = revoked_tx {
3155+
for tx in node_txn.iter() {
3156+
if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
3157+
let mut funding_tx_map = HashMap::new();
3158+
funding_tx_map.insert(revoked_tx.txid(), revoked_tx.clone());
3159+
tx.verify(&funding_tx_map).unwrap();
3160+
res.push(tx.clone());
3161+
break;
3162+
}
3163+
}
3164+
assert_eq!(res.len(), 1);
3165+
}
3166+
}
31483167
node_txn.clear();
31493168
res
31503169
}
@@ -3222,10 +3241,10 @@ mod tests {
32223241
// Simple case with no pending HTLCs:
32233242
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), true);
32243243
{
3225-
let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, HTLCType::NONE);
3244+
let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, None, HTLCType::NONE, PenaltyType::NONE);
32263245
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
32273246
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn.drain(..).next().unwrap()] }, 1);
3228-
test_txn_broadcast(&nodes[0], &chan_1, None, HTLCType::NONE);
3247+
test_txn_broadcast(&nodes[0], &chan_1, None, None, HTLCType::NONE, PenaltyType::NONE);
32293248
}
32303249
get_announce_close_broadcast_events(&nodes, 0, 1);
32313250
assert_eq!(nodes[0].node.list_channels().len(), 0);
@@ -3237,10 +3256,10 @@ mod tests {
32373256
// Simple case of one pending HTLC to HTLC-Timeout
32383257
nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), true);
32393258
{
3240-
let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
3259+
let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, None, HTLCType::TIMEOUT, PenaltyType::NONE);
32413260
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
32423261
nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn.drain(..).next().unwrap()] }, 1);
3243-
test_txn_broadcast(&nodes[2], &chan_2, None, HTLCType::NONE);
3262+
test_txn_broadcast(&nodes[2], &chan_2, None, None, HTLCType::NONE, PenaltyType::NONE);
32443263
}
32453264
get_announce_close_broadcast_events(&nodes, 1, 2);
32463265
assert_eq!(nodes[1].node.list_channels().len(), 0);
@@ -3274,7 +3293,7 @@ mod tests {
32743293
// HTLC-Timeout and a nodes[3] claim against it (+ its own announces)
32753294
nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id(), true);
32763295
{
3277-
let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT);
3296+
let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, None, HTLCType::TIMEOUT, PenaltyType::NONE);
32783297

32793298
// Claim the payment on nodes[3], giving it knowledge of the preimage
32803299
claim_funds!(nodes[3], nodes[2], payment_preimage_1);
@@ -3299,9 +3318,9 @@ mod tests {
32993318
nodes[3].chain_monitor.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]);
33003319
}
33013320

3302-
let node_txn = test_txn_broadcast(&nodes[3], &chan_4, None, HTLCType::TIMEOUT);
3321+
let node_txn = test_txn_broadcast(&nodes[3], &chan_4, None, None, HTLCType::TIMEOUT, PenaltyType::NONE);
33033322

3304-
// Claim the payment on nodes[3], giving it knowledge of the preimage
3323+
// Claim the payment on nodes[4], giving it knowledge of the preimage
33053324
claim_funds!(nodes[4], nodes[3], payment_preimage_2);
33063325

33073326
header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
@@ -3311,7 +3330,7 @@ mod tests {
33113330
nodes[4].chain_monitor.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]);
33123331
}
33133332

3314-
test_txn_broadcast(&nodes[4], &chan_4, None, HTLCType::SUCCESS);
3333+
test_txn_broadcast(&nodes[4], &chan_4, None, None, HTLCType::SUCCESS, PenaltyType::NONE);
33153334

33163335
header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
33173336
nodes[4].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, TEST_FINAL_CLTV - 5);
@@ -3346,15 +3365,13 @@ mod tests {
33463365
node_txn[0].verify(&funding_tx_map).unwrap();
33473366
node_txn.swap_remove(0);
33483367
}
3349-
test_txn_broadcast(&nodes[1], &chan_5, None, HTLCType::NONE);
3368+
test_txn_broadcast(&nodes[1], &chan_5, None, None, HTLCType::NONE, PenaltyType::NONE);
33503369

33513370
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
3352-
let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::TIMEOUT);
3371+
let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), None, HTLCType::TIMEOUT, PenaltyType::NONE);
33533372
header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
33543373
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[1].clone()] }, 1);
3355-
3356-
//TODO: At this point nodes[1] should claim the revoked HTLC-Timeout output, but that's
3357-
//not yet implemented in ChannelMonitor
3374+
test_txn_broadcast(&nodes[1], &chan_5, None, Some(node_txn[1].clone()), HTLCType::NONE, PenaltyType::HTLC);
33583375
}
33593376
get_announce_close_broadcast_events(&nodes, 0, 1);
33603377
assert_eq!(nodes[0].node.list_channels().len(), 0);

0 commit comments

Comments
 (0)