Skip to content

Commit

Permalink
compile time config with some type troubles
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwample committed Jan 14, 2025
1 parent 11d6ee2 commit 0be8602
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 12 deletions.
15 changes: 14 additions & 1 deletion Cargo.lock

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

13 changes: 12 additions & 1 deletion common/client-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
nym-client-core-config-types = { path = "./config-types", features = [
"disk-persistence",
] }
toml.workspace = true
uneval = "0.2.4"

[dependencies]
async-trait = { workspace = true }
base64 = { workspace = true }
Expand Down Expand Up @@ -43,6 +50,7 @@ nym-gateway-requests = { path = "../gateway-requests" }
nym-metrics = { path = "../nym-metrics" }
nym-nonexhaustive-delayqueue = { path = "../nonexhaustive-delayqueue" }
nym-sphinx = { path = "../nymsphinx" }
nym-sphinx-params = { path = "../nymsphinx/params" }
nym-statistics-common = { path = "../statistics" }
nym-pemstore = { path = "../pemstore" }
nym-topology = { path = "../topology", features = ["persistence"] }
Expand Down Expand Up @@ -117,12 +125,15 @@ features = ["wasm-bindgen"]

[dev-dependencies]
tempfile = { workspace = true }
toml.workspace = true

[features]
default = []
default = ["enable-cfg"]
cli = ["clap", "comfy-table"]
fs-credentials-storage = ["nym-credential-storage/persistent-storage"]
fs-surb-storage = ["nym-client-core-surb-storage/fs-surb-storage"]
fs-gateways-storage = ["nym-client-core-gateways-storage/fs-gateways-storage"]
wasm = ["nym-gateway-client/wasm"]
metrics-server = []

enable-cfg = []
109 changes: 109 additions & 0 deletions common/client-core/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use nym_client_core_config_types::Config;

use std::io::{BufWriter, Write};

fn main() {
write_conditional_default();

println!("cargo:rerun-if-changed=build.rs");
}

#[allow(unused)]
const DEFAULT_CONFIG_FILE_NAME: &str = "nymvpn-config.toml";

const CONFIG_INIT_FN_PREAMBLE: &[u8] = br#"
pub fn new_bootstrapped<S1, S2>(id: S1, version: S2) -> Config
where
S1: Into<String>,
S2: Into<String>,
{
use nym_sphinx_params::{PacketSize, PacketType};
let mut cfg: Config = "#;

const CONFIG_INIT_FN_EPILOGUE: &[u8] = br#";
cfg.client.id = id.into();
cfg.client.version = version.into();
cfg
}"#;

// #[cfg(feature = "enable-cfg")]
// const CUSTOM_BREAK: &[u8] = br#"
// #[cfg(feature="enable-cfg")]
// "#;

// #[cfg(not(feature = "enable-cfg"))]
// const DEFAULT_BREAK: &[u8] = br#"
// #[cfg(not(feature="enable-cfg"))]
// "#;

// const CONFIG_INIT_FN_PREAMBLE: &[u8] = br#"
// impl Default for BaseClientConfig {
// fn default() -> Self {
// use config_types::Keys;

// Self("#;

// const DEFAULT_CONFIG_EPILOGUE: &[u8] = br#"
// )
// }
// }"#;

// #[cfg(feature = "enable-cfg")]
// const CUSTOM_BREAK: &[u8] = br#"
// #[cfg(feature="enable-cfg")]
// "#;

// #[cfg(not(feature = "enable-cfg"))]
// const DEFAULT_BREAK: &[u8] = br#"
// #[cfg(not(feature="enable-cfg"))]
// "#;

fn write_conditional_default() {
// creating a string with the values from our default Config object
let mut array_string = BufWriter::new(Vec::new());
array_string.write(CONFIG_INIT_FN_PREAMBLE).unwrap();

#[cfg(feature = "enable-cfg")]
write_custom_config(&mut array_string);

#[cfg(not(feature = "enable-cfg"))]
{
let default_config = Config::new("", "");
uneval::write(default_config, &mut array_string).unwrap();
}

array_string.write(CONFIG_INIT_FN_EPILOGUE).unwrap();

// write the string to a file. OUT_DIR environment variable is defined by cargo
let out_dir = std::env::var("OUT_DIR").unwrap();
let dest_path = std::path::Path::new(&out_dir).join("default.rs");

let out_str = String::from_utf8(array_string.into_inner().unwrap()).unwrap();
std::fs::write(&dest_path, out_str).unwrap();
}

#[cfg(feature = "enable-cfg")]
pub(crate) fn write_custom_config(out: impl Write) {
// allow the name of the file we draw hardcoded values from to be set by an
// environment variable at compile time.
let cfg_file_name = option_env!("NYMVPN_CONFIG_PATH").unwrap_or(DEFAULT_CONFIG_FILE_NAME);

// set reasons to rebuild
println!("cargo:rerun-if-changed={cfg_file_name}");
println!("cargo:rerun-if-env-changed=NYMVPN_CONFIG_PATH");

let path = std::path::PathBuf::from(cfg_file_name);
let cfg_file_path = if path.is_absolute() {
path
} else {
let workspace_path = std::env::var("CARGO_MANIFEST_DIR").unwrap();
std::path::Path::new(&workspace_path).join(cfg_file_name)
};

let config_str: String = std::fs::read_to_string(cfg_file_path).unwrap();
let config: Config = toml::from_str(&config_str).unwrap();

uneval::write(config, out).unwrap();
}
54 changes: 54 additions & 0 deletions common/client-core/nymvpn-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

