1234567891011121314151617use fc_mapping_sync::{EthereumBlockNotificationSinks, EthereumBlockNotification};18use sp_runtime::traits::BlakeTwo256;19use fc_rpc::{20 EthBlockDataCacheTask, OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override,21 StorageOverride, SchemaV2Override, SchemaV3Override,22};23use jsonrpsee::RpcModule;24use fc_rpc_core::types::{FilterPool, FeeHistoryCache};25use fp_storage::EthereumStorageSchema;26use sc_client_api::{27 backend::{AuxStore, StorageProvider},28 client::BlockchainEvents,29 StateBackend, Backend,30};31use sc_network::NetworkService;32use sc_network_sync::SyncingService;33use sc_rpc::SubscriptionTaskExecutor;34pub use sc_rpc_api::DenyUnsafe;35use sc_transaction_pool::{ChainApi, Pool};36use sp_api::ProvideRuntimeApi;37use sp_block_builder::BlockBuilder;38use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};39use sc_service::TransactionPool;40use std::{collections::BTreeMap, sync::Arc};4142use up_common::types::opaque::*;4344#[cfg(feature = "pov-estimate")]45type FullBackend = sc_service::TFullBackend<Block>;464748pub struct FullDeps<C, P, SC> {49 50 pub client: Arc<C>,51 52 pub pool: Arc<P>,53 54 pub select_chain: SC,55 56 pub deny_unsafe: DenyUnsafe,5758 59 pub runtime_id: RuntimeId,60 61 #[cfg(feature = "pov-estimate")]62 pub exec_params: uc_rpc::pov_estimate::ExecutorParams,63 64 #[cfg(feature = "pov-estimate")]65 pub backend: Arc<FullBackend>,66}6768pub fn overrides_handle<C, BE, R>(client: Arc<C>) -> Arc<OverrideHandle<Block>>69where70 C: ProvideRuntimeApi<Block> + StorageProvider<Block, BE> + AuxStore,71 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError>,72 C: Send + Sync + 'static,73 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,74 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,75 BE: Backend<Block> + 'static,76 BE::State: StateBackend<BlakeTwo256>,77 R: RuntimeInstance + Send + Sync + 'static,78{79 let mut overrides_map = BTreeMap::new();80 overrides_map.insert(81 EthereumStorageSchema::V1,82 Box::new(SchemaV1Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,83 );84 overrides_map.insert(85 EthereumStorageSchema::V2,86 Box::new(SchemaV2Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,87 );88 overrides_map.insert(89 EthereumStorageSchema::V3,90 Box::new(SchemaV3Override::new(client.clone())) as Box<dyn StorageOverride<_> + 'static>,91 );9293 Arc::new(OverrideHandle {94 schemas: overrides_map,95 fallback: Box::new(RuntimeApiStorageOverride::new(client)),96 })97}9899100pub fn create_full<C, P, SC, R, A, B>(101 io: &mut RpcModule<()>,102 deps: FullDeps<C, P, SC>,103) -> Result<(), Box<dyn std::error::Error + Send + Sync>>104where105 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,106 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,107 C: Send + Sync + 'static,108 C: BlockchainEvents<Block>,109 C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,110 C::Api: BlockBuilder<Block>,111 112 C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,113 C::Api: up_rpc::UniqueApi<Block, <R as RuntimeInstance>::CrossAccountId, AccountId>,114 C::Api: app_promotion_rpc::AppPromotionApi<115 Block,116 BlockNumber,117 <R as RuntimeInstance>::CrossAccountId,118 AccountId,119 >,120 C::Api: up_pov_estimate_rpc::PovEstimateApi<Block>,121 B: sc_client_api::Backend<Block> + Send + Sync + 'static,122 B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,123 P: TransactionPool<Block = Block> + 'static,124 R: RuntimeInstance + Send + Sync + 'static,125 <R as RuntimeInstance>::CrossAccountId: serde::Serialize,126 C: sp_api::CallApiAt<127 sp_runtime::generic::Block<128 sp_runtime::generic::Header<u32, BlakeTwo256>,129 sp_runtime::OpaqueExtrinsic,130 >,131 >,132 for<'de> <R as RuntimeInstance>::CrossAccountId: serde::Deserialize<'de>,133{134 use uc_rpc::{UniqueApiServer, Unique};135136 use uc_rpc::{AppPromotionApiServer, AppPromotion};137138 #[cfg(feature = "pov-estimate")]139 use uc_rpc::pov_estimate::{PovEstimateApiServer, PovEstimate};140141 142 use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};143 use substrate_frame_rpc_system::{System, SystemApiServer};144145 let FullDeps {146 client,147 pool,148 select_chain: _,149 deny_unsafe,150151 runtime_id: _,152153 #[cfg(feature = "pov-estimate")]154 exec_params,155156 #[cfg(feature = "pov-estimate")]157 backend,158 } = deps;159160 io.merge(System::new(Arc::clone(&client), Arc::clone(&pool), deny_unsafe).into_rpc())?;161 io.merge(TransactionPayment::new(Arc::clone(&client)).into_rpc())?;162163 io.merge(Unique::new(client.clone()).into_rpc())?;164165 io.merge(AppPromotion::new(client).into_rpc())?;166167 #[cfg(feature = "pov-estimate")]168 io.merge(169 PovEstimate::new(170 client.clone(),171 backend,172 deny_unsafe,173 exec_params,174 runtime_id,175 )176 .into_rpc(),177 )?;178179 Ok(())180}181182pub struct EthDeps<C, P, CA: ChainApi> {183 184 pub client: Arc<C>,185 186 pub pool: Arc<P>,187 188 pub graph: Arc<Pool<CA>>,189 190 pub sync: Arc<SyncingService<Block>>,191 192 pub is_authority: bool,193 194 pub network: Arc<NetworkService<Block, Hash>>,195196 197 pub eth_backend: Arc<dyn fc_db::BackendReader<Block> + Send + Sync>,198 199 pub max_past_logs: u32,200 201 pub fee_history_limit: u64,202 203 pub fee_history_cache: FeeHistoryCache,204 pub eth_block_data_cache: Arc<EthBlockDataCacheTask<Block>>,205 206 pub eth_filter_pool: Option<FilterPool>,207 pub eth_pubsub_notification_sinks:208 Arc<EthereumBlockNotificationSinks<EthereumBlockNotification<Block>>>,209 210 pub enable_dev_signer: bool,211212 pub overrides: Arc<OverrideHandle<Block>>,213}214215216217218219enum NeverConvert {}220impl<T> fp_rpc::ConvertTransaction<T> for NeverConvert {221 fn convert_transaction(&self, _transaction: pallet_ethereum::Transaction) -> T {222 unreachable!()223 }224}225226pub fn create_eth<C, P, CA, B>(227 io: &mut RpcModule<()>,228 deps: EthDeps<C, P, CA>,229 subscription_task_executor: SubscriptionTaskExecutor,230) -> Result<(), Box<dyn std::error::Error + Send + Sync>>231where232 C: ProvideRuntimeApi<Block> + StorageProvider<Block, B> + AuxStore,233 C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,234 C: Send + Sync + 'static,235 C: BlockchainEvents<Block>,236 C::Api: BlockBuilder<Block>,237 C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,238 C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,239 P: TransactionPool<Block = Block> + 'static,240 CA: ChainApi<Block = Block> + 'static,241 B: sc_client_api::Backend<Block> + Send + Sync + 'static,242 C: sp_api::CallApiAt<243 sp_runtime::generic::Block<244 sp_runtime::generic::Header<u32, BlakeTwo256>,245 sp_runtime::OpaqueExtrinsic,246 >,247 >,248{249 use fc_rpc::{250 Eth, EthApiServer, EthDevSigner, EthFilter, EthFilterApiServer, EthPubSub,251 EthPubSubApiServer, EthSigner, Net, NetApiServer, Web3, Web3ApiServer, TxPool,252 TxPoolApiServer,253 };254255 let EthDeps {256 client,257 pool,258 graph,259 eth_backend,260 max_past_logs,261 fee_history_limit,262 fee_history_cache,263 eth_block_data_cache,264 eth_filter_pool,265 eth_pubsub_notification_sinks,266 enable_dev_signer,267 sync,268 is_authority,269 network,270 overrides,271 } = deps;272273 let mut signers = Vec::new();274 if enable_dev_signer {275 signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);276 }277 let execute_gas_limit_multiplier = 10;278 io.merge(279 Eth::new(280 client.clone(),281 pool.clone(),282 graph.clone(),283 284 None::<NeverConvert>,285 sync.clone(),286 signers,287 overrides.clone(),288 eth_backend.clone(),289 is_authority,290 eth_block_data_cache.clone(),291 fee_history_cache,292 fee_history_limit,293 execute_gas_limit_multiplier,294 None,295 )296 .into_rpc(),297 )?;298299 let tx_pool = TxPool::new(client.clone(), graph);300301 if let Some(filter_pool) = eth_filter_pool {302 io.merge(303 EthFilter::new(304 client.clone(),305 eth_backend,306 tx_pool.clone(),307 filter_pool,308 500_usize, 309 max_past_logs,310 eth_block_data_cache,311 )312 .into_rpc(),313 )?;314 }315 io.merge(316 Net::new(317 client.clone(),318 network,319 320 true,321 )322 .into_rpc(),323 )?;324 io.merge(Web3::new(client.clone()).into_rpc())?;325 io.merge(326 EthPubSub::new(327 pool,328 client,329 sync,330 subscription_task_executor,331 overrides,332 eth_pubsub_notification_sinks,333 )334 .into_rpc(),335 )?;336 io.merge(tx_pool.into_rpc())?;337338 Ok(())339}