Skip to content

Commit 7d07498

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

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/ln/channelmanager.rs

+34-16
Original file line numberDiff line numberDiff line change
@@ -3108,9 +3108,12 @@ 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+
println!("node_txn {}", node_txn.len());
3116+
assert!(node_txn.len() >= if has_htlc_tx == HTLCType::NONE { 0 } else { 1 } + if has_penalty_tx == PenaltyType::NONE { 0 } else { 1 });
31143117

31153118
let mut res = Vec::with_capacity(2);
31163119

@@ -3126,7 +3129,9 @@ mod tests {
31263129
}
31273130
}
31283131
}
3129-
assert_eq!(res.len(), 1);
3132+
if revoked_tx.is_none() {
3133+
assert_eq!(res.len(), 1);
3134+
}
31303135

31313136
if has_htlc_tx != HTLCType::NONE {
31323137
for tx in node_txn.iter() {
@@ -3145,6 +3150,21 @@ mod tests {
31453150
}
31463151
assert_eq!(res.len(), 2);
31473152
}
3153+
3154+
if has_penalty_tx == PenaltyType::HTLC {
3155+
if let Some(revoked_tx) = revoked_tx {
3156+
for tx in node_txn.iter() {
3157+
if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
3158+
let mut funding_tx_map = HashMap::new();
3159+
funding_tx_map.insert(revoked_tx.txid(), revoked_tx.clone());
3160+
tx.verify(&funding_tx_map).unwrap();
3161+
res.push(tx.clone());
3162+
break;
3163+
}
3164+
}
3165+
assert_eq!(res.len(), 1);
3166+
}
3167+
}
31483168
node_txn.clear();
31493169
res
31503170
}
@@ -3222,10 +3242,10 @@ mod tests {
32223242
// Simple case with no pending HTLCs:
32233243
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), true);
32243244
{
3225-
let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, HTLCType::NONE);
3245+
let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, None, HTLCType::NONE, PenaltyType::NONE);
32263246
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
32273247
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);
3248+
test_txn_broadcast(&nodes[0], &chan_1, None, None, HTLCType::NONE, PenaltyType::NONE);
32293249
}
32303250
get_announce_close_broadcast_events(&nodes, 0, 1);
32313251
assert_eq!(nodes[0].node.list_channels().len(), 0);
@@ -3237,10 +3257,10 @@ mod tests {
32373257
// Simple case of one pending HTLC to HTLC-Timeout
32383258
nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), true);
32393259
{
3240-
let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
3260+
let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, None, HTLCType::TIMEOUT, PenaltyType::NONE);
32413261
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
32423262
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);
3263+
test_txn_broadcast(&nodes[2], &chan_2, None, None, HTLCType::NONE, PenaltyType::NONE);
32443264
}
32453265
get_announce_close_broadcast_events(&nodes, 1, 2);
32463266
assert_eq!(nodes[1].node.list_channels().len(), 0);
@@ -3274,7 +3294,7 @@ mod tests {
32743294
// HTLC-Timeout and a nodes[3] claim against it (+ its own announces)
32753295
nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id(), true);
32763296
{
3277-
let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT);
3297+
let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, None, HTLCType::TIMEOUT, PenaltyType::NONE);
32783298

32793299
// Claim the payment on nodes[3], giving it knowledge of the preimage
32803300
claim_funds!(nodes[3], nodes[2], payment_preimage_1);
@@ -3299,9 +3319,9 @@ mod tests {
32993319
nodes[3].chain_monitor.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]);
33003320
}
33013321

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

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

33073327
header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
@@ -3311,7 +3331,7 @@ mod tests {
33113331
nodes[4].chain_monitor.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]);
33123332
}
33133333

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

33163336
header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
33173337
nodes[4].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, TEST_FINAL_CLTV - 5);
@@ -3346,15 +3366,13 @@ mod tests {
33463366
node_txn[0].verify(&funding_tx_map).unwrap();
33473367
node_txn.swap_remove(0);
33483368
}
3349-
test_txn_broadcast(&nodes[1], &chan_5, None, HTLCType::NONE);
3369+
test_txn_broadcast(&nodes[1], &chan_5, None, None, HTLCType::NONE, PenaltyType::NONE);
33503370

33513371
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);
3372+
let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), None, HTLCType::TIMEOUT, PenaltyType::NONE);
33533373
header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
33543374
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
3375+
test_txn_broadcast(&nodes[1], &chan_5, None, Some(node_txn[1].clone()), HTLCType::NONE, PenaltyType::HTLC);
33583376
}
33593377
get_announce_close_broadcast_events(&nodes, 0, 1);
33603378
assert_eq!(nodes[0].node.list_channels().len(), 0);

0 commit comments

Comments
 (0)