introduce a method for fetching relay chain header to RelayChainInterface (#2794)

* introduce a method for fetching header to RelayChainInterface

* cargo fmt

* return Option<Header>
This commit is contained in:
Web3 Philosopher
2023-07-04 16:27:31 +02:00
committed by GitHub
parent 7f2238a218
commit 9c183893ae
5 changed files with 60 additions and 4 deletions
+5 -1
View File
@@ -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<Option<PHeader>> {
unimplemented!("Not needed for test")
}
}
fn build_block<B: InitBlockBuilder>(
+13
View File
@@ -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<Option<PHeader>> {
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(
@@ -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<T> Clone for RelayChainInProcessInterface<T> {
impl<Client> RelayChainInterface for RelayChainInProcessInterface<Client>
where
Client: ProvideRuntimeApi<PBlock>
+ HeaderBackend<PBlock>
+ BlockchainEvents<PBlock>
+ AuxStore
+ UsageProvider<PBlock>
@@ -110,6 +112,18 @@ where
)?)
}
async fn header(&self, block_id: BlockId) -> RelayChainResult<Option<PHeader>> {
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<Client, Api, Backend>(self, client: Arc<Client>) -> Self::Output
where
Client: ProvideRuntimeApi<PBlock>
+ HeaderBackend<PBlock>
+ BlockchainEvents<PBlock>
+ AuxStore
+ UsageProvider<PBlock>
@@ -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<PHash>;
/// Fetch the block header of a given height
async fn header(&self, block_id: BlockId) -> RelayChainResult<Option<PHeader>>;
/// Get the hash of the finalized block.
async fn finalized_block_hash(&self) -> RelayChainResult<PHash>;
@@ -293,4 +297,8 @@ where
) -> RelayChainResult<Pin<Box<dyn Stream<Item = PHeader> + Send>>> {
(**self).new_best_notification_stream().await
}
async fn header(&self, block_id: BlockId) -> RelayChainResult<Option<PHeader>> {
(**self).header(block_id).await
}
}
@@ -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<Option<PHeader>> {
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,