Prerequisites for validate_block in Cumulus (#1926)

* Adds benchmark for direct/indirect wasm function calls

* Store the benchmark function pointer in a `Cell`

* Add some documentation

* Make function implementations exchangeable

* Add parachain stub

* Add macro for registering the `validate_block` function

* Make all functions replace-able by unimplemented

* Some more refactoring

* Adds tests for executing empty parachain block

* Work on a new test with empty witness data

* Don't exchange `ext_print_*` stuff

* Some cleanup and one more function for `validate_block`

* More tests and more functions

* Fixes after merging master

* Use `parity-codec` `derive` feature

* CHange implementation of `wasm-nice-panic-message`

* Move `parachain` stuff to cumulus

* Updated wasm files

* Integrate feedback

* Switch to `ExchangeableFunction` struct

* More fixes

* Switch to Cell and panic on multiple replaces

* Increase `impl_version`

* Fix shifting

* Make the API more verbose of `ExchangeableFunction`

* Increase `impl_version`
This commit is contained in:
Bastian Köcher
2019-03-14 21:29:12 +01:00
committed by GitHub
parent b92b2cc29b
commit 990d368f0d
17 changed files with 578 additions and 170 deletions
+2
View File
@@ -20,6 +20,7 @@ runtime_io = { package = "sr-io", path = "../sr-io", default-features = false }
runtime_primitives = { package = "sr-primitives", path = "../sr-primitives", default-features = false }
runtime_version = { package = "sr-version", path = "../sr-version", default-features = false }
runtime_support = { package = "srml-support", path = "../../srml/support", default-features = false }
executive = { package = "srml-executive", path = "../../srml/executive", default-features = false }
cfg-if = "0.1.6"
[dev-dependencies]
@@ -44,4 +45,5 @@ std = [
"runtime_primitives/std",
"runtime_version/std",
"consensus_aura/std",
"executive/std",
]
+42 -2
View File
@@ -18,7 +18,8 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")] pub mod genesismap;
#[cfg(feature = "std")]
pub mod genesismap;
pub mod system;
use rstd::{prelude::*, marker::PhantomData};
@@ -77,6 +78,16 @@ pub struct Transfer {
pub nonce: u64,
}
impl Transfer {
/// Convert into a signed extrinsic.
#[cfg(feature = "std")]
pub fn into_signed_tx(self) -> Extrinsic {
let signature = keyring::AccountKeyring::from_public(&self.from)
.expect("Creates keyring from public key.").sign(&self.encode()).into();
Extrinsic::Transfer(self, signature)
}
}
/// Extrinsic for test-runtime.
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
@@ -219,6 +230,8 @@ cfg_if! {
fn function_signature_changed() -> u64;
fn fail_on_native() -> u64;
fn fail_on_wasm() -> u64;
fn benchmark_indirect_call() -> u64;
fn benchmark_direct_call() -> u64;
}
}
} else {
@@ -239,6 +252,8 @@ cfg_if! {
fn function_signature_changed() -> Vec<u64>;
fn fail_on_native() -> u64;
fn fail_on_wasm() -> u64;
fn benchmark_indirect_call() -> u64;
fn benchmark_direct_call() -> u64;
}
}
}
@@ -254,6 +269,16 @@ impl GetRuntimeBlockType for Runtime {
type RuntimeBlock = Block;
}
/// Adds one to the given input and returns the final result.
#[inline(never)]
fn benchmark_add_one(i: u64) -> u64 {
i + 1
}
/// The `benchmark_add_one` function as function pointer.
#[cfg(not(feature = "std"))]
static BENCHMARK_ADD_ONE: runtime_io::ExchangeableFunction<fn(u64) -> u64> = runtime_io::ExchangeableFunction::new(benchmark_add_one);
cfg_if! {
if #[cfg(feature = "std")] {
impl_runtime_apis! {
@@ -340,6 +365,13 @@ cfg_if! {
fn fail_on_wasm() -> u64 {
1
}
fn benchmark_indirect_call() -> u64 {
let function = benchmark_add_one;
(0..1000).fold(0, |p, i| p + function(i))
}
fn benchmark_direct_call() -> u64 {
(0..1000).fold(0, |p, i| p + benchmark_add_one(i))
}
}
impl consensus_aura::AuraApi<Block> for Runtime {
@@ -435,6 +467,14 @@ cfg_if! {
fn fail_on_wasm() -> u64 {
panic!("Failing because we are on wasm")
}
fn benchmark_indirect_call() -> u64 {
(0..10000).fold(0, |p, i| p + BENCHMARK_ADD_ONE.get()(i))
}
fn benchmark_direct_call() -> u64 {
(0..10000).fold(0, |p, i| p + benchmark_add_one(i))
}
}
impl consensus_aura::AuraApi<Block> for Runtime {
@@ -442,4 +482,4 @@ cfg_if! {
}
}
}
}
}
+30 -18
View File
@@ -21,7 +21,7 @@ use rstd::prelude::*;
use runtime_io::{storage_root, enumerated_trie_root, storage_changes_root, twox_128};
use runtime_support::storage::{self, StorageValue, StorageMap};
use runtime_support::storage_items;
use runtime_primitives::traits::{Hash as HashT, BlakeTwo256, Digest as DigestT};
use runtime_primitives::traits::{Hash as HashT, BlakeTwo256, Digest as DigestT, NumberFor, Block as BlockT};
use runtime_primitives::generic;
use runtime_primitives::{ApplyError, ApplyOutcome, ApplyResult, transaction_validity::TransactionValidity};
use parity_codec::{KeyedVec, Encode};
@@ -70,6 +70,15 @@ pub fn initialise_block(header: &Header) {
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &0u32);
}
fn execute_extrinsics_without_checks(extrinsics: Vec<<Block as BlockT>::Extrinsic>) {
// execute transactions
extrinsics.into_iter().enumerate().for_each(|(i, e)| {
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &(i as u32));
execute_transaction_backend(&e).unwrap_or_else(|_| panic!("Invalid transaction"));
storage::unhashed::kill(well_known_keys::EXTRINSIC_INDEX);
});
}
/// Actually execute all transitioning for `block`.
pub fn polish_block(block: &mut Block) {
let header = &mut block.header;
@@ -111,12 +120,7 @@ pub fn execute_block(block: Block) {
info_expect_equal_hash(&txs_root, &header.extrinsics_root);
assert!(txs_root == header.extrinsics_root, "Transaction trie root must be valid.");
// execute transactions
block.extrinsics.iter().enumerate().for_each(|(i, e)| {
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &(i as u32));
execute_transaction_backend(e).unwrap_or_else(|_| panic!("Invalid transaction"));
storage::unhashed::kill(well_known_keys::EXTRINSIC_INDEX);
});
execute_extrinsics_without_checks(block.extrinsics);
// check storage root.
let storage_root = storage_root().into();
@@ -134,6 +138,19 @@ pub fn execute_block(block: Block) {
assert!(digest == header.digest, "Header digest items must match that calculated.");
}
/// The block executor.
pub struct BlockExecutor;
impl executive::ExecuteBlock<Block> for BlockExecutor {
fn execute_block(block: Block) {
execute_block(block);
}
fn execute_extrinsics_without_checks(_: NumberFor<Block>, extrinsics: Vec<<Block as BlockT>::Extrinsic>) {
execute_extrinsics_without_checks(extrinsics);
}
}
/// Execute a transaction outside of the block execution function.
/// This doesn't attempt to validate anything regarding the block.
pub fn validate_transaction(utx: Extrinsic) -> TransactionValidity {
@@ -307,11 +324,6 @@ mod tests {
])
}
fn construct_signed_tx(tx: Transfer) -> Extrinsic {
let signature = AccountKeyring::from_public(&tx.from).unwrap().sign(&tx.encode()).into();
Extrinsic::Transfer(tx, signature)
}
fn block_import_works<F>(block_executor: F) where F: Fn(Block, &mut TestExternalities<Blake2Hasher>) {
let h = Header {
parent_hash: [69u8; 32].into(),
@@ -356,12 +368,12 @@ mod tests {
digest: Default::default(),
},
extrinsics: vec![
construct_signed_tx(Transfer {
Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Bob.into(),
amount: 69,
nonce: 0,
})
}.into_signed_tx()
],
};
@@ -377,18 +389,18 @@ mod tests {
digest: Default::default(),
},
extrinsics: vec![
construct_signed_tx(Transfer {
Transfer {
from: AccountKeyring::Bob.into(),
to: AccountKeyring::Alice.into(),
amount: 27,
nonce: 0,
}),
construct_signed_tx(Transfer {
}.into_signed_tx(),
Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Charlie.into(),
amount: 69,
nonce: 1,
}),
}.into_signed_tx(),
],
};
+52 -13
View File
@@ -61,7 +61,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitmask"
version = "0.5.0"
source = "git+https://github.com/paritytech/bitmask#c2d8d196e30b018d1385be8357fdca61b978facf"
source = "git+https://github.com/paritytech/bitmask#a84e147be602631617badd18b6b9af83391db4a9"
[[package]]
name = "blake2-rfc"
@@ -378,7 +378,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
"synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -854,7 +854,7 @@ dependencies = [
"proc-macro-crate 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -903,7 +903,7 @@ dependencies = [
"proc-macro-hack 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -960,7 +960,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -1194,6 +1194,14 @@ name = "ryu"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "safe-mix"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "schnorrkel"
version = "0.0.0"
@@ -1241,7 +1249,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -1353,7 +1361,7 @@ dependencies = [
"proc-macro-crate 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -1406,6 +1414,19 @@ dependencies = [
"sr-std 0.1.0",
]
[[package]]
name = "srml-executive"
version = "0.1.0"
dependencies = [
"parity-codec 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-io 0.1.0",
"sr-primitives 0.1.0",
"sr-std 0.1.0",
"srml-support 0.1.0",
"srml-system 0.1.0",
]
[[package]]
name = "srml-metadata"
version = "0.1.0"
@@ -1444,7 +1465,7 @@ dependencies = [
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-api-macros 0.1.0",
"srml-support-procedural-tools 0.1.0",
"syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -1455,7 +1476,7 @@ dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"srml-support-procedural-tools-derive 0.1.0",
"syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -1464,7 +1485,23 @@ version = "0.1.0"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "srml-system"
version = "0.1.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-io 0.1.0",
"sr-primitives 0.1.0",
"sr-std 0.1.0",
"srml-support 0.1.0",
"substrate-primitives 0.1.0",
]
[[package]]
@@ -1677,6 +1714,7 @@ dependencies = [
"sr-primitives 0.1.0",
"sr-std 0.1.0",
"sr-version 0.1.0",
"srml-executive 0.1.0",
"srml-support 0.1.0",
"substrate-client 0.1.0",
"substrate-consensus-aura-primitives 0.1.0",
@@ -1715,7 +1753,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "syn"
version = "0.15.27"
version = "0.15.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1730,7 +1768,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -2262,6 +2300,7 @@ dependencies = [
"checksum rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "403bb3a286107a04825a5f82e1270acc1e14028d3d554d7a1e08914549575ab8"
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
"checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7"
"checksum safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7f7bf422d23a88c16d5090d455f182bc99c60af4df6a345c63428acf5129e347"
"checksum schnorrkel 0.0.0 (git+https://github.com/w3f/schnorrkel)" = "<none>"
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
@@ -2285,7 +2324,7 @@ dependencies = [
"checksum substrate-bip39 0.2.0 (git+https://github.com/paritytech/substrate-bip39)" = "<none>"
"checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee"
"checksum subtle 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "702662512f3ddeb74a64ce2fbbf3707ee1b6bb663d28bb054e0779bbc720d926"
"checksum syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)" = "525bd55255f03c816e5d7f615587bd13030c7103354fadb104993dcee6a788ec"
"checksum syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)" = "1825685f977249735d510a242a6727b46efe914bb67e38d30c071b1b72b1d5c2"
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
"checksum take_mut 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"