Skip to content

Commit 9eedadb

Browse files
authored
Merge pull request #282 from elnosh/alias
add alias to simulated network
2 parents de92e1f + 98b6dbc commit 9eedadb

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

simln-lib/src/sim_node.rs

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ struct Htlc {
118118
#[derive(Debug, Clone, Serialize, Deserialize)]
119119
pub struct ChannelPolicy {
120120
pub pubkey: PublicKey,
121+
#[serde(default)]
122+
pub alias: String,
121123
pub max_htlc_count: u64,
122124
pub max_in_flight_msat: u64,
123125
pub min_htlc_size_msat: u64,
@@ -459,8 +461,8 @@ impl SimulatedChannel {
459461

460462
pub fn create_simulated_nodes(&self) -> (NodeInfo, NodeInfo) {
461463
(
462-
node_info(self.node_1.policy.pubkey),
463-
node_info(self.node_2.policy.pubkey),
464+
node_info(self.node_1.policy.pubkey, self.node_1.policy.alias.clone()),
465+
node_info(self.node_2.policy.pubkey, self.node_2.policy.alias.clone()),
464466
)
465467
}
466468
}
@@ -481,7 +483,7 @@ pub trait SimNetwork: Send + Sync {
481483
/// Looks up a node in the simulated network and a list of its channel capacities.
482484
fn lookup_node(&self, node: &PublicKey) -> Result<(NodeInfo, Vec<u64>), LightningError>;
483485
/// Lists all nodes in the simulated network.
484-
fn list_nodes(&self) -> Result<Vec<NodeInfo>, LightningError>;
486+
fn list_nodes(&self) -> Vec<NodeInfo>;
485487
}
486488

487489
/// A wrapper struct used to implement the LightningNode trait (can be thought of as "the" lightning node). Passes
@@ -505,7 +507,7 @@ impl<'a, T: SimNetwork> SimNode<'a, T> {
505507
/// Creates a new simulation node that refers to the high level network coordinator provided to process payments
506508
/// on its behalf. The pathfinding graph is provided separately so that each node can handle its own pathfinding.
507509
pub fn new(
508-
pubkey: PublicKey,
510+
info: NodeInfo,
509511
payment_network: Arc<Mutex<T>>,
510512
pathfinding_graph: Arc<NetworkGraph<&'a WrappedLog>>,
511513
) -> Self {
@@ -519,7 +521,7 @@ impl<'a, T: SimNetwork> SimNode<'a, T> {
519521
);
520522

521523
SimNode {
522-
info: node_info(pubkey),
524+
info,
523525
network: payment_network,
524526
in_flight: HashMap::new(),
525527
pathfinding_graph,
@@ -560,14 +562,14 @@ impl<'a, T: SimNetwork> SimNode<'a, T> {
560562
}
561563

562564
/// Produces the node info for a mocked node, filling in the features that the simulator requires.
563-
fn node_info(pubkey: PublicKey) -> NodeInfo {
565+
fn node_info(pubkey: PublicKey, alias: String) -> NodeInfo {
564566
// Set any features that the simulator requires here.
565567
let mut features = NodeFeatures::empty();
566568
features.set_keysend_optional();
567569

568570
NodeInfo {
569571
pubkey,
570-
alias: "".to_string(), // TODO: store alias?
572+
alias,
571573
features,
572574
}
573575
}
@@ -710,19 +712,11 @@ impl<T: SimNetwork> LightningNode for SimNode<'_, T> {
710712
}
711713

712714
async fn get_graph(&mut self) -> Result<Graph, LightningError> {
713-
let nodes = self.network.lock().await.list_nodes()?;
715+
let nodes = self.network.lock().await.list_nodes();
714716

715717
let mut nodes_by_pk = HashMap::new();
716-
717718
for node in nodes {
718-
nodes_by_pk.insert(
719-
node.pubkey,
720-
NodeInfo {
721-
pubkey: node.pubkey,
722-
alias: node.alias.clone(),
723-
features: node.features,
724-
},
725-
);
719+
nodes_by_pk.insert(node.pubkey, node);
726720
}
727721

728722
Ok(Graph { nodes_by_pk })
@@ -935,7 +929,7 @@ async fn handle_intercepted_htlc(
935929
pub struct SimGraph {
936930
/// nodes caches the list of nodes in the network with a vector of their channel capacities, only used for quick
937931
/// lookup.
938-
nodes: HashMap<PublicKey, Vec<u64>>,
932+
nodes: HashMap<PublicKey, (NodeInfo, Vec<u64>)>,
939933

940934
/// channels maps the scid of a channel to its current simulation state.
941935
channels: Arc<Mutex<HashMap<ShortChannelID, SimulatedChannel>>>,
@@ -967,7 +961,7 @@ impl SimGraph {
967961
default_custom_records: CustomRecords,
968962
shutdown_signal: (Trigger, Listener),
969963
) -> Result<Self, SimulationError> {
970-
let mut nodes: HashMap<PublicKey, Vec<u64>> = HashMap::new();
964+
let mut nodes: HashMap<PublicKey, (NodeInfo, Vec<u64>)> = HashMap::new();
971965
let mut channels = HashMap::new();
972966

973967
for channel in graph_channels.iter() {
@@ -985,11 +979,14 @@ impl SimGraph {
985979
};
986980

987981
// It's okay to have duplicate pubkeys because one node can have many channels.
988-
for pubkey in [channel.node_1.policy.pubkey, channel.node_2.policy.pubkey] {
989-
match nodes.entry(pubkey) {
990-
Entry::Occupied(o) => o.into_mut().push(channel.capacity_msat),
982+
for info in [&channel.node_1.policy, &channel.node_2.policy] {
983+
match nodes.entry(info.pubkey) {
984+
Entry::Occupied(o) => o.into_mut().1.push(channel.capacity_msat),
991985
Entry::Vacant(v) => {
992-
v.insert(vec![channel.capacity_msat]);
986+
v.insert((
987+
node_info(info.pubkey, info.alias.clone()),
988+
vec![channel.capacity_msat],
989+
));
993990
},
994991
}
995992
}
@@ -1013,11 +1010,11 @@ pub async fn ln_node_from_graph(
10131010
) -> HashMap<PublicKey, Arc<Mutex<dyn LightningNode + '_>>> {
10141011
let mut nodes: HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>> = HashMap::new();
10151012

1016-
for pk in graph.lock().await.nodes.keys() {
1013+
for node in graph.lock().await.nodes.iter() {
10171014
nodes.insert(
1018-
*pk,
1015+
*node.0,
10191016
Arc::new(Mutex::new(SimNode::new(
1020-
*pk,
1017+
node.1 .0.clone(),
10211018
graph.clone(),
10221019
routing_graph.clone(),
10231020
))),
@@ -1134,22 +1131,20 @@ impl SimNetwork for SimGraph {
11341131

11351132
/// lookup_node fetches a node's information and channel capacities.
11361133
fn lookup_node(&self, node: &PublicKey) -> Result<(NodeInfo, Vec<u64>), LightningError> {
1137-
self.nodes
1138-
.get(node)
1139-
.map(|channels| (node_info(*node), channels.clone()))
1140-
.ok_or(LightningError::GetNodeInfoError(
1134+
match self.nodes.get(node) {
1135+
Some(node) => Ok(node.clone()),
1136+
None => Err(LightningError::GetNodeInfoError(
11411137
"Node not found".to_string(),
1142-
))
1138+
)),
1139+
}
11431140
}
11441141

1145-
fn list_nodes(&self) -> Result<Vec<NodeInfo>, LightningError> {
1146-
let mut nodes = vec![];
1147-
1142+
fn list_nodes(&self) -> Vec<NodeInfo> {
1143+
let mut nodes = Vec::with_capacity(self.nodes.len());
11481144
for node in &self.nodes {
1149-
nodes.push(node_info(*node.0));
1145+
nodes.push(node.1 .0.clone());
11501146
}
1151-
1152-
Ok(nodes)
1147+
nodes
11531148
}
11541149
}
11551150

@@ -1516,6 +1511,7 @@ mod tests {
15161511
let (_, pk) = get_random_keypair();
15171512
ChannelPolicy {
15181513
pubkey: pk,
1514+
alias: String::default(),
15191515
max_htlc_count: 10,
15201516
max_in_flight_msat,
15211517
min_htlc_size_msat: 2,
@@ -1543,6 +1539,7 @@ mod tests {
15431539

15441540
let node_1_to_2 = ChannelPolicy {
15451541
pubkey: node_1,
1542+
alias: String::default(),
15461543
max_htlc_count: 483,
15471544
max_in_flight_msat: capacity_msat / 2,
15481545
min_htlc_size_msat: 1,
@@ -1554,6 +1551,7 @@ mod tests {
15541551

15551552
let node_2_to_1 = ChannelPolicy {
15561553
pubkey: node_2,
1554+
alias: String::default(),
15571555
max_htlc_count: 483,
15581556
max_in_flight_msat: capacity_msat / 2,
15591557
min_htlc_size_msat: 1,
@@ -1881,7 +1879,7 @@ mod tests {
18811879
);
18821880

18831881
fn lookup_node(&self, node: &PublicKey) -> Result<(NodeInfo, Vec<u64>), LightningError>;
1884-
fn list_nodes(&self) -> Result<Vec<NodeInfo>, LightningError>;
1882+
fn list_nodes(&self) -> Vec<NodeInfo>;
18851883
}
18861884
}
18871885

@@ -1897,15 +1895,19 @@ mod tests {
18971895

18981896
// Create a simulated node for the first channel in our network.
18991897
let pk = channels[0].node_1.policy.pubkey;
1900-
let mut node = SimNode::new(pk, sim_network.clone(), Arc::new(graph));
1898+
let mut node = SimNode::new(
1899+
node_info(pk, String::default()),
1900+
sim_network.clone(),
1901+
Arc::new(graph),
1902+
);
19011903

19021904
// Prime mock to return node info from lookup and assert that we get the pubkey we're expecting.
19031905
let lookup_pk = channels[3].node_1.policy.pubkey;
19041906
sim_network
19051907
.lock()
19061908
.await
19071909
.expect_lookup_node()
1908-
.returning(move |_| Ok((node_info(lookup_pk), vec![1, 2, 3])));
1910+
.returning(move |_| Ok((node_info(lookup_pk, String::default()), vec![1, 2, 3])));
19091911

19101912
// Assert that we get three channels from the mock.
19111913
let node_info = node.get_node_info(&lookup_pk).await.unwrap();
@@ -2284,7 +2286,7 @@ mod tests {
22842286
DispatchPaymentTestKit::new(chan_capacity, vec![], CustomRecords::default()).await;
22852287

22862288
let mut node = SimNode::new(
2287-
test_kit.nodes[0],
2289+
node_info(test_kit.nodes[0], String::default()),
22882290
Arc::new(Mutex::new(test_kit.graph)),
22892291
test_kit.routing_graph.clone(),
22902292
);

0 commit comments

Comments
 (0)