1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::{path::PathBuf, sync::Arc};
use drml_primitives::{OpaqueBlock as Block, *};
pub struct DvmTaskParams<'a, B, C, BE>
where
B: sp_runtime::traits::Block,
{
pub task_manager: &'a sc_service::TaskManager,
pub client: Arc<C>,
pub substrate_backend: Arc<BE>,
pub dvm_backend: Arc<fc_db::Backend<B>>,
pub filter_pool: Option<fc_rpc_core::types::FilterPool>,
pub is_archive: bool,
pub rpc_config: drml_rpc::EthRpcConfig,
pub fee_history_cache: fc_rpc_core::types::FeeHistoryCache,
pub overrides: Arc<fc_rpc::OverrideHandle<B>>,
pub sync_from: BlockNumber,
}
impl<'a, B, C, BE> DvmTaskParams<'a, B, C, BE>
where
B: sp_runtime::traits::Block,
{
pub fn spawn_task(self) -> drml_rpc::EthRpcRequesters
where
C: 'static
+ sp_api::ProvideRuntimeApi<B>
+ sp_blockchain::HeaderBackend<B>
+ sp_blockchain::HeaderMetadata<B, Error = sp_blockchain::Error>
+ sc_client_api::BlockOf
+ sc_client_api::BlockchainEvents<B>
+ sc_client_api::backend::StorageProvider<B, BE>,
C::Api: sp_block_builder::BlockBuilder<B>
+ fp_rpc::EthereumRuntimeRPCApi<B>
+ moonbeam_rpc_primitives_debug::DebugRuntimeApi<B>,
B: 'static + Send + Sync + sp_runtime::traits::Block<Hash = Hash>,
B::Header: sp_api::HeaderT<Number = BlockNumber>,
BE: 'static + sc_client_api::backend::Backend<B>,
BE::State: sc_client_api::backend::StateBackend<Hashing>,
{
use std::time::Duration;
use futures::StreamExt;
use tokio::sync::Semaphore;
use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};
use fc_rpc::EthTask;
use drml_rpc::{EthRpcConfig, EthRpcRequesters};
use moonbeam_rpc_debug::DebugHandler;
use moonbeam_rpc_trace::CacheTask;
let DvmTaskParams {
task_manager,
client,
substrate_backend,
dvm_backend,
filter_pool,
is_archive,
rpc_config:
EthRpcConfig {
ethapi_debug_targets,
ethapi_max_permits,
ethapi_trace_cache_duration,
fee_history_limit,
..
},
fee_history_cache,
overrides,
sync_from,
} = self;
log::info!("DVM mapping worker starts syncing from {sync_from}");
if is_archive {
task_manager.spawn_essential_handle().spawn(
"frontier-fee-history",
Some("frontier"),
EthTask::fee_history_task(
client.clone(),
overrides.clone(),
fee_history_cache,
fee_history_limit,
),
);
task_manager.spawn_essential_handle().spawn(
"frontier-mapping-sync-worker",
Some("frontier"),
MappingSyncWorker::new(
client.import_notification_stream(),
Duration::new(6, 0),
client.clone(),
substrate_backend.clone(),
dvm_backend.clone(),
3,
sync_from,
SyncStrategy::Normal,
)
.for_each(|_| futures::future::ready(())),
);
if let Some(filter_pool) = filter_pool {
const FILTER_RETAIN_THRESHOLD: u64 = 100;
task_manager.spawn_essential_handle().spawn(
"frontier-filter-pool",
Some("frontier"),
EthTask::filter_pool_task(client.clone(), filter_pool, FILTER_RETAIN_THRESHOLD),
);
}
}
if ethapi_debug_targets.iter().any(|cmd| matches!(cmd.as_str(), "debug" | "trace")) {
let permit_pool = Arc::new(Semaphore::new(ethapi_max_permits as _));
let (trace_filter_task, trace_filter_requester) =
if ethapi_debug_targets.iter().any(|target| target.as_str() == "trace") {
let (trace_filter_task, trace_filter_requester) = CacheTask::create(
client.clone(),
substrate_backend.clone(),
Duration::from_secs(ethapi_trace_cache_duration),
permit_pool.clone(),
overrides.clone(),
);
(Some(trace_filter_task), Some(trace_filter_requester))
} else {
(None, None)
};
let (debug_task, debug_requester) =
if ethapi_debug_targets.iter().any(|target| target.as_str() == "debug") {
let (debug_task, debug_requester) = DebugHandler::task(
client,
substrate_backend,
dvm_backend,
permit_pool,
overrides,
);
(Some(debug_task), Some(debug_requester))
} else {
(None, None)
};
if let Some(trace_filter_task) = trace_filter_task {
task_manager.spawn_essential_handle().spawn(
"trace-filter-cache",
Some("evm-tracing"),
trace_filter_task,
);
}
if let Some(debug_task) = debug_task {
task_manager.spawn_essential_handle().spawn(
"ethapi-debug",
Some("evm-tracing"),
debug_task,
);
}
EthRpcRequesters { debug: debug_requester, trace: trace_filter_requester }
} else {
EthRpcRequesters { debug: None, trace: None }
}
}
}
pub fn db_path(config: &sc_service::Configuration) -> PathBuf {
let config_dir = config
.base_path
.as_ref()
.map(|base_path| base_path.config_dir(config.chain_spec.id()))
.expect("Config dir must be set.");
config_dir.join("dvm").join("db")
}
pub fn open_backend(
config: &sc_service::Configuration,
) -> Result<Arc<fc_db::Backend<Block>>, String> {
use fc_db::{Backend, DatabaseSettings, DatabaseSettingsSrc};
Ok(Arc::new(Backend::<Block>::new(&DatabaseSettings {
source: DatabaseSettingsSrc::RocksDb { path: db_path(config), cache_size: 0 },
})?))
}