Skip to content

Commit 4e326c9

Browse files
committed
simlib: return Network instead of Result from get_network
When get_network was called, for most node impls it would call the get info method to get the network. Instead, since the network shouldn't change, store it when instantiating the node and return it from get_network instead of returning a Result.
1 parent 1fdcc84 commit 4e326c9

File tree

6 files changed

+56
-76
lines changed

6 files changed

+56
-76
lines changed

simln-lib/src/cln.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub struct ClnConnection {
4040
pub struct ClnNode {
4141
pub client: Mutex<NodeClient<Channel>>,
4242
info: NodeInfo,
43+
network: Network,
4344
}
4445

4546
impl ClnNode {
@@ -83,38 +84,35 @@ impl ClnNode {
8384
})?,
8485
));
8586

86-
let (id, mut alias, our_features) = client
87+
let info = client
8788
.lock()
8889
.await
8990
.getinfo(GetinfoRequest {})
9091
.await
91-
.map(|r| {
92-
let inner = r.into_inner();
93-
(
94-
inner.id,
95-
inner.alias.unwrap_or_default(),
96-
inner.our_features,
97-
)
98-
})
99-
.map_err(|err| LightningError::GetInfoError(err.to_string()))?;
92+
.map_err(|err| LightningError::GetInfoError(err.to_string()))?
93+
.into_inner();
10094

101-
let pubkey = PublicKey::from_slice(&id)
95+
let pubkey = PublicKey::from_slice(&info.id)
10296
.map_err(|err| LightningError::GetInfoError(err.to_string()))?;
97+
let mut alias = info.alias.unwrap_or_default();
10398
connection.id.validate(&pubkey, &mut alias)?;
10499

105-
let features = if let Some(features) = our_features {
106-
NodeFeatures::from_be_bytes(features.node)
107-
} else {
108-
NodeFeatures::empty()
100+
let features = match info.our_features {
101+
Some(features) => NodeFeatures::from_be_bytes(features.node),
102+
None => NodeFeatures::empty(),
109103
};
110104

105+
let network = Network::from_core_arg(&info.network)
106+
.map_err(|err| LightningError::GetInfoError(err.to_string()))?;
107+
111108
Ok(Self {
112109
client,
113110
info: NodeInfo {
114111
pubkey,
115112
features,
116113
alias,
117114
},
115+
network,
118116
})
119117
}
120118

@@ -157,16 +155,8 @@ impl LightningNode for ClnNode {
157155
&self.info
158156
}
159157

