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,45 AccountId,46 RuntimeInstance,47 Index,48 Block,49 BlockNumber,50 Balance,51};5253use up_data_structs::{54 RmrkCollectionInfo, RmrkInstanceInfo, RmrkResourceInfo, RmrkPropertyInfo, RmrkBaseInfo, RmrkPartType, RmrkTheme, 55};565758pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;596061pub struct GrandpaDeps<B> {62 63 pub shared_voter_state: SharedVoterState,64 65 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,66 67 pub justification_stream: GrandpaJustificationStream<Block>,68 69 pub subscription_executor: SubscriptionTaskExecutor,70 71 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,72}737475pub struct FullDeps<C, P, SC, CA: ChainApi> {76 77 pub client: Arc<C>,78 79 pub pool: Arc<P>,80 81 pub graph: Arc<Pool<CA>>,82 83 pub select_chain: SC,84 85 pub is_authority: bool,86 87 pub enable_dev_signer: bool,88 89 pub network: Arc<NetworkService<Block, Hash>>,90 91 pub deny_unsafe: DenyUnsafe,92 93 pub filter_pool: Option<FilterPool>,94 95 pub backend: Arc<fc_db::Backend<Block>>,96 97 pub max_past_logs: u32,98 99 pub fee_history_limit: u64,100 101 pub fee_history_cache: FeeHistoryCache,102 103 pub block_data_cache: Arc<EthBlockDataCache<Block>>,104}105106pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>107where108 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,109 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,110 C: Send + Sync + 'static,111 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,112 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,113 BE: Backend<Block> + 'static,114 BE::State: StateBackend<BlakeTwo256>,115 R: RuntimeInstance + Send + Sync + 'static,116{117 let mut overrides_map = BTreeMap::new();118 overrides_map.insert(119 EthereumStorageSchema::V1,120 Box::new(SchemaV1Override::new(client.clone()))121 as Box<dyn StorageOverride<_> + Send + Sync>,122 );123 overrides_map.insert(124 EthereumStorageSchema::V2,125 Box::new(SchemaV2Override::new(client.clone()))126 as Box<dyn StorageOverride<_> + Send + Sync>,127 );128 overrides_map.insert(129 EthereumStorageSchema::V3,130 Box::new(SchemaV3Override::new(client.clone()))131 as Box<dyn StorageOverride<_> + Send + Sync>,132 );133134 Arc::new(OverrideHandle {135 schemas: overrides_map,136 fallback: Box::new(RuntimeApiStorageOverride::new(client)),137 })138}139140141pub fn create_full<C, P, SC, CA, R, A, B>(142 deps: FullDeps<C, P, SC, CA>,143 subscription_task_executor: SubscriptionTaskExecutor,144) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata>145where146 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,147 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,148 C: Send + Sync + 'static,149 C: BlockchainEvents<Block>,150 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,151 C::Api: BlockBuilder<Block>,152 153 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,154 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,155 C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,156 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,157 C::Api: rmrk_rpc::RmrkApi<158 Block,159 AccountId,160 RmrkCollectionInfo<AccountId>,161 RmrkInstanceInfo<AccountId>,162 RmrkResourceInfo,163 RmrkPropertyInfo,164 RmrkBaseInfo<AccountId>,165 RmrkPartType,166 RmrkTheme,167 >,168 B: sc_client_api::Backend<Block> + Send + Sync + 'static,169 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,170 P: TransactionPool<Block = Block> + 'static,171 CA: ChainApi<Block = Block> + 'static,172 R: RuntimeInstance + Send + Sync + 'static,173 <R as RuntimeInstance>::CrossAccountId: serde::Serialize,174 for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,175{176 use fc_rpc::{177 EthApi, EthApiServer, EthDevSigner, EthFilterApi, EthFilterApiServer, EthPubSubApi,178 EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,179 Web3ApiServer,180 };181 use uc_rpc::{UniqueApi, RmrkApi, Unique};182 183 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};184 use substrate_frame_rpc_system::{FullSystem, SystemApi};185186 let mut io = jsonrpc_core::IoHandler::default();187 let FullDeps {188 client,189 pool,190 graph,191 select_chain: _,192 fee_history_limit,193 fee_history_cache,194 block_data_cache,195 enable_dev_signer,196 is_authority,197 network,198 deny_unsafe,199 filter_pool,200 backend,201 max_past_logs,202 } = deps;203204 io.extend_with(SystemApi::to_delegate(FullSystem::new(205 client.clone(),206 pool.clone(),207 deny_unsafe,208 )));209210 io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(211 client.clone(),212 )));213214 215216 let mut signers = Vec::new();217 if enable_dev_signer {218 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);219 }220221 let overrides = overrides_handle::<_, _, R>(client.clone());222223 io.extend_with(EthApiServer::to_delegate(EthApi::new(224 client.clone(),225 pool.clone(),226 graph,227 Some(<R as RuntimeInstance>::get_transaction_converter()),228 network.clone(),229 signers,230 overrides.clone(),231 backend.clone(),232 is_authority,233 block_data_cache.clone(),234 fee_history_limit,235 fee_history_cache,236 )));237238 239 240 io.extend_with(UniqueApi::to_delegate(Unique::new(client.clone())));241 io.extend_with(RmrkApi::to_delegate(Unique::new(client.clone())));242243 if let Some(filter_pool) = filter_pool {244 io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(245 client.clone(),246 backend,247 filter_pool,248 500_usize, 249 max_past_logs,250 block_data_cache,251 )));252 }253254 io.extend_with(NetApiServer::to_delegate(NetApi::new(255 client.clone(),256 network.clone(),257 258 true,259 )));260261 io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));262263 io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(264 pool,265 client,266 network,267 SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(268 HexEncodedIdProvider::default(),269 Arc::new(subscription_task_executor),270 ),271 overrides,272 )));273274 io275}