Skip to content

Commit 09fb1ba

Browse files
committed
simln-lib: repurpose list_channels to channel_capacities
Currently, list_channels is only used for returning a list of channel capacities and using that to determine how much the node should send in a month. Change it to channel_capacities that will return the sum of the capacities of the channels that should be taken into account.
1 parent ec1aa36 commit 09fb1ba

File tree

6 files changed

+29
-38
lines changed

6 files changed

+29
-38
lines changed

simln-lib/src/cln.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ impl LightningNode for ClnNode {
264264
}
265265
}
266266

267-
async fn list_channels(&self) -> Result<Vec<u64>, LightningError> {
267+
async fn channel_capacities(&self) -> Result<u64, LightningError> {
268268
let mut node_channels = self.node_channels(true).await?;
269269
node_channels.extend(self.node_channels(false).await?);
270-
Ok(node_channels)
270+
Ok(node_channels.iter().sum())
271271
}
272272

273273
async fn get_graph(&self) -> Result<Graph, LightningError> {

simln-lib/src/eclair.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl LightningNode for EclairNode {
222222
})
223223
}
224224

225-
async fn list_channels(&self) -> Result<Vec<u64>, LightningError> {
225+
async fn channel_capacities(&self) -> Result<u64, LightningError> {
226226
let client = self.client.lock().await;
227227
let channels: ChannelsResponse = client
228228
.request("channels", None)
@@ -242,7 +242,7 @@ impl LightningNode for EclairNode {
242242
})
243243
.collect();
244244

245-
Ok(capacities_msat)
245+
Ok(capacities_msat.iter().sum())
246246
}
247247

