Skip to content

Commit

Permalink
feat: unify data directory cli (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
morph-dev authored Sep 22, 2024
1 parent 14c8f31 commit 286adfb
Show file tree
Hide file tree
Showing 26 changed files with 240 additions and 238 deletions.
7 changes: 3 additions & 4 deletions Cargo.lock

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

23 changes: 15 additions & 8 deletions ethportal-api/src/types/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub struct TrinConfig {
pub trusted_block_root: Option<B256>,

#[arg(
long = "portal-subnetworks",
long = "portal-subnetworks",
help = "Comma-separated list of which portal subnetworks to activate",
default_value = DEFAULT_SUBNETWORKS,
value_parser = subnetwork_parser,
Expand All @@ -155,10 +155,10 @@ pub struct TrinConfig {

#[arg(
long = "network",
help = "Choose mainnet or angelfood",
default_value = DEFAULT_NETWORK,
value_parser = network_parser
)]
help = "Choose mainnet or angelfood",
default_value = DEFAULT_NETWORK,
value_parser = network_parser
)]
pub network: Arc<NetworkSpec>,

/// Storage capacity specified in megabytes.
Expand All @@ -176,9 +176,15 @@ pub struct TrinConfig {
pub enable_metrics_with_url: Option<SocketAddr>,

#[arg(
short = 'e',
long = "ephemeral",
help = "Use temporary data storage that is deleted on exit."
long,
help = "The directory for storing application data. If used together with --ephemeral, new child directory will be created. Can be alternatively set via TRIN_DATA_PATH env variable."
)]
pub data_dir: Option<PathBuf>,

#[arg(
long,
short,
help = "Use new data directory, located in OS temporary directory. If used together with --data-dir, new directory will be created there instead."
)]
pub ephemeral: bool,

