Skip to content

Commit 3002df0

Browse files
committed
Ignore HTLC txn we dont know how to claim instead of unwrap()ing
This fixes a crash introduced in 3e149b1 and introduces a test which will tickle the bug.
1 parent e323c13 commit 3002df0

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/ln/channelmanager.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3380,6 +3380,48 @@ mod tests {
33803380
}
33813381
}
33823382

3383+
#[test]
3384+
fn test_htlc_ignore_latest_remote_commitment() {
3385+
// Test that HTLC transactions spending the latest remote commitment transaction are simply
3386+
// ignored if we cannot claim them. This originally tickled an invalid unwrap().
3387+
let nodes = create_network(2);
3388+
create_announced_chan_between_nodes(&nodes, 0, 1);
3389+
3390+
route_payment(&nodes[0], &[&nodes[1]], 10000000);
3391+
nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id);
3392+
{
3393+
let events = nodes[0].node.get_and_clear_pending_events();
3394+
assert_eq!(events.len(), 1);
3395+
match events[0] {
3396+
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { contents: msgs::UnsignedChannelUpdate { flags, .. }, .. } } => {
3397+
assert_eq!(flags & 0b10, 0b10);
3398+
},
3399+
_ => panic!("Unexpected event"),
3400+
}
3401+
}
3402+
3403+
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
3404+
assert_eq!(node_txn.len(), 2);
3405+
3406+
let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3407+
nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0], &node_txn[1]], &[1; 2]);
3408+
3409+
{
3410+
let events = nodes[1].node.get_and_clear_pending_events();
3411+
assert_eq!(events.len(), 1);
3412+
match events[0] {
3413+
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { contents: msgs::UnsignedChannelUpdate { flags, .. }, .. } } => {
3414+
assert_eq!(flags & 0b10, 0b10);
3415+
},
3416+
_ => panic!("Unexpected event"),
3417+
}
3418+
}
3419+
3420+
// Duplicate the block_connected call since this may happen due to other listeners
3421+
// registering new transactions
3422+
nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0], &node_txn[1]], &[1; 2]);
3423+
}
3424+
33833425
#[test]
33843426
fn test_unconf_chan() {
33853427
// After creating a chan between nodes, we disconnect all blocks previously seen to force a channel close on nodes[0] side

src/ln/channelmonitor.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ impl ChannelMonitor {
12221222
};
12231223
}
12241224

1225-
let secret = self.get_secret(commitment_number).unwrap();
1225+
let secret = ignore_error!(self.get_secret(commitment_number));
12261226
let per_commitment_key = ignore_error!(SecretKey::from_slice(&self.secp_ctx, &secret));
12271227
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
12281228
let revocation_pubkey = match self.key_storage {
@@ -1269,7 +1269,6 @@ impl ChannelMonitor {
12691269
output: outputs,
12701270
};
12711271

1272-
12731272
let sighash_parts = bip143::SighashComponents::new(&spend_tx);
12741273

12751274
let sig = match self.key_storage {

0 commit comments

Comments
 (0)