Skip to content

Commit ad77f72

Browse files
committed
Use new ChannelError in HTLC-removal msgs, filling out more handling
1 parent 662485a commit ad77f72

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/ln/channel.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,38 +1508,38 @@ 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, HandleError> {
1511+
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<[u8; 32]>, fail_reason: Option<HTLCFailReason>) -> Result<&HTLCSource, ChannelError> {
15121512
for htlc in self.pending_outbound_htlcs.iter_mut() {
15131513
if htlc.htlc_id == htlc_id {
15141514
match check_preimage {
15151515
None => {},
15161516
Some(payment_hash) =>
15171517
if payment_hash != htlc.payment_hash {
1518-
return Err(HandleError{err: "Remote tried to fulfill HTLC with an incorrect preimage", action: None});
1518+
return Err(ChannelError::Close("Remote tried to fulfill HTLC with an incorrect preimage"));
15191519
}
15201520
};
15211521
match htlc.state {
15221522
OutboundHTLCState::LocalAnnounced =>
1523-
return Err(HandleError{err: "Remote tried to fulfill HTLC before it had been committed", action: None}),
1523+
return Err(ChannelError::Close("Remote tried to fulfill HTLC before it had been committed")),
15241524
OutboundHTLCState::Committed => {
15251525
htlc.state = OutboundHTLCState::RemoteRemoved;
15261526
htlc.fail_reason = fail_reason;
15271527
},
15281528
OutboundHTLCState::AwaitingRemoteRevokeToRemove | OutboundHTLCState::AwaitingRemovedRemoteRevoke | OutboundHTLCState::RemoteRemoved =>
1529-
return Err(HandleError{err: "Remote tried to fulfill HTLC that they'd already fulfilled", action: None}),
1529+
return Err(ChannelError::Close("Remote tried to fulfill HTLC that they'd already fulfilled")),
15301530
}
15311531
return Ok(&htlc.source);
15321532
}
15331533
}
1534-
Err(HandleError{err: "Remote tried to fulfill/fail an HTLC we couldn't find", action: None})
1534+
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, HandleError> {
1537+
pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<&HTLCSource, ChannelError> {
15381538
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
1539-
return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", action: None});
1539+
return Err(ChannelError::Close("Got fulfill HTLC message when channel was not in an operational state"));
15401540
}
15411541
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
1542-
return Err(HandleError{err: "Peer sent update_fulfill_htlc when we needed a channel_reestablish", action: Some(msgs::ErrorAction::SendErrorMessage{msg: msgs::ErrorMessage{data: "Peer sent update_fulfill_htlc when we needed a channel_reestablish".to_string(), channel_id: msg.channel_id}})});
1542+
return Err(ChannelError::Close("Peer sent update_fulfill_htlc when we needed a channel_reestablish"));
15431543
}
15441544

15451545
let mut sha = Sha256::new();
@@ -1550,23 +1550,23 @@ 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, HandleError> {
1553+
pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<&HTLCSource, ChannelError> {
15541554
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
1555-
return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", action: None});
1555+
return Err(ChannelError::Close("Got fail HTLC message when channel was not in an operational state"));
15561556
}
15571557
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
1558-
return Err(HandleError{err: "Peer sent update_fail_htlc when we needed a channel_reestablish", action: Some(msgs::ErrorAction::SendErrorMessage{msg: msgs::ErrorMessage{data: "Peer sent update_fail_htlc when we needed a channel_reestablish".to_string(), channel_id: msg.channel_id}})});
1558+
return Err(ChannelError::Close("Peer sent update_fail_htlc when we needed a channel_reestablish"));
15591559
}
15601560

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, HandleError> {
1564+
pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<&HTLCSource, ChannelError> {
15651565
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
1566-
return Err(HandleError{err: "Got add HTLC message when channel was not in an operational state", action: None});
1566+
return Err(ChannelError::Close("Got fail malformed HTLC message when channel was not in an operational state"));
15671567
}
15681568
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
1569-
return Err(HandleError{err: "Peer sent update_fail_malformed_htlc when we needed a channel_reestablish", action: Some(msgs::ErrorAction::SendErrorMessage{msg: msgs::ErrorMessage{data: "Peer sent update_fail_malformed_htlc when we needed a channel_reestablish".to_string(), channel_id: msg.channel_id}})});
1569+
return Err(ChannelError::Close("Peer sent update_fail_malformed_htlc when we needed a channel_reestablish"));
15701570
}
15711571

15721572
self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))

src/ln/channelmanager.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,8 @@ impl ChannelManager {
17371737
//TODO: here and below MsgHandleErrInternal, #153 case
17381738
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
17391739
}
1740-
chan.update_fulfill_htlc(&msg).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?.clone()
1740+
chan.update_fulfill_htlc(&msg)
1741+
.map_err(|e| MsgHandleErrInternal::from_chan_maybe_close(e, msg.channel_id))?.clone()
17411742
},
17421743
None => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
17431744
};
@@ -1753,7 +1754,8 @@ impl ChannelManager {
17531754
//TODO: here and below MsgHandleErrInternal, #153 case
17541755
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
17551756
}
1756-
chan.update_fail_htlc(&msg, HTLCFailReason::ErrorPacket { err: msg.reason.clone() }).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))
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))
17571759
},
17581760
None => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
17591761
}?;
@@ -1825,7 +1827,11 @@ impl ChannelManager {
18251827
//TODO: here and below MsgHandleErrInternal, #153 case
18261828
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
18271829
}
1828-
chan.update_fail_malformed_htlc(&msg, HTLCFailReason::Reason { failure_code: msg.failure_code, data: Vec::new() }).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?;
1830+
if (msg.failure_code & 0x8000) != 0 {
1831+
return Err(MsgHandleErrInternal::send_err_msg_close_chan("Got update_fail_malformed_htlc with BADONION set", msg.channel_id));
1832+
}
1833+
chan.update_fail_malformed_htlc(&msg, HTLCFailReason::Reason { failure_code: msg.failure_code, data: Vec::new() })
1834+
.map_err(|e| MsgHandleErrInternal::from_chan_maybe_close(e, msg.channel_id))?;
18291835
Ok(())
18301836
},
18311837
None => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))

0 commit comments

Comments
 (0)