Extracted all crdt-related code into a named module
This commit is contained in:
63
side-node/src/list_transaction_crdt.rs
Normal file
63
side-node/src/list_transaction_crdt.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use bft_crdt_derive::add_crdt_fields;
|
||||
|
||||
use bft_json_crdt::{
|
||||
json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode},
|
||||
keypair::{make_keypair, Ed25519KeyPair},
|
||||
list_crdt::ListCrdt,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
use websockets::WebSocket;
|
||||
|
||||
#[add_crdt_fields]
|
||||
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
||||
pub(crate) struct ListExample {
|
||||
pub(crate) list: ListCrdt<Transaction>, // 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)]
|
||||
pub(crate) struct Transaction {
|
||||
from: String,
|
||||
to: String,
|
||||
amount: f64,
|
||||
}
|
||||
|
||||
pub(crate) fn new() -> (BaseCrdt<ListExample>, Ed25519KeyPair) {
|
||||
let keys = make_keypair();
|
||||
let bft_crdt = BaseCrdt::<ListExample>::new(&keys);
|
||||
(bft_crdt, keys)
|
||||
}
|
||||
|
||||
pub(crate) async fn send(
|
||||
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
|
||||
})
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
use bft_json_crdt::keypair::Ed25519KeyPair;
|
||||
use bft_json_crdt::{json_crdt::BaseCrdt, keypair::make_keypair};
|
||||
use cli::{parse_args, Commands};
|
||||
use types::ListExample;
|
||||
|
||||
pub(crate) mod cli;
|
||||
pub(crate) mod init;
|
||||
pub(crate) mod types;
|
||||
pub(crate) mod list_transaction_crdt;
|
||||
pub(crate) mod websocket;
|
||||
|
||||
#[tokio::main]
|
||||
@@ -17,15 +14,9 @@ async fn main() {
|
||||
init::init();
|
||||
}
|
||||
Some(Commands::Run {}) => {
|
||||
let (mut bft_crdt, keys) = setup_crdt();
|
||||
let (mut bft_crdt, keys) = list_transaction_crdt::new();
|
||||
websocket::start(keys, &mut bft_crdt).await.unwrap();
|
||||
}
|
||||
None => println!("No command provided. Exiting. See --help for more information."),
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_crdt() -> (BaseCrdt<ListExample>, Ed25519KeyPair) {
|
||||
let keys = make_keypair();
|
||||
let bft_crdt = BaseCrdt::<ListExample>::new(&keys);
|
||||
(bft_crdt, keys)
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
use bft_crdt_derive::add_crdt_fields;
|
||||
|
||||
use bft_json_crdt::{
|
||||
json_crdt::{CrdtNode, IntoCrdtNode},
|
||||
list_crdt::ListCrdt,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[add_crdt_fields]
|
||||
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
||||
pub(crate) struct ListExample {
|
||||
pub(crate) list: ListCrdt<Transaction>, // 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)]
|
||||
pub(crate) struct Transaction {
|
||||
from: String,
|
||||
to: String,
|
||||
amount: f64,
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user