Skip to content

Commit 8e4c062

Browse files
committed
Document+check commitment_signed generation success on send_htlc
Because we don't have an HTLCState for update_add_htlc-generated-but-not-yet-commitment_signed to simplify the mess of HTLCState match arms, any time a Channel::send_htlc call returns Ok(Some(_)) we MUST call commitment_signed and it MUST return success (or close the channel). We mention this in the docs and panic if its not met in ChannelManager (which lets the fuzz tester check this).
1 parent eeefdaf commit 8e4c062

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/ln/channel.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,7 @@ impl Channel {
23342334
/// This returns an option instead of a pure UpdateAddHTLC as we may be in a state where we are
23352335
/// waiting on the remote peer to send us a revoke_and_ack during which time we cannot add new
23362336
/// HTLCs on the wire or we wouldn't be able to determine what they actually ACK'ed.
2337+
/// You MUST call send_commitment prior to any other calls on this Channel
23372338
pub fn send_htlc(&mut self, amount_msat: u64, payment_hash: [u8; 32], cltv_expiry: u32, onion_routing_packet: msgs::OnionPacket) -> Result<Option<msgs::UpdateAddHTLC>, HandleError> {
23382339
if (self.channel_state & (ChannelState::ChannelFunded as u32 | BOTH_SIDES_SHUTDOWN_MASK)) != (ChannelState::ChannelFunded as u32) {
23392340
return Err(HandleError{err: "Cannot send HTLC until channel is fully established and we haven't started shutting down", action: None});
@@ -2401,6 +2402,8 @@ impl Channel {
24012402
}
24022403

24032404
/// Creates a signed commitment transaction to send to the remote peer.
2405+
/// Always returns a Channel-failing HandleError::action if an immediately-preceding (read: the
2406+
/// last call to this Channel) send_htlc returned Ok(Some(_)) and there is an Err.
24042407
pub fn send_commitment(&mut self) -> Result<(msgs::CommitmentSigned, ChannelMonitor), HandleError> {
24052408
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
24062409
return Err(HandleError{err: "Cannot create commitment tx until channel is fully established", action: None});

src/ln/channelmanager.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,12 @@ impl ChannelManager {
11471147
if !add_htlc_msgs.is_empty() {
11481148
let (commitment_msg, monitor) = match forward_chan.send_commitment() {
11491149
Ok(res) => res,
1150-
Err(_e) => {
1150+
Err(e) => {
1151+
if let &Some(msgs::ErrorAction::DisconnectPeer{msg: Some(ref _err_msg)}) = &e.action {
1152+
} else if let &Some(msgs::ErrorAction::SendErrorMessage{msg: ref _err_msg}) = &e.action {
1153+
} else {
1154+
panic!("Stated return value requirements in send_commitment() were not met");
1155+
}
11511156
//TODO: Handle...this is bad!
11521157
continue;
11531158
},

0 commit comments

Comments
 (0)