Skip to content

Commit

Permalink
Creating subfolders on data persistence for trade engine & bitcoin ne…
Browse files Browse the repository at this point in the history
…twork changes
  • Loading branch information
nobu-maeda committed May 8, 2024
1 parent f7bd37c commit 41905f5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
33 changes: 17 additions & 16 deletions src/comms/comms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Comms {
// Constructors

pub(crate) async fn new(
trade_engine_name: impl Into<String>,
trade_engine_name: impl AsRef<str>,
network: impl Borrow<BitcoinNetwork>,
data_dir_path: impl AsRef<Path>,
) -> Self {
Expand All @@ -272,7 +272,7 @@ impl Comms {

pub(crate) async fn new_with_key(
secret_key: SecretKey,
trade_engine_name: impl Into<String>,
trade_engine_name: impl AsRef<str>,
network: impl Borrow<BitcoinNetwork>,
data_dir_path: impl AsRef<Path>,
) -> Self {
Expand All @@ -282,7 +282,7 @@ impl Comms {

pub(super) async fn new_with_nostr_client(
client: Client,
trade_engine_name: impl Into<String>,
trade_engine_name: impl AsRef<str>,
network: impl Borrow<BitcoinNetwork>,
data_dir_path: impl AsRef<Path>,
) -> Self {
Expand Down Expand Up @@ -405,28 +405,29 @@ impl CommsActor {

pub(super) async fn new(
rx: mpsc::Receiver<CommsRequest>,
trade_engine_name: impl Into<String>,
trade_engine_name: impl AsRef<str>,
network: impl Borrow<BitcoinNetwork>,
client: Client,
data_dir_path: impl AsRef<Path>,
) -> Self {
let pubkey = client.keys().await.public_key();
let data = match CommsData::new(&data_dir_path, pubkey) {
Ok(data) => data,
Err(error) => {
panic!(
"Comms w/ pubkey {} failed to initialize CommsData with path {} - {}",
pubkey,
data_dir_path.as_ref().display().to_string(),
error
);
}
};
let data =
match CommsData::new(&data_dir_path, pubkey, &trade_engine_name, network.borrow()) {
Ok(data) => data,
Err(error) => {
panic!(
"Comms w/ pubkey {} failed to initialize CommsData with path {} - {}",
pubkey,
data_dir_path.as_ref().display().to_string(),
error
);
}
};
let relays = data.relays();

let actor = CommsActor {
rx,
trade_engine_name: trade_engine_name.into(),
trade_engine_name: trade_engine_name.as_ref().to_string(),
network: network.borrow().to_owned(),
pubkey,
data,
Expand Down
22 changes: 18 additions & 4 deletions src/comms/data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
borrow::Borrow,
collections::{HashMap, HashSet},
net::SocketAddr,
path::{Path, PathBuf},
Expand All @@ -9,7 +10,11 @@ use tracing::debug;
use secp256k1::XOnlyPublicKey;
use serde::{Deserialize, Serialize};

use crate::common::{error::N3xbError, persist::Persister, types::SerdeGenericTrait};
use crate::common::{
error::N3xbError,
persist::Persister,
types::{BitcoinNetwork, SerdeGenericTrait},
};

#[derive(Clone, Debug, Serialize, Deserialize)]
struct CommsDataStore {
Expand All @@ -34,8 +39,10 @@ impl CommsData {
pub(crate) fn new(
dir_path: impl AsRef<Path>,
pubkey: XOnlyPublicKey,
trade_engine_name: impl AsRef<str>,
network: impl Borrow<BitcoinNetwork>,
) -> Result<Self, N3xbError> {
let data_path = Self::setup_data_path(&dir_path, pubkey.to_string())?;
let data_path = Self::setup_data_path(&dir_path, pubkey, trade_engine_name, network)?;

let mut store = CommsDataStore {
relays: HashMap::new(),
Expand Down Expand Up @@ -80,9 +87,16 @@ impl CommsData {

fn setup_data_path(
data_dir_path: impl AsRef<Path>,
pubkey_string: String,
pubkey: XOnlyPublicKey,
trade_engine_name: impl AsRef<str>,
network: impl Borrow<BitcoinNetwork>,
) -> Result<PathBuf, N3xbError> {
let dir_path = data_dir_path.as_ref().join(format!("{}/", pubkey_string));
let dir_path = data_dir_path.as_ref().join(format!(
"{}/{}/{}",
pubkey.to_string(),
trade_engine_name.as_ref(),
network.borrow().to_string().to_lowercase()
));
std::fs::create_dir_all(&dir_path)?;
let data_path = dir_path.join("comms.json");
Ok(data_path)
Expand Down
22 changes: 16 additions & 6 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl Manager {
) -> Manager {
let data_dir_path = root_dir_path.as_ref().join(DATA_DIR_PATH_STR);
// This will always create a new Comms with a randomly generated key pair
let comms = Comms::new(trade_engine_name.as_ref(), network, &data_dir_path).await;
Self::new_with_comms(comms, &data_dir_path).await
let comms = Comms::new(&trade_engine_name, network.borrow(), &data_dir_path).await;
Self::new_with_comms(comms, &trade_engine_name, network.borrow(), &data_dir_path).await
}

pub async fn new_with_key(
Expand All @@ -57,14 +57,24 @@ impl Manager {
let data_dir_path = root_dir_path.as_ref().join(DATA_DIR_PATH_STR);
// Will try to look for Comms data that matches the pubkey and restore relays if found. New Comms is created otherwise
let comms =
Comms::new_with_key(key, trade_engine_name.as_ref(), network, &data_dir_path).await;
Self::new_with_comms(comms, &data_dir_path).await
Comms::new_with_key(key, &trade_engine_name, network.borrow(), &data_dir_path).await;
Self::new_with_comms(comms, &trade_engine_name, network.borrow(), &data_dir_path).await
}

async fn new_with_comms(comms: Comms, data_dir_path: impl AsRef<Path>) -> Manager {
async fn new_with_comms(
comms: Comms,
trade_engine_name: impl AsRef<str>,
network: impl Borrow<BitcoinNetwork>,
data_dir_path: impl AsRef<Path>,
) -> Manager {
let comms_accessor = comms.new_accessor();
let pubkey = comms_accessor.get_pubkey().await;
let manager_dir_path = data_dir_path.as_ref().join(pubkey.to_string());
let manager_dir_path = data_dir_path.as_ref().join(format!(
"{}/{}/{}",
pubkey.to_string(),
trade_engine_name.as_ref(),
network.borrow().to_string().to_lowercase(),
));

let (makers, takers) =
Self::maker_taker_setup_restore(&comms_accessor, pubkey.to_string(), &manager_dir_path)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ mod test_query {
let relay4_url = Url::from_str(&format!("{}:{}", "ws://localhost", relay4.port)).unwrap();
let relay4_addr = (relay4_url.clone(), None);

let test_engine_name1 = String::from("TestEngine1");
let test_engine_name2 = String::from("TestEngine2");
let test_engine_name1 = String::from("test-engine1");
let test_engine_name2 = String::from("test-engine2");

let manager_m1 = Manager::new(&test_engine_name1, BitcoinNetwork::Regtest, "").await;
let manager_m2 = Manager::new(&test_engine_name2, BitcoinNetwork::Regtest, "").await;
Expand Down

0 comments on commit 41905f5

Please sign in to comment.