1
// Copyright 2019-2022 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Moonbeam is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! A collection of node-specific RPC extensions and related background tasks.
18

            
19
pub mod tracing;
20

            
21
use std::{sync::Arc, time::Duration};
22

            
23
use fp_rpc::EthereumRuntimeRPCApi;
24
use sp_block_builder::BlockBuilder;
25

            
26
use crate::client::RuntimeApiCollection;
27
use cumulus_primitives_core::{ParaId, PersistedValidationData};
28
use cumulus_primitives_parachain_inherent::ParachainInherentData;
29
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
30
use fc_mapping_sync::{kv::MappingSyncWorker, SyncStrategy};
31
use fc_rpc::{pending::ConsensusDataProvider, EthBlockDataCacheTask, EthTask, StorageOverride};
32
use fc_rpc_core::types::{FeeHistoryCache, FilterPool, TransactionRequest};
33
use futures::StreamExt;
34
use jsonrpsee::RpcModule;
35
use moonbeam_cli_opt::EthApi as EthApiCmd;
36
use moonbeam_core_primitives::{Block, Hash};
37
use sc_client_api::{
38
	backend::{AuxStore, Backend, StateBackend, StorageProvider},
39
	client::BlockchainEvents,
40
	BlockOf,
41
};
42
use sc_consensus_manual_seal::rpc::{EngineCommand, ManualSeal, ManualSealApiServer};
43
use sc_network::service::traits::NetworkService;
44
use sc_network_sync::SyncingService;
45
use sc_rpc::SubscriptionTaskExecutor;
46
use sc_rpc_api::DenyUnsafe;
47
use sc_service::TaskManager;
48
use sc_transaction_pool::{ChainApi, Pool};
49
use sc_transaction_pool_api::TransactionPool;
50
use sp_api::{CallApiAt, ProvideRuntimeApi};
51
use sp_blockchain::{
52
	Backend as BlockchainBackend, Error as BlockChainError, HeaderBackend, HeaderMetadata,
53
};
54
use sp_core::H256;
55
use sp_runtime::traits::{BlakeTwo256, Block as BlockT, Header as HeaderT};
56
use std::collections::BTreeMap;
57

            
58
pub struct MoonbeamEGA;
59

            
60
impl fc_rpc::EstimateGasAdapter for MoonbeamEGA {
61
8022
	fn adapt_request(mut request: TransactionRequest) -> TransactionRequest {
62
8022
		// Redirect any call to batch precompile:
63
8022
		// force usage of batchAll method for estimation
64
8022
		use sp_core::H160;
65
8022
		const BATCH_PRECOMPILE_ADDRESS: H160 = H160(hex_literal::hex!(
66
8022
			"0000000000000000000000000000000000000808"
67
8022
		));
68
8022
		const BATCH_PRECOMPILE_BATCH_ALL_SELECTOR: [u8; 4] = hex_literal::hex!("96e292b8");
69
8022
		if request.to == Some(BATCH_PRECOMPILE_ADDRESS) {
70
18
			match (&mut request.data.input, &mut request.data.data) {
71
				(Some(ref mut input), _) => {
72
					if input.0.len() >= 4 {
73
						input.0[..4].copy_from_slice(&BATCH_PRECOMPILE_BATCH_ALL_SELECTOR);
74
					}
75
				}
76
18
				(None, Some(ref mut data)) => {
77
18
					if data.0.len() >= 4 {
78
18
						data.0[..4].copy_from_slice(&BATCH_PRECOMPILE_BATCH_ALL_SELECTOR);
79
18
					}
80
				}
81
				(_, _) => {}
82
			};
83
8004
		}
84
8022
		request
85
8022
	}
86
}
87

            
88
pub struct MoonbeamEthConfig<C, BE>(std::marker::PhantomData<(C, BE)>);
89

            
90
impl<C, BE> fc_rpc::EthConfig<Block, C> for MoonbeamEthConfig<C, BE>
91
where
92
	C: sc_client_api::StorageProvider<Block, BE> + Sync + Send + 'static,
93
	BE: Backend<Block> + 'static,
