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
6384
	fn adapt_request(mut request: TransactionRequest) -> TransactionRequest {
62
6384
		// Redirect any call to batch precompile:
63
6384
		// force usage of batchAll method for estimation
64
6384
		use sp_core::H160;
65
6384
		const BATCH_PRECOMPILE_ADDRESS: H160 = H160(hex_literal::hex!(
66
6384
			"0000000000000000000000000000000000000808"
67
6384
		));
68
6384
		const BATCH_PRECOMPILE_BATCH_ALL_SELECTOR: [u8; 4] = hex_literal::hex!("96e292b8");
69
6384
		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
6366
		}
84
6384
		request
85
6384
	}
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 xcm_senders: Option<(flume::Sender<Vec<u8>>, flume::Sender<(ParaId, Vec<u8>)>)>,
134
	/// Ethereum data access overrides.
135
	pub overrides: Arc<dyn StorageOverride<Block>>,
136
	/// Cache for Ethereum block data.
137
	pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,
138
	/// Mandated parent hashes for a given block hash.
139
	pub forced_parent_hashes: Option<BTreeMap<H256, H256>>,
140
}
141

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

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

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

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

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

            
213
1796
	enum Never {}
214
1796
	impl<T> fp_rpc::ConvertTransaction<T> for Never {
215
1796
		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
1796
		}
221
1796
	}
222
1796
	let convert_transaction: Option<Never> = None;
223
1796

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

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

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

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

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

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

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

            
321
1796
	if let Some((downward_message_channel, hrmp_message_channel)) = xcm_senders {
322
1796
		io.merge(
323
1796
			ManualXcm {
324
1796
				downward_message_channel,
325
1796
				hrmp_message_channel,
326
1796
			}
327
1796
			.into_rpc(),
328
1796
		)?;
329
	}
330

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

            
343
		if let Some(debug_requester) = tracing_config.tracing_requesters.debug {
344
			io.merge(Debug::new(debug_requester).into_rpc())?;
345
		}
346
1796
	}
347

            
348
1796
	Ok(io)
349
1796
}
350

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

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

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

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