1use unique_runtime::{Hash, AccountId, CrossAccountId, Index, opaque::Block, BlockNumber, Balance};2use fc_rpc::{3 EthBlockDataCache, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override, StorageOverride,4};5use fc_rpc_core::types::FilterPool;6use jsonrpc_pubsub::manager::SubscriptionManager;7use pallet_ethereum::EthereumStorageSchema;8use sc_client_api::{9 backend::{AuxStore, StorageProvider},10 client::BlockchainEvents,11};12use sc_finality_grandpa::{13 FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,14};15use sc_network::NetworkService;16use sc_rpc::SubscriptionTaskExecutor;17pub use sc_rpc_api::DenyUnsafe;18use sc_transaction_pool::{ChainApi, Pool};19use sp_api::ProvideRuntimeApi;20use sp_block_builder::BlockBuilder;21use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};22use sp_consensus::SelectChain;23use sc_service::TransactionPool;24use std::{collections::BTreeMap, marker::PhantomData, sync::Arc};252627pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;282930pub struct GrandpaDeps<B> {31 32 pub shared_voter_state: SharedVoterState,33 34 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,35 36 pub justification_stream: GrandpaJustificationStream<Block>,37 38 pub subscription_executor: SubscriptionTaskExecutor,39 40 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,41}424344pub struct FullDeps<C, P, SC, CA: ChainApi> {45 46 pub client: Arc<C>,47 48 pub pool: Arc<P>,49 50 pub graph: Arc<Pool<CA>>,51 52 pub select_chain: SC,53 54 pub is_authority: bool,55 56 pub enable_dev_signer: bool,57 58 pub network: Arc<NetworkService<Block, Hash>>,59 60 pub deny_unsafe: DenyUnsafe,61 62 pub filter_pool: Option<FilterPool>,63 64 pub backend: Arc<fc_db::Backend<Block>>,65 66 pub max_past_logs: u32,67}6869struct AccountCodes<C, B> {70 client: Arc<C>,71 _marker: PhantomData<B>,72}7374impl<C, Block> AccountCodes<C, Block>75where76 Block: sp_api::BlockT,77 C: ProvideRuntimeApi<Block>,78{79 fn new(client: Arc<C>) -> Self {80 Self {81 client,82 _marker: PhantomData,83 }84 }85}8687impl<C, Block> fc_rpc::AccountCodeProvider<Block> for AccountCodes<C, Block>88where89 Block: sp_api::BlockT,90 C: ProvideRuntimeApi<Block>,91 C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,92{93 fn code(&self, block: &sp_api::BlockId<Block>, account: sp_core::H160) -> Option<Vec<u8>> {94 use up_rpc::UniqueApi;95 self.client96 .runtime_api()97 .eth_contract_code(block, account)98 .ok()99 .flatten()100 }101}102103104pub fn create_full<C, P, SC, CA, A, B>(105 deps: FullDeps<C, P, SC, CA>,106 subscription_task_executor: SubscriptionTaskExecutor,107) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>108where109 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,110 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,111 C: Send + Sync + 'static,112 C: BlockchainEvents<Block>,113 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,114 C::Api: BlockBuilder<Block>,115 116 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,117 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,118 C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,119 B: sc_client_api::Backend<Block> + Send + Sync + 'static,120 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,121 SC: SelectChain<Block> + 'static,122 P: TransactionPool<Block = Block> + 'static,123 CA: ChainApi<Block = Block> + 'static,124{125 use fc_rpc::{126 EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,127 EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,128 Web3ApiServer,129 };130 use uc_rpc::{UniqueApi, Unique};131 132 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};133 use substrate_frame_rpc_system::{FullSystem, SystemApi};134135 let mut io = jsonrpc_core::IoHandler::default();136 let FullDeps {137 client,138 pool,139 graph,140 select_chain: _,141 enable_dev_signer,142 is_authority,143 network,144 deny_unsafe,145 filter_pool,146 backend,147 max_past_logs,148 } = deps;149150 io.extend_with(SystemApi::to_delegate(FullSystem::new(151 client.clone(),152 pool.clone(),153 deny_unsafe,154 )));155156 io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(157 client.clone(),158 )));159160 161162 let mut signers = Vec::new();163 if enable_dev_signer {164 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);165 }166 let mut overrides_map = BTreeMap::new();167 overrides_map.insert(168 EthereumStorageSchema::V1,169 Box::new(SchemaV1Override::new_with_code_provider(170 client.clone(),171 Arc::new(AccountCodes::<C, Block>::new(client.clone())),172 )) as Box<dyn StorageOverride<_> + Send + Sync>,173 );174175 let overrides = Arc::new(OverrideHandle {176 schemas: overrides_map,177 fallback: Box::new(RuntimeApiStorageOverride::new(client.clone())),178 });179180 let block_data_cache = Arc::new(EthBlockDataCache::new(50, 50));181182 io.extend_with(EthApiServer::to_delegate(EthApi::new(183 client.clone(),184 pool.clone(),185 graph,186 unique_runtime::TransactionConverter,187 network.clone(),188 signers,189 overrides.clone(),190 backend.clone(),191 is_authority,192 max_past_logs,193 block_data_cache.clone(),194 )));195 io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));196197 if let Some(filter_pool) = filter_pool {198 io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(199 client.clone(),200 backend,201 filter_pool,202 500_usize, 203 overrides.clone(),204 max_past_logs,205 block_data_cache,206 )));207 }208209 io.extend_with(NetApiServer::to_delegate(NetApi::new(210 client.clone(),211 network.clone(),212 213 true,214 )));215216 io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));217218 io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(219 pool,220 client,221 network,222 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(223 HexEncodedIdProvider::default(),224 Arc::new(subscription_task_executor),225 ),226 overrides,227 )));228229 io230}