Starting a refactor into more functional modules

This commit is contained in:
Dave Hrycyszyn
2024-06-25 13:31:02 +01:00
parent d4809a48e6
commit 037fc27b7b
14 changed files with 54 additions and 21 deletions

5
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/side.iml" filepath="$PROJECT_DIR$/.idea/side.iml" />
</modules>
</component>
</project>

17
.idea/side.iml generated Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/crates/bft-json-crdt/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/crates/bft-json-crdt/bft-crdt-derive/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/crates/bft-json-crdt/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/crates/bft-json-crdt/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/side-node/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/side-node/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/side-watcher/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@@ -1,4 +1,4 @@
use crate::{keys, utils}; use crate::{ utils, bitcoin};
use bdk::bitcoin::psbt::PartiallySignedTransaction; use bdk::bitcoin::psbt::PartiallySignedTransaction;
use bdk::bitcoin::Network; use bdk::bitcoin::Network;
use bdk::database::MemoryDatabase; use bdk::database::MemoryDatabase;
@@ -21,8 +21,8 @@ use bdk::{FeeRate, KeychainKind, SignOptions, TransactionDetails, Wallet};
pub async fn run() -> Result<(), anyhow::Error> { pub async fn run() -> Result<(), anyhow::Error> {
let dave = utils::home(&"dave".to_string()); let dave = utils::home(&"dave".to_string());
let sammy = utils::home(&"sammy".to_string()); let sammy = utils::home(&"sammy".to_string());
let dave_key = keys::bitcoin::load_from_file(&dave).unwrap(); let dave_key = bitcoin::bitcoin_keys::load_from_file(&dave).unwrap();
let sammy_key = keys::bitcoin::load_from_file(&sammy).unwrap(); let sammy_key = bitcoin::bitcoin_keys::load_from_file(&sammy).unwrap();
let dave_wallet = create_wallet(dave_key)?; let dave_wallet = create_wallet(dave_key)?;
let sammy_wallet = create_wallet(sammy_key)?; let sammy_wallet = create_wallet(sammy_key)?;

View File

@@ -0,0 +1,2 @@
pub mod btc_electrum_client;
pub mod btc_esplora_client;

View File

@@ -0,0 +1,3 @@
pub mod clients;
pub mod bitcoin_keys;

View File

@@ -1,3 +1 @@
pub mod btc_electrum_client;
pub mod btc_esplora_client;
pub mod websocket; pub mod websocket;

View File

@@ -2,7 +2,7 @@ use std::path::PathBuf;
use config::SideNodeConfig; use config::SideNodeConfig;
use crate::{keys, utils}; use crate::{keys, utils, bitcoin};
pub(crate) mod config; pub(crate) mod config;
@@ -14,7 +14,7 @@ pub(crate) fn init(home: PathBuf, config: SideNodeConfig) -> Result<(), std::io:
keys::bft_crdt::write(&bft_crdt_key_path)?; keys::bft_crdt::write(&bft_crdt_key_path)?;
println!("Writing bitcoin key to: {:?}", bitcoin_key_path); println!("Writing bitcoin key to: {:?}", bitcoin_key_path);
keys::bitcoin::write(&bitcoin_key_path)?; bitcoin::bitcoin_keys::write(&bitcoin_key_path)?;
println!("Writing config to: {:?}", config_path); println!("Writing config to: {:?}", config_path);
config::write_toml(&config, &config_path).expect("unable to write config file"); config::write_toml(&config, &config_path).expect("unable to write config file");

View File

@@ -1,2 +1 @@
pub mod bft_crdt; pub mod bft_crdt;
pub mod bitcoin;

View File

@@ -5,6 +5,7 @@ use crdt::TransactionList;
use node::SideNode; use node::SideNode;
use tokio::{sync::mpsc, task}; use tokio::{sync::mpsc, task};
pub(crate) mod bitcoin;
pub(crate) mod cli; pub(crate) mod cli;
pub mod clients; pub mod clients;
pub mod crdt; pub mod crdt;
@@ -31,19 +32,19 @@ pub async fn run() {
node.start().await; node.start().await;
} }
Some(Commands::Btc {}) => { Some(Commands::Btc {}) => {
let _ = clients::btc_esplora_client::run().await; let _ = bitcoin::clients::btc_esplora_client::run().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 /// 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-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 = keys::bft_crdt::load_from_file(&side_dir);
let bitcoin_keys = keys::bitcoin::load_from_file(&side_dir).unwrap(); let bitcoin_keys = bitcoin::bitcoin_keys::load_from_file(&side_dir).unwrap();
let bitcoin_wallet = clients::btc_electrum_client::create_wallet(bitcoin_keys).unwrap(); let bitcoin_wallet = bitcoin::clients::btc_electrum_client::create_wallet(bitcoin_keys).unwrap();
let crdt = BaseCrdt::<TransactionList>::new(&bft_crdt_keys); let crdt = BaseCrdt::<TransactionList>::new(&bft_crdt_keys);
// Channels for internal communication, and a tokio task for stdin input // Channels for internal communication, and a tokio task for stdin input

View File

@@ -2,13 +2,7 @@ use bft_json_crdt::{
json_crdt::{BaseCrdt, SignedOp}, json_crdt::{BaseCrdt, SignedOp},
keypair::make_keypair, keypair::make_keypair,
}; };
use side_node::{ use side_node::{clients::websocket::Client, crdt::TransactionList, keys, node::SideNode, utils};
clients::{btc_electrum_client, websocket::Client},
crdt::TransactionList,
keys,
node::SideNode,
utils,
};
use tokio::sync::mpsc; use tokio::sync::mpsc;
#[tokio::test] #[tokio::test]
@@ -41,8 +35,8 @@ async fn test_distribute_via_websockets() {
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-crdt
let bft_crdt_keys = make_keypair(); let bft_crdt_keys = make_keypair();
let mnemonic_words = keys::bitcoin::make_mnemonic(); let mnemonic_words = bitcoin::bitcoin_keys::make_mnemonic();
let bitcoin_keys = keys::bitcoin::get(mnemonic_words).unwrap(); let bitcoin_keys = bitcoin::bitcoin_keys::get(mnemonic_words).unwrap();
let bitcoin_wallet = btc_electrum_client::create_wallet(bitcoin_keys).unwrap(); let bitcoin_wallet = btc_electrum_client::create_wallet(bitcoin_keys).unwrap();
let crdt = BaseCrdt::<TransactionList>::new(&bft_crdt_keys); let crdt = BaseCrdt::<TransactionList>::new(&bft_crdt_keys);