XXX: Generate Runtime API

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2023-02-07 19:16:19 +02:00
parent 7038902c74
commit cd497aba54
10 changed files with 57231 additions and 27 deletions
-14
View File
@@ -159,30 +159,16 @@ impl<T: Config> OnlineClient<T> {
async fn fetch_metadata(rpc: &Rpc<T>) -> Result<Metadata, Error> {
use codec::Encode;
let param = 15u32.encode();
let bytes = rpc
.state_call("Metadata_metadata_at_version", Some(&param), None)
.await?;
// println!("GOT BYTES: {:?}", bytes);
let decoded: Option<OpaqueMetadata> = Decode::decode(&mut &*bytes)?;
println!("Decoded opaque");
let decoded = decoded.unwrap();
let bytes = &decoded.0;
let meta: RuntimeMetadataPrefixed = Decode::decode(&mut &bytes[..])?;
// let metadata: Metadata = meta.try_into()?;
// println!("Availb methods {:?}", decoded.0);
// let cursor = &mut &*bytes;
// let _ = <Compact<u32>>::decode(cursor)?;
// let meta: frame_metadata::RuntimeMetadataPrefixed = Decode::decode(cursor)?;
println!("METADATA {:#?}", meta);
let metadata: Metadata = meta.try_into()?;
+2
View File
@@ -5,7 +5,9 @@
//! Types associated with executing runtime API calls.
mod runtime_client;
mod runtime_payload;
mod runtime_types;
pub use runtime_client::RuntimeApiClient;
pub use runtime_payload::RuntimeAPIPayload;
pub use runtime_types::RuntimeApi;
+44
View File
@@ -0,0 +1,44 @@
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
/// Payload for a runtime API fn.
#[derive(Debug)]
pub struct RuntimeAPIPayload {
func_name: &'static str,
data: Vec<u8>,
validation_hash: Option<[u8; 32]>,
}
impl RuntimeAPIPayload {
/// Create a new [`RuntimeAPIPayload`] from static data.
pub fn new(
func_name: &'static str,
data: Vec<u8>,
validation_hash: [u8; 32],
) -> Self {
RuntimeAPIPayload {
func_name,
data,
validation_hash: Some(validation_hash),
}
}
/// Do not validate this prior to submitting it.
pub fn unvalidated(self) -> Self {
Self {
validation_hash: None,
..self
}
}
/// Returns the function name.
pub fn func_name(&self) -> &'static str {
&self.func_name
}
/// Returns the parameter data.
pub fn param_data(&self) -> &[u8] {
&self.data
}
}
+24
View File
@@ -13,6 +13,8 @@ use std::{
marker::PhantomData,
};
use super::RuntimeAPIPayload;
/// Execute runtime API calls.
#[derive(Derivative)]
#[derivative(Clone(bound = "Client: Clone"))]
@@ -56,4 +58,26 @@ where
Ok(data.0)
}
}
/// Execute a runtime API call for the given payload.
pub fn call(
&self,
payload: RuntimeAPIPayload,
) -> impl Future<Output = Result<Vec<u8>, Error>> {
let client = self.client.clone();
let block_hash = self.block_hash;
// Ensure that the returned future doesn't have a lifetime tied to api.runtime_api(),
// which is a temporary thing we'll be throwing away quickly:
async move {
let payload = payload;
let function = payload.func_name();
let call_parameters = Some(payload.param_data());
let data = client
.rpc()
.state_call(function, call_parameters, Some(block_hash))
.await?;
Ok(data.0)
}
}
}