Skip to content

Commit 07c87f1

Browse files
committed
simln-lib: add unique index to htlcs in ChannelState
1 parent bb69f66 commit 07c87f1

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

simln-lib/src/sim_node.rs

+27-13
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ macro_rules! fail_forwarding_inequality {
161161
#[derive(Clone)]
162162
struct ChannelState {
163163
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)>,
165166
policy: ChannelPolicy,
167+
/// Tracks unique identifier for htlcs proposed by this node (sent in the outgoing direction).
168+
index: u64,
166169
}
167170

168171
impl ChannelState {
@@ -174,12 +177,13 @@ impl ChannelState {
174177
local_balance_msat,
175178
in_flight: HashMap::new(),
176179
policy,
180+
index: 0,
177181
}
178182
}
179183

180184
/// Returns the sum of all the *in flight outgoing* HTLCs on the channel.
181185
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()
183187
}
184188

185189
/// Checks whether the proposed HTLC abides by the channel policy advertised for using this channel as the
@@ -233,18 +237,21 @@ impl ChannelState {
233237
///
234238
/// Note: MPP payments are not currently supported, so this function will fail if a duplicate payment hash is
235239
/// 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> {
237241
self.check_outgoing_addition(&htlc)?;
238242
if self.in_flight.contains_key(&hash) {
239243
return Err(ForwardingError::PaymentHashExists(hash));
240244
}
245+
let index = self.index;
246+
self.index += 1;
247+
241248
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)
244251
}
245252

246253
/// 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> {
248255
self.in_flight
249256
.remove(hash)
250257
.ok_or(ForwardingError::PaymentHashNotFound(*hash))
@@ -352,14 +359,17 @@ impl SimulatedChannel {
352359
sending_node: &PublicKey,
353360
hash: PaymentHash,
354361
htlc: Htlc,
355-
) -> Result<(), ForwardingError> {
362+
) -> Result<u64, ForwardingError> {
356363
if htlc.amount_msat == 0 {
357364
return Err(ForwardingError::ZeroAmountHtlc);
358365
}
359366

360-
self.get_node_mut(sending_node)?
367+
let index = self
368+
.get_node_mut(sending_node)?
361369
.add_outgoing_htlc(hash, htlc)?;
362-
self.sanity_check()
370+
self.sanity_check()?;
371+
372+
Ok(index)
363373
}
364374

365375
/// 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 {
382392
sending_node: &PublicKey,
383393
hash: &PaymentHash,
384394
success: bool,
385-
) -> Result<(), ForwardingError> {
395+
) -> Result<(Htlc, u64), ForwardingError> {
386396
let htlc = self
387397
.get_node_mut(sending_node)?
388398
.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)
391403
}
392404

393405
/// 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(
969981
.await
970982
.get_mut(&ShortChannelID::from(hop.short_channel_id))
971983
{
972-
Some(channel) => channel.remove_htlc(&incoming_node, &payment_hash, success)?,
984+
Some(channel) => {
985+
channel.remove_htlc(&incoming_node, &payment_hash, success)?;
986+
},
973987
None => {
974988
return Err(ForwardingError::ChannelNotFound(ShortChannelID::from(
975989
hop.short_channel_id,

0 commit comments

Comments
 (0)