mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 11:38:01 +00:00
Replace JSON metadata with custom metadata structures (#766)
* Move module metadata from json string to custom metadata * Revisit the metadata structures 1. Move the structures into the metadata crate. 2. Switch to using Cow/MaybeOwnedArray to support Encode/Decode * Adapt to new metadata structures * Convert event json metadata to new metadata structures * Convert storage json metadata to new metadata structures * Convert runtime metadata from json to new metadata structs * Implements new metadata structures in client and runtime * Fixes errors on `no_std` * Fixes errors after rebasing master * Do not use `Cow` anymore in metadata Also replace `String` with our own type definition `StringBuf`. This fixes compilation on `no_std`. * Wrap `RuntimeMetadata` in `RuntimeMetadataVersioned` to support versioning * Move metadata into `srml` and make core unaware of the implementation
This commit is contained in:
@@ -23,7 +23,6 @@ use primitives::AuthorityId;
|
||||
use runtime_primitives::{bft::Justification, generic::{BlockId, SignedBlock, Block as RuntimeBlock}};
|
||||
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, Zero, One, As, NumberFor};
|
||||
use runtime_primitives::BuildStorage;
|
||||
use substrate_metadata::JsonMetadataDecodable;
|
||||
use primitives::{Blake2Hasher, RlpCodec, H256};
|
||||
use primitives::storage::{StorageKey, StorageData};
|
||||
use primitives::storage::well_known_keys;
|
||||
@@ -254,26 +253,9 @@ 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| Vec::<JsonMetadataDecodable>::decode(&mut &r.return_data[..])
|
||||
.ok_or("JSON Metadata decoding failed".into()))
|
||||
.and_then(|metadata| {
|
||||
let mut json = metadata.into_iter().enumerate().fold(String::from("{"),
|
||||
|mut json, (i, m)| {
|
||||
if i > 0 {
|
||||
json.push_str(",");
|
||||
}
|
||||
let (mtype, val) = m.into_json_string();
|
||||
json.push_str(&format!(r#" "{}": {}"#, mtype, val));
|
||||
json
|
||||
}
|
||||
);
|
||||
json.push_str(" }");
|
||||
|
||||
Ok(json)
|
||||
})
|
||||
/// Returns the runtime metadata.
|
||||
pub fn metadata(&self, id: &BlockId<Block>) -> error::Result<Vec<u8>> {
|
||||
self.executor.call(id, "metadata",&[]).map(|v| v.return_data)
|
||||
}
|
||||
|
||||
/// Reads storage value at a given block + key, returning read proof.
|
||||
@@ -772,32 +754,4 @@ mod tests {
|
||||
assert!(client.state_at(&BlockId::Number(1)).unwrap() != client.state_at(&BlockId::Number(0)).unwrap());
|
||||
assert_eq!(client.body(&BlockId::Number(1)).unwrap().unwrap().len(), 1)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn json_metadata() {
|
||||
let client = test_client::new();
|
||||
|
||||
let mut builder = client.new_block().unwrap();
|
||||
|
||||
builder.push_transfer(Transfer {
|
||||
from: Keyring::Alice.to_raw_public().into(),
|
||||
to: Keyring::Ferdie.to_raw_public().into(),
|
||||
amount: 42,
|
||||
nonce: 0,
|
||||
}).unwrap();
|
||||
|
||||
assert!(builder.push_transfer(Transfer {
|
||||
from: Keyring::Eve.to_raw_public().into(),
|
||||
to: Keyring::Alice.to_raw_public().into(),
|
||||
amount: 42,
|
||||
nonce: 0,
|
||||
}).is_err());
|
||||
|
||||
client.justify_and_import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
client.json_metadata(&BlockId::Number(1)).unwrap(),
|
||||
r#"{ "events": { "name": "Test", "events": { "event": hallo } } }"#
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user