94
{
95
	type EstimateGasAdapter = MoonbeamEGA;
96
	type RuntimeStorageOverride =
97
		fc_rpc::frontier_backend_client::SystemAccountId20StorageOverride<Block, C, BE>;
98
}
99

            
100
/// Full client dependencies.
101
pub struct FullDeps<C, P, A: ChainApi, BE> {
102
	/// The client instance to use.
103
	pub client: Arc<C>,
104
	/// Transaction pool instance.
105
	pub pool: Arc<P>,
106
	/// Graph pool instance.
107
	pub graph: Arc<Pool<A>>,
108
	/// Whether to deny unsafe calls
109
	pub deny_unsafe: DenyUnsafe,
110
	/// The Node authority flag
111
	pub is_authority: bool,
112
	/// Network service
113
	pub network: Arc<dyn NetworkService>,
114
	/// Chain syncing service
115
	pub sync: Arc<SyncingService<Block>>,
116
	/// EthFilterApi pool.
117
	pub filter_pool: Option<FilterPool>,
118
	/// The list of optional RPC extensions.
119
	pub ethapi_cmd: Vec<EthApiCmd>,
120
	/// Frontier Backend.
121
	pub frontier_backend: Arc<dyn fc_api::Backend<Block>>,
122
	/// Backend.
123
	pub backend: Arc<BE>,
124
	/// Manual seal command sink
125
	pub command_sink: Option<futures::channel::mpsc::Sender<EngineCommand<Hash>>>,
126
	/// Maximum number of logs in a query.
127
	pub max_past_logs: u32,
128
	/// Maximum fee history cache size.
129
	pub fee_history_limit: u64,
130
	/// Fee history cache.
131
	pub fee_history_cache: FeeHistoryCache,
132
	/// Channels for manual xcm messages (downward, hrmp)
133
	pub dev_rpc_data: Option<(
134
		flume::Sender<Vec<u8>>,
135
		flume::Sender<(ParaId, Vec<u8>)>,
136
		Arc<std::sync::atomic::AtomicU32>,
137
	)>,
138
	/// Ethereum data access overrides.
139
	pub overrides: Arc<dyn StorageOverride<Block>>,
140
	/// Cache for Ethereum block data.
141
	pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,
142
	/// Mandated parent hashes for a given block hash.
143
	pub forced_parent_hashes: Option<BTreeMap<H256, H256>>,
144
}
145

            
146
pub struct TracingConfig {
147
	pub tracing_requesters: crate::rpc::tracing::RpcRequesters,
148
	pub trace_filter_max_count: u32,
149
}
150

            
151
/// Instantiate all Full RPC extensions.
152
1788
pub fn create_full<C, P, BE, A>(
153
1788
	deps: FullDeps<C, P, A, BE>,
154
1788
	subscription_task_executor: SubscriptionTaskExecutor,
155
1788
	maybe_tracing_config: Option<TracingConfig>,
156
1788
	pubsub_notification_sinks: Arc<
157
1788
		fc_mapping_sync::EthereumBlockNotificationSinks<
158
1788
			fc_mapping_sync::EthereumBlockNotification<Block>,
159
1788
		>,
160
1788
	>,
161
1788
	pending_consenus_data_provider: Box<dyn ConsensusDataProvider<Block>>,
162
1788
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
163
1788
where
164
1788
	BE: Backend<Block> + 'static,
165
1788
	BE::State: StateBackend<BlakeTwo256>,
166
1788
	BE::Blockchain: BlockchainBackend<Block>,
167
1788
	C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,
168
1788
	C: BlockchainEvents<Block>,
169
1788
	C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,
170
1788
	C: CallApiAt<Block>,
171
1788
	C: Send + Sync + 'static,
172
1788
	A: ChainApi<Block = Block> + 'static,
173
1788
	C::Api: RuntimeApiCollection,
174
1788
	P: TransactionPool<Block = Block> + 'static,
175
1788
{
176
1788
	use fc_rpc::{
177
1788
		Eth, EthApiServer, EthFilter, EthFilterApiServer, EthPubSub, EthPubSubApiServer, Net,
178
1788
		NetApiServer, Web3, Web3ApiServer,
179
1788
	};
180
1788
	use moonbeam_dev_rpc::{DevApiServer, DevRpc};
181
1788
	use moonbeam_finality_rpc::{MoonbeamFinality, MoonbeamFinalityApiServer};
182
1788
	use moonbeam_rpc_debug::{Debug, DebugServer};
183
1788
	use moonbeam_rpc_trace::{Trace, TraceServer};
184
1788
	use moonbeam_rpc_txpool::{TxPool, TxPoolServer};
185
1788
	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
186
1788
	use substrate_frame_rpc_system::{System, SystemApiServer};
187
1788

            
188
1788
	let mut io = RpcModule::new(());
189
1788
	let FullDeps {
190
1788
		client,
191
1788
		pool,
192
1788
		graph,
193
1788
		deny_unsafe,
194
1788
		is_authority,
195
1788
		network,
196
1788
		sync,
197
1788
		filter_pool,
198
1788
		ethapi_cmd,
199
1788
		command_sink,
200
1788
		frontier_backend,
201
1788
		backend: _,
202
1788
		max_past_logs,
203
1788
		fee_history_limit,
204
1788
		fee_history_cache,
205
1788
		dev_rpc_data,
206
1788
		overrides,
207
1788
		block_data_cache,
208
1788
		forced_parent_hashes,
209
1788
	} = deps;
210
1788

            
211
1788
	io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;
212
1788
	io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;
213

            
214
	// TODO: are we supporting signing?
215
1788
	let signers = Vec::new();
216
1788

            
217
1788
	enum Never {}
218
1788
	impl<T> fp_rpc::ConvertTransaction<T> for Never {
219
1788
		fn convert_transaction(&self, _transaction: pallet_ethereum::Transaction) -> T {
220
			// The Never type is not instantiable, but this method requires the type to be
221
			// instantiated to be called (`&self` parameter), so if the code compiles we have the
222
			// guarantee that this function will never be called.
223
			unreachable!()
224
1788
		}
225
1788
	}
226
1788
	let convert_transaction: Option<Never> = None;
227
1788

            
228
1788
	let pending_create_inherent_data_providers = move |_, _| async move {
229
8
		let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
230
8
		// Create a dummy parachain inherent data provider which is required to pass
231
8
		// the checks by the para chain system. We use dummy values because in the 'pending context'
232
8
		// neither do we have access to the real values nor do we need them.
233
8
		let (relay_parent_storage_root, relay_chain_state) =
234
8
			RelayStateSproofBuilder::default().into_state_root_and_proof();
235
8
		let vfp = PersistedValidationData {
236
8
			// This is a hack to make `cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases`
237
8
			// happy. Relay parent number can't be bigger than u32::MAX.
238
8
			relay_parent_number: u32::MAX,
239
8
			relay_parent_storage_root,
240
8
			..Default::default()
241
8
		};
242
8
		let parachain_inherent_data = ParachainInherentData {
243
8
			validation_data: vfp,
244
8
			relay_chain_state,
245
8
			downward_messages: Default::default(),
246
8
			horizontal_messages: Default::default(),
247
8
		};
248
8
		Ok((timestamp, parachain_inherent_data))
249
8
	};
250

            
251
1788
	io.merge(
252
1788
		Eth::<_, _, _, _, _, _, _, MoonbeamEthConfig<_, _>>::new(
253
1788
			Arc::clone(&client),
254
1788
			Arc::clone(&pool),
255
1788
			graph.clone(),
256
1788
			convert_transaction,
257
1788
			Arc::clone(&sync),
258
1788
			signers,
259
1788
			Arc::clone(&overrides),
260
1788
			Arc::clone(&frontier_backend),
261
1788
			is_authority,
262
1788
			Arc::clone(&block_data_cache),
263
1788
			fee_history_cache,
264
1788
			fee_history_limit,
265
1788
			10,
266
1788
			forced_parent_hashes,
267
1788
			pending_create_inherent_data_providers,
268
1788
			Some(pending_consenus_data_provider),
269
1788
		)
270
1788
		.replace_config::<MoonbeamEthConfig<C, BE>>()
271
1788
		.into_rpc(),
272
1788
	)?;
273

            
274
1788
	if let Some(filter_pool) = filter_pool {
275
1788
		io.merge(
276
1788
			EthFilter::new(
277
1788
				client.clone(),
278
1788
				frontier_backend.clone(),
279
1788
				graph.clone(),
280
1788
				filter_pool,
281
1788
				500_usize, // max stored filters
282
1788
				max_past_logs,
283
1788
				block_data_cache,
284
1788
			)
285
1788
			.into_rpc(),
286
1788
		)?;
287
	}
288

            
289
1788
	io.merge(
290
1788
		Net::new(
291
1788
			Arc::clone(&client),
292
1788
			network.clone(),
293
1788
			// Whether to format the `peer_count` response as Hex (default) or not.
294
1788
			true,
295
1788
		)
296
1788
		.into_rpc(),
297
1788
	)?;
298

            
299
1788
	io.merge(Web3::new(Arc::clone(&client)).into_rpc())?;
300
1788
	io.merge(
301
1788
		EthPubSub::new(
302
1788
			pool,
303
1788
			Arc::clone(&client),
304
1788
			sync.clone(),
305
1788
			subscription_task_executor,
306
1788
			overrides,
307
1788
			pubsub_notification_sinks.clone(),
308
1788
		)
309
1788
		.into_rpc(),
310
1788
	)?;
311
1788
	if ethapi_cmd.contains(&EthApiCmd::Txpool) {
312
1788
		io.merge(TxPool::new(Arc::clone(&client), graph).into_rpc())?;
313
	}
314

            
315
1788
	io.merge(MoonbeamFinality::new(client.clone(), frontier_backend.clone()).into_rpc())?;
316

            
317
1788
	if let Some(command_sink) = command_sink {
318
1788
		io.merge(
319
1788
			// We provide the rpc handler with the sending end of the channel to allow the rpc
320
1788
			// send EngineCommands to the background block authorship task.
321
1788
			ManualSeal::new(command_sink).into_rpc(),
322
1788
		)?;
323
	};
324

            
325
1788
	if let Some((downward_message_channel, hrmp_message_channel, additional_relay_offset)) =
326
1788
		dev_rpc_data
327
	{
328
1788
		io.merge(
329
1788
			DevRpc {
330
1788
				downward_message_channel,
331
1788
				hrmp_message_channel,
332
1788
				additional_relay_offset,
333
1788
			}
334
1788
			.into_rpc(),
335
1788
		)?;
336
	}
337

            
338
1788
	if let Some(tracing_config) = maybe_tracing_config {
339
		if let Some(trace_filter_requester) = tracing_config.tracing_requesters.trace {
340
			io.merge(
341
				Trace::new(
342
					client,
343
					trace_filter_requester,
344
					tracing_config.trace_filter_max_count,
345
				)
346
				.into_rpc(),
347
			)?;
348
		}
349

            
350
		if let Some(debug_requester) = tracing_config.tracing_requesters.debug {
351
			io.merge(Debug::new(debug_requester).into_rpc())?;
352
		}
353
1788
	}
354

            
355
1788
	Ok(io)
356
1788
}
357

            
358
pub struct SpawnTasksParams<'a, B: BlockT, C, BE> {
359
	pub task_manager: &'a TaskManager,
360
	pub client: Arc<C>,
361
	pub substrate_backend: Arc<BE>,
362
	pub frontier_backend: Arc<fc_db::Backend<B, C>>,
363
	pub filter_pool: Option<FilterPool>,
364
	pub overrides: Arc<dyn StorageOverride<B>>,
365
	pub fee_history_limit: u64,
366
	pub fee_history_cache: FeeHistoryCache,
367
}
368

            
369
/// Spawn the tasks that are required to run Moonbeam.
370
894
pub fn spawn_essential_tasks<B, C, BE>(
371
894
	params: SpawnTasksParams<B, C, BE>,
372
894
	sync: Arc<SyncingService<B>>,
373
894
	pubsub_notification_sinks: Arc<
374
894
		fc_mapping_sync::EthereumBlockNotificationSinks<
375
894
			fc_mapping_sync::EthereumBlockNotification<B>,
376
894
		>,
377
894
	>,
378
894
) where
379
894
	C: ProvideRuntimeApi<B> + BlockOf,
