WIP: hash inequality seems to be happening from something on the wire

This commit is contained in:
Dave Hrycyszyn
2024-06-18 10:17:59 +01:00
parent e9870241cb
commit 416d1ad88b
8 changed files with 92 additions and 21 deletions

View File

@@ -8,11 +8,11 @@ use websocket::WebSocketClient;
pub(crate) mod cli;
pub mod crdt;
pub(crate) mod init;
pub(crate) mod keys;
pub(crate) mod node;
pub mod keys;
pub mod node;
pub(crate) mod stdin;
pub mod utils;
pub(crate) mod websocket;
pub mod websocket;
#[tokio::main]
pub async fn run() {

View File

@@ -4,7 +4,7 @@ use tokio::sync::mpsc;
use crate::{crdt::TransactionList, utils, websocket::WebSocketClient};
pub(crate) struct SideNode {
pub struct SideNode {
crdt: BaseCrdt<TransactionList>,
keys: fastcrypto::ed25519::Ed25519KeyPair,
incoming_receiver: mpsc::Receiver<SignedOp>,
@@ -13,7 +13,7 @@ pub(crate) struct SideNode {
}
impl SideNode {
pub(crate) fn new(
pub fn new(
crdt: BaseCrdt<TransactionList>,
keys: Ed25519KeyPair,
incoming_receiver: mpsc::Receiver<SignedOp>,
@@ -39,12 +39,14 @@ impl SideNode {
let transaction = utils::fake_transaction_json(stdin);
let json = serde_json::to_value(transaction).unwrap();
let signed_op = self.add_transaction_local(json);
println!("STDIN: {}", shappy(signed_op.clone()));
self.send_to_network(signed_op).await;
}
Err(_) => {} // ignore empty channel errors in this PoC
}
match self.incoming_receiver.try_recv() {
Ok(incoming) => {
println!("INCOMING");
self.handle_incoming(incoming);
}
Err(_) => {} // ignore empty channel errors in this PoC
@@ -57,12 +59,13 @@ impl SideNode {
self.handle.call(to_send).unwrap();
}
fn handle_incoming(&mut self, incoming: SignedOp) {
pub fn handle_incoming(&mut self, incoming: SignedOp) {
println!("handle_incoming: {}", shappy(incoming.clone()));
self.crdt.apply(incoming);
self.trace_crdt();
// self.trace_crdt();
}
pub(crate) fn add_transaction_local(
pub fn add_transaction_local(
&mut self,
transaction: serde_json::Value,
) -> bft_json_crdt::json_crdt::SignedOp {
@@ -79,12 +82,21 @@ impl SideNode {
.list
.insert(last.id, transaction)
.sign(&self.keys);
self.trace_crdt();
// self.trace_crdt();
signed_op
}
/// Print the current state of the CRDT, can be used to debug
pub(crate) fn trace_crdt(&self) {
pub fn trace_crdt(&self) {
println!("{:?}", self.crdt.doc.view_sha());
}
pub fn current_sha(&self) -> String {
self.crdt.doc.view_sha()
}
}
fn shappy(op: SignedOp) -> String {
let b = serde_json::to_string(&op).unwrap().into_bytes();
sha256::digest(b).to_string()
}

View File

@@ -3,17 +3,16 @@ use bft_json_crdt::json_crdt::SignedOp;
use ezsockets::ClientConfig;
use tokio::sync::mpsc;
pub(crate) struct WebSocketClient {
pub struct WebSocketClient {
incoming_sender: mpsc::Sender<SignedOp>,
handle: ezsockets::Client<WebSocketClient>,
}
impl WebSocketClient {
/// Start the websocket client
pub(crate) async fn new(
pub async fn new(
incoming_sender: mpsc::Sender<SignedOp>,
) -> ezsockets::Client<WebSocketClient> {
tracing_subscriber::fmt::init();
let config = ClientConfig::new("ws://localhost:8080/websocket");
let (handle, future) = ezsockets::connect(
|client| WebSocketClient {
@@ -40,7 +39,6 @@ impl ezsockets::ClientExt for WebSocketClient {
/// When we receive a text message, apply the crdt operation contained in it to our
/// local crdt.
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
tracing::info!("received text: {text:?}");
let incoming: bft_json_crdt::json_crdt::SignedOp = serde_json::from_str(&text).unwrap();
self.incoming_sender.send(incoming).await?;
Ok(())
@@ -55,7 +53,6 @@ impl ezsockets::ClientExt for WebSocketClient {
/// Call this with the `Call` type to send application data to the websocket client
/// (and from there, to the server).
async fn on_call(&mut self, call: Self::Call) -> Result<(), ezsockets::Error> {
tracing::info!("sending signed op: {call:?}");
self.handle.text(call)?;
Ok(())
}