More pushing code around
This commit is contained in:
34
side-node/src/crdt.rs
Normal file
34
side-node/src/crdt.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
use bft_crdt_derive::add_crdt_fields;
|
||||||
|
use bft_json_crdt::{
|
||||||
|
json_crdt::{CrdtNode, IntoCrdtNode},
|
||||||
|
list_crdt::ListCrdt,
|
||||||
|
};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[add_crdt_fields]
|
||||||
|
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
||||||
|
pub(crate) struct TransactionList {
|
||||||
|
pub(crate) list: ListCrdt<Transaction>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A fake Transaction struct we can use as a simulated payload
|
||||||
|
#[add_crdt_fields]
|
||||||
|
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
||||||
|
pub(crate) struct Transaction {
|
||||||
|
from: String,
|
||||||
|
to: String,
|
||||||
|
amount: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// }
|
||||||
|
// }
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
use bft_crdt_derive::add_crdt_fields;
|
|
||||||
|
|
||||||
use bft_json_crdt::{
|
|
||||||
json_crdt::{CrdtNode, IntoCrdtNode},
|
|
||||||
keypair::{Ed25519KeyPair, KeyPair},
|
|
||||||
list_crdt::ListCrdt,
|
|
||||||
op::Op,
|
|
||||||
};
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use serde_json::{json, Value};
|
|
||||||
|
|
||||||
#[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)]
|
|
||||||
pub(crate) struct Transaction {
|
|
||||||
from: String,
|
|
||||||
to: String,
|
|
||||||
amount: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generate a fake transaction with customizable from_pubkey String
|
|
||||||
fn fake_transaction(from_pubkey: String) -> Value {
|
|
||||||
json!({
|
|
||||||
"from": from_pubkey,
|
|
||||||
"to": "Bob",
|
|
||||||
"amount": 1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
use bft_json_crdt::json_crdt::BaseCrdt;
|
use bft_json_crdt::json_crdt::BaseCrdt;
|
||||||
use cli::{parse_args, Commands};
|
use cli::{parse_args, Commands};
|
||||||
|
use crdt::TransactionList;
|
||||||
use fastcrypto::ed25519::Ed25519KeyPair;
|
use fastcrypto::ed25519::Ed25519KeyPair;
|
||||||
use list_transaction_crdt::TransactionList;
|
|
||||||
use node::SideNode;
|
use node::SideNode;
|
||||||
use websocket::WebSocketClient;
|
use websocket::WebSocketClient;
|
||||||
|
|
||||||
pub(crate) mod cli;
|
pub(crate) mod cli;
|
||||||
|
pub(crate) mod crdt;
|
||||||
pub(crate) mod init;
|
pub(crate) mod init;
|
||||||
pub(crate) mod keys;
|
pub(crate) mod keys;
|
||||||
pub(crate) mod list_transaction_crdt;
|
|
||||||
pub(crate) mod node;
|
pub(crate) mod node;
|
||||||
pub(crate) mod utils;
|
pub(crate) mod utils;
|
||||||
pub(crate) mod websocket;
|
pub(crate) mod websocket;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use bft_json_crdt::json_crdt::BaseCrdt;
|
use bft_json_crdt::json_crdt::BaseCrdt;
|
||||||
use fastcrypto::ed25519::Ed25519KeyPair;
|
use fastcrypto::ed25519::Ed25519KeyPair;
|
||||||
|
|
||||||
use crate::{list_transaction_crdt::TransactionList, websocket::WebSocketClient};
|
use crate::{crdt::TransactionList, websocket::WebSocketClient};
|
||||||
|
|
||||||
pub(crate) struct SideNode {
|
pub(crate) struct SideNode {
|
||||||
crdt: BaseCrdt<TransactionList>,
|
crdt: BaseCrdt<TransactionList>,
|
||||||
@@ -26,11 +26,10 @@ impl SideNode {
|
|||||||
self.websocket_client.start().await;
|
self.websocket_client.start().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_transaction_locally(
|
fn add_transaction_local(
|
||||||
&mut self,
|
&mut self,
|
||||||
transaction: serde_json::Value,
|
transaction: serde_json::Value,
|
||||||
) -> bft_json_crdt::json_crdt::SignedOp {
|
) -> bft_json_crdt::json_crdt::SignedOp {
|
||||||
// let last: &Op<Transaction>;
|
|
||||||
let last = self
|
let last = self
|
||||||
.crdt
|
.crdt
|
||||||
.doc
|
.doc
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
pub(crate) const KEY_FILE: &str = "keys.pem";
|
pub(crate) const KEY_FILE: &str = "keys.pem";
|
||||||
pub(crate) const CONFIG_FILE: &str = "config.toml";
|
pub(crate) const CONFIG_FILE: &str = "config.toml";
|
||||||
|
|
||||||
@@ -20,3 +22,12 @@ pub(crate) fn home(name: &String) -> std::path::PathBuf {
|
|||||||
path.push(name);
|
path.push(name);
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generate a fake transaction with customizable from_pubkey String
|
||||||
|
pub(crate) fn fake_transaction(from_pubkey: String) -> Value {
|
||||||
|
json!({
|
||||||
|
"from": from_pubkey,
|
||||||
|
"to": "Bob",
|
||||||
|
"amount": 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -39,7 +39,8 @@ impl ezsockets::ClientExt for WebSocketClient {
|
|||||||
|
|
||||||
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
|
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
|
||||||
tracing::info!("received message: {text}");
|
tracing::info!("received message: {text}");
|
||||||
let incoming: bft_json_crdt::json_crdt::SignedOp = serde_json::from_str(&text).unwrap();
|
let _incoming: bft_json_crdt::json_crdt::SignedOp = serde_json::from_str(&text).unwrap();
|
||||||
|
// TODO: make this sucker work
|
||||||
// self.bft_crdt.apply(incoming.clone());
|
// self.bft_crdt.apply(incoming.clone());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user