1234567891011121314151617use sp_runtime::traits::BlakeTwo256;18use fc_rpc::{19 EthBlockDataCache, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override,20 StorageOverride, SchemaV2Override, SchemaV3Override,21};22use fc_rpc_core::types::{FilterPool, FeeHistoryCache};23use jsonrpc_pubsub::manager::SubscriptionManager;24use fp_storage::EthereumStorageSchema;25use sc_client_api::{26 backend::{AuxStore, StorageProvider},27 client::BlockchainEvents,28 StateBackend, Backend,29};30use sc_finality_grandpa::{31 FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,32};33use sc_network::NetworkService;34use sc_rpc::SubscriptionTaskExecutor;35pub use sc_rpc_api::DenyUnsafe;36use sc_transaction_pool::{ChainApi, Pool};37use sp_api::ProvideRuntimeApi;38use sp_block_builder::BlockBuilder;39use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};40use sc_service::TransactionPool;41use std::{collections::BTreeMap, sync::Arc};4243use unique_runtime_common::types::{44 Hash, AccountId, RuntimeInstance, Index, Block, BlockNumber, Balance,45};464748pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;495051pub struct GrandpaDeps<B> {52 53 pub shared_voter_state: SharedVoterState,54 55 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,56 57 pub justification_stream: GrandpaJustificationStream<Block>,58 59 pub subscription_executor: SubscriptionTaskExecutor,60 61 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,62}636465pub struct FullDeps<C, P, SC, CA: ChainApi> {66 67 pub client: Arc<C>,68 69 pub pool: Arc<P>,70 71 pub graph: Arc<Pool<CA>>,72 73 pub select_chain: SC,74 75 pub is_authority: bool,76 77 pub enable_dev_signer: bool,78 79 pub network: Arc<NetworkService<Block, Hash>>,80 81 pub deny_unsafe: DenyUnsafe,82 83 pub filter_pool: Option<FilterPool>,84 85 pub backend: Arc<fc_db::Backend<Block>>,86 87 pub max_past_logs: u32,88 89 pub fee_history_limit: u64,90 91 pub fee_history_cache: FeeHistoryCache,92 93 pub block_data_cache: Arc<EthBlockDataCache<Block>>,94}9596pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>97where98 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,99 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,100 C: Send + Sync + 'static,101 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,102 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,103 BE: Backend<Block> + 'static,104 BE::State: StateBackend<BlakeTwo256>,105 R: RuntimeInstance + Send + Sync + 'static,106{107 let mut overrides_map = BTreeMap::new();108 overrides_map.insert(109 EthereumStorageSchema::V1,110 Box::new(SchemaV1Override::new(client.clone()))111 as Box<dyn StorageOverride<_> + Send + Sync>,112 );113 overrides_map.insert(114 EthereumStorageSchema::V2,115 Box::new(SchemaV2Override::new(client.clone()))116 as Box<dyn StorageOverride<_> + Send + Sync>,117 );118 overrides_map.insert(119 EthereumStorageSchema::V3,120 Box::new(SchemaV3Override::new(client.clone()))121 as Box<dyn StorageOverride<_> + Send + Sync>,122 );123124 Arc::new(OverrideHandle {125 schemas: overrides_map,126 fallback: Box::new(RuntimeApiStorageOverride::new(client)),127 })128}129130131pub fn create_full<C, P, SC, CA, R, A, B>(132 deps: FullDeps<C, P, SC, CA>,133 subscription_task_executor: SubscriptionTaskExecutor,134) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>135where136 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,137 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,138 C: Send + Sync + 'static,139 C: BlockchainEvents<Block>,140 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,141 C::Api: BlockBuilder<Block>,142 143 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,144 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,145 C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,146 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,147 B: sc_client_api::Backend<Block> + Send + Sync + 'static,148 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,149 P: TransactionPool<Block = Block> + 'static,150 CA: ChainApi<Block = Block> + 'static,151 R: RuntimeInstance + Send + Sync + 'static,152 <R as RuntimeInstance>::CrossAccountId: serde::Serialize,153 for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,154{155 use fc_rpc::{156 EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,157 EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,158 Web3ApiServer,159 };160 use uc_rpc::{UniqueApi, Unique};161 162 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};163 use substrate_frame_rpc_system::{FullSystem, SystemApi};164165 let mut io = jsonrpc_core::IoHandler::default();166 let FullDeps {167 client,168 pool,169 graph,170 select_chain: _,171 fee_history_limit,172 fee_history_cache,173 block_data_cache,174 enable_dev_signer,175 is_authority,176 network,177 deny_unsafe,178 filter_pool,179 backend,180 max_past_logs,181 } = deps;182183 io.extend_with(SystemApi::to_delegate(FullSystem::new(184 client.clone(),185 pool.clone(),186 deny_unsafe,187 )));188189 io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(190 client.clone(),191 )));192193 194195 let mut signers = Vec::new();196 if enable_dev_signer {197 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);198 }199200 let overrides = overrides_handle::<_, _, R>(client.clone());201202 io.extend_with(EthApiServer::to_delegate(EthApi::new(203 client.clone(),204 pool.clone(),205 graph,206 Some(<R as RuntimeInstance>::get_transaction_converter()),207 network.clone(),208 signers,209 overrides.clone(),210 backend.clone(),211 is_authority,212 block_data_cache.clone(),213 fee_history_limit,214 fee_history_cache,215 )));216 io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));217218 if let Some(filter_pool) = filter_pool {219 io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(220 client.clone(),221 backend,222 filter_pool,223 500_usize, 224 max_past_logs,225 block_data_cache,226 )));227 }228229 io.extend_with(NetApiServer::to_delegate(NetApi::new(230 client.clone(),231 network.clone(),232 233 true,234 )));235236 io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));237238 io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(239 pool,240 client,241 network,242 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(243 HexEncodedIdProvider::default(),244 Arc::new(subscription_task_executor),245 ),246 overrides,247 )));248249 io250}