2024-06-18 10:17:59 +01:00
|
|
|
use bft_json_crdt::{
|
|
|
|
|
json_crdt::{BaseCrdt, SignedOp},
|
|
|
|
|
keypair::make_keypair,
|
|
|
|
|
};
|
2024-06-25 13:36:50 +01:00
|
|
|
use side_node::{
|
2024-06-25 13:50:02 +01:00
|
|
|
bft_crdt::TransactionList, bitcoin, clients::websocket::Client, node::SideNode, utils,
|
2024-06-25 13:36:50 +01:00
|
|
|
};
|
2024-06-18 10:17:59 +01:00
|
|
|
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());
|
|
|
|
|
|
2024-06-18 17:12:05 +01:00
|
|
|
let transaction = utils::fake_generic_transaction_json("from_alice".to_string());
|
2024-06-18 10:17:59 +01:00
|
|
|
let signed_op = node1.add_transaction_local(transaction);
|
|
|
|
|
node2.handle_incoming(signed_op);
|
|
|
|
|
|
|
|
|
|
assert_eq!(node1.current_sha(), node2.current_sha());
|
|
|
|
|
|
2024-06-18 17:12:05 +01:00
|
|
|
let transaction = utils::fake_generic_transaction_json("from_alice2".to_string());
|
2024-06-18 10:17:59 +01:00
|
|
|
let signed_op = node1.add_transaction_local(transaction);
|
|
|
|
|
node2.handle_incoming(signed_op);
|
|
|
|
|
|
|
|
|
|
assert_eq!(node1.current_sha(), node2.current_sha());
|
|
|
|
|
|
2024-06-18 17:12:05 +01:00
|
|
|
let transaction = utils::fake_generic_transaction_json("from_alice3".to_string());
|
2024-06-18 10:17:59 +01:00
|
|
|
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 {
|
2024-06-25 13:50:02 +01:00
|
|
|
// First, load up the keys and create a bft-bft-crdt
|
2024-06-18 17:03:31 +01:00
|
|
|
let bft_crdt_keys = make_keypair();
|
2024-06-25 13:36:50 +01:00
|
|
|
let mnemonic_words = bitcoin::keys::make_mnemonic();
|
|
|
|
|
let keys = bitcoin::keys::get(mnemonic_words).unwrap();
|
2024-06-25 13:38:23 +01:00
|
|
|
let bitcoin_wallet = bitcoin::clients::electrum::create_wallet(keys).unwrap();
|
2024-06-18 17:03:31 +01:00
|
|
|
let crdt = BaseCrdt::<TransactionList>::new(&bft_crdt_keys);
|
2024-06-18 10:17:59 +01:00
|
|
|
|
|
|
|
|
// 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
|
2024-06-20 17:21:41 +01:00
|
|
|
let handle = Client::new(incoming_sender).await;
|
2024-06-18 17:03:31 +01:00
|
|
|
let node = SideNode::new(
|
|
|
|
|
crdt,
|
|
|
|
|
bft_crdt_keys,
|
2024-06-24 08:02:17 +01:00
|
|
|
bitcoin_wallet,
|
2024-06-18 17:03:31 +01:00
|
|
|
incoming_receiver,
|
|
|
|
|
stdin_receiver,
|
|
|
|
|
handle,
|
|
|
|
|
);
|
2024-06-18 10:17:59 +01:00
|
|
|
node
|
|
|
|
|
}
|