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_service::TaskManager;
47
use sc_transaction_pool::{ChainApi, Pool};
48
use sc_transaction_pool_api::TransactionPool;
49
use sp_api::{CallApiAt, ProvideRuntimeApi};
50
use sp_blockchain::{
51
	Backend as BlockchainBackend, Error as BlockChainError, HeaderBackend, HeaderMetadata,
52
};
53
use sp_core::H256;
54
use sp_runtime::traits::{BlakeTwo256, Block as BlockT, Header as HeaderT};
55
use std::collections::BTreeMap;
56

            
57
pub struct MoonbeamEGA;
58

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

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

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

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

            
143
pub struct TracingConfig {
144
	pub tracing_requesters: crate::rpc::tracing::RpcRequesters,
145
	pub trace_filter_max_count: u32,
146
}
147

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

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

            
207
1804
	io.merge(System::new(Arc::clone(&client), Arc::clone(&pool)).into_rpc())?;
208
1804
	io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;
209

            
210
	// TODO: are we supporting signing?
211
1804
	let signers = Vec::new();
212
1804

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

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

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

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

            
285
1804
	io.merge(
286
1804
		Net::new(
287
1804
			Arc::clone(&client),
288
1804
			network.clone(),
289
1804
			// Whether to format the `peer_count` response as Hex (default) or not.
290
1804
			true,
291
1804
		)
292
1804
		.into_rpc(),
293
1804
	)?;
294

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

            
311
1804
	io.merge(MoonbeamFinality::new(client.clone(), frontier_backend.clone()).into_rpc())?;
312

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

            
321
1804
	if let Some((downward_message_channel, hrmp_message_channel, additional_relay_offset)) =
322
1804
		dev_rpc_data
323
	{
324
1804
		io.merge(
325
1804
			DevRpc {
326
1804
				downward_message_channel,
327
1804
				hrmp_message_channel,
328
1804
				additional_relay_offset,
329
1804
			}
330
1804
			.into_rpc(),
331
1804
		)?;
332
	}
333

            
334
1804
	if let Some(tracing_config) = maybe_tracing_config {
335
		if let Some(trace_filter_requester) = tracing_config.tracing_requesters.trace {
336
			io.merge(
337
				Trace::new(
338
					client,
339
					trace_filter_requester,
340
					tracing_config.trace_filter_max_count,
341
				)
342
				.into_rpc(),
343
			)?;
344
		}
345

            
346
		if let Some(debug_requester) = tracing_config.tracing_requesters.debug {
347
			io.merge(Debug::new(debug_requester).into_rpc())?;
348
		}
349
1804
	}
350

            
351
1804
	Ok(io)
352
1804
}
353

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

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

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

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