blocks: Fetch chainHead call result

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2022-11-30 13:05:42 +00:00
parent a635f3bebf
commit c2dc0ce2cd
3 changed files with 66 additions and 3 deletions
+35
View File
@@ -174,6 +174,41 @@ where
Err(Error::Other("Failed to fetch the block body".into()))
}
/// Fetch the body (vector of extrinsics) of this block.
pub async fn call(
&self,
function: String,
call_parameters: &[u8],
) -> Result<Vec<u8>, Error> {
let mut sub = self
.client
.rpc()
.subscribe_chainhead_call(
self.subscription_id.clone(),
self.hash,
function,
call_parameters,
)
.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()))?;
Ok(bytes)
}
_ => 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.
+21
View File
@@ -681,6 +681,27 @@ impl<T: Config> Rpc<T> {
Ok(subscription)
}
/// Subscribe to the chain head call.
pub async fn subscribe_chainhead_call(
&self,
subscription_id: String,
hash: T::Hash,
function: String,
call_parameters: &[u8],
) -> Result<Subscription<ChainHeadEvent<String>>, Error> {
let subscription = self
.client
.subscribe(
"chainHead_unstable_call",
rpc_params![subscription_id, hash, function, to_hex(call_parameters)],
"chainHead_unstable_stopCall",
)
.await?;
Ok(subscription)
}
/// Subscribe to finalized block headers.
///
/// Note: this may not produce _every_ block in the finalized chain;