[client]
version = ""
id = ""
disabled_credentials_mode = true
nyxd_urls = ["https://rpc.nymtech.net/"]
nym_api_urls = ["https://validator.nymtech.net/api/"]

[debug.traffic]
average_packet_delay = "50ms"
message_sending_average_delay = "20ms"
disable_main_poisson_packet_distribution = false
deterministic_route_selection = false
primary_packet_size = "regular"
packet_type = "mix"

[debug.cover_traffic]
loop_cover_traffic_average_delay = "200ms"
cover_traffic_primary_size_ratio = 0.7
disable_loop_cover_traffic_stream = false

[debug.gateway_connection]
gateway_response_timeout = "5m"

[debug.acknowledgements]
average_ack_delay = "50ms"
ack_wait_multiplier = 1.5
ack_wait_addition = "1s 500ms"

[debug.topology]
topology_refresh_rate = "5m"
topology_resolution_timeout = "5s"
disable_refreshing = false
max_startup_gateway_waiting_period = "1h 10m"
topology_structure = "NymApi"
minimum_mixnode_performance = 50
minimum_gateway_performance = 50
use_extended_topology = false
ignore_egress_epoch_role = false

[debug.reply_surbs]
minimum_reply_surb_storage_threshold = 10
maximum_reply_surb_storage_threshold = 200
minimum_reply_surb_request_size = 10
maximum_reply_surb_request_size = 100
maximum_allowed_reply_surb_request_size = 500
maximum_reply_surb_rerequest_waiting_period = "10s"
maximum_reply_surb_drop_waiting_period = "5m"
maximum_reply_surb_age = "12h"
maximum_reply_key_age = "1day"

[debug.stats_reporting]
enabled = true
reporting_interval = "5m"
37 changes: 37 additions & 0 deletions common/client-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,40 @@ pub use nym_client_core_config_types::old::{
old_config_v1_1_33,
};
pub use nym_client_core_config_types::*;



include!(concat!(env!("OUT_DIR"), "/default.rs"));


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn dump_default() {
let cfg = Config::new("","");
println!("{}", toml::to_string(&cfg).unwrap());
}

#[test]
fn bootstrapped_config() {
#[cfg(not(feature = "enable-cfg"))]
{
let config = new_bootstrapped("id1", "v0.0.0");
assert_eq!(config.client.id, "id1");
assert_eq!(config.client.version, "v0.0.0");
assert_eq!(config.client.disabled_credentials_mode, true);

assert_eq!(config.debug.topology.use_extended_topology, false);
assert_eq!(config.debug.stats_reporting.enabled, true);
}
#[cfg(feature = "enable-cfg")]
{
let config = new_bootstrapped("id2", "v0.0.0-beta");
assert_eq!(config.client.id, "id2");
assert_eq!(config.client.version, "v0.0.0-beta");
assert_eq!(config.client.disabled_credentials_mode, true);
}
}
}
14 changes: 7 additions & 7 deletions common/nymsphinx/params/src/packet_sizes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,30 @@ pub enum InvalidPacketSize {
pub enum PacketSize {
// for example instant messaging use case
#[default]
#[serde(rename = "regular")]
#[serde(alias = "regular")]
RegularPacket = 1,

// for sending SURB-ACKs
#[serde(rename = "ack")]
#[serde(alias = "ack")]
AckPacket = 2,

// for example for streaming fast and furious in uncompressed 10bit 4K HDR quality
#[serde(rename = "extended32")]
#[serde(alias = "extended32")]
ExtendedPacket32 = 3,

// for example for streaming fast and furious in heavily compressed lossy RealPlayer quality
#[serde(rename = "extended8")]
#[serde(alias = "extended8")]
ExtendedPacket8 = 4,

// for example for streaming fast and furious in compressed XviD quality
#[serde(rename = "extended16")]
#[serde(alias = "extended16")]
ExtendedPacket16 = 5,

#[serde(rename = "outfox_regular")]
#[serde(alias = "outfox_regular")]
OutfoxRegularPacket = 6,

// for sending SURB-ACKs
#[serde(rename = "outfox_ack")]
#[serde(alias = "outfox_ack")]
OutfoxAckPacket = 7,
}

Expand Down
6 changes: 3 additions & 3 deletions common/nymsphinx/params/src/packet_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ pub enum PacketType {
/// Represents 'normal' packet sent through the network that should be delayed by an appropriate
/// value at each hop.
#[default]
#[serde(rename = "mix")]
#[serde(alias = "mix")]
#[serde(alias = "sphinx")]
Mix = 0,

/// Represents a packet that should be sent through the network as fast as possible.
#[deprecated]
#[serde(rename = "unsupported-mix-vpn")]
#[serde(alias = "unsupported-mix-vpn")]
Vpn = 1,

/// Abusing this to add Outfox support
#[serde(rename = "outfox")]
#[serde(alias = "outfox")]
Outfox = 2,
}

Expand Down

0 comments on commit 0be8602

Please sign in to comment.