248248
async fn get_graph(&self) -> Result<Graph, LightningError> {

simln-lib/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,9 @@ pub trait LightningNode: Send {
343343
) -> Result<PaymentResult, LightningError>;
344344
/// Gets information on a specific node.
345345
async fn get_node_info(&self, node_id: &PublicKey) -> Result<NodeInfo, LightningError>;
346-
/// Lists all channels, at present only returns a vector of channel capacities in msat because no further
347-
/// information is required.
348-
async fn list_channels(&self) -> Result<Vec<u64>, LightningError>;
346+
/// Sum of channel capacities. It will be used to determine how much the node will send per
347+
/// month.
348+
async fn channel_capacities(&self) -> Result<u64, LightningError>;
349349
/// Get the network graph from the point of view of a given node.
350350
async fn get_graph(&self) -> Result<Graph, LightningError>;
351351
}
@@ -1003,7 +1003,7 @@ impl<C: Clock + 'static> Simulation<C> {
10031003
// While we're at it, we get the node info and store it with capacity to create activity generators in our
10041004
// second pass.
10051005
for (pk, node) in self.nodes.iter() {
1006-
let chan_capacity = node.lock().await.list_channels().await?.iter().sum::<u64>();
1006+
let chan_capacity = node.lock().await.channel_capacities().await?;
10071007

10081008
if let Err(e) = RandomPaymentActivity::validate_capacity(
10091009
chan_capacity,
@@ -1935,8 +1935,8 @@ mod tests {
19351935
.expect_get_network()
19361936
.returning(|| Network::Regtest);
19371937
mock_node
1938-
.expect_list_channels()
1939-
.returning(|| Ok(vec![100_000_000]));
1938+
.expect_channel_capacities()
1939+
.returning(|| Ok(100_000_000));
19401940
mock_node
19411941
.expect_get_node_info()
19421942
.returning(move |_| Ok(node_info.clone()));

simln-lib/src/lnd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ impl LightningNode for LndNode {
252252
}
253253
}
254254

255-
async fn list_channels(&self) -> Result<Vec<u64>, LightningError> {
255+
async fn channel_capacities(&self) -> Result<u64, LightningError> {
256256
let mut client = self.client.lock().await;
257257
let channels = client
258258
.lightning()
@@ -268,7 +268,7 @@ impl LightningNode for LndNode {
268268
.channels
269269
.iter()
270270
.map(|channel| 1000 * channel.capacity as u64)
271-
.collect())
271+
.sum())
272272
}
273273

274274
async fn get_graph(&self) -> Result<Graph, LightningError> {

simln-lib/src/sim_node.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,9 @@ impl<T: SimNetwork, C: Clock> LightningNode for SimNode<T, C> {
797797
Ok(self.network.lock().await.lookup_node(node_id)?.0)
798798
}
799799

800-
async fn list_channels(&self) -> Result<Vec<u64>, LightningError> {
801-
Ok(self.network.lock().await.lookup_node(&self.info.pubkey)?.1)
800+
async fn channel_capacities(&self) -> Result<u64, LightningError> {
801+
let channels = self.network.lock().await.lookup_node(&self.info.pubkey)?;
802+
Ok(channels.1.iter().sum())
802803
}
803804

804805
async fn get_graph(&self) -> Result<Graph, LightningError> {
@@ -1965,31 +1966,16 @@ mod tests {
19651966
.await
19661967
.unwrap();
19671968

1968-
let node_1_channels = nodes
1969-
.get(&pk1)
1970-
.unwrap()
1971-
.lock()
1972-
.await
1973-
.list_channels()
1974-
.await
1975-
.unwrap();
1969+
let node_1 = nodes.get(&pk1).unwrap().lock().await;
1970+
let node_1_capacity = node_1.channel_capacities().await.unwrap();
19761971

19771972
// Node 1 has 2 channels but one was excluded so here we should only have the one that was
19781973
// not excluded.
1979-
assert!(node_1_channels.len() == 1);
1980-
assert!(node_1_channels[0] == capacity_1);
1981-
1982-
let node_2_channels = nodes
1983-
.get(&pk2)
1984-
.unwrap()
1985-
.lock()
1986-
.await
1987-
.list_channels()
1988-
.await
1989-
.unwrap();
1974+
assert!(node_1_capacity == capacity_1);
19901975

1991-
assert!(node_2_channels.len() == 1);
1992-
assert!(node_2_channels[0] == capacity_1);
1976+
let node_2 = nodes.get(&pk2).unwrap().lock().await;
1977+
let node_2_capacity = node_2.channel_capacities().await.unwrap();
1978+
assert!(node_2_capacity == capacity_1);
19931979

19941980
// Node 3's only channel was excluded so it won't be present here.
19951981
assert!(!nodes.contains_key(&pk3));
@@ -2103,12 +2089,17 @@ mod tests {
21032089
.lock()
21042090
.await
21052091
.expect_lookup_node()
2106-
.returning(move |_| Ok((node_info(lookup_pk, String::default()), vec![1, 2, 3])));
2092+
.returning(move |_| {
2093+
Ok((
2094+
node_info(lookup_pk, String::default()),
2095+
vec![10_000, 20_000, 10_000],
2096+
))
2097+
});
21072098

21082099
// Assert that we get three channels from the mock.
21092100
let node_info = node.get_node_info(&lookup_pk).await.unwrap();
21102101
assert_eq!(lookup_pk, node_info.pubkey);
2111-
assert_eq!(node.list_channels().await.unwrap().len(), 3);
2102+
assert_eq!(node.channel_capacities().await.unwrap(), 40_000);
21122103

21132104
// Next, we're going to test handling of in-flight payments. To do this, we'll mock out calls to our dispatch
21142105
// function to send different results depending on the destination.

simln-lib/src/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ mock! {
8989
shutdown: triggered::Listener,
9090
) -> Result<crate::PaymentResult, LightningError>;
9191
async fn get_node_info(&self, node_id: &PublicKey) -> Result<NodeInfo, LightningError>;
92-
async fn list_channels(&self) -> Result<Vec<u64>, LightningError>;
92+
async fn channel_capacities(&self) -> Result<u64, LightningError>;
9393
async fn get_graph(&self) -> Result<Graph, LightningError>;
9494
}
9595
}

0 commit comments

Comments
 (0)