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
// This file is part of Darwinia.
//
// Copyright (C) 2018-2022 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia 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.
//
// Darwinia 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 Darwinia. If not, see <https://www.gnu.org/licenses/>.

#[cfg(feature = "template")]
pub mod template;

pub use sc_rpc::{DenyUnsafe, SubscriptionTaskExecutor};

// --- std ---
use std::sync::Arc;
// --- darwinia-network ---
use drml_primitives::{OpaqueBlock as Block, *};

/// A type representing all RPC extensions.
pub type RpcExtension = jsonrpc_core::IoHandler<sc_rpc::Metadata>;
/// RPC result.
pub type RpcResult = Result<RpcExtension, Box<dyn std::error::Error + Send + Sync>>;

/// Full client dependencies.
pub struct FullDeps<C, P, SC, B, A>
where
	A: sc_transaction_pool::ChainApi,
{
	/// The client instance to use.
	pub client: Arc<C>,
	/// Transaction pool instance.
	pub pool: Arc<P>,
	/// The SelectChain Strategy
	pub select_chain: SC,
	/// A copy of the chain spec.
	pub chain_spec: Box<dyn sc_chain_spec::ChainSpec>,
	/// Whether to deny unsafe calls
	pub deny_unsafe: sc_rpc::DenyUnsafe,
	/// BABE specific dependencies.
	pub babe: BabeDeps,
	/// GRANDPA specific dependencies.
	pub grandpa: GrandpaDeps<B>,
	// /// BEEFY specific dependencies.
	// pub beefy: BeefyDeps,
	/// DVM related rpc helper.
	pub eth: EthDeps<A>,
}

/// Extra dependencies for BABE.
pub struct BabeDeps {
	/// BABE protocol config.
	pub babe_config: sc_consensus_babe::Config,
	/// BABE pending epoch changes.
	pub shared_epoch_changes:
		sc_consensus_epochs::SharedEpochChanges<Block, sc_consensus_babe::Epoch>,
	/// The keystore that manages the keys of the node.
	pub keystore: sp_keystore::SyncCryptoStorePtr,
}

/// Dependencies for GRANDPA
pub struct GrandpaDeps<B> {
	/// Voting round info.
	pub shared_voter_state: sc_finality_grandpa::SharedVoterState,
	/// Authority set info.
	pub shared_authority_set: sc_finality_grandpa::SharedAuthoritySet<Hash, BlockNumber>,
	/// Receives notifications about justification events from Grandpa.
	pub justification_stream: sc_finality_grandpa::GrandpaJustificationStream<Block>,
	/// Executor to drive the subscription manager in the Grandpa RPC handler.
	pub subscription_executor: sc_rpc::SubscriptionTaskExecutor,
	/// Finality proof provider.
	pub finality_proof_provider: Arc<sc_finality_grandpa::FinalityProofProvider<B, Block>>,
}

// /// Extra dependencies for BEEFY
// pub struct BeefyDeps {
// 	/// Receives notifications about signed commitment events from BEEFY.
// 	pub beefy_commitment_stream: beefy_gadget::notification::BeefySignedCommitmentStream<Block>,
// 	/// Executor to drive the subscription manager in the BEEFY RPC handler.
// 	pub subscription_executor: sc_rpc::SubscriptionTaskExecutor,
// }

pub struct EthDeps<A>
where
	A: sc_transaction_pool::ChainApi,
{
	/// DVM related RPC Config
	pub config: EthRpcConfig,
	/// Graph pool instance.
	pub graph: Arc<sc_transaction_pool::Pool<A>>,
	/// The Node authority flag
	pub is_authority: bool,
	/// Network service
	pub network: Arc<sc_network::NetworkService<Block, Hash>>,
	/// EthFilterApi pool.
	pub filter_pool: Option<fc_rpc_core::types::FilterPool>,
	/// DVM Backend.
	pub backend: Arc<fc_db::Backend<Block>>,
	/// Fee history cache.
	pub fee_history_cache: fc_rpc_core::types::FeeHistoryCache,
	/// Ethereum data access overrides.
	pub overrides: Arc<fc_rpc::OverrideHandle<Block>>,
	// Cache for Ethereum block data.
	pub block_data_cache: Arc<fc_rpc::EthBlockDataCache<Block>>,
	/// RPC requester for evm trace.
	pub rpc_requesters: EthRpcRequesters,
}

