Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds json_metadata RPC call
  • Loading branch information
bkchr committed Sep 7, 2018
commit 8f35c770805640e28d6640271af950eb18d61dff
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions substrate/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ impl<B, E, Block> Client<B, E, Block> where
&self.executor
}

/// Returns the runtime metadata as JSON.
pub fn json_metadata(&self, id: &BlockId<Block>) -> error::Result<String> {
self.executor.call(id, "json_metadata",&[])
.and_then(|r| String::decode(&mut &r.return_data[..])
.ok_or("Metadata decoding failed".into()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check style guide on indentation. basically, it's only ever a single tab at a time, never ever mix tabs + spaces.

}

/// Reads storage value at a given block + key, returning read proof.
pub fn read_proof(&self, id: &BlockId<Block>, key: &[u8]) -> error::Result<Vec<Vec<u8>>> {
self.state_at(id)
Expand Down
1 change: 1 addition & 0 deletions substrate/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ substrate-runtime-primitives = { path = "../runtime/primitives" }
substrate-runtime-version = { path = "../runtime/version" }
substrate-state-machine = { path = "../state-machine" }
tokio = "0.1.7"
serde_json = "1.0"

[dev-dependencies]
assert_matches = "1.1"
Expand Down
1 change: 1 addition & 0 deletions substrate/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern crate substrate_runtime_primitives as runtime_primitives;
extern crate substrate_state_machine as state_machine;
extern crate substrate_runtime_version as runtime_version;
extern crate tokio;
extern crate serde_json;

#[macro_use]
extern crate error_chain;
Expand Down
6 changes: 6 additions & 0 deletions substrate/rpc/src/state/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ use rpc;

use errors;

use serde_json;

error_chain! {
links {
Client(client::error::Error, client::error::ErrorKind) #[doc = "Client error"];
}

foreign_links {
Json(serde_json::Error);
}

errors {
/// Provided block range couldn't be resolved to a list of blocks.
InvalidBlockRange(from: String, to: String, details: String) {
Expand Down
11 changes: 11 additions & 0 deletions substrate/rpc/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use rpc::futures::{stream, Future, Sink, Stream};
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Block as BlockT, Header};
use tokio::runtime::TaskExecutor;
use serde_json;

use subscriptions::Subscriptions;

Expand Down Expand Up @@ -63,6 +64,10 @@ build_rpc_trait! {
#[rpc(name = "state_getStorageSize", alias = ["state_getStorageSizeAt", ])]
fn storage_size(&self, StorageKey, Trailing<Hash>) -> Result<Option<u64>>;

/// Returns the runtime metadata as JSON.
#[rpc(name = "state_metadata", alias = ["state_metadataAt", ])]
fn json_metadata(&self, Trailing<Hash>) -> Result<serde_json::Value>;

/// Query historical storage entries (by key) starting from a block given as the second parameter.
///
/// NOTE This first returned result contains the initial state of storage for all keys.
Expand Down Expand Up @@ -138,6 +143,12 @@ impl<B, E, Block> StateApi<Block::Hash> for State<B, E, Block> where
Ok(self.storage(key, block)?.map(|x| x.0.len() as u64))
}

fn json_metadata(&self, block: Trailing<Block::Hash>) -> Result<serde_json::Value> {
let block = self.unwrap_or_best(block)?;
let metadata = self.client.json_metadata(&BlockId::Hash(block))?;
serde_json::from_str(&metadata).map_err(Into::into)
}

fn query_storage(&self, keys: Vec<StorageKey>, from: Block::Hash, to: Trailing<Block::Hash>) -> Result<Vec<StorageChangeSet<Block::Hash>>> {
let to = self.unwrap_or_best(to)?;

Expand Down