1use unique_runtime::{Hash, AccountId, CrossAccountId, Index, opaque::Block, BlockNumber, Balance};2use fc_rpc::{3 EthBlockDataCache, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override, StorageOverride,4};5use fc_rpc_core::types::FilterPool;6use jsonrpc_pubsub::manager::SubscriptionManager;7use pallet_ethereum::EthereumStorageSchema;8use sc_client_api::{9 backend::{AuxStore, StorageProvider},10 client::BlockchainEvents,11};12use sc_finality_grandpa::{13 FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,14};15use sc_network::NetworkService;16use sc_rpc::SubscriptionTaskExecutor;17pub use sc_rpc_api::DenyUnsafe;18use sc_transaction_pool::{ChainApi, Pool};19use sp_api::ProvideRuntimeApi;20use sp_block_builder::BlockBuilder;21use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};22use sp_consensus::SelectChain;23use sc_service::TransactionPool;24use std::{collections::BTreeMap, marker::PhantomData, sync::Arc};252627pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;282930pub struct LightDeps<C, F, P> {31 32 pub client: Arc<C>,33 34 pub pool: Arc<P>,35 36 pub remote_blockchain: Arc<dyn sc_client_api::light::RemoteBlockchain<Block>>,37 38 pub fetcher: Arc<F>,39}404142pub struct GrandpaDeps<B> {43 44 pub shared_voter_state: SharedVoterState,45 46 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,47 48 pub justification_stream: GrandpaJustificationStream<Block>,49 50 pub subscription_executor: SubscriptionTaskExecutor,51 52 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,53}545556pub struct FullDeps<C, P, SC, CA: ChainApi> {57 58 pub client: Arc<C>,59 60 pub pool: Arc<P>,61 62 pub graph: Arc<Pool<CA>>,63 64 pub select_chain: SC,65 66 pub is_authority: bool,67 68 pub enable_dev_signer: bool,69 70 pub network: Arc<NetworkService<Block, Hash>>,71 72 pub deny_unsafe: DenyUnsafe,73 74 pub filter_pool: Option<FilterPool>,75 76 pub backend: Arc<fc_db::Backend<Block>>,77 78 pub max_past_logs: u32,79}8081struct AccountCodes<C, B> {82 client: Arc<C>,83 _marker: PhantomData<B>,84}8586impl<C, Block> AccountCodes<C, Block>87where88 Block: sp_api::BlockT,89 C: ProvideRuntimeApi<Block>,90{91 fn new(client: Arc<C>) -> Self {92 Self {93 client,94 _marker: PhantomData,95 }96 }97}9899impl<C, Block> fc_rpc::AccountCodeProvider<Block> for AccountCodes<C, Block>100where101 Block: sp_api::BlockT,102 C: ProvideRuntimeApi<Block>,103 C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,104{105 fn code(&self, block: &sp_api::BlockId<Block>, account: sp_core::H160) -> Option<Vec<u8>> {106 use up_rpc::UniqueApi;107 self.client108 .runtime_api()109 .eth_contract_code(block, account)110 .ok()111 .flatten()112 }113}114115116pub fn create_full<C, P, SC, CA, A, B>(117 deps: FullDeps<C, P, SC, CA>,118 subscription_task_executor: SubscriptionTaskExecutor,119) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>120where121 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,122 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,123 C: Send + Sync + 'static,124 C: BlockchainEvents<Block>,125 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,126 C::Api: BlockBuilder<Block>,127 128 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,129 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,130 C::Api: up_rpc::UniqueApi<Block, CrossAccountId, AccountId>,131 B: sc_client_api::Backend<Block> + Send + Sync + 'static,132 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,133 SC: SelectChain<Block> + 'static,134 P: TransactionPool<Block = Block> + 'static,135 CA: ChainApi<Block = Block> + 'static,136{137 use fc_rpc::{138 EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,139 EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,140 Web3ApiServer,141 };142 use uc_rpc::{UniqueApi, Unique};143 144 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};145 use substrate_frame_rpc_system::{FullSystem, SystemApi};146147 let mut io = jsonrpc_core::IoHandler::default();148 let FullDeps {149 client,150 pool,151 graph,152 select_chain: _,153 enable_dev_signer,154 is_authority,155 network,156 deny_unsafe,157 filter_pool,158 backend,159 max_past_logs,160 } = deps;161162 io.extend_with(SystemApi::to_delegate(FullSystem::new(163 client.clone(),164 pool.clone(),165 deny_unsafe,166 )));167168 io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(169 client.clone(),170 )));171172 173174 let mut signers = Vec::new();175 if enable_dev_signer {176 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);177 }178 let mut overrides_map = BTreeMap::new();179 overrides_map.insert(180 EthereumStorageSchema::V1,181 Box::new(SchemaV1Override::new_with_code_provider(182 client.clone(),183 Arc::new(AccountCodes::<C, Block>::new(client.clone())),184 )) as Box<dyn StorageOverride<_> + Send + Sync>,185 );186187 let overrides = Arc::new(OverrideHandle {188 schemas: overrides_map,189 fallback: Box::new(RuntimeApiStorageOverride::new(client.clone())),190 });191192 let block_data_cache = Arc::new(EthBlockDataCache::new(50, 50));193194 io.extend_with(EthApiServer::to_delegate(EthApi::new(195 client.clone(),196 pool.clone(),197 graph,198 unique_runtime::TransactionConverter,199 network.clone(),200 signers,201 overrides.clone(),202 backend.clone(),203 is_authority,204 max_past_logs,205 block_data_cache.clone(),206 )));207 io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));208209 if let Some(filter_pool) = filter_pool {210 io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(211 client.clone(),212 backend,213 filter_pool,214 500_usize, 215 overrides.clone(),216 max_past_logs,217 block_data_cache.clone(),218 )));219 }220221 io.extend_with(NetApiServer::to_delegate(NetApi::new(222 client.clone(),223 network.clone(),224 225 true,226 )));227228 io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));229230 io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(231 pool,232 client,233 network,234 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(235 HexEncodedIdProvider::default(),236 Arc::new(subscription_task_executor),237 ),238 overrides,239 )));240241 io242}243244245pub fn create_light<C, P, M, F>(deps: LightDeps<C, F, P>) -> jsonrpc_core::IoHandler<M>246where247 C: sp_blockchain::HeaderBackend<Block>,248 C: Send + Sync + 'static,249 F: sc_client_api::light::Fetcher<Block> + 'static,250 P: TransactionPool + 'static,251 M: jsonrpc_core::Metadata + Default,252{253 use substrate_frame_rpc_system::{LightSystem, SystemApi};254255 let LightDeps {256 client,257 pool,258 remote_blockchain,259 fetcher,260 } = deps;261 let mut io = jsonrpc_core::IoHandler::default();262 io.extend_with(SystemApi::<Hash, AccountId, Index>::to_delegate(263 LightSystem::new(client, remote_blockchain, fetcher, pool),264 ));265266 io267}