Decouple node-executor structures from wasmi (#3610)

* executor: Support non-Wasmi sandbox supervisor environments.

* node-executor: Tests don't reference WasmExecutor directly.

* executor: Simplify encoding of heap metadata.

* executor: Decouple allocator from wasmi::MemoryRef.
This commit is contained in:
Jim Posen
2019-09-23 17:38:51 +02:00
committed by Sergei Pepyakin
parent c200ce757b
commit 82e7f9e423
4 changed files with 324 additions and 224 deletions
+68 -19
View File
@@ -38,7 +38,6 @@ mod tests {
use super::Executor;
use {balances, contracts, indices, system, timestamp};
use runtime_io;
use substrate_executor::WasmExecutor;
use codec::{Encode, Decode, Joiner};
use runtime_support::{
Hashable, StorageValue, StorageMap, assert_eq_error_rate, traits::Currency,
@@ -46,7 +45,7 @@ mod tests {
use state_machine::TestExternalities as CoreTestExternalities;
use primitives::{
Blake2Hasher, NeverNativeValue, NativeOrEncoded, map,
traits::{CodeExecutor, Externalities},
traits::{CodeExecutor, Externalities}, storage::well_known_keys,
};
use sr_primitives::{
traits::{Header as HeaderT, Hash as HashT, Convert}, ApplyResult,
@@ -121,6 +120,10 @@ mod tests {
substrate_executor::NativeExecutor::new(None)
}
fn set_heap_pages<E: Externalities<Blake2Hasher>>(ext: &mut E, heap_pages: u64) {
ext.place_storage(well_known_keys::HEAP_PAGES.to_vec(), Some(heap_pages.encode()));
}
#[test]
fn panic_execution_with_foreign_code_gives_error() {
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(BLOATY_CODE, (map![
@@ -529,14 +532,26 @@ mod tests {
let (block1, block2) = blocks();
WasmExecutor::new().call(&mut t, 8, COMPACT_CODE, "Core_execute_block", &block1.0).unwrap();
executor().call::<_, NeverNativeValue, fn() -> _>(
&mut t,
"Core_execute_block",
&block1.0,
false,
None,
).0.unwrap();
runtime_io::with_externalities(&mut t, || {
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()));
assert_eq!(Balances::total_balance(&bob()), 169 * DOLLARS);
});
WasmExecutor::new().call(&mut t, 8, COMPACT_CODE, "Core_execute_block", &block2.0).unwrap();
executor().call::<_, NeverNativeValue, fn() -> _>(
&mut t,
"Core_execute_block",
&block2.0,
false,
None,
).0.unwrap();
runtime_io::with_externalities(&mut t, || {
assert_eq_error_rate!(
@@ -692,7 +707,13 @@ mod tests {
let mut t = new_test_ext(COMPACT_CODE, false);
WasmExecutor::new().call(&mut t, 8, COMPACT_CODE,"Core_execute_block", &b.0).unwrap();
executor().call::<_, NeverNativeValue, fn() -> _>(
&mut t,
"Core_execute_block",
&b.0,
false,
None,
).0.unwrap();
runtime_io::with_externalities(&mut t, || {
// Verify that the contract constructor worked well and code of TRANSFER contract is actually deployed.
@@ -710,13 +731,15 @@ mod tests {
fn wasm_big_block_import_fails() {
let mut t = new_test_ext(COMPACT_CODE, false);
let result = WasmExecutor::new().call(
set_heap_pages(&mut t, 4);
let result = executor().call::<_, NeverNativeValue, fn() -> _>(
&mut t,
4,
COMPACT_CODE,
"Core_execute_block",
&block_with_size(42, 0, 120_000).0
);
&block_with_size(42, 0, 120_000).0,
false,
None,
).0;
assert!(result.is_err()); // Err(Wasmi(Trap(Trap { kind: Host(AllocatorOutOfSpace) })))
}
@@ -761,11 +784,21 @@ mod tests {
<system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32]
], map![]));
let r = WasmExecutor::new()
.call(&mut t, 8, COMPACT_CODE, "Core_initialize_block", &vec![].and(&from_block_number(1u32)));
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
&mut t,
"Core_initialize_block",
&vec![].and(&from_block_number(1u32)),
false,
None,
).0;
assert!(r.is_ok());
let r = WasmExecutor::new()
.call(&mut t, 8, COMPACT_CODE, "BlockBuilder_apply_extrinsic", &vec![].and(&xt())).unwrap();
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
&mut t,
"BlockBuilder_apply_extrinsic",
&vec![].and(&xt()),
false,
None,
).0.unwrap().into_encoded();
let r = ApplyResult::decode(&mut &r[..]).unwrap();
assert_eq!(r, Err(InvalidTransaction::Payment.into()));
}
@@ -783,11 +816,21 @@ mod tests {
<system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32]
], map![]));
let r = WasmExecutor::new()
.call(&mut t, 8, COMPACT_CODE, "Core_initialize_block", &vec![].and(&from_block_number(1u32)));
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
&mut t,
"Core_initialize_block",
&vec![].and(&from_block_number(1u32)),
false,
None,
).0;
assert!(r.is_ok());
let r = WasmExecutor::new()
.call(&mut t, 8, COMPACT_CODE, "BlockBuilder_apply_extrinsic", &vec![].and(&xt())).unwrap();
let r = executor().call::<_, NeverNativeValue, fn() -> _>(
&mut t,
"BlockBuilder_apply_extrinsic",
&vec![].and(&xt()),
false,
None,
).0.unwrap().into_encoded();
ApplyResult::decode(&mut &r[..])
.unwrap()
.expect("Extrinsic could be applied")
@@ -822,7 +865,13 @@ mod tests {
let block1 = changes_trie_block();
let mut t = new_test_ext(COMPACT_CODE, true);
WasmExecutor::new().call(&mut t, 8, COMPACT_CODE, "Core_execute_block", &block1.0).unwrap();
executor().call::<_, NeverNativeValue, fn() -> _>(
&mut t,
"Core_execute_block",
&block1.0,
false,
None,
).0.unwrap();
assert!(t.storage_changes_root(GENESIS_HASH.into()).unwrap().is_some());
}