Skip to content

Commit 87996e0

Browse files
committed
[NO MERGE]: run simulation with ring of nodes for testing
This commit is not intended for merge, it just spins up a circle of sim nodes so that we can test this functionality out. It'll be dropped before merge and replaced with the ability to properly configure your simulation (likely in a followup PR).
1 parent 5b9b6c9 commit 87996e0

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sim-cli/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ sim-lib = { path = "../sim-lib" }
2020
tokio = { version = "1.26.0", features = ["full"] }
2121
bitcoin = { version = "0.30.1" }
2222
ctrlc = "3.4.0"
23+
triggered = "0.1.2"
24+
rand = "0.8.5"

sim-cli/src/main.rs

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
use bitcoin::secp256k1::PublicKey;
1+
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
2+
use rand::distributions::Uniform;
3+
use rand::Rng;
4+
use sim_lib::sim_node::{
5+
ln_node_from_graph, populate_network_graph, ChannelPolicy, SimGraph, SimulatedChannel,
6+
};
7+
28
use std::collections::HashMap;
39
use std::path::PathBuf;
410
use std::sync::Arc;
@@ -189,7 +195,6 @@ async fn main() -> anyhow::Result<()> {
189195
amount_msat: act.amount_msat,
190196
});
191197
}
192-
193198
let write_results = if !cli.no_results {
194199
Some(WriteResults {
195200
results_dir: mkdir(cli.data_dir.join("results")).await?,
@@ -199,8 +204,22 @@ async fn main() -> anyhow::Result<()> {
199204
None
200205
};
201206

207+
let channels = generate_sim_nodes();
208+
209+
// TODO: use the shutdown trigger and listener across simulator and graph.
210+
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
211+
let graph = match SimGraph::new(channels.clone(), shutdown_trigger, shutdown_listener) {
212+
Ok(graph) => Arc::new(Mutex::new(graph)),
213+
Err(e) => anyhow::bail!("failed: {:?}", e),
214+
};
215+
216+
let routing_graph = match populate_network_graph(channels) {
217+
Ok(r) => r,
218+
Err(e) => anyhow::bail!("failed: {:?}", e),
219+
};
220+
202221
let sim = Simulation::new(
203-
clients,
222+
ln_node_from_graph(graph, Arc::new(routing_graph)).await,
204223
validated_activities,
205224
cli.total_time,
206225
cli.expected_pmt_amt,
@@ -219,6 +238,84 @@ async fn main() -> anyhow::Result<()> {
219238
Ok(())
220239
}
221240

241+
fn generate_sim_nodes() -> Vec<SimulatedChannel> {
242+
let capacity = 300000000;
243+
let mut channels: Vec<SimulatedChannel> = vec![];
244+
let (_, first_node) = get_random_keypair();
245+
246+
// Create channels in a ring so that we'll get long payment paths.
247+
let mut node_1 = first_node;
248+
for i in 0..10 {
249+
// Create a new node that we'll create a channel with. If we're on the last node in the circle, we'll loop
250+
// back around to the first node to close it.
251+
let node_2 = if i == 10 {
252+
first_node
253+
} else {
254+
let (_, pk) = get_random_keypair();
255+
pk
256+
};
257+
258+
let node_1_to_2 = ChannelPolicy {
259+
pubkey: node_1,
260+
max_htlc_count: 483,
261+
max_in_flight_msat: capacity / 2,
262+
min_htlc_size_msat: 1,
263+
max_htlc_size_msat: capacity / 2,
264+
cltv_expiry_delta: 40,
265+
// Alter fee rate a little for different values.
266+
base_fee: 1000 * i,
267+
fee_rate_prop: 1500 * i,
268+
};
269+
270+
let node_2_to_1 = ChannelPolicy {
271+
pubkey: node_2,
272+
max_htlc_count: 483,
273+
max_in_flight_msat: capacity / 2,
274+
min_htlc_size_msat: 1,
275+
max_htlc_size_msat: capacity / 2,
276+
cltv_expiry_delta: 40 + 10 * i as u32,
277+
// Alter fee rate a little for different values.
278+
base_fee: 2000 * i,
279+
fee_rate_prop: i,
280+
};
281+
282+
channels.push(SimulatedChannel::new(
283+
capacity,
284+
// Unique channel ID per link.
285+
100 + i,
286+
node_1_to_2,
287+
node_2_to_1,
288+
));
289+
290+
// Once we've created this link in the circle, progress our current node to be node_1 so that we can generate
291+
// a new edge.
292+
node_1 = node_2;
293+
}
294+
295+
channels
296+
}
297+
298+
/// COPIED from test utils!
299+
pub fn get_random_bytes(size: usize) -> Vec<u8> {
300+
rand::thread_rng()
301+
.sample_iter(Uniform::new(u8::MIN, u8::MAX))
302+
.take(size)
303+
.collect()
304+
}
305+
306+
pub fn get_random_int(s: u64, e: u64) -> u64 {
307+
rand::thread_rng().gen_range(s..e)
308+
}
309+
310+
pub fn get_random_keypair() -> (SecretKey, PublicKey) {
311+
loop {
312+
if let Ok(sk) = SecretKey::from_slice(&get_random_bytes(32)) {
313+
return (sk, PublicKey::from_secret_key(&Secp256k1::new(), &sk));
314+
}
315+
}
316+
}
317+
/// COPIED from test utils!
318+
222319
async fn read_sim_path(data_dir: PathBuf, sim_file: PathBuf) -> anyhow::Result<PathBuf> {
223320
let sim_path = if sim_file.is_relative() {
224321
data_dir.join(sim_file)

0 commit comments

Comments
 (0)