Starting to format out BFT-CRDTs.
This commit is contained in:
@@ -1,18 +1,60 @@
|
||||
use bft_json_crdt::keypair::Ed25519KeyPair;
|
||||
use bft_crdt_derive::add_crdt_fields;
|
||||
use bft_json_crdt::{
|
||||
json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode},
|
||||
keypair::make_keypair,
|
||||
list_crdt::ListCrdt,
|
||||
op::ROOT_ID,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
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(keys: Ed25519KeyPair) -> Result<(), websockets::WebSocketError> {
|
||||
pub(crate) async fn start() -> Result<(), websockets::WebSocketError> {
|
||||
let mut ws = WebSocket::connect("ws://localhost:8080/").await?;
|
||||
|
||||
let mut interval = time::interval(time::Duration::from_secs(2));
|
||||
|
||||
let json = generate_transaction().unwrap();
|
||||
let keys = make_keypair();
|
||||
let mut bft_crdt = BaseCrdt::<ListExample>::new(&keys);
|
||||
|
||||
// next job is to keep adding to this guy
|
||||
let _a = bft_crdt.doc.list.insert(ROOT_ID, 'a').sign(&keys);
|
||||
|
||||
loop {
|
||||
interval.tick().await;
|
||||
println!("Sending: period");
|
||||
ws.send_text("period".to_string()).await?;
|
||||
let msg = ws.receive().await?;
|
||||
println!("Sending: {}", json);
|
||||
ws.send_text(json.clone()).await?;
|
||||
|
||||
// TODO: somewhere down in here when we receive socket input from other nodes
|
||||
|
||||
let msg = ws.receive().await?;
|
||||
println!("Received: {:?}", msg);
|
||||
}
|
||||
}
|
||||
|
||||
#[add_crdt_fields]
|
||||
#[derive(Clone, CrdtNode)]
|
||||
struct ListExample {
|
||||
list: ListCrdt<char>, // switch to Transaction as soon as char is working
|
||||
}
|
||||
|
||||
/// A fake Transaction struct we can use as a simulated payload
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Transaction {
|
||||
from: String,
|
||||
to: String,
|
||||
amount: u64,
|
||||
}
|
||||
|
||||
fn generate_transaction() -> serde_json::Result<String> {
|
||||
let transaction = Transaction {
|
||||
from: "Alice".to_string(),
|
||||
to: "Bob".to_string(),
|
||||
amount: 100,
|
||||
};
|
||||
|
||||
let json = serde_json::to_string(&transaction).unwrap();
|
||||
Ok(json)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user