Renamed binaries to make things a bit more general
This commit is contained in:
54
crdt-node/tests/crdt.rs
Normal file
54
crdt-node/tests/crdt.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use bft_json_crdt::json_crdt::BaseCrdt;
|
||||
use bft_json_crdt::keypair::make_keypair;
|
||||
use bft_json_crdt::op::ROOT_ID;
|
||||
use side_node::bft_crdt::TransactionList;
|
||||
|
||||
// case 1 - send valid updates
|
||||
#[test]
|
||||
fn test_valid_updates() {
|
||||
// Insert to bft-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_generic_transaction_json(String::from("a"));
|
||||
let val_b = side_node::utils::fake_generic_transaction_json(String::from("b"));
|
||||
let val_c = side_node::utils::fake_generic_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);
|
||||
|
||||
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"
|
||||
);
|
||||
}
|
||||
60
crdt-node/tests/side_node.rs
Normal file
60
crdt-node/tests/side_node.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use bft_json_crdt::{
|
||||
json_crdt::{BaseCrdt, SignedOp},
|
||||
keypair::make_keypair,
|
||||
};
|
||||
use side_node::{
|
||||
bft_crdt::websocket::Client, bft_crdt::TransactionList, bitcoin, node::SideNode, utils,
|
||||
};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_distribute_via_websockets() {
|
||||
let mut node1 = setup("alice").await;
|
||||
let mut node2 = setup("bob").await;
|
||||
|
||||
assert_eq!(node1.current_sha(), node2.current_sha());
|
||||
|
||||
let transaction = utils::fake_generic_transaction_json("from_alice".to_string());
|
||||
let signed_op = node1.add_transaction_local(transaction);
|
||||
node2.handle_incoming(signed_op);
|
||||
|
||||
assert_eq!(node1.current_sha(), node2.current_sha());
|
||||
|
||||
let transaction = utils::fake_generic_transaction_json("from_alice2".to_string());
|
||||
let signed_op = node1.add_transaction_local(transaction);
|
||||
node2.handle_incoming(signed_op);
|
||||
|
||||
assert_eq!(node1.current_sha(), node2.current_sha());
|
||||
|
||||
let transaction = utils::fake_generic_transaction_json("from_alice3".to_string());
|
||||
let signed_op = node1.add_transaction_local(transaction);
|
||||
node2.handle_incoming(signed_op);
|
||||
|
||||
assert_eq!(node1.current_sha(), node2.current_sha());
|
||||
}
|
||||
|
||||
/// Wire everything up, ignoring things we are not using in the test
|
||||
async fn setup(_: &str) -> SideNode {
|
||||
// First, load up the keys and create a bft-bft-crdt
|
||||
let bft_crdt_keys = make_keypair();
|
||||
let mnemonic_words = bitcoin::keys::make_mnemonic();
|
||||
let keys = bitcoin::keys::get(mnemonic_words).unwrap();
|
||||
let bitcoin_wallet = bitcoin::clients::electrum::create_wallet(keys).unwrap();
|
||||
let crdt = BaseCrdt::<TransactionList>::new(&bft_crdt_keys);
|
||||
|
||||
// Channels for internal communication, and a tokio task for stdin input
|
||||
let (incoming_sender, incoming_receiver) = mpsc::channel::<SignedOp>(32);
|
||||
let (_, stdin_receiver) = std::sync::mpsc::channel();
|
||||
|
||||
// Finally, create the node and return it
|
||||
let handle = Client::new(incoming_sender).await;
|
||||
let node = SideNode::new(
|
||||
crdt,
|
||||
bft_crdt_keys,
|
||||
bitcoin_wallet,
|
||||
incoming_receiver,
|
||||
stdin_receiver,
|
||||
handle,
|
||||
);
|
||||
node
|
||||
}
|
||||
Reference in New Issue
Block a user