1234567891011121314151617use sp_runtime::traits::BlakeTwo256;18use fc_rpc::{19 EthBlockDataCacheTask, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override,20 StorageOverride, SchemaV2Override, SchemaV3Override,21};22use jsonrpsee::RpcModule;23use fc_rpc_core::types::{FilterPool, FeeHistoryCache};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 uc_rpc::AppPromotion;42use std::{collections::BTreeMap, sync::Arc};4344use up_common::types::opaque::{Hash, AccountId, RuntimeInstance, Index, Block, BlockNumber, Balance};454647use up_data_structs::{48 RmrkCollectionInfo, RmrkInstanceInfo, RmrkResourceInfo, RmrkPropertyInfo, RmrkBaseInfo,49 RmrkPartType, RmrkTheme,50};515253pub struct GrandpaDeps<B> {54 55 pub shared_voter_state: SharedVoterState,56 57 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,58 59 pub justification_stream: GrandpaJustificationStream<Block>,60 61 pub subscription_executor: SubscriptionTaskExecutor,62 63 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,64}656667pub struct FullDeps<C, P, SC, CA: ChainApi> {68 69 pub client: Arc<C>,70 71 pub pool: Arc<P>,72 73 pub graph: Arc<Pool<CA>>,74 75 pub select_chain: SC,76 77 pub is_authority: bool,78 79 pub enable_dev_signer: bool,80 81 pub network: Arc<NetworkService<Block, Hash>>,82 83 pub deny_unsafe: DenyUnsafe,84 85 pub filter_pool: Option<FilterPool>,86 87 pub backend: Arc<fc_db::Backend<Block>>,88 89 pub max_past_logs: u32,90 91 pub fee_history_limit: u64,92 93 pub fee_history_cache: FeeHistoryCache,94 95 pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,96}9798pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>99where100 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,101 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,102 C: Send + Sync + 'static,103 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,104 C::Api:105 up_rpc::UniqueApi<Block, BlockNumber, <R as RuntimeInstance>::CrossAccountId, AccountId>,106 BE: Backend<Block> + 'static,107 BE::State: StateBackend<BlakeTwo256>,108 R: RuntimeInstance + Send + Sync + 'static,109{110 let mut overrides_map = BTreeMap::new();111 overrides_map.insert(112 EthereumStorageSchema::V1,113 Box::new(SchemaV1Override::new(client.clone()))114 as Box<dyn StorageOverride<_> + Send + Sync>,115 );116 overrides_map.insert(117 EthereumStorageSchema::V2,118 Box::new(SchemaV2Override::new(client.clone()))119 as Box<dyn StorageOverride<_> + Send + Sync>,120 );121 overrides_map.insert(122 EthereumStorageSchema::V3,123 Box::new(SchemaV3Override::new(client.clone()))124 as Box<dyn StorageOverride<_> + Send + Sync>,125 );126127 Arc::new(OverrideHandle {128 schemas: overrides_map,129 fallback: Box::new(RuntimeApiStorageOverride::new(client)),130 })131}132133134pub fn create_full<C, P, SC, CA, R, A, B>(135 deps: FullDeps<C, P, SC, CA>,136 subscription_task_executor: SubscriptionTaskExecutor,137) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>138where139 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,140 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,141 C: Send + Sync + 'static,142 C: BlockchainEvents<Block>,143 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,144 C::Api: BlockBuilder<Block>,145 146 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,147 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,148 C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,149 C::Api:150 up_rpc::UniqueApi<Block, BlockNumber, <R as RuntimeInstance>::CrossAccountId, AccountId>,151 C::Api: app_promotion_rpc::AppPromotionApi<152 Block,153 BlockNumber,154 <R as RuntimeInstance>::CrossAccountId,155 AccountId,156 >,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 Eth, EthApiServer, EthDevSigner, EthFilter, EthFilterApiServer, EthPubSub,178 EthPubSubApiServer, EthSigner, Net, NetApiServer, Web3, Web3ApiServer,179 };180 use uc_rpc::{UniqueApiServer, Unique};181 use uc_rpc::{AppPromotionApiServer, AppPromotion};182183 #[cfg(not(feature = "unique-runtime"))]184 use uc_rpc::{RmrkApiServer, Rmrk};185186 187 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};188 use substrate_frame_rpc_system::{System, SystemApiServer};189190 let mut io = RpcModule::new(());191 let FullDeps {192 client,193 pool,194 graph,195 select_chain: _,196 fee_history_limit,197 fee_history_cache,198 block_data_cache,199 enable_dev_signer,200 is_authority,201 network,202 deny_unsafe,203 filter_pool,204 backend,205 max_past_logs,206 } = deps;207208 io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;209 io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;210211 212213 let mut signers = Vec::new();214 if enable_dev_signer {215 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);216 }217218 let overrides = overrides_handle::<_, _, R>(client.clone());219220 io.merge(221 Eth::new(222 client.clone(),223 pool.clone(),224 graph,225 Some(<R as RuntimeInstance>::get_transaction_converter()),226 network.clone(),227 signers,228 overrides.clone(),229 backend.clone(),230 is_authority,231 block_data_cache.clone(),232 fee_history_cache,233 fee_history_limit,234 )235 .into_rpc(),236 )?;237238 io.merge(Unique::new(client.clone()).into_rpc())?;239240 241 io.merge(AppPromotion::new(client.clone()).into_rpc())?;242243 #[cfg(not(feature = "unique-runtime"))]244 io.merge(Rmrk::new(client.clone()).into_rpc())?;245246 if let Some(filter_pool) = filter_pool {247 io.merge(248 EthFilter::new(249 client.clone(),250 backend,251 filter_pool,252 500_usize, 253 max_past_logs,254 block_data_cache,255 )256 .into_rpc(),257 )?;258 }259260 io.merge(261 Net::new(262 client.clone(),263 network.clone(),264 265 true,266 )267 .into_rpc(),268 )?;269270 io.merge(Web3::new(client.clone()).into_rpc())?;271272 io.merge(273 EthPubSub::new(pool, client, network, subscription_task_executor, overrides).into_rpc(),274 )?;275276 Ok(io)277}