astar_collator/
evm_tracing_types.rs

1// This file is part of Astar.
2
3// Copyright (C) Stake Technologies Pte.Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later
5
6// Astar is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// Astar is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with Astar. If not, see <http://www.gnu.org/licenses/>.
18
19use crate::rpc::FrontierBackendType;
20use clap::Parser;
21
22/// Defines the frontier backend configuration.
23#[derive(Clone)]
24pub enum FrontierBackendConfig {
25    KeyValue,
26    Sql {
27        pool_size: u32,
28        num_ops_timeout: u32,
29        thread_count: u32,
30        cache_size: u64,
31    },
32}
33
34impl Default for FrontierBackendConfig {
35    fn default() -> FrontierBackendConfig {
36        FrontierBackendConfig::KeyValue
37    }
38}
39
40/// EVM tracing CLI flags.
41#[derive(Debug, PartialEq, Clone)]
42pub enum EthApi {
43    /// Enable EVM debug RPC methods.
44    Debug,
45    /// Enable EVM trace RPC methods.
46    Trace,
47    /// Enable pending transactions RPC methods.
48    TxPool,
49}
50
51impl std::str::FromStr for EthApi {
52    type Err = String;
53
54    fn from_str(s: &str) -> Result<Self, Self::Err> {
55        Ok(match s {
56            "debug" => Self::Debug,
57            "trace" => Self::Trace,
58            "txpool" => Self::TxPool,
59            _ => {
60                return Err(format!(
61                    "`{}` is not recognized as a supported Ethereum Api",
62                    s
63                ))
64            }
65        })
66    }
67}
68
69#[allow(dead_code)]
70#[derive(Clone)]
71/// Overall Frontier (EVM compatibility) configuration:
72/// Controls enabled APIs, tracing, and backend storage.
73pub struct FrontierConfig {
74    /// Enabled EVM tracing flags.
75    pub ethapi: Vec<EthApi>,
76    /// Number of concurrent tracing tasks.
77    pub ethapi_max_permits: u32,
78    /// Maximum number of trace entries a single request of `trace_filter` is allowed to return.
79    /// A request asking for more or an unbounded one going over this limit will both return an
80    /// error.
81    pub ethapi_trace_max_count: u32,
82    /// Duration (in seconds) after which the cache of `trace_filter` for a given block will be
83    /// discarded.
84    pub ethapi_trace_cache_duration: u64,
85    /// Size in bytes of the LRU cache for block data.
86    pub eth_log_block_cache: usize,
87    /// Size in bytes of the LRU cache for transactions statuses data.
88    pub eth_statuses_cache: usize,
89    /// Maximum number of logs in a query.
90    pub max_past_logs: u32,
91    /// Size in bytes of data a raw tracing request is allowed to use.
92    /// Bound the size of memory, stack and storage data.
93    pub tracing_raw_max_memory_usage: usize,
94    /// Configuration for the frontier db backend.
95    pub frontier_backend_config: FrontierBackendConfig,
96}
97
98#[derive(Debug, Parser)]
99pub struct EthApiOptions {
100    /// Enable EVM tracing module on a non-authority node.
101    #[cfg_attr(
102        not(feature = "manual-seal"),
103        clap(
104            long,
105            conflicts_with = "collator",
106            conflicts_with = "validator",
107            value_delimiter = ','
108        )
109    )]
110    #[cfg_attr(feature = "manual-seal", clap(long))]
111    pub ethapi: Vec<EthApi>,
112
113    /// Number of concurrent tracing tasks. Meant to be shared by both "debug" and "trace" modules.
114    #[clap(long, default_value = "10")]
115    pub ethapi_max_permits: u32,
116
117    /// Maximum number of trace entries a single request of `trace_filter` is allowed to return.
118    /// A request asking for more or an unbounded one going over this limit will both return an
119    /// error.
120
121    #[clap(long, default_value = "500")]
122    pub ethapi_trace_max_count: u32,
123
124    /// Duration (in seconds) after which the cache of `trace_filter` for a given block will be
125    /// discarded.
126    #[clap(long, default_value = "300")]
127    pub ethapi_trace_cache_duration: u64,
128
129    /// Size in bytes of the LRU cache for block data.
130    #[clap(long, default_value = "300000000")]
131    pub eth_log_block_cache: usize,
132
133    /// Size in bytes of the LRU cache for transactions statuses data.
134    #[clap(long, default_value = "300000000")]
135    pub eth_statuses_cache: usize,
136
137    /// Size in bytes of data a raw tracing request is allowed to use.
138    /// Bound the size of memory, stack and storage data.
139    #[clap(long, default_value = "20000000")]
140    pub tracing_raw_max_memory_usage: usize,
141
142    /// Maximum number of logs in a query.
143    #[clap(long, default_value = "10000")]
144    pub max_past_logs: u32,
145
146    /// Sets the backend type (KeyValue or Sql)
147    #[clap(long, value_enum, ignore_case = true, default_value_t = FrontierBackendType::default())]
148    pub frontier_backend_type: FrontierBackendType,
149
150    /// Sets the SQL backend's pool size. Only applies when the backend is SQL
151    #[clap(
152        long,
153        default_value = "100",
154        help = "Only used when --frontier-backend-type=sql"
155    )]
156    pub frontier_sql_backend_pool_size: u32,
157
158    /// Sets the SQL backend's query timeout in number of VM ops. Only applies when the backend is SQL
159    #[clap(
160        long,
161        default_value = "10000000",
162        help = "Only used when --frontier-backend-type=sql"
163    )]
164    pub frontier_sql_backend_num_ops_timeout: u32,
165
166    /// Sets the SQL backend's auxiliary thread limit. Only applies when the backend is SQL
167    #[clap(
168        long,
169        default_value = "4",
170        help = "Only used when --frontier-backend-type=sql"
171    )]
172    pub frontier_sql_backend_thread_count: u32,
173
174    /// Sets the SQL backend's query timeout in number of VM ops. Only applies when the backend is SQL
175    /// Default value is 200MB.
176    #[clap(
177        long,
178        default_value = "209715200",
179        help = "Only used when --frontier-backend-type=sql"
180    )]
181    pub frontier_sql_backend_cache_size: u64,
182}
183
184impl EthApiOptions {
185    pub fn new_rpc_config(&self) -> FrontierConfig {
186        FrontierConfig {
187            ethapi: self.ethapi.clone(),
188            ethapi_max_permits: self.ethapi_max_permits,
189            ethapi_trace_max_count: self.ethapi_trace_max_count,
190            ethapi_trace_cache_duration: self.ethapi_trace_cache_duration,
191            eth_log_block_cache: self.eth_log_block_cache,
192            eth_statuses_cache: self.eth_statuses_cache,
193            max_past_logs: self.max_past_logs,
194            tracing_raw_max_memory_usage: self.tracing_raw_max_memory_usage,
195            frontier_backend_config: match self.frontier_backend_type {
196                FrontierBackendType::KeyValue => FrontierBackendConfig::KeyValue,
197                FrontierBackendType::Sql => FrontierBackendConfig::Sql {
198                    pool_size: self.frontier_sql_backend_pool_size,
199                    num_ops_timeout: self.frontier_sql_backend_num_ops_timeout,
200                    thread_count: self.frontier_sql_backend_thread_count,
201                    cache_size: self.frontier_sql_backend_cache_size,
202                },
203            },
204        }
205    }
206}