Skip to content

Commit

Permalink
rusk: upgrade toml to workspace version
Browse files Browse the repository at this point in the history
- Updated `toml` version in Rusk to workspace version
- Fixed u64 (de)serialization due to `toml` limitations
- Ingest `feeder_gas_call` as String, update example.toml accordingly
  • Loading branch information
HDauven committed Oct 29, 2024
1 parent 4f267cf commit 4ee4eaa
Show file tree
Hide file tree
Showing 3 changed files with 62 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
#feeder_call_gas = "18446744073709551615"

#ws_sub_channel_cap = 16,
#ws_event_channel_cap = 1024,
Expand Down
56 changes: 51 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,23 @@ 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());
}
}

0 comments on commit 4ee4eaa

Please sign in to comment.