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    #[clap(
102        long,
103        conflicts_with = "collator",
104        conflicts_with = "validator",
105        value_delimiter = ','
106    )]
107    pub ethapi: Vec<EthApi>,
108
109    /// Number of concurrent tracing tasks. Meant to be shared by both "debug" and "trace" modules.
110    #[clap(long, default_value = "10")]
111    pub ethapi_max_permits: u32,
112
113    /// Maximum number of trace entries a single request of `trace_filter` is allowed to return.
114    /// A request asking for more or an unbounded one going over this limit will both return an
115    /// error.
116
117    #[clap(long, default_value = "500")]
118    pub ethapi_trace_max_count: u32,
119
120    /// Duration (in seconds) after which the cache of `trace_filter` for a given block will be
121    /// discarded.
122    #[clap(long, default_value = "300")]
123    pub ethapi_trace_cache_duration: u64,
124
125    /// Size in bytes of the LRU cache for block data.
126    #[clap(long, default_value = "300000000")]
127    pub eth_log_block_cache: usize,
128
129    /// Size in bytes of the LRU cache for transactions statuses data.
130    #[clap(long, default_value = "300000000")]
131    pub eth_statuses_cache: usize,
132
133    /// Size in bytes of data a raw tracing request is allowed to use.
134    /// Bound the size of memory, stack and storage data.
135    #[clap(long, default_value = "20000000")]
136    pub tracing_raw_max_memory_usage: usize,
137
138    /// Maximum number of logs in a query.
139    #[clap(long, default_value = "10000")]
140    pub max_past_logs: u32,
141
142    /// Sets the backend type (KeyValue or Sql)
143    #[clap(long, value_enum, ignore_case = true, default_value_t = FrontierBackendType::default())]
144    pub frontier_backend_type: FrontierBackendType,
145
146    /// Sets the SQL backend's pool size. Only applies when the backend is SQL
147    #[clap(
148        long,
149        default_value = "100",
150        help = "Only used when --frontier-backend-type=sql"
151    )]
152    pub frontier_sql_backend_pool_size: u32,
153
154    /// Sets the SQL backend's query timeout in number of VM ops. Only applies when the backend is SQL
155    #[clap(
156        long,
157        default_value = "10000000",
158        help = "Only used when --frontier-backend-type=sql"
159    )]
160    pub frontier_sql_backend_num_ops_timeout: u32,
161
162    /// Sets the SQL backend's auxiliary thread limit. Only applies when the backend is SQL
163    #[clap(
164        long,
165        default_value = "4",
166        help = "Only used when --frontier-backend-type=sql"
167    )]
168    pub frontier_sql_backend_thread_count: u32,
169
170    /// Sets the SQL backend's query timeout in number of VM ops. Only applies when the backend is SQL
171    /// Default value is 200MB.
172    #[clap(
173        long,
174        default_value = "209715200",
175        help = "Only used when --frontier-backend-type=sql"
176    )]
177    pub frontier_sql_backend_cache_size: u64,
178}
179
180impl EthApiOptions {
181    pub fn new_rpc_config(&self) -> FrontierConfig {
182        FrontierConfig {
183            ethapi: self.ethapi.clone(),
184            ethapi_max_permits: self.ethapi_max_permits,
185            ethapi_trace_max_count: self.ethapi_trace_max_count,
186            ethapi_trace_cache_duration: self.ethapi_trace_cache_duration,
187            eth_log_block_cache: self.eth_log_block_cache,
188            eth_statuses_cache: self.eth_statuses_cache,
189            max_past_logs: self.max_past_logs,
190            tracing_raw_max_memory_usage: self.tracing_raw_max_memory_usage,
191            frontier_backend_config: match self.frontier_backend_type {
192                FrontierBackendType::KeyValue => FrontierBackendConfig::KeyValue,
193                FrontierBackendType::Sql => FrontierBackendConfig::Sql {
194                    pool_size: self.frontier_sql_backend_pool_size,
195                    num_ops_timeout: self.frontier_sql_backend_num_ops_timeout,
196                    thread_count: self.frontier_sql_backend_thread_count,
197                    cache_size: self.frontier_sql_backend_cache_size,
198                },
199            },
200        }
201    }
202}