mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 03:31:10 +00:00
Merge branch 'master' into rh-grandpa-dynamic2
This commit is contained in:
@@ -11,8 +11,8 @@ serde_derive = { version = "1.0", optional = true }
|
||||
parity-codec = { version = "2.1", default-features = false }
|
||||
parity-codec-derive = { version = "2.1", default-features = false }
|
||||
substrate-keyring = { path = "../keyring", optional = true }
|
||||
substrate-client = { path = "../client", 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 }
|
||||
@@ -26,9 +26,9 @@ std = [
|
||||
"hex-literal",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"substrate-client",
|
||||
"substrate-keyring",
|
||||
"parity-codec/std",
|
||||
"sr-api/std",
|
||||
"sr-std/std",
|
||||
"sr-io/std",
|
||||
"srml-support/std",
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
|
||||
= Test runtime
|
||||
|
||||
.Summary
|
||||
[source, toml]
|
||||
----
|
||||
include::Cargo.toml[lines=2..5]
|
||||
----
|
||||
|
||||
.Description
|
||||
----
|
||||
include::src/lib.rs[tag=description]
|
||||
----
|
||||
@@ -70,6 +70,6 @@ impl GenesisConfig {
|
||||
|
||||
pub fn additional_storage_with_genesis(genesis_block: &::Block) -> HashMap<Vec<u8>, Vec<u8>> {
|
||||
map![
|
||||
twox_128(&b"latest"[..]).to_vec() => genesis_block.hash().0.to_vec()
|
||||
twox_128(&b"latest"[..]).to_vec() => genesis_block.hash().as_fixed_bytes().to_vec()
|
||||
]
|
||||
}
|
||||
|
||||
@@ -14,9 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// tag::description[]
|
||||
//! The Substrate runtime. This can be compiled with #[no_std], ready for Wasm.
|
||||
// end::description[]
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
@@ -24,16 +22,13 @@ extern crate sr_std as rstd;
|
||||
extern crate parity_codec as codec;
|
||||
extern crate sr_primitives as runtime_primitives;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
extern crate substrate_client as client;
|
||||
|
||||
#[macro_use]
|
||||
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,12 +47,20 @@ pub mod system;
|
||||
use rstd::prelude::*;
|
||||
use codec::{Encode, Decode};
|
||||
|
||||
use runtime_api::runtime::*;
|
||||
use client::{runtime_api::runtime::*, block_builder::api::runtime::*};
|
||||
#[cfg(feature = "std")]
|
||||
use client::runtime_api::ApiExt;
|
||||
use runtime_primitives::traits::{BlindCheckable, BlakeTwo256, Block as BlockT, Extrinsic as ExtrinsicT};
|
||||
#[cfg(feature = "std")]
|
||||
use runtime_primitives::traits::ApiRef;
|
||||
use runtime_primitives::{ApplyResult, Ed25519Signature, transaction_validity::TransactionValidity};
|
||||
#[cfg(feature = "std")]
|
||||
use runtime_primitives::generic::BlockId;
|
||||
use runtime_version::RuntimeVersion;
|
||||
pub use primitives::hash::H256;
|
||||
use primitives::AuthorityId;
|
||||
#[cfg(feature = "std")]
|
||||
use primitives::OpaqueMetadata;
|
||||
#[cfg(any(feature = "std", test))]
|
||||
use runtime_version::NativeVersion;
|
||||
|
||||
@@ -157,8 +160,8 @@ pub fn changes_trie_config() -> primitives::ChangesTrieConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
mod test_api {
|
||||
decl_apis! {
|
||||
pub mod test_api {
|
||||
decl_runtime_apis! {
|
||||
pub trait TestAPI {
|
||||
fn balance_of<AccountId>(id: AccountId) -> u64;
|
||||
}
|
||||
@@ -167,10 +170,161 @@ mod test_api {
|
||||
|
||||
use test_api::runtime::TestAPI;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub struct ClientWithApi {
|
||||
call: ::std::ptr::NonNull<client::runtime_api::CallApiAt<Block>>,
|
||||
commit_on_success: ::std::cell::RefCell<bool>,
|
||||
initialised_block: ::std::cell::RefCell<Option<BlockId<Block>>>,
|
||||
changes: ::std::cell::RefCell<client::runtime_api::OverlayedChanges>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
unsafe impl Send for ClientWithApi {}
|
||||
#[cfg(feature = "std")]
|
||||
unsafe impl Sync for ClientWithApi {}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl ApiExt for ClientWithApi {
|
||||
fn map_api_result<F: FnOnce(&Self) -> Result<R, E>, R, E>(&self, map_call: F) -> Result<R, E> {
|
||||
*self.commit_on_success.borrow_mut() = false;
|
||||
let res = map_call(self);
|
||||
*self.commit_on_success.borrow_mut() = true;
|
||||
|
||||
self.commit_on_ok(&res);
|
||||
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl client::runtime_api::ConstructRuntimeApi<Block> for ClientWithApi {
|
||||
fn construct_runtime_api<'a, T: client::runtime_api::CallApiAt<Block>>(call: &'a T) -> ApiRef<'a, Self> {
|
||||
ClientWithApi {
|
||||
call: unsafe {
|
||||
::std::ptr::NonNull::new_unchecked(
|
||||
::std::mem::transmute(
|
||||
call as &client::runtime_api::CallApiAt<Block>
|
||||
)
|
||||
)
|
||||
},
|
||||
commit_on_success: true.into(),
|
||||
initialised_block: None.into(),
|
||||
changes: Default::default(),
|
||||
}.into()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl ClientWithApi {
|
||||
fn call_api_at<A: Encode, R: Decode>(
|
||||
&self,
|
||||
at: &BlockId<Block>,
|
||||
function: &'static str,
|
||||
args: &A
|
||||
) -> client::error::Result<R> {
|
||||
let res = unsafe {
|
||||
self.call.as_ref().call_api_at(
|
||||
at,
|
||||
function,
|
||||
args.encode(),
|
||||
&mut *self.changes.borrow_mut(),
|
||||
&mut *self.initialised_block.borrow_mut()
|
||||
).and_then(|r|
|
||||
R::decode(&mut &r[..])
|
||||
.ok_or_else(||
|
||||
client::error::ErrorKind::CallResultDecode(function).into()
|
||||
)
|
||||
)
|
||||
};
|
||||
|
||||
self.commit_on_ok(&res);
|
||||
res
|
||||
}
|
||||
|
||||
fn commit_on_ok<R, E>(&self, res: &Result<R, E>) {
|
||||
if *self.commit_on_success.borrow() {
|
||||
if res.is_err() {
|
||||
self.changes.borrow_mut().discard_prospective();
|
||||
} else {
|
||||
self.changes.borrow_mut().commit_prospective();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl client::runtime_api::Core<Block> for ClientWithApi {
|
||||
fn version(&self, at: &BlockId<Block>) -> Result<RuntimeVersion, client::error::Error> {
|
||||
self.call_api_at(at, "version", &())
|
||||
}
|
||||
|
||||
fn authorities(&self, at: &BlockId<Block>) -> Result<Vec<AuthorityId>, client::error::Error> {
|
||||
self.call_api_at(at, "authorities", &())
|
||||
}
|
||||
|
||||
fn execute_block(&self, at: &BlockId<Block>, block: &Block) -> Result<(), client::error::Error> {
|
||||
self.call_api_at(at, "execute_block", block)
|
||||
}
|
||||
|
||||
fn initialise_block(&self, at: &BlockId<Block>, header: &<Block as BlockT>::Header) -> Result<(), client::error::Error> {
|
||||
self.call_api_at(at, "initialise_block", header)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl client::block_builder::api::BlockBuilder<Block> for ClientWithApi {
|
||||
fn apply_extrinsic(&self, at: &BlockId<Block>, extrinsic: &<Block as BlockT>::Extrinsic) -> Result<ApplyResult, client::error::Error> {
|
||||
self.call_api_at(at, "apply_extrinsic", extrinsic)
|
||||
}
|
||||
|
||||
fn finalise_block(&self, at: &BlockId<Block>) -> Result<<Block as BlockT>::Header, client::error::Error> {
|
||||
self.call_api_at(at, "finalise_block", &())
|
||||
}
|
||||
|
||||
fn inherent_extrinsics<Inherent: Decode + Encode, Unchecked: Decode + Encode>(
|
||||
&self, at: &BlockId<Block>, inherent: &Inherent
|
||||
) -> Result<Vec<Unchecked>, client::error::Error> {
|
||||
self.call_api_at(at, "inherent_extrinsics", inherent)
|
||||
}
|
||||
|
||||
fn check_inherents<Inherent: Decode + Encode, Error: Decode + Encode>(&self, at: &BlockId<Block>, block: &Block, inherent: &Inherent) -> Result<Result<(), Error>, client::error::Error> {
|
||||
self.call_api_at(at, "check_inherents", &(block, inherent))
|
||||
}
|
||||
|
||||
fn random_seed(&self, at: &BlockId<Block>) -> Result<<Block as BlockT>::Hash, client::error::Error> {
|
||||
self.call_api_at(at, "random_seed", &())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl client::runtime_api::TaggedTransactionQueue<Block> for ClientWithApi {
|
||||
fn validate_transaction(
|
||||
&self,
|
||||
at: &BlockId<Block>,
|
||||
utx: &<Block as BlockT>::Extrinsic
|
||||
) -> Result<TransactionValidity, client::error::Error> {
|
||||
self.call_api_at(at, "validate_transaction", utx)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl client::runtime_api::Metadata<Block> for ClientWithApi {
|
||||
fn metadata(&self, at: &BlockId<Block>) -> Result<OpaqueMetadata, client::error::Error> {
|
||||
self.call_api_at(at, "metadata", &())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl test_api::TestAPI<Block> for ClientWithApi {
|
||||
fn balance_of<AccountId: Encode + Decode>(&self, at: &BlockId<Block>, id: &AccountId) -> Result<u64, client::error::Error> {
|
||||
self.call_api_at(at, "balance_of", id)
|
||||
}
|
||||
}
|
||||
|
||||
struct Runtime;
|
||||
|
||||
impl_apis! {
|
||||
impl Core<Block, AuthorityId> for Runtime {
|
||||
impl_runtime_apis! {
|
||||
impl Core<Block> for Runtime {
|
||||
fn version() -> RuntimeVersion {
|
||||
version()
|
||||
}
|
||||
@@ -182,19 +336,19 @@ impl_apis! {
|
||||
fn execute_block(block: Block) {
|
||||
system::execute_block(block)
|
||||
}
|
||||
|
||||
fn initialise_block(header: <Block as BlockT>::Header) {
|
||||
system::initialise_block(header)
|
||||
}
|
||||
}
|
||||
|
||||
impl TaggedTransactionQueue<Block, TransactionValidity> for Runtime {
|
||||
impl TaggedTransactionQueue<Block> for Runtime {
|
||||
fn validate_transaction(utx: <Block as BlockT>::Extrinsic) -> TransactionValidity {
|
||||
system::validate_transaction(utx)
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockBuilder<Block, u32, u32, 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)
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ pub fn execute_block(block: Block) {
|
||||
|
||||
// check digest
|
||||
let mut digest = Digest::default();
|
||||
if let Some(storage_changes_root) = storage_changes_root(header.number) {
|
||||
if let Some(storage_changes_root) = storage_changes_root(header.parent_hash.into(), header.number - 1) {
|
||||
digest.push(generic::DigestItem::ChangesTrieRoot::<Hash, u64>(storage_changes_root.into()));
|
||||
}
|
||||
assert!(digest == header.digest, "Header digest items must match that calculated.");
|
||||
@@ -117,7 +117,7 @@ pub fn validate_transaction(utx: Extrinsic) -> TransactionValidity {
|
||||
}
|
||||
|
||||
let hash = |from: &AccountId, nonce: u64| {
|
||||
twox_128(&nonce.to_keyed_vec(&*from)).to_vec()
|
||||
twox_128(&nonce.to_keyed_vec(from.as_bytes())).to_vec()
|
||||
};
|
||||
let requires = if tx.nonce != expected_nonce && tx.nonce > 0 {
|
||||
let mut deps = Vec::new();
|
||||
@@ -160,7 +160,7 @@ pub fn finalise_block() -> Header {
|
||||
let number = <Number>::take();
|
||||
let parent_hash = <ParentHash>::take();
|
||||
let storage_root = BlakeTwo256::storage_root();
|
||||
let storage_changes_root = BlakeTwo256::storage_changes_root(number);
|
||||
let storage_changes_root = BlakeTwo256::storage_changes_root(parent_hash, number - 1);
|
||||
|
||||
let mut digest = Digest::default();
|
||||
if let Some(storage_changes_root) = storage_changes_root {
|
||||
@@ -221,7 +221,11 @@ fn execute_transaction_backend(utx: &Extrinsic) -> ApplyResult {
|
||||
fn info_expect_equal_hash(given: &Hash, expected: &Hash) {
|
||||
use primitives::hexdisplay::HexDisplay;
|
||||
if given != expected {
|
||||
println!("Hash: given={}, expected={}", HexDisplay::from(&given.0), HexDisplay::from(&expected.0));
|
||||
println!(
|
||||
"Hash: given={}, expected={}",
|
||||
HexDisplay::from(given.as_fixed_bytes()),
|
||||
HexDisplay::from(expected.as_fixed_bytes())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,8 +233,8 @@ fn info_expect_equal_hash(given: &Hash, expected: &Hash) {
|
||||
fn info_expect_equal_hash(given: &Hash, expected: &Hash) {
|
||||
if given != expected {
|
||||
::runtime_io::print("Hash not equal");
|
||||
::runtime_io::print(&given.0[..]);
|
||||
::runtime_io::print(&expected.0[..]);
|
||||
::runtime_io::print(given.as_bytes());
|
||||
::runtime_io::print(expected.as_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -257,7 +261,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn construct_signed_tx(tx: Transfer) -> Extrinsic {
|
||||
let signature = Keyring::from_raw_public(tx.from.0).unwrap().sign(&tx.encode()).into();
|
||||
let signature = Keyring::from_raw_public(tx.from.to_fixed_bytes()).unwrap().sign(&tx.encode()).into();
|
||||
Extrinsic { transfer: tx, signature }
|
||||
}
|
||||
|
||||
|
||||
+803
-27
File diff suppressed because it is too large
Load Diff
@@ -9,7 +9,7 @@ hex-literal = { version = "0.1.0", optional = true }
|
||||
parity-codec = { version = "2.1", default-features = false }
|
||||
parity-codec-derive = { version = "2.1", default-features = false }
|
||||
substrate-primitives = { path = "../../primitives", default-features = false }
|
||||
sr-api = { path = "../../sr-api", default-features = false }
|
||||
substrate-client = { path = "../../client", 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 }
|
||||
@@ -22,12 +22,12 @@ std = [
|
||||
"log",
|
||||
"hex-literal",
|
||||
"parity-codec/std",
|
||||
"sr-api/std",
|
||||
"sr-std/std",
|
||||
"sr-io/std",
|
||||
"srml-support/std",
|
||||
"sr-version/std",
|
||||
"substrate-primitives/std",
|
||||
"substrate-client/std",
|
||||
"sr-primitives/std"
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user