Starting to format out BFT-CRDTs.

This commit is contained in:
Dave Hrycyszyn
2024-05-29 18:17:34 +01:00
parent fff4bfe9ee
commit 56061bb94b
4 changed files with 70 additions and 8 deletions

19
Cargo.lock generated
View File

@@ -1031,6 +1031,12 @@ dependencies = [
"subtle",
]
[[package]]
name = "half"
version = "1.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b43ede17f21864e81be2fa654110bf1e793774238d86ef8555c37e6519c0403"
[[package]]
name = "hashbrown"
version = "0.12.3"
@@ -1947,6 +1953,16 @@ dependencies = [
"serde_derive",
]
[[package]]
name = "serde_cbor"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5"
dependencies = [
"half",
"serde",
]
[[package]]
name = "serde_derive"
version = "1.0.203"
@@ -2073,6 +2089,9 @@ dependencies = [
"bft-crdt-derive",
"bft-json-crdt",
"clap",
"serde",
"serde_cbor",
"serde_json",
"tokio",
"websockets",
]

View File

@@ -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"]

View File

@@ -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."),
}

View File

@@ -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)
}