Skip to content

Commit bc1970c

Browse files
committed
Move fail_reason into OutboundHTLCState states
This should probably have happened when we moved most state into the state enums themselves, but specifically forcing awareness of the removed/not removed state would have prevented me from introducing a bug in the first version of an upcoming reserve-value patch.
1 parent fef2eba commit bc1970c

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

src/ln/channel.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,19 @@ enum OutboundHTLCState {
106106
Committed,
107107
/// Remote removed this (outbound) HTLC. We're waiting on their commitment_signed to finalize
108108
/// the change (though they'll need to revoke before we fail the payment).
109-
RemoteRemoved,
109+
RemoteRemoved(Option<HTLCFailReason>),
110110
/// Remote removed this and sent a commitment_signed (implying we've revoke_and_ack'ed it), but
111111
/// the remote side hasn't yet revoked their previous state, which we need them to do before we
112112
/// can do any backwards failing. Implies AwaitingRemoteRevoke.
113113
/// We also have not yet removed this HTLC in a commitment_signed message, and are waiting on a
114114
/// remote revoke_and_ack on a previous state before we can do so.
115-
AwaitingRemoteRevokeToRemove,
115+
AwaitingRemoteRevokeToRemove(Option<HTLCFailReason>),
116116
/// Remote removed this and sent a commitment_signed (implying we've revoke_and_ack'ed it), but
117117
/// the remote side hasn't yet revoked their previous state, which we need them to do before we
118118
/// can do any backwards failing. Implies AwaitingRemoteRevoke.
119119
/// We have removed this HTLC in our latest commitment_signed and are now just waiting on a
120120
/// revoke_and_ack to drop completely.
121-
AwaitingRemovedRemoteRevoke,
121+
AwaitingRemovedRemoteRevoke(Option<HTLCFailReason>),
122122
}
123123

124124
struct OutboundHTLCOutput {
@@ -128,8 +128,6 @@ struct OutboundHTLCOutput {
128128
payment_hash: PaymentHash,
129129
state: OutboundHTLCState,
130130
source: HTLCSource,
131-
/// If we're in a removed state, set if they failed, otherwise None
132-
fail_reason: Option<HTLCFailReason>,
133131
}
134132

135133
/// See AwaitingRemoteRevoke ChannelState for more info
@@ -858,9 +856,9 @@ impl Channel {
858856
let (include, state_name) = match htlc.state {
859857
OutboundHTLCState::LocalAnnounced(_) => (generated_by_local, "LocalAnnounced"),
860858
OutboundHTLCState::Committed => (true, "Committed"),
861-
OutboundHTLCState::RemoteRemoved => (generated_by_local, "RemoteRemoved"),
862-
OutboundHTLCState::AwaitingRemoteRevokeToRemove => (generated_by_local, "AwaitingRemoteRevokeToRemove"),
863-
OutboundHTLCState::AwaitingRemovedRemoteRevoke => (false, "AwaitingRemovedRemoteRevoke"),
859+
OutboundHTLCState::RemoteRemoved(_) => (generated_by_local, "RemoteRemoved"),
860+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(_) => (generated_by_local, "AwaitingRemoteRevokeToRemove"),
861+
OutboundHTLCState::AwaitingRemovedRemoteRevoke(_) => (false, "AwaitingRemovedRemoteRevoke"),
864862
};
865863

866864
if include {
@@ -869,13 +867,11 @@ impl Channel {
869867
} else {
870868
log_trace!(self, " ...not including outbound HTLC {} (hash {}) with value {} due to state ({})", htlc.htlc_id, log_bytes!(htlc.payment_hash.0), htlc.amount_msat, state_name);
871869
match htlc.state {
872-
OutboundHTLCState::AwaitingRemoteRevokeToRemove|OutboundHTLCState::AwaitingRemovedRemoteRevoke => {
873-
if htlc.fail_reason.is_none() {
874-
value_to_self_msat_offset -= htlc.amount_msat as i64;
875-
}
870+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(None)|OutboundHTLCState::AwaitingRemovedRemoteRevoke(None) => {
871+
value_to_self_msat_offset -= htlc.amount_msat as i64;
876872
},
877-
OutboundHTLCState::RemoteRemoved => {
878-
if !generated_by_local && htlc.fail_reason.is_none() {
873+
OutboundHTLCState::RemoteRemoved(None) => {
874+
if !generated_by_local {
879875
value_to_self_msat_offset -= htlc.amount_msat as i64;
880876
}
881877
},
@@ -1644,10 +1640,9 @@ impl Channel {
16441640
OutboundHTLCState::LocalAnnounced(_) =>
16451641
return Err(ChannelError::Close("Remote tried to fulfill/fail HTLC before it had been committed")),
16461642
OutboundHTLCState::Committed => {
1647-
htlc.state = OutboundHTLCState::RemoteRemoved;
1648-
htlc.fail_reason = fail_reason;
1643+
htlc.state = OutboundHTLCState::RemoteRemoved(fail_reason);
16491644
},
1650-
OutboundHTLCState::AwaitingRemoteRevokeToRemove | OutboundHTLCState::AwaitingRemovedRemoteRevoke | OutboundHTLCState::RemoteRemoved =>
1645+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(_) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(_) | OutboundHTLCState::RemoteRemoved(_) =>
16511646
return Err(ChannelError::Close("Remote tried to fulfill/fail HTLC that they'd already fulfilled/failed")),
16521647
}
16531648
return Ok(&htlc.source);
@@ -1800,8 +1795,10 @@ impl Channel {
18001795
}
18011796
}
18021797
for htlc in self.pending_outbound_htlcs.iter_mut() {
1803-
if let OutboundHTLCState::RemoteRemoved = htlc.state {
1804-
htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove;
1798+
if let Some(fail_reason) = if let &mut OutboundHTLCState::RemoteRemoved(ref mut fail_reason) = &mut htlc.state {
1799+
Some(fail_reason.take())
1800+
} else { None } {
1801+
htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove(fail_reason);
18051802
need_our_commitment = true;
18061803
}
18071804
}
@@ -2019,9 +2016,9 @@ impl Channel {
20192016
} else { true }
20202017
});
20212018
pending_outbound_htlcs.retain(|htlc| {
2022-
if let OutboundHTLCState::AwaitingRemovedRemoteRevoke = htlc.state {
2019+
if let &OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref fail_reason) = &htlc.state {
20232020
log_trace!(logger, " ...removing outbound AwaitingRemovedRemoteRevoke {}", log_bytes!(htlc.payment_hash.0));
2024-
if let Some(reason) = htlc.fail_reason.clone() { // We really want take() here, but, again, non-mut ref :(
2021+
if let Some(reason) = fail_reason.clone() { // We really want take() here, but, again, non-mut ref :(
20252022
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
20262023
} else {
20272024
// They fulfilled, so we sent them money
@@ -2072,9 +2069,12 @@ impl Channel {
20722069
if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
20732070
log_trace!(logger, " ...promoting outbound LocalAnnounced {} to Committed", log_bytes!(htlc.payment_hash.0));
20742071
htlc.state = OutboundHTLCState::Committed;
2075-
} else if let OutboundHTLCState::AwaitingRemoteRevokeToRemove = htlc.state {
2072+
}
2073+
if let Some(fail_reason) = if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut fail_reason) = &mut htlc.state {
2074+
Some(fail_reason.take())
2075+
} else { None } {
20762076
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", log_bytes!(htlc.payment_hash.0));
2077-
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke;
2077+
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(fail_reason);
20782078
require_commitment = true;
20792079
}
20802080
}
@@ -2230,7 +2230,7 @@ impl Channel {
22302230
self.next_remote_htlc_id -= inbound_drop_count;
22312231

22322232
for htlc in self.pending_outbound_htlcs.iter_mut() {
2233-
if let OutboundHTLCState::RemoteRemoved = htlc.state {
2233+
if let OutboundHTLCState::RemoteRemoved(_) = htlc.state {
22342234
// They sent us an update to remove this but haven't yet sent the corresponding
22352235
// commitment_signed, we need to move it back to Committed and they can re-send
22362236
// the update upon reconnection.
@@ -3248,7 +3248,6 @@ impl Channel {
32483248
cltv_expiry: cltv_expiry,
32493249
state: OutboundHTLCState::LocalAnnounced(Box::new(onion_routing_packet.clone())),
32503250
source,
3251-
fail_reason: None,
32523251
});
32533252

32543253
let res = msgs::UpdateAddHTLC {
@@ -3313,8 +3312,10 @@ impl Channel {
33133312
}
33143313
}
33153314
for htlc in self.pending_outbound_htlcs.iter_mut() {
3316-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove = htlc.state {
3317-
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke;
3315+
if let Some(fail_reason) = if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut fail_reason) = &mut htlc.state {
3316+
Some(fail_reason.take())
3317+
} else { None } {
3318+
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(fail_reason);
33183319
}
33193320
}
33203321

@@ -3580,7 +3581,6 @@ impl Writeable for Channel {
35803581
htlc.cltv_expiry.write(writer)?;
35813582
htlc.payment_hash.write(writer)?;
35823583
htlc.source.write(writer)?;
3583-
write_option!(htlc.fail_reason);
35843584
match &htlc.state {
35853585
&OutboundHTLCState::LocalAnnounced(ref onion_packet) => {
35863586
0u8.write(writer)?;
@@ -3589,14 +3589,17 @@ impl Writeable for Channel {
35893589
&OutboundHTLCState::Committed => {
35903590
1u8.write(writer)?;
35913591
},
3592-
&OutboundHTLCState::RemoteRemoved => {
3592+
&OutboundHTLCState::RemoteRemoved(ref fail_reason) => {
35933593
2u8.write(writer)?;
3594+
write_option!(fail_reason);
35943595
},
3595-
&OutboundHTLCState::AwaitingRemoteRevokeToRemove => {
3596+
&OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref fail_reason) => {
35963597
3u8.write(writer)?;
3598+
write_option!(fail_reason);
35973599
},
3598-
&OutboundHTLCState::AwaitingRemovedRemoteRevoke => {
3600+
&OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref fail_reason) => {
35993601
4u8.write(writer)?;
3602+
write_option!(fail_reason);
36003603
},
36013604
}
36023605
}
@@ -3759,13 +3762,12 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
37593762
cltv_expiry: Readable::read(reader)?,
37603763
payment_hash: Readable::read(reader)?,
37613764
source: Readable::read(reader)?,
3762-
fail_reason: Readable::read(reader)?,
37633765
state: match <u8 as Readable<R>>::read(reader)? {
37643766
0 => OutboundHTLCState::LocalAnnounced(Box::new(Readable::read(reader)?)),
37653767
1 => OutboundHTLCState::Committed,
3766-
2 => OutboundHTLCState::RemoteRemoved,
3767-
3 => OutboundHTLCState::AwaitingRemoteRevokeToRemove,
3768-
4 => OutboundHTLCState::AwaitingRemovedRemoteRevoke,
3768+
2 => OutboundHTLCState::RemoteRemoved(Readable::read(reader)?),
3769+
3 => OutboundHTLCState::AwaitingRemoteRevokeToRemove(Readable::read(reader)?),
3770+
4 => OutboundHTLCState::AwaitingRemovedRemoteRevoke(Readable::read(reader)?),
37693771
_ => return Err(DecodeError::InvalidValue),
37703772
},
37713773
});
@@ -4158,7 +4160,6 @@ mod tests {
41584160
payment_hash: PaymentHash([0; 32]),
41594161
state: OutboundHTLCState::Committed,
41604162
source: HTLCSource::dummy(),
4161-
fail_reason: None,
41624163
};
41634164
out.payment_hash.0 = Sha256::hash(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()).into_inner();
41644165
out
@@ -4171,7 +4172,6 @@ mod tests {
41714172
payment_hash: PaymentHash([0; 32]),
41724173
state: OutboundHTLCState::Committed,
41734174
source: HTLCSource::dummy(),
4174-
fail_reason: None,
41754175
};
41764176
out.payment_hash.0 = Sha256::hash(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()).into_inner();
41774177
out

0 commit comments

Comments
 (0)