Skip to content

Commit b4769df

Browse files
author
Antoine Riard
committed
Add is_resolving_output in ChannelMonitor to be called by ChannelManager
block_connected to claim funds backward Add caching of PreviousHopData even in case of non-terminal peer to be able to route backward from on-chain resolution and prune them when htlc is updated by other node
1 parent 826233e commit b4769df

File tree

4 files changed

+241
-26
lines changed

4 files changed

+241
-26
lines changed

src/ln/channel.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ impl Channel {
15081508

15091509
/// Removes an outbound HTLC which has been commitment_signed by the remote end
15101510
#[inline]
1511-
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<[u8; 32]>, fail_reason: Option<HTLCFailReason>) -> Result<&HTLCSource, ChannelError> {
1511+
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<[u8; 32]>, fail_reason: Option<HTLCFailReason>) -> Result<(&HTLCSource, [u8;32]), ChannelError> {
15121512
for htlc in self.pending_outbound_htlcs.iter_mut() {
15131513
if htlc.htlc_id == htlc_id {
15141514
match check_preimage {
@@ -1528,13 +1528,13 @@ impl Channel {
15281528
OutboundHTLCState::AwaitingRemoteRevokeToRemove | OutboundHTLCState::AwaitingRemovedRemoteRevoke | OutboundHTLCState::RemoteRemoved =>
15291529
return Err(ChannelError::Close("Remote tried to fulfill HTLC that they'd already fulfilled")),
15301530
}
1531-
return Ok(&htlc.source);
1531+
return Ok((&htlc.source, htlc.payment_hash));
15321532
}
15331533
}
15341534
Err(ChannelError::Close("Remote tried to fulfill/fail an HTLC we couldn't find"))
15351535
}
15361536

1537-
pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<&HTLCSource, ChannelError> {
1537+
pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<(&HTLCSource, [u8;32]), ChannelError> {
15381538
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
15391539
return Err(ChannelError::Close("Got fulfill HTLC message when channel was not in an operational state"));
15401540
}
@@ -1550,7 +1550,7 @@ impl Channel {
15501550
self.mark_outbound_htlc_removed(msg.htlc_id, Some(payment_hash), None)
15511551
}
15521552

1553-
pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<&HTLCSource, ChannelError> {
1553+
pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<(&HTLCSource, [u8;32]), ChannelError> {
15541554
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
15551555
return Err(ChannelError::Close("Got fail HTLC message when channel was not in an operational state"));
15561556
}
@@ -1561,7 +1561,7 @@ impl Channel {
15611561
self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))
15621562
}
15631563

1564-
pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<&HTLCSource, ChannelError> {
1564+
pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<(&HTLCSource, [u8;32]), ChannelError> {
15651565
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
15661566
return Err(ChannelError::Close("Got fail malformed HTLC message when channel was not in an operational state"));
15671567
}

src/ln/channelmanager.rs

Lines changed: 162 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -518,13 +518,7 @@ impl ChannelManager {
518518
for tx in local_txn {
519519
self.tx_broadcaster.broadcast_transaction(&tx);
520520
}
521-
//TODO: We need to have a way where outbound HTLC claims can result in us claiming the
522-
//now-on-chain HTLC output for ourselves (and, thereafter, passing the HTLC backwards).
523-
//TODO: We need to handle monitoring of pending offered HTLCs which just hit the chain and
524-
//may be claimed, resulting in us claiming the inbound HTLCs (and back-failing after
525-
//timeouts are hit and our claims confirm).
526-
//TODO: In any case, we need to make sure we remove any pending htlc tracking (via
527-
//fail_backwards or claim_funds) eventually for all HTLCs that were in the channel
521+
528522
}
529523

530524
/// Force closes a channel, immediately broadcasting the latest local commitment transaction to
@@ -1207,11 +1201,16 @@ impl ChannelManager {
12071201

12081202
let mut add_htlc_msgs = Vec::new();
12091203
for HTLCForwardInfo { prev_short_channel_id, prev_htlc_id, forward_info } in pending_forwards.drain(..) {
1210-
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
1204+
let prev_hop_data = HTLCPreviousHopData {
12111205
short_channel_id: prev_short_channel_id,
12121206
htlc_id: prev_htlc_id,
12131207
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
1214-
});
1208+
};
1209+
match channel_state.claimable_htlcs.entry(forward_info.payment_hash) {
1210+
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push(prev_hop_data.clone()),
1211+
hash_map::Entry::Vacant(entry) => { entry.insert(vec![prev_hop_data.clone()]); },
1212+
};
1213+
let htlc_source = HTLCSource::PreviousHopData(prev_hop_data);
12151214
match forward_chan.send_htlc(forward_info.amt_to_forward, forward_info.payment_hash, forward_info.outgoing_cltv_value, htlc_source.clone(), forward_info.onion_packet.unwrap()) {
12161215
Err(_e) => {
12171216
let chan_update = self.get_channel_update(forward_chan).unwrap();
@@ -1731,34 +1730,47 @@ impl ChannelManager {
17311730

17321731
fn internal_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) -> Result<(), MsgHandleErrInternal> {
17331732
let mut channel_state = self.channel_state.lock().unwrap();
1734-
let htlc_source = match channel_state.by_id.get_mut(&msg.channel_id) {
1733+
let (htlc_source, payment_hash) = match channel_state.by_id.get_mut(&msg.channel_id) {
17351734
Some(chan) => {
17361735
if chan.get_their_node_id() != *their_node_id {
17371736
//TODO: here and below MsgHandleErrInternal, #153 case
17381737
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
17391738
}
1740-
chan.update_fulfill_htlc(&msg)
1741-
.map_err(|e| MsgHandleErrInternal::from_chan_maybe_close(e, msg.channel_id))?.clone()
1739+
let (htlc_source, payment_hash) = chan.update_fulfill_htlc(&msg)
1740+
.map_err(|e| MsgHandleErrInternal::from_chan_maybe_close(e, msg.channel_id))?;
1741+
(htlc_source.clone(), payment_hash)
17421742
},
17431743
None => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
17441744
};
1745+
if let Some(sources) = channel_state.claimable_htlcs.get_mut(&payment_hash) {
1746+
if let HTLCSource::PreviousHopData(previous_hop_data) = htlc_source.clone() {
1747+
sources.retain(|ref source| !(source.short_channel_id == previous_hop_data.short_channel_id && source.htlc_id == previous_hop_data.htlc_id));
1748+
}
1749+
}
17451750
self.claim_funds_internal(channel_state, htlc_source, msg.payment_preimage.clone());
17461751
Ok(())
17471752
}
17481753

17491754
fn internal_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<Option<msgs::HTLCFailChannelUpdate>, MsgHandleErrInternal> {
1750-
let mut channel_state = self.channel_state.lock().unwrap();
1751-
let htlc_source = match channel_state.by_id.get_mut(&msg.channel_id) {
1755+
let mut channel_state_lock = self.channel_state.lock().unwrap();
1756+
let channel_state = channel_state_lock.borrow_parts();
1757+
let (htlc_source, payment_hash) = match channel_state.by_id.get_mut(&msg.channel_id) {
17521758
Some(chan) => {
17531759
if chan.get_their_node_id() != *their_node_id {
17541760
//TODO: here and below MsgHandleErrInternal, #153 case
17551761
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
17561762
}
1757-
chan.update_fail_htlc(&msg, HTLCFailReason::ErrorPacket { err: msg.reason.clone() })
1758-
.map_err(|e| MsgHandleErrInternal::from_chan_maybe_close(e, msg.channel_id))
1763+
let (htlc_source, payment_hash) = chan.update_fail_htlc(&msg, HTLCFailReason::ErrorPacket { err: msg.reason.clone() })
1764+
.map_err(|e| MsgHandleErrInternal::from_chan_maybe_close(e, msg.channel_id))?;
1765+
(htlc_source, payment_hash)
17591766
},
17601767
None => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
1761-
}?;
1768+
};
1769+
if let Some(sources) = channel_state.claimable_htlcs.get_mut(&payment_hash) {
1770+
if let HTLCSource::PreviousHopData(previous_hop_data) = htlc_source.clone() {
1771+
sources.retain(|ref source| !(source.short_channel_id == previous_hop_data.short_channel_id && source.htlc_id == previous_hop_data.htlc_id));
1772+
}
1773+
}
17621774

17631775
match htlc_source {
17641776
&HTLCSource::OutboundRoute { ref route, ref session_priv, .. } => {
@@ -1821,7 +1833,7 @@ impl ChannelManager {
18211833

18221834
fn internal_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) -> Result<(), MsgHandleErrInternal> {
18231835
let mut channel_state = self.channel_state.lock().unwrap();
1824-
match channel_state.by_id.get_mut(&msg.channel_id) {
1836+
let (htlc_source, payment_hash) = match channel_state.by_id.get_mut(&msg.channel_id) {
18251837
Some(chan) => {
18261838
if chan.get_their_node_id() != *their_node_id {
18271839
//TODO: here and below MsgHandleErrInternal, #153 case
@@ -1830,12 +1842,18 @@ impl ChannelManager {
18301842
if (msg.failure_code & 0x8000) != 0 {
18311843
return Err(MsgHandleErrInternal::send_err_msg_close_chan("Got update_fail_malformed_htlc with BADONION set", msg.channel_id));
18321844
}
1833-
chan.update_fail_malformed_htlc(&msg, HTLCFailReason::Reason { failure_code: msg.failure_code, data: Vec::new() })
1845+
let (htlc_source, payment_hash) = chan.update_fail_malformed_htlc(&msg, HTLCFailReason::Reason { failure_code: msg.failure_code, data: Vec::new() })
18341846
.map_err(|e| MsgHandleErrInternal::from_chan_maybe_close(e, msg.channel_id))?;
1835-
Ok(())
1847+
(htlc_source.clone(), payment_hash)
18361848
},
18371849
None => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
1850+
};
1851+
if let Some(sources) = channel_state.claimable_htlcs.get_mut(&payment_hash) {
1852+
if let HTLCSource::PreviousHopData(previous_hop_data) = htlc_source {
1853+
sources.retain(|ref source| !(source.short_channel_id == previous_hop_data.short_channel_id && source.htlc_id == previous_hop_data.htlc_id));
1854+
}
18381855
}
1856+
Ok(())
18391857
}
18401858

18411859
fn internal_commitment_signed(&self, their_node_id: &PublicKey, msg: &msgs::CommitmentSigned) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>), MsgHandleErrInternal> {
@@ -2040,6 +2058,7 @@ impl ChainListener for ChannelManager {
20402058
fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) {
20412059
let mut new_events = Vec::new();
20422060
let mut failed_channels = Vec::new();
2061+
let mut hash_to_remove = Vec::new();
20432062
{
20442063
let mut channel_lock = self.channel_state.lock().unwrap();
20452064
let channel_state = channel_lock.borrow_parts();
@@ -2102,10 +2121,33 @@ impl ChainListener for ChannelManager {
21022121
}
21032122
true
21042123
});
2124+
2125+
for tx in txn_matched {
2126+
if let Some(payments_data) = self.monitor.is_resolving_output(&tx) {
2127+
for payment_data in payments_data {
2128+
hash_to_remove.push((payment_data.0, payment_data.1));
2129+
}
2130+
}
2131+
}
21052132
}
21062133
for failure in failed_channels.drain(..) {
21072134
self.finish_force_close_channel(failure);
21082135
}
2136+
2137+
{
2138+
let mut channel_state = Some(self.channel_state.lock().unwrap());
2139+
for (preimage, hash) in hash_to_remove {
2140+
if let Some(preimage) = preimage {
2141+
if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap());}
2142+
if let Some(mut sources) = channel_state.as_mut().unwrap().claimable_htlcs.remove(&hash) {
2143+
for source in sources.drain(..) {
2144+
self.claim_funds_internal(channel_state.take().unwrap(), HTLCSource::PreviousHopData(source), preimage);
2145+
}
2146+
}
2147+
}
2148+
}
2149+
}
2150+
21092151
let mut pending_events = self.pending_events.lock().unwrap();
21102152
for funding_locked in new_events.drain(..) {
21112153
pending_events.push(funding_locked);
@@ -3817,7 +3859,7 @@ mod tests {
38173859
}
38183860

38193861
/// Tests that the given node has broadcast a claim transaction against the provided revoked
3820-
/// HTLC transaction.
3862+
/// HTLC transaction issued from a revoked commitment tx
38213863
fn test_revoked_htlc_claim_txn_broadcast(node: &Node, revoked_tx: Transaction) {
38223864
let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
38233865
assert_eq!(node_txn.len(), 1);
@@ -4205,6 +4247,105 @@ mod tests {
42054247
assert_eq!(nodes[1].node.list_channels().len(), 0);
42064248
}
42074249

4250+
#[test]
4251+
fn test_htlc_on_chain_success() {
4252+
// Test that in case of an unilateral close onchain, we detect the state of output thanks to
4253+
// ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is
4254+
// broadcasting the right event to other nodes in payment path.
4255+
// A --------------------> B ----------------------> C (preimage)
4256+
// A's commitment tx C's commitment tx
4257+
// \ \
4258+
// B's preimage tx C's HTLC Success tx
4259+
4260+
let nodes = create_network(3);
4261+
4262+
// Create some initial channels
4263+
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4264+
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
4265+
4266+
// Rebalance the network a bit by relaying one payment through all the channels...
4267+
send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
4268+
send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
4269+
4270+
let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
4271+
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
4272+
4273+
// Broadcast legit commitment tx from C on B's chain
4274+
// Broadcast HTLC Success transation by C on received output from C's commitment tx on B's chain
4275+
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
4276+
nodes[2].node.claim_funds(payment_preimage);
4277+
{
4278+
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
4279+
assert_eq!(added_monitors.len(), 1);
4280+
added_monitors.clear();
4281+
}
4282+
let events = nodes[2].node.get_and_clear_pending_events();
4283+
assert_eq!(events.len(), 1);
4284+
match events[0] {
4285+
Event::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
4286+
assert!(update_add_htlcs.is_empty());
4287+
assert!(update_fail_htlcs.is_empty());
4288+
assert!(!update_fulfill_htlcs.is_empty());
4289+
assert!(update_fail_malformed_htlcs.is_empty());
4290+
assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
4291+
},
4292+
_ => panic!("Unexpected event"),
4293+
};
4294+
nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
4295+
let events = nodes[2].node.get_and_clear_pending_events();
4296+
assert_eq!(events.len(), 1);
4297+
match events[0] {
4298+
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
4299+
_ => panic!("Unexpected event"),
4300+
}
4301+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4302+
4303+
// Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
4304+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn}, 1);
4305+
{
4306+
let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap();
4307+
assert_eq!(added_monitors.len(), 1);
4308+
added_monitors.clear();
4309+
}
4310+
let events = nodes[1].node.get_and_clear_pending_events();
4311+
assert_eq!(events.len(), 2);
4312+
match events[0] {
4313+
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
4314+
_ => panic!("Unexpected event"),
4315+
}
4316+
match events[1] {
4317+
Event::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
4318+
assert!(update_add_htlcs.is_empty());
4319+
assert!(update_fail_htlcs.is_empty());
4320+
assert!(!update_fulfill_htlcs.is_empty());
4321+
assert!(update_fail_malformed_htlcs.is_empty());
4322+
assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
4323+
},
4324+
_ => panic!("Unexpected event"),
4325+
};
4326+
4327+
// Broadcast legit commitment tx from A on B's chain
4328+
// Broadcast preimage tx by B on offered output from A commitment tx on A's chain
4329+
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
4330+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
4331+
let events = nodes[1].node.get_and_clear_pending_events();
4332+
assert_eq!(events.len(), 1);
4333+
match events[0] {
4334+
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
4335+
_ => panic!("Unexpected event"),
4336+
}
4337+
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4338+
4339+
// Verify that A's ChannelManager is able to extract preimage from preimage tx and pass it backward
4340+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
4341+
let events = nodes[0].node.get_and_clear_pending_events();
4342+
assert_eq!(events.len(), 1);
4343+
match events[0] {
4344+
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
4345+
_ => panic!("Unexpected event"),
4346+
}
4347+
}
4348+
42084349
#[test]
42094350
fn test_htlc_ignore_latest_remote_commitment() {
42104351
// Test that HTLC transactions spending the latest remote commitment transaction are simply

0 commit comments

Comments
 (0)