mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 19:51:05 +00:00
Refactor the runtime API to use traits. (#878)
* Add missing `As` imports. * Adds new API traits that will be used by the client and runtime * Switch consensus to new API's * Switches transaction-pool to new API's * Move runtime api stuff into its own crate * Adds `impl_apis!` macro for implementing the new API traits * Make `metadata` return directly a blob * Runtime replace `impl_stubs!` with `impl_apis!` * Switches to none feature based approach for declaring the different API traits * Fixes compilation error * Fixes errors * Make the `decl_apis!` trait usable from the outside * Make the `test-client` use the new API traits * Remove last `impl_stubs!` bits and move some of them into wasm executor for tests * A little bit more documentation
This commit is contained in:
@@ -12,6 +12,7 @@ parity-codec = { version = "2.0", default-features = false }
|
||||
parity-codec-derive = { version = "2.0", default-features = false }
|
||||
substrate-keyring = { path = "../keyring", optional = true }
|
||||
substrate-primitives = { path = "../primitives", default-features = false }
|
||||
sr-api = { path = "../sr-api", default-features = false }
|
||||
sr-std = { path = "../sr-std", default-features = false }
|
||||
sr-io = { path = "../sr-io", default-features = false }
|
||||
sr-primitives = { path = "../sr-primitives", default-features = false }
|
||||
@@ -27,6 +28,7 @@ std = [
|
||||
"serde_derive",
|
||||
"substrate-keyring",
|
||||
"parity-codec/std",
|
||||
"sr-api/std",
|
||||
"sr-std/std",
|
||||
"sr-io/std",
|
||||
"srml-support/std",
|
||||
|
||||
@@ -33,6 +33,7 @@ extern crate srml_support as runtime_support;
|
||||
#[macro_use]
|
||||
extern crate parity_codec_derive;
|
||||
#[macro_use]
|
||||
extern crate sr_api as runtime_api;
|
||||
extern crate sr_io as runtime_io;
|
||||
#[macro_use]
|
||||
extern crate sr_version as runtime_version;
|
||||
@@ -52,10 +53,12 @@ pub mod system;
|
||||
use rstd::prelude::*;
|
||||
use codec::{Encode, Decode};
|
||||
|
||||
use runtime_primitives::traits::{BlindCheckable, BlakeTwo256};
|
||||
use runtime_primitives::Ed25519Signature;
|
||||
use runtime_api::runtime::*;
|
||||
use runtime_primitives::traits::{BlindCheckable, BlakeTwo256, Block as BlockT};
|
||||
use runtime_primitives::{ApplyResult, Ed25519Signature};
|
||||
use runtime_version::RuntimeVersion;
|
||||
pub use primitives::hash::H256;
|
||||
use primitives::AuthorityId;
|
||||
#[cfg(any(feature = "std", test))]
|
||||
use runtime_version::NativeVersion;
|
||||
|
||||
@@ -149,15 +152,57 @@ pub fn changes_trie_config() -> primitives::ChangesTrieConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod api {
|
||||
use system;
|
||||
impl_stubs!(
|
||||
version => |()| super::version(),
|
||||
authorities => |()| system::authorities(),
|
||||
initialise_block => |header| system::initialise_block(header),
|
||||
execute_block => |block| system::execute_block(block),
|
||||
apply_extrinsic => |utx| system::execute_transaction(utx),
|
||||
finalise_block => |()| system::finalise_block(),
|
||||
balance_of => |a| system::balance_of(a)
|
||||
);
|
||||
mod test_api {
|
||||
decl_apis! {
|
||||
pub trait TestAPI {
|
||||
fn balance_of<AccountId>(id: AccountId) -> u64;
|
||||
}
|
||||
}
|
||||
}
|
||||
use test_api::runtime::TestAPI;
|
||||
|
||||
struct Runtime;
|
||||
|
||||
impl_apis! {
|
||||
impl Core<Block, AuthorityId> for Runtime {
|
||||
fn version() -> RuntimeVersion {
|
||||
version()
|
||||
}
|
||||
|
||||
fn authorities() -> Vec<AuthorityId> {
|
||||
system::authorities()
|
||||
}
|
||||
|
||||
fn execute_block(block: Block) {
|
||||
system::execute_block(block)
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockBuilder<Block, u32, u32> for Runtime {
|
||||
fn initialise_block(header: <Block as BlockT>::Header) {
|
||||
system::initialise_block(header)
|
||||
}
|
||||
|
||||
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyResult {
|
||||
system::execute_transaction(extrinsic)
|
||||
}
|
||||
|
||||
fn finalise_block() -> <Block as BlockT>::Header {
|
||||
system::finalise_block()
|
||||
}
|
||||
|
||||
fn inherent_extrinsics(_data: u32) -> Vec<u32> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn random_seed() -> <Block as BlockT>::Hash {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl TestAPI<AccountId> for Runtime {
|
||||
fn balance_of(id: AccountId) -> u64 {
|
||||
system::balance_of(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+15
-3
@@ -464,6 +464,16 @@ dependencies = [
|
||||
"unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sr-api"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"parity-codec 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sr-primitives 0.1.0",
|
||||
"sr-std 0.1.0",
|
||||
"sr-version 0.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sr-io"
|
||||
version = "0.1.0"
|
||||
@@ -491,6 +501,7 @@ dependencies = [
|
||||
"serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sr-io 0.1.0",
|
||||
"sr-std 0.1.0",
|
||||
"sr-version 0.1.0",
|
||||
"substrate-primitives 0.1.0",
|
||||
]
|
||||
|
||||
@@ -564,7 +575,7 @@ dependencies = [
|
||||
"twox-hash 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"uint 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"untrusted 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"wasmi 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"wasmi 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -591,6 +602,7 @@ dependencies = [
|
||||
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-codec 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-codec-derive 2.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sr-api 0.1.0",
|
||||
"sr-io 0.1.0",
|
||||
"sr-primitives 0.1.0",
|
||||
"sr-std 0.1.0",
|
||||
@@ -697,7 +709,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "wasmi"
|
||||
version = "0.4.0"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -801,7 +813,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum untrusted 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f392d7819dbe58833e26872f5f6f0d68b7bbbe90fc3667e98731c4a15ad9a7ae"
|
||||
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
|
||||
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
|
||||
"checksum wasmi 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "522fe3fdd44a56f25cd5ddcd8ccdb1cf2e982ceb28fcb00f41d8a018ae5245a8"
|
||||
"checksum wasmi 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d184c4b7081f30316f74f8d73c197314dcb56ea7af9323522b42a2fa9cb19453"
|
||||
"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"
|
||||
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
@@ -9,6 +9,7 @@ hex-literal = { version = "0.1.0", optional = true }
|
||||
parity-codec = { version = "2.0", default-features = false }
|
||||
parity-codec-derive = { version = "2.0", default-features = false }
|
||||
substrate-primitives = { path = "../../primitives", default-features = false }
|
||||
sr-api = { path = "../../sr-api", default-features = false }
|
||||
sr-std = { path = "../../sr-std", default-features = false }
|
||||
sr-io = { path = "../../sr-io", default-features = false }
|
||||
sr-version = { path = "../../sr-version", default-features = false }
|
||||
@@ -21,6 +22,7 @@ std = [
|
||||
"log",
|
||||
"hex-literal",
|
||||
"parity-codec/std",
|
||||
"sr-api/std",
|
||||
"sr-std/std",
|
||||
"sr-io/std",
|
||||
"srml-support/std",
|
||||
|
||||
Reference in New Issue
Block a user