Skip to content

Commit 2cf5eef

Browse files
author
Antoine Riard
committed
Implement fail backward in case of detection of revoked tx
Refactor block_connected to ease output resolution Add test_commitment_revoked_fail_backward Close lightningdevkit#137
1 parent 3259c8e commit 2cf5eef

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/ln/channelmanager.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6347,6 +6347,54 @@ mod tests {
63476347
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
63486348
}
63496349

6350+
#[test]
6351+
fn test_commitment_revoked_fail_backward() {
6352+
// Test that in case of a revoked commitment tx, we detect the resolution of output by justice tx
6353+
// and fail backward accordingly.
6354+
6355+
let nodes = create_network(3);
6356+
6357+
// Create some initial channels
6358+
create_announced_chan_between_nodes(&nodes, 0, 1);
6359+
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
6360+
6361+
// Rebalance the network a bit by relaying one payment through all the channels...
6362+
send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
6363+
send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
6364+
6365+
let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 3000000);
6366+
// Get the will-be-revoked local txn from nodes[2]
6367+
let revoked_local_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
6368+
// Revoke the old state
6369+
claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], payment_preimage);
6370+
6371+
route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 3000000);
6372+
6373+
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
6374+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
6375+
{
6376+
let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap();
6377+
assert_eq!(added_monitors.len(), 1);
6378+
added_monitors.clear();
6379+
}
6380+
let events = nodes[1].node.get_and_clear_pending_msg_events();
6381+
assert_eq!(events.len(), 2);
6382+
match events[0] {
6383+
MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
6384+
_ => panic!("Unexpected event"),
6385+
}
6386+
match events[1] {
6387+
MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
6388+
assert!(update_add_htlcs.is_empty());
6389+
assert!(!update_fail_htlcs.is_empty());
6390+
assert!(update_fulfill_htlcs.is_empty());
6391+
assert!(update_fail_malformed_htlcs.is_empty());
6392+
assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
6393+
},
6394+
_ => panic!("Unexpected event"),
6395+
}
6396+
}
6397+
63506398
#[test]
63516399
fn test_htlc_ignore_latest_remote_commitment() {
63526400
// Test that HTLC transactions spending the latest remote commitment transaction are simply

src/ln/channelmonitor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key>
274274
monitors.insert(key, monitor);
275275
Ok(())
276276
}
277+
277278
}
278279

279280
impl ManyChannelMonitor for SimpleManyChannelMonitor<OutPoint> {
@@ -1932,6 +1933,7 @@ impl ChannelMonitor {
19321933
}
19331934
htlc_updated
19341935
}
1936+
19351937
}
19361938

19371939
const MAX_ALLOC_SIZE: usize = 64*1024;

0 commit comments

Comments
 (0)