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 /// Size in bytes of the LRU cache used by `trace_filter` to store pre-computed block traces.
83 /// Oldest entries are evicted when this budget is exceeded.
84 pub ethapi_trace_cache_size: u64,
85 /// Maximum number of blocks a single `trace_filter` request may span.
86 /// Requests with a wider range are rejected to prevent runaway memory use.
87 pub trace_filter_max_block_range: u32,
88 /// Size in bytes of the LRU cache for block data.
89 pub eth_log_block_cache: usize,
90 /// Size in bytes of the LRU cache for transactions statuses data.
91 pub eth_statuses_cache: usize,
92 /// Maximum number of logs in a query.
93 pub max_past_logs: u32,
94 /// Size in bytes of data a raw tracing request is allowed to use.
95 /// Bound the size of memory, stack and storage data.
96 pub tracing_raw_max_memory_usage: usize,
97 /// Configuration for the frontier db backend.
98 pub frontier_backend_config: FrontierBackendConfig,
99}
100
101#[derive(Debug, Parser)]
102pub struct EthApiOptions {
103 /// Enable EVM tracing module on a non-authority node.
104 #[clap(
105 long,
106 conflicts_with = "collator",
107 conflicts_with = "validator",
108 value_delimiter = ','
109 )]
110 pub ethapi: Vec<EthApi>,
111
112 /// Number of concurrent tracing tasks. Meant to be shared by both "debug" and "trace" modules.
113 #[clap(long, default_value = "10")]
114 pub ethapi_max_permits: u32,
115
116 /// Maximum number of trace entries a single request of `trace_filter` is allowed to return.
117 /// A request asking for more or an unbounded one going over this limit will both return an
118 /// error.
119
120 #[clap(long, default_value = "500")]
121 pub ethapi_trace_max_count: u32,
122
123 /// Size in bytes of the LRU cache used by `trace_filter` to store pre-computed block traces.
124 /// Oldest entries are evicted when this budget is exceeded.
125 /// Default value is 100 MB.
126 #[clap(long, default_value = "104857600")]
127 pub ethapi_trace_cache_size: u64,
128
129 /// Maximum number of blocks a single `trace_filter` request may span.
130 /// Requests wider than this are rejected with an error.
131 #[clap(long, default_value = "1024")]
132 pub trace_filter_max_block_range: u32,
133
134 /// Size in bytes of the LRU cache for block data.
135 #[clap(long, default_value = "300000000")]
136 pub eth_log_block_cache: usize,
137
138 /// Size in bytes of the LRU cache for transactions statuses data.
139 #[clap(long, default_value = "300000000")]
140 pub eth_statuses_cache: usize,
141
142 /// Size in bytes of data a raw tracing request is allowed to use.
143 /// Bound the size of memory, stack and storage data.
144 #[clap(long, default_value = "20000000")]
145 pub tracing_raw_max_memory_usage: usize,
146
147 /// Maximum number of logs in a query.
148 #[clap(long, default_value = "10000")]
149 pub max_past_logs: u32,
150
151 /// Sets the backend type (KeyValue or Sql)
152 #[clap(long, value_enum, ignore_case = true, default_value_t = FrontierBackendType::default())]
153 pub frontier_backend_type: FrontierBackendType,
154
155 /// Sets the SQL backend's pool size. Only applies when the backend is SQL
156 #[clap(
157 long,
158 default_value = "100",
159 help = "Only used when --frontier-backend-type=sql"
160 )]
161 pub frontier_sql_backend_pool_size: u32,
162
163 /// Sets the SQL backend's query timeout in number of VM ops. Only applies when the backend is SQL
164 #[clap(
165 long,
166 default_value = "10000000",
167 help = "Only used when --frontier-backend-type=sql"
168 )]
169 pub frontier_sql_backend_num_ops_timeout: u32,
170
171 /// Sets the SQL backend's auxiliary thread limit. Only applies when the backend is SQL
172 #[clap(
173 long,
174 default_value = "4",
175 help = "Only used when --frontier-backend-type=sql"
176 )]
177 pub frontier_sql_backend_thread_count: u32,
178
179 /// Sets the SQL backend's query timeout in number of VM ops. Only applies when the backend is SQL
180 /// Default value is 200MB.
181 #[clap(
182 long,
183 default_value = "209715200",
184 help = "Only used when --frontier-backend-type=sql"
185 )]
186 pub frontier_sql_backend_cache_size: u64,
187}
188
189impl EthApiOptions {
190 pub fn new_rpc_config(&self) -> FrontierConfig {
191 FrontierConfig {
192 ethapi: self.ethapi.clone(),
193 ethapi_max_permits: self.ethapi_max_permits,
194 ethapi_trace_max_count: self.ethapi_trace_max_count,
195 ethapi_trace_cache_size: self.ethapi_trace_cache_size,
196 trace_filter_max_block_range: self.trace_filter_max_block_range,
197 eth_log_block_cache: self.eth_log_block_cache,
198 eth_statuses_cache: self.eth_statuses_cache,
199 max_past_logs: self.max_past_logs,
200 tracing_raw_max_memory_usage: self.tracing_raw_max_memory_usage,
201 frontier_backend_config: match self.frontier_backend_type {
202 FrontierBackendType::KeyValue => FrontierBackendConfig::KeyValue,
203 FrontierBackendType::Sql => FrontierBackendConfig::Sql {
204 pool_size: self.frontier_sql_backend_pool_size,
205 num_ops_timeout: self.frontier_sql_backend_num_ops_timeout,
206 thread_count: self.frontier_sql_backend_thread_count,
207 cache_size: self.frontier_sql_backend_cache_size,
208 },
209 },
210 }
211 }
212}