-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Runtime API to execute runtime calls #777
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
78ec57c
rpc: Add RuntimeAPI call via `state_call` method
lexnv c00330e
subxt: Add runtime API
lexnv 5e0bb44
Expose the RuntimeAPI client
lexnv d42791a
tests: Test the runtime API call against the metadata
lexnv 49d4352
client: Fetch the metadata from runtime API
lexnv 54c28b0
blocks: Fix doc typo
lexnv 29bec1a
blocks: Use &str instead of String to identify fn names
lexnv b225532
Update subxt/src/runtime_api/runtime_client.rs
lexnv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// 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. | ||
|
||
//! Types associated with executing runtime API calls. | ||
|
||
mod runtime_client; | ||
mod runtime_types; | ||
|
||
pub use runtime_client::RuntimeApiClient; | ||
pub use runtime_types::RuntimeApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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. | ||
|
||
use super::runtime_types::RuntimeApi; | ||
|
||
use crate::{ | ||
client::OnlineClientT, | ||
error::Error, | ||
Config, | ||
}; | ||
use derivative::Derivative; | ||
use std::{ | ||
future::Future, | ||
marker::PhantomData, | ||
}; | ||
|
||
/// Execute runtime API calls. | ||
#[derive(Derivative)] | ||
#[derivative(Clone(bound = "Client: Clone"))] | ||
pub struct RuntimeApiClient<T, Client> { | ||
client: Client, | ||
_marker: PhantomData<T>, | ||
} | ||
|
||
impl<T, Client> RuntimeApiClient<T, Client> { | ||
/// Create a new [`RuntimeApiClient`] | ||
pub fn new(client: Client) -> Self { | ||
Self { | ||
client, | ||
_marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<T, Client> RuntimeApiClient<T, Client> | ||
where | ||
T: Config, | ||
Client: OnlineClientT<T>, | ||
{ | ||
/// Obtain a runtime API at some block hash. | ||
pub fn at( | ||
&self, | ||
block_hash: Option<T::Hash>, | ||
) -> impl Future<Output = Result<RuntimeApi<T, Client>, Error>> + Send + 'static { | ||
// Clone and pass the client in like this so that we can explicitly | ||
// return a Future that's Send + 'static, rather than tied to &self. | ||
let client = self.client.clone(); | ||
async move { | ||
// If block hash is not provided, get the hash | ||
// for the latest block and use that. | ||
let block_hash = match block_hash { | ||
Some(hash) => hash, | ||
None => { | ||
client | ||
.rpc() | ||
.block_hash(None) | ||
.await? | ||
.expect("didn't pass a block number; qed") | ||
niklasad1 marked this conversation as resolved.
Show resolved
Hide resolved
lexnv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}; | ||
|
||
Ok(RuntimeApi::new(client, block_hash)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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. | ||
|
||
use crate::{ | ||
client::OnlineClientT, | ||
error::Error, | ||
Config, | ||
}; | ||
use derivative::Derivative; | ||
use std::{ | ||
future::Future, | ||
marker::PhantomData, | ||
}; | ||
|
||
/// Execute runtime API calls. | ||
#[derive(Derivative)] | ||
#[derivative(Clone(bound = "Client: Clone"))] | ||
pub struct RuntimeApi<T: Config, Client> { | ||
client: Client, | ||
block_hash: T::Hash, | ||
_marker: PhantomData<T>, | ||
} | ||
|
||
impl<T: Config, Client> RuntimeApi<T, Client> { | ||
/// Create a new [`RuntimeApi`] | ||
pub(crate) fn new(client: Client, block_hash: T::Hash) -> Self { | ||
Self { | ||
client, | ||
block_hash, | ||
_marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<T, Client> RuntimeApi<T, Client> | ||
where | ||
T: Config, | ||
Client: OnlineClientT<T>, | ||
{ | ||
/// Execute a raw runtime API call. | ||
pub fn call_raw<'a>( | ||
&self, | ||
function: String, | ||
lexnv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
call_parameters: Option<&'a [u8]>, | ||
) -> impl Future<Output = Result<Vec<u8>, Error>> + 'a { | ||
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 data = client | ||
.rpc() | ||
.state_call(function, call_parameters, Some(block_hash)) | ||
.await?; | ||
Ok(data.0) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! :)