From a1d1b9d002832d16589ef05a149e0696878687df Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Mon, 13 May 2024 22:38:07 +0200 Subject: [PATCH 1/5] fix shiden genesis sync --- bin/collator/src/local/service.rs | 18 ++------------ bin/collator/src/parachain/service.rs | 18 ++------------ bin/collator/src/rpc.rs | 36 ++++++++++++--------------- 3 files changed, 20 insertions(+), 52 deletions(-) diff --git a/bin/collator/src/local/service.rs b/bin/collator/src/local/service.rs index 3421792c2d..ee53aea2b0 100644 --- a/bin/collator/src/local/service.rs +++ b/bin/collator/src/local/service.rs @@ -368,15 +368,10 @@ pub fn start_node( enable_evm_rpc: true, // enable EVM RPC for dev node by default }; - let pending_consensus_data_provider = Box::new( - fc_rpc::pending::AuraConsensusDataProvider::new(client.clone()), - ); - crate::rpc::create_full( deps, subscription, pubsub_notification_sinks.clone(), - pending_consensus_data_provider, rpc_config.clone(), ) .map_err::(Into::into) @@ -656,17 +651,8 @@ pub fn start_node(config: Configuration) -> Result { enable_evm_rpc: true, // enable EVM RPC for dev node by default }; - let pending_consensus_data_provider = Box::new( - fc_rpc::pending::AuraConsensusDataProvider::new(client.clone()), - ); - - crate::rpc::create_full( - deps, - subscription, - pubsub_notification_sinks.clone(), - pending_consensus_data_provider, - ) - .map_err::(Into::into) + crate::rpc::create_full(deps, subscription, pubsub_notification_sinks.clone()) + .map_err::(Into::into) }) }; diff --git a/bin/collator/src/parachain/service.rs b/bin/collator/src/parachain/service.rs index 8f5ea41ba3..9865e8e88d 100644 --- a/bin/collator/src/parachain/service.rs +++ b/bin/collator/src/parachain/service.rs @@ -494,17 +494,8 @@ where enable_evm_rpc: additional_config.enable_evm_rpc, }; - let pending_consensus_data_provider = Box::new( - fc_rpc::pending::AuraConsensusDataProvider::new(client.clone()), - ); - - crate::rpc::create_full( - deps, - subscription, - pubsub_notification_sinks.clone(), - pending_consensus_data_provider, - ) - .map_err(Into::into) + crate::rpc::create_full(deps, subscription, pubsub_notification_sinks.clone()) + .map_err(Into::into) }) }; @@ -845,15 +836,10 @@ where enable_evm_rpc: additional_config.enable_evm_rpc, }; - let pending_consensus_data_provider = Box::new( - fc_rpc::pending::AuraConsensusDataProvider::new(client.clone()), - ); - crate::rpc::create_full( deps, subscription, pubsub_notification_sinks.clone(), - pending_consensus_data_provider, rpc_config.clone(), ) .map_err(Into::into) diff --git a/bin/collator/src/rpc.rs b/bin/collator/src/rpc.rs index 5da4f28d35..9b7f196c6e 100644 --- a/bin/collator/src/rpc.rs +++ b/bin/collator/src/rpc.rs @@ -21,9 +21,8 @@ use cumulus_primitives_parachain_inherent::ParachainInherentData; use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; use fc_rpc::{ - pending::ConsensusDataProvider, Eth, EthApiServer, EthBlockDataCacheTask, EthFilter, - EthFilterApiServer, EthPubSub, EthPubSubApiServer, Net, NetApiServer, OverrideHandle, Web3, - Web3ApiServer, + Eth, EthApiServer, EthBlockDataCacheTask, EthFilter, EthFilterApiServer, EthPubSub, + EthPubSubApiServer, Net, NetApiServer, OverrideHandle, Web3, Web3ApiServer, }; use fc_rpc_core::types::{FeeHistoryCache, FilterPool}; use jsonrpsee::RpcModule; @@ -38,7 +37,7 @@ use sc_rpc::dev::DevApiServer; pub use sc_rpc::{DenyUnsafe, SubscriptionTaskExecutor}; use sc_transaction_pool::{ChainApi, Pool}; use sc_transaction_pool_api::TransactionPool; -use sp_api::{CallApiAt, ProvideRuntimeApi}; +use sp_api::{ApiExt, CallApiAt, ProvideRuntimeApi}; use sp_block_builder::BlockBuilder; use sp_blockchain::{ Backend as BlockchainBackend, Error as BlockChainError, HeaderBackend, HeaderMetadata, @@ -149,7 +148,6 @@ pub fn create_full( fc_mapping_sync::EthereumBlockNotification, >, >, - pending_consenus_data_provider: Box>, tracing_config: EvmTracingConfig, ) -> Result, Box> where @@ -182,12 +180,7 @@ where let client = Arc::clone(&deps.client); let graph = Arc::clone(&deps.graph); - let mut io = create_full_rpc( - deps, - subscription_task_executor, - pubsub_notification_sinks, - pending_consenus_data_provider, - )?; + let mut io = create_full_rpc(deps, subscription_task_executor, pubsub_notification_sinks)?; if tracing_config.enable_txpool { io.merge(MoonbeamTxPool::new(Arc::clone(&client), graph).into_rpc())?; @@ -221,7 +214,6 @@ pub fn create_full( fc_mapping_sync::EthereumBlockNotification, >, >, - pending_consenus_data_provider: Box>, ) -> Result, Box> where C: ProvideRuntimeApi @@ -248,12 +240,7 @@ where BE::Blockchain: BlockchainBackend, A: ChainApi + 'static, { - create_full_rpc( - deps, - subscription_task_executor, - pubsub_notification_sinks, - pending_consenus_data_provider, - ) + create_full_rpc(deps, subscription_task_executor, pubsub_notification_sinks) } fn create_full_rpc( @@ -264,7 +251,6 @@ fn create_full_rpc( fc_mapping_sync::EthereumBlockNotification, >, >, - pending_consenus_data_provider: Box>, ) -> Result, Box> where C: ProvideRuntimeApi @@ -319,6 +305,14 @@ where let no_tx_converter: Option = None; + if !client + .runtime_api() + .has_api::>(client.info().best_hash) + .unwrap_or_default() + { + return Err("EVM RPC cannot be enable at the current state. Please sync at least 200K blocks before enabling it.".into()); + } + let slot_duration = sc_consensus_aura::slot_duration(&*client)?; let pending_create_inherent_data_providers = move |_, _| async move { let current = sp_timestamp::InherentDataProvider::from_system_time(); @@ -368,7 +362,9 @@ where 10, None, pending_create_inherent_data_providers, - Some(pending_consenus_data_provider), + Some(Box::new(fc_rpc::pending::AuraConsensusDataProvider::new( + client.clone(), + ))), ) .replace_config::>() .into_rpc(), From 74986286ae6f79940f9e4a64fe496344ff82a42b Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Tue, 14 May 2024 14:48:24 +0200 Subject: [PATCH 2/5] proper solution for shiden eth rpc --- bin/collator/src/rpc.rs | 194 ++++++++++++++++++++++++++++++---------- 1 file changed, 147 insertions(+), 47 deletions(-) diff --git a/bin/collator/src/rpc.rs b/bin/collator/src/rpc.rs index 9b7f196c6e..d69f7a793c 100644 --- a/bin/collator/src/rpc.rs +++ b/bin/collator/src/rpc.rs @@ -18,11 +18,11 @@ //! Astar RPCs implementation. -use cumulus_primitives_parachain_inherent::ParachainInherentData; use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; use fc_rpc::{ - Eth, EthApiServer, EthBlockDataCacheTask, EthFilter, EthFilterApiServer, EthPubSub, - EthPubSubApiServer, Net, NetApiServer, OverrideHandle, Web3, Web3ApiServer, + pending::ConsensusDataProvider, Eth, EthApiServer, EthBlockDataCacheTask, EthFilter, + EthFilterApiServer, EthPubSub, EthPubSubApiServer, Net, NetApiServer, OverrideHandle, Web3, + Web3ApiServer, }; use fc_rpc_core::types::{FeeHistoryCache, FilterPool}; use jsonrpsee::RpcModule; @@ -42,9 +42,18 @@ use sp_block_builder::BlockBuilder; use sp_blockchain::{ Backend as BlockchainBackend, Error as BlockChainError, HeaderBackend, HeaderMetadata, }; -use sp_consensus_aura::{sr25519::AuthorityId as AuraId, AuraApi}; -use sp_runtime::traits::BlakeTwo256; -use std::sync::Arc; +use sp_consensus_aura::{ + digests::CompatibleDigestItem, + sr25519::{AuthorityId as AuraId, AuthoritySignature}, + AuraApi, +}; +use sp_inherents::{CreateInherentDataProviders, Error, InherentData}; +use sp_runtime::{ + traits::{BlakeTwo256, Block as BlockT, Header}, + Digest, DigestItem, +}; +use sp_timestamp::TimestampInherentData; +use std::{marker::PhantomData, sync::Arc}; use substrate_frame_rpc_system::{System, SystemApiServer}; #[cfg(feature = "evm-tracing")] @@ -305,45 +314,6 @@ where let no_tx_converter: Option = None; - if !client - .runtime_api() - .has_api::>(client.info().best_hash) - .unwrap_or_default() - { - return Err("EVM RPC cannot be enable at the current state. Please sync at least 200K blocks before enabling it.".into()); - } - - let slot_duration = sc_consensus_aura::slot_duration(&*client)?; - let pending_create_inherent_data_providers = move |_, _| async move { - let current = sp_timestamp::InherentDataProvider::from_system_time(); - let next_slot = current.timestamp().as_millis() + slot_duration.as_millis(); - let timestamp = sp_timestamp::InherentDataProvider::new(next_slot.into()); - let slot = - sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration( - *timestamp, - slot_duration, - ); - // Create a dummy parachain inherent data provider which is required to pass - // the checks by the para chain system. We use dummy values because in the 'pending context' - // neither do we have access to the real values nor do we need them. - let (relay_parent_storage_root, relay_chain_state) = - RelayStateSproofBuilder::default().into_state_root_and_proof(); - let vfp = PersistedValidationData { - // This is a hack to make `cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases` - // happy. Relay parent number can't be bigger than u32::MAX. - relay_parent_number: u32::MAX, - relay_parent_storage_root, - ..Default::default() - }; - let parachain_inherent_data = ParachainInherentData { - validation_data: vfp, - relay_chain_state, - downward_messages: Default::default(), - horizontal_messages: Default::default(), - }; - Ok((slot, timestamp, parachain_inherent_data)) - }; - io.merge( Eth::<_, _, _, _, _, _, _, ()>::new( client.clone(), @@ -361,8 +331,8 @@ where // Allow 10x max allowed weight for non-transactional calls 10, None, - pending_create_inherent_data_providers, - Some(Box::new(fc_rpc::pending::AuraConsensusDataProvider::new( + PendingCrateInherentDataProvider::new(client.clone()), + Some(Box::new(AuraConsensusDataProviderFallback::new( client.clone(), ))), ) @@ -403,3 +373,133 @@ where Ok(io) } + +struct AuraConsensusDataProviderFallback { + client: Arc, + phantom_data: PhantomData, +} + +impl AuraConsensusDataProviderFallback +where + B: BlockT, + C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, + C::Api: AuraApi, +{ + fn new(client: Arc) -> Self { + Self { + client, + phantom_data: Default::default(), + } + } +} + +impl ConsensusDataProvider for AuraConsensusDataProviderFallback +where + B: BlockT, + C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, + C::Api: AuraApi, +{ + fn create_digest(&self, parent: &B::Header, data: &InherentData) -> Result { + if self + .client + .runtime_api() + .has_api::>(parent.hash()) + .unwrap_or_default() + { + let slot_duration = sc_consensus_aura::slot_duration(&*self.client) + .expect("slot_duration should be present at this point; qed."); + let timestamp = data + .timestamp_inherent_data()? + .expect("Timestamp is always present; qed"); + + let digest_item = + >::aura_pre_digest( + sp_consensus_aura::Slot::from_timestamp(timestamp, slot_duration), + ); + + return Ok(Digest { + logs: vec![digest_item], + }); + } + Err(Error::Application("AuraApi is not present".into())) + } +} + +struct PendingCrateInherentDataProvider { + client: Arc, + phantom_data: PhantomData, +} + +impl PendingCrateInherentDataProvider +where + B: BlockT, + C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, + C::Api: AuraApi, +{ + fn new(client: Arc) -> Self { + Self { + client, + phantom_data: Default::default(), + } + } +} + +#[async_trait::async_trait] +impl CreateInherentDataProviders for PendingCrateInherentDataProvider +where + B: BlockT, + C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, + C::Api: AuraApi, +{ + type InherentDataProviders = ( + sp_consensus_aura::inherents::InherentDataProvider, + sp_timestamp::InherentDataProvider, + cumulus_primitives_parachain_inherent::ParachainInherentData, + ); + + async fn create_inherent_data_providers( + &self, + parent: B::Hash, + _extra_args: (), + ) -> Result> { + if !self + .client + .runtime_api() + .has_api::>(parent) + .unwrap_or_default() + { + return Err("AuraApi is not present".into()); + } + + let slot_duration = sc_consensus_aura::slot_duration(&*self.client) + .expect("slot_duration should be present at this point; qed."); + let current = sp_timestamp::InherentDataProvider::from_system_time(); + let next_slot = current.timestamp().as_millis() + slot_duration.as_millis(); + let timestamp = sp_timestamp::InherentDataProvider::new(next_slot.into()); + let slot = + sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration( + *timestamp, + slot_duration, + ); + // Create a dummy parachain inherent data provider which is required to pass + // the checks by the para chain system. We use dummy values because in the 'pending context' + // neither do we have access to the real values nor do we need them. + let (relay_parent_storage_root, relay_chain_state) = + RelayStateSproofBuilder::default().into_state_root_and_proof(); + let vfp = PersistedValidationData { + // This is a hack to make `cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases` + // happy. Relay parent number can't be bigger than u32::MAX. + relay_parent_number: u32::MAX, + relay_parent_storage_root, + ..Default::default() + }; + let parachain_inherent_data = + cumulus_primitives_parachain_inherent::ParachainInherentData { + validation_data: vfp, + relay_chain_state, + downward_messages: Default::default(), + horizontal_messages: Default::default(), + }; + Ok((slot, timestamp, parachain_inherent_data)) + } +} From d88f8de9b4ffde0069e55f765ecbed6dee97bfb3 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Wed, 15 May 2024 17:14:17 +0200 Subject: [PATCH 3/5] relocate implementation --- bin/collator/src/parachain/mod.rs | 2 + bin/collator/src/parachain/shell_upgrade.rs | 150 ++++++++++- bin/collator/src/rpc.rs | 282 +++----------------- 3 files changed, 180 insertions(+), 254 deletions(-) diff --git a/bin/collator/src/parachain/mod.rs b/bin/collator/src/parachain/mod.rs index e988e01dba..b5c34c8a84 100644 --- a/bin/collator/src/parachain/mod.rs +++ b/bin/collator/src/parachain/mod.rs @@ -31,3 +31,5 @@ pub use service::{ astar, build_import_queue, new_partial, shibuya, shiden, start_astar_node, start_shibuya_node, start_shiden_node, HostFunctions, }; + +pub(crate) use shell_upgrade::{AuraConsensusDataProviderFallback, PendingCrateInherentDataProvider}; diff --git a/bin/collator/src/parachain/shell_upgrade.rs b/bin/collator/src/parachain/shell_upgrade.rs index ce63e84db9..d420ce9a55 100644 --- a/bin/collator/src/parachain/shell_upgrade.rs +++ b/bin/collator/src/parachain/shell_upgrade.rs @@ -21,12 +21,21 @@ use astar_primitives::*; use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus}; use cumulus_primitives_core::relay_chain::{Hash as PHash, PersistedValidationData}; +use cumulus_primitives_core::BlockT; +use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; +use fc_rpc::pending::ConsensusDataProvider; use futures::lock::Mutex; +use sc_client_api::{AuxStore, UsageProvider}; use sc_consensus::{import_queue::Verifier as VerifierT, BlockImportParams, ForkChoiceStrategy}; -use sp_api::ApiExt; +use sp_api::{ApiExt, ProvideRuntimeApi}; +use sp_consensus_aura::digests::CompatibleDigestItem; +use sp_consensus_aura::sr25519::AuthoritySignature; use sp_consensus_aura::{sr25519::AuthorityId as AuraId, AuraApi}; +use sp_inherents::{CreateInherentDataProviders, Error, InherentData}; use sp_runtime::traits::Header as HeaderT; -use std::sync::Arc; +use sp_runtime::{Digest, DigestItem}; +use sp_timestamp::TimestampInherentData; +use std::{marker::PhantomData, sync::Arc}; pub enum BuildOnAccess { Uninitialized(Option R + Send + Sync>>), @@ -138,3 +147,140 @@ where } } } + +/// AuraConsensusDataProvider custom implementation which awaits for AuraApi to become available, +/// until then it will return error. Shiden genesis did not start with AuraApi, therefore this +/// implementation makes sure to return digest after AuraApi becomes available. +/// This is currently required by EVM RPC. +pub struct AuraConsensusDataProviderFallback { + client: Arc, + phantom_data: PhantomData, +} + +impl AuraConsensusDataProviderFallback +where + B: BlockT, + C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, + C::Api: AuraApi, +{ + pub fn new(client: Arc) -> Self { + Self { + client, + phantom_data: Default::default(), + } + } +} + +impl ConsensusDataProvider for AuraConsensusDataProviderFallback +where + B: BlockT, + C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, + C::Api: AuraApi, +{ + fn create_digest(&self, parent: &B::Header, data: &InherentData) -> Result { + if self + .client + .runtime_api() + .has_api::>(parent.hash()) + .unwrap_or_default() + { + let slot_duration = sc_consensus_aura::slot_duration(&*self.client) + .expect("slot_duration should be present at this point; qed."); + let timestamp = data + .timestamp_inherent_data()? + .expect("Timestamp is always present; qed"); + + let digest_item = + >::aura_pre_digest( + sp_consensus_aura::Slot::from_timestamp(timestamp, slot_duration), + ); + + return Ok(Digest { + logs: vec![digest_item], + }); + } + Err(Error::Application("AuraApi is not present".into())) + } +} + +/// Shiden genesis did not start with AuraApi, therefore this implementation makes sure to return +/// inherent data after AuraApi becomes available. +/// This is currently required by EVM RPC. +pub struct PendingCrateInherentDataProvider { + client: Arc, + phantom_data: PhantomData, +} + +impl PendingCrateInherentDataProvider +where + B: BlockT, + C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, + C::Api: AuraApi, +{ + pub fn new(client: Arc) -> Self { + Self { + client, + phantom_data: Default::default(), + } + } +} + +#[async_trait::async_trait] +impl CreateInherentDataProviders for PendingCrateInherentDataProvider +where + B: BlockT, + C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, + C::Api: AuraApi, +{ + type InherentDataProviders = ( + sp_consensus_aura::inherents::InherentDataProvider, + sp_timestamp::InherentDataProvider, + cumulus_primitives_parachain_inherent::ParachainInherentData, + ); + + async fn create_inherent_data_providers( + &self, + parent: B::Hash, + _extra_args: (), + ) -> Result> { + if !self + .client + .runtime_api() + .has_api::>(parent) + .unwrap_or_default() + { + return Err("AuraApi is not present".into()); + } + + let slot_duration = sc_consensus_aura::slot_duration(&*self.client) + .expect("slot_duration should be present at this point; qed."); + let current = sp_timestamp::InherentDataProvider::from_system_time(); + let next_slot = current.timestamp().as_millis() + slot_duration.as_millis(); + let timestamp = sp_timestamp::InherentDataProvider::new(next_slot.into()); + let slot = + sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration( + *timestamp, + slot_duration, + ); + // Create a dummy parachain inherent data provider which is required to pass + // the checks by the para chain system. We use dummy values because in the 'pending context' + // neither do we have access to the real values nor do we need them. + let (relay_parent_storage_root, relay_chain_state) = + RelayStateSproofBuilder::default().into_state_root_and_proof(); + let vfp = PersistedValidationData { + // This is a hack to make `cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases` + // happy. Relay parent number can't be bigger than u32::MAX. + relay_parent_number: u32::MAX, + relay_parent_storage_root, + ..Default::default() + }; + let parachain_inherent_data = + cumulus_primitives_parachain_inherent::ParachainInherentData { + validation_data: vfp, + relay_chain_state, + downward_messages: Default::default(), + horizontal_messages: Default::default(), + }; + Ok((slot, timestamp, parachain_inherent_data)) + } +} diff --git a/bin/collator/src/rpc.rs b/bin/collator/src/rpc.rs index d69f7a793c..59e2db32f4 100644 --- a/bin/collator/src/rpc.rs +++ b/bin/collator/src/rpc.rs @@ -18,16 +18,14 @@ //! Astar RPCs implementation. -use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; use fc_rpc::{ - pending::ConsensusDataProvider, Eth, EthApiServer, EthBlockDataCacheTask, EthFilter, - EthFilterApiServer, EthPubSub, EthPubSubApiServer, Net, NetApiServer, OverrideHandle, Web3, - Web3ApiServer, + Eth, EthApiServer, EthBlockDataCacheTask, EthFilter, EthFilterApiServer, EthPubSub, + EthPubSubApiServer, Net, NetApiServer, OverrideHandle, Web3, Web3ApiServer, }; use fc_rpc_core::types::{FeeHistoryCache, FilterPool}; use jsonrpsee::RpcModule; use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer}; -use polkadot_primitives::PersistedValidationData; + use sc_client_api::{ AuxStore, Backend, BlockchainEvents, StateBackend, StorageProvider, UsageProvider, }; @@ -37,23 +35,14 @@ use sc_rpc::dev::DevApiServer; pub use sc_rpc::{DenyUnsafe, SubscriptionTaskExecutor}; use sc_transaction_pool::{ChainApi, Pool}; use sc_transaction_pool_api::TransactionPool; -use sp_api::{ApiExt, CallApiAt, ProvideRuntimeApi}; +use sp_api::{CallApiAt, ProvideRuntimeApi}; use sp_block_builder::BlockBuilder; use sp_blockchain::{ Backend as BlockchainBackend, Error as BlockChainError, HeaderBackend, HeaderMetadata, }; -use sp_consensus_aura::{ - digests::CompatibleDigestItem, - sr25519::{AuthorityId as AuraId, AuthoritySignature}, - AuraApi, -}; -use sp_inherents::{CreateInherentDataProviders, Error, InherentData}; -use sp_runtime::{ - traits::{BlakeTwo256, Block as BlockT, Header}, - Digest, DigestItem, -}; -use sp_timestamp::TimestampInherentData; -use std::{marker::PhantomData, sync::Arc}; +use sp_consensus_aura::{sr25519::AuthorityId as AuraId, AuraApi}; +use sp_runtime::traits::BlakeTwo256; +use std::sync::Arc; use substrate_frame_rpc_system::{System, SystemApiServer}; #[cfg(feature = "evm-tracing")] @@ -84,7 +73,7 @@ pub fn open_frontier_backend( config: &sc_service::Configuration, ) -> Result>, String> where - C: sp_blockchain::HeaderBackend, + C: HeaderBackend, { let config_dir = config.base_path.config_dir(config.chain_spec.id()); let path = config_dir.join("frontier").join("db"); @@ -104,7 +93,7 @@ pub struct AstarEthConfig(std::marker::PhantomData<(C, BE)>); impl fc_rpc::EthConfig for AstarEthConfig where - C: sc_client_api::StorageProvider + Sync + Send + 'static, + C: StorageProvider + Sync + Send + 'static, BE: Backend + 'static, { // Use to override (adapt) evm call to precompiles for proper gas estimation. @@ -147,74 +136,7 @@ pub struct FullDeps { pub enable_evm_rpc: bool, } -/// Instantiate all RPC extensions and Tracing RPC. -#[cfg(feature = "evm-tracing")] -pub fn create_full( - deps: FullDeps, - subscription_task_executor: SubscriptionTaskExecutor, - pubsub_notification_sinks: Arc< - fc_mapping_sync::EthereumBlockNotificationSinks< - fc_mapping_sync::EthereumBlockNotification, - >, - >, - tracing_config: EvmTracingConfig, -) -> Result, Box> -where - C: ProvideRuntimeApi - + HeaderBackend - + UsageProvider - + CallApiAt - + AuxStore - + StorageProvider - + HeaderMetadata - + BlockchainEvents - + Send - + Sync - + 'static, - C: sc_client_api::BlockBackend, - C::Api: substrate_frame_rpc_system::AccountNonceApi - + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi - + fp_rpc::ConvertTransactionRuntimeApi - + fp_rpc::EthereumRuntimeRPCApi - + BlockBuilder - + AuraApi - + moonbeam_rpc_primitives_debug::DebugRuntimeApi - + moonbeam_rpc_primitives_txpool::TxPoolRuntimeApi, - P: TransactionPool + Sync + Send + 'static, - BE: Backend + 'static, - BE::State: StateBackend, - BE::Blockchain: BlockchainBackend, - A: ChainApi + 'static, -{ - let client = Arc::clone(&deps.client); - let graph = Arc::clone(&deps.graph); - - let mut io = create_full_rpc(deps, subscription_task_executor, pubsub_notification_sinks)?; - - if tracing_config.enable_txpool { - io.merge(MoonbeamTxPool::new(Arc::clone(&client), graph).into_rpc())?; - } - - if let Some(trace_filter_requester) = tracing_config.tracing_requesters.trace { - io.merge( - Trace::new( - client, - trace_filter_requester, - tracing_config.trace_filter_max_count, - ) - .into_rpc(), - )?; - } - - if let Some(debug_requester) = tracing_config.tracing_requesters.debug { - io.merge(Debug::new(debug_requester).into_rpc())?; - } - - Ok(io) -} - /// Instantiate all RPC extensions. -#[cfg(not(feature = "evm-tracing"))] pub fn create_full( deps: FullDeps, subscription_task_executor: SubscriptionTaskExecutor, @@ -223,6 +145,7 @@ pub fn create_full( fc_mapping_sync::EthereumBlockNotification, >, >, + #[cfg(feature = "evm-tracing")] tracing_config: EvmTracingConfig, ) -> Result, Box> where C: ProvideRuntimeApi @@ -248,43 +171,6 @@ where BE::State: StateBackend, BE::Blockchain: BlockchainBackend, A: ChainApi + 'static, -{ - create_full_rpc(deps, subscription_task_executor, pubsub_notification_sinks) -} - -fn create_full_rpc( - deps: FullDeps, - subscription_task_executor: SubscriptionTaskExecutor, - pubsub_notification_sinks: Arc< - fc_mapping_sync::EthereumBlockNotificationSinks< - fc_mapping_sync::EthereumBlockNotification, - >, - >, -) -> Result, Box> -where - C: ProvideRuntimeApi - + UsageProvider - + HeaderBackend - + CallApiAt - + AuxStore - + StorageProvider - + HeaderMetadata - + BlockchainEvents - + Send - + Sync - + 'static, - C: sc_client_api::BlockBackend, - C::Api: substrate_frame_rpc_system::AccountNonceApi - + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi - + fp_rpc::ConvertTransactionRuntimeApi - + fp_rpc::EthereumRuntimeRPCApi - + BlockBuilder - + AuraApi, - P: TransactionPool + Sync + Send + 'static, - BE: Backend + 'static, - BE::State: StateBackend, - BE::Blockchain: BlockchainBackend, - A: ChainApi + 'static, { let mut io = RpcModule::new(()); let FullDeps { @@ -331,10 +217,10 @@ where // Allow 10x max allowed weight for non-transactional calls 10, None, - PendingCrateInherentDataProvider::new(client.clone()), - Some(Box::new(AuraConsensusDataProviderFallback::new( - client.clone(), - ))), + crate::parachain::PendingCrateInherentDataProvider::new(client.clone()), + Some(Box::new( + crate::parachain::AuraConsensusDataProviderFallback::new(client.clone()), + )), ) .replace_config::>() .into_rpc(), @@ -371,135 +257,27 @@ where .into_rpc(), )?; - Ok(io) -} - -struct AuraConsensusDataProviderFallback { - client: Arc, - phantom_data: PhantomData, -} - -impl AuraConsensusDataProviderFallback -where - B: BlockT, - C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, - C::Api: AuraApi, -{ - fn new(client: Arc) -> Self { - Self { - client, - phantom_data: Default::default(), + #[cfg(feature = "evm-tracing")] + { + if tracing_config.enable_txpool { + io.merge(MoonbeamTxPool::new(client.clone(), graph.clone()).into_rpc())?; } - } -} -impl ConsensusDataProvider for AuraConsensusDataProviderFallback -where - B: BlockT, - C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, - C::Api: AuraApi, -{ - fn create_digest(&self, parent: &B::Header, data: &InherentData) -> Result { - if self - .client - .runtime_api() - .has_api::>(parent.hash()) - .unwrap_or_default() - { - let slot_duration = sc_consensus_aura::slot_duration(&*self.client) - .expect("slot_duration should be present at this point; qed."); - let timestamp = data - .timestamp_inherent_data()? - .expect("Timestamp is always present; qed"); - - let digest_item = - >::aura_pre_digest( - sp_consensus_aura::Slot::from_timestamp(timestamp, slot_duration), - ); - - return Ok(Digest { - logs: vec![digest_item], - }); + if let Some(trace_filter_requester) = tracing_config.tracing_requesters.trace { + io.merge( + Trace::new( + client, + trace_filter_requester, + tracing_config.trace_filter_max_count, + ) + .into_rpc(), + )?; } - Err(Error::Application("AuraApi is not present".into())) - } -} - -struct PendingCrateInherentDataProvider { - client: Arc, - phantom_data: PhantomData, -} -impl PendingCrateInherentDataProvider -where - B: BlockT, - C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, - C::Api: AuraApi, -{ - fn new(client: Arc) -> Self { - Self { - client, - phantom_data: Default::default(), + if let Some(debug_requester) = tracing_config.tracing_requesters.debug { + io.merge(Debug::new(debug_requester).into_rpc())?; } } -} -#[async_trait::async_trait] -impl CreateInherentDataProviders for PendingCrateInherentDataProvider -where - B: BlockT, - C: AuxStore + ProvideRuntimeApi + UsageProvider + Send + Sync, - C::Api: AuraApi, -{ - type InherentDataProviders = ( - sp_consensus_aura::inherents::InherentDataProvider, - sp_timestamp::InherentDataProvider, - cumulus_primitives_parachain_inherent::ParachainInherentData, - ); - - async fn create_inherent_data_providers( - &self, - parent: B::Hash, - _extra_args: (), - ) -> Result> { - if !self - .client - .runtime_api() - .has_api::>(parent) - .unwrap_or_default() - { - return Err("AuraApi is not present".into()); - } - - let slot_duration = sc_consensus_aura::slot_duration(&*self.client) - .expect("slot_duration should be present at this point; qed."); - let current = sp_timestamp::InherentDataProvider::from_system_time(); - let next_slot = current.timestamp().as_millis() + slot_duration.as_millis(); - let timestamp = sp_timestamp::InherentDataProvider::new(next_slot.into()); - let slot = - sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration( - *timestamp, - slot_duration, - ); - // Create a dummy parachain inherent data provider which is required to pass - // the checks by the para chain system. We use dummy values because in the 'pending context' - // neither do we have access to the real values nor do we need them. - let (relay_parent_storage_root, relay_chain_state) = - RelayStateSproofBuilder::default().into_state_root_and_proof(); - let vfp = PersistedValidationData { - // This is a hack to make `cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases` - // happy. Relay parent number can't be bigger than u32::MAX. - relay_parent_number: u32::MAX, - relay_parent_storage_root, - ..Default::default() - }; - let parachain_inherent_data = - cumulus_primitives_parachain_inherent::ParachainInherentData { - validation_data: vfp, - relay_chain_state, - downward_messages: Default::default(), - horizontal_messages: Default::default(), - }; - Ok((slot, timestamp, parachain_inherent_data)) - } + Ok(io) } From 1431f0f2320dd0807ad23462ce380d3d2ffbf996 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Wed, 15 May 2024 19:21:24 +0200 Subject: [PATCH 4/5] revert duplicate --- bin/collator/src/parachain/mod.rs | 4 +- bin/collator/src/rpc.rs | 131 ++++++++++++++++++++++++------ 2 files changed, 109 insertions(+), 26 deletions(-) diff --git a/bin/collator/src/parachain/mod.rs b/bin/collator/src/parachain/mod.rs index b5c34c8a84..077a99f1de 100644 --- a/bin/collator/src/parachain/mod.rs +++ b/bin/collator/src/parachain/mod.rs @@ -32,4 +32,6 @@ pub use service::{ start_shiden_node, HostFunctions, }; -pub(crate) use shell_upgrade::{AuraConsensusDataProviderFallback, PendingCrateInherentDataProvider}; +pub(crate) use shell_upgrade::{ + AuraConsensusDataProviderFallback, PendingCrateInherentDataProvider, +}; diff --git a/bin/collator/src/rpc.rs b/bin/collator/src/rpc.rs index 59e2db32f4..b3c143c99d 100644 --- a/bin/collator/src/rpc.rs +++ b/bin/collator/src/rpc.rs @@ -73,7 +73,7 @@ pub fn open_frontier_backend( config: &sc_service::Configuration, ) -> Result>, String> where - C: HeaderBackend, + C: sp_blockchain::HeaderBackend, { let config_dir = config.base_path.config_dir(config.chain_spec.id()); let path = config_dir.join("frontier").join("db"); @@ -93,7 +93,7 @@ pub struct AstarEthConfig(std::marker::PhantomData<(C, BE)>); impl fc_rpc::EthConfig for AstarEthConfig where - C: StorageProvider + Sync + Send + 'static, + C: sc_client_api::StorageProvider + Sync + Send + 'static, BE: Backend + 'static, { // Use to override (adapt) evm call to precompiles for proper gas estimation. @@ -136,7 +136,74 @@ pub struct FullDeps { pub enable_evm_rpc: bool, } +/// Instantiate all RPC extensions and Tracing RPC. +#[cfg(feature = "evm-tracing")] +pub fn create_full( + deps: FullDeps, + subscription_task_executor: SubscriptionTaskExecutor, + pubsub_notification_sinks: Arc< + fc_mapping_sync::EthereumBlockNotificationSinks< + fc_mapping_sync::EthereumBlockNotification, + >, + >, + tracing_config: EvmTracingConfig, +) -> Result, Box> +where + C: ProvideRuntimeApi + + HeaderBackend + + UsageProvider + + CallApiAt + + AuxStore + + StorageProvider + + HeaderMetadata + + BlockchainEvents + + Send + + Sync + + 'static, + C: sc_client_api::BlockBackend, + C::Api: substrate_frame_rpc_system::AccountNonceApi + + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi + + fp_rpc::ConvertTransactionRuntimeApi + + fp_rpc::EthereumRuntimeRPCApi + + BlockBuilder + + AuraApi + + moonbeam_rpc_primitives_debug::DebugRuntimeApi + + moonbeam_rpc_primitives_txpool::TxPoolRuntimeApi, + P: TransactionPool + Sync + Send + 'static, + BE: Backend + 'static, + BE::State: StateBackend, + BE::Blockchain: BlockchainBackend, + A: ChainApi + 'static, +{ + let client = Arc::clone(&deps.client); + let graph = Arc::clone(&deps.graph); + + let mut io = create_full_rpc(deps, subscription_task_executor, pubsub_notification_sinks)?; + + if tracing_config.enable_txpool { + io.merge(MoonbeamTxPool::new(Arc::clone(&client), graph).into_rpc())?; + } + + if let Some(trace_filter_requester) = tracing_config.tracing_requesters.trace { + io.merge( + Trace::new( + client, + trace_filter_requester, + tracing_config.trace_filter_max_count, + ) + .into_rpc(), + )?; + } + + if let Some(debug_requester) = tracing_config.tracing_requesters.debug { + io.merge(Debug::new(debug_requester).into_rpc())?; + } + + Ok(io) +} + /// Instantiate all RPC extensions. +#[cfg(not(feature = "evm-tracing"))] pub fn create_full( deps: FullDeps, subscription_task_executor: SubscriptionTaskExecutor, @@ -145,7 +212,6 @@ pub fn create_full( fc_mapping_sync::EthereumBlockNotification, >, >, - #[cfg(feature = "evm-tracing")] tracing_config: EvmTracingConfig, ) -> Result, Box> where C: ProvideRuntimeApi @@ -171,6 +237,43 @@ where BE::State: StateBackend, BE::Blockchain: BlockchainBackend, A: ChainApi + 'static, +{ + create_full_rpc(deps, subscription_task_executor, pubsub_notification_sinks) +} + +fn create_full_rpc( + deps: FullDeps, + subscription_task_executor: SubscriptionTaskExecutor, + pubsub_notification_sinks: Arc< + fc_mapping_sync::EthereumBlockNotificationSinks< + fc_mapping_sync::EthereumBlockNotification, + >, + >, +) -> Result, Box> +where + C: ProvideRuntimeApi + + UsageProvider + + HeaderBackend + + CallApiAt + + AuxStore + + StorageProvider + + HeaderMetadata + + BlockchainEvents + + Send + + Sync + + 'static, + C: sc_client_api::BlockBackend, + C::Api: substrate_frame_rpc_system::AccountNonceApi + + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi + + fp_rpc::ConvertTransactionRuntimeApi + + fp_rpc::EthereumRuntimeRPCApi + + BlockBuilder + + AuraApi, + P: TransactionPool + Sync + Send + 'static, + BE: Backend + 'static, + BE::State: StateBackend, + BE::Blockchain: BlockchainBackend, + A: ChainApi + 'static, { let mut io = RpcModule::new(()); let FullDeps { @@ -257,27 +360,5 @@ where .into_rpc(), )?; - #[cfg(feature = "evm-tracing")] - { - if tracing_config.enable_txpool { - io.merge(MoonbeamTxPool::new(client.clone(), graph.clone()).into_rpc())?; - } - - if let Some(trace_filter_requester) = tracing_config.tracing_requesters.trace { - io.merge( - Trace::new( - client, - trace_filter_requester, - tracing_config.trace_filter_max_count, - ) - .into_rpc(), - )?; - } - - if let Some(debug_requester) = tracing_config.tracing_requesters.debug { - io.merge(Debug::new(debug_requester).into_rpc())?; - } - } - Ok(io) } From 41579e132c99a792c7c69facfc0c1778f83187d5 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Thu, 16 May 2024 10:01:39 +0200 Subject: [PATCH 5/5] group imports --- bin/collator/src/parachain/shell_upgrade.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/bin/collator/src/parachain/shell_upgrade.rs b/bin/collator/src/parachain/shell_upgrade.rs index d420ce9a55..510565bade 100644 --- a/bin/collator/src/parachain/shell_upgrade.rs +++ b/bin/collator/src/parachain/shell_upgrade.rs @@ -21,19 +21,22 @@ use astar_primitives::*; use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus}; use cumulus_primitives_core::relay_chain::{Hash as PHash, PersistedValidationData}; -use cumulus_primitives_core::BlockT; use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; use fc_rpc::pending::ConsensusDataProvider; use futures::lock::Mutex; use sc_client_api::{AuxStore, UsageProvider}; use sc_consensus::{import_queue::Verifier as VerifierT, BlockImportParams, ForkChoiceStrategy}; use sp_api::{ApiExt, ProvideRuntimeApi}; -use sp_consensus_aura::digests::CompatibleDigestItem; -use sp_consensus_aura::sr25519::AuthoritySignature; -use sp_consensus_aura::{sr25519::AuthorityId as AuraId, AuraApi}; +use sp_consensus_aura::{ + digests::CompatibleDigestItem, + sr25519::{AuthorityId as AuraId, AuthoritySignature}, + AuraApi, +}; use sp_inherents::{CreateInherentDataProviders, Error, InherentData}; -use sp_runtime::traits::Header as HeaderT; -use sp_runtime::{Digest, DigestItem}; +use sp_runtime::{ + traits::{Block as BlockT, Header as HeaderT}, + Digest, DigestItem, +}; use sp_timestamp::TimestampInherentData; use std::{marker::PhantomData, sync::Arc};