From 9c183893ae545906791c987e626f2f319726b46a Mon Sep 17 00:00:00 2001 From: Web3 Philosopher Date: Tue, 4 Jul 2023 16:27:31 +0200 Subject: [PATCH] introduce a method for fetching relay chain header to `RelayChainInterface` (#2794) * introduce a method for fetching header to RelayChainInterface * cargo fmt * return Option
--- cumulus/client/consensus/common/src/tests.rs | 6 +++++- cumulus/client/network/src/tests.rs | 13 +++++++++++++ .../src/lib.rs | 19 +++++++++++++++++-- .../client/relay-chain-interface/src/lib.rs | 8 ++++++++ .../relay-chain-rpc-interface/src/lib.rs | 18 +++++++++++++++++- 5 files changed, 60 insertions(+), 4 deletions(-) diff --git a/cumulus/client/consensus/common/src/tests.rs b/cumulus/client/consensus/common/src/tests.rs index f1bc4d42b8..ffb9aaee77 100644 --- a/cumulus/client/consensus/common/src/tests.rs +++ b/cumulus/client/consensus/common/src/tests.rs @@ -19,7 +19,7 @@ use crate::*; use async_trait::async_trait; use codec::Encode; use cumulus_client_pov_recovery::RecoveryKind; -use cumulus_primitives_core::{InboundDownwardMessage, InboundHrmpMessage}; +use cumulus_primitives_core::{relay_chain::BlockId, InboundDownwardMessage, InboundHrmpMessage}; use cumulus_relay_chain_interface::{ CommittedCandidateReceipt, OccupiedCoreAssumption, OverseerHandle, PHeader, ParaId, RelayChainInterface, RelayChainResult, SessionIndex, StorageValue, ValidatorId, @@ -207,6 +207,10 @@ impl RelayChainInterface for Relaychain { }) .boxed()) } + + async fn header(&self, _block_id: BlockId) -> RelayChainResult> { + unimplemented!("Not needed for test") + } } fn build_block( diff --git a/cumulus/client/network/src/tests.rs b/cumulus/client/network/src/tests.rs index 08127fe390..ce2246f04c 100644 --- a/cumulus/client/network/src/tests.rs +++ b/cumulus/client/network/src/tests.rs @@ -16,6 +16,7 @@ use super::*; use async_trait::async_trait; +use cumulus_primitives_core::relay_chain::BlockId; use cumulus_relay_chain_inprocess_interface::{check_block_in_chain, BlockCheckStatus}; use cumulus_relay_chain_interface::{ OverseerHandle, PHeader, ParaId, RelayChainError, RelayChainResult, @@ -237,6 +238,18 @@ impl RelayChainInterface for DummyRelayChainInterface { }); Ok(Box::pin(notifications_stream)) } + + async fn header(&self, block_id: BlockId) -> RelayChainResult> { + let hash = match block_id { + BlockId::Hash(hash) => hash, + BlockId::Number(num) => self.relay_client.hash(num)?.ok_or_else(|| { + RelayChainError::GenericError(format!("block with number {num} not found")) + })?, + }; + let header = self.relay_client.header(hash)?; + + Ok(header) + } } fn make_validator_and_api( diff --git a/cumulus/client/relay-chain-inprocess-interface/src/lib.rs b/cumulus/client/relay-chain-inprocess-interface/src/lib.rs index c1e19bd20b..8788c25cf2 100644 --- a/cumulus/client/relay-chain-inprocess-interface/src/lib.rs +++ b/cumulus/client/relay-chain-inprocess-interface/src/lib.rs @@ -19,8 +19,9 @@ use std::{pin::Pin, sync::Arc, time::Duration}; use async_trait::async_trait; use cumulus_primitives_core::{ relay_chain::{ - runtime_api::ParachainHost, Block as PBlock, CommittedCandidateReceipt, Hash as PHash, - Header as PHeader, InboundHrmpMessage, OccupiedCoreAssumption, SessionIndex, ValidatorId, + runtime_api::ParachainHost, Block as PBlock, BlockId, CommittedCandidateReceipt, + Hash as PHash, Header as PHeader, InboundHrmpMessage, OccupiedCoreAssumption, SessionIndex, + ValidatorId, }, InboundDownwardMessage, ParaId, PersistedValidationData, }; @@ -79,6 +80,7 @@ impl Clone for RelayChainInProcessInterface { impl RelayChainInterface for RelayChainInProcessInterface where Client: ProvideRuntimeApi + + HeaderBackend + BlockchainEvents + AuxStore + UsageProvider @@ -110,6 +112,18 @@ where )?) } + async fn header(&self, block_id: BlockId) -> RelayChainResult> { + let hash = match block_id { + BlockId::Hash(hash) => hash, + BlockId::Number(num) => self.full_client.hash(num)?.ok_or_else(|| { + RelayChainError::GenericError(format!("block with number {num} not found")) + })?, + }; + let header = self.full_client.header(hash)?; + + Ok(header) + } + async fn persisted_validation_data( &self, hash: PHash, @@ -299,6 +313,7 @@ impl ExecuteWithClient for RelayChainInProcessInterfaceBuilder { fn execute_with_client(self, client: Arc) -> Self::Output where Client: ProvideRuntimeApi + + HeaderBackend + BlockchainEvents + AuxStore + UsageProvider diff --git a/cumulus/client/relay-chain-interface/src/lib.rs b/cumulus/client/relay-chain-interface/src/lib.rs index 3629aea84c..9f7156f90e 100644 --- a/cumulus/client/relay-chain-interface/src/lib.rs +++ b/cumulus/client/relay-chain-interface/src/lib.rs @@ -26,6 +26,7 @@ use jsonrpsee_core::Error as JsonRpcError; use parity_scale_codec::Error as CodecError; use sp_api::ApiError; +use cumulus_primitives_core::relay_chain::BlockId; pub use cumulus_primitives_core::{ relay_chain::{ CommittedCandidateReceipt, Hash as PHash, Header as PHeader, InboundHrmpMessage, @@ -110,6 +111,9 @@ pub trait RelayChainInterface: Send + Sync { /// Get the hash of the current best block. async fn best_block_hash(&self) -> RelayChainResult; + /// Fetch the block header of a given height + async fn header(&self, block_id: BlockId) -> RelayChainResult>; + /// Get the hash of the finalized block. async fn finalized_block_hash(&self) -> RelayChainResult; @@ -293,4 +297,8 @@ where ) -> RelayChainResult + Send>>> { (**self).new_best_notification_stream().await } + + async fn header(&self, block_id: BlockId) -> RelayChainResult> { + (**self).header(block_id).await + } } diff --git a/cumulus/client/relay-chain-rpc-interface/src/lib.rs b/cumulus/client/relay-chain-rpc-interface/src/lib.rs index 475d5d905b..3c6f3ca323 100644 --- a/cumulus/client/relay-chain-rpc-interface/src/lib.rs +++ b/cumulus/client/relay-chain-rpc-interface/src/lib.rs @@ -23,7 +23,9 @@ use cumulus_primitives_core::{ }, InboundDownwardMessage, ParaId, PersistedValidationData, }; -use cumulus_relay_chain_interface::{RelayChainError, RelayChainInterface, RelayChainResult}; +use cumulus_relay_chain_interface::{ + PHeader, RelayChainError, RelayChainInterface, RelayChainResult, +}; use futures::{FutureExt, Stream, StreamExt}; use polkadot_overseer::Handle; @@ -33,6 +35,7 @@ use sp_state_machine::StorageValue; use sp_storage::StorageKey; use std::pin::Pin; +use cumulus_primitives_core::relay_chain::BlockId; pub use url::Url; mod reconnecting_ws_client; @@ -75,6 +78,19 @@ impl RelayChainInterface for RelayChainRpcInterface { .await } + async fn header(&self, block_id: BlockId) -> RelayChainResult> { + let hash = match block_id { + BlockId::Hash(hash) => hash, + BlockId::Number(num) => + self.rpc_client.chain_get_block_hash(Some(num)).await?.ok_or_else(|| { + RelayChainError::GenericError(format!("block with number {num} not found")) + })?, + }; + let header = self.rpc_client.chain_get_header(Some(hash)).await?; + + Ok(header) + } + async fn persisted_validation_data( &self, hash: RelayHash,