Skip to content

Commit

Permalink
Merge pull request #15 from bjohnson5/cln-model
Browse files Browse the repository at this point in the history
CLN Model
  • Loading branch information
bjohnson5 authored Dec 6, 2024
2 parents c783ab2 + 0edd945 commit 1d91b24
Show file tree
Hide file tree
Showing 27 changed files with 1,201 additions and 194 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ jobs:
name: Build
runs-on: ubuntu-latest
steps:
- name: Install Dependencies
run: |
sudo apt-get install -y jq autoconf automake build-essential git libtool libsqlite3-dev python3 python3-pip net-tools zlib1g-dev libsodium-dev gettext
- name: Install Python Dependencies
run: |
pip3 install --upgrade pip
pip3 install mako
pip3 install grpcio-tools
- name: Install Protoc
uses: arduino/setup-protoc@v3
- name: Installing protoc-gen-go
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ sudo install -m 0755 -o root -g root -t /usr/local/bin bitcoin-25.0/bin/*

### Install utils
```bash
sudo apt install bc
sudo apt install jq
sudo apt-get install bc
sudo apt-get install jq
sudo apt-get install -y jq autoconf automake build-essential git libtool libsqlite3-dev python3 python3-pip net-tools zlib1g-dev libsodium-dev gettext
pip3 install --upgrade pip
pip3 install mako
pip3 install grpcio-tools
```

### Install Rust
Expand Down
4 changes: 2 additions & 2 deletions blast_cli/src/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ impl BlastTab for ConfigureTab {
"start".bold(),
" command to start simulation, ".into(),
"help".bold(),
" command to show commands,".into(),
"Tab".bold(),
" command to show commands, ".into(),
"tab".bold(),
" to change sections".into()
];
let text = Text::from(Line::from(msg)).patch_style(Style::default());
Expand Down
6 changes: 3 additions & 3 deletions blast_cli/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ impl BlastTab for LoadTab {
let msg = vec![
"Press ".into(),
"q".bold(),
" to exit, ".into(),
"Enter".bold(),
" to quit, ".into(),
"enter".bold(),
" to load simulation, ".into(),
"Esc".bold(),
"esc".bold(),
" to change tabs".into()
];
let text = Text::from(Line::from(msg)).patch_style(Style::default());
Expand Down
11 changes: 7 additions & 4 deletions blast_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use simplelog::WriteLogger;
use simplelog::Config;
use log::LevelFilter;
use tokio::task::JoinSet;
use anyhow::Error as AnyError;

// TUI libraries
use ratatui::{
Expand Down Expand Up @@ -88,7 +87,7 @@ async fn run<B: Backend>(terminal: &mut Terminal<B>, mut blast_cli: BlastCli) ->
let mut error: Option<String> = None;
let running = Arc::new(AtomicBool::new(true));
let mut running_models: Vec<Child> = Vec::new();
let mut sim_tasks: Option<JoinSet<Result<(), AnyError>>> = None;
let mut sim_tasks: Option<JoinSet<()>> = None;

loop {
// Draw the frame
Expand Down Expand Up @@ -423,7 +422,7 @@ async fn run_command(blast: &mut blast_core::Blast, cmd: String) -> Vec<String>
output.push(String::from("close_channel source_node channel_id"));
output.push(String::from("connect_peer source_node dest_node"));
output.push(String::from("disconnect_peer source_node dest_node"));
output.push(String::from("fund_node source_node"));
output.push(String::from("fund_node source_node amount_btc"));
}
"save" => {
match blast.save(words.next().unwrap_or("simulation1")).await {
Expand Down Expand Up @@ -599,7 +598,11 @@ async fn run_command(blast: &mut blast_core::Blast, cmd: String) -> Vec<String>
},
"fund_node" => {
let source = String::from(words.next().unwrap_or(""));
match blast.fund_node(source, true).await {
let amount = match words.next().unwrap_or("1.0").parse::<f64>() {
Ok(value) => { value },
Err(_) => { 1.0 }
};
match blast.fund_node(source, amount, true).await {
Ok(_) => {},
Err(e) => {
let msg = format!("Unable to fund node: {}", e);
Expand Down
6 changes: 3 additions & 3 deletions blast_cli/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ impl BlastTab for NewTab {
let msg = vec![
"Press ".into(),
"q".bold(),
" to exit, ".into(),
"Enter".bold(),
" to quit, ".into(),
"enter".bold(),
" to start network, ".into(),
"Esc".bold(),
"esc".bold(),
" to change tabs".into()
];
let text = Text::from(Line::from(msg)).patch_style(Style::default());
Expand Down
2 changes: 1 addition & 1 deletion blast_cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl BlastTab for RunTab {
"Press ".into(),
"s".bold(),
" to stop sim, ".into(),
"Tab".bold(),
"tab".bold(),
" to change sections".into()
];
let text = Text::from(Line::from(msg)).patch_style(Style::default());
Expand Down
2 changes: 1 addition & 1 deletion blast_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
bitcoin = { version = "0.30.1" }
sim-lib = { git = "https://github.com/bjohnson5/sim-ln", branch = "main" }
simln-lib = { git = "https://github.com/bjohnson5/sim-ln", branch = "208-feature-bits-fix" }
tokio = { version = "1.37.0", features = ["full"] }
serde = { version = "1.0.104", features = ["derive"] }
serde_json = "1.0.104"
Expand Down
18 changes: 9 additions & 9 deletions blast_core/src/blast_model_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ impl BlastModelManager {
// Get the current working directory
let mut current_dir = match env::current_dir() {
Ok(d) => d,
Err(_) => {
return Err(String::from("Failed to get the current directory"));
Err(e) => {
return Err(format!("Failed to get the current directory: {:?}", e));
}
};

Expand Down Expand Up @@ -185,12 +185,12 @@ impl BlastModelManager {
// Create a stop request
let request = tonic::Request::new(BlastStopModelRequest {
});

// Execute the stop RPC
let response = match client.stop_model(request).await {
Ok(r) => r,
Err(_) => {
return Err(String::from("RPC stop_model failed"));
Err(e) => {
return Err(format!("RPC stop_model failed: {:?}", e));
}
};

Expand Down Expand Up @@ -277,8 +277,8 @@ impl BlastModelManager {
// Execute the start RPC
let response = match client.start_nodes(request).await {
Ok(r) => r,
Err(_) => {
return Err(String::from("RPC start nodes failed"));
Err(e) => {
return Err(format!("RPC start nodes failed: {:?}", e));
}
};

Expand All @@ -288,8 +288,8 @@ impl BlastModelManager {
let request = tonic::Request::new(BlastSimlnRequest {});
let response = match client.get_sim_ln(request).await {
Ok(r) => r,
Err(_) => {
return Err(String::from("RPC get_sim_ln failed"));
Err(e) => {
return Err(format!("RPC get_sim_ln failed: {:?}", e));
}
};
match std::str::from_utf8(&response.get_ref().simln_data) {
Expand Down
41 changes: 24 additions & 17 deletions blast_core/src/blast_simln_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use std::env;

// Extra dependencies
use serde::{Serialize, Deserialize};
use sim_lib::ActivityDefinition;
use sim_lib::Simulation;
use sim_lib::LightningNode;
use sim_lib::SimParams;
use sim_lib::*;
use sim_lib::lnd::*;
use sim_lib::cln::*;
use simln_lib::ActivityDefinition;
use simln_lib::Simulation;
use simln_lib::LightningNode;
use simln_lib::SimParams;
use simln_lib::*;
use simln_lib::lnd::*;
use simln_lib::cln::*;
use anyhow::{anyhow, Error};
use bitcoin::secp256k1::PublicKey;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -211,16 +211,18 @@ impl BlastSimLnManager {
let folder_path = PathBuf::from(home).join(RESULTS_DIR);

let sim = Simulation::new(
SimulationCfg::new(
Some(crate::TOTAL_FRAMES as u32),
EXPECTED_PAYMENT_AMOUNT,
ACTIVITY_MULTIPLIER,
Some(WriteResults {
results_dir: folder_path,
batch_size: 1,
}),
None
),
clients,
validated_activities,
Some(crate::TOTAL_FRAMES as u32),
EXPECTED_PAYMENT_AMOUNT,
ACTIVITY_MULTIPLIER,
Some(WriteResults {
results_dir: folder_path,
batch_size: 1,
}),
None
);
self.sim = Some(sim);
Ok(())
Expand All @@ -242,7 +244,12 @@ impl BlastSimLnManager {
// Start the sim-ln simulation
match &self.sim {
Some(s) => {
s.run().await?;
match s.run().await {
Ok(_) => {},
Err(e) => {
return Err(anyhow!("Error starting simulation: {:?}", e));
}
}
Ok(())
},
None => return Err(anyhow!("Simln not setup. Call set_simln before starting the simulation")),
Expand Down Expand Up @@ -271,7 +278,7 @@ impl BlastSimLnManager {
for n in &self.data.nodes {
let id = match n {
NodeConnection::LND(c) => c.id.to_string(),
NodeConnection::CLN(_) => String::from(""),
NodeConnection::CLN(c) => c.id.to_string(),
};
ids.push(id);
}
Expand Down
34 changes: 27 additions & 7 deletions blast_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::env;
// Extra dependencies
use bitcoincore_rpc::Auth;
use bitcoincore_rpc::RpcApi;
use anyhow::Error;
use bitcoincore_rpc::Client;
use tokio::task::JoinSet;
use tokio::sync::mpsc;
Expand Down Expand Up @@ -171,7 +170,7 @@ impl Blast {
}

/// Start the simulation -- this will start the simulation events and the simln transaction generation
pub async fn start_simulation(&mut self) -> Result<JoinSet<Result<(),Error>>, String> {
pub async fn start_simulation(&mut self) -> Result<JoinSet<()>, String> {
let net = match &self.network {
Some(n) => n,
None => return Err(format!("No network found")),
Expand All @@ -190,17 +189,32 @@ impl Blast {

// Start the simln thread
sim_tasks.spawn(async move {
simln_man.start().await
match simln_man.start().await {
Ok(_) => {},
Err(e) => {
log::error!("Error running simln: {:?}", e);
}
};
});

// Start the event thread
sim_tasks.spawn(async move {
event_man.start(sender).await
match event_man.start(sender).await {
Ok(_) => {},
Err(e) => {
log::error!("Error running event thread: {:?}", e);
}
};
});

// Start the model manager thread
sim_tasks.spawn(async move {
model_man.process_events(receiver).await
match model_man.process_events(receiver).await {
Ok(_) => {},
Err(e) => {
log::error!("Error running model thread: {:?}", e);
}
};
});

Ok(sim_tasks)
Expand Down Expand Up @@ -539,12 +553,18 @@ impl Blast {
}

/// Send funds to a node on-chain and optionally mines blocks to confirm that payment
pub async fn fund_node(&mut self, node_id: String, confirm: bool) -> Result<String, String> {
pub async fn fund_node(&mut self, node_id: String, amt_btc: f64, confirm: bool) -> Result<String, String> {
match self.blast_model_manager.get_btc_address(node_id).await {
Ok(a) => {
let address = bitcoincore_rpc::bitcoin::Address::from_str(&a).map_err(|e|e.to_string())?
.require_network(bitcoincore_rpc::bitcoin::Network::Regtest).map_err(|e|e.to_string())?;
let txid = self.bitcoin_rpc.as_mut().unwrap().send_to_address(&address, bitcoincore_rpc::bitcoin::Amount::ONE_BTC, None, None, None, None, None, None)
let amt = match bitcoincore_rpc::bitcoin::Amount::from_btc(amt_btc) {
Ok(a) => a,
Err(e) => {
return Err(format!("Error getting funding amount: {}", e));
}
};
let txid = self.bitcoin_rpc.as_mut().unwrap().send_to_address(&address, amt, None, None, None, None, None, None)
.map_err(|e| e.to_string())?;

if confirm {
Expand Down
4 changes: 2 additions & 2 deletions blast_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ async fn new_simulation() {

println!("----------------------------------------------- FUND / CONNECT NODES -----------------------------------------------");

match blast.fund_node(String::from("blast_lnd-0000"), true).await {
match blast.fund_node(String::from("blast_lnd-0000"), 1.0, true).await {
Ok(_) => {},
Err(e) => {
println!("{}", format!("Unable to fund node: {}", e));
}
}

match blast.fund_node(String::from("blast_lnd-0001"), true).await {
match blast.fund_node(String::from("blast_lnd-0001"), 1.0, true).await {
Ok(_) => {},
Err(e) => {
println!("{}", format!("Unable to fund node: {}", e));
Expand Down
3 changes: 3 additions & 0 deletions blast_models/blast_cln/blast_cln/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
blast_data
Cargo.lock
20 changes: 20 additions & 0 deletions blast_models/blast_cln/blast_cln/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "blast_cln"
version = "0.1.0"
edition = "2021"

[dependencies]
tonic = { version = "0.12.3", features = ["tls"] }
tokio = { version = "1.37.0", features = ["full"] }
prost = "0.13.3"
serde = { version = "1.0.104", features = ["derive"] }
serde_json = "1.0.104"
hex = "0.4.3"
simplelog = "0.12.2"
log = "0.4.20"
tar = "0.4"
flate2 = "1.0"

[build-dependencies]
tonic-build = "0.12.3"
prost-build = "0.13.3"
18 changes: 18 additions & 0 deletions blast_models/blast_cln/blast_cln/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::env;
use std::path::PathBuf;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut current_dir = env::current_dir()?;
current_dir.push("../../../blast_proto/blast_proto.proto");
let current_dir_string = current_dir.to_string_lossy().into_owned();
tonic_build::compile_protos(current_dir_string)?;

let mut config = prost_build::Config::new();
config.protoc_arg("--experimental_allow_proto3_optional");
let home = env::var("HOME").expect("HOME environment variable not set");
let proto_path = PathBuf::from(home.clone()).join(".blast/clightning/cln-grpc/proto/node.proto");
let folder_path = PathBuf::from(home.clone()).join(".blast/clightning/cln-grpc/proto");
tonic_build::configure().compile_protos_with_config(config, &[proto_path], &[folder_path])?;

Ok(())
}
Loading

0 comments on commit 1d91b24

Please sign in to comment.