Skip to content

Commit

Permalink
Merge pull request #116 from mrferris/master
Browse files Browse the repository at this point in the history
Modifies get_default_data_dir to take node id instead of enr
  • Loading branch information
mrferris authored Sep 24, 2021
2 parents a6329b7 + 57f2083 commit 2c9f277
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
discovery.write().await.start().await.unwrap();

// Setup Overlay database
let db = Arc::new(setup_overlay_db(discovery.read().await.local_enr()));
let db = Arc::new(setup_overlay_db(
discovery.read().await.local_enr().node_id(),
));

debug!("Selected networks to spawn: {:?}", trin_config.networks);
// Initialize state sub-network service and event handlers, if selected
Expand Down
18 changes: 9 additions & 9 deletions trin-core/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::portalnet::Enr;
use directories::ProjectDirs;
use discv5::enr::NodeId;
use rocksdb::{Options, DB};
use std::{env, fs};

Expand All @@ -17,22 +17,22 @@ pub fn xor_two_values(first: &[u8], second: &[u8]) -> Vec<u8> {
.collect()
}

pub fn get_data_dir(local_enr: Enr) -> String {
let path = env::var(TRIN_DATA_ENV_VAR).unwrap_or_else(|_| get_default_data_dir(local_enr));
pub fn get_data_dir(node_id: NodeId) -> String {
let path = env::var(TRIN_DATA_ENV_VAR).unwrap_or_else(|_| get_default_data_dir(node_id));

fs::create_dir_all(&path).expect("Unable to create data directory folder");
path
}

pub fn get_default_data_dir(local_enr: Enr) -> String {
pub fn get_default_data_dir(node_id: NodeId) -> String {
// Windows: C:\Users\Username\AppData\Roaming\Trin\data
// macOS: ~/Library/Application Support/Trin
// Unix-like: $HOME/.local/share/trin

// Append last 8 enr base64 encoded chars to application dir name
// Append first 8 characters of Node ID
let mut application_string = "Trin_".to_owned();
let len = &local_enr.to_base64().len();
let suffix = &local_enr.to_base64()[len - 8..];
let node_id_string = hex::encode(node_id.raw());
let suffix = &node_id_string[..8];
application_string.push_str(suffix);

match ProjectDirs::from("", "", &application_string) {
Expand All @@ -41,8 +41,8 @@ pub fn get_default_data_dir(local_enr: Enr) -> String {
}
}

pub fn setup_overlay_db(local_enr: Enr) -> DB {
let data_path = get_data_dir(local_enr);
pub fn setup_overlay_db(node_id: NodeId) -> DB {
let data_path = get_data_dir(node_id);
let mut db_opts = Options::default();
db_opts.create_if_missing(true);
DB::open(&db_opts, data_path).unwrap()
Expand Down
4 changes: 3 additions & 1 deletion trin-history/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
let discovery = Arc::new(RwLock::new(discovery));

// Setup Overlay database
let db = Arc::new(setup_overlay_db(discovery.read().await.local_enr()));
let db = Arc::new(setup_overlay_db(
discovery.read().await.local_enr().node_id(),
));

let (history_event_tx, history_event_rx) = mpsc::unbounded_channel::<TalkRequest>();
let portal_events_discovery = Arc::clone(&discovery);
Expand Down
4 changes: 3 additions & 1 deletion trin-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
let discovery = Arc::new(RwLock::new(discovery));

// Setup Overlay database
let db = Arc::new(setup_overlay_db(discovery.read().await.local_enr()));
let db = Arc::new(setup_overlay_db(
discovery.read().await.local_enr().node_id(),
));

let (state_event_tx, state_event_rx) = mpsc::unbounded_channel::<TalkRequest>();
let portal_events_discovery = Arc::clone(&discovery);
Expand Down

0 comments on commit 2c9f277

Please # to comment.