380
894
	C: HeaderBackend<B> + HeaderMetadata<B, Error = BlockChainError> + 'static,
381
894
	C: BlockchainEvents<B> + StorageProvider<B, BE>,
382
894
	C: Send + Sync + 'static,
383
894
	C::Api: EthereumRuntimeRPCApi<B>,
384
894
	C::Api: BlockBuilder<B>,
385
894
	B: BlockT<Hash = H256> + Send + Sync + 'static,
386
894
	B::Header: HeaderT<Number = u32>,
387
894
	BE: Backend<B> + 'static,
388
894
	BE::State: StateBackend<BlakeTwo256>,
389
894
{
390
894
	// Frontier offchain DB task. Essential.
391
894
	// Maps emulated ethereum data to substrate native data.
392
894
	match *params.frontier_backend {
393
894
		fc_db::Backend::KeyValue(ref b) => {
394
894
			params.task_manager.spawn_essential_handle().spawn(
395
894
				"frontier-mapping-sync-worker",
396
894
				Some("frontier"),
397
894
				MappingSyncWorker::new(
398
894
					params.client.import_notification_stream(),
399
894
					Duration::new(6, 0),
400
894
					params.client.clone(),
401
894
					params.substrate_backend.clone(),
402
894
					params.overrides.clone(),
403
894
					b.clone(),
404
894
					3,
405
894
					0,
406
894
					SyncStrategy::Parachain,
407
894
					sync.clone(),
408
894
					pubsub_notification_sinks.clone(),
409
894
				)
410
48624
				.for_each(|()| futures::future::ready(())),
411
894
			);
412
894
		}
413
		fc_db::Backend::Sql(ref b) => {
414
			params.task_manager.spawn_essential_handle().spawn_blocking(
415
				"frontier-mapping-sync-worker",
416
				Some("frontier"),
417
				fc_mapping_sync::sql::SyncWorker::run(
418
					params.client.clone(),
419
					params.substrate_backend.clone(),
420
					b.clone(),
421
					params.client.import_notification_stream(),
422
					fc_mapping_sync::sql::SyncWorkerConfig {
423
						read_notification_timeout: Duration::from_secs(10),
424
						check_indexed_blocks_interval: Duration::from_secs(60),
425
					},
426
					fc_mapping_sync::SyncStrategy::Parachain,
427
					sync.clone(),
428
					pubsub_notification_sinks.clone(),
429
				),
430
			);
431
		}
432
	}
433

            
434
	// Frontier `EthFilterApi` maintenance.
435
	// Manages the pool of user-created Filters.
436
894
	if let Some(filter_pool) = params.filter_pool {
437
894
		// Each filter is allowed to stay in the pool for 100 blocks.
438
894
		const FILTER_RETAIN_THRESHOLD: u64 = 100;
439
894
		params.task_manager.spawn_essential_handle().spawn(
440
894
			"frontier-filter-pool",
441
894
			Some("frontier"),
442
894
			EthTask::filter_pool_task(
443
894
				Arc::clone(&params.client),
444
894
				filter_pool,
445
894
				FILTER_RETAIN_THRESHOLD,
446
894
			),
447
894
		);
448
894
	}
449

            
450
	// Spawn Frontier FeeHistory cache maintenance task.
451
894
	params.task_manager.spawn_essential_handle().spawn(
452
894
		"frontier-fee-history",
453
894
		Some("frontier"),
454
894
		EthTask::fee_history_task(
455
894
			Arc::clone(&params.client),
456
894
			Arc::clone(&params.overrides),
457
894
			params.fee_history_cache,
458
894
			params.fee_history_limit,
459
894
		),
460
894
	);
461
894
}