Files
pezkuwi-subxt/substrate/primitives/api/test/tests/runtime_calls.rs
T
Michal Kucharczyk 6a295e7c28 substrate-test-runtime migrated to "pure" frame runtime (#13737)
* substrate-test-runtime migrated to pure-frame based

* test block builder: helpers added

* simple renaming

* basic_authorship test adjusted

* block_building storage_proof test adjusted

* babe: tests: should_panic expected added

* babe: tests adjusted

ConsensusLog::NextEpochData is now added by pallet_babe as
pallet_babe::SameAuthoritiesForever trigger is used in runtime config.

* beefy: tests adjusted

test-substrate-runtime is now using frame::executive to finalize the
block. during finalization the digests stored during block execution are
checked against header digests:
https://github.com/paritytech/substrate/blob/91bb2d29ca905599098a5b35eaf24867c4fbd60a/frame/executive/src/lib.rs#L585-L591

It makes impossible to directly manipulate header's digets, w/o
depositing logs into system pallet storage `Digest<T: Config>`.

Instead of this dedicated extrinsic allowing to store logs items
(MmrRoot / AuthoritiesChange) is used.

* grandpa: tests adjusted

test-substrate-runtime is now using frame::executive to finalize the
block. during finalization the digest logs stored during block execution are
checked against header digest logs:
https://github.com/paritytech/substrate/blob/91bb2d29ca905599098a5b35eaf24867c4fbd60a/frame/executive/src/lib.rs#L585-L591

It makes impossible to directly manipulate header's digets, w/o
depositing logs into system pallet storage `Digest<T: Config>`.

Instead of this dedicated extrinsic allowing to store logs items
(ScheduledChange / ForcedChange and DigestItem::Other) is used.

* network:bitswap: test adjusted

The size of unchecked extrinsic was increased. The pattern used in test will
be placed at the end of scale-encoded buffer.

* runtime apis versions adjusted

* storage keys used in runtime adjusted

* wasm vs native tests removed

* rpc tests: adjusted

Transfer transaction processing was slightly improved, test was
adjusted.

* tests: sizes adjusted

Runtime extrinsic size was increased. Size of data read during block
execution was also increased due to usage of new pallets in runtime.

Sizes were adjusted in tests.

* cargo.lock update

cargo update -p substrate-test-runtime -p substrate-test-runtime-client

* warnings fixed

* builders cleanup: includes / std

* extrinsic validation cleanup

* txpool: benches performance fixed

* fmt

* spelling

* Apply suggestions from code review

Co-authored-by: Davide Galassi <davxy@datawok.net>

* Apply code review suggestions

* Apply code review suggestions

* get rid of 1063 const

* renaming: UncheckedExtrinsic -> Extrinsic

* test-utils-runtime: further step to pure-frame

* basic-authorship: tests OK

* CheckSubstrateCall added + tests fixes

* test::Transfer call removed

* priority / propagate / no sudo+root-testing

* fixing warnings + format

* cleanup: build2/nonce + format

* final tests fixes

all tests are passing

* logs/comments removal

* should_not_accept_old_signatures test removed

* make txpool benches work again

* Cargo.lock reset

* format

* sudo hack removed

* txpool benches fix+cleanup

* .gitignore reverted

* rebase fixing + unsigned cleanup

* Cargo.toml/Cargo.lock cleanup

* force-debug feature removed

* mmr tests fixed

* make cargo-clippy happy

* network sync test uses unsigned extrinsic

* cleanup

* ".git/.scripts/commands/fmt/fmt.sh"

* push_storage_change signed call remove

* GenesisConfig cleanup

* fix

* fix

* GenesisConfig simplified

* storage_keys_works: reworked

* storage_keys_works: expected keys in vec

* storage keys list moved to substrate-test-runtime

* substrate-test: some sanity tests + GenesisConfigBuilder rework

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Apply suggestions from code review

* Review suggestions

* fix

* fix

* beefy: generate_blocks_and_sync block_num sync with actaul value

* Apply suggestions from code review

Co-authored-by: Davide Galassi <davxy@datawok.net>

* Update test-utils/runtime/src/genesismap.rs

Co-authored-by: Davide Galassi <davxy@datawok.net>

* cargo update -p sc-rpc -p sc-transaction-pool

* Review suggestions

* fix

* doc added

* slot_duration adjusted for Babe::slot_duration

* small doc fixes

* array_bytes::hex used instead of hex

* tiny -> medium name fix

* Apply suggestions from code review

Co-authored-by: Sebastian Kunert <skunert49@gmail.com>

* TransferData::try_from_unchecked_extrinsic -> try_from

* Update Cargo.lock

---------

Co-authored-by: parity-processbot <>
Co-authored-by: Davide Galassi <davxy@datawok.net>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Sebastian Kunert <skunert49@gmail.com>
2023-05-04 16:20:22 +00:00

190 lines
5.6 KiB
Rust

// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use sp_api::{Core, ProvideRuntimeApi};
use sp_runtime::traits::{HashFor, Header as HeaderT};
use sp_state_machine::{
create_proof_check_backend, execution_proof_check_on_trie_backend, ExecutionStrategy,
};
use substrate_test_runtime_client::{
prelude::*,
runtime::{Block, Header, TestAPI, Transfer},
DefaultTestClientBuilderExt, TestClientBuilder,
};
use codec::Encode;
use sc_block_builder::BlockBuilderProvider;
use sp_consensus::SelectChain;
use substrate_test_runtime_client::sc_executor::WasmExecutor;
fn calling_function_with_strat(strat: ExecutionStrategy) {
let client = TestClientBuilder::new().set_execution_strategy(strat).build();
let runtime_api = client.runtime_api();
let best_hash = client.chain_info().best_hash;
assert_eq!(runtime_api.benchmark_add_one(best_hash, &1).unwrap(), 2);
}
#[test]
fn calling_native_runtime_function() {
calling_function_with_strat(ExecutionStrategy::NativeWhenPossible);
}
#[test]
fn calling_wasm_runtime_function() {
calling_function_with_strat(ExecutionStrategy::AlwaysWasm);
}
#[test]
fn calling_native_runtime_signature_changed_function() {
let client = TestClientBuilder::new()
.set_execution_strategy(ExecutionStrategy::NativeWhenPossible)
.build();
let runtime_api = client.runtime_api();
let best_hash = client.chain_info().best_hash;
assert_eq!(runtime_api.function_signature_changed(best_hash).unwrap(), 1);
}
#[test]
fn use_trie_function() {
let client = TestClientBuilder::new()
.set_execution_strategy(ExecutionStrategy::AlwaysWasm)
.build();
let runtime_api = client.runtime_api();
let best_hash = client.chain_info().best_hash;
assert_eq!(runtime_api.use_trie(best_hash).unwrap(), 2);
}
#[test]
fn initialize_block_works() {
let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::Both).build();
let runtime_api = client.runtime_api();
let best_hash = client.chain_info().best_hash;
runtime_api
.initialize_block(
best_hash,
&Header::new(
1,
Default::default(),
Default::default(),
Default::default(),
Default::default(),
),
)
.unwrap();
assert_eq!(runtime_api.get_block_number(best_hash).unwrap(), 1);
}
#[test]
fn record_proof_works() {
let (client, longest_chain) = TestClientBuilder::new()
.set_execution_strategy(ExecutionStrategy::Both)
.build_with_longest_chain();
let storage_root =
*futures::executor::block_on(longest_chain.best_chain()).unwrap().state_root();
let runtime_code = sp_core::traits::RuntimeCode {
code_fetcher: &sp_core::traits::WrappedRuntimeCode(
client.code_at(client.chain_info().best_hash).unwrap().into(),
),
hash: vec![1],
heap_pages: None,
};
let transaction = Transfer {
amount: 1000,
nonce: 0,
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Bob.into(),
}
.into_unchecked_extrinsic();
// Build the block and record proof
let mut builder = client
.new_block_at(client.chain_info().best_hash, Default::default(), true)
.expect("Creates block builder");
builder.push(transaction.clone()).unwrap();
let (block, _, proof) = builder.build().expect("Bake block").into_inner();
let backend = create_proof_check_backend::<HashFor<Block>>(
storage_root,
proof.expect("Proof was generated"),
)
.expect("Creates proof backend.");
// Use the proof backend to execute `execute_block`.
let mut overlay = Default::default();
let executor = NativeElseWasmExecutor::<LocalExecutorDispatch>::new_with_wasm_executor(
WasmExecutor::builder().build(),
);
execution_proof_check_on_trie_backend(
&backend,
&mut overlay,
&executor,
"Core_execute_block",
&block.encode(),
&runtime_code,
)
.expect("Executes block while using the proof backend");
}
#[test]
fn call_runtime_api_with_multiple_arguments() {
let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::Both).build();
let data = vec![1, 2, 4, 5, 6, 7, 8, 8, 10, 12];
let best_hash = client.chain_info().best_hash;
client
.runtime_api()
.test_multiple_arguments(best_hash, data.clone(), data.clone(), data.len() as u32)
.unwrap();
}
#[test]
fn disable_logging_works() {
if std::env::var("RUN_TEST").is_ok() {
sp_tracing::try_init_simple();
let mut builder =
TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::AlwaysWasm);
builder.genesis_init_mut().set_wasm_code(
substrate_test_runtime_client::runtime::wasm_binary_logging_disabled_unwrap().to_vec(),
);
let client = builder.build();
let runtime_api = client.runtime_api();
runtime_api
.do_trace_log(client.chain_info().genesis_hash)
.expect("Logging should not fail");
log::error!("Logging from native works");
} else {
let executable = std::env::current_exe().unwrap();
let output = std::process::Command::new(executable)
.env("RUN_TEST", "1")
.env("RUST_LOG", "info")
.args(&["--nocapture", "disable_logging_works"])
.output()
.unwrap();
let output = dbg!(String::from_utf8(output.stderr).unwrap());
assert!(!output.contains("Hey I'm runtime"));
assert!(output.contains("Logging from native works"));
}
}