160-
async fn get_network(&self) -> Result<Network, LightningError> {
161-
let mut client = self.client.lock().await;
162-
let info = client
163-
.getinfo(GetinfoRequest {})
164-
.await
165-
.map_err(|err| LightningError::GetInfoError(err.to_string()))?
166-
.into_inner();
167-
168-
Ok(Network::from_core_arg(&info.network)
169-
.map_err(|err| LightningError::ValidationError(err.to_string()))?)
158+
fn get_network(&self) -> Network {
159+
self.network
170160
}
171161

172162
async fn send_payment(

simln-lib/src/eclair.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ impl LightningNode for EclairNode {
128128
&self.info
129129
}
130130

131-
async fn get_network(&self) -> Result<Network, LightningError> {
132-
Ok(self.network)
131+
fn get_network(&self) -> Network {
132+
self.network
133133
}
134134

135135
async fn send_payment(

simln-lib/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ 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(&self) -> Result<Network, LightningError>;
327+
fn get_network(&self) -> Network;
328328
/// Keysend payment worth `amount_msat` from a source node to the destination node.
329329
async fn send_payment(
330330
&self,
@@ -709,7 +709,7 @@ impl<C: Clock + 'static> Simulation<C> {
709709
let mut running_network = Option::None;
710710

711711
for node in self.nodes.values() {
712-
let network = node.lock().await.get_network().await?;
712+
let network = node.lock().await.get_network();
713713
if network == Network::Bitcoin {
714714
return Err(LightningError::ValidationError(
715715
"mainnet is not supported".to_string(),

simln-lib/src/lnd.rs

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use lightning::ln::features::NodeFeatures;
1313
use lightning::ln::{PaymentHash, PaymentPreimage};
1414
use serde::{Deserialize, Serialize};
1515
use tokio::sync::Mutex;
16-
use tonic_lnd::lnrpc::{payment::PaymentStatus, GetInfoRequest, GetInfoResponse};
16+
use tonic_lnd::lnrpc::{payment::PaymentStatus, GetInfoRequest};
1717
use tonic_lnd::lnrpc::{
1818
ChannelGraphRequest, ListChannelsRequest, NodeInfoRequest, PaymentFailureReason,
1919
};
@@ -41,6 +41,7 @@ pub struct LndConnection {
4141
pub struct LndNode {
4242
client: Mutex<Client>,
4343
info: NodeInfo,
44+
network: Network,
4445
}
4546

4647
// TODO: We could even generalize this to parse any type of Features
@@ -68,29 +69,50 @@ impl LndNode {
6869
.await
6970
.map_err(|err| LightningError::ConnectionError(err.to_string()))?;
7071

71-
let GetInfoResponse {
72-
identity_pubkey,
73-
features,
74-
mut alias,
75-
..
76-
} = client
72+
let info = client
7773
.lightning()
7874
.get_info(GetInfoRequest {})
7975
.await
8076
.map_err(|err| LightningError::GetInfoError(err.to_string()))?
8177
.into_inner();
8278

83-
let pubkey = PublicKey::from_str(&identity_pubkey)
79+
let mut alias = info.alias;
80+
let pubkey = PublicKey::from_str(&info.identity_pubkey)
8481
.map_err(|err| LightningError::GetInfoError(err.to_string()))?;
8582
connection.id.validate(&pubkey, &mut alias)?;
8683

84+
let network = {
85+
if info.chains.is_empty() {
86+
return Err(LightningError::GetInfoError(
87+
"node is not connected any chain".to_string(),
88+
));
89+
} else if info.chains.len() > 1 {
90+
return Err(LightningError::GetInfoError(format!(
91+
"node is connected to more than one chain: {:?}",
92+
info.chains.iter().map(|c| c.chain.to_string())
93+
)));
94+
}
95+
96+
Network::from_str(match info.chains[0].network.as_str() {
97+
"mainnet" => "bitcoin",
98+
"simnet" => {
99+
return Err(LightningError::ValidationError(
100+
"simnet is not supported".to_string(),
101+
))
102+
},
103+
x => x,
104+
})
105+
.map_err(|e| LightningError::GetInfoError(e.to_string()))?
106+
};
107+
87108
Ok(Self {
88109
client: Mutex::new(client),
89110
info: NodeInfo {
90111
pubkey,
91-
features: parse_node_features(features.keys().cloned().collect()),
112+
features: parse_node_features(info.features.keys().cloned().collect()),
92113
alias,
93114
},
115+
network,
94116
})
95117
}
96118
}
@@ -104,38 +126,8 @@ impl LightningNode for LndNode {
104126
&self.info
105127
}
106128

107-
async fn get_network(&self) -> Result<Network, LightningError> {
108-
let mut client = self.client.lock().await;
109-
let info = client
110-
.lightning()
111-
.get_info(GetInfoRequest {})
112-
.await
113-
.map_err(|err| LightningError::GetInfoError(err.to_string()))?
114-
.into_inner();
115-
116-
if info.chains.is_empty() {
117-
return Err(LightningError::ValidationError(format!(
118-
"{} is not connected any chain",
119-
self.get_info()
120-
)));
121-
} else if info.chains.len() > 1 {
122-
return Err(LightningError::ValidationError(format!(
123-
"{} is connected to more than one chain: {:?}",
124-
self.get_info(),
125-
info.chains.iter().map(|c| c.chain.to_string())
126-
)));
127-
}
128-
129-
Ok(Network::from_str(match info.chains[0].network.as_str() {
130-
"mainnet" => "bitcoin",
131-
"simnet" => {
132-
return Err(LightningError::ValidationError(
133-
"simnet is not supported".to_string(),
134-
))
135-
},
136-
x => x,
137-
})
138-
.map_err(|err| LightningError::ValidationError(err.to_string()))?)
129+
fn get_network(&self) -> Network {
130+
self.network
139131
}
140132

141133
async fn send_payment(

simln-lib/src/sim_node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,8 @@ impl<T: SimNetwork> LightningNode for SimNode<T> {
622622
&self.info
623623
}
624624

625-
async fn get_network(&self) -> Result<Network, LightningError> {
626-
Ok(Network::Regtest)
625+
fn get_network(&self) -> Network {
626+
Network::Regtest
627627
}
628628

629629
/// send_payment picks a random preimage for a payment, dispatches it in the network and adds a tracking channel

simln-lib/src/test_utils.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mock! {
7676
#[async_trait]
7777
impl crate::LightningNode for LightningNode {
7878
fn get_info(&self) -> &NodeInfo;
79-
async fn get_network(&self) -> Result<bitcoin::Network, LightningError>;
79+
fn get_network(&self) -> bitcoin::Network;
8080
async fn send_payment(
8181
&self,
8282
dest: bitcoin::secp256k1::PublicKey,
@@ -182,9 +182,7 @@ impl LightningTestNodeBuilder {
182182

183183
if let Some(networks) = &self.networks {
184184
let network = networks[idx];
185-
mock_node
186-
.expect_get_network()
187-
.returning(move || Ok(network));
185+
mock_node.expect_get_network().return_const(network);
188186
}
189187

190188
clients.insert(node_info.pubkey, Arc::new(Mutex::new(mock_node)));

0 commit comments

Comments
 (0)