Splitting side-node crate into lib/bin for integration tests

This commit is contained in:
Dave Hrycyszyn
2024-06-17 15:25:22 +01:00
parent 8fa0eebe2b
commit d38721e1a0
6 changed files with 113 additions and 75 deletions

View File

@@ -7,20 +7,20 @@ use serde::{Deserialize, Serialize};
#[add_crdt_fields]
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
pub(crate) struct TransactionList {
pub(crate) list: ListCrdt<Transaction>,
pub struct TransactionList {
pub list: ListCrdt<Transaction>,
}
impl TransactionList {
pub(crate) fn view_sha(&self) -> String {
pub fn view_sha(&self) -> String {
sha256::digest(serde_json::to_string(&self.list.view()).unwrap().as_bytes()).to_string()
}
}
/// A fake Transaction struct we can use as a simulated payload
#[add_crdt_fields]
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
pub(crate) struct Transaction {
#[derive(Clone, CrdtNode, Serialize, Deserialize, PartialEq)]
pub struct Transaction {
from: String,
to: String,
amount: f64,

56
side-node/src/lib.rs Normal file
View File

@@ -0,0 +1,56 @@
use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
use cli::{parse_args, Commands};
use crdt::TransactionList;
use node::SideNode;
use tokio::{sync::mpsc, task};
use websocket::WebSocketClient;
pub(crate) mod cli;
pub mod crdt;
pub(crate) mod init;
pub(crate) mod keys;
pub(crate) mod node;
pub(crate) mod stdin;
pub mod utils;
pub(crate) mod websocket;
#[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;
}
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);
let keys = keys::load_from_file(side_dir);
let crdt = BaseCrdt::<TransactionList>::new(&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
let handle = WebSocketClient::new(incoming_sender).await;
let node = SideNode::new(crdt, keys, incoming_receiver, stdin_receiver, handle);
println!("Node setup complete.");
node
}

View File

@@ -1,56 +1,5 @@
use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
use cli::{parse_args, Commands};
use crdt::TransactionList;
use node::SideNode;
use tokio::{sync::mpsc, task};
use websocket::WebSocketClient;
use side_node;
pub(crate) mod cli;
pub(crate) mod crdt;
pub(crate) mod init;
pub(crate) mod keys;
pub(crate) mod node;
pub(crate) mod stdin;
pub(crate) mod utils;
pub(crate) mod websocket;
#[tokio::main]
async fn main() {
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;
}
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);
let keys = keys::load_from_file(side_dir);
let crdt = BaseCrdt::<TransactionList>::new(&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
let handle = WebSocketClient::new(incoming_sender).await;
let node = SideNode::new(crdt, keys, incoming_receiver, stdin_receiver, handle);
println!("Node setup complete.");
node
fn main() {
side_node::run();
}

View File

@@ -36,7 +36,7 @@ impl SideNode {
loop {
match self.stdin_receiver.try_recv() {
Ok(stdin) => {
let transaction = utils::fake_transaction(stdin);
let transaction = utils::fake_transaction_json(stdin);
let json = serde_json::to_value(transaction).unwrap();
let signed_op = self.add_transaction_local(json);
self.send_to_network(signed_op).await;

View File

@@ -24,9 +24,9 @@ pub(crate) fn home(name: &String) -> std::path::PathBuf {
}
/// Generate a fake transaction with customizable from_pubkey String
pub(crate) fn fake_transaction(from_pubkey: String) -> Value {
pub fn fake_transaction_json(from: String) -> Value {
json!({
"from": from_pubkey,
"from": from,
"to": "Bob",
"amount": 1
})