use bft_crdt_derive::add_crdt_fields; use bft_json_crdt::json_crdt::SignedOp; use bft_json_crdt::keypair::{Ed25519KeyPair, KeyPair}; use bft_json_crdt::{ json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode}, keypair::make_keypair, list_crdt::ListCrdt, // op::ROOT_ID, }; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; use tokio::time; // use tokio::time::{self}; use websockets::WebSocket; /// Starts a websocket and periodically sends a BFT-CRDT message to the websocket server pub(crate) async fn start() -> Result<(), websockets::WebSocketError> { println!("connecting to websocket at ws://127.0.0.1:8080/"); let mut ws = WebSocket::connect("ws://127.0.0.1:8080/").await?; // set up a new BFT-CRDT let keys = make_keypair(); let mut bft_crdt = BaseCrdt::::new(&keys); println!("Generated a new CRDT with public key: {}", keys.public()); let mut interval = every_two_seconds(); let mut count = 0; loop { let _ = send_a_transaction(count, &mut bft_crdt, &mut ws, &keys).await; let msg = ws.receive().await?; println!("Received: {:?}", msg); // deserialize the received Frame into a string let msg = msg.into_text().unwrap().0; // deserialize the message into a Transaction struct let incoming_operation: SignedOp = serde_json::from_str(&msg).unwrap(); println!("Received a new network operation: {:?}", incoming_operation); bft_crdt.apply(incoming_operation); println!("New crdt state is: {}", bft_crdt.doc.view()); count = count + 1; interval.tick().await; } } fn every_two_seconds() -> time::Interval { time::interval(time::Duration::from_secs(2)) } #[add_crdt_fields] #[derive(Clone, CrdtNode, Serialize, Deserialize)] struct ListExample { list: ListCrdt, // switch to Transaction as soon as char is working } /// A fake Transaction struct we can use as a simulated payload #[add_crdt_fields] #[derive(Clone, CrdtNode, Serialize, Deserialize)] struct Transaction { from: String, to: String, amount: f64, } fn generate_transaction(count: u32) -> Value { json!({ "from": "Alice", "to": "Bob", "amount": count }) } async fn send_a_transaction( count: u32, bft_crdt: &mut BaseCrdt, ws: &mut WebSocket, keys: &Ed25519KeyPair, ) -> Result<(), websockets::WebSocketError> { // generate a placeholder transaction let transaction = generate_transaction(count); // next job is to keep adding to this guy let signed_op = bft_crdt .doc .list .insert_idx(0, transaction.clone()) .sign(&keys); println!("SignedOp being sent is: {:?}", signed_op); println!("Sending: {:?}", signed_op); Ok(ws .send_text(serde_json::to_string(&signed_op).unwrap()) .await?) }