@@ -1946,7 +1946,20 @@ impl<Signer: Sign> Channel<Signer> {
1946
1946
/// before we fail backwards.
1947
1947
/// If we do fail twice, we debug_assert!(false) and return Ok(None). Thus, will always return
1948
1948
/// Ok(_) if debug assertions are turned on or preconditions are met.
1949
- 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 {
1949
+ pub fn queue_fail_htlc < L : Deref > ( & mut self , htlc_id_arg : u64 , err_packet : msgs:: OnionErrorPacket , logger : & L )
1950
+ -> Result < ( ) , ChannelError > where L :: Target : Logger {
1951
+ self . fail_htlc ( htlc_id_arg, err_packet, true , logger)
1952
+ . map ( |msg_opt| assert ! ( msg_opt. is_none( ) , "We forced holding cell?" ) )
1953
+ }
1954
+
1955
+ /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
1956
+ /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
1957
+ /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
1958
+ /// before we fail backwards.
1959
+ /// If we do fail twice, we debug_assert!(false) and return Ok(None). Thus, will always return
1960
+ /// Ok(_) if debug assertions are turned on or preconditions are met.
1961
+ fn fail_htlc < L : Deref > ( & mut self , htlc_id_arg : u64 , err_packet : msgs:: OnionErrorPacket , mut force_holding_cell : bool , logger : & L )
1962
+ -> Result < Option < msgs:: UpdateFailHTLC > , ChannelError > where L :: Target : Logger {
1950
1963
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 ) ) != ( ChannelState :: ChannelReady as u32 ) {
1951
1964
panic ! ( "Was asked to fail an HTLC when channel was not in an operational state" ) ;
1952
1965
}
@@ -1984,8 +1997,13 @@ impl<Signer: Sign> Channel<Signer> {
1984
1997
return Ok ( None ) ;
1985
1998
}
1986
1999
1987
- // Now update local state:
1988
2000
if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateInProgress as u32 ) ) != 0 {
2001
+ debug_assert ! ( force_holding_cell, "We don't expect to need to use the holding cell if we weren't trying to" ) ;
2002
+ force_holding_cell = true ;
2003
+ }
2004
+
2005
+ // Now update local state:
2006
+ if force_holding_cell {
1989
2007
for pending_update in self . holding_cell_htlc_updates . iter ( ) {
1990
2008
match pending_update {
1991
2009
& HTLCUpdateAwaitingACK :: ClaimHTLC { htlc_id, .. } => {
@@ -3171,7 +3189,7 @@ impl<Signer: Sign> Channel<Signer> {
3171
3189
// to rebalance channels.
3172
3190
match & htlc_update {
3173
3191
& HTLCUpdateAwaitingACK :: AddHTLC { amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet, ..} => {
3174
- match self . send_htlc ( amount_msat, * payment_hash, cltv_expiry, source. clone ( ) , onion_routing_packet. clone ( ) , logger) {
3192
+ match self . send_htlc ( amount_msat, * payment_hash, cltv_expiry, source. clone ( ) , onion_routing_packet. clone ( ) , false , logger) {
3175
3193
Ok ( update_add_msg_option) => update_add_htlcs. push ( update_add_msg_option. unwrap ( ) ) ,
3176
3194
Err ( e) => {
3177
3195
match e {
@@ -3207,13 +3225,13 @@ impl<Signer: Sign> Channel<Signer> {
3207
3225
monitor_update. updates . append ( & mut additional_monitor_update. updates ) ;
3208
3226
} ,
3209
3227
& HTLCUpdateAwaitingACK :: FailHTLC { htlc_id, ref err_packet } => {
3210
- match self . get_update_fail_htlc ( htlc_id, err_packet. clone ( ) , logger) {
3228
+ match self . fail_htlc ( htlc_id, err_packet. clone ( ) , false , logger) {
3211
3229
Ok ( update_fail_msg_option) => {
3212
3230
// If an HTLC failure was previously added to the holding cell (via
3213
- // `get_update_fail_htlc `) then generating the fail message itself
3214
- // must not fail - we should never end up in a state where we
3215
- // double-fail an HTLC or fail-then-claim an HTLC as it indicates
3216
- // we didn't wait for a full revocation before failing.
3231
+ // `fail_htlc `) then generating the fail message itself must not
3232
+ // fail - we should never end up in a state where we double-fail an
3233
+ // HTLC or fail-then-claim an HTLC as it indicates we didn't wait
3234
+ // for a full revocation before failing.
3217
3235
update_fail_htlcs. push ( update_fail_msg_option. unwrap ( ) )
3218
3236
} ,
3219
3237
Err ( e) => {
@@ -5458,6 +5476,19 @@ impl<Signer: Sign> Channel<Signer> {
5458
5476
5459
5477
// Send stuff to our remote peers:
5460
5478
5479
+ /// Queues up an outbound HTLC to send by placing it in the holding cell. You should call
5480
+ /// `maybe_free_holding_cell_htlcs` in order to actually generate and send the commitment
5481
+ /// update.
5482
+ ///
5483
+ /// If an Err is returned, it's a ChannelError::Ignore!
5484
+ pub fn queue_htlc < L : Deref > ( & mut self , amount_msat : u64 , payment_hash : PaymentHash , cltv_expiry : u32 , source : HTLCSource ,
5485
+ onion_routing_packet : msgs:: OnionPacket , logger : & L )
5486
+ -> Result < ( ) , ChannelError > where L :: Target : Logger {
5487
+ self . send_htlc ( amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, true , logger)
5488
+ . map ( |msg_opt| assert ! ( msg_opt. is_none( ) , "We forced holding cell?" ) )
5489
+
5490
+ }
5491
+
5461
5492
/// Adds a pending outbound HTLC to this channel, note that you probably want
5462
5493
/// send_htlc_and_commit instead cause you'll want both messages at once.
5463
5494
///
@@ -5473,7 +5504,9 @@ impl<Signer: Sign> Channel<Signer> {
5473
5504
/// You MUST call send_commitment prior to calling any other methods on this Channel!
5474
5505
///
5475
5506
/// If an Err is returned, it's a ChannelError::Ignore!
5476
- 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 {
5507
+ fn send_htlc < L : Deref > ( & mut self , amount_msat : u64 , payment_hash : PaymentHash , cltv_expiry : u32 , source : HTLCSource ,
5508
+ onion_routing_packet : msgs:: OnionPacket , mut force_holding_cell : bool , logger : & L )
5509
+ -> Result < Option < msgs:: UpdateAddHTLC > , ChannelError > where L :: Target : Logger {
5477
5510
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 | BOTH_SIDES_SHUTDOWN_MASK ) ) != ( ChannelState :: ChannelReady as u32 ) {
5478
5511
return Err ( ChannelError :: Ignore ( "Cannot send HTLC until channel is fully established and we haven't started shutting down" . to_owned ( ) ) ) ;
5479
5512
}
@@ -5568,8 +5601,12 @@ impl<Signer: Sign> Channel<Signer> {
5568
5601
return Err ( ChannelError :: Ignore ( format ! ( "Cannot send value that would put our balance under counterparty-announced channel reserve value ({})" , chan_reserve_msat) ) ) ;
5569
5602
}
5570
5603
5571
- // Now update local state:
5572
5604
if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: MonitorUpdateInProgress as u32 ) ) != 0 {
5605
+ force_holding_cell = true ;
5606
+ }
5607
+
5608
+ // Now update local state:
5609
+ if force_holding_cell {
5573
5610
self . holding_cell_htlc_updates . push ( HTLCUpdateAwaitingACK :: AddHTLC {
5574
5611
amount_msat,
5575
5612
payment_hash,
@@ -5762,7 +5799,7 @@ impl<Signer: Sign> Channel<Signer> {
5762
5799
/// Shorthand for calling send_htlc() followed by send_commitment(), see docs on those for
5763
5800
/// more info.
5764
5801
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 {
5765
- match self . send_htlc ( amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, logger) ? {
5802
+ match self . send_htlc ( amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet, false , logger) ? {
5766
5803
Some ( update_add_htlc) => {
5767
5804
let ( commitment_signed, monitor_update) = self . send_commitment_no_status_check ( logger) ?;
5768
5805
Ok ( Some ( ( update_add_htlc, commitment_signed, monitor_update) ) )
0 commit comments