Skip to content

Commit

Permalink
Merge pull request #2790 from dusk-network/2494/rusk-toml-dep
Browse files Browse the repository at this point in the history
rusk: Upgrade toml to workspace version
  • Loading branch information
HDauven authored Oct 29, 2024
2 parents 3c09734 + 0b5ccfb commit 1728f90
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
11 changes: 9 additions & 2 deletions rusk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ semver = { workspace = true }
anyhow = { workspace = true }
rustc_tools_util = { workspace = true }
rand = { workspace = true }
toml = "=0.5.11"
toml = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_with = { workspace = true, features = ["hex"] }
Expand Down Expand Up @@ -92,7 +92,14 @@ criterion = { workspace = true }
rustc_tools_util = { workspace = true }

[features]
default = ["ephemeral", "recovery-state", "recovery-keys", "prover", "chain", "http-wasm"]
default = [
"ephemeral",
"recovery-state",
"recovery-keys",
"prover",
"chain",
"http-wasm",
]
ephemeral = ["dep:rusk-recovery", "dep:tempfile", "recovery-state", "chain"]
recovery-state = ["rusk-recovery/state", "dep:tempfile"]
recovery-keys = ["rusk-recovery/keys"]
Expand Down
4 changes: 2 additions & 2 deletions rusk/default.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#key = <path_of_key>

# The default max cost for feeder calls is the maximum representable. Put in a
# normal number to change
#feeder_call_gas = u64::MAX
# a string wrapped number up to u64::MAX
#feeder_call_gas = "18446744073709551615"

#ws_sub_channel_cap = 16,
#ws_event_channel_cap = 1024,
Expand Down
70 changes: 65 additions & 5 deletions rusk/src/bin/config/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use std::path::PathBuf;

use hyper::HeaderMap;
use serde::{Deserialize, Serialize};
use serde::de::{self, Unexpected};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::args::Args;

Expand All @@ -17,7 +18,11 @@ pub struct HttpConfig {
pub key: Option<PathBuf>,
#[serde(default = "default_listen")]
pub listen: bool,
#[serde(default = "default_feeder_call_gas")]
#[serde(
default = "default_feeder_call_gas",
deserialize_with = "deserialize_feeder_call_gas",
serialize_with = "serialize_feeder_call_gas"
)]
pub feeder_call_gas: u64,
listen_address: Option<String>,
#[serde(default = "default_ws_sub_channel_cap")]
Expand All @@ -28,6 +33,36 @@ pub struct HttpConfig {
pub headers: HeaderMap,
}

// Custom deserialization function for `feeder_call_gas`.
// TOML values are limited to `i64::MAX` in `toml-rs`, so we parse `u64` as a
// string.
fn deserialize_feeder_call_gas<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: Deserializer<'de>,
{
String::deserialize(deserializer)?
.parse::<u64>()
.map_err(|_| {
de::Error::invalid_value(
Unexpected::Str("a valid u64 as a string"),
&"a u64 integer",
)
})
}

// Custom serialization function for `feeder_call_gas`.
// Serializes `u64` as a string to bypass `i64::MAX` limitations in TOML
// parsing.
fn serialize_feeder_call_gas<S>(
value: &u64,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&value.to_string())
}

impl Default for HttpConfig {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -119,7 +154,7 @@ mod vec_header_map {
struct TupleVecVisitor;

impl<'de> Visitor<'de> for TupleVecVisitor {
type Value = Vec<(&'de str, &'de str)>;
type Value = Vec<(String, String)>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a tuple header name and value")
Expand All @@ -146,7 +181,7 @@ mod vec_header_map {
for (k, v) in tuple_vec {
let name = HeaderName::from_bytes(k.as_bytes())
.map_err(D::Error::custom)?;
let value = HeaderValue::from_str(v).map_err(D::Error::custom)?;
let value = HeaderValue::from_str(&v).map_err(D::Error::custom)?;
headers.insert(name, value);
}

Expand Down Expand Up @@ -179,12 +214,37 @@ mod tests {
#[test]
fn deserialize_config() {
let config_str = r#"listen = true
feeder_call_gas = 18446744
feeder_call_gas = "18446744"
ws_sub_channel_cap = 16
ws_event_channel_cap = 1024
headers = [["name1", "value1"], ["name2", "value2"]]"#;

toml::from_str::<HttpConfig>(config_str)
.expect("deserializing config should succeed");
}

#[test]
fn deserialize_invalid_feeder_call_gas() {
let config_str = r#"feeder_call_gas = "invalid_number""#;
let result = toml::from_str::<HttpConfig>(config_str);
assert!(result.is_err());

let config_str = r#"feeder_call_gas = 18446744"#;
let result = toml::from_str::<HttpConfig>(config_str);
assert!(result.is_err());
}

#[test]
fn deserialize_large_feeder_call_gas() {
// A feeder_call_gas value beyond i64::MAX, but within u64 limits
let config_str = r#"feeder_call_gas = "9999999999999999999""#;
let config: HttpConfig = toml::from_str(config_str)
.expect("parsing large u64 value should succeed");
assert_eq!(config.feeder_call_gas, 9999999999999999999);

let config_str = r#"feeder_call_gas = "18446744073709551615""#; // u64::MAX
let config: HttpConfig = toml::from_str(config_str)
.expect("parsing u64::MAX should succeed");
assert_eq!(config.feeder_call_gas, u64::MAX);
}
}

0 comments on commit 1728f90

Please sign in to comment.