@@ -301,6 +301,33 @@ pub struct CounterpartyForwardingInfo {
301301 pub cltv_expiry_delta : u16 ,
302302}
303303
304+ /// A return value enum for get_update_fulfill_htlc. See UpdateFulfillCommitFetch variants for
305+ /// description
306+ enum UpdateFulfillFetch {
307+ NewClaim {
308+ monitor_update : ChannelMonitorUpdate ,
309+ msg : Option < msgs:: UpdateFulfillHTLC > ,
310+ } ,
311+ DuplicateClaim { } ,
312+ }
313+
314+ /// The return type of get_update_fulfill_htlc_and_commit.
315+ pub enum UpdateFulfillCommitFetch {
316+ /// Indicates the HTLC fulfill is new, and either generated an update_fulfill message, placed
317+ /// it in the holding cell, or re-generated the update_fulfill message after the same claim was
318+ /// previously placed in the holding cell (and has since been removed).
319+ NewClaim {
320+ /// The ChannelMonitorUpdate which places the new payment preimage in the channel monitor
321+ monitor_update : ChannelMonitorUpdate ,
322+ /// The update_fulfill message and commitment_signed message (if the claim was not placed
323+ /// in the holding cell).
324+ msgs : Option < ( msgs:: UpdateFulfillHTLC , msgs:: CommitmentSigned ) > ,
325+ } ,
326+ /// Indicates the HTLC fulfill is duplicative, and already existed either in the holding cell,
327+ /// or the HTLC is forgotten completely (presumably previously claimed).
328+ DuplicateClaim { } ,
329+ }
330+
304331// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
305332// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
306333// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
@@ -1232,7 +1259,7 @@ impl<Signer: Sign> Channel<Signer> {
12321259 make_funding_redeemscript ( & self . get_holder_pubkeys ( ) . funding_pubkey , self . counterparty_funding_pubkey ( ) )
12331260 }
12341261
1235- fn get_update_fulfill_htlc < L : Deref > ( & mut self , htlc_id_arg : u64 , payment_preimage_arg : PaymentPreimage , logger : & L ) -> Option < ( Option < msgs :: UpdateFulfillHTLC > , ChannelMonitorUpdate ) > where L :: Target : Logger {
1262+ fn get_update_fulfill_htlc < L : Deref > ( & mut self , htlc_id_arg : u64 , payment_preimage_arg : PaymentPreimage , logger : & L ) -> UpdateFulfillFetch where L :: Target : Logger {
12361263 // Either ChannelFunded got set (which means it won't be unset) or there is no way any
12371264 // caller thought we could have something claimed (cause we wouldn't have accepted in an
12381265 // incoming HTLC anyway). If we got to ShutdownComplete, callers aren't allowed to call us,
@@ -1260,7 +1287,7 @@ impl<Signer: Sign> Channel<Signer> {
12601287 log_warn ! ( logger, "Have preimage and want to fulfill HTLC with payment hash {} we already failed against channel {}" , log_bytes!( htlc. payment_hash. 0 ) , log_bytes!( self . channel_id( ) ) ) ;
12611288 debug_assert ! ( false , "Tried to fulfill an HTLC that was already failed" ) ;
12621289 }
1263- return None ;
1290+ return UpdateFulfillFetch :: DuplicateClaim { } ;
12641291 } ,
12651292 _ => {
12661293 debug_assert ! ( false , "Have an inbound HTLC we tried to claim before it was fully committed to" ) ;
@@ -1276,7 +1303,7 @@ impl<Signer: Sign> Channel<Signer> {
12761303 // If we failed to find an HTLC to fulfill, make sure it was previously fulfilled and
12771304 // this is simply a duplicate claim, not previously failed and we lost funds.
12781305 debug_assert ! ( self . historical_inbound_htlc_fulfills. contains( & htlc_id_arg) ) ;
1279- return None ;
1306+ return UpdateFulfillFetch :: DuplicateClaim { } ;
12801307 }
12811308
12821309 // Now update local state:
@@ -1300,7 +1327,7 @@ impl<Signer: Sign> Channel<Signer> {
13001327 self . latest_monitor_update_id -= 1 ;
13011328 #[ cfg( any( test, feature = "fuzztarget" ) ) ]
13021329 debug_assert ! ( self . historical_inbound_htlc_fulfills. contains( & htlc_id_arg) ) ;
1303- return None ;
1330+ return UpdateFulfillFetch :: DuplicateClaim { } ;
13041331 }
13051332 } ,
13061333 & HTLCUpdateAwaitingACK :: FailHTLC { htlc_id, .. } => {
@@ -1309,7 +1336,7 @@ impl<Signer: Sign> Channel<Signer> {
13091336 // TODO: We may actually be able to switch to a fulfill here, though its
13101337 // rare enough it may not be worth the complexity burden.
13111338 debug_assert ! ( false , "Tried to fulfill an HTLC that was already failed" ) ;
1312- return Some ( ( None , monitor_update) ) ;
1339+ return UpdateFulfillFetch :: NewClaim { monitor_update, msg : None } ;
13131340 }
13141341 } ,
13151342 _ => { }
@@ -1321,7 +1348,7 @@ impl<Signer: Sign> Channel<Signer> {
13211348 } ) ;
13221349 #[ cfg( any( test, feature = "fuzztarget" ) ) ]
13231350 self . historical_inbound_htlc_fulfills . insert ( htlc_id_arg) ;
1324- return Some ( ( None , monitor_update) ) ;
1351+ return UpdateFulfillFetch :: NewClaim { monitor_update, msg : None } ;
13251352 }
13261353 #[ cfg( any( test, feature = "fuzztarget" ) ) ]
13271354 self . historical_inbound_htlc_fulfills . insert ( htlc_id_arg) ;
@@ -1331,31 +1358,34 @@ impl<Signer: Sign> Channel<Signer> {
13311358 if let InboundHTLCState :: Committed = htlc. state {
13321359 } else {
13331360 debug_assert ! ( false , "Have an inbound HTLC we tried to claim before it was fully committed to" ) ;
1334- return Some ( ( None , monitor_update) ) ;
1361+ return UpdateFulfillFetch :: NewClaim { monitor_update, msg : None } ;
13351362 }
13361363 log_trace ! ( logger, "Upgrading HTLC {} to LocalRemoved with a Fulfill in channel {}!" , log_bytes!( htlc. payment_hash. 0 ) , log_bytes!( self . channel_id) ) ;
13371364 htlc. state = InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: Fulfill ( payment_preimage_arg. clone ( ) ) ) ;
13381365 }
13391366
1340- Some ( ( Some ( msgs:: UpdateFulfillHTLC {
1341- channel_id : self . channel_id ( ) ,
1342- htlc_id : htlc_id_arg,
1343- payment_preimage : payment_preimage_arg,
1344- } ) , monitor_update) )
1367+ UpdateFulfillFetch :: NewClaim {
1368+ monitor_update,
1369+ msg : Some ( msgs:: UpdateFulfillHTLC {
1370+ channel_id : self . channel_id ( ) ,
1371+ htlc_id : htlc_id_arg,
1372+ payment_preimage : payment_preimage_arg,
1373+ } ) ,
1374+ }
13451375 }
13461376
1347- pub fn get_update_fulfill_htlc_and_commit < L : Deref > ( & mut self , htlc_id : u64 , payment_preimage : PaymentPreimage , logger : & L ) -> Result < Option < ( Option < ( msgs :: UpdateFulfillHTLC , msgs :: CommitmentSigned ) > , ChannelMonitorUpdate ) > , ChannelError > where L :: Target : Logger {
1377+ pub fn get_update_fulfill_htlc_and_commit < L : Deref > ( & mut self , htlc_id : u64 , payment_preimage : PaymentPreimage , logger : & L ) -> Result < UpdateFulfillCommitFetch , ChannelError > where L :: Target : Logger {
13481378 match self . get_update_fulfill_htlc ( htlc_id, payment_preimage, logger) {
1349- Some ( ( Some ( update_fulfill_htlc ) , mut monitor_update) ) => {
1379+ UpdateFulfillFetch :: NewClaim { mut monitor_update, msg : Some ( update_fulfill_htlc ) } => {
13501380 let ( commitment, mut additional_update) = self . send_commitment_no_status_check ( logger) ?;
13511381 // send_commitment_no_status_check may bump latest_monitor_id but we want them to be
13521382 // strictly increasing by one, so decrement it here.
13531383 self . latest_monitor_update_id = monitor_update. update_id ;
13541384 monitor_update. updates . append ( & mut additional_update. updates ) ;
1355- Ok ( Some ( ( Some ( ( update_fulfill_htlc, commitment) ) , monitor_update ) ) )
1385+ Ok ( UpdateFulfillCommitFetch :: NewClaim { monitor_update , msgs : Some ( ( update_fulfill_htlc, commitment) ) } )
13561386 } ,
1357- Some ( ( None , monitor_update) ) => Ok ( Some ( ( None , monitor_update) ) ) ,
1358- None => Ok ( None )
1387+ UpdateFulfillFetch :: NewClaim { monitor_update, msg : None } => Ok ( UpdateFulfillCommitFetch :: NewClaim { monitor_update, msgs : None } ) ,
1388+ UpdateFulfillFetch :: DuplicateClaim { } => Ok ( UpdateFulfillCommitFetch :: DuplicateClaim { } ) ,
13591389 }
13601390 }
13611391
@@ -2463,7 +2493,10 @@ impl<Signer: Sign> Channel<Signer> {
24632493 // not fail - any in between attempts to claim the HTLC will have resulted
24642494 // in it hitting the holding cell again and we cannot change the state of a
24652495 // holding cell HTLC from fulfill to anything else.
2466- let ( update_fulfill_msg_option, mut additional_monitor_update) = self . get_update_fulfill_htlc ( htlc_id, * payment_preimage, logger) . unwrap ( ) ;
2496+ let ( update_fulfill_msg_option, mut additional_monitor_update) =
2497+ if let UpdateFulfillFetch :: NewClaim { msg, monitor_update } = self . get_update_fulfill_htlc ( htlc_id, * payment_preimage, logger) {
2498+ ( msg, monitor_update)
2499+ } else { unreachable ! ( ) } ;
24672500 update_fulfill_htlcs. push ( update_fulfill_msg_option. unwrap ( ) ) ;
24682501 monitor_update. updates . append ( & mut additional_monitor_update. updates ) ;
24692502 } ,
0 commit comments