More re-jigging
This commit is contained in:
@@ -1,24 +1,34 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use bft_crdt_derive::add_crdt_fields;
|
||||
|
||||
use bft_json_crdt::{
|
||||
json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode},
|
||||
json_crdt::{CrdtNode, IntoCrdtNode},
|
||||
keypair::{Ed25519KeyPair, KeyPair},
|
||||
list_crdt::ListCrdt,
|
||||
op::Op,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::keys;
|
||||
|
||||
#[add_crdt_fields]
|
||||
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
||||
pub(crate) struct TransactionList {
|
||||
pub(crate) list: ListCrdt<Transaction>,
|
||||
}
|
||||
|
||||
impl TransactionList {
|
||||
fn create(&mut self, keys: &Ed25519KeyPair) -> bft_json_crdt::json_crdt::SignedOp {
|
||||
// generate a placeholder transaction
|
||||
let transaction = fake_transaction(keys.public().to_string());
|
||||
|
||||
// next job is to keep adding to this guy
|
||||
let last: &Op<Transaction>;
|
||||
last = self.list.ops.last().expect("couldn't find last op");
|
||||
let signed_op = self.list.insert(last.id, transaction.clone()).sign(&keys);
|
||||
signed_op
|
||||
}
|
||||
}
|
||||
|
||||
/// A fake Transaction struct we can use as a simulated payload
|
||||
#[add_crdt_fields]
|
||||
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
||||
@@ -28,30 +38,7 @@ pub(crate) struct Transaction {
|
||||
amount: f64,
|
||||
}
|
||||
|
||||
pub(crate) fn new(side_dir: PathBuf) -> (BaseCrdt<TransactionList>, Ed25519KeyPair) {
|
||||
let keys = keys::load_from_file(side_dir);
|
||||
let bft_crdt = BaseCrdt::<TransactionList>::new(&keys);
|
||||
println!("Author is {}", keys.public().to_string());
|
||||
(bft_crdt, keys)
|
||||
}
|
||||
|
||||
pub(crate) fn create(
|
||||
bft_crdt: &mut BaseCrdt<TransactionList>,
|
||||
keys: &Ed25519KeyPair,
|
||||
) -> bft_json_crdt::json_crdt::SignedOp {
|
||||
// generate a placeholder transaction
|
||||
let transaction = fake_transaction(keys.public().to_string());
|
||||
|
||||
// next job is to keep adding to this guy
|
||||
let last = bft_crdt.doc.list.ops.last().expect("couldn't find last op");
|
||||
let signed_op = bft_crdt
|
||||
.doc
|
||||
.list
|
||||
.insert(last.id, transaction.clone())
|
||||
.sign(&keys);
|
||||
signed_op
|
||||
}
|
||||
|
||||
/// Generate a fake transaction with customizable from_pubkey String
|
||||
fn fake_transaction(from_pubkey: String) -> Value {
|
||||
json!({
|
||||
"from": from_pubkey,
|
||||
|
||||
@@ -26,17 +26,17 @@ async fn main() {
|
||||
}
|
||||
Some(Commands::Run { name }) => {
|
||||
let (crdt, websocket_client) = setup(name);
|
||||
let side_node = &mut SideNode::new(websocket_client, crdt, name.to_owned());
|
||||
let side_node = &mut SideNode::new(websocket_client, crdt);
|
||||
side_node.start().await;
|
||||
}
|
||||
None => println!("No command provided. Exiting. See --help for more information."),
|
||||
}
|
||||
}
|
||||
|
||||
/// Wire everything up outside the application so we can test more easily later
|
||||
fn setup(name: &String) -> (BaseCrdt<TransactionList>, WebSocketClient) {
|
||||
let side_dir = utils::home(name);
|
||||
let keys = keys::load_from_file(side_dir);
|
||||
|
||||
let websocket_client = WebSocketClient::new();
|
||||
let crdt = BaseCrdt::<TransactionList>::new(&keys);
|
||||
|
||||
|
||||
@@ -1,26 +1,16 @@
|
||||
use bft_json_crdt::json_crdt::BaseCrdt;
|
||||
use fastcrypto::ed25519::Ed25519KeyPair;
|
||||
|
||||
use crate::{keys, list_transaction_crdt::TransactionList, utils, websocket::WebSocketClient};
|
||||
use crate::{list_transaction_crdt::TransactionList, websocket::WebSocketClient};
|
||||
|
||||
pub(crate) struct SideNode {
|
||||
crdt: BaseCrdt<TransactionList>,
|
||||
keys: Ed25519KeyPair,
|
||||
websocket_client: WebSocketClient,
|
||||
}
|
||||
|
||||
impl SideNode {
|
||||
pub(crate) fn new(
|
||||
websocket_client: WebSocketClient,
|
||||
crdt: BaseCrdt<TransactionList>,
|
||||
name: String,
|
||||
) -> Self {
|
||||
let side_dir = utils::home(&name);
|
||||
let keys = keys::load_from_file(side_dir);
|
||||
|
||||
pub(crate) fn new(websocket_client: WebSocketClient, crdt: BaseCrdt<TransactionList>) -> Self {
|
||||
Self {
|
||||
crdt,
|
||||
keys,
|
||||
websocket_client,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user