Moved all the bft_crdt stuff into its own module

This commit is contained in:
Dave Hrycyszyn
2024-06-25 13:50:02 +01:00
parent 7fb4585deb
commit e6a4fe0fd6
12 changed files with 23 additions and 23 deletions

View File

@@ -11,7 +11,7 @@ use syn::{
/// Helper to get tokenstream representing the parent crate /// Helper to get tokenstream representing the parent crate
fn get_crate_name() -> TokenStream { fn get_crate_name() -> TokenStream {
let cr8 = crate_name("bft-json-crdt").unwrap_or(FoundCrate::Itself); let cr8 = crate_name("bft-json-bft-crdt").unwrap_or(FoundCrate::Itself);
match cr8 { match cr8 {
FoundCrate::Itself => quote! { ::bft_json_crdt }, FoundCrate::Itself => quote! { ::bft_json_crdt },
FoundCrate::Name(name) => { FoundCrate::Name(name) => {

View File

@@ -18,7 +18,7 @@ use fastcrypto::{
// Verifier, // Verifier,
}; };
// TODO: serde's json object serialization and deserialization (correctly) do not define anything // TODO: serde's json object serialization and deserialization (correctly) do not define anything
// object field order in JSON objects. However, the hash check impl in bft-json-crdt does take order // object field order in JSON objects. However, the hash check impl in bft-json-bft-crdt does take order
// into account. This is going to cause problems later for non-Rust implementations, BFT hash checking // into account. This is going to cause problems later for non-Rust implementations, BFT hash checking
// currently depends on JSON serialization/deserialization object order. This shouldn't be the case // currently depends on JSON serialization/deserialization object order. This shouldn't be the case
// but I've hacked in an IndexMap for the moment to get the PoC working. To see the problem, replace this with // but I've hacked in an IndexMap for the moment to get the PoC working. To see the problem, replace this with

View File

@@ -259796,7 +259796,7 @@ function insertAt(idx, elt) {
const pos = new_log.findIndex(log => log[0] === parent_id) const pos = new_log.findIndex(log => log[0] === parent_id)
new_log.push([ID_COUNTER, pos, 0, elt]) new_log.push([ID_COUNTER, pos, 0, elt])
crdt.splice(raw_i + 1, 0, { deleted: false, content: elt, id: ID_COUNTER }) crdt.splice(raw_i + 1, 0, { deleted: false, content: elt, id: ID_COUNTER })
// console.log(`insert at ${idx} translated as op [${ID_COUNTER}, ${pos}, ${0}, ${escape(elt)}] found at ${raw_i + 1}::`, crdt[raw_i + 1]) // console.log(`insert at ${idx} translated as op [${ID_COUNTER}, ${pos}, ${0}, ${escape(elt)}] found at ${raw_i + 1}::`, bft-crdt[raw_i + 1])
return return
} }
@@ -259816,7 +259816,7 @@ function deleteAt(idx) {
const pos = new_log.findIndex(log => log[0] === our_id) const pos = new_log.findIndex(log => log[0] === our_id)
new_log.push([ID_COUNTER, pos, 1]); new_log.push([ID_COUNTER, pos, 1]);
crdt[raw_i].deleted = true crdt[raw_i].deleted = true
// console.log(`delete at ${idx} translated as op [${ID_COUNTER}, ${pos}, ${1}] found at ${raw_i} with our_id ${our_id}::`, crdt[raw_i]) // console.log(`delete at ${idx} translated as op [${ID_COUNTER}, ${pos}, ${1}] found at ${raw_i} with our_id ${our_id}::`, bft-crdt[raw_i])
return return
} }
@@ -259853,7 +259853,7 @@ function rawJSString(edits) {
// deleteAt(edit[0]) // deleteAt(edit[0])
// } // }
// } // }
// console.log(crdt) // console.log(bft-crdt)
// rawJSString(mock_edits) // rawJSString(mock_edits)
// console.log(new_log) // console.log(new_log)
// const subset = edits.slice(0, 50000) // const subset = edits.slice(0, 50000)

View File

@@ -20,7 +20,7 @@ pub(crate) fn write(key_path: &PathBuf) -> Result<(), std::io::Error> {
pub(crate) fn load_from_file(side_dir: &PathBuf) -> Ed25519KeyPair { pub(crate) fn load_from_file(side_dir: &PathBuf) -> Ed25519KeyPair {
let key_path = crate::utils::side_paths(side_dir.clone()).0; let key_path = crate::utils::side_paths(side_dir.clone()).0;
let data = fs::read_to_string(key_path).expect("couldn't read bft-crdt key file"); let data = fs::read_to_string(key_path).expect("couldn't read bft-bft-crdt key file");
println!("data: {:?}", data); println!("data: {:?}", data);
Ed25519KeyPair::decode_base64(&data).expect("couldn't load keypair from file") Ed25519KeyPair::decode_base64(&data).expect("couldn't load keypair from file")

View File

@@ -5,6 +5,8 @@ use bft_json_crdt::{
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub mod keys;
#[add_crdt_fields] #[add_crdt_fields]
#[derive(Clone, CrdtNode, Serialize, Deserialize)] #[derive(Clone, CrdtNode, Serialize, Deserialize)]
pub struct TransactionList { pub struct TransactionList {

View File

@@ -36,8 +36,8 @@ impl ezsockets::ClientExt for Client {
// match on it. // match on it.
type Call = String; type Call = String;
/// When we receive a text message, apply the crdt operation contained in it to our /// When we receive a text message, apply the bft-crdt operation contained in it to our
/// local crdt. /// local bft-crdt.
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> { async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
let string_sha = utils::shassy(text.clone()); let string_sha = utils::shassy(text.clone());
println!("received text, sha: {string_sha}"); println!("received text, sha: {string_sha}");
@@ -45,7 +45,7 @@ impl ezsockets::ClientExt for Client {
let object_sha = utils::shappy(incoming.clone()); let object_sha = utils::shappy(incoming.clone());
println!("deserialized: {}", object_sha); println!("deserialized: {}", object_sha);
if string_sha != object_sha { if string_sha != object_sha {
panic!("sha mismatch: {string_sha} != {object_sha}, bft-crdt has failed"); panic!("sha mismatch: {string_sha} != {object_sha}, bft-bft-crdt has failed");
} }
self.incoming_sender.send(incoming).await?; self.incoming_sender.send(incoming).await?;
Ok(()) Ok(())

View File

@@ -2,7 +2,7 @@ use std::path::PathBuf;
use config::SideNodeConfig; use config::SideNodeConfig;
use crate::{bitcoin, keys, utils}; use crate::{bft_crdt, bitcoin, utils};
pub(crate) mod config; pub(crate) mod config;
@@ -10,8 +10,8 @@ pub(crate) fn init(home: PathBuf, config: SideNodeConfig) -> Result<(), std::io:
ensure_side_directory_exists(&home)?; ensure_side_directory_exists(&home)?;
let (bft_crdt_key_path, bitcoin_key_path, config_path) = utils::side_paths(home.clone()); let (bft_crdt_key_path, bitcoin_key_path, config_path) = utils::side_paths(home.clone());
println!("Writing bft crdt key to: {:?}", bft_crdt_key_path); println!("Writing bft bft-crdt key to: {:?}", bft_crdt_key_path);
keys::bft_crdt::write(&bft_crdt_key_path)?; bft_crdt::keys::write(&bft_crdt_key_path)?;
println!("Writing bitcoin key to: {:?}", bitcoin_key_path); println!("Writing bitcoin key to: {:?}", bitcoin_key_path);
bitcoin::keys::write(&bitcoin_key_path)?; bitcoin::keys::write(&bitcoin_key_path)?;

View File

@@ -1 +0,0 @@
pub mod bft_crdt;

View File

@@ -1,16 +1,15 @@
use bft_crdt::TransactionList;
use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp}; use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
use cli::{parse_args, Commands}; use cli::{parse_args, Commands};
use clients::websocket; use clients::websocket;
use crdt::TransactionList;
use node::SideNode; use node::SideNode;
use tokio::{sync::mpsc, task}; use tokio::{sync::mpsc, task};
pub mod bft_crdt;
pub mod bitcoin; pub mod bitcoin;
pub(crate) mod cli; pub(crate) mod cli;
pub mod clients; pub mod clients;
pub mod crdt;
pub(crate) mod init; pub(crate) mod init;
pub mod keys;
pub mod node; pub mod node;
pub(crate) mod stdin; pub(crate) mod stdin;
pub mod utils; pub mod utils;
@@ -40,9 +39,9 @@ pub async fn run() {
/// Wire everything up outside the application so that we can test more easily later /// Wire everything up outside the application so that we can test more easily later
async fn setup(name: &String) -> SideNode { async fn setup(name: &String) -> SideNode {
// First, load up the keys and create a bft-crdt // First, load up the keys and create a bft-bft-crdt
let side_dir = utils::home(name); let side_dir = utils::home(name);
let bft_crdt_keys = keys::bft_crdt::load_from_file(&side_dir); let bft_crdt_keys = bft_crdt::keys::load_from_file(&side_dir);
let keys = bitcoin::keys::load_from_file(&side_dir).unwrap(); let keys = bitcoin::keys::load_from_file(&side_dir).unwrap();
let bitcoin_wallet = bitcoin::clients::electrum::create_wallet(keys).unwrap(); let bitcoin_wallet = bitcoin::clients::electrum::create_wallet(keys).unwrap();
let crdt = BaseCrdt::<TransactionList>::new(&bft_crdt_keys); let crdt = BaseCrdt::<TransactionList>::new(&bft_crdt_keys);

View File

@@ -3,7 +3,7 @@ use bft_json_crdt::json_crdt::{BaseCrdt, SignedOp};
use fastcrypto::ed25519::Ed25519KeyPair; use fastcrypto::ed25519::Ed25519KeyPair;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use crate::{clients::websocket::Client, crdt::TransactionList, utils}; use crate::{bft_crdt::TransactionList, clients::websocket::Client, utils};
pub struct SideNode { pub struct SideNode {
crdt: BaseCrdt<TransactionList>, crdt: BaseCrdt<TransactionList>,

View File

@@ -1,12 +1,12 @@
use bft_json_crdt::json_crdt::BaseCrdt; use bft_json_crdt::json_crdt::BaseCrdt;
use bft_json_crdt::keypair::make_keypair; use bft_json_crdt::keypair::make_keypair;
use bft_json_crdt::op::ROOT_ID; use bft_json_crdt::op::ROOT_ID;
use side_node::crdt::TransactionList; use side_node::bft_crdt::TransactionList;
// case 1 - send valid updates // case 1 - send valid updates
#[test] #[test]
fn test_valid_updates() { fn test_valid_updates() {
// Insert to crdt.doc on local node, test applying the same operation to a remote node // 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 // and check that the view is the same
let keypair1 = make_keypair(); let keypair1 = make_keypair();
let mut crdt1 = BaseCrdt::<TransactionList>::new(&keypair1); let mut crdt1 = BaseCrdt::<TransactionList>::new(&keypair1);

View File

@@ -3,7 +3,7 @@ use bft_json_crdt::{
keypair::make_keypair, keypair::make_keypair,
}; };
use side_node::{ use side_node::{
bitcoin, clients::websocket::Client, crdt::TransactionList, node::SideNode, utils, bft_crdt::TransactionList, bitcoin, clients::websocket::Client, node::SideNode, utils,
}; };
use tokio::sync::mpsc; use tokio::sync::mpsc;
@@ -35,7 +35,7 @@ async fn test_distribute_via_websockets() {
/// Wire everything up, ignoring things we are not using in the test /// Wire everything up, ignoring things we are not using in the test
async fn setup(_: &str) -> SideNode { async fn setup(_: &str) -> SideNode {
// First, load up the keys and create a bft-crdt // First, load up the keys and create a bft-bft-crdt
let bft_crdt_keys = make_keypair(); let bft_crdt_keys = make_keypair();
let mnemonic_words = bitcoin::keys::make_mnemonic(); let mnemonic_words = bitcoin::keys::make_mnemonic();
let keys = bitcoin::keys::get(mnemonic_words).unwrap(); let keys = bitcoin::keys::get(mnemonic_words).unwrap();