BFT-CRDT sending of transactions works on a timer.
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
use std::io::{self, BufRead};
|
|
||||||
|
|
||||||
use bft_crdt_derive::add_crdt_fields;
|
use bft_crdt_derive::add_crdt_fields;
|
||||||
use bft_json_crdt::json_crdt::SignedOp;
|
use bft_json_crdt::json_crdt::SignedOp;
|
||||||
use bft_json_crdt::keypair::{Ed25519KeyPair, KeyPair};
|
use bft_json_crdt::keypair::{Ed25519KeyPair, KeyPair};
|
||||||
@@ -7,10 +5,11 @@ use bft_json_crdt::{
|
|||||||
json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode},
|
json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode},
|
||||||
keypair::make_keypair,
|
keypair::make_keypair,
|
||||||
list_crdt::ListCrdt,
|
list_crdt::ListCrdt,
|
||||||
op::ROOT_ID,
|
// op::ROOT_ID,
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
use tokio::time;
|
||||||
// use tokio::time::{self};
|
// use tokio::time::{self};
|
||||||
use websockets::WebSocket;
|
use websockets::WebSocket;
|
||||||
|
|
||||||
@@ -24,16 +23,10 @@ pub(crate) async fn start() -> Result<(), websockets::WebSocketError> {
|
|||||||
let mut bft_crdt = BaseCrdt::<ListExample>::new(&keys);
|
let mut bft_crdt = BaseCrdt::<ListExample>::new(&keys);
|
||||||
println!("Generated a new CRDT with public key: {}", keys.public());
|
println!("Generated a new CRDT with public key: {}", keys.public());
|
||||||
|
|
||||||
// let mut interval = every_two_seconds();
|
let mut interval = every_two_seconds();
|
||||||
|
let mut count = 0;
|
||||||
loop {
|
loop {
|
||||||
let stdin = io::stdin();
|
let _ = send_a_transaction(count, &mut bft_crdt, &mut ws, &keys).await;
|
||||||
let mut handle = stdin.lock();
|
|
||||||
|
|
||||||
let mut line = String::new();
|
|
||||||
handle.read_line(&mut line).unwrap();
|
|
||||||
|
|
||||||
println!("You entered: {}", line);
|
|
||||||
let _ = send_a_transaction(line, &mut bft_crdt, &mut ws, &keys).await;
|
|
||||||
|
|
||||||
let msg = ws.receive().await?;
|
let msg = ws.receive().await?;
|
||||||
println!("Received: {:?}", msg);
|
println!("Received: {:?}", msg);
|
||||||
@@ -42,17 +35,20 @@ pub(crate) async fn start() -> Result<(), websockets::WebSocketError> {
|
|||||||
let msg = msg.into_text().unwrap().0;
|
let msg = msg.into_text().unwrap().0;
|
||||||
|
|
||||||
// deserialize the message into a Transaction struct
|
// deserialize the message into a Transaction struct
|
||||||
let operation: SignedOp = serde_json::from_str(&msg).unwrap();
|
let incoming_operation: SignedOp = serde_json::from_str(&msg).unwrap();
|
||||||
|
println!("Received a new network operation: {:?}", incoming_operation);
|
||||||
|
|
||||||
bft_crdt.apply(operation);
|
bft_crdt.apply(incoming_operation);
|
||||||
|
|
||||||
println!("New crdt state is: {}", bft_crdt.doc.view())
|
println!("New crdt state is: {}", bft_crdt.doc.view());
|
||||||
|
count = count + 1;
|
||||||
|
interval.tick().await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn every_two_seconds() -> time::Interval {
|
fn every_two_seconds() -> time::Interval {
|
||||||
// time::interval(time::Duration::from_secs(2))
|
time::interval(time::Duration::from_secs(2))
|
||||||
// }
|
}
|
||||||
|
|
||||||
#[add_crdt_fields]
|
#[add_crdt_fields]
|
||||||
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
||||||
@@ -69,28 +65,28 @@ struct Transaction {
|
|||||||
amount: f64,
|
amount: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_transaction(from: String) -> Value {
|
fn generate_transaction(count: u32) -> Value {
|
||||||
json!({
|
json!({
|
||||||
"from": from,
|
"from": "Alice",
|
||||||
"to": "Bob",
|
"to": "Bob",
|
||||||
"amount": 100.0
|
"amount": count
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_a_transaction(
|
async fn send_a_transaction(
|
||||||
input: String,
|
count: u32,
|
||||||
bft_crdt: &mut BaseCrdt<ListExample>,
|
bft_crdt: &mut BaseCrdt<ListExample>,
|
||||||
ws: &mut WebSocket,
|
ws: &mut WebSocket,
|
||||||
keys: &Ed25519KeyPair,
|
keys: &Ed25519KeyPair,
|
||||||
) -> Result<(), websockets::WebSocketError> {
|
) -> Result<(), websockets::WebSocketError> {
|
||||||
// generate a placeholder transaction
|
// generate a placeholder transaction
|
||||||
let transaction = generate_transaction(input);
|
let transaction = generate_transaction(count);
|
||||||
|
|
||||||
// next job is to keep adding to this guy
|
// next job is to keep adding to this guy
|
||||||
let signed_op = bft_crdt
|
let signed_op = bft_crdt
|
||||||
.doc
|
.doc
|
||||||
.list
|
.list
|
||||||
.insert(ROOT_ID, transaction.clone())
|
.insert_idx(0, transaction.clone())
|
||||||
.sign(&keys);
|
.sign(&keys);
|
||||||
println!("SignedOp being sent is: {:?}", signed_op);
|
println!("SignedOp being sent is: {:?}", signed_op);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user