Files
bft-crdt-experiment/side-node/src/websocket/mod.rs

102 lines
2.9 KiB
Rust
Raw Normal View History

use std::io::{self, BufRead};
2024-05-29 18:17:34 +01:00
use bft_crdt_derive::add_crdt_fields;
use bft_json_crdt::json_crdt::SignedOp;
use bft_json_crdt::keypair::{Ed25519KeyPair, KeyPair};
2024-05-29 18:17:34 +01:00
use bft_json_crdt::{
json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode},
keypair::make_keypair,
2024-05-29 18:17:34 +01:00
list_crdt::ListCrdt,
op::ROOT_ID,
};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
// use tokio::time::{self};
use websockets::WebSocket;
/// Starts a websocket and periodically sends a BFT-CRDT message to the websocket server
2024-05-29 18:17:34 +01:00
pub(crate) async fn start() -> Result<(), websockets::WebSocketError> {
2024-06-05 16:18:47 +01:00
println!("connecting to websocket at ws://127.0.0.1:8080/");
let mut ws = WebSocket::connect("ws://127.0.0.1:8080/").await?;
2024-05-29 22:04:05 +01:00
// set up a new BFT-CRDT
2024-05-29 18:17:34 +01:00
let keys = make_keypair();
let mut bft_crdt = BaseCrdt::<ListExample>::new(&keys);
2024-05-30 15:41:08 +01:00
println!("Generated a new CRDT with public key: {}", keys.public());
2024-05-29 18:17:34 +01:00
// let mut interval = every_two_seconds();
loop {
let stdin = io::stdin();
let mut handle = stdin.lock();
let mut line = String::new();
handle.read_line(&mut line).unwrap();
println!("You entered: {}", line);
let _ = send_a_transaction(line, &mut bft_crdt, &mut ws, &keys).await;
2024-05-29 18:17:34 +01:00
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 operation: SignedOp = serde_json::from_str(&msg).unwrap();
bft_crdt.apply(operation);
2024-06-05 11:04:55 +01:00
println!("New crdt state is: {}", bft_crdt.doc.view())
}
}
2024-05-29 18:17:34 +01:00
// fn every_two_seconds() -> time::Interval {
// time::interval(time::Duration::from_secs(2))
// }
2024-05-29 22:04:05 +01:00
2024-05-29 18:17:34 +01:00
#[add_crdt_fields]
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
2024-05-29 18:17:34 +01:00
struct ListExample {
list: ListCrdt<Transaction>, // switch to Transaction as soon as char is working
2024-05-29 18:17:34 +01:00
}
/// A fake Transaction struct we can use as a simulated payload
#[add_crdt_fields]
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
2024-05-29 18:17:34 +01:00
struct Transaction {
from: String,
to: String,
amount: f64,
2024-05-29 18:17:34 +01:00
}
fn generate_transaction(from: String) -> Value {
json!({
"from": from,
"to": "Bob",
"amount": 100.0
})
}
2024-05-29 18:17:34 +01:00
async fn send_a_transaction(
input: String,
bft_crdt: &mut BaseCrdt<ListExample>,
ws: &mut WebSocket,
keys: &Ed25519KeyPair,
) -> Result<(), websockets::WebSocketError> {
// generate a placeholder transaction
let transaction = generate_transaction(input);
// next job is to keep adding to this guy
let signed_op = bft_crdt
.doc
.list
.insert(ROOT_ID, 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?)
2024-05-29 18:17:34 +01:00
}