Nearly workign

This commit is contained in:
Dave Hrycyszyn
2024-06-11 19:16:36 +01:00
parent 097fbea9a0
commit 0a74c86c5e
3 changed files with 37 additions and 5 deletions

View File

@@ -25,6 +25,33 @@ struct ListExample {
list: ListCrdt<char>, list: ListCrdt<char>,
} }
// 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::<ListExample>::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::<ListExample>::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 // case 2a + 2b
#[test] #[test]
fn test_equivocation() { fn test_equivocation() {

View File

@@ -11,6 +11,12 @@ pub(crate) struct TransactionList {
pub(crate) list: ListCrdt<Transaction>, pub(crate) list: ListCrdt<Transaction>,
} }
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 /// A fake Transaction struct we can use as a simulated payload
#[add_crdt_fields] #[add_crdt_fields]
#[derive(Clone, CrdtNode, Serialize, Deserialize)] #[derive(Clone, CrdtNode, Serialize, Deserialize)]

View File

@@ -36,7 +36,6 @@ impl SideNode {
loop { loop {
match self.stdin_receiver.try_recv() { match self.stdin_receiver.try_recv() {
Ok(stdin) => { Ok(stdin) => {
println!("Received stdin input: {:?}", stdin);
let transaction = utils::fake_transaction(stdin); let transaction = utils::fake_transaction(stdin);
let json = serde_json::to_value(transaction).unwrap(); let json = serde_json::to_value(transaction).unwrap();
let signed_op = self.add_transaction_local(json); let signed_op = self.add_transaction_local(json);
@@ -54,14 +53,13 @@ impl SideNode {
} }
async fn send_to_network(&self, signed_op: SignedOp) { 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(); let to_send = serde_json::to_string(&signed_op).unwrap();
self.handle.call(to_send).unwrap(); self.handle.call(to_send).unwrap();
} }
fn handle_incoming(&mut self, incoming: SignedOp) { fn handle_incoming(&mut self, incoming: SignedOp) {
println!("WINNNINGINGINGINGINGIGNIGN");
self.crdt.apply(incoming); self.crdt.apply(incoming);
self.trace_crdt();
} }
pub(crate) fn add_transaction_local( pub(crate) fn add_transaction_local(
@@ -81,11 +79,12 @@ impl SideNode {
.list .list
.insert(last.id, transaction) .insert(last.id, transaction)
.sign(&self.keys); .sign(&self.keys);
self.trace_crdt();
signed_op signed_op
} }
/// Print the current state of the CRDT, can be used to debug /// Print the current state of the CRDT, can be used to debug
pub(crate) fn _trace_crdt(&self) { pub(crate) fn trace_crdt(&self) {
println!("{:?}", self.crdt.doc.list); println!("{:?}", self.crdt.doc.view_sha());
} }
} }