Starting to format out BFT-CRDTs.
This commit is contained in:
19
Cargo.lock
generated
19
Cargo.lock
generated
@@ -1031,6 +1031,12 @@ dependencies = [
|
|||||||
"subtle",
|
"subtle",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "half"
|
||||||
|
version = "1.8.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1b43ede17f21864e81be2fa654110bf1e793774238d86ef8555c37e6519c0403"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hashbrown"
|
name = "hashbrown"
|
||||||
version = "0.12.3"
|
version = "0.12.3"
|
||||||
@@ -1947,6 +1953,16 @@ dependencies = [
|
|||||||
"serde_derive",
|
"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]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.203"
|
version = "1.0.203"
|
||||||
@@ -2073,6 +2089,9 @@ dependencies = [
|
|||||||
"bft-crdt-derive",
|
"bft-crdt-derive",
|
||||||
"bft-json-crdt",
|
"bft-json-crdt",
|
||||||
"clap",
|
"clap",
|
||||||
|
"serde",
|
||||||
|
"serde_cbor",
|
||||||
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
"websockets",
|
"websockets",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ tokio = { version = "1.37.0", features = ["time"] }
|
|||||||
websockets = "0.3.0"
|
websockets = "0.3.0"
|
||||||
bft-json-crdt = { path = "../../bft-json-crdt" }
|
bft-json-crdt = { path = "../../bft-json-crdt" }
|
||||||
bft-crdt-derive = { path = "../../bft-json-crdt/bft-crdt-derive" }
|
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]
|
[features]
|
||||||
default = ["bft", "logging-list", "logging-json"]
|
default = ["bft", "logging-list", "logging-json"]
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use bft_json_crdt::keypair::make_keypair;
|
|
||||||
use cli::{parse_args, Commands};
|
use cli::{parse_args, Commands};
|
||||||
|
|
||||||
pub(crate) mod cli;
|
pub(crate) mod cli;
|
||||||
@@ -14,8 +13,7 @@ async fn main() {
|
|||||||
init::init();
|
init::init();
|
||||||
}
|
}
|
||||||
Some(Commands::Run {}) => {
|
Some(Commands::Run {}) => {
|
||||||
let keys = make_keypair();
|
websocket::start().await.unwrap();
|
||||||
websocket::start(keys).await.unwrap();
|
|
||||||
}
|
}
|
||||||
None => println!("No command provided. Exiting. See --help for more information."),
|
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 tokio::time::{self};
|
||||||
use websockets::WebSocket;
|
use websockets::WebSocket;
|
||||||
|
|
||||||
/// Starts a websocket and periodically sends a BFT-CRDT message to the websocket server
|
/// 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 ws = WebSocket::connect("ws://localhost:8080/").await?;
|
||||||
|
|
||||||
let mut interval = time::interval(time::Duration::from_secs(2));
|
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 {
|
loop {
|
||||||
interval.tick().await;
|
interval.tick().await;
|
||||||
println!("Sending: period");
|
println!("Sending: {}", json);
|
||||||
ws.send_text("period".to_string()).await?;
|
ws.send_text(json.clone()).await?;
|
||||||
let msg = ws.receive().await?;
|
|
||||||
|
|
||||||
|
// TODO: somewhere down in here when we receive socket input from other nodes
|
||||||
|
|
||||||
|
let msg = ws.receive().await?;
|
||||||
println!("Received: {:?}", msg);
|
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