@@ -155,8 +155,11 @@ macro_rules! fail_forwarding_inequality {
155
155
#[ derive( Clone ) ]
156
156
struct ChannelState {
157
157
local_balance_msat : u64 ,
158
- in_flight : HashMap < PaymentHash , Htlc > ,
158
+ /// Maps payment hash to htlc and index that it was added at.
159
+ in_flight : HashMap < PaymentHash , ( Htlc , u64 ) > ,
159
160
policy : ChannelPolicy ,
161
+ /// Tracks unique identifier for htlcs proposed by this node (sent in the outgoing direction).
162
+ index : u64 ,
160
163
}
161
164
162
165
impl ChannelState {
@@ -168,12 +171,13 @@ impl ChannelState {
168
171
local_balance_msat,
169
172
in_flight : HashMap :: new ( ) ,
170
173
policy,
174
+ index : 0 ,
171
175
}
172
176
}
173
177
174
178
/// Returns the sum of all the *in flight outgoing* HTLCs on the channel.
175
179
fn in_flight_total ( & self ) -> u64 {
176
- self . in_flight . values ( ) . map ( |h| h. amount_msat ) . sum ( )
180
+ self . in_flight . values ( ) . map ( |h| h. 0 . amount_msat ) . sum ( )
177
181
}
178
182
179
183
/// Checks whether the proposed HTLC abides by the channel policy advertised for using this channel as the
@@ -232,21 +236,28 @@ impl ChannelState {
232
236
///
233
237
/// Note: MPP payments are not currently supported, so this function will fail if a duplicate payment hash is
234
238
/// reported.
235
- fn add_outgoing_htlc ( & mut self , hash : PaymentHash , htlc : Htlc ) -> ForwardResult {
239
+ fn add_outgoing_htlc (
240
+ & mut self ,
241
+ hash : PaymentHash ,
242
+ htlc : Htlc ,
243
+ ) -> Result < Result < u64 , ForwardingError > , CriticalError > {
236
244
if let Err ( fwd_err) = self . check_outgoing_addition ( & htlc) {
237
245
return Ok ( Err ( fwd_err) ) ;
238
246
}
239
247
240
248
if self . in_flight . contains_key ( & hash) {
241
249
return Err ( CriticalError :: PaymentHashExists ( hash) ) ;
242
250
}
251
+ let index = self . index ;
252
+ self . index += 1 ;
253
+
243
254
self . local_balance_msat -= htlc. amount_msat ;
244
- self . in_flight . insert ( hash, htlc) ;
245
- Ok ( Ok ( ( ) ) )
255
+ self . in_flight . insert ( hash, ( htlc, index ) ) ;
256
+ Ok ( Ok ( index ) )
246
257
}
247
258
248
259
/// Removes the HTLC from our set of outgoing in-flight HTLCs, failing if the payment hash is not found.
249
- fn remove_outgoing_htlc ( & mut self , hash : & PaymentHash ) -> Result < Htlc , CriticalError > {
260
+ fn remove_outgoing_htlc ( & mut self , hash : & PaymentHash ) -> Result < ( Htlc , u64 ) , CriticalError > {
250
261
self . in_flight
251
262
. remove ( hash)
252
263
. ok_or ( CriticalError :: PaymentHashNotFound ( * hash) )
@@ -354,19 +365,14 @@ impl SimulatedChannel {
354
365
sending_node : & PublicKey ,
355
366
hash : PaymentHash ,
356
367
htlc : Htlc ,
357
- ) -> ForwardResult {
368
+ ) -> Result < Result < u64 , ForwardingError > , CriticalError > {
358
369
if htlc. amount_msat == 0 {
359
370
return Err ( CriticalError :: ZeroAmountHtlc ) ;
360
371
}
361
372
362
- if let Err ( fwd_err) = self
363
- . get_node_mut ( sending_node) ?
364
- . add_outgoing_htlc ( hash, htlc) ?
365
- {
366
- return Ok ( Err ( fwd_err) ) ;
367
- }
368
373
self . sanity_check ( ) ?;
369
- Ok ( Ok ( ( ) ) )
374
+ self . get_node_mut ( sending_node) ?
375
+ . add_outgoing_htlc ( hash, htlc)
370
376
}
371
377
372
378
/// Performs a sanity check on the total balances in a channel. Note that we do not currently include on-chain
@@ -395,12 +401,14 @@ impl SimulatedChannel {
395
401
sending_node : & PublicKey ,
396
402
hash : & PaymentHash ,
397
403
success : bool ,
398
- ) -> Result < ( ) , CriticalError > {
404
+ ) -> Result < ( Htlc , u64 ) , CriticalError > {
399
405
let htlc = self
400
406
. get_node_mut ( sending_node) ?
401
407
. remove_outgoing_htlc ( hash) ?;
402
- self . settle_htlc ( sending_node, htlc. amount_msat , success) ?;
403
- self . sanity_check ( )
408
+ self . settle_htlc ( sending_node, htlc. 0 . amount_msat , success) ?;
409
+ self . sanity_check ( ) ?;
410
+
411
+ Ok ( htlc)
404
412
}
405
413
406
414
/// Updates the local balance of each node in the channel once a htlc has been resolved, pushing funds to the
0 commit comments