Files
bft-crdt-experiment/side-node/tests/crdt.rs

79 lines
2.2 KiB
Rust

use bft_crdt_derive::add_crdt_fields;
use bft_json_crdt::json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode, SignedOp};
use bft_json_crdt::op::ROOT_ID;
use bft_json_crdt::{keypair::make_keypair, list_crdt::ListCrdt};
use serde_json::Value;
use side_node::crdt::{Transaction, TransactionList};
// 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 keypair1 = make_keypair();
let mut crdt1 = BaseCrdt::<TransactionList>::new(&keypair1);
let val_a = side_node::utils::fake_transaction_json(String::from("a"));
let val_b = side_node::utils::fake_transaction_json(String::from("b"));
let val_c = side_node::utils::fake_transaction_json(String::from("c"));
let _a = crdt1
.doc
.list
.insert(ROOT_ID, val_a.clone())
.sign(&keypair1);
let _b = crdt1
.doc
.list
.insert(_a.id(), val_b.clone())
.sign(&keypair1);
let _c = crdt1
.doc
.list
.insert(_b.id(), val_c.clone())
.sign(&keypair1);
assert_eq!(
crdt1.doc.list.view(),
vec![to_tx(_a.clone()), to_tx(_b.clone()), to_tx(_c.clone())]
);
let keypair2 = make_keypair();
let mut crdt2 = BaseCrdt::<TransactionList>::new(&keypair2);
crdt2.apply(_a.clone());
crdt2.apply(_b);
crdt2.apply(_c.clone());
assert_eq!(
crdt2.doc.list.view(),
crdt1.doc.list.view(),
"views should be equal"
);
crdt2.apply(_a.clone());
crdt2.apply(_a);
assert_eq!(
crdt1.doc.list.view(),
crdt2.doc.list.view(),
"views are still equal after repeated applies"
);
}
fn to_tx(op: SignedOp) -> Transaction {
let val = op.inner.content;
serde_json::from_value(val.into()).unwrap()
}
// pub fn iter(&self) -> impl Iterator<Item = &Transaction> {
// self.ops
// .iter()
// .filter(|op| !op.is_deleted && op.content.is_some())
// .map(|op| op.content.as_ref().unwrap())
// }
// /// Convenience function to get a vector of visible list elements
// pub fn view(&self) -> Vec<T> {
// self.iter().map(|i| i.to_owned()).collect()
// }