#[derive(Clone)]
pub struct EthRpcConfig {
	pub ethapi_debug_targets: Vec<String>,
	pub ethapi_max_permits: u32,
	pub ethapi_trace_max_count: u32,
	pub ethapi_trace_cache_duration: u64,
	pub eth_log_block_cache: usize,
	pub max_past_logs: u32,
	pub fee_history_limit: u64,
}

#[derive(Clone)]
pub struct EthRpcRequesters {
	pub debug: Option<moonbeam_rpc_debug::DebugRequester>,
	pub trace: Option<moonbeam_rpc_trace::CacheRequester>,
}

/// Instantiate all RPC extensions.
pub fn create_full<C, P, SC, B, A>(
	deps: FullDeps<C, P, SC, B, A>,
	subscription_task_executor: sc_rpc::SubscriptionTaskExecutor,
) -> RpcResult
where
	C: 'static
		+ Send
		+ Sync
		+ sc_client_api::AuxStore
		+ sc_client_api::BlockchainEvents<Block>
		+ sc_client_api::StorageProvider<Block, B>
		+ sp_api::ProvideRuntimeApi<Block>
		+ sp_blockchain::HeaderBackend<Block>
		+ sp_blockchain::HeaderMetadata<Block, Error = sp_blockchain::Error>,
	C::Api: sc_consensus_babe::BabeApi<Block>
		+ sp_block_builder::BlockBuilder<Block>
		+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
		+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
		+ fp_rpc::EthereumRuntimeRPCApi<Block>
		+ fp_rpc::ConvertTransactionRuntimeApi<Block>
		+ moonbeam_rpc_primitives_debug::DebugRuntimeApi<Block>,
	P: 'static + Sync + Send + sc_transaction_pool_api::TransactionPool<Block = Block>,
	SC: 'static + sp_consensus::SelectChain<Block>,
	B: 'static + Send + Sync + sc_client_api::Backend<Block>,
	B::State: sc_client_api::StateBackend<Hashing>,
	A: 'static + sc_transaction_pool::ChainApi<Block = Block>,
{
	// --- crates.io ---
	use jsonrpc_core::IoHandler;
	use jsonrpc_pubsub::manager::SubscriptionManager;
	// --- paritytech ---
	// use beefy_gadget_rpc::*;
	use fc_rpc::*;
	use fp_rpc::*;
	use pallet_transaction_payment_rpc::*;
	use sc_consensus_babe_rpc::*;
	use sc_finality_grandpa_rpc::*;
	use sc_sync_state_rpc::*;
	use substrate_frame_rpc_system::*;
	// --- darwinia-network ---
	use moonbeam_rpc_debug::*;
	use moonbeam_rpc_trace::*;

	let FullDeps {
		client,
		pool,
		select_chain,
		chain_spec,
		deny_unsafe,
		babe: BabeDeps { keystore, babe_config, shared_epoch_changes },
		grandpa:
			GrandpaDeps {
				shared_voter_state,
				shared_authority_set,
				justification_stream,
				subscription_executor: grandpa_subscription_executor,
				finality_proof_provider,
			},
		// beefy:
		// BeefyDeps { beefy_commitment_stream, subscription_executor: beefy_subscription_executor
		// },
		eth:
			EthDeps {
				config:
					EthRpcConfig {
						ethapi_debug_targets,
						ethapi_trace_max_count,
						max_past_logs,
						fee_history_limit,
						..
					},
				graph,
				is_authority,
				network,
				filter_pool,
				backend,
				fee_history_cache,
				overrides,
				block_data_cache,
				rpc_requesters,
			},
	} = deps;
	let mut io = IoHandler::default();

	io.extend_with(SystemApi::to_delegate(FullSystem::new(
		client.clone(),
		pool.clone(),
		deny_unsafe,
	)));
	io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone())));
	io.extend_with(BabeApi::to_delegate(BabeRpcHandler::new(
		client.clone(),
		shared_epoch_changes.clone(),
		keystore,
		babe_config,
		select_chain,
		deny_unsafe,
	)));
	io.extend_with(GrandpaApi::to_delegate(GrandpaRpcHandler::new(
		shared_authority_set.clone(),
		shared_voter_state,
		justification_stream,
		grandpa_subscription_executor,
		finality_proof_provider,
	)));
	// io.extend_with(BeefyApi::to_delegate(BeefyRpcHandler::new(
	// 	beefy_commitment_stream,
	// 	beefy_subscription_executor,
	// )));
	io.extend_with(SyncStateRpcApi::to_delegate(SyncStateRpcHandler::new(
		chain_spec,
		client.clone(),
		shared_authority_set,
		shared_epoch_changes,
		deny_unsafe,
	)?));
	io.extend_with(EthApiServer::to_delegate(EthApi::new(
		client.clone(),
		pool.clone(),
		graph,
		<Option<NoTransactionConverter>>::None,
		network.clone(),
		vec![],
		overrides.clone(),
		backend.clone(),
		is_authority,
		block_data_cache.clone(),
		fee_history_limit,
		fee_history_cache,
	)));
	if let Some(filter_pool) = filter_pool {
		io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(
			client.clone(),
			backend,
			filter_pool,
			500_usize, // max stored filters
			max_past_logs,
			block_data_cache,
		)));
	}
	io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(
		pool,
		client.clone(),
		network.clone(),
		SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(
			HexEncodedIdProvider::default(),
			Arc::new(subscription_task_executor),
		),
		overrides,
	)));
	io.extend_with(NetApiServer::to_delegate(NetApi::new(
		client.clone(),
		network,
		// Whether to format the `peer_count` response as Hex (default) or not.
		true,
	)));
	io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));

	if ethapi_debug_targets.iter().any(|cmd| matches!(cmd.as_str(), "debug" | "trace")) {
		if let Some(trace_requester) = rpc_requesters.trace {
			io.extend_with(TraceServer::to_delegate(Trace::new(
				client,
				trace_requester,
				ethapi_trace_max_count,
			)));
		}

		if let Some(debug_requester) = rpc_requesters.debug {
			io.extend_with(DebugServer::to_delegate(Debug::new(debug_requester)));
		}
	}

	Ok(io)
}

