-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathrpc.rs
376 lines (342 loc) · 12.4 KB
/
rpc.rs
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// This file is part of Astar.
// Copyright (C) Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.
//! Astar RPCs implementation.
use fc_rpc::{
Eth, EthApiServer, EthBlockDataCacheTask, EthFilter, EthFilterApiServer, EthPubSub,
EthPubSubApiServer, Net, NetApiServer, OverrideHandle, Web3, Web3ApiServer,
};
use fc_rpc_core::types::{FeeHistoryCache, FilterPool};
use jsonrpsee::RpcModule;
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
use sc_client_api::{
AuxStore, Backend, BlockchainEvents, StateBackend, StorageProvider, UsageProvider,
};
use sc_network::NetworkService;
use sc_network_sync::SyncingService;
use sc_rpc::dev::DevApiServer;
pub use sc_rpc::{DenyUnsafe, SubscriptionTaskExecutor};
use sc_transaction_pool::{ChainApi, Pool};
use sc_transaction_pool_api::TransactionPool;
use sp_api::{CallApiAt, ProvideRuntimeApi};
use sp_block_builder::BlockBuilder;
use sp_blockchain::{
Backend as BlockchainBackend, Error as BlockChainError, HeaderBackend, HeaderMetadata,
};
use sp_consensus_aura::{sr25519::AuthorityId as AuraId, AuraApi};
use sp_runtime::traits::BlakeTwo256;
use std::sync::Arc;
use substrate_frame_rpc_system::{System, SystemApiServer};
#[cfg(feature = "evm-tracing")]
use moonbeam_rpc_debug::{Debug, DebugServer};
#[cfg(feature = "evm-tracing")]
use moonbeam_rpc_trace::{Trace, TraceServer};
// TODO: get rid of this completely now that it's part of frontier?
#[cfg(feature = "evm-tracing")]
use moonbeam_rpc_txpool::{TxPool as MoonbeamTxPool, TxPoolServer};
use astar_primitives::*;
#[cfg(feature = "evm-tracing")]
pub mod tracing;
#[cfg(feature = "evm-tracing")]
#[derive(Clone)]
pub struct EvmTracingConfig {
pub tracing_requesters: tracing::RpcRequesters,
pub trace_filter_max_count: u32,
pub enable_txpool: bool,
}
// TODO This is copied from frontier. It should be imported instead after
// https://github.com/paritytech/frontier/issues/333 is solved
pub fn open_frontier_backend<C>(
client: Arc<C>,
config: &sc_service::Configuration,
) -> Result<Arc<fc_db::kv::Backend<Block>>, String>
where
C: sp_blockchain::HeaderBackend<Block>,
{
let config_dir = config.base_path.config_dir(config.chain_spec.id());
let path = config_dir.join("frontier").join("db");
Ok(Arc::new(fc_db::kv::Backend::<Block>::new(
client,
&fc_db::kv::DatabaseSettings {
source: fc_db::DatabaseSource::RocksDb {
path,
cache_size: 0,
},
},
)?))
}
pub struct AstarEthConfig<C, BE>(std::marker::PhantomData<(C, BE)>);
impl<C, BE> fc_rpc::EthConfig<Block, C> for AstarEthConfig<C, BE>
where
C: sc_client_api::StorageProvider<Block, BE> + Sync + Send + 'static,
BE: Backend<Block> + 'static,
{
// Use to override (adapt) evm call to precompiles for proper gas estimation.
// We are not aware of any of our precompile that require this.
type EstimateGasAdapter = ();
// This assumes the use of HashedMapping<BlakeTwo256> for address mapping
type RuntimeStorageOverride =
fc_rpc::frontier_backend_client::SystemAccountId32StorageOverride<Block, C, BE>;
}
/// Full client dependencies
pub struct FullDeps<C, P, A: ChainApi> {
/// The client instance to use.
pub client: Arc<C>,
/// Transaction pool instance.
pub pool: Arc<P>,
/// Graph pool instance.
pub graph: Arc<Pool<A>>,
/// Network service
pub network: Arc<NetworkService<Block, Hash>>,
/// Chain syncing service
pub sync: Arc<SyncingService<Block>>,
/// Whether to deny unsafe calls
pub deny_unsafe: DenyUnsafe,
/// The Node authority flag
pub is_authority: bool,
/// Frontier Backend.
pub frontier_backend: Arc<dyn fc_api::Backend<Block>>,
/// EthFilterApi pool.
pub filter_pool: FilterPool,
/// Maximum fee history cache size.
pub fee_history_limit: u64,
/// Fee history cache.
pub fee_history_cache: FeeHistoryCache,
/// Ethereum data access overrides.
pub overrides: Arc<OverrideHandle<Block>>,
/// Cache for Ethereum block data.
pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,
/// Enable EVM RPC servers
pub enable_evm_rpc: bool,
/// Command sink for manual sealing
#[cfg(feature = "manual-seal")]
pub command_sink:
Option<futures::channel::mpsc::Sender<sc_consensus_manual_seal::EngineCommand<Hash>>>,
}
/// Instantiate all RPC extensions and Tracing RPC.
#[cfg(feature = "evm-tracing")]
pub fn create_full<C, P, BE, A>(
deps: FullDeps<C, P, A>,
subscription_task_executor: SubscriptionTaskExecutor,
pubsub_notification_sinks: Arc<
fc_mapping_sync::EthereumBlockNotificationSinks<
fc_mapping_sync::EthereumBlockNotification<Block>,
>,
>,
tracing_config: EvmTracingConfig,
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
where
C: ProvideRuntimeApi<Block>
+ HeaderBackend<Block>
+ UsageProvider<Block>
+ CallApiAt<Block>
+ AuxStore
+ StorageProvider<Block, BE>
+ HeaderMetadata<Block, Error = BlockChainError>
+ BlockchainEvents<Block>
+ Send
+ Sync
+ 'static,
C: sc_client_api::BlockBackend<Block>,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
+ fp_rpc::ConvertTransactionRuntimeApi<Block>
+ fp_rpc::EthereumRuntimeRPCApi<Block>
+ BlockBuilder<Block>
+ AuraApi<Block, AuraId>
+ moonbeam_rpc_primitives_debug::DebugRuntimeApi<Block>
+ moonbeam_rpc_primitives_txpool::TxPoolRuntimeApi<Block>,
P: TransactionPool<Block = Block> + Sync + Send + 'static,
BE: Backend<Block> + 'static,
BE::State: StateBackend<BlakeTwo256>,
BE::Blockchain: BlockchainBackend<Block>,
A: ChainApi<Block = Block> + 'static,
{
let client = Arc::clone(&deps.client);
let graph = Arc::clone(&deps.graph);
let mut io = create_full_rpc(deps, subscription_task_executor, pubsub_notification_sinks)?;
if tracing_config.enable_txpool {
io.merge(MoonbeamTxPool::new(Arc::clone(&client), graph).into_rpc())?;
}
if let Some(trace_filter_requester) = tracing_config.tracing_requesters.trace {
io.merge(
Trace::new(
client,
trace_filter_requester,
tracing_config.trace_filter_max_count,
)
.into_rpc(),
)?;
}
if let Some(debug_requester) = tracing_config.tracing_requesters.debug {
io.merge(Debug::new(debug_requester).into_rpc())?;
}
Ok(io)
}
/// Instantiate all RPC extensions.
#[cfg(not(feature = "evm-tracing"))]
pub fn create_full<C, P, BE, A>(
deps: FullDeps<C, P, A>,
subscription_task_executor: SubscriptionTaskExecutor,
pubsub_notification_sinks: Arc<
fc_mapping_sync::EthereumBlockNotificationSinks<
fc_mapping_sync::EthereumBlockNotification<Block>,
>,
>,
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
where
C: ProvideRuntimeApi<Block>
+ HeaderBackend<Block>
+ UsageProvider<Block>
+ CallApiAt<Block>
+ AuxStore
+ StorageProvider<Block, BE>
+ HeaderMetadata<Block, Error = BlockChainError>
+ BlockchainEvents<Block>
+ Send
+ Sync
+ 'static,
C: sc_client_api::BlockBackend<Block>,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
+ fp_rpc::ConvertTransactionRuntimeApi<Block>
+ fp_rpc::EthereumRuntimeRPCApi<Block>
+ BlockBuilder<Block>
+ AuraApi<Block, AuraId>,
P: TransactionPool<Block = Block> + Sync + Send + 'static,
BE: Backend<Block> + 'static,
BE::State: StateBackend<BlakeTwo256>,
BE::Blockchain: BlockchainBackend<Block>,
A: ChainApi<Block = Block> + 'static,
{
create_full_rpc(deps, subscription_task_executor, pubsub_notification_sinks)
}
fn create_full_rpc<C, P, BE, A>(
deps: FullDeps<C, P, A>,
subscription_task_executor: SubscriptionTaskExecutor,
pubsub_notification_sinks: Arc<
fc_mapping_sync::EthereumBlockNotificationSinks<
fc_mapping_sync::EthereumBlockNotification<Block>,
>,
>,
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
where
C: ProvideRuntimeApi<Block>
+ UsageProvider<Block>
+ HeaderBackend<Block>
+ CallApiAt<Block>
+ AuxStore
+ StorageProvider<Block, BE>
+ HeaderMetadata<Block, Error = BlockChainError>
+ BlockchainEvents<Block>
+ Send
+ Sync
+ 'static,
C: sc_client_api::BlockBackend<Block>,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
+ fp_rpc::ConvertTransactionRuntimeApi<Block>
+ fp_rpc::EthereumRuntimeRPCApi<Block>
+ BlockBuilder<Block>
+ AuraApi<Block, AuraId>,
P: TransactionPool<Block = Block> + Sync + Send + 'static,
BE: Backend<Block> + 'static,
BE::State: StateBackend<BlakeTwo256>,
BE::Blockchain: BlockchainBackend<Block>,
A: ChainApi<Block = Block> + 'static,
{
let mut io = RpcModule::new(());
let FullDeps {
client,
pool,
graph,
network,
sync,
deny_unsafe,
is_authority,
frontier_backend,
filter_pool,
fee_history_limit,
fee_history_cache,
overrides,
block_data_cache,
enable_evm_rpc,
#[cfg(feature = "manual-seal")]
command_sink,
} = deps;
io.merge(System::new(client.clone(), pool.clone(), deny_unsafe).into_rpc())?;
io.merge(TransactionPayment::new(client.clone()).into_rpc())?;
io.merge(sc_rpc::dev::Dev::new(client.clone(), deny_unsafe).into_rpc())?;
#[cfg(feature = "manual-seal")]
if let Some(command_sink) = command_sink {
use sc_consensus_manual_seal::rpc::ManualSealApiServer;
io.merge(sc_consensus_manual_seal::rpc::ManualSeal::new(command_sink).into_rpc())?;
}
if !enable_evm_rpc {
return Ok(io);
}
let no_tx_converter: Option<fp_rpc::NoTransactionConverter> = None;
io.merge(
Eth::<_, _, _, _, _, _, _, ()>::new(
client.clone(),
pool.clone(),
graph.clone(),
no_tx_converter,
sync.clone(),
Default::default(),
overrides.clone(),
frontier_backend.clone(),
is_authority,
block_data_cache.clone(),
fee_history_cache,
fee_history_limit,
// Allow 10x max allowed weight for non-transactional calls
10,
None,
crate::parachain::PendingCrateInherentDataProvider::new(client.clone()),
Some(Box::new(
crate::parachain::AuraConsensusDataProviderFallback::new(client.clone()),
)),
)
.replace_config::<AstarEthConfig<C, BE>>()
.into_rpc(),
)?;
let max_past_logs: u32 = 10_000;
let max_stored_filters: usize = 500;
io.merge(
EthFilter::new(
client.clone(),
frontier_backend,
graph.clone(),
filter_pool,
max_stored_filters,
max_past_logs,
block_data_cache,
)
.into_rpc(),
)?;
io.merge(Net::new(client.clone(), network.clone(), true).into_rpc())?;
io.merge(Web3::new(client.clone()).into_rpc())?;
io.merge(
EthPubSub::new(
pool,
client.clone(),
sync,
subscription_task_executor,
overrides,
pubsub_notification_sinks,
)
.into_rpc(),
)?;
Ok(io)
}