Skip to content

Commit dbff601

Browse files
authored
Merge pull request #124 from TheBlueMatt/2018-08-htlc-fail-spec
Do not return UpdateFailHTLC updates until the HTLC is committed
2 parents f5b4759 + ab00e4c commit dbff601

File tree

7 files changed

+318
-296
lines changed

7 files changed

+318
-296
lines changed

fuzz/fuzz_targets/channel_target.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bitcoin::util::hash::Sha256dHash;
88
use bitcoin::network::serialize::{serialize, BitcoinHash};
99

1010
use lightning::ln::channel::{Channel, ChannelKeys};
11-
use lightning::ln::channelmanager::{HTLCFailReason, PendingForwardHTLCInfo};
11+
use lightning::ln::channelmanager::{HTLCFailReason, PendingHTLCStatus};
1212
use lightning::ln::msgs;
1313
use lightning::ln::msgs::{MsgDecodable, ErrorAction};
1414
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
@@ -256,7 +256,6 @@ pub fn do_test(data: &[u8]) {
256256
Ok(r) => Some(r),
257257
Err(e) => match e.action {
258258
None => return,
259-
Some(ErrorAction::UpdateFailHTLC {..}) => None,
260259
Some(ErrorAction::DisconnectPeer {..}) => return,
261260
Some(ErrorAction::IgnoreError) => None,
262261
Some(ErrorAction::SendErrorMessage {..}) => None,
@@ -280,7 +279,7 @@ pub fn do_test(data: &[u8]) {
280279
},
281280
2 => {
282281
let update_add_htlc = decode_msg!(msgs::UpdateAddHTLC, 32+8+8+32+4+4+33+20*65+32);
283-
test_err!(channel.update_add_htlc(&update_add_htlc, PendingForwardHTLCInfo::dummy()));
282+
test_err!(channel.update_add_htlc(&update_add_htlc, PendingHTLCStatus::dummy()));
284283
},
285284
3 => {
286285
let update_fulfill_htlc = decode_msg!(msgs::UpdateFulfillHTLC, 32 + 8 + 32);

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,7 @@ mod tests {
703703
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 for channel 3f00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 4
704704
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 133 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 5
705705
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 132 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 6
706-
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendHTLCs event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 with 1 HTLCs for channel 3f00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 7
707-
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFulfillHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with payment_preimage ff00888888888888888888888888888888888888888888888888888888888888 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 8
708-
706+
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 with 1 adds, 0 fulfills, 0 fails for channel 3f00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 7
707+
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with 0 adds, 1 fulfills, 0 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 8
709708
}
710709
}

src/ln/channel.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crypto::hkdf::{hkdf_extract,hkdf_expand};
1616
use ln::msgs;
1717
use ln::msgs::{ErrorAction, HandleError, MsgEncodable};
1818
use ln::channelmonitor::ChannelMonitor;
19-
use ln::channelmanager::{PendingForwardHTLCInfo, HTLCFailReason};
19+
use ln::channelmanager::{PendingHTLCStatus, PendingForwardHTLCInfo, HTLCFailReason};
2020
use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT};
2121
use ln::chan_utils;
2222
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
@@ -164,7 +164,7 @@ struct HTLCOutput { //TODO: Refactor into Outbound/InboundHTLCOutput (will save
164164
/// If we're in LocalRemoved*, set to true if we fulfilled the HTLC, and can claim money
165165
local_removed_fulfilled: bool,
166166
/// state pre-committed Remote* implies pending_forward_state, otherwise it must be None
167-
pending_forward_state: Option<PendingForwardHTLCInfo>,
167+
pending_forward_state: Option<PendingHTLCStatus>,
168168
}
169169

170170
impl HTLCOutput {
@@ -1381,7 +1381,7 @@ impl Channel {
13811381
(inbound_htlc_count, outbound_htlc_count, htlc_outbound_value_msat, htlc_inbound_value_msat)
13821382
}
13831383

1384-
pub fn update_add_htlc(&mut self, msg: &msgs::UpdateAddHTLC, pending_forward_state: PendingForwardHTLCInfo) -> Result<(), HandleError> {
1384+
pub fn update_add_htlc(&mut self, msg: &msgs::UpdateAddHTLC, pending_forward_state: PendingHTLCStatus) -> Result<(), HandleError> {
13851385
if (self.channel_state & (ChannelState::ChannelFunded as u32 | ChannelState::RemoteShutdownSent as u32)) != (ChannelState::ChannelFunded as u32) {
13861386
return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", action: None});
13871387
}
@@ -1670,6 +1670,7 @@ impl Channel {
16701670

16711671
let mut to_forward_infos = Vec::new();
16721672
let mut revoked_htlcs = Vec::new();
1673+
let mut failed_htlcs = Vec::new();
16731674
let mut require_commitment = false;
16741675
let mut value_to_self_msat_diff: i64 = 0;
16751676
// We really shouldnt have two passes here, but retain gives a non-mutable ref (Rust bug)
@@ -1693,8 +1694,17 @@ impl Channel {
16931694
htlc.state = HTLCState::AwaitingAnnouncedRemoteRevoke;
16941695
require_commitment = true;
16951696
} else if htlc.state == HTLCState::AwaitingAnnouncedRemoteRevoke {
1696-
htlc.state = HTLCState::Committed;
1697-
to_forward_infos.push(htlc.pending_forward_state.take().unwrap());
1697+
match htlc.pending_forward_state.take().unwrap() {
1698+
PendingHTLCStatus::Fail(fail_msg) => {
1699+
htlc.state = HTLCState::LocalRemoved;
1700+
require_commitment = true;
1701+
failed_htlcs.push(fail_msg);
1702+
},
1703+
PendingHTLCStatus::Forward(forward_info) => {
1704+
to_forward_infos.push(forward_info);
1705+
htlc.state = HTLCState::Committed;
1706+
}
1707+
}
16981708
} else if htlc.state == HTLCState::AwaitingRemoteRevokeToRemove {
16991709
htlc.state = HTLCState::AwaitingRemovedRemoteRevoke;
17001710
require_commitment = true;
@@ -1706,7 +1716,11 @@ impl Channel {
17061716
self.value_to_self_msat = (self.value_to_self_msat as i64 + value_to_self_msat_diff) as u64;
17071717

17081718
match self.free_holding_cell_htlcs()? {
1709-
Some(commitment_update) => {
1719+
Some(mut commitment_update) => {
1720+
commitment_update.0.update_fail_htlcs.reserve(failed_htlcs.len());
1721+
for fail_msg in failed_htlcs.drain(..) {
1722+
commitment_update.0.update_fail_htlcs.push(fail_msg);
1723+
}
17101724
Ok((Some(commitment_update.0), to_forward_infos, revoked_htlcs, commitment_update.1))
17111725
},
17121726
None => {
@@ -1715,7 +1729,7 @@ impl Channel {
17151729
Ok((Some(msgs::CommitmentUpdate {
17161730
update_add_htlcs: Vec::new(),
17171731
update_fulfill_htlcs: Vec::new(),
1718-
update_fail_htlcs: Vec::new(),
1732+
update_fail_htlcs: failed_htlcs,
17191733
commitment_signed
17201734
}), to_forward_infos, revoked_htlcs, monitor_update))
17211735
} else {

0 commit comments

Comments
 (0)