Skip to content

Commit

Permalink
fix: ensure torii options use serde default
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Nov 14, 2024
1 parent eecd8bf commit 504b1a9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
12 changes: 8 additions & 4 deletions crates/torii/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ impl ToriiArgs {
self.explorer = config.explorer.unwrap_or_default();
}

if self.indexing == IndexingOptions::default() {
self.indexing = config.indexing.unwrap_or_default();
}
self.indexing.merge(config.indexing.as_ref());

if self.events == EventsOptions::default() {
self.events = config.events.unwrap_or_default();
Expand Down Expand Up @@ -206,7 +204,10 @@ mod test {
world_address = "0x1234"
rpc = "http://0.0.0.0:5050"
db_dir = "/tmp/torii-test"
[indexing]
transactions = false
[events]
raw = true
historical = [
Expand All @@ -231,6 +232,8 @@ mod test {
"false",
"--events.historical",
"a-A",
"--indexing.transactions",
"true",
"--config",
path_str.as_str(),
];
Expand All @@ -243,6 +246,7 @@ mod test {
assert!(!torii_args.events.raw);
assert_eq!(torii_args.events.historical, vec!["a-A".to_string()]);
assert_eq!(torii_args.server, ServerOptions::default());
assert!(torii_args.indexing.transactions);
}

#[test]
Expand Down
48 changes: 48 additions & 0 deletions crates/torii/cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct RelayOptions {
help = "Path to a local identity key file. If not specified, a new identity will be \
generated."
)]
#[serde(default)]
pub local_key_path: Option<String>,

/// Path to a local certificate file. If not specified, a new certificate will be generated
Expand All @@ -70,6 +71,7 @@ pub struct RelayOptions {
help = "Path to a local certificate file. If not specified, a new certificate will be \
generated for WebRTC connections."
)]
#[serde(default)]
pub cert_path: Option<String>,
}

Expand Down Expand Up @@ -100,6 +102,7 @@ pub struct IndexingOptions {

/// Enable indexing pending blocks
#[arg(long = "indexing.pending", action = ArgAction::Set, default_value_t = true, help = "Whether or not to index pending blocks.")]
#[serde(default)]
pub pending: bool,

/// Polling interval in ms
Expand Down Expand Up @@ -127,6 +130,7 @@ pub struct IndexingOptions {
default_value_t = false,
help = "Whether or not to index world transactions and keep them in the database."
)]
#[serde(default)]
pub transactions: bool,

/// ERC contract addresses to index
Expand All @@ -137,6 +141,7 @@ pub struct IndexingOptions {
help = "ERC contract addresses to index. You may only specify ERC20 or ERC721 contracts."
)]
#[serde(deserialize_with = "deserialize_contracts")]
#[serde(default)]
pub contracts: Vec<Contract>,

/// Namespaces to index
Expand All @@ -146,6 +151,7 @@ pub struct IndexingOptions {
help = "The namespaces of the world that torii should index. If empty, all namespaces \
will be indexed."
)]
#[serde(default)]
pub namespaces: Vec<String>,
}

Expand All @@ -164,11 +170,50 @@ impl Default for IndexingOptions {
}
}

impl IndexingOptions {
pub fn merge(&mut self, other: Option<&Self>) {
if let Some(other) = other {
if self.events_chunk_size == DEFAULT_EVENTS_CHUNK_SIZE {
self.events_chunk_size = other.events_chunk_size;
}

if self.blocks_chunk_size == DEFAULT_BLOCKS_CHUNK_SIZE {
self.blocks_chunk_size = other.blocks_chunk_size;
}

if !self.pending {
self.pending = other.pending;
}

if self.polling_interval == DEFAULT_POLLING_INTERVAL {
self.polling_interval = other.polling_interval;
}

if self.max_concurrent_tasks == DEFAULT_MAX_CONCURRENT_TASKS {
self.max_concurrent_tasks = other.max_concurrent_tasks;
}

if !self.transactions {
self.transactions = other.transactions;
}

if self.contracts.is_empty() {
self.contracts = other.contracts.clone();
}

if self.namespaces.is_empty() {
self.namespaces = other.namespaces.clone();
}
}
}
}

#[derive(Debug, clap::Args, Clone, Serialize, Deserialize, PartialEq)]
#[command(next_help_heading = "Events indexing options")]
pub struct EventsOptions {
/// Whether or not to index raw events
#[arg(long = "events.raw", action = ArgAction::Set, default_value_t = true, help = "Whether or not to index raw events.")]
#[serde(default)]
pub raw: bool,

/// Event messages that are going to be treated as historical
Expand All @@ -178,6 +223,7 @@ pub struct EventsOptions {
value_delimiter = ',',
help = "Event messages that are going to be treated as historical during indexing."
)]
#[serde(default)]
pub historical: Vec<String>,
}

Expand Down Expand Up @@ -205,6 +251,7 @@ pub struct ServerOptions {
/// Comma separated list of domains from which to accept cross origin requests.
#[arg(long = "http.cors_origins")]
#[arg(value_delimiter = ',')]
#[serde(default)]
pub http_cors_origins: Option<Vec<String>>,
}

Expand All @@ -222,6 +269,7 @@ pub struct MetricsOptions {
/// For now, metrics will still be collected even if this flag is not set. This only
/// controls whether the metrics server is started or not.
#[arg(long)]
#[serde(default)]
pub metrics: bool,

/// The metrics will be served at the given address.
Expand Down

0 comments on commit 504b1a9

Please sign in to comment.