Skip to content

Commit 5f67d6c

Browse files
authored
reorganize utils/mod.rs into separate utils/db.rs and utils/distance.rs files (#144)
1 parent c6b77ab commit 5f67d6c

File tree

10 files changed

+85
-84
lines changed

10 files changed

+85
-84
lines changed

ethportal-peertest/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use trin_core::portalnet::{
1717
utp::UtpListener,
1818
Enr, U256,
1919
};
20-
use trin_core::utils::setup_overlay_db;
20+
use trin_core::utils::db::setup_overlay_db;
2121

2222
#[tokio::main]
2323
async fn main() -> Result<(), Box<dyn std::error::Error>> {

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use trin_core::{
1313
cli::{TrinConfig, HISTORY_NETWORK, STATE_NETWORK},
1414
jsonrpc::service::launch_jsonrpc_server,
1515
portalnet::{discovery::Discovery, types::PortalnetConfig},
16-
utils::setup_overlay_db,
16+
utils::db::setup_overlay_db,
1717
};
1818
use trin_history::initialize_history_network;
1919
use trin_state::initialize_state_network;

trin-core/src/bin/generate-data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::Arc;
77
use structopt::StructOpt;
88
use trin_core::portalnet::storage::{DistanceFunction, PortalStorage, PortalStorageConfig};
99
use trin_core::portalnet::U256;
10-
use trin_core::utils::get_data_dir;
10+
use trin_core::utils::db::get_data_dir;
1111

1212
// For every 1 kb of data we store (key + value), RocksDB tends to grow by this many kb on disk...
1313
// ...but this is a crude empirical estimation that works mainly with default value data size of 32

trin-core/src/portalnet/overlay.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::locks::RwLoggingExt;
2-
use crate::utils::xor_two_values;
2+
use crate::utils::distance::xor_two_values;
33

44
use super::{
55
discovery::Discovery,

trin-core/src/portalnet/storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::U256;
2-
use crate::utils::{get_data_dir, xor_two_values};
2+
use crate::utils::{db::get_data_dir, distance::xor_two_values};
33
use discv5::enr::NodeId;
44
use hex;
55
use log;

trin-core/src/utils/db.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::{env, fs};
2+
3+
use directories::ProjectDirs;
4+
use discv5::enr::NodeId;
5+
use rocksdb::{Options, DB};
6+
7+
const TRIN_DATA_ENV_VAR: &str = "TRIN_DATA_PATH";
8+
9+
pub fn get_data_dir(node_id: NodeId) -> String {
10+
let path = env::var(TRIN_DATA_ENV_VAR).unwrap_or_else(|_| get_default_data_dir(node_id));
11+
12+
fs::create_dir_all(&path).expect("Unable to create data directory folder");
13+
path
14+
}
15+
16+
pub fn get_default_data_dir(node_id: NodeId) -> String {
17+
// Windows: C:\Users\Username\AppData\Roaming\Trin\data
18+
// macOS: ~/Library/Application Support/Trin
19+
// Unix-like: $HOME/.local/share/trin
20+
21+
// Append first 8 characters of Node ID
22+
let mut application_string = "Trin_".to_owned();
23+
let node_id_string = hex::encode(node_id.raw());
24+
let suffix = &node_id_string[..8];
25+
application_string.push_str(suffix);
26+
27+
match ProjectDirs::from("", "", &application_string) {
28+
Some(proj_dirs) => proj_dirs.data_local_dir().to_str().unwrap().to_string(),
29+
None => panic!("Unable to find data directory"),
30+
}
31+
}
32+
33+
pub fn setup_overlay_db(node_id: NodeId) -> DB {
34+
let data_path = get_data_dir(node_id);
35+
let mut db_opts = Options::default();
36+
db_opts.create_if_missing(true);
37+
DB::open(&db_opts, data_path).unwrap()
38+
}

trin-core/src/utils/distance.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
pub fn xor_two_values(first: &[u8], second: &[u8]) -> Vec<u8> {
2+
if first.len() != second.len() {
3+
panic!("Can only xor vectors of equal length.")
4+
};
5+
6+
first
7+
.iter()
8+
.zip(&mut second.iter())
9+
.map(|(&first, &second)| first ^ second)
10+
.collect()
11+
}
12+
13+
#[cfg(test)]
14+
mod test {
15+
use super::*;
16+
17+
#[test]
18+
fn test_xor_two_zeros() {
19+
let one = vec![0];
20+
let two = vec![0];
21+
assert_eq!(xor_two_values(&one, &two), [0]);
22+
}
23+
24+
#[test]
25+
fn test_xor_two_values() {
26+
let one = vec![1, 0, 0];
27+
let two = vec![0, 0, 1];
28+
assert_eq!(xor_two_values(&one, &two), [1, 0, 1]);
29+
}
30+
31+
#[test]
32+
#[should_panic(expected = "Can only xor vectors of equal length.")]
33+
fn test_xor_panics_with_different_lengths() {
34+
let one = vec![1, 0];
35+
let two = vec![0, 0, 1];
36+
xor_two_values(&one, &two);
37+
}
38+
}

trin-core/src/utils/mod.rs

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,3 @@
1-
use directories::ProjectDirs;
2-
use discv5::enr::NodeId;
3-
use rocksdb::{Options, DB};
4-
use std::{env, fs};
5-
1+
pub mod db;
2+
pub mod distance;
63
pub mod hash_delay_queue;
7-
8-
const TRIN_DATA_ENV_VAR: &str = "TRIN_DATA_PATH";
9-
10-
pub fn xor_two_values(first: &[u8], second: &[u8]) -> Vec<u8> {
11-
if first.len() != second.len() {
12-
panic!("Can only xor vectors of equal length.")
13-
};
14-
15-
first
16-
.iter()
17-
.zip(&mut second.iter())
18-
.map(|(&first, &second)| first ^ second)
19-
.collect()
20-
}
21-
22-
pub fn get_data_dir(node_id: NodeId) -> String {
23-
let path = env::var(TRIN_DATA_ENV_VAR).unwrap_or_else(|_| get_default_data_dir(node_id));
24-
25-
fs::create_dir_all(&path).expect("Unable to create data directory folder");
26-
path
27-
}
28-
29-
pub fn get_default_data_dir(node_id: NodeId) -> String {
30-
// Windows: C:\Users\Username\AppData\Roaming\Trin\data
31-
// macOS: ~/Library/Application Support/Trin
32-
// Unix-like: $HOME/.local/share/trin
33-
34-
// Append first 8 characters of Node ID
35-
let mut application_string = "Trin_".to_owned();
36-
let node_id_string = hex::encode(node_id.raw());
37-
let suffix = &node_id_string[..8];
38-
application_string.push_str(suffix);
39-
40-
match ProjectDirs::from("", "", &application_string) {
41-
Some(proj_dirs) => proj_dirs.data_local_dir().to_str().unwrap().to_string(),
42-
None => panic!("Unable to find data directory"),
43-
}
44-
}
45-
46-
pub fn setup_overlay_db(node_id: NodeId) -> DB {
47-
let data_path = get_data_dir(node_id);
48-
let mut db_opts = Options::default();
49-
db_opts.create_if_missing(true);
50-
DB::open(&db_opts, data_path).unwrap()
51-
}
52-
53-
#[cfg(test)]
54-
mod test {
55-
use super::*;
56-
57-
#[test]
58-
fn test_xor_two_zeros() {
59-
let one = vec![0];
60-
let two = vec![0];
61-
assert_eq!(xor_two_values(&one, &two), [0]);
62-
}
63-
64-
#[test]
65-
fn test_xor_two_values() {
66-
let one = vec![1, 0, 0];
67-
let two = vec![0, 0, 1];
68-
assert_eq!(xor_two_values(&one, &two), [1, 0, 1]);
69-
}
70-
71-
#[test]
72-
#[should_panic(expected = "Can only xor vectors of equal length.")]
73-
fn test_xor_panics_with_different_lengths() {
74-
let one = vec![1, 0];
75-
let two = vec![0, 0, 1];
76-
xor_two_values(&one, &two);
77-
}
78-
}

trin-history/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use trin_core::jsonrpc::types::HistoryJsonRpcRequest;
1717
use trin_core::portalnet::discovery::Discovery;
1818
use trin_core::portalnet::events::PortalnetEvents;
1919
use trin_core::portalnet::types::PortalnetConfig;
20-
use trin_core::utils::setup_overlay_db;
20+
use trin_core::utils::db::setup_overlay_db;
2121

2222
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
2323
println!("Launching trin-history...");

trin-state/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use trin_core::locks::RwLoggingExt;
1414
use trin_core::portalnet::discovery::Discovery;
1515
use trin_core::portalnet::events::PortalnetEvents;
1616
use trin_core::portalnet::types::PortalnetConfig;
17-
use trin_core::utils::setup_overlay_db;
17+
use trin_core::utils::db::setup_overlay_db;
1818

1919
pub mod events;
2020
mod jsonrpc;

0 commit comments

Comments
 (0)