mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 17:01:09 +00:00
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:
@@ -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! {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
],
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user