123456use std::sync::Arc;7use core::marker::PhantomData;89use std::collections::BTreeMap;10use fc_rpc::RuntimeApiStorageOverride;11use fc_rpc::OverrideHandle;12use fc_rpc_core::types::{PendingTransactions, FilterPool};13use nft_runtime::{Hash, AccountId, Index, opaque::Block, BlockNumber, Balance, NftApi};14use sp_api::ProvideRuntimeApi;15use sp_blockchain::{Error as BlockChainError, HeaderMetadata, HeaderBackend};16use sc_client_api::{17 backend::{StorageProvider, Backend, StateBackend, AuxStore},18 client::BlockchainEvents19};20use sc_rpc::SubscriptionTaskExecutor;21use sp_runtime::traits::BlakeTwo256;22use sp_block_builder::BlockBuilder;23use sc_rpc_api::DenyUnsafe;24use sp_transaction_pool::TransactionPool;25use sc_network::NetworkService;26use jsonrpc_pubsub::manager::SubscriptionManager;27use pallet_ethereum::EthereumStorageSchema;28use fc_rpc::{StorageOverride, SchemaV1Override};293031pub struct LightDeps<C, F, P> {32 33 pub client: Arc<C>,34 35 pub pool: Arc<P>,36 37 pub remote_blockchain: Arc<dyn sc_client_api::light::RemoteBlockchain<Block>>,38 39 pub fetcher: Arc<F>,40}414243pub struct FullDeps<C, P> {44 45 pub client: Arc<C>,46 47 pub pool: Arc<P>,48 49 pub deny_unsafe: DenyUnsafe,50 51 pub is_authority: bool,52 53 pub enable_dev_signer: bool,54 55 pub network: Arc<NetworkService<Block, Hash>>,56 57 pub pending_transactions: PendingTransactions,58 59 pub filter_pool: Option<FilterPool>,60 61 pub backend: Arc<fc_db::Backend<Block>>,62 63 pub max_past_logs: u32,64}6566struct AccountCodes<C, B> {67 client: Arc<C>,68 _marker: PhantomData<B>,69}7071impl<C, Block> AccountCodes<C, Block>72where73 Block: sp_api::BlockT,74 C: ProvideRuntimeApi<Block>,75{76 fn new(client: Arc<C>) -> Self {77 Self {78 client,79 _marker: PhantomData,80 }81 }82}8384impl<C, Block> fc_rpc::AccountCodeProvider<Block> for AccountCodes<C, Block>85where86 Block: sp_api::BlockT,87 C: ProvideRuntimeApi<Block>,88 C::Api: pallet_nft::NftApi<Block>,89{90 fn code(&self, block: &sp_api::BlockId<Block>, account: sp_core::H160) -> Option<Vec<u8>> {91 self.client.runtime_api().eth_contract_code(block, account).ok().flatten()92 }93}949596pub fn create_full<C, P, BE>(97 deps: FullDeps<C, P>,98 subscription_task_executor: SubscriptionTaskExecutor99) -> jsonrpc_core::IoHandler<sc_rpc::Metadata> where100 BE: Backend<Block> + 'static,101 BE::State: StateBackend<BlakeTwo256>,102 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,103 C: BlockchainEvents<Block>,104 C: HeaderBackend<Block> + HeaderMetadata<Block, Error=BlockChainError>,105 C: Send + Sync + 'static,106 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,107 C::Api: BlockBuilder<Block>,108 C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber>,109 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,110 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,111 C::Api: pallet_nft::NftApi<Block>,112 P: TransactionPool<Block=Block> + 'static,113{114 use substrate_frame_rpc_system::{FullSystem, SystemApi};115 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};116 use fc_rpc::{117 EthApi, EthApiServer, EthFilterApi, EthFilterApiServer, NetApi, NetApiServer,118 EthPubSubApi, EthPubSubApiServer, Web3Api, Web3ApiServer, EthDevSigner, EthSigner,119 HexEncodedIdProvider,120 };121122 let mut io = jsonrpc_core::IoHandler::default();123 let FullDeps {124 client,125 pool,126 deny_unsafe,127 is_authority,128 network,129 pending_transactions,130 filter_pool,131 backend,132 enable_dev_signer,133 max_past_logs,134 } = deps;135136 io.extend_with(137 SystemApi::to_delegate(FullSystem::new(client.clone(), pool.clone(), deny_unsafe))138 );139140 io.extend_with(141 TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone()))142 );143144 io.extend_with(145 pallet_contracts_rpc::ContractsApi::to_delegate(pallet_contracts_rpc::Contracts::new(client.clone()))146 );147148 let mut signers = Vec::new();149 if enable_dev_signer {150 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);151 }152 let mut overrides_map = BTreeMap::new();153 overrides_map.insert(154 EthereumStorageSchema::V1,155 Box::new(SchemaV1Override::new_with_code_provider(156 client.clone(),157 Arc::new(AccountCodes::<C, Block>::new(client.clone()))158 )) as Box<dyn StorageOverride<_> + Send + Sync>159 );160161 let overrides = Arc::new(OverrideHandle {162 schemas: overrides_map,163 fallback: Box::new(RuntimeApiStorageOverride::new(client.clone())),164 });165166 io.extend_with(167 EthApiServer::to_delegate(EthApi::new(168 client.clone(),169 pool.clone(),170 nft_runtime::TransactionConverter,171 network.clone(),172 pending_transactions.clone(),173 signers,174 overrides.clone(),175 backend,176 is_authority,177 max_past_logs,178 ))179 );180181 if let Some(filter_pool) = filter_pool {182 io.extend_with(183 EthFilterApiServer::to_delegate(EthFilterApi::new(184 client.clone(),185 filter_pool.clone(),186 500 as usize, 187 overrides.clone(),188 max_past_logs,189 ))190 );191 }192193 io.extend_with(194 NetApiServer::to_delegate(NetApi::new(195 client.clone(),196 network.clone(),197 true,198 ))199 );200201 io.extend_with(202 Web3ApiServer::to_delegate(Web3Api::new(203 client.clone(),204 ))205 );206207 io.extend_with(208 EthPubSubApiServer::to_delegate(EthPubSubApi::new(209 pool.clone(),210 client.clone(),211 network.clone(),212 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(213 HexEncodedIdProvider::default(),214 Arc::new(subscription_task_executor)215 ),216 overrides,217 ))218 );219220 io221}222223224pub fn create_light<C, P, M, F>(225 deps: LightDeps<C, F, P>,226) -> jsonrpc_core::IoHandler<M> where227 C: sp_blockchain::HeaderBackend<Block>,228 C: Send + Sync + 'static,229 F: sc_client_api::light::Fetcher<Block> + 'static,230 P: TransactionPool + 'static,231 M: jsonrpc_core::Metadata + Default,232{233 use substrate_frame_rpc_system::{LightSystem, SystemApi};234235 let LightDeps {236 client,237 pool,238 remote_blockchain,239 fetcher240 } = deps;241 let mut io = jsonrpc_core::IoHandler::default();242 io.extend_with(243 SystemApi::<Hash, AccountId, Index>::to_delegate(244 LightSystem::new(client, remote_blockchain, fetcher, pool)245 )246 );247248 io249}