@@ -161,8 +161,11 @@ macro_rules! fail_forwarding_inequality {
161
161
#[ derive( Clone ) ]
162
162
struct ChannelState {
163
163
local_balance_msat : u64 ,
164
- in_flight : HashMap < PaymentHash , Htlc > ,
164
+ /// Maps payment hash to htlc and index that it was added at.
165
+ in_flight : HashMap < PaymentHash , ( Htlc , u64 ) > ,
165
166
policy : ChannelPolicy ,
167
+ /// Tracks unique identifier for htlcs proposed by this node (sent in the outgoing direction).
168
+ index : u64 ,
166
169
}
167
170
168
171
impl ChannelState {
@@ -174,12 +177,13 @@ impl ChannelState {
174
177
local_balance_msat,
175
178
in_flight : HashMap :: new ( ) ,
176
179
policy,
180
+ index : 0 ,
177
181
}
178
182
}
179
183
180
184
/// Returns the sum of all the *in flight outgoing* HTLCs on the channel.
181
185
fn in_flight_total ( & self ) -> u64 {
182
- self . in_flight . values ( ) . map ( |h| h. amount_msat ) . sum ( )
186
+ self . in_flight . values ( ) . map ( |h| h. 0 . amount_msat ) . sum ( )
183
187
}
184
188
185
189
/// Checks whether the proposed HTLC abides by the channel policy advertised for using this channel as the
@@ -233,18 +237,21 @@ impl ChannelState {
233
237
///
234
238
/// Note: MPP payments are not currently supported, so this function will fail if a duplicate payment hash is
235
239
/// reported.
236
- fn add_outgoing_htlc ( & mut self , hash : PaymentHash , htlc : Htlc ) -> Result < ( ) , ForwardingError > {
240
+ fn add_outgoing_htlc ( & mut self , hash : PaymentHash , htlc : Htlc ) -> Result < u64 , ForwardingError > {
237
241
self . check_outgoing_addition ( & htlc) ?;
238
242
if self . in_flight . contains_key ( & hash) {
239
243
return Err ( ForwardingError :: PaymentHashExists ( hash) ) ;
240
244
}
245
+ let index = self . index ;
246
+ self . index += 1 ;
247
+
241
248
self . local_balance_msat -= htlc. amount_msat ;
242
- self . in_flight . insert ( hash, htlc) ;
243
- Ok ( ( ) )
249
+ self . in_flight . insert ( hash, ( htlc, index ) ) ;
250
+ Ok ( index )
244
251
}
245
252
246
253
/// Removes the HTLC from our set of outgoing in-flight HTLCs, failing if the payment hash is not found.
247
- fn remove_outgoing_htlc ( & mut self , hash : & PaymentHash ) -> Result < Htlc , ForwardingError > {
254
+ fn remove_outgoing_htlc ( & mut self , hash : & PaymentHash ) -> Result < ( Htlc , u64 ) , ForwardingError > {
248
255
self . in_flight
249
256
. remove ( hash)
250
257
. ok_or ( ForwardingError :: PaymentHashNotFound ( * hash) )
@@ -352,14 +359,17 @@ impl SimulatedChannel {
352
359
sending_node : & PublicKey ,
353
360
hash : PaymentHash ,
354
361
htlc : Htlc ,
355
- ) -> Result < ( ) , ForwardingError > {
362
+ ) -> Result < u64 , ForwardingError > {
356
363
if htlc. amount_msat == 0 {
357
364
return Err ( ForwardingError :: ZeroAmountHtlc ) ;
358
365
}
359
366
360
- self . get_node_mut ( sending_node) ?
367
+ let index = self
368
+ . get_node_mut ( sending_node) ?
361
369
. add_outgoing_htlc ( hash, htlc) ?;
362
- self . sanity_check ( )
370
+ self . sanity_check ( ) ?;
371
+
372
+ Ok ( index)
363
373
}
364
374
365
375
/// Performs a sanity check on the total balances in a channel. Note that we do not currently include on-chain
@@ -382,12 +392,14 @@ impl SimulatedChannel {
382
392
sending_node : & PublicKey ,
383
393
hash : & PaymentHash ,
384
394
success : bool ,
385
- ) -> Result < ( ) , ForwardingError > {
395
+ ) -> Result < ( Htlc , u64 ) , ForwardingError > {
386
396
let htlc = self
387
397
. get_node_mut ( sending_node) ?
388
398
. remove_outgoing_htlc ( hash) ?;
389
- self . settle_htlc ( sending_node, htlc. amount_msat , success) ?;
390
- self . sanity_check ( )
399
+ self . settle_htlc ( sending_node, htlc. 0 . amount_msat , success) ?;
400
+ self . sanity_check ( ) ?;
401
+
402
+ Ok ( htlc)
391
403
}
392
404
393
405
/// Updates the local balance of each node in the channel once a htlc has been resolved, pushing funds to the
@@ -969,7 +981,9 @@ async fn remove_htlcs(
969
981
. await
970
982
. get_mut ( & ShortChannelID :: from ( hop. short_channel_id ) )
971
983
{
972
- Some ( channel) => channel. remove_htlc ( & incoming_node, & payment_hash, success) ?,
984
+ Some ( channel) => {
985
+ channel. remove_htlc ( & incoming_node, & payment_hash, success) ?;
986
+ } ,
973
987
None => {
974
988
return Err ( ForwardingError :: ChannelNotFound ( ShortChannelID :: from (
975
989
hop. short_channel_id ,
0 commit comments