64 lines
1.6 KiB
Rust
64 lines
1.6 KiB
Rust
|
|
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
|
||
|
|
})
|
||
|
|
}
|