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};4647484950515253pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;545556pub struct GrandpaDeps<B> {57 58 pub shared_voter_state: SharedVoterState,59 60 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,61 62 pub justification_stream: GrandpaJustificationStream<Block>,63 64 pub subscription_executor: SubscriptionTaskExecutor,65 66 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,67}686970pub struct FullDeps<C, P, SC, CA: ChainApi> {71 72 pub client: Arc<C>,73 74 pub pool: Arc<P>,75 76 pub graph: Arc<Pool<CA>>,77 78 pub select_chain: SC,79 80 pub is_authority: bool,81 82 pub enable_dev_signer: bool,83 84 pub network: Arc<NetworkService<Block, Hash>>,85 86 pub deny_unsafe: DenyUnsafe,87 88 pub filter_pool: Option<FilterPool>,89 90 pub backend: Arc<fc_db::Backend<Block>>,91 92 pub max_past_logs: u32,93 94 pub fee_history_limit: u64,95 96 pub fee_history_cache: FeeHistoryCache,97 98 pub block_data_cache: Arc<EthBlockDataCache<Block>>,99}100101pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>102where103 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,104 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,105 C: Send + Sync + 'static,106 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,107 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,108 BE: Backend<Block> + 'static,109 BE::State: StateBackend<BlakeTwo256>,110 R: RuntimeInstance + Send + Sync + 'static,111{112 let mut overrides_map = BTreeMap::new();113 overrides_map.insert(114 EthereumStorageSchema::V1,115 Box::new(SchemaV1Override::new(client.clone()))116 as Box<dyn StorageOverride<_> + Send + Sync>,117 );118 overrides_map.insert(119 EthereumStorageSchema::V2,120 Box::new(SchemaV2Override::new(client.clone()))121 as Box<dyn StorageOverride<_> + Send + Sync>,122 );123 overrides_map.insert(124 EthereumStorageSchema::V3,125 Box::new(SchemaV3Override::new(client.clone()))126 as Box<dyn StorageOverride<_> + Send + Sync>,127 );128129 Arc::new(OverrideHandle {130 schemas: overrides_map,131 fallback: Box::new(RuntimeApiStorageOverride::new(client)),132 })133}134135136pub fn create_full<C, P, SC, CA, R, A, B>(137 deps: FullDeps<C, P, SC, CA>,138 subscription_task_executor: SubscriptionTaskExecutor,139) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>140where141 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,142 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,143 C: Send + Sync + 'static,144 C: BlockchainEvents<Block>,145 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,146 C::Api: BlockBuilder<Block>,147 148 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,149 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,150 C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,151 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,152 153154155156157158159160161162163164 B: sc_client_api::Backend<Block> + Send + Sync + 'static,165 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,166 P: TransactionPool<Block = Block> + 'static,167 CA: ChainApi<Block = Block> + 'static,168 R: RuntimeInstance + Send + Sync + 'static,169 <R as RuntimeInstance>::CrossAccountId: serde::Serialize,170 for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,171{172 use fc_rpc::{173 EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,174 EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,175 Web3ApiServer,176 };177 use uc_rpc::{UniqueApi, RmrkApi, Unique};178 179 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};180 use substrate_frame_rpc_system::{FullSystem, SystemApi};181182 let mut io = jsonrpc_core::IoHandler::default();183 let FullDeps {184 client,185 pool,186 graph,187 select_chain: _,188 fee_history_limit,189 fee_history_cache,190 block_data_cache,191 enable_dev_signer,192 is_authority,193 network,194 deny_unsafe,195 filter_pool,196 backend,197 max_past_logs,198 } = deps;199200 io.extend_with(SystemApi::to_delegate(FullSystem::new(201 client.clone(),202 pool.clone(),203 deny_unsafe,204 )));205206 io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(207 client.clone(),208 )));209210 211212 let mut signers = Vec::new();213 if enable_dev_signer {214 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);215 }216217 let overrides = overrides_handle::<_, _, R>(client.clone());218219 io.extend_with(EthApiServer::to_delegate(EthApi::new(220 client.clone(),221 pool.clone(),222 graph,223 Some(<R as RuntimeInstance>::get_transaction_converter()),224 network.clone(),225 signers,226 overrides.clone(),227 backend.clone(),228 is_authority,229 block_data_cache.clone(),230 fee_history_limit,231 fee_history_cache,232 )));233234 235 236 io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));237 238239 if let Some(filter_pool) = filter_pool {240 io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(241 client.clone(),242 backend,243 filter_pool,244 500_usize, 245 max_past_logs,246 block_data_cache,247 )));248 }249250 io.extend_with(NetApiServer::to_delegate(NetApi::new(251 client.clone(),252 network.clone(),253 254 true,255 )));256257 io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));258259 io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(260 pool,261 client,262 network,263 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(264 HexEncodedIdProvider::default(),265 Arc::new(subscription_task_executor),266 ),267 overrides,268 )));269270 io271}