Expand Down Expand Up @@ -237,6 +243,7 @@ impl Default for TrinConfig {
.parse()
.expect("Parsing static DEFAULT_STORAGE_CAPACITY_MB to work"),
enable_metrics_with_url: None,
data_dir: None,
ephemeral: false,
disable_poke: false,
ws: false,
Expand Down
4 changes: 2 additions & 2 deletions ethportal-api/src/types/portal_wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ pub struct NetworkSpec {
}

impl NetworkSpec {
pub fn get_network_name(&self) -> String {
self.network.to_string()
pub fn network(&self) -> Network {
self.network
}

pub fn get_protocol_id_from_hex(&self, hex: &str) -> Result<ProtocolId, ProtocolIdError> {
Expand Down
8 changes: 4 additions & 4 deletions portal-bridge/src/bridge/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ use trin_execution::{
create_contract_content_value, create_storage_content_key, create_storage_content_value,
},
execution::TrinExecution,
storage::utils::setup_temp_dir,
trie_walker::TrieWalker,
types::{block_to_trace::BlockToTrace, trie_proof::TrieProof},
utils::full_nibble_path_to_address_hash,
};
use trin_metrics::bridge::BridgeMetricsReporter;
use trin_utils::dir::create_temp_dir;

use crate::{
bridge::history::SERVE_BLOCK_TIMEOUT,
Expand Down Expand Up @@ -85,15 +85,14 @@ impl StateBridge {

async fn launch_state(&self, last_block: u64) -> anyhow::Result<()> {
info!("Gossiping state data from block 0 to {last_block}");
let temp_directory = setup_temp_dir()?;
let temp_directory = create_temp_dir("trin-bridge-state", None)?;

// Enable contract storage changes caching required for gossiping the storage trie
let state_config = StateConfig {
cache_contract_storage_changes: true,
block_to_trace: BlockToTrace::None,
};
let mut trin_execution =
TrinExecution::new(Some(temp_directory.path().to_path_buf()), state_config).await?;
let mut trin_execution = TrinExecution::new(temp_directory.path(), state_config).await?;
for block_number in 0..=last_block {
info!("Gossipping state for block at height: {block_number}");

Expand Down Expand Up @@ -178,6 +177,7 @@ impl StateBridge {
// This is used for gossiping storage trie diffs
trin_execution.database.storage_cache.clear();
}
temp_directory.close()?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion portal-bridge/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn build_trin(bridge_config: &BridgeConfig) -> anyhow::Result<Child> {
.args(["--no-upnp"])
.args(["--mb", "0"])
.args(["--web3-transport", "http"])
.args(["--network", &bridge_config.network.get_network_name()])
.args(["--network", &bridge_config.network.network().to_string()])
.args(["--portal-subnetworks", &subnetworks_flag(bridge_config)])
.args(["--unsafe-private-key", &private_key])
.args([
Expand Down
2 changes: 0 additions & 2 deletions portalnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ async-trait.workspace = true
bytes.workspace = true
crossbeam-channel = "0.5.13"
delay_map.workspace = true
directories.workspace = true
discv5.workspace = true
ethereum_ssz.workspace = true
ethportal-api.workspace = true
Expand All @@ -36,7 +35,6 @@ serde.workspace = true
smallvec = "1.8.0"
ssz_types.workspace = true
stunclient = "0.4.1"
tempfile.workspace = true
thiserror.workspace = true
tokio.workspace = true
tokio-stream = { version = "0.1.14", features = ["sync"] }
Expand Down
34 changes: 15 additions & 19 deletions portalnet/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
hash::{Hash, Hasher},
io,
net::{Ipv4Addr, SocketAddr},
path::PathBuf,
path::Path,
str::FromStr,
sync::Arc,
time::Duration,
Expand Down Expand Up @@ -84,7 +84,7 @@ impl fmt::Debug for Discovery {
impl Discovery {
pub fn new(
portal_config: PortalnetConfig,
node_data_dir: PathBuf,
node_data_dir: &Path,
network_spec: Arc<NetworkSpec>,
) -> Result<Self, String> {
let listen_all_ips = SocketAddr::new(
Expand Down Expand Up @@ -521,17 +521,18 @@ impl AsyncUdpSocket<UtpEnr> for Discv5UdpSocket {
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::db::{configure_node_data_dir, configure_trin_data_dir};
use crate::utils::db::configure_node_data_dir;
use ethportal_api::types::{bootnodes::Bootnodes, portal_wire::MAINNET};
use trin_utils::dir::create_temp_test_dir;

#[test]
fn test_enr_file() {
// Setup temp trin data directory if we're in ephemeral mode
let trin_data_dir = configure_trin_data_dir(true).unwrap();
// Setup temp trin data directory, as if we're in ephemeral mode
let temp_dir = create_temp_test_dir().unwrap();

// Configure node data dir based on the provided private key
let (node_data_dir, private_key) =
configure_node_data_dir(trin_data_dir, None, "test".to_string()).unwrap();
configure_node_data_dir(temp_dir.path(), None, MAINNET.network()).unwrap();

let mut portalnet_config = PortalnetConfig {
private_key,
Expand All @@ -544,25 +545,17 @@ mod tests {
assert!(!trin_enr_file_location.is_file());

// test trin.enr is made on first run
let discovery = Discovery::new(
portalnet_config.clone(),
node_data_dir.clone(),
MAINNET.clone(),
)
.unwrap();
let discovery =
Discovery::new(portalnet_config.clone(), &node_data_dir, MAINNET.clone()).unwrap();
let data = fs::read_to_string(trin_enr_file_location.clone()).unwrap();
let old_enr = Enr::from_str(&data).unwrap();
assert_eq!(discovery.local_enr(), old_enr);
assert_eq!(old_enr.seq(), 1);

// test if Enr changes the Enr sequence is increased and if it is written to disk
portalnet_config.listen_port = 2424;
let discovery = Discovery::new(
portalnet_config.clone(),
node_data_dir.clone(),
MAINNET.clone(),
)
.unwrap();
let discovery =
Discovery::new(portalnet_config.clone(), &node_data_dir, MAINNET.clone()).unwrap();
assert_ne!(discovery.local_enr(), old_enr);
let data = fs::read_to_string(trin_enr_file_location.clone()).unwrap();
let old_enr = Enr::from_str(&data).unwrap();
Expand All @@ -571,12 +564,15 @@ mod tests {
assert_eq!(discovery.local_enr(), old_enr);

// test if the enr isn't changed that it's sequence stays the same
let discovery = Discovery::new(portalnet_config, node_data_dir, MAINNET.clone()).unwrap();
let discovery = Discovery::new(portalnet_config, &node_data_dir, MAINNET.clone()).unwrap();
assert_eq!(discovery.local_enr(), old_enr);
let data = fs::read_to_string(trin_enr_file_location).unwrap();
let old_enr = Enr::from_str(&data).unwrap();
assert_eq!(discovery.local_enr().seq(), 2);
assert_eq!(old_enr.seq(), 2);
assert_eq!(discovery.local_enr(), old_enr);

// close and remove temp directory
temp_dir.close().unwrap();
}
}
7 changes: 4 additions & 3 deletions portalnet/src/overlay/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2754,12 +2754,12 @@ mod tests {
time::timeout,
};
use tokio_test::{assert_pending, assert_ready, task};
use trin_utils::dir::create_temp_test_dir;

use crate::{
config::PortalnetConfig,
discovery::{Discovery, NodeAddress},
overlay::config::OverlayConfig,
utils::db::setup_temp_dir,
};
use ethportal_api::types::{
cli::{DEFAULT_DISCOVERY_PORT, DEFAULT_UTP_TRANSFER_LIMIT},
Expand All @@ -2785,8 +2785,9 @@ mod tests {
no_upnp: true,
..Default::default()
};
let temp_dir = setup_temp_dir().unwrap().into_path();
let discovery = Arc::new(Discovery::new(portal_config, temp_dir, MAINNET.clone()).unwrap());
let temp_dir = create_temp_test_dir().unwrap().into_path();
let discovery =
Arc::new(Discovery::new(portal_config, &temp_dir, MAINNET.clone()).unwrap());

let header_oracle = HeaderOracle::default();
let header_oracle = Arc::new(TokioRwLock::new(header_oracle));
Expand Down
Loading

0 comments on commit 286adfb

Please sign in to comment.