blocks: Fetch chainHead body

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2022-11-25 19:12:59 +00:00
parent 07638f39c3
commit 76aa08b1dc
2 changed files with 51 additions and 2 deletions
+47 -1
View File
@@ -12,9 +12,16 @@ use crate::{
Error,
},
events,
rpc::ChainBlockResponse,
rpc::{
subscription_events::{
ChainHeadEvent,
ChainHeadResult,
},
ChainBlockResponse,
},
Config,
};
use codec::Decode;
use derivative::Derivative;
use futures::lock::Mutex as AsyncMutex;
use sp_runtime::traits::{
@@ -45,7 +52,46 @@ where
client,
}
}
/// Return the block hash.
pub fn hash(&self) -> T::Hash {
self.hash.clone()
}
}
impl<T, C> FollowBlock<T, C>
where
T: Config,
C: OnlineClientT<T>,
{
pub async fn body(&self) -> Result<Vec<Vec<u8>>, Error> {
let mut sub = self
.client
.rpc()
.subscribe_chainhead_body(self.hash, self.subscription_id.clone())
.await?;
if let Some(event) = sub.next().await {
let event = event?;
println!("Got event: {:?}", event);
return match event {
ChainHeadEvent::Done(ChainHeadResult { result }) => {
let bytes = hex::decode(result.trim_start_matches("0x"))
.map_err(|err| Error::Other(err.to_string()))?;
let extrinsics: Vec<Vec<u8>> = Decode::decode(&mut &bytes[..])?;
Ok(extrinsics)
}
_ => Err(Error::Other("Failed to fetch the block body".into())),
}
}
Err(Error::Other("Failed to fetch the block body".into()))
}
}
/// A representation of a block.
pub struct Block<T: Config, C> {
header: T::Header,
+4 -1
View File
@@ -172,11 +172,14 @@ where
.subscribe_chainhead_follow(runtime_updates)
.await?;
let subscription_id = match sub.subscription_id() {
let _subscription_id = match sub.subscription_id() {
Some(id) => id.clone(),
None => return Err(Error::Other("Subscription without ID".into())),
};
// TODO: Jsonrpsee needs update.
let subscription_id = "A".to_string();
// Flatten the finalized events and map them into a `FollowBlock`.
Ok(sub
.flat_map(move |event| {