pub fn overrides_handle<C, BE>(client: Arc<C>) -> Arc<fc_rpc::OverrideHandle<Block>>
where
	C: 'static
		+ Send
		+ Sync
		+ sc_client_api::backend::AuxStore
		+ sc_client_api::backend::StorageProvider<Block, BE>
		+ sp_api::ProvideRuntimeApi<Block>
		+ sp_blockchain::HeaderBackend<Block>
		+ sp_blockchain::HeaderMetadata<Block, Error = sp_blockchain::Error>,
	C::Api: sp_api::ApiExt<Block>
		+ fp_rpc::EthereumRuntimeRPCApi<Block>
		+ fp_rpc::ConvertTransactionRuntimeApi<Block>,
	BE: 'static + sc_client_api::backend::Backend<Block>,
	BE::State: sc_client_api::backend::StateBackend<Hashing>,
{
	// --- std ---
	use std::collections::BTreeMap;
	// --- paritytech ---
	use fc_rpc::*;
	use fp_storage::EthereumStorageSchema;

	Arc::new(OverrideHandle {
		schemas: BTreeMap::from_iter([
			(
				EthereumStorageSchema::V1,
				Box::new(SchemaV1Override::new(client.clone()))
					as Box<dyn StorageOverride<_> + Send + Sync>,
			),
			(
				EthereumStorageSchema::V2,
				Box::new(SchemaV2Override::new(client.clone()))
					as Box<dyn StorageOverride<_> + Send + Sync>,
			),
			(
				EthereumStorageSchema::V3,
				Box::new(SchemaV3Override::new(client.clone()))
					as Box<dyn StorageOverride<_> + Send + Sync>,
			),
		]),
		fallback: Box::new(RuntimeApiStorageOverride::new(client)),
	})
}