Skip to content

Commit 13ebb89

Browse files
committed
fixup! sim_node/feat: track payment path with in flight
1 parent c9ef909 commit 13ebb89

File tree

6 files changed

+21
-20
lines changed

6 files changed

+21
-20
lines changed

simln-lib/src/cln.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl LightningNode for ClnNode {
160160
}
161161

162162
async fn send_payment(
163-
&mut self,
163+
&self,
164164
dest: PublicKey,
165165
amount_msat: u64,
166166
) -> Result<PaymentHash, LightningError> {
@@ -194,7 +194,7 @@ impl LightningNode for ClnNode {
194194
}
195195

196196
async fn track_payment(
197-
&mut self,
197+
&self,
198198
hash: &PaymentHash,
199199
shutdown: Listener,
200200
) -> Result<PaymentResult, LightningError> {

simln-lib/src/eclair.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl LightningNode for EclairNode {
133133
}
134134

135135
async fn send_payment(
136-
&mut self,
136+
&self,
137137
dest: PublicKey,
138138
amount_msat: u64,
139139
) -> Result<PaymentHash, LightningError> {
@@ -164,7 +164,7 @@ impl LightningNode for EclairNode {
164164
}
165165

166166
async fn track_payment(
167-
&mut self,
167+
&self,
168168
hash: &PaymentHash,
169169
shutdown: Listener,
170170
) -> Result<PaymentResult, LightningError> {

simln-lib/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,13 @@ pub trait LightningNode: Send {
330330
fn get_network(&self) -> Network;
331331
/// Keysend payment worth `amount_msat` from a source node to the destination node.
332332
async fn send_payment(
333-
&mut self,
333+
&self,
334334
dest: PublicKey,
335335
amount_msat: u64,
336336
) -> Result<PaymentHash, LightningError>;
337337
/// Track a payment with the specified hash.
338338
async fn track_payment(
339-
&mut self,
339+
&self,
340340
hash: &PaymentHash,
341341
shutdown: Listener,
342342
) -> Result<PaymentResult, LightningError>;
@@ -1154,7 +1154,7 @@ async fn consume_events(
11541154
if let Some(event) = simulation_event {
11551155
match event {
11561156
SimulationEvent::SendPayment(dest, amt_msat) => {
1157-
let mut node = node.lock().await;
1157+
let node = node.lock().await;
11581158

11591159
let mut payment = Payment {
11601160
source: node.get_info().pubkey,
@@ -1506,7 +1506,7 @@ async fn track_payment_result(
15061506
) -> Result<(), SimulationError> {
15071507
log::trace!("Payment result tracker starting.");
15081508

1509-
let mut node = node.lock().await;
1509+
let node = node.lock().await;
15101510

15111511
let res = match payment.hash {
15121512
Some(hash) => {

simln-lib/src/lnd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl LightningNode for LndNode {
131131
}
132132

133133
async fn send_payment(
134-
&mut self,
134+
&self,
135135
dest: PublicKey,
136136
amount_msat: u64,
137137
) -> Result<PaymentHash, LightningError> {
@@ -170,7 +170,7 @@ impl LightningNode for LndNode {
170170
}
171171

172172
async fn track_payment(
173-
&mut self,
173+
&self,
174174
hash: &PaymentHash,
175175
shutdown: Listener,
176176
) -> Result<PaymentResult, LightningError> {

simln-lib/src/sim_node.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ pub struct SimNode<T: SimNetwork, C: Clock> {
507507
/// The underlying execution network that will be responsible for dispatching payments.
508508
network: Arc<Mutex<T>>,
509509
/// Tracks the channel that will provide updates for payments by hash.
510-
in_flight: HashMap<PaymentHash, InFlightPayment>,
510+
in_flight: Mutex<HashMap<PaymentHash, InFlightPayment>>,
511511
/// A read-only graph used for pathfinding.
512512
pathfinding_graph: Arc<LdkNetworkGraph>,
513513
/// Probabilistic scorer used to rank paths through the network for routing. This is reused across
@@ -538,7 +538,7 @@ impl<T: SimNetwork, C: Clock> SimNode<T, C> {
538538
Ok(SimNode {
539539
info,
540540
network: payment_network,
541-
in_flight: HashMap::new(),
541+
in_flight: Mutex::new(HashMap::new()),
542542
pathfinding_graph,
543543
scorer,
544544
clock,
@@ -569,7 +569,7 @@ impl<T: SimNetwork, C: Clock> SimNode<T, C> {
569569
}
570570

571571
// Check for payment hash collision, failing the payment if we happen to repeat one.
572-
match self.in_flight.entry(payment_hash) {
572+
match self.in_flight.lock().await.entry(payment_hash) {
573573
Entry::Occupied(_) => {
574574
return Err(LightningError::SendPaymentError(
575575
"payment hash exists".to_string(),
@@ -651,7 +651,7 @@ impl<T: SimNetwork, C: Clock> LightningNode for SimNode<T, C> {
651651
/// send_payment picks a random preimage for a payment, dispatches it in the network and adds a tracking channel
652652
/// to our node state to be used for subsequent track_payment calls.
653653
async fn send_payment(
654-
&mut self,
654+
&self,
655655
dest: PublicKey,
656656
amount_msat: u64,
657657
) -> Result<PaymentHash, LightningError> {
@@ -662,7 +662,8 @@ impl<T: SimNetwork, C: Clock> LightningNode for SimNode<T, C> {
662662
let payment_hash = preimage.into();
663663

664664
// Check for payment hash collision, failing the payment if we happen to repeat one.
665-
let entry = match self.in_flight.entry(payment_hash) {
665+
let mut in_flight_guard = self.in_flight.lock().await;
666+
let entry = match in_flight_guard.entry(payment_hash) {
666667
Entry::Occupied(_) => {
667668
return Err(LightningError::SendPaymentError(
668669
"payment hash exists".to_string(),
@@ -724,11 +725,11 @@ impl<T: SimNetwork, C: Clock> LightningNode for SimNode<T, C> {
724725
/// provided is triggered. This call will fail if the hash provided was not obtained from send_payment or passed
725726
/// into send_to_route first.
726727
async fn track_payment(
727-
&mut self,
728+
&self,
728729
hash: &PaymentHash,
729730
listener: Listener,
730731
) -> Result<PaymentResult, LightningError> {
731-
match self.in_flight.remove(hash) {
732+
match self.in_flight.lock().await.remove(hash) {
732733
Some(in_flight) => {
733734
select! {
734735
biased;
@@ -2050,7 +2051,7 @@ mod tests {
20502051

20512052
// Create a simulated node for the first channel in our network.
20522053
let pk = channels[0].node_1.policy.pubkey;
2053-
let mut node = SimNode::new(
2054+
let node = SimNode::new(
20542055
node_info(pk, String::default()),
20552056
sim_network.clone(),
20562057
Arc::new(graph),

simln-lib/src/test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ mock! {
7878
fn get_info(&self) -> &NodeInfo;
7979
fn get_network(&self) -> bitcoin::Network;
8080
async fn send_payment(
81-
&mut self,
81+
&self,
8282
dest: bitcoin::secp256k1::PublicKey,
8383
amount_msat: u64,
8484
) -> Result<lightning::ln::PaymentHash, LightningError>;
8585
async fn track_payment(
86-
&mut self,
86+
&self,
8787
hash: &lightning::ln::PaymentHash,
8888
shutdown: triggered::Listener,
8989
) -> Result<crate::PaymentResult, LightningError>;

0 commit comments

Comments
 (0)