Skip to content

Commit 6fa81f1

Browse files
committed
sim-node: add ChannelState functions for HTLC settle
To allow more isolated testing of channel state, move balance updates to function on the individual sides of the channel.
1 parent 1f726bd commit 6fa81f1

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

sim-lib/src/sim_node.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,24 @@ impl ChannelState {
231231
.remove(hash)
232232
.ok_or(ForwardingError::PaymentHashNotFound(*hash))
233233
}
234+
235+
// Updates channel state to account for the resolution of an outgoing in-flight HTLC. If the HTLC failed, the
236+
// balance is failed back to the channel's local balance. If not, the in-flight balance is settled to the other
237+
// node, so there is no operation.
238+
fn settle_outgoing_htlc(&mut self, amt: u64, success: bool) {
239+
if !success {
240+
self.local_balance_msat += amt
241+
}
242+
}
243+
244+
// Updates channel state to account for the resolution of an incoming in-flight HTLC. If the HTLC succeeded,
245+
// the balance is settled to the channel's local balance. If not, the in-flight balance is failed back to the
246+
// other node, so there is no operation.
247+
fn settle_incoming_htlc(&mut self, amt: u64, success: bool) {
248+
if success {
249+
self.local_balance_msat += amt
250+
}
251+
}
234252
}
235253

236254
/// Represents a simulated channel, and is responsible for managing addition and removal of HTLCs from the channel and
@@ -348,20 +366,13 @@ impl SimulatedChannel {
348366
amount_msat: u64,
349367
success: bool,
350368
) -> Result<(), ForwardingError> {
351-
// Successful payments push balance to the receiver, failures return it to the sender.
352-
let (sender_delta_msat, receiver_delta_msat) = if success {
353-
(0, amount_msat)
354-
} else {
355-
(amount_msat, 0)
356-
};
357-
358369
if sending_node == &self.node_1.policy.pubkey {
359-
self.node_1.local_balance_msat += sender_delta_msat;
360-
self.node_2.local_balance_msat += receiver_delta_msat;
370+
self.node_1.settle_outgoing_htlc(amount_msat, success);
371+
self.node_2.settle_incoming_htlc(amount_msat, success);
361372
Ok(())
362373
} else if sending_node == &self.node_2.policy.pubkey {
363-
self.node_2.local_balance_msat += sender_delta_msat;
364-
self.node_1.local_balance_msat += receiver_delta_msat;
374+
self.node_2.settle_outgoing_htlc(amount_msat, success);
375+
self.node_1.settle_incoming_htlc(amount_msat, success);
365376
Ok(())
366377
} else {
367378
Err(ForwardingError::NodeNotFound(*sending_node))

0 commit comments

Comments
 (0)