mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 06:57:58 +00:00
Substrate runtime interface 2.0 (#4057)
* Adds first version of traits for generating the host functions * First steps of the procedural macro * Implements generation of the host extern functions * Prefix ext host function with snake case trait name * Implement host functions implementation on the host * Change `HostFunctions` interface * Implement `HostFunctions` for tuples * Make `WasmExecutor` generic over the host functions * Begin to add a test and make it compile * Make the test succeed * Add test to ensure that host functions are not found * It's alive! Make the `set_storage` test work * Add test for mutable references * Code cleanup and documentation etc * Add marker trait for types that should be passed as SCALE encoded * Inherit the visibility from the trait and more improvements * More impls and move them into their own file * Code simplification by dropping one trait * Give it a better name * Implement traits for arrays * Refactor code to support pass by codec/inner * Docs * Implement pass by inner for some crypto types and add a test * Implement exchangeable function support * Rewrite sr-io with as runtime interface * Start reworking after master merge * Adds `PassByCodec` derive * Adds `PassByInner` derive * Fix compilation errors * More implementations * Implement runtime interface traits for `str` * Make `sr-io` compile again * Fix more compilation errors * More progress on getting stuff back to compile * More compilation fixes * Fix warnings * Remove le conversions * Add support for `wasm_only` interfaces * Implement `Allocator` interface * Improve error message * Move `WasmAllocator` to `sr-io` and more clean ups * Use correct function signature for wasm functions * Store the host functions with the Wasm runtime * Docs update * Fix compilation after master merge * Remove `sr-io/without_std` * Make `srml-support` tests run again * More compilation error fixes * Use correct doc syntax * Fix test-runtime * Fix compilation * Catch native panics when executing the wasm runtime As with the native runtime, we now catch all native panics when we execute the wasm runtime. The panics inside the wasm runtime were already catched before by the wasm executor automatically, but any panic in the host functions could bring down the node. The recent switch to execute the native counterpart of the host function in `sr-io`, makes this change required. The native `sr-io` functions just `panic` when something is not provided or any other error occured. * Fix compilation * Don't panic in a panic * Move `sr-sandbox` to new runtime interface * Fixes tests after sandbox changes * Make sure we detect invalid utf8 * Fixes after master merge * Adds pass by enum strategy * Fix wasmtime integration * Some macro structure clean up * Rework and test exchangebale host functions * PassBy derive macros documentation * Docs for `runtime_interface` macro * Support wild card argument names * Adds ui tests * Make sure that we are backwards compatible to the old runtime interfaces * Documentation * Fixes after latest master merge * Make `wasmtime` happy * Make `full_crypto` work * Make the new interface versionable * Rename `Sanboxing` to `Sandbox` * Don't finalize in test while importing * Fix Performance regression * Fix test
This commit is contained in:
@@ -8,6 +8,7 @@ edition = "2018"
|
||||
serde = { version = "1.0.101", default-features = false, features = ["derive"] }
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
|
||||
runtime-io ={ package = "sr-io", path = "../../../core/sr-io", default-features = false }
|
||||
state-machine ={ package = "substrate-state-machine", path = "../../../core/state-machine", optional = true }
|
||||
support = { package = "srml-support", version = "2", path = "../", default-features = false }
|
||||
inherents = { package = "substrate-inherents", path = "../../../core/inherents", default-features = false }
|
||||
sr-primitives = { package = "sr-primitives", path = "../../../core/sr-primitives", default-features = false }
|
||||
@@ -25,4 +26,5 @@ std = [
|
||||
"inherents/std",
|
||||
"primitives/std",
|
||||
"sr-primitives/std",
|
||||
"state-machine",
|
||||
]
|
||||
|
||||
@@ -19,9 +19,8 @@
|
||||
#[allow(dead_code)]
|
||||
mod tests {
|
||||
use support::metadata::*;
|
||||
use support::metadata::StorageHasher;
|
||||
use support::rstd::marker::PhantomData;
|
||||
use support::codec::{Encode, Decode, EncodeLike};
|
||||
use std::marker::PhantomData;
|
||||
use codec::{Encode, Decode, EncodeLike};
|
||||
|
||||
support::decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
|
||||
|
||||
@@ -14,10 +14,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use runtime_io::with_storage;
|
||||
use support::storage::unhashed;
|
||||
use codec::Encode;
|
||||
use support::{StorageDoubleMap, StorageLinkedMap, StorageMap, StorageValue};
|
||||
use runtime_io::{TestExternalities, hashing};
|
||||
|
||||
mod no_instance {
|
||||
use codec::{Encode, Decode, EncodeLike};
|
||||
@@ -87,141 +87,141 @@ mod instance {
|
||||
|
||||
#[test]
|
||||
fn final_keys_no_instance() {
|
||||
with_storage(&mut Default::default(), || {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
no_instance::Value::put(1);
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(b"FinalKeysNone Value")), Some(1u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::twox_128(b"FinalKeysNone Value")), Some(1u32));
|
||||
|
||||
no_instance::Map::insert(1, 2);
|
||||
let mut k = b"FinalKeysNone Map".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&k)), Some(2u32));
|
||||
|
||||
no_instance::Map2::insert(1, 2);
|
||||
let mut k = b"FinalKeysNone Map2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::twox_128(&k)), Some(2u32));
|
||||
|
||||
let head = b"head of FinalKeysNone LinkedMap".to_vec();
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&head)), None);
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&head)), None);
|
||||
|
||||
no_instance::LinkedMap::insert(1, 2);
|
||||
let mut k = b"FinalKeysNone LinkedMap".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&head)), Some(1u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&head)), Some(1u32));
|
||||
|
||||
no_instance::LinkedMap2::insert(1, 2);
|
||||
let mut k = b"FinalKeysNone LinkedMap2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::twox_128(&k)), Some(2u32));
|
||||
|
||||
no_instance::DoubleMap::insert(&1, &2, &3);
|
||||
let mut k = b"FinalKeysNone DoubleMap".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
let mut k = runtime_io::blake2_256(&k).to_vec();
|
||||
k.extend(&runtime_io::blake2_256(&2u32.encode()));
|
||||
let mut k = hashing::blake2_256(&k).to_vec();
|
||||
k.extend(&hashing::blake2_256(&2u32.encode()));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
|
||||
no_instance::DoubleMap2::insert(&1, &2, &3);
|
||||
let mut k = b"FinalKeysNone DoubleMap2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
let mut k = runtime_io::twox_128(&k).to_vec();
|
||||
k.extend(&runtime_io::blake2_128(&2u32.encode()));
|
||||
let mut k = hashing::twox_128(&k).to_vec();
|
||||
k.extend(&hashing::blake2_128(&2u32.encode()));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn final_keys_default_instance() {
|
||||
with_storage(&mut Default::default(), || {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
<instance::Value<instance::DefaultInstance>>::put(1);
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(b"FinalKeysSome Value")), Some(1u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::twox_128(b"FinalKeysSome Value")), Some(1u32));
|
||||
|
||||
<instance::Map<instance::DefaultInstance>>::insert(1, 2);
|
||||
let mut k = b"FinalKeysSome Map".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&k)), Some(2u32));
|
||||
|
||||
<instance::Map2<instance::DefaultInstance>>::insert(1, 2);
|
||||
let mut k = b"FinalKeysSome Map2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::twox_128(&k)), Some(2u32));
|
||||
|
||||
let head = b"head of FinalKeysSome LinkedMap".to_vec();
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&head)), None);
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&head)), None);
|
||||
|
||||
<instance::LinkedMap<instance::DefaultInstance>>::insert(1, 2);
|
||||
let mut k = b"FinalKeysSome LinkedMap".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&head)), Some(1u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&head)), Some(1u32));
|
||||
|
||||
< instance::LinkedMap2<instance::DefaultInstance>>::insert(1, 2);
|
||||
let mut k = b"FinalKeysSome LinkedMap2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::twox_128(&k)), Some(2u32));
|
||||
|
||||
<instance::DoubleMap<instance::DefaultInstance>>::insert(&1, &2, &3);
|
||||
let mut k = b"FinalKeysSome DoubleMap".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
let mut k = runtime_io::blake2_256(&k).to_vec();
|
||||
k.extend(&runtime_io::blake2_256(&2u32.encode()));
|
||||
let mut k = hashing::blake2_256(&k).to_vec();
|
||||
k.extend(&hashing::blake2_256(&2u32.encode()));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
|
||||
<instance::DoubleMap2<instance::DefaultInstance>>::insert(&1, &2, &3);
|
||||
let mut k = b"FinalKeysSome DoubleMap2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
let mut k = runtime_io::twox_128(&k).to_vec();
|
||||
k.extend(&runtime_io::blake2_128(&2u32.encode()));
|
||||
let mut k = hashing::twox_128(&k).to_vec();
|
||||
k.extend(&hashing::blake2_128(&2u32.encode()));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn final_keys_instance_2() {
|
||||
with_storage(&mut Default::default(), || {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
<instance::Value<instance::Instance2>>::put(1);
|
||||
assert_eq!(
|
||||
unhashed::get::<u32>(&runtime_io::twox_128(b"Instance2FinalKeysSome Value")),
|
||||
unhashed::get::<u32>(&hashing::twox_128(b"Instance2FinalKeysSome Value")),
|
||||
Some(1u32)
|
||||
);
|
||||
|
||||
<instance::Map<instance::Instance2>>::insert(1, 2);
|
||||
let mut k = b"Instance2FinalKeysSome Map".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&k)), Some(2u32));
|
||||
|
||||
<instance::Map2<instance::Instance2>>::insert(1, 2);
|
||||
let mut k = b"Instance2FinalKeysSome Map2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::twox_128(&k)), Some(2u32));
|
||||
|
||||
let head = b"head of Instance2FinalKeysSome LinkedMap".to_vec();
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&head)), None);
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&head)), None);
|
||||
|
||||
<instance::LinkedMap<instance::Instance2>>::insert(1, 2);
|
||||
let mut k = b"Instance2FinalKeysSome LinkedMap".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&head)), Some(1u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::blake2_256(&head)), Some(1u32));
|
||||
|
||||
<instance::LinkedMap2<instance::Instance2>>::insert(1, 2);
|
||||
let mut k = b"Instance2FinalKeysSome LinkedMap2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(&k)), Some(2u32));
|
||||
assert_eq!(unhashed::get::<u32>(&hashing::twox_128(&k)), Some(2u32));
|
||||
|
||||
<instance::DoubleMap<instance::Instance2>>::insert(&1, &2, &3);
|
||||
let mut k = b"Instance2FinalKeysSome DoubleMap".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
let mut k = runtime_io::blake2_256(&k).to_vec();
|
||||
k.extend(&runtime_io::blake2_256(&2u32.encode()));
|
||||
let mut k = hashing::blake2_256(&k).to_vec();
|
||||
k.extend(&hashing::blake2_256(&2u32.encode()));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
|
||||
<instance::DoubleMap2<instance::Instance2>>::insert(&1, &2, &3);
|
||||
let mut k = b"Instance2FinalKeysSome DoubleMap2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
let mut k = runtime_io::twox_128(&k).to_vec();
|
||||
k.extend(&runtime_io::blake2_128(&2u32.encode()));
|
||||
let mut k = hashing::twox_128(&k).to_vec();
|
||||
k.extend(&hashing::blake2_128(&2u32.encode()));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -303,7 +303,7 @@ fn new_test_ext() -> runtime_io::TestExternalities {
|
||||
#[test]
|
||||
fn storage_instance_independance() {
|
||||
let mut storage = (std::collections::HashMap::new(), std::collections::HashMap::new());
|
||||
runtime_io::with_storage(&mut storage, || {
|
||||
state_machine::BasicExternalities::execute_with_storage(&mut storage, || {
|
||||
module2::Value::<Runtime>::put(0);
|
||||
module2::Value::<Runtime, module2::Instance1>::put(0);
|
||||
module2::Value::<Runtime, module2::Instance2>::put(0);
|
||||
|
||||
Reference in New Issue
Block a user