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
@@ -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,