Almost working, I've got a blocking I/O problem with stdin now :)
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
use bft_json_crdt::json_crdt::BaseCrdt;
|
use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
|
||||||
use cli::{parse_args, Commands};
|
use cli::{parse_args, Commands};
|
||||||
use crdt::TransactionList;
|
use crdt::TransactionList;
|
||||||
use fastcrypto::ed25519::Ed25519KeyPair;
|
|
||||||
use node::SideNode;
|
use node::SideNode;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
use websocket::WebSocketClient;
|
use websocket::WebSocketClient;
|
||||||
|
|
||||||
pub(crate) mod cli;
|
pub(crate) mod cli;
|
||||||
@@ -26,20 +26,21 @@ async fn main() {
|
|||||||
let _ = init::init(utils::home(name), config);
|
let _ = init::init(utils::home(name), config);
|
||||||
}
|
}
|
||||||
Some(Commands::Run { name }) => {
|
Some(Commands::Run { name }) => {
|
||||||
let (crdt, websocket_client, keys) = setup(name);
|
let mut node = setup(name).await;
|
||||||
let side_node = &mut SideNode::new(websocket_client, crdt, keys);
|
node.start().await;
|
||||||
side_node.start().await;
|
|
||||||
}
|
}
|
||||||
None => println!("No command provided. Exiting. See --help for more information."),
|
None => println!("No command provided. Exiting. See --help for more information."),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wire everything up outside the application so we can test more easily later
|
/// Wire everything up outside the application so we can test more easily later
|
||||||
fn setup(name: &String) -> (BaseCrdt<TransactionList>, WebSocketClient, Ed25519KeyPair) {
|
async fn setup(name: &String) -> SideNode {
|
||||||
let side_dir = utils::home(name);
|
let side_dir = utils::home(name);
|
||||||
let keys = keys::load_from_file(side_dir);
|
let keys = keys::load_from_file(side_dir);
|
||||||
let websocket_client = WebSocketClient::new();
|
let (incoming_sender, incoming_receiver) = mpsc::channel::<SignedOp>(32);
|
||||||
let crdt = BaseCrdt::<TransactionList>::new(&keys);
|
let crdt = BaseCrdt::<TransactionList>::new(&keys);
|
||||||
|
let mut node = SideNode::new(crdt, keys, incoming_receiver);
|
||||||
(crdt, websocket_client, keys)
|
println!("Node setup complete.");
|
||||||
|
WebSocketClient::start(&mut node, incoming_sender).await;
|
||||||
|
node
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +1,44 @@
|
|||||||
use bft_json_crdt::json_crdt::BaseCrdt;
|
use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
|
||||||
use fastcrypto::ed25519::Ed25519KeyPair;
|
use fastcrypto::ed25519::Ed25519KeyPair;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
use crate::{crdt::TransactionList, websocket::WebSocketClient};
|
use crate::crdt::TransactionList;
|
||||||
|
|
||||||
pub(crate) struct SideNode {
|
pub(crate) struct SideNode {
|
||||||
crdt: BaseCrdt<TransactionList>,
|
crdt: BaseCrdt<TransactionList>,
|
||||||
keys: fastcrypto::ed25519::Ed25519KeyPair,
|
keys: fastcrypto::ed25519::Ed25519KeyPair,
|
||||||
websocket_client: WebSocketClient,
|
incoming_receiver: mpsc::Receiver<SignedOp>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SideNode {
|
impl SideNode {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
websocket_client: WebSocketClient,
|
|
||||||
crdt: BaseCrdt<TransactionList>,
|
crdt: BaseCrdt<TransactionList>,
|
||||||
keys: Ed25519KeyPair,
|
keys: Ed25519KeyPair,
|
||||||
|
incoming_receiver: mpsc::Receiver<SignedOp>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
let node = Self {
|
||||||
crdt,
|
crdt,
|
||||||
keys,
|
keys,
|
||||||
websocket_client,
|
incoming_receiver,
|
||||||
}
|
};
|
||||||
|
node
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn start(&mut self) {
|
pub(crate) async fn start(&mut self) {
|
||||||
self.websocket_client.start().await;
|
println!("Starting node...");
|
||||||
|
loop {
|
||||||
|
let incoming = self.incoming_receiver.recv().await.unwrap();
|
||||||
|
println!("Received incoming message: {:?}", incoming);
|
||||||
|
self.handle_incoming(incoming);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_transaction_local(
|
fn handle_incoming(&mut self, incoming: SignedOp) {
|
||||||
|
println!("WINNNINGINGINGINGINGIGNIGN");
|
||||||
|
self.crdt.apply(incoming.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn add_transaction_local(
|
||||||
&mut self,
|
&mut self,
|
||||||
transaction: serde_json::Value,
|
transaction: serde_json::Value,
|
||||||
) -> bft_json_crdt::json_crdt::SignedOp {
|
) -> bft_json_crdt::json_crdt::SignedOp {
|
||||||
@@ -45,4 +57,9 @@ impl SideNode {
|
|||||||
.sign(&self.keys);
|
.sign(&self.keys);
|
||||||
signed_op
|
signed_op
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Print the current state of the CRDT, can be used to debug
|
||||||
|
pub(crate) fn trace_crdt(&self) {
|
||||||
|
println!("{:?}", self.crdt.doc.list);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,36 +1,48 @@
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use bft_json_crdt::json_crdt::SignedOp;
|
||||||
use ezsockets::ClientConfig;
|
use ezsockets::ClientConfig;
|
||||||
use std::io::BufRead;
|
use tokio::fs::File;
|
||||||
|
use tokio::io;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
pub(crate) struct WebSocketClient {}
|
use crate::{node::SideNode, utils};
|
||||||
|
|
||||||
|
pub(crate) struct WebSocketClient {
|
||||||
|
incoming_sender: mpsc::Sender<SignedOp>,
|
||||||
|
}
|
||||||
|
|
||||||
impl WebSocketClient {
|
impl WebSocketClient {
|
||||||
pub(crate) fn new() -> Self {
|
pub(crate) fn new(incoming_sender: mpsc::Sender<SignedOp>) -> Self {
|
||||||
Self {}
|
Self { incoming_sender }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start the websocket client
|
/// Start the websocket client
|
||||||
pub(crate) async fn start(&mut self) {
|
pub(crate) async fn start(node: &mut SideNode, incoming_sender: mpsc::Sender<SignedOp>) {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
let config = ClientConfig::new("ws://localhost:8080/websocket");
|
let config = ClientConfig::new("ws://localhost:8080/websocket");
|
||||||
let (handle, future) = ezsockets::connect(|_client| WebSocketClient {}, config).await;
|
let (handle, future) =
|
||||||
|
ezsockets::connect(|_client| WebSocketClient { incoming_sender }, config).await;
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
future.await.unwrap();
|
future.await.unwrap();
|
||||||
});
|
});
|
||||||
let stdin = std::io::stdin();
|
let stdin = tokio::io::stdin();
|
||||||
let lines = stdin.lock().lines();
|
// let lines = stdin.lock().lines();
|
||||||
for line in lines {
|
//
|
||||||
let line = line.unwrap();
|
let mut reader = FramedRead::new(stdin, LinesCodec::new());
|
||||||
|
let line = reader.next().await.transpose()?.unwrap();
|
||||||
let signed_op = if let "exit" = line.as_str() {
|
let signed_op = if let "exit" = line.as_str() {
|
||||||
break;
|
break;
|
||||||
|
} else if let "trace" = line.as_str() {
|
||||||
|
node.trace_crdt();
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
// list_transaction_crdt::create(bft_crdt, &keys)
|
let fake = utils::fake_transaction("foo123".to_string());
|
||||||
|
node.add_transaction_local(fake)
|
||||||
};
|
};
|
||||||
tracing::info!("sending {:?}", signed_op);
|
tracing::info!("sending {:?}", signed_op);
|
||||||
let json = serde_json::to_string(&signed_op).unwrap();
|
let json = serde_json::to_string(&signed_op).unwrap();
|
||||||
handle.text(json).unwrap();
|
handle.text(json).unwrap();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
@@ -39,9 +51,9 @@ impl ezsockets::ClientExt for WebSocketClient {
|
|||||||
|
|
||||||
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
|
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
|
||||||
tracing::info!("received message: {text}");
|
tracing::info!("received message: {text}");
|
||||||
let _incoming: bft_json_crdt::json_crdt::SignedOp = serde_json::from_str(&text).unwrap();
|
let incoming: bft_json_crdt::json_crdt::SignedOp = serde_json::from_str(&text).unwrap();
|
||||||
// TODO: make this sucker work
|
tracing::info!("received signed op: {incoming:?}");
|
||||||
// self.bft_crdt.apply(incoming.clone());
|
self.incoming_sender.send(incoming).await.unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user