Almost working, I've got a blocking I/O problem with stdin now :)

This commit is contained in:
Dave Hrycyszyn
2024-06-10 14:25:05 +01:00
parent c3f5b2890b
commit 4717ffa7e8
3 changed files with 70 additions and 40 deletions

View File

@@ -1,32 +1,44 @@
use bft_json_crdt::json_crdt::BaseCrdt;
use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
use fastcrypto::ed25519::Ed25519KeyPair;
use tokio::sync::mpsc;
use crate::{crdt::TransactionList, websocket::WebSocketClient};
use crate::crdt::TransactionList;
pub(crate) struct SideNode {
crdt: BaseCrdt<TransactionList>,
keys: fastcrypto::ed25519::Ed25519KeyPair,
websocket_client: WebSocketClient,
incoming_receiver: mpsc::Receiver<SignedOp>,
}
impl SideNode {
pub(crate) fn new(
websocket_client: WebSocketClient,
crdt: BaseCrdt<TransactionList>,
keys: Ed25519KeyPair,
incoming_receiver: mpsc::Receiver<SignedOp>,
) -> Self {
Self {
let node = Self {
crdt,
keys,
websocket_client,
}
incoming_receiver,
};
node
}
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,
transaction: serde_json::Value,
) -> bft_json_crdt::json_crdt::SignedOp {
@@ -45,4 +57,9 @@ impl SideNode {
.sign(&self.keys);
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);
}
}