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 pallet_ethereum::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, marker::PhantomData, sync::Arc};4243#[cfg(feature = "unique-runtime")]44use unique_runtime as runtime;4546#[cfg(feature = "quartz-runtime")]47use quartz_runtime as runtime;4849#[cfg(feature = "opal-runtime")]50use opal_runtime as runtime;5152use runtime::opaque::{Hash, AccountId, CrossAccountId, Index, Block, BlockNumber, Balance};535455pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;565758pub struct GrandpaDeps<B> {59 60 pub shared_voter_state: SharedVoterState,61 62 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,63 64 pub justification_stream: GrandpaJustificationStream<Block>,65 66 pub subscription_executor: SubscriptionTaskExecutor,67 68 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,69}707172pub struct FullDeps<C, P, SC, CA: ChainApi> {73 74 pub client: Arc<C>,75 76 pub pool: Arc<P>,77 78 pub graph: Arc<Pool<CA>>,79 80 pub select_chain: SC,81 82 pub is_authority: bool,83 84 pub enable_dev_signer: bool,85 86 pub network: Arc<NetworkService<Block, Hash>>,87 88 pub deny_unsafe: DenyUnsafe,89 90 pub filter_pool: Option<FilterPool>,91 92 pub backend: Arc<fc_db::Backend<Block>>,93 94 pub max_past_logs: u32,95 96 pub fee_history_limit: u64,97 98 pub fee_history_cache: FeeHistoryCache,99 100 pub block_data_cache: Arc<EthBlockDataCache<Block>>,101}102103struct AccountCodes<C, B> {104 client: Arc<C>,105 _marker: PhantomData<B>,106}107108impl<C, Block> AccountCodes<C, Block>109where110 Block: sp_api::BlockT,111 C: ProvideRuntimeApi<Block>,112{113 fn new(client: Arc<C>) -> Self {114 Self {115 client,116 _marker: PhantomData,117 }118 }119}120121impl<C, Block> fc_rpc::AccountCodeProvider<Block> for AccountCodes<C, Block>122where123 Block: sp_api::BlockT,124 C: ProvideRuntimeApi<Block>,125 C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,126{127 fn code(&self, block: &sp_api::BlockId<Block>, account: sp_core::H160) -> Option<Vec<u8>> {128 use up_rpc::UniqueApi;129 self.client130 .runtime_api()131 .eth_contract_code(block, account)132 .ok()133 .flatten()134 }135}136137pub fn overrides_handle<C, BE>(client: Arc<C>) -> Arc<OverrideHandle<Block>>138where139 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,140 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,141 C: Send + Sync + 'static,142 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,143 C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,144 BE: Backend<Block> + 'static,145 BE::State: StateBackend<BlakeTwo256>,146{147 let mut overrides_map = BTreeMap::new();148 overrides_map.insert(149 EthereumStorageSchema::V1,150 Box::new(SchemaV1Override::new_with_code_provider(151 client.clone(),152 Arc::new(AccountCodes::<C, Block>::new(client.clone())),153 )) as Box<dyn StorageOverride<_> + Send + Sync>,154 );155 overrides_map.insert(156 EthereumStorageSchema::V2,157 Box::new(SchemaV2Override::new(client.clone()))158 as Box<dyn StorageOverride<_> + Send + Sync>,159 );160 overrides_map.insert(161 EthereumStorageSchema::V3,162 Box::new(SchemaV3Override::new(client.clone()))163 as Box<dyn StorageOverride<_> + Send + Sync>,164 );165166 Arc::new(OverrideHandle {167 schemas: overrides_map,168 fallback: Box::new(RuntimeApiStorageOverride::new(client)),169 })170}171172173pub fn create_full<C, P, SC, CA, A, B>(174 deps: FullDeps<C, P, SC, CA>,175 subscription_task_executor: SubscriptionTaskExecutor,176) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>177where178 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,179 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,180 C: Send + Sync + 'static,181 C: BlockchainEvents<Block>,182 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,183 C::Api: BlockBuilder<Block>,184 185 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,186 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,187 C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,188 B: sc_client_api::Backend<Block> + Send + Sync + 'static,189 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,190 P: TransactionPool<Block = Block> + 'static,191 CA: ChainApi<Block = Block> + 'static,192{193 use fc_rpc::{194 EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,195 EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,196 Web3ApiServer,197 };198 use uc_rpc::{UniqueApi, Unique};199 200 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};201 use substrate_frame_rpc_system::{FullSystem, SystemApi};202203 let mut io = jsonrpc_core::IoHandler::default();204 let FullDeps {205 client,206 pool,207 graph,208 select_chain: _,209 fee_history_limit,210 fee_history_cache,211 block_data_cache,212 enable_dev_signer,213 is_authority,214 network,215 deny_unsafe,216 filter_pool,217 backend,218 max_past_logs,219 } = deps;220221 io.extend_with(SystemApi::to_delegate(FullSystem::new(222 client.clone(),223 pool.clone(),224 deny_unsafe,225 )));226227 io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(228 client.clone(),229 )));230231 232233 let mut signers = Vec::new();234 if enable_dev_signer {235 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);236 }237238 let overrides = overrides_handle(client.clone());239240 io.extend_with(EthApiServer::to_delegate(EthApi::new(241 client.clone(),242 pool.clone(),243 graph,244 runtime::TransactionConverter,245 network.clone(),246 signers,247 overrides.clone(),248 backend.clone(),249 is_authority,250 max_past_logs,251 block_data_cache.clone(),252 fee_history_limit,253 fee_history_cache,254 )));255 io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));256257 if let Some(filter_pool) = filter_pool {258 io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(259 client.clone(),260 backend,261 filter_pool,262 500_usize, 263 max_past_logs,264 block_data_cache,265 )));266 }267268 io.extend_with(NetApiServer::to_delegate(NetApi::new(269 client.clone(),270 network.clone(),271 272 true,273 )));274275 io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));276277 io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(278 pool,279 client,280 network,281 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(282 HexEncodedIdProvider::default(),283 Arc::new(subscription_task_executor),284 ),285 overrides,286 )));287288 io289}