Skip to main content

forest/cli_shared/cli/
config.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use super::client::Client;
5use crate::db::db_engine::DbConfig;
6use crate::libp2p::Libp2pConfig;
7use crate::shim::clock::ChainEpoch;
8use crate::shim::econ::TokenAmount;
9use crate::utils::misc::env::is_env_set_and_truthy;
10use crate::{chain_sync::SyncConfig, networks::NetworkChain};
11use serde::{Deserialize, Serialize};
12use std::path::PathBuf;
13
14const FOREST_CHAIN_INDEXER_ENABLED: &str = "FOREST_CHAIN_INDEXER_ENABLED";
15
16/// Structure that defines daemon configuration when process is detached
17#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
18#[cfg_attr(test, derive(derive_quickcheck_arbitrary::Arbitrary))]
19pub struct DaemonConfig {
20    pub user: Option<String>,
21    pub group: Option<String>,
22    pub umask: u16,
23    pub stdout: PathBuf,
24    pub stderr: PathBuf,
25    pub work_dir: PathBuf,
26    pub pid_file: Option<PathBuf>,
27}
28
29impl Default for DaemonConfig {
30    fn default() -> Self {
31        Self {
32            user: None,
33            group: None,
34            umask: 0o027,
35            stdout: "forest.out".into(),
36            stderr: "forest.err".into(),
37            work_dir: ".".into(),
38            pid_file: None,
39        }
40    }
41}
42
43/// Structure that defines events configuration
44#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
45#[cfg_attr(test, derive(derive_quickcheck_arbitrary::Arbitrary))]
46#[serde(default)]
47pub struct EventsConfig {
48    /// Caps the events returned by event-filter queries used by the actor
49    /// events API and the Ethereum event and receipt APIs (`eth_getLogs`,
50    /// `eth_getFilterLogs`, `eth_getFilterChanges`). Set to `0` for no limit.
51    ///
52    /// The cap is a hard limit only when a query's events come from more than
53    /// one tipset. A range whose events all live in a single tipset may
54    /// exceed this value; queries scoped to a single tipset (`block_hash`,
55    /// `eth_getBlockReceipts`) bypass it entirely. `eth_getTransactionReceipt`
56    /// narrows to a single message and is also unaffected.
57    ///
58    /// Self-hosted nodes serving trusted callers can use `0` or a high value.
59    /// Public RPC operators should keep it bounded.
60    #[cfg_attr(test, arbitrary(gen(|g| u32::arbitrary(g) as _)))]
61    pub max_filter_results: usize,
62    /// Maximum block-range span (in epochs) accepted in event-filter queries.
63    pub max_filter_height_range: ChainEpoch,
64    /// Installed `eth` filters idle for longer than this many
65    /// seconds are removed. Set to `0` to never expire filters.
66    pub filter_ttl_secs: u64,
67}
68
69impl Default for EventsConfig {
70    fn default() -> Self {
71        Self {
72            max_filter_results: 10000,
73            max_filter_height_range: 2880,
74            filter_ttl_secs: 3600,
75        }
76    }
77}
78
79/// Structure that defines `FEVM` configuration
80#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
81#[cfg_attr(test, derive(derive_quickcheck_arbitrary::Arbitrary))]
82pub struct FevmConfig {
83    #[cfg_attr(test, arbitrary(gen(|g| u32::arbitrary(g) as _)))]
84    pub eth_trace_filter_max_results: usize,
85}
86
87impl Default for FevmConfig {
88    fn default() -> Self {
89        Self {
90            eth_trace_filter_max_results: 500,
91        }
92    }
93}
94
95#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
96#[cfg_attr(test, derive(derive_quickcheck_arbitrary::Arbitrary))]
97pub struct ChainIndexerConfig {
98    /// Enable indexing Ethereum mappings
99    pub enable_indexer: bool,
100    /// Number of retention epochs for indexed entries. Set to `None` to disable garbage collection.
101    pub gc_retention_epochs: Option<u32>,
102}
103
104impl Default for ChainIndexerConfig {
105    fn default() -> Self {
106        Self {
107            enable_indexer: is_env_set_and_truthy(FOREST_CHAIN_INDEXER_ENABLED).unwrap_or(true),
108            gc_retention_epochs: None,
109        }
110    }
111}
112
113#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
114#[cfg_attr(test, derive(derive_quickcheck_arbitrary::Arbitrary))]
115pub struct FeeConfig {
116    /// Indicates the default max fee for a message
117    #[serde(with = "crate::lotus_json")]
118    pub max_fee: TokenAmount,
119}
120
121impl Default for FeeConfig {
122    fn default() -> Self {
123        // The code is taken from https://github.com/filecoin-project/lotus/blob/release/v1.34.1/node/config/def.go#L39
124        Self {
125            max_fee: TokenAmount::from_atto(70_000_000_000_000_000u64), // 0.07 FIL
126        }
127    }
128}
129
130#[derive(Serialize, Deserialize, PartialEq, Default, Debug, Clone)]
131#[cfg_attr(test, derive(derive_quickcheck_arbitrary::Arbitrary))]
132#[serde(default)]
133pub struct Config {
134    pub chain: NetworkChain,
135    pub client: Client,
136    pub parity_db: crate::db::parity_db_config::ParityDbConfig,
137    pub network: Libp2pConfig,
138    pub sync: SyncConfig,
139    pub daemon: DaemonConfig,
140    pub events: EventsConfig,
141    pub fevm: FevmConfig,
142    pub fee: FeeConfig,
143    pub chain_indexer: ChainIndexerConfig,
144}
145
146impl Config {
147    pub fn db_config(&self) -> &DbConfig {
148        &self.parity_db
149    }
150
151    pub fn chain(&self) -> &NetworkChain {
152        &self.chain
153    }
154}
155
156#[cfg(test)]
157mod test {
158    use quickcheck_macros::quickcheck;
159
160    use super::*;
161
162    #[quickcheck]
163    fn test_config_all_params_under_section(config: Config) {
164        let serialized_config =
165            toml::to_string(&config).expect("could not serialize the configuration");
166        assert_eq!(
167            serialized_config
168                .trim_start()
169                .chars()
170                .next()
171                .expect("configuration empty"),
172            '['
173        )
174    }
175}