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_crdt_derive::add_crdt_fields;
|
||||||
|
|
||||||
use bft_json_crdt::{
|
use bft_json_crdt::{
|
||||||
json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode},
|
json_crdt::{CrdtNode, IntoCrdtNode},
|
||||||
keypair::{Ed25519KeyPair, KeyPair},
|
keypair::{Ed25519KeyPair, KeyPair},
|
||||||
list_crdt::ListCrdt,
|
list_crdt::ListCrdt,
|
||||||
|
op::Op,
|
||||||
};
|
};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
use crate::keys;
|
|
||||||
|
|
||||||
#[add_crdt_fields]
|
#[add_crdt_fields]
|
||||||
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
||||||
pub(crate) struct TransactionList {
|
pub(crate) struct TransactionList {
|
||||||
pub(crate) list: ListCrdt<Transaction>,
|
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
|
/// 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)]
|
||||||
@@ -28,30 +38,7 @@ pub(crate) struct Transaction {
|
|||||||
amount: f64,
|
amount: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn new(side_dir: PathBuf) -> (BaseCrdt<TransactionList>, Ed25519KeyPair) {
|
/// Generate a fake transaction with customizable from_pubkey String
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fake_transaction(from_pubkey: String) -> Value {
|
fn fake_transaction(from_pubkey: String) -> Value {
|
||||||
json!({
|
json!({
|
||||||
"from": from_pubkey,
|
"from": from_pubkey,
|
||||||
|
|||||||
@@ -26,17 +26,17 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
Some(Commands::Run { name }) => {
|
Some(Commands::Run { name }) => {
|
||||||
let (crdt, websocket_client) = setup(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;
|
side_node.start().await;
|
||||||
}
|
}
|
||||||
None => println!("No command provided. Exiting. See --help for more information."),
|
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) {
|
fn setup(name: &String) -> (BaseCrdt<TransactionList>, WebSocketClient) {
|
||||||
let side_dir = utils::home(name);
|
let side_dir = utils::home(name);
|
||||||
let keys = keys::load_from_file(side_dir);
|
let keys = keys::load_from_file(side_dir);
|
||||||
|
|
||||||
let websocket_client = WebSocketClient::new();
|
let websocket_client = WebSocketClient::new();
|
||||||
let crdt = BaseCrdt::<TransactionList>::new(&keys);
|
let crdt = BaseCrdt::<TransactionList>::new(&keys);
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +1,16 @@
|
|||||||
use bft_json_crdt::json_crdt::BaseCrdt;
|
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 {
|
pub(crate) struct SideNode {
|
||||||
crdt: BaseCrdt<TransactionList>,
|
crdt: BaseCrdt<TransactionList>,
|
||||||
keys: Ed25519KeyPair,
|
|
||||||
websocket_client: WebSocketClient,
|
websocket_client: WebSocketClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SideNode {
|
impl SideNode {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(websocket_client: WebSocketClient, crdt: BaseCrdt<TransactionList>) -> Self {
|
||||||
websocket_client: WebSocketClient,
|
|
||||||
crdt: BaseCrdt<TransactionList>,
|
|
||||||
name: String,
|
|
||||||
) -> Self {
|
|
||||||
let side_dir = utils::home(&name);
|
|
||||||
let keys = keys::load_from_file(side_dir);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
crdt,
|
crdt,
|
||||||
keys,
|
|
||||||
websocket_client,
|
websocket_client,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user