Skip to content

Commit

Permalink
Merge 028e621 into a1c292c
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Aug 24, 2023
2 parents a1c292c + 028e621 commit d167937
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
63 changes: 43 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rln/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ rand = "=0.8.5"
rand_chacha = "=0.3.1"
tiny-keccak = { version = "=2.0.2", features = ["keccak"] }
utils = { package = "zerokit_utils", version = "=0.3.1", path = "../utils/", default-features = false }
tempfile = "=3.7.0"

# serialization
serde_json = "=1.0.96"
Expand Down
15 changes: 11 additions & 4 deletions rln/src/pm_tree_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::str::FromStr;

use color_eyre::{Report, Result};
use serde_json::Value;
use tempfile::Builder;

use utils::pmtree::{Database, Hasher};
use utils::*;
Expand Down Expand Up @@ -48,8 +49,14 @@ impl Hasher for PoseidonHash {
}
}

fn get_tmp_path() -> PathBuf {
std::env::temp_dir().join(format!("pmtree-{}", rand::random::<u64>()))
fn get_tmp_path() -> std::io::Result<PathBuf> {
let temp_dir = Builder::new().prefix("pmtree-").rand_bytes(8).tempdir();

match temp_dir {
Ok(dir) => Ok(dir.path().to_path_buf()),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => get_tmp_path(),
Err(e) => Err(e),
}
}

fn get_tmp() -> bool {
Expand Down Expand Up @@ -89,7 +96,7 @@ impl FromStr for PmtreeConfig {

let config = Config::new()
.temporary(temporary.unwrap_or(get_tmp()))
.path(path.unwrap_or(get_tmp_path()))
.path(path.unwrap_or(get_tmp_path()?))
.cache_capacity(cache_capacity.unwrap_or(1024 * 1024 * 1024))
.flush_every_ms(flush_every_ms)
.mode(mode)
Expand All @@ -100,7 +107,7 @@ impl FromStr for PmtreeConfig {

impl Default for PmtreeConfig {
fn default() -> Self {
let tmp_path = get_tmp_path();
let tmp_path = get_tmp_path().unwrap();
PmtreeConfig(
Config::new()
.temporary(true)
Expand Down
2 changes: 1 addition & 1 deletion utils/src/pm_tree/sled_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Database for SledDB {
}

fn load(config: Self::Config) -> PmtreeResult<Self> {
let db: Sled = match sled::open(&config.path) {
let db: Sled = match config.open() {
Ok(db) => db,
Err(e) => {
return Err(PmtreeErrorKind::DatabaseError(
Expand Down

0 comments on commit d167937

Please sign in to comment.