Skip to content

Commit 8ded28e

Browse files
committed
feat(LNnode):remove mutable reference in Lightening node impl
1 parent 9eedadb commit 8ded28e

File tree

6 files changed

+87
-80
lines changed

6 files changed

+87
-80
lines changed

simln-lib/src/cln.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use cln_grpc::pb::{
1010
};
1111
use lightning::ln::features::NodeFeatures;
1212
use lightning::ln::PaymentHash;
13-
1413
use serde::{Deserialize, Serialize};
1514
use tokio::fs::File;
1615
use tokio::io::{AsyncReadExt, Error};
16+
use tokio::sync::Mutex;
1717
use tokio::time::{self, Duration};
1818
use tonic::transport::{Certificate, Channel, ClientTlsConfig, Identity};
1919
use triggered::Listener;
@@ -38,7 +38,7 @@ pub struct ClnConnection {
3838
}
3939

4040
pub struct ClnNode {
41-
pub client: NodeClient<Channel>,
41+
pub client: Mutex<NodeClient<Channel>>,
4242
info: NodeInfo,
4343
}
4444

@@ -60,7 +60,7 @@ impl ClnNode {
6060
})?,
6161
));
6262

63-
let mut client = NodeClient::new(
63+
let client = Mutex::new(NodeClient::new(
6464
Channel::from_shared(connection.address)
6565
.map_err(|err| LightningError::ConnectionError(err.to_string()))?
6666
.tls_config(tls)
@@ -72,9 +72,11 @@ impl ClnNode {
7272
.map_err(|_| {
7373
LightningError::ConnectionError("Cannot connect to gRPC server".to_string())
7474
})?,
75-
);
75+
));
7676

7777
let (id, mut alias, our_features) = client
78+
.lock()
79+
.await
7880
.getinfo(GetinfoRequest {})
7981
.await
8082
.map(|r| {
@@ -125,6 +127,8 @@ impl ClnNode {
125127

126128
let resp = self
127129
.client
130+
.lock()
131+
.await
128132
.list_channels(req)
129133
.await
130134
.map_err(|err| LightningError::ListChannelsError(err.to_string()))?
@@ -144,9 +148,9 @@ impl LightningNode for ClnNode {
144148
&self.info
145149
}
146150

147-
async fn get_network(&mut self) -> Result<Network, LightningError> {
148-
let info = self
149-
.client
151+
async fn get_network(&self) -> Result<Network, LightningError> {
152+
let mut client = self.client.lock().await;
153+
let info = client
150154
.getinfo(GetinfoRequest {})
151155
.await
152156
.map_err(|err| LightningError::GetInfoError(err.to_string()))?
@@ -157,12 +161,12 @@ impl LightningNode for ClnNode {
157161
}
158162

159163
async fn send_payment(
160-
&mut self,
164+
&self,
161165
dest: PublicKey,
162166
amount_msat: u64,
163167
) -> Result<PaymentHash, LightningError> {
164-
let KeysendResponse { payment_hash, .. } = self
165-
.client
168+
let mut client = self.client.lock().await;
169+
let KeysendResponse { payment_hash, .. } = client
166170
.key_send(KeysendRequest {
167171
destination: dest.serialize().to_vec(),
168172
amount_msat: Some(Amount { msat: amount_msat }),
@@ -191,7 +195,7 @@ impl LightningNode for ClnNode {
191195
}
192196

193197
async fn track_payment(
194-
&mut self,
198+
&self,
195199
hash: &PaymentHash,
196200
shutdown: Listener,
197201
) -> Result<PaymentResult, LightningError> {
@@ -202,8 +206,8 @@ impl LightningNode for ClnNode {
202206
return Err(LightningError::TrackPaymentError("Shutdown before tracking results".to_string()));
203207
},
204208
_ = time::sleep(Duration::from_millis(500)) => {
205-
let ListpaysResponse { pays } = self
206-
.client
209+
let mut client = self.client.lock().await;
210+
let ListpaysResponse { pays } = client
207211
.list_pays(ListpaysRequest {
208212
payment_hash: Some(hash.0.to_vec()),
209213
..Default::default()
@@ -233,9 +237,9 @@ impl LightningNode for ClnNode {
233237
}
234238
}
235239

236-
async fn get_node_info(&mut self, node_id: &PublicKey) -> Result<NodeInfo, LightningError> {
237-
let mut nodes: Vec<cln_grpc::pb::ListnodesNodes> = self
238-
.client
240+
async fn get_node_info(&self, node_id: &PublicKey) -> Result<NodeInfo, LightningError> {
241+
let mut client = self.client.lock().await;
242+
let mut nodes: Vec<cln_grpc::pb::ListnodesNodes> = client
239243
.list_nodes(ListnodesRequest {
240244
id: Some(node_id.serialize().to_vec()),
241245
})
@@ -267,9 +271,9 @@ impl LightningNode for ClnNode {
267271
Ok(node_channels)
268272
}
269273

270-
async fn get_graph(&mut self) -> Result<Graph, LightningError> {
271-
let nodes: Vec<cln_grpc::pb::ListnodesNodes> = self
272-
.client
274+
async fn get_graph(&self) -> Result<Graph, LightningError> {
275+
let mut client = self.client.lock().await;
276+
let nodes: Vec<cln_grpc::pb::ListnodesNodes> = client
273277
.list_nodes(ListnodesRequest { id: None })
274278
.await
275279
.map_err(|err| LightningError::GetNodeInfoError(err.to_string()))?

simln-lib/src/eclair.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::collections::HashMap;
1515
use std::error::Error;
1616
use std::str::FromStr;
1717
use std::time::Duration;
18+
use tokio::sync::Mutex;
1819
use tokio::time;
1920
use triggered::Listener;
2021

@@ -83,7 +84,7 @@ impl TryInto<EclairClient> for EclairConnection {
8384
}
8485

8586
pub struct EclairNode {
86-
client: EclairClient,
87+
client: Mutex<EclairClient>,
8788
info: NodeInfo,
8889
network: Network,
8990
}
@@ -110,7 +111,7 @@ impl EclairNode {
110111
let features = parse_json_to_node_features(&info.features);
111112

112113
Ok(Self {
113-
client,
114+
client: Mutex::new(client),
114115
info: NodeInfo {
115116
pubkey,
116117
alias: info.alias,
@@ -127,31 +128,30 @@ impl LightningNode for EclairNode {
127128
&self.info
128129
}
129130

130-
async fn get_network(&mut self) -> Result<Network, LightningError> {
131+
async fn get_network(&self) -> Result<Network, LightningError> {
131132
Ok(self.network)
132133
}
133134

134135
async fn send_payment(
135-
&mut self,
136+
&self,
136137
dest: PublicKey,
137138
amount_msat: u64,
138139
) -> Result<PaymentHash, LightningError> {
140+
let client = self.client.lock().await;
139141
let preimage = PaymentPreimage(rand::random()).0;
140142
let mut params = HashMap::new();
141143
params.insert("nodeId".to_string(), hex::encode(dest.serialize()));
142144
params.insert("amountMsat".to_string(), amount_msat.to_string());
143145
params.insert("paymentHash".to_string(), hex::encode(preimage));
144-
let uuid: String = self
145-
.client
146+
let uuid: String = client
146147
.request("sendtonode", Some(params))
147148
.await
148149
.map_err(|err| LightningError::SendPaymentError(err.to_string()))?;
149150

150151
let mut params = HashMap::new();
151152
params.insert("paymentHash".to_string(), hex::encode(preimage));
152153
params.insert("id".to_string(), uuid);
153-
let payment_parts: PaymentInfoResponse = self
154-
.client
154+
let payment_parts: PaymentInfoResponse = client
155155
.request("getsentinfo", Some(params))
156156
.await
157157
.map_err(|_| LightningError::InvalidPaymentHash)?;
@@ -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> {
@@ -175,11 +175,11 @@ impl LightningNode for EclairNode {
175175
return Err(LightningError::TrackPaymentError("Shutdown before tracking results".to_string()));
176176
},
177177
_ = time::sleep(Duration::from_millis(500)) => {
178+
let client = self.client.lock().await;
178179
let mut params = HashMap::new();
179180
params.insert("paymentHash".to_string(), hex::encode(hash.0));
180181

181-
let payment_parts: PaymentInfoResponse = self
182-
.client
182+
let payment_parts: PaymentInfoResponse = client
183183
.request("getsentinfo", Some(params))
184184
.await
185185
.map_err(|err| LightningError::TrackPaymentError(err.to_string()))?;
@@ -204,12 +204,12 @@ impl LightningNode for EclairNode {
204204
}
205205
}
206206

207-
async fn get_node_info(&mut self, node_id: &PublicKey) -> Result<NodeInfo, LightningError> {
207+
async fn get_node_info(&self, node_id: &PublicKey) -> Result<NodeInfo, LightningError> {
208208
let mut params = HashMap::new();
209209
params.insert("nodeId".to_string(), hex::encode(node_id.serialize()));
210210

211-
let node_info: NodeResponse = self
212-
.client
211+
let client = self.client.lock().await;
212+
let node_info: NodeResponse = client
213213
.request("node", Some(params))
214214
.await
215215
.map_err(|err| LightningError::GetNodeInfoError(err.to_string()))?;
@@ -223,8 +223,8 @@ impl LightningNode for EclairNode {
223223
}
224224

225225
async fn list_channels(&mut self) -> Result<Vec<u64>, LightningError> {
226-
let channels: ChannelsResponse = self
227-
.client
226+
let client = self.client.lock().await;
227+
let channels: ChannelsResponse = client
228228
.request("channels", None)
229229
.await
230230
.map_err(|err| LightningError::ListChannelsError(err.to_string()))?;
@@ -245,9 +245,9 @@ impl LightningNode for EclairNode {
245245
Ok(capacities_msat)
246246
}
247247

248-
async fn get_graph(&mut self) -> Result<Graph, LightningError> {
249-
let nodes: NodesResponse = self
250-
.client
248+
async fn get_graph(&self) -> Result<Graph, LightningError> {
249+
let client = self.client.lock().await;
250+
let nodes: NodesResponse = client
251251
.request("nodes", None)
252252
.await
253253
.map_err(|err| LightningError::GetNodeInfoError(err.to_string()))?;

simln-lib/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,26 +324,26 @@ pub trait LightningNode: Send {
324324
/// Get information about the node.
325325
fn get_info(&self) -> &NodeInfo;
326326
/// Get the network this node is running at.
327-
async fn get_network(&mut self) -> Result<Network, LightningError>;
327+
async fn get_network(&self) -> Result<Network, LightningError>;
328328
/// Keysend payment worth `amount_msat` from a source node to the destination node.
329329
async fn send_payment(
330-
&mut self,
330+
&self,
331331
dest: PublicKey,
332332
amount_msat: u64,
333333
) -> Result<PaymentHash, LightningError>;
334334
/// Track a payment with the specified hash.
335335
async fn track_payment(
336-
&mut self,
336+
&self,
337337
hash: &PaymentHash,
338338
shutdown: Listener,
339339
) -> Result<PaymentResult, LightningError>;
340340
/// Gets information on a specific node.
341-
async fn get_node_info(&mut self, node_id: &PublicKey) -> Result<NodeInfo, LightningError>;
341+
async fn get_node_info(&self, node_id: &PublicKey) -> Result<NodeInfo, LightningError>;
342342
/// Lists all channels, at present only returns a vector of channel capacities in msat because no further
343343
/// information is required.
344344
async fn list_channels(&mut self) -> Result<Vec<u64>, LightningError>;
345345
/// Get the network graph from the point of view of a given node.
346-
async fn get_graph(&mut self) -> Result<Graph, LightningError>;
346+
async fn get_graph(&self) -> Result<Graph, LightningError>;
347347
}
348348

349349
/// Represents an error that occurs when generating a destination for a payment.
@@ -1149,7 +1149,7 @@ async fn consume_events(
11491149
if let Some(event) = simulation_event {
11501150
match event {
11511151
SimulationEvent::SendPayment(dest, amt_msat) => {
1152-
let mut node = node.lock().await;
1152+
let node = node.lock().await;
11531153

11541154
let mut payment = Payment {
11551155
source: node.get_info().pubkey,
@@ -1501,7 +1501,7 @@ async fn track_payment_result(
15011501
) -> Result<(), SimulationError> {
15021502
log::trace!("Payment result tracker starting.");
15031503

1504-
let mut node = node.lock().await;
1504+
let node = node.lock().await;
15051505

15061506
let res = match payment.hash {
15071507
Some(hash) => {

0 commit comments

Comments
 (0)