diff --git a/crates/bft-json-crdt/tests/byzantine.rs b/crates/bft-json-crdt/tests/byzantine.rs index f195da1..792874b 100644 --- a/crates/bft-json-crdt/tests/byzantine.rs +++ b/crates/bft-json-crdt/tests/byzantine.rs @@ -25,6 +25,33 @@ struct ListExample { list: ListCrdt, } +// case 1 - send valid updates +#[test] +fn test_valid_updates() { + // Insert to crdt.doc on local node, test applying the same operation to a remote node + // and check that the view is the same + + let key = make_keypair(); + let mut crdt = BaseCrdt::::new(&key); + let _a = crdt.doc.list.insert(ROOT_ID, 'a').sign(&key); + let _b = crdt.doc.list.insert(_a.id(), 'b').sign(&key); + let _c = crdt.doc.list.insert(_b.id(), 'c').sign(&key); + + assert_eq!(crdt.doc.list.view(), vec!['a', 'b', 'c']); + + let key2 = make_keypair(); + let mut crdt2 = BaseCrdt::::new(&key2); + crdt2.apply(_a.clone()); + crdt2.apply(_a.clone()); + crdt2.apply(_b); + crdt2.apply(_c); + crdt2.apply(_a); + let _d = crdt2.doc.list.insert(_d.id(), 'c').sign(&key); // fix this in the morning + + // Ok something is seriously wrong here. + assert_eq!(crdt2.doc.list.view(), crdt.doc.list.view()); +} + // case 2a + 2b #[test] fn test_equivocation() { diff --git a/side-node/src/crdt.rs b/side-node/src/crdt.rs index 1a803e4..5962b3d 100644 --- a/side-node/src/crdt.rs +++ b/side-node/src/crdt.rs @@ -11,6 +11,12 @@ pub(crate) struct TransactionList { pub(crate) list: ListCrdt, } +impl TransactionList { + pub(crate) fn view_sha(&self) -> String { + sha256::digest(serde_json::to_string(&self.list.view()).unwrap().as_bytes()).to_string() + } +} + /// A fake Transaction struct we can use as a simulated payload #[add_crdt_fields] #[derive(Clone, CrdtNode, Serialize, Deserialize)] diff --git a/side-node/src/node.rs b/side-node/src/node.rs index aac03dd..18a1b3a 100644 --- a/side-node/src/node.rs +++ b/side-node/src/node.rs @@ -36,7 +36,6 @@ impl SideNode { loop { match self.stdin_receiver.try_recv() { Ok(stdin) => { - println!("Received stdin input: {:?}", stdin); let transaction = utils::fake_transaction(stdin); let json = serde_json::to_value(transaction).unwrap(); let signed_op = self.add_transaction_local(json); @@ -54,14 +53,13 @@ impl SideNode { } async fn send_to_network(&self, signed_op: SignedOp) { - println!("sending to network: {:?}", signed_op); let to_send = serde_json::to_string(&signed_op).unwrap(); self.handle.call(to_send).unwrap(); } fn handle_incoming(&mut self, incoming: SignedOp) { - println!("WINNNINGINGINGINGINGIGNIGN"); self.crdt.apply(incoming); + self.trace_crdt(); } pub(crate) fn add_transaction_local( @@ -81,11 +79,12 @@ impl SideNode { .list .insert(last.id, transaction) .sign(&self.keys); + self.trace_crdt(); signed_op } /// Print the current state of the CRDT, can be used to debug - pub(crate) fn _trace_crdt(&self) { - println!("{:?}", self.crdt.doc.list); + pub(crate) fn trace_crdt(&self) { + println!("{:?}", self.crdt.doc.view_sha()); } }