1234567891011121314151617use sp_runtime::traits::BlakeTwo256;18use unique_runtime::{Hash, AccountId, CrossAccountId, Index, opaque::Block, BlockNumber, Balance};19use fc_rpc::{20 EthBlockDataCache, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override,21 StorageOverride, SchemaV2Override, SchemaV3Override,22};23use fc_rpc_core::types::{FilterPool, FeeHistoryCache};24use jsonrpc_pubsub::manager::SubscriptionManager;25use pallet_ethereum::EthereumStorageSchema;26use sc_client_api::{27 backend::{AuxStore, StorageProvider},28 client::BlockchainEvents,29 StateBackend, Backend,30};31use sc_finality_grandpa::{32 FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,33};34use sc_network::NetworkService;35use sc_rpc::SubscriptionTaskExecutor;36pub use sc_rpc_api::DenyUnsafe;37use sc_transaction_pool::{ChainApi, Pool};38use sp_api::ProvideRuntimeApi;39use sp_block_builder::BlockBuilder;40use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};41use sc_service::TransactionPool;42use std::{collections::BTreeMap, marker::PhantomData, sync::Arc};434445pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;464748pub struct GrandpaDeps<B> {49 50 pub shared_voter_state: SharedVoterState,51 52 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,53 54 pub justification_stream: GrandpaJustificationStream<Block>,55 56 pub subscription_executor: SubscriptionTaskExecutor,57 58 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,59}606162pub struct FullDeps<C, P, SC, CA: ChainApi> {63 64 pub client: Arc<C>,65 66 pub pool: Arc<P>,67 68 pub graph: Arc<Pool<CA>>,69 70 pub select_chain: SC,71 72 pub is_authority: bool,73 74 pub enable_dev_signer: bool,75 76 pub network: Arc<NetworkService<Block, Hash>>,77 78 pub deny_unsafe: DenyUnsafe,79 80 pub filter_pool: Option<FilterPool>,81 82 pub backend: Arc<fc_db::Backend<Block>>,83 84 pub max_past_logs: u32,85 86 pub fee_history_limit: u64,87 88 pub fee_history_cache: FeeHistoryCache,89 90 pub block_data_cache: Arc<EthBlockDataCache<Block>>,91}9293struct AccountCodes<C, B> {94 client: Arc<C>,95 _marker: PhantomData<B>,96}9798impl<C, Block> AccountCodes<C, Block>99where100 Block: sp_api::BlockT,101 C: ProvideRuntimeApi<Block>,102{103 fn new(client: Arc<C>) -> Self {104 Self {105 client,106 _marker: PhantomData,107 }108 }109}110111impl<C, Block> fc_rpc::AccountCodeProvider<Block> for AccountCodes<C, Block>112where113 Block: sp_api::BlockT,114 C: ProvideRuntimeApi<Block>,115 C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,116{117 fn code(&self, block: &sp_api::BlockId<Block>, account: sp_core::H160) -> Option<Vec<u8>> {118 use up_rpc::UniqueApi;119 self.client120 .runtime_api()121 .eth_contract_code(block, account)122 .ok()123 .flatten()124 }125}126127pub fn overrides_handle<C, BE>(client: Arc<C>) -> Arc<OverrideHandle<Block>>128where129 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,130 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,131 C: Send + Sync + 'static,132 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,133 C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,134 BE: Backend<Block> + 'static,135 BE::State: StateBackend<BlakeTwo256>,136{137 let mut overrides_map = BTreeMap::new();138 overrides_map.insert(139 EthereumStorageSchema::V1,140 Box::new(SchemaV1Override::new_with_code_provider(141 client.clone(),142 Arc::new(AccountCodes::<C, Block>::new(client.clone())),143 )) as Box<dyn StorageOverride<_> + Send + Sync>,144 );145 overrides_map.insert(146 EthereumStorageSchema::V2,147 Box::new(SchemaV2Override::new(client.clone()))148 as Box<dyn StorageOverride<_> + Send + Sync>,149 );150 overrides_map.insert(151 EthereumStorageSchema::V3,152 Box::new(SchemaV3Override::new(client.clone()))153 as Box<dyn StorageOverride<_> + Send + Sync>,154 );155156 Arc::new(OverrideHandle {157 schemas: overrides_map,158 fallback: Box::new(RuntimeApiStorageOverride::new(client)),159 })160}161162163pub fn create_full<C, P, SC, CA, A, B>(164 deps: FullDeps<C, P, SC, CA>,165 subscription_task_executor: SubscriptionTaskExecutor,166) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>167where168 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,169 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,170 C: Send + Sync + 'static,171 C: BlockchainEvents<Block>,172 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,173 C::Api: BlockBuilder<Block>,174 175 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,176 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,177 C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,178 B: sc_client_api::Backend<Block> + Send + Sync + 'static,179 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,180 P: TransactionPool<Block = Block> + 'static,181 CA: ChainApi<Block = Block> + 'static,182{183 use fc_rpc::{184 EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,185 EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,186 Web3ApiServer,187 };188 use uc_rpc::{UniqueApi, Unique};189 190 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};191 use substrate_frame_rpc_system::{FullSystem, SystemApi};192193 let mut io = jsonrpc_core::IoHandler::default();194 let FullDeps {195 client,196 pool,197 graph,198 select_chain: _,199 fee_history_limit,200 fee_history_cache,201 block_data_cache,202 enable_dev_signer,203 is_authority,204 network,205 deny_unsafe,206 filter_pool,207 backend,208 max_past_logs,209 } = deps;210211 io.extend_with(SystemApi::to_delegate(FullSystem::new(212 client.clone(),213 pool.clone(),214 deny_unsafe,215 )));216217 io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(218 client.clone(),219 )));220221 222223 let mut signers = Vec::new();224 if enable_dev_signer {225 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);226 }227228 let overrides = overrides_handle(client.clone());229230 io.extend_with(EthApiServer::to_delegate(EthApi::new(231 client.clone(),232 pool.clone(),233 graph,234 unique_runtime::TransactionConverter,235 network.clone(),236 signers,237 overrides.clone(),238 backend.clone(),239 is_authority,240 max_past_logs,241 block_data_cache.clone(),242 fee_history_limit,243 fee_history_cache,244 )));245 io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));246247 if let Some(filter_pool) = filter_pool {248 io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(249 client.clone(),250 backend,251 filter_pool,252 500_usize, 253 max_past_logs,254 block_data_cache,255 )));256 }257258 io.extend_with(NetApiServer::to_delegate(NetApi::new(259 client.clone(),260 network.clone(),261 262 true,263 )));264265 io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));266267 io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(268 pool,269 client,270 network,271 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(272 HexEncodedIdProvider::default(),273 Arc::new(subscription_task_executor),274 ),275 overrides,276 )));277278 io279}