Skip to main content

miden_node_utils/
clap.rs

1//! Public module for share clap pieces to reduce duplication
2
3use std::num::NonZeroUsize;
4use std::time::Duration;
5
6use crate::grpc::DEFAULT_MAX_DECODING_MESSAGE_SIZE;
7
8mod rocksdb;
9pub use rocksdb::*;
10
11const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
12const TEST_REQUEST_TIMEOUT: Duration = Duration::from_secs(5);
13
14// Formats a Duration into a human-readable string for display in clap help text and yields a
15// &'static str by _leaking_ the string deliberately.
16pub fn duration_to_human_readable_string(duration: Duration) -> &'static str {
17    Box::new(humantime::format_duration(duration).to_string()).leak()
18}
19
20/// Maximum encoded size accepted by every gRPC client and server in a process.
21#[derive(clap::Args, Copy, Clone, Debug, PartialEq, Eq)]
22pub struct GrpcMessageSizeOptions {
23    /// Maximum encoded bytes in each received gRPC request or response, including stream items.
24    #[arg(long = "grpc.max-decoding-message-size", default_value_t = NonZeroUsize::new(DEFAULT_MAX_DECODING_MESSAGE_SIZE).unwrap(), value_name = "BYTES")]
25    pub max_decoding_message_size: NonZeroUsize,
26}
27
28impl Default for GrpcMessageSizeOptions {
29    fn default() -> Self {
30        Self {
31            max_decoding_message_size: NonZeroUsize::new(DEFAULT_MAX_DECODING_MESSAGE_SIZE)
32                .unwrap(),
33        }
34    }
35}
36
37#[derive(clap::Args, Copy, Clone, Debug, PartialEq, Eq)]
38pub struct GrpcOptions {
39    #[command(flatten)]
40    pub message_size: GrpcMessageSizeOptions,
41    /// Maximum duration a gRPC request is allocated before being dropped by the server.
42    ///
43    /// This may occur if the server is overloaded or due to an internal bug.
44    #[arg(
45        long = "grpc.timeout",
46        default_value = duration_to_human_readable_string(DEFAULT_REQUEST_TIMEOUT),
47        value_parser = humantime::parse_duration,
48        value_name = "DURATION"
49    )]
50    pub request_timeout: Duration,
51}
52
53impl Default for GrpcOptions {
54    fn default() -> Self {
55        Self {
56            request_timeout: DEFAULT_REQUEST_TIMEOUT,
57            message_size: GrpcMessageSizeOptions::default(),
58        }
59    }
60}
61
62impl GrpcOptions {
63    pub fn test() -> Self {
64        Self {
65            request_timeout: TEST_REQUEST_TIMEOUT,
66            ..Self::default()
67        }
68    }
69}
70
71/// Collection of per usage storage backend configurations, plus store write-path tuning.
72#[derive(clap::Args, Clone, Debug, PartialEq, Eq)]
73pub struct StorageOptions {
74    #[clap(flatten)]
75    pub account_tree: AccountTreeRocksDbOptions,
76    #[clap(flatten)]
77    pub nullifier_tree: NullifierTreeRocksDbOptions,
78    #[clap(flatten)]
79    pub account_state_forest: AccountStateForestRocksDbOptions,
80
81    /// Whether the store's apply-block thread pool runs at raised OS thread priority (best-effort).
82    #[arg(
83        id = "apply_block_thread_priority",
84        long = "apply_block.thread_priority",
85        default_value_t = true,
86        action = clap::ArgAction::Set,
87        value_name = "BOOL"
88    )]
89    pub apply_block_thread_priority: bool,
90}
91
92impl Default for StorageOptions {
93    fn default() -> Self {
94        Self {
95            account_tree: AccountTreeRocksDbOptions::default(),
96            nullifier_tree: NullifierTreeRocksDbOptions::default(),
97            account_state_forest: AccountStateForestRocksDbOptions::default(),
98            apply_block_thread_priority: true,
99        }
100    }
101}
102
103impl StorageOptions {
104    /// Benchmark setup.
105    ///
106    /// These values were determined during development of `LargeSmt`
107    pub fn bench() -> Self {
108        let account_tree = AccountTreeRocksDbOptions {
109            max_open_fds: self::rocksdb::BENCH_ROCKSDB_MAX_OPEN_FDS,
110            cache_size_in_bytes: self::rocksdb::DEFAULT_ROCKSDB_CACHE_SIZE,
111            durability_mode: None,
112        };
113        let nullifier_tree = NullifierTreeRocksDbOptions {
114            max_open_fds: BENCH_ROCKSDB_MAX_OPEN_FDS,
115            cache_size_in_bytes: DEFAULT_ROCKSDB_CACHE_SIZE,
116            durability_mode: None,
117        };
118        let account_state_forest = AccountStateForestRocksDbOptions {
119            max_open_fds: BENCH_ROCKSDB_MAX_OPEN_FDS,
120            cache_size_in_bytes: DEFAULT_ROCKSDB_CACHE_SIZE,
121            durability_mode: None,
122        };
123        Self {
124            account_tree,
125            nullifier_tree,
126            account_state_forest,
127            apply_block_thread_priority: true,
128        }
129    }
130}