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 std::{collections::BTreeMap, sync::Arc};4243use up_common::types::opaque::*;4445#[cfg(feature = "pov-estimate")]46type FullBackend = sc_service::TFullBackend<Block>;474849pub struct GrandpaDeps<B> {50 51 pub shared_voter_state: SharedVoterState,52 53 pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,54 55 pub justification_stream: GrandpaJustificationStream<Block>,56 57 pub subscription_executor: SubscriptionTaskExecutor,58 59 pub finality_provider: Arc<FinalityProofProvider<B, Block>>,60}616263pub struct FullDeps<C, P, SC, CA: ChainApi> {64 65 pub client: Arc<C>,66 67 pub pool: Arc<P>,68 69 pub graph: Arc<Pool<CA>>,70 71 pub select_chain: SC,72 73 pub is_authority: bool,74 75 pub enable_dev_signer: bool,76 77 pub network: Arc<NetworkService<Block, Hash>>,78 79 pub deny_unsafe: DenyUnsafe,80 81 pub filter_pool: Option<FilterPool>,8283 84 pub runtime_id: RuntimeId,85 86 #[cfg(feature = "pov-estimate")]87 pub exec_params: uc_rpc::pov_estimate::ExecutorParams,88 89 #[cfg(feature = "pov-estimate")]90 pub backend: Arc<FullBackend>,9192 93 pub eth_backend: Arc<fc_db::Backend<Block>>,94 95 pub max_past_logs: u32,96 97 pub fee_history_limit: u64,98 99 pub fee_history_cache: FeeHistoryCache,100 101 pub block_data_cache: Arc<EthBlockDataCacheTask<Block>>,102}103104pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>105where106 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,107 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,108 C: Send + Sync + 'static,109 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,110 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,111 BE: Backend<Block> + 'static,112 BE::State: StateBackend<BlakeTwo256>,113 R: RuntimeInstance + Send + Sync + 'static,114{115 let mut overrides_map = BTreeMap::new();116 overrides_map.insert(117 EthereumStorageSchema::V1,118 Box::new(SchemaV1Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,119 );120 overrides_map.insert(121 EthereumStorageSchema::V2,122 Box::new(SchemaV2Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,123 );124 overrides_map.insert(125 EthereumStorageSchema::V3,126 Box::new(SchemaV3Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,127 );128129 Arc::new(OverrideHandle {130 schemas: overrides_map,131 fallback: Box::new(RuntimeApiStorageOverride::new(client)),132 })133}134135136pub fn create_full<C, P, SC, CA, R, A, B>(137 deps: FullDeps<C, P, SC, CA>,138 subscription_task_executor: SubscriptionTaskExecutor,139) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>140where141 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,142 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,143 C: Send + Sync + 'static,144 C: BlockchainEvents<Block>,145 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,146 C::Api: BlockBuilder<Block>,147 148 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,149 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,150 C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,151 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,152 C::Api: app_promotion_rpc::AppPromotionApi<153 Block,154 BlockNumber,155 <R as RuntimeInstance>::CrossAccountId,156 AccountId,157 >,158 C::Api: up_pov_estimate_rpc::PovEstimateApi<Block>,159 B: sc_client_api::Backend<Block> + Send + Sync + 'static,160 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,161 P: TransactionPool<Block = Block> + 'static,162 CA: ChainApi<Block = Block> + 'static,163 R: RuntimeInstance + Send + Sync + 'static,164 <R as RuntimeInstance>::CrossAccountId: serde::Serialize,165 for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,166{167 use fc_rpc::{168 Eth, EthApiServer, EthDevSigner, EthFilter, EthFilterApiServer, EthPubSub,169 EthPubSubApiServer, EthSigner, Net, NetApiServer, Web3, Web3ApiServer,170 };171 use uc_rpc::{UniqueApiServer, Unique};172173 use uc_rpc::{AppPromotionApiServer, AppPromotion};174175 #[cfg(feature = "pov-estimate")]176 use uc_rpc::pov_estimate::{PovEstimateApiServer, PovEstimate};177178 179 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};180 use substrate_frame_rpc_system::{System, SystemApiServer};181182 let mut io = RpcModule::new(());183 let FullDeps {184 client,185 pool,186 graph,187 select_chain: _,188 fee_history_limit,189 fee_history_cache,190 block_data_cache,191 enable_dev_signer,192 is_authority,193 network,194 deny_unsafe,195 filter_pool,196197 runtime_id: _,198199 #[cfg(feature = "pov-estimate")]200 exec_params,201202 #[cfg(feature = "pov-estimate")]203 backend,204205 eth_backend,206 max_past_logs,207 } = deps;208209 io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;210 io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;211212 213214 let mut signers = Vec::new();215 if enable_dev_signer {216 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);217 }218219 let overrides = overrides_handle::<_, _, R>(client.clone());220221 let execute_gas_limit_multiplier = 10;222 io.merge(223 Eth::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 eth_backend.clone(),232 is_authority,233 block_data_cache.clone(),234 fee_history_cache,235 fee_history_limit,236 execute_gas_limit_multiplier,237 )238 .into_rpc(),239 )?;240241 io.merge(Unique::new(client.clone()).into_rpc())?;242243 io.merge(AppPromotion::new(client.clone()).into_rpc())?;244245 #[cfg(feature = "pov-estimate")]246 io.merge(247 PovEstimate::new(248 client.clone(),249 backend,250 deny_unsafe,251 exec_params,252 runtime_id,253 )254 .into_rpc(),255 )?;256257 if let Some(filter_pool) = filter_pool {258 io.merge(259 EthFilter::new(260 client.clone(),261 eth_backend,262 filter_pool,263 500_usize, 264 max_past_logs,265 block_data_cache,266 )267 .into_rpc(),268 )?;269 }270271 io.merge(272 Net::new(273 client.clone(),274 network.clone(),275 276 true,277 )278 .into_rpc(),279 )?;280281 io.merge(Web3::new(client.clone()).into_rpc())?;282283 io.merge(284 EthPubSub::new(pool, client, network, subscription_task_executor, overrides).into_rpc(),285 )?;286287 Ok(io)288}