Skip to content

Commit c6b89a2

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 9fe38bc commit c6b89a2

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/ln/channelmanager.rs

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

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

src/ln/channelmonitor.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key>
255255
monitors.insert(key, monitor);
256256
Ok(())
257257
}
258+
258259
}
259260

260261
impl ManyChannelMonitor for SimpleManyChannelMonitor<OutPoint> {
@@ -1826,6 +1827,16 @@ impl ChannelMonitor {
18261827
pub(crate) fn is_resolving_output(&self, tx: &Transaction) -> Option<Vec<(Option<[u8;32]>, [u8;32])>> {
18271828
let mut hash_to_remove = Vec::new();
18281829
if tx.input.len() > 0 {
1830+
let commitment_number = 0xffffffffffff - ((((tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (tx.lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor);
1831+
if commitment_number >= self.get_min_seen_secret() {
1832+
if let Some(ref local_commitment_tx) = self.current_local_signed_commitment_tx {
1833+
for &(ref htlc_output, _, _) in &local_commitment_tx.htlc_outputs {
1834+
if htlc_output.offered {
1835+
hash_to_remove.push((None, htlc_output.payment_hash.clone()));
1836+
}
1837+
}
1838+
}
1839+
}
18291840
for input in &tx.input {
18301841
let mut payment_data = (None, None);
18311842
if let Some(ref current_local_signed_commitment_tx) = self.current_local_signed_commitment_tx {
@@ -1876,6 +1887,7 @@ impl ChannelMonitor {
18761887
}
18771888
None
18781889
}
1890+
18791891
}
18801892

18811893
const MAX_ALLOC_SIZE: usize = 64*1024;

0 commit comments

Comments
 (0)