@@ -1948,7 +1948,20 @@ impl<Signer: Sign> Channel<Signer> {
1948
1948
/// before we fail backwards.
1949
1949
/// If we do fail twice, we debug_assert!(false) and return Ok(None). Thus, will always return
1950
1950
/// Ok(_) if debug assertions are turned on or preconditions are met.
1951
- pub fn get_update_fail_htlc < L : Deref > ( & mut self , htlc_id_arg : u64 , err_packet : msgs:: OnionErrorPacket , logger : & L ) -> Result < Option < msgs:: UpdateFailHTLC > , ChannelError > where L :: Target : Logger {
1951
+ pub fn queue_fail_htlc < L : Deref > ( & mut self , htlc_id_arg : u64 , err_packet : msgs:: OnionErrorPacket , logger : & L )
1952
+ -> Result < ( ) , ChannelError > where L :: Target : Logger {
1953
+ self . fail_htlc ( htlc_id_arg, err_packet, true , logger)
1954
+ . map ( |msg_opt| assert ! ( msg_opt. is_none( ) , "We forced holding cell?" ) )
1955
+ }
1956
+
1957
+ /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
1958
+ /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
1959
+ /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
1960
+ /// before we fail backwards.
1961
+ /// If we do fail twice, we debug_assert!(false) and return Ok(None). Thus, will always return
1962
+ /// Ok(_) if debug assertions are turned on or preconditions are met.
1963
+ fn fail_htlc < L : Deref > ( & mut self , htlc_id_arg : u64 , err_packet : msgs:: OnionErrorPacket , mut force_holding_cell : bool , logger : & L )
1964
+ -> Result < Option < msgs:: UpdateFailHTLC > , ChannelError > where L :: Target : Logger {
1952
1965
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 ) ) != ( ChannelState :: ChannelReady as u32 ) {
1953
1966
panic ! ( "Was asked to fail an HTLC when channel was not in an operational state" ) ;
1954
1967
}
@@ -1986,8 +1999,13 @@ impl<Signer: Sign> Channel<Signer> {
1986
1999
return Ok ( None ) ;
1987
2000
}
1988
2001
1989
- // Now update local state:
1990
2002
if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateInProgress as u32 ) ) != 0 {
2003
+ debug_assert ! ( force_holding_cell, "We don't expect to need to use the holding cell if we weren't trying to" ) ;
2004
+ force_holding_cell = true ;
2005
+ }
2006
+
2007
+ // Now update local state:
2008
+ if force_holding_cell {
1991
2009
for pending_update in self . holding_cell_htlc_updates . iter ( ) {
1992
2010
match pending_update {
1993
2011
& HTLCUpdateAwaitingACK :: ClaimHTLC { htlc_id, .. } => {
@@ -3173,7 +3191,7 @@ impl<Signer: Sign> Channel<Signer> {
3173
3191
// to rebalance channels.
3174
3192
match & htlc_update {
3175
3193
& HTLCUpdateAwaitingACK :: AddHTLC { amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet, ..} => {
3176
- match self . send_htlc ( amount_msat, * payment_hash, cltv_expiry, source. clone ( ) , onion_routing_packet. clone ( ) , logger) {
3194
+ match self . send_htlc ( amount_msat, * payment_hash, cltv_expiry, source. clone ( ) , onion_routing_packet. clone ( ) , false , logger) {
3177
3195
Ok ( update_add_msg_option) => update_add_htlcs. push ( update_add_msg_option. unwrap ( ) ) ,
3178
3196
Err ( e) => {
3179
3197
match e {
@@ -3209,13 +3227,13 @@ impl<Signer: Sign> Channel<Signer> {
3209
3227
monitor_update. updates . append ( & mut additional_monitor_update. updates ) ;
3210
3228
} ,
3211
3229
& HTLCUpdateAwaitingACK :: FailHTLC { htlc_id, ref err_packet } => {
3212
- match self . get_update_fail_htlc ( htlc_id, err_packet. clone ( ) , logger) {
3230
+ match self . fail_htlc ( htlc_id, err_packet. clone ( ) , false , logger) {
3213
3231
Ok ( update_fail_msg_option) => {
3214
3232
// If an HTLC failure was previously added to the holding cell (via
3215
- // `get_update_fail_htlc `) then generating the fail message itself
3216
- // must not fail - we should never end up in a state where we
3217
- // double-fail an HTLC or fail-then-claim an HTLC as it indicates
3218
- // we didn't wait for a full revocation before failing.
3233
+ // `fail_htlc `) then generating the fail message itself must not
3234
+ // fail - we should never end up in a state where we double-fail an
3235
+ // HTLC or fail-then-claim an HTLC as it indicates we didn't wait
3236
+ // for a full revocation before failing.
3219
3237
update_fail_htlcs. push ( update_fail_msg_option. unwrap ( ) )
3220
3238
} ,
3221
3239
Err ( e) => {
@@ -5499,6 +5517,19 @@ impl<Signer: Sign> Channel<Signer> {
5499
5517
5500
5518
// Send stuff to our remote peers:
5501
5519
5520
+ /// Queues up an outbound HTLC to send by placing it in the holding cell. You should call
5521
+ /// `maybe_free_holding_cell_htlcs` in order to actually generate and send the commitment
5522
+ /// update.
5523
+ ///
5524
+ /// If an Err is returned, it's a ChannelError::Ignore!
5525
+ pub fn queue_htlc < L : Deref > ( & mut self , amount_msat : u64 , payment_hash : PaymentHash , cltv_expiry : u32 , source : HTLCSource ,
5526
+ onion_routing_packet : msgs:: OnionPacket , logger : & L )
5527
+ -> Result < ( ) , ChannelError > where L :: Target : Logger {
5528
+ self . send_htlc ( amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, true , logger)
5529
+ . map ( |msg_opt| assert ! ( msg_opt. is_none( ) , "We forced holding cell?" ) )
5530
+
5531
+ }
5532
+
5502
5533
/// Adds a pending outbound HTLC to this channel, note that you probably want
5503
5534
/// send_htlc_and_commit instead cause you'll want both messages at once.
5504
5535
///
@@ -5514,7 +5545,9 @@ impl<Signer: Sign> Channel<Signer> {
5514
5545
/// You MUST call send_commitment prior to calling any other methods on this Channel!
5515
5546
///
5516
5547
/// If an Err is returned, it's a ChannelError::Ignore!
5517
- pub fn send_htlc < L : Deref > ( & mut self , amount_msat : u64 , payment_hash : PaymentHash , cltv_expiry : u32 , source : HTLCSource , onion_routing_packet : msgs:: OnionPacket , logger : & L ) -> Result < Option < msgs:: UpdateAddHTLC > , ChannelError > where L :: Target : Logger {
5548
+ fn send_htlc < L : Deref > ( & mut self , amount_msat : u64 , payment_hash : PaymentHash , cltv_expiry : u32 , source : HTLCSource ,
5549
+ onion_routing_packet : msgs:: OnionPacket , mut force_holding_cell : bool , logger : & L )
5550
+ -> Result < Option < msgs:: UpdateAddHTLC > , ChannelError > where L :: Target : Logger {
5518
5551
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 | BOTH_SIDES_SHUTDOWN_MASK ) ) != ( ChannelState :: ChannelReady as u32 ) {
5519
5552
return Err ( ChannelError :: Ignore ( "Cannot send HTLC until channel is fully established and we haven't started shutting down" . to_owned ( ) ) ) ;
5520
5553
}
@@ -5609,8 +5642,12 @@ impl<Signer: Sign> Channel<Signer> {
5609
5642
return Err ( ChannelError :: Ignore ( format ! ( "Cannot send value that would put our balance under counterparty-announced channel reserve value ({})" , chan_reserve_msat) ) ) ;
5610
5643
}
5611
5644
5612
- // Now update local state:
5613
5645
if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: MonitorUpdateInProgress as u32 ) ) != 0 {
5646
+ force_holding_cell = true ;
5647
+ }
5648
+
5649
+ // Now update local state:
5650
+ if force_holding_cell {
5614
5651
self . holding_cell_htlc_updates . push ( HTLCUpdateAwaitingACK :: AddHTLC {
5615
5652
amount_msat,
5616
5653
payment_hash,
@@ -5803,7 +5840,7 @@ impl<Signer: Sign> Channel<Signer> {
5803
5840
/// Shorthand for calling send_htlc() followed by send_commitment(), see docs on those for
5804
5841
/// more info.
5805
5842
pub fn send_htlc_and_commit < L : Deref > ( & mut self , amount_msat : u64 , payment_hash : PaymentHash , cltv_expiry : u32 , source : HTLCSource , onion_routing_packet : msgs:: OnionPacket , logger : & L ) -> Result < Option < ( msgs:: UpdateAddHTLC , msgs:: CommitmentSigned , ChannelMonitorUpdate ) > , ChannelError > where L :: Target : Logger {
5806
- match self . send_htlc ( amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, logger) ? {
5843
+ match self . send_htlc ( amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, false , logger) ? {
5807
5844
Some ( update_add_htlc) => {
5808
5845
let ( commitment_signed, monitor_update) = self . send_commitment_no_status_check ( logger) ?;
5809
5846
Ok ( Some ( ( update_add_htlc, commitment_signed, monitor_update) ) )
0 commit comments