2024-05-29 18:17:34 +01:00
|
|
|
use bft_crdt_derive::add_crdt_fields;
|
2024-05-30 15:41:08 +01:00
|
|
|
use bft_json_crdt::keypair::KeyPair;
|
2024-05-29 18:17:34 +01:00
|
|
|
use bft_json_crdt::{
|
|
|
|
|
json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode},
|
2024-05-29 22:20:35 +01:00
|
|
|
keypair::{make_keypair, ED25519_PUBLIC_KEY_LENGTH},
|
2024-05-29 18:17:34 +01:00
|
|
|
list_crdt::ListCrdt,
|
|
|
|
|
op::ROOT_ID,
|
|
|
|
|
};
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
2024-05-29 17:29:20 +01:00
|
|
|
use tokio::time::{self};
|
2024-05-29 16:35:00 +01:00
|
|
|
use websockets::WebSocket;
|
|
|
|
|
|
2024-05-29 17:29:20 +01:00
|
|
|
/// 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 16:35:00 +01:00
|
|
|
|
2024-05-29 22:04:05 +01:00
|
|
|
// generate a placeholder transaction
|
2024-05-29 18:17:34 +01:00
|
|
|
let json = generate_transaction().unwrap();
|
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
|
|
|
|
|
|
|
|
// next job is to keep adding to this guy
|
|
|
|
|
let _a = bft_crdt.doc.list.insert(ROOT_ID, 'a').sign(&keys);
|
|
|
|
|
|
2024-05-29 22:04:05 +01:00
|
|
|
let mut interval = every_two_seconds();
|
2024-05-29 16:35:00 +01:00
|
|
|
loop {
|
2024-05-29 17:29:20 +01:00
|
|
|
interval.tick().await;
|
2024-05-29 18:17:34 +01:00
|
|
|
println!("Sending: {}", json);
|
|
|
|
|
ws.send_text(json.clone()).await?;
|
2024-05-29 17:29:20 +01:00
|
|
|
|
2024-05-29 18:17:34 +01:00
|
|
|
let msg = ws.receive().await?;
|
2024-05-29 16:35:00 +01:00
|
|
|
println!("Received: {:?}", msg);
|
2024-06-05 11:04:55 +01:00
|
|
|
// TODO: bft_crdt.apply() changes in here when we receive socket input from other nodes
|
|
|
|
|
bft_crdt.apply(msg);
|
|
|
|
|
println!("New crdt state is: {}", bft_crdt.doc.view())
|
2024-05-29 16:35:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-29 18:17:34 +01:00
|
|
|
|
2024-05-29 22:04:05 +01:00
|
|
|
fn every_two_seconds() -> time::Interval {
|
|
|
|
|
time::interval(time::Duration::from_secs(2))
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 18:17:34 +01:00
|
|
|
#[add_crdt_fields]
|
2024-05-29 22:20:35 +01:00
|
|
|
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
2024-05-29 18:17:34 +01:00
|
|
|
struct ListExample {
|
2024-05-29 22:20:35 +01:00
|
|
|
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
|
2024-05-29 22:20:35 +01:00
|
|
|
#[add_crdt_fields]
|
|
|
|
|
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
2024-05-29 18:17:34 +01:00
|
|
|
struct Transaction {
|
|
|
|
|
from: String,
|
|
|
|
|
to: String,
|
2024-05-29 22:20:35 +01:00
|
|
|
amount: f64,
|
2024-05-29 18:17:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn generate_transaction() -> serde_json::Result<String> {
|
|
|
|
|
let transaction = Transaction {
|
|
|
|
|
from: "Alice".to_string(),
|
|
|
|
|
to: "Bob".to_string(),
|
2024-05-29 22:20:35 +01:00
|
|
|
amount: 100.0,
|
|
|
|
|
path: vec![],
|
|
|
|
|
id: [0; ED25519_PUBLIC_KEY_LENGTH],
|
2024-05-29 18:17:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let json = serde_json::to_string(&transaction).unwrap();
|
|
|
|
|
Ok(json)
|
|
|
|
|
}
|