Starting to format out BFT-CRDTs.
This commit is contained in:
@@ -11,6 +11,9 @@ tokio = { version = "1.37.0", features = ["time"] }
|
||||
websockets = "0.3.0"
|
||||
bft-json-crdt = { path = "../../bft-json-crdt" }
|
||||
bft-crdt-derive = { path = "../../bft-json-crdt/bft-crdt-derive" }
|
||||
serde_cbor = "0.11.2"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0.117"
|
||||
|
||||
[features]
|
||||
default = ["bft", "logging-list", "logging-json"]
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use bft_json_crdt::keypair::make_keypair;
|
||||
use cli::{parse_args, Commands};
|
||||
|
||||
pub(crate) mod cli;
|
||||
@@ -14,8 +13,7 @@ async fn main() {
|
||||
init::init();
|
||||
}
|
||||
Some(Commands::Run {}) => {
|
||||
let keys = make_keypair();
|
||||
websocket::start(keys).await.unwrap();
|
||||
websocket::start().await.unwrap();
|
||||
}
|
||||
None => println!("No command provided. Exiting. See --help for more information."),
|
||||
}
|
||||
|
||||
@@ -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