Skip to content

Commit bb07809

Browse files
committed
simln-lib: add unique index to htlcs in ChannelState
1 parent 8cf2042 commit bb07809

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

simln-lib/src/sim_node.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,11 @@ macro_rules! fail_forwarding_inequality {
155155
#[derive(Clone)]
156156
struct ChannelState {
157157
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)>,
159160
policy: ChannelPolicy,
161+
/// Tracks unique identifier for htlcs proposed by this node (sent in the outgoing direction).
162+
index: u64,
160163
}
161164

162165
impl ChannelState {
@@ -168,12 +171,13 @@ impl ChannelState {
168171
local_balance_msat,
169172
in_flight: HashMap::new(),
170173
policy,
174+
index: 0,
171175
}
172176
}
173177

174178
/// Returns the sum of all the *in flight outgoing* HTLCs on the channel.
175179
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()
177181
}
178182

179183
/// Checks whether the proposed HTLC abides by the channel policy advertised for using this channel as the
@@ -232,21 +236,28 @@ impl ChannelState {
232236
///
233237
/// Note: MPP payments are not currently supported, so this function will fail if a duplicate payment hash is
234238
/// 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> {
236244
if let Err(fwd_err) = self.check_outgoing_addition(&htlc) {
237245
return Ok(Err(fwd_err));
238246
}
239247

240248
if self.in_flight.contains_key(&hash) {
241249
return Err(CriticalError::PaymentHashExists(hash));
242250
}
251+
let index = self.index;
252+
self.index += 1;
253+
243254
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))
246257
}
247258

248259
/// 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> {
250261
self.in_flight
251262
.remove(hash)
252263
.ok_or(CriticalError::PaymentHashNotFound(*hash))
@@ -354,19 +365,14 @@ impl SimulatedChannel {
354365
sending_node: &PublicKey,
355366
hash: PaymentHash,
356367
htlc: Htlc,
357-
) -> ForwardResult {
368+
) -> Result<Result<u64, ForwardingError>, CriticalError> {
358369
if htlc.amount_msat == 0 {
359370
return Err(CriticalError::ZeroAmountHtlc);
360371
}
361372

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-
}
368373
self.sanity_check()?;
369-
Ok(Ok(()))
374+
self.get_node_mut(sending_node)?
375+
.add_outgoing_htlc(hash, htlc)
370376
}
371377

372378
/// 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 {
395401
sending_node: &PublicKey,
396402
hash: &PaymentHash,
397403
success: bool,
398-
) -> Result<(), CriticalError> {
404+
) -> Result<(Htlc, u64), CriticalError> {
399405
let htlc = self
400406
.get_node_mut(sending_node)?
401407
.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)
404412
}
405413

406414
/// Updates the local balance of each node in the channel once a htlc has been resolved, pushing funds to the

0 commit comments

Comments
 (0)