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}959697pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>98where99 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,100 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,101 C: Send + Sync + 'static,102 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,103 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,104 BE: Backend<Block> + 'static,105 BE::State: StateBackend<BlakeTwo256>,106 R: RuntimeInstance + Send + Sync + 'static,107{108 let mut overrides_map = BTreeMap::new();109 overrides_map.insert(110 EthereumStorageSchema::V1,111 Box::new(SchemaV1Override::new(client.clone()))112 as Box<dyn StorageOverride<_> + Send + Sync>,113 );114 overrides_map.insert(115 EthereumStorageSchema::V2,116 Box::new(SchemaV2Override::new(client.clone()))117 as Box<dyn StorageOverride<_> + Send + Sync>,118 );119 overrides_map.insert(120 EthereumStorageSchema::V3,121 Box::new(SchemaV3Override::new(client.clone()))122 as Box<dyn StorageOverride<_> + Send + Sync>,123 );124125 Arc::new(OverrideHandle {126 schemas: overrides_map,127 fallback: Box::new(RuntimeApiStorageOverride::new(client)),128 })129}130131132pub fn create_full<C, P, SC, CA, R, A, B>(133 deps: FullDeps<C, P, SC, CA>,134 subscription_task_executor: SubscriptionTaskExecutor,135) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>136where137 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,138 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,139 C: Send + Sync + 'static,140 C: BlockchainEvents<Block>,141 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,142 C::Api: BlockBuilder<Block>,143 144 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,145 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,146 C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,147 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,148 B: sc_client_api::Backend<Block> + Send + Sync + 'static,149 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,150 P: TransactionPool<Block = Block> + 'static,151 CA: ChainApi<Block = Block> + 'static,152 R: RuntimeInstance + Send + Sync + 'static,153 <R as RuntimeInstance>::CrossAccountId: serde::Serialize,154 for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,155{156 use fc_rpc::{157 EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,158 EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,159 Web3ApiServer,160 };161 use uc_rpc::{UniqueApi, Unique};162 163 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};164 use substrate_frame_rpc_system::{FullSystem, SystemApi};165166 let mut io = jsonrpc_core::IoHandler::default();167 let FullDeps {168 client,169 pool,170 graph,171 select_chain: _,172 fee_history_limit,173 fee_history_cache,174 block_data_cache,175 enable_dev_signer,176 is_authority,177 network,178 deny_unsafe,179 filter_pool,180 backend,181 max_past_logs,182 } = deps;183184 io.extend_with(SystemApi::to_delegate(FullSystem::new(185 client.clone(),186 pool.clone(),187 deny_unsafe,188 )));189190 io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(191 client.clone(),192 )));193194 195196 let mut signers = Vec::new();197 if enable_dev_signer {198 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);199 }200201 let overrides = overrides_handle::<_, _, R>(client.clone());202203 io.extend_with(EthApiServer::to_delegate(EthApi::new(204 client.clone(),205 pool.clone(),206 graph,207 Some(<R as RuntimeInstance>::get_transaction_converter()),208 network.clone(),209 signers,210 overrides.clone(),211 backend.clone(),212 is_authority,213 block_data_cache.clone(),214 fee_history_limit,215 fee_history_cache,216 )));217 io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));218219 if let Some(filter_pool) = filter_pool {220 io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(221 client.clone(),222 backend,223 filter_pool,224 500_usize, 225 max_past_logs,226 block_data_cache,227 )));228 }229230 io.extend_with(NetApiServer::to_delegate(NetApi::new(231 client.clone(),232 network.clone(),233 234 true,235 )));236237 io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));238239 io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(240 pool,241 client,242 network,243 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(244 HexEncodedIdProvider::default(),245 Arc::new(subscription_task_executor),246 ),247 overrides,248 )));249250 io251}