Files
bft-crdt-experiment/side-node/src/lib.rs

69 lines
2.1 KiB
Rust
Raw Normal View History

use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
use cli::{parse_args, Commands};
2024-06-20 17:21:41 +01:00
use clients::websocket;
use crdt::TransactionList;
use node::SideNode;
use tokio::{sync::mpsc, task};
pub(crate) mod cli;
2024-06-20 17:21:41 +01:00
pub mod clients;
pub mod crdt;
pub(crate) mod init;
2024-06-20 17:13:34 +01:00
pub mod keys;
pub mod node;
pub(crate) mod stdin;
pub mod utils;
#[tokio::main]
pub async fn run() {
let args = parse_args();
match &args.command {
Some(Commands::Init { name }) => {
let config = init::config::SideNodeConfig {
name: name.to_string(),
};
let _ = init::init(utils::home(name), config);
}
Some(Commands::Run { name }) => {
let mut node = setup(name).await;
node.start().await;
}
2024-06-20 17:21:41 +01:00
Some(Commands::Btc {}) => {
let _ = clients::btc_esplora_client::run().await;
2024-06-20 17:21:41 +01:00
}
None => println!("No command provided. Exiting. See --help for more information."),
}
}
/// Wire everything up outside the application so we can test more easily later
async fn setup(name: &String) -> SideNode {
// First, load up the keys and create a bft-crdt
let side_dir = utils::home(name);
2024-06-20 17:13:34 +01:00
let bft_crdt_keys = keys::bft_crdt::load_from_file(&side_dir);
let bitcoin_keys = keys::bitcoin::load_from_file(&side_dir).unwrap();
let bitcoin_wallet = clients::btc_electrum_client::create_wallet(bitcoin_keys).unwrap();
2024-06-18 16:34:03 +01:00
let crdt = BaseCrdt::<TransactionList>::new(&bft_crdt_keys);
// Channels for internal communication, and a tokio task for stdin input
let (incoming_sender, incoming_receiver) = mpsc::channel::<SignedOp>(32);
let (stdin_sender, stdin_receiver) = std::sync::mpsc::channel();
task::spawn(async move {
stdin::input(stdin_sender);
});
// Finally, create the node and return it
2024-06-20 17:21:41 +01:00
let handle = websocket::Client::new(incoming_sender).await;
2024-06-18 16:34:03 +01:00
let node = SideNode::new(
crdt,
bft_crdt_keys,
2024-06-24 08:02:17 +01:00
bitcoin_wallet,
2024-06-18 16:34:03 +01:00
incoming_receiver,
stdin_receiver,
handle,
);
println!("Node setup complete.");
node
}