Extracted all crdt-related code into a named module

This commit is contained in:
Dave Hrycyszyn
2024-06-05 19:49:13 +01:00
parent eb1d3f3527
commit 1cc1119883
5 changed files with 67 additions and 75 deletions

View File

@@ -1,13 +1,10 @@
use crate::types::ListExample;
use crate::list_transaction_crdt::{self, ListExample};
use bft_json_crdt::json_crdt::SignedOp;
use bft_json_crdt::json_crdt::{BaseCrdt, CrdtNode};
use bft_json_crdt::keypair::Ed25519KeyPair;
use send_transaction::send_a_transaction;
use tokio::time;
use websockets::WebSocket;
pub(crate) mod send_transaction;
/// Starts a websocket and periodically sends a BFT-CRDT message to the websocket server
pub(crate) async fn start(
keys: Ed25519KeyPair,
@@ -19,7 +16,7 @@ pub(crate) async fn start(
let mut interval = every_two_seconds();
let mut count = 0;
loop {
let _ = send_a_transaction(count, bft_crdt, &mut ws, &keys).await;
let _ = list_transaction_crdt::send(count, bft_crdt, &mut ws, &keys).await;
let msg = ws.receive().await?;
println!("Received: {:?}", msg);

View File

@@ -1,36 +0,0 @@
use bft_json_crdt::{json_crdt::BaseCrdt, keypair::Ed25519KeyPair};
use serde_json::{json, Value};
use websockets::WebSocket;
use crate::types::ListExample;
pub(crate) async fn send_a_transaction(
count: u32,
bft_crdt: &mut BaseCrdt<ListExample>,
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?)
}
fn generate_transaction(count: u32) -> Value {
json!({
"from": "Alice",
"to": "Bob",
"amount": count
})
}