From 6d0c2199d0a7839802237c1d892b1eb20435faa1 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 3 Jul 2020 17:38:59 +0200 Subject: [PATCH 01/14] Better handling of stable-only build --- bin/node/cli/src/chain_spec.rs | 2 +- bin/node/executor/tests/basic.rs | 38 +++++++++---------- bin/node/executor/tests/common.rs | 2 +- bin/node/executor/tests/fees.rs | 14 +++---- bin/node/executor/tests/submit_transaction.rs | 12 +++--- bin/node/testing/src/bench.rs | 2 +- bin/node/testing/src/genesis.rs | 2 +- client/executor/src/integration_tests/mod.rs | 14 +++---- client/executor/src/lib.rs | 2 +- frame/system/src/tests.rs | 4 +- primitives/runtime-interface/test/src/lib.rs | 34 ++++++++--------- test-utils/runtime/src/genesismap.rs | 2 +- test-utils/runtime/src/system.rs | 6 +-- utils/wasm-builder-runner/src/lib.rs | 2 +- utils/wasm-builder/src/lib.rs | 4 +- 15 files changed, 70 insertions(+), 70 deletions(-) diff --git a/bin/node/cli/src/chain_spec.rs b/bin/node/cli/src/chain_spec.rs index 8617024255f28..e7b45ac7cc48f 100644 --- a/bin/node/cli/src/chain_spec.rs +++ b/bin/node/cli/src/chain_spec.rs @@ -241,7 +241,7 @@ pub fn testnet_genesis( GenesisConfig { frame_system: Some(SystemConfig { - code: WASM_BINARY.to_vec(), + code: WASM_BINARY.expect("Testing wasm binary not available").to_vec(), changes_trie_config: Default::default(), }), pallet_balances: Some(BalancesConfig { diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index e4de98d90e94d..227478c9082a1 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -47,7 +47,7 @@ use self::common::{*, sign}; /// The idea here is to pass it as the current runtime code to the executor so the executor will /// have to execute provided wasm code instead of the native equivalent. This trick is used to /// test code paths that differ between native and wasm versions. -pub const BLOATY_CODE: &[u8] = node_runtime::WASM_BINARY_BLOATY; +pub const BLOATY_CODE: Option<&[u8]> = node_runtime::WASM_BINARY_BLOATY; /// Default transfer fee. This will use the same logic that is implemented in transaction-payment module. /// @@ -75,7 +75,7 @@ fn set_heap_pages(ext: &mut E, heap_pages: u64) { fn changes_trie_block() -> (Vec, Hash) { construct_block( - &mut new_test_ext(COMPACT_CODE, true), + &mut new_test_ext(COMPACT_CODE.unwrap(), true), 1, GENESIS_HASH.into(), vec![ @@ -95,7 +95,7 @@ fn changes_trie_block() -> (Vec, Hash) { /// are not guaranteed to be deterministic) and to ensure that the correct state is propagated /// from block1's execution to block2 to derive the correct storage_root. fn blocks() -> ((Vec, Hash), (Vec, Hash)) { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); let block1 = construct_block( &mut t, 1, @@ -140,7 +140,7 @@ fn blocks() -> ((Vec, Hash), (Vec, Hash)) { fn block_with_size(time: u64, nonce: u32, size: usize) -> (Vec, Hash) { construct_block( - &mut new_test_ext(COMPACT_CODE, false), + &mut new_test_ext(COMPACT_CODE.unwrap(), false), 1, GENESIS_HASH.into(), vec![ @@ -158,7 +158,7 @@ fn block_with_size(time: u64, nonce: u32, size: usize) -> (Vec, Hash) { #[test] fn panic_execution_with_foreign_code_gives_error() { - let mut t = new_test_ext(BLOATY_CODE, false); + let mut t = new_test_ext(BLOATY_CODE.unwrap(), false); t.insert( >::hashed_key_for(alice()), (69u128, 0u8, 0u128, 0u128, 0u128).encode() @@ -187,7 +187,7 @@ fn panic_execution_with_foreign_code_gives_error() { #[test] fn bad_extrinsic_with_native_equivalent_code_gives_error() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 69u128, 0u128, 0u128, 0u128).encode() @@ -216,7 +216,7 @@ fn bad_extrinsic_with_native_equivalent_code_gives_error() { #[test] fn successful_execution_with_native_equivalent_code_gives_ok() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode() @@ -259,7 +259,7 @@ fn successful_execution_with_native_equivalent_code_gives_ok() { #[test] fn successful_execution_with_foreign_code_gives_ok() { - let mut t = new_test_ext(BLOATY_CODE, false); + let mut t = new_test_ext(BLOATY_CODE.unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode() @@ -302,7 +302,7 @@ fn successful_execution_with_foreign_code_gives_ok() { #[test] fn full_native_block_import_works() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); let (block1, block2) = blocks(); @@ -439,7 +439,7 @@ fn full_native_block_import_works() { #[test] fn full_wasm_block_import_works() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); let (block1, block2) = blocks(); @@ -586,7 +586,7 @@ fn deploying_wasm_contract_should_work() { ); let b = construct_block( - &mut new_test_ext(COMPACT_CODE, false), + &mut new_test_ext(COMPACT_CODE.unwrap(), false), 1, GENESIS_HASH.into(), vec![ @@ -625,7 +625,7 @@ fn deploying_wasm_contract_should_work() { ] ); - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); executor_call:: _>( &mut t, @@ -649,7 +649,7 @@ fn deploying_wasm_contract_should_work() { #[test] fn wasm_big_block_import_fails() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); set_heap_pages(&mut t.ext(), 4); @@ -665,7 +665,7 @@ fn wasm_big_block_import_fails() { #[test] fn native_big_block_import_succeeds() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); executor_call:: _>( &mut t, @@ -678,7 +678,7 @@ fn native_big_block_import_succeeds() { #[test] fn native_big_block_import_fails_on_fallback() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); assert!( executor_call:: _>( @@ -693,7 +693,7 @@ fn native_big_block_import_fails_on_fallback() { #[test] fn panic_execution_gives_error() { - let mut t = new_test_ext(BLOATY_CODE, false); + let mut t = new_test_ext(BLOATY_CODE.unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 0 * DOLLARS, 0u128, 0u128, 0u128).encode() @@ -722,7 +722,7 @@ fn panic_execution_gives_error() { #[test] fn successful_execution_gives_ok() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode() @@ -775,7 +775,7 @@ fn full_native_block_import_works_with_changes_trie() { let block_data = block1.0; let block = Block::decode(&mut &block_data[..]).unwrap(); - let mut t = new_test_ext(COMPACT_CODE, true); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), true); executor_call:: _>( &mut t, "Core_execute_block", @@ -791,7 +791,7 @@ fn full_native_block_import_works_with_changes_trie() { fn full_wasm_block_import_works_with_changes_trie() { let block1 = changes_trie_block(); - let mut t = new_test_ext(COMPACT_CODE, true); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), true); executor_call:: _>( &mut t, "Core_execute_block", diff --git a/bin/node/executor/tests/common.rs b/bin/node/executor/tests/common.rs index 9f4d9f71e72dd..b99a6a3fccbb8 100644 --- a/bin/node/executor/tests/common.rs +++ b/bin/node/executor/tests/common.rs @@ -68,7 +68,7 @@ impl AppCrypto for TestAuthorityId { /// making the binary slimmer. There is a convention to use compact version of the runtime /// as canonical. This is why `native_executor_instance` also uses the compact version of the /// runtime. -pub const COMPACT_CODE: &[u8] = node_runtime::WASM_BINARY; +pub const COMPACT_CODE: Option<&[u8]> = node_runtime::WASM_BINARY; pub const GENESIS_HASH: [u8; 32] = [69u8; 32]; diff --git a/bin/node/executor/tests/fees.rs b/bin/node/executor/tests/fees.rs index 8f828263c5bdb..82ca1b1f08a37 100644 --- a/bin/node/executor/tests/fees.rs +++ b/bin/node/executor/tests/fees.rs @@ -36,7 +36,7 @@ use self::common::{*, sign}; #[test] fn fee_multiplier_increases_and_decreases_on_big_weight() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); // initial fee multiplier must be one. let mut prev_multiplier = Multiplier::one(); @@ -45,7 +45,7 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() { assert_eq!(TransactionPayment::next_fee_multiplier(), prev_multiplier); }); - let mut tt = new_test_ext(COMPACT_CODE, false); + let mut tt = new_test_ext(COMPACT_CODE.unwrap(), false); // big one in terms of weight. let block1 = construct_block( @@ -130,7 +130,7 @@ fn transaction_fee_is_correct() { // - 1 MILLICENTS in substrate node. // - 1 milli-dot based on current polkadot runtime. // (this baed on assigning 0.1 CENT to the cheapest tx with `weight = 100`) - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 100 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode() @@ -209,9 +209,9 @@ fn block_weight_capacity_report() { use node_primitives::Index; // execution ext. - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); // setup ext. - let mut tt = new_test_ext(COMPACT_CODE, false); + let mut tt = new_test_ext(COMPACT_CODE.unwrap(), false); let factor = 50; let mut time = 10; @@ -276,9 +276,9 @@ fn block_length_capacity_report() { use node_primitives::Index; // execution ext. - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); // setup ext. - let mut tt = new_test_ext(COMPACT_CODE, false); + let mut tt = new_test_ext(COMPACT_CODE.unwrap(), false); let factor = 256 * 1024; let mut time = 10; diff --git a/bin/node/executor/tests/submit_transaction.rs b/bin/node/executor/tests/submit_transaction.rs index b3fc25e6cd8fc..abb3875b8020b 100644 --- a/bin/node/executor/tests/submit_transaction.rs +++ b/bin/node/executor/tests/submit_transaction.rs @@ -41,7 +41,7 @@ use self::common::*; #[test] fn should_submit_unsigned_transaction() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); let (pool, state) = TestTransactionPoolExt::new(); t.register_extension(TransactionPoolExt::new(pool)); @@ -67,7 +67,7 @@ const PHRASE: &str = "news slush supreme milk chapter athlete soap sausage put c #[test] fn should_submit_signed_transaction() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); let (pool, state) = TestTransactionPoolExt::new(); t.register_extension(TransactionPoolExt::new(pool)); @@ -92,7 +92,7 @@ fn should_submit_signed_transaction() { #[test] fn should_submit_signed_twice_from_the_same_account() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); let (pool, state) = TestTransactionPoolExt::new(); t.register_extension(TransactionPoolExt::new(pool)); @@ -136,7 +136,7 @@ fn should_submit_signed_twice_from_the_same_account() { #[test] fn should_submit_signed_twice_from_all_accounts() { - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); let (pool, state) = TestTransactionPoolExt::new(); t.register_extension(TransactionPoolExt::new(pool)); @@ -195,7 +195,7 @@ fn submitted_transaction_should_be_valid() { use sp_runtime::transaction_validity::{ValidTransaction, TransactionSource}; use sp_runtime::traits::StaticLookup; - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); let (pool, state) = TestTransactionPoolExt::new(); t.register_extension(TransactionPoolExt::new(pool)); @@ -216,7 +216,7 @@ fn submitted_transaction_should_be_valid() { // check that transaction is valid, but reset environment storage, // since CreateTransaction increments the nonce let tx0 = state.read().transactions[0].clone(); - let mut t = new_test_ext(COMPACT_CODE, false); + let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); t.execute_with(|| { let source = TransactionSource::External; let extrinsic = UncheckedExtrinsic::decode(&mut &*tx0).unwrap(); diff --git a/bin/node/testing/src/bench.rs b/bin/node/testing/src/bench.rs index 507d3420d8354..54b597ce4ea40 100644 --- a/bin/node/testing/src/bench.rs +++ b/bin/node/testing/src/bench.rs @@ -541,7 +541,7 @@ impl BenchKeyring { pub fn generate_genesis(&self) -> node_runtime::GenesisConfig { crate::genesis::config_endowed( false, - Some(node_runtime::WASM_BINARY), + Some(node_runtime::WASM_BINARY.expect("Wasm binary must be built for testing")), self.collect_account_ids(), ) } diff --git a/bin/node/testing/src/genesis.rs b/bin/node/testing/src/genesis.rs index 2bbae96cf4332..69fafcd342d21 100644 --- a/bin/node/testing/src/genesis.rs +++ b/bin/node/testing/src/genesis.rs @@ -61,7 +61,7 @@ pub fn config_endowed( digest_interval: 2, digest_levels: 2, }) } else { None }, - code: code.map(|x| x.to_vec()).unwrap_or_else(|| WASM_BINARY.to_vec()), + code: code.map(|x| x.to_vec()).unwrap_or_else(|| WASM_BINARY.expect("Wasm binary must be built for testing").to_vec()), }), pallet_indices: Some(IndicesConfig { indices: vec![], diff --git a/client/executor/src/integration_tests/mod.rs b/client/executor/src/integration_tests/mod.rs index 21924270b8c1e..a819da94296a0 100644 --- a/client/executor/src/integration_tests/mod.rs +++ b/client/executor/src/integration_tests/mod.rs @@ -49,7 +49,7 @@ fn call_in_wasm( 8, ); executor.call_in_wasm( - &WASM_BINARY[..], + &WASM_BINARY.unwrap()[..], None, function, call_data, @@ -533,7 +533,7 @@ fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) { 8, ); executor.call_in_wasm( - &WASM_BINARY[..], + &WASM_BINARY.unwrap()[..], None, "test_exhaust_heap", &[0], @@ -548,7 +548,7 @@ fn returns_mutable_static(wasm_method: WasmExecutionMethod) { let runtime = crate::wasm_runtime::create_wasm_runtime_with_code( wasm_method, 1024, - &WASM_BINARY[..], + &WASM_BINARY.unwrap()[..], HostFunctions::host_functions(), true, ).expect("Creates runtime"); @@ -582,7 +582,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) { let runtime = crate::wasm_runtime::create_wasm_runtime_with_code( wasm_method, REQUIRED_MEMORY_PAGES, - &WASM_BINARY[..], + &WASM_BINARY.unwrap()[..], HostFunctions::host_functions(), true, ).expect("Creates runtime"); @@ -602,7 +602,7 @@ fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) { let runtime = crate::wasm_runtime::create_wasm_runtime_with_code( wasm_method, 1024, - &WASM_BINARY[..], + &WASM_BINARY.unwrap()[..], HostFunctions::host_functions(), true, ).expect("Creates runtime"); @@ -630,7 +630,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) { HostFunctions::host_functions(), 8, )); - let code_hash = blake2_256(WASM_BINARY).to_vec(); + let code_hash = blake2_256(WASM_BINARY.unwrap()).to_vec(); let threads: Vec<_> = (0..8).map(|_| { let executor = executor.clone(); @@ -640,7 +640,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) { let mut ext = ext.ext(); assert_eq!( executor.call_in_wasm( - &WASM_BINARY[..], + &WASM_BINARY.unwrap()[..], Some(code_hash.clone()), "test_twox_128", &[0], diff --git a/client/executor/src/lib.rs b/client/executor/src/lib.rs index c02568c734b69..6b2d4bb55ab70 100644 --- a/client/executor/src/lib.rs +++ b/client/executor/src/lib.rs @@ -82,7 +82,7 @@ mod tests { 8, ); let res = executor.call_in_wasm( - &WASM_BINARY[..], + &WASM_BINARY.unwrap()[..], None, "test_empty_return", &[], diff --git a/frame/system/src/tests.rs b/frame/system/src/tests.rs index 2f93dc858f10b..396247021d078 100644 --- a/frame/system/src/tests.rs +++ b/frame/system/src/tests.rs @@ -368,7 +368,7 @@ fn set_code_with_real_wasm_blob() { System::set_block_number(1); System::set_code( RawOrigin::Root.into(), - substrate_test_runtime_client::runtime::WASM_BINARY.to_vec(), + substrate_test_runtime_client::runtime::WASM_BINARY.unwrap().to_vec(), ).unwrap(); assert_eq!( @@ -392,7 +392,7 @@ fn runtime_upgraded_with_set_storage() { RawOrigin::Root.into(), vec![( well_known_keys::CODE.to_vec(), - substrate_test_runtime_client::runtime::WASM_BINARY.to_vec() + substrate_test_runtime_client::runtime::WASM_BINARY.unwrap().to_vec() )], ).unwrap(); }); diff --git a/primitives/runtime-interface/test/src/lib.rs b/primitives/runtime-interface/test/src/lib.rs index 109caab6062f8..18b074fa613a7 100644 --- a/primitives/runtime-interface/test/src/lib.rs +++ b/primitives/runtime-interface/test/src/lib.rs @@ -64,17 +64,17 @@ fn call_wasm_method(binary: &[u8], method: &str) -> TestExte #[test] fn test_return_data() { - call_wasm_method::(&WASM_BINARY[..], "test_return_data"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_return_data"); } #[test] fn test_return_option_data() { - call_wasm_method::(&WASM_BINARY[..], "test_return_option_data"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_return_option_data"); } #[test] fn test_set_storage() { - let mut ext = call_wasm_method::(&WASM_BINARY[..], "test_set_storage"); + let mut ext = call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_set_storage"); let expected = "world"; assert_eq!(expected.as_bytes(), &ext.ext().storage("hello".as_bytes()).unwrap()[..]); @@ -82,27 +82,27 @@ fn test_set_storage() { #[test] fn test_return_value_into_mutable_reference() { - call_wasm_method::(&WASM_BINARY[..], "test_return_value_into_mutable_reference"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_return_value_into_mutable_reference"); } #[test] fn test_get_and_return_array() { - call_wasm_method::(&WASM_BINARY[..], "test_get_and_return_array"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_get_and_return_array"); } #[test] fn test_array_as_mutable_reference() { - call_wasm_method::(&WASM_BINARY[..], "test_array_as_mutable_reference"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_array_as_mutable_reference"); } #[test] fn test_return_input_public_key() { - call_wasm_method::(&WASM_BINARY[..], "test_return_input_public_key"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_return_input_public_key"); } #[test] fn host_function_not_found() { - let err = call_wasm_method_with_result::<()>(&WASM_BINARY[..], "test_return_data").unwrap_err(); + let err = call_wasm_method_with_result::<()>(&WASM_BINARY.unwrap()[..], "test_return_data").unwrap_err(); assert!(err.contains("Instantiation: Export ")); assert!(err.contains(" not found")); @@ -111,46 +111,46 @@ fn host_function_not_found() { #[test] #[should_panic(expected = "Invalid utf8 data provided")] fn test_invalid_utf8_data_should_return_an_error() { - call_wasm_method::(&WASM_BINARY[..], "test_invalid_utf8_data_should_return_an_error"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_invalid_utf8_data_should_return_an_error"); } #[test] fn test_overwrite_native_function_implementation() { - call_wasm_method::(&WASM_BINARY[..], "test_overwrite_native_function_implementation"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_overwrite_native_function_implementation"); } #[test] fn test_u128_i128_as_parameter_and_return_value() { - call_wasm_method::(&WASM_BINARY[..], "test_u128_i128_as_parameter_and_return_value"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_u128_i128_as_parameter_and_return_value"); } #[test] fn test_vec_return_value_memory_is_freed() { - call_wasm_method::(&WASM_BINARY[..], "test_vec_return_value_memory_is_freed"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_vec_return_value_memory_is_freed"); } #[test] fn test_encoded_return_value_memory_is_freed() { - call_wasm_method::(&WASM_BINARY[..], "test_encoded_return_value_memory_is_freed"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_encoded_return_value_memory_is_freed"); } #[test] fn test_array_return_value_memory_is_freed() { - call_wasm_method::(&WASM_BINARY[..], "test_array_return_value_memory_is_freed"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_array_return_value_memory_is_freed"); } #[test] fn test_versionining_with_new_host_works() { // We call to the new wasm binary with new host function. call_wasm_method::( - &WASM_BINARY[..], + &WASM_BINARY.unwrap()[..], "test_versionning_works", ); // we call to the old wasm binary with a new host functions // old versions of host functions should be called and test should be ok! call_wasm_method::( - &WASM_BINARY_DEPRECATED[..], + &WASM_BINARY_DEPRECATED.unwrap()[..], "test_versionning_works", ); } @@ -192,7 +192,7 @@ fn test_tracing() { let _guard = tracing::subscriber::set_default(subscriber.clone()); // Call some method to generate a trace - call_wasm_method::(&WASM_BINARY[..], "test_return_data"); + call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_return_data"); let inner = subscriber.0.lock().unwrap(); assert!(inner.spans.contains("return_input_version_1")); diff --git a/test-utils/runtime/src/genesismap.rs b/test-utils/runtime/src/genesismap.rs index 0b717fc5a99a2..687e1b427d510 100644 --- a/test-utils/runtime/src/genesismap.rs +++ b/test-utils/runtime/src/genesismap.rs @@ -55,7 +55,7 @@ impl GenesisConfig { } pub fn genesis_map(&self) -> Storage { - let wasm_runtime = WASM_BINARY.to_vec(); + let wasm_runtime = WASM_BINARY.expect("Wasm binary must be built for testing").to_vec(); let mut map: BTreeMap, Vec> = self.balances.iter() .map(|&(ref account, balance)| (account.to_keyed_vec(b"balance:"), vec![].and(&balance))) .map(|(k, v)| (blake2_256(&k[..])[..].to_vec(), v.to_vec())) diff --git a/test-utils/runtime/src/system.rs b/test-utils/runtime/src/system.rs index 3cbc416a40eea..c666a4705dccc 100644 --- a/test-utils/runtime/src/system.rs +++ b/test-utils/runtime/src/system.rs @@ -365,7 +365,7 @@ mod tests { Sr25519Keyring::Charlie.to_raw_public() ]; TestExternalities::new_with_code( - WASM_BINARY, + WASM_BINARY.unwrap(), sp_core::storage::Storage { top: map![ twox_128(b"latest").to_vec() => vec![69u8; 32], @@ -407,7 +407,7 @@ mod tests { block_import_works(|b, ext| { let mut ext = ext.ext(); let runtime_code = RuntimeCode { - code_fetcher: &sp_core::traits::WrappedRuntimeCode(WASM_BINARY.into()), + code_fetcher: &sp_core::traits::WrappedRuntimeCode(WASM_BINARY.unwrap().into()), hash: Vec::new(), heap_pages: None, }; @@ -507,7 +507,7 @@ mod tests { block_import_with_transaction_works(|b, ext| { let mut ext = ext.ext(); let runtime_code = RuntimeCode { - code_fetcher: &sp_core::traits::WrappedRuntimeCode(WASM_BINARY.into()), + code_fetcher: &sp_core::traits::WrappedRuntimeCode(WASM_BINARY.unwrap().into()), hash: Vec::new(), heap_pages: None, }; diff --git a/utils/wasm-builder-runner/src/lib.rs b/utils/wasm-builder-runner/src/lib.rs index ae1a6e4968f93..7990ea2bb97d1 100644 --- a/utils/wasm-builder-runner/src/lib.rs +++ b/utils/wasm-builder-runner/src/lib.rs @@ -466,7 +466,7 @@ fn check_provide_dummy_wasm_binary() -> bool { fn provide_dummy_wasm_binary(file_path: &Path) { fs::write( file_path, - "pub const WASM_BINARY: &[u8] = &[]; pub const WASM_BINARY_BLOATY: &[u8] = &[];", + "pub const WASM_BINARY: Option<&[u8]> = None; pub const WASM_BINARY_BLOATY: Option<&[u8]> = None;", ).expect("Writing dummy WASM binary should not fail"); } diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs index 95b75c5867f60..22375087a25d1 100644 --- a/utils/wasm-builder/src/lib.rs +++ b/utils/wasm-builder/src/lib.rs @@ -172,8 +172,8 @@ pub fn build_project_with_default_rustflags( file_name.into(), format!( r#" - pub const WASM_BINARY: &[u8] = include_bytes!("{wasm_binary}"); - pub const WASM_BINARY_BLOATY: &[u8] = include_bytes!("{wasm_binary_bloaty}"); + pub const WASM_BINARY: Option<&[u8]> = Some(include_bytes!("{wasm_binary}")); + pub const WASM_BINARY_BLOATY: Option<&[u8]> = Some(include_bytes!("{wasm_binary_bloaty}")); "#, wasm_binary = wasm_binary.wasm_binary_path_escaped(), wasm_binary_bloaty = bloaty.wasm_binary_bloaty_path_escaped(), From f487350834be36245b1acc6778ae0ed45dbee33c Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sat, 4 Jul 2020 05:45:40 +0200 Subject: [PATCH 02/14] Fix node template build --- bin/node-template/node/src/chain_spec.rs | 19 ++++++++++++++----- bin/node/executor/benches/bench.rs | 10 +++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/bin/node-template/node/src/chain_spec.rs b/bin/node-template/node/src/chain_spec.rs index fb53edd9a1a06..8279ebb505331 100644 --- a/bin/node-template/node/src/chain_spec.rs +++ b/bin/node-template/node/src/chain_spec.rs @@ -38,12 +38,15 @@ pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) { ) } -pub fn development_config() -> ChainSpec { +pub fn development_config() -> Result { + let wasm_binary = WASM_BINARY.ok_or("Development wasm binary not available".to_string())?; + ChainSpec::from_genesis( "Development", "dev", ChainType::Development, || testnet_genesis( + wasm_binary, vec![ authority_keys_from_seed("Alice"), ], @@ -64,12 +67,15 @@ pub fn development_config() -> ChainSpec { ) } -pub fn local_testnet_config() -> ChainSpec { +pub fn local_testnet_config() -> Result { + let wasm_binary = WASM_BINARY.ok_or("Development wasm binary not available".to_string())?; + ChainSpec::from_genesis( "Local Testnet", "local_testnet", ChainType::Local, || testnet_genesis( + wasm_binary, vec![ authority_keys_from_seed("Alice"), authority_keys_from_seed("Bob"), @@ -99,13 +105,16 @@ pub fn local_testnet_config() -> ChainSpec { ) } -fn testnet_genesis(initial_authorities: Vec<(AuraId, GrandpaId)>, +fn testnet_genesis( + wasm_binary: &[u8], + initial_authorities: Vec<(AuraId, GrandpaId)>, root_key: AccountId, endowed_accounts: Vec, - _enable_println: bool) -> GenesisConfig { + _enable_println: bool +) -> GenesisConfig { GenesisConfig { system: Some(SystemConfig { - code: WASM_BINARY.to_vec(), + code: wasm_binary.to_vec(), changes_trie_config: Default::default(), }), balances: Some(BalancesConfig { diff --git a/bin/node/executor/benches/bench.rs b/bin/node/executor/benches/bench.rs index ad735c87661ca..144b1e3043704 100644 --- a/bin/node/executor/benches/bench.rs +++ b/bin/node/executor/benches/bench.rs @@ -36,7 +36,7 @@ criterion_group!(benches, bench_execute_block); criterion_main!(benches); /// The wasm runtime code. -const COMPACT_CODE: &[u8] = node_runtime::WASM_BINARY; +const COMPACT_CODE: Option<&[u8]> = node_runtime::WASM_BINARY; const GENESIS_HASH: [u8; 32] = [69u8; 32]; @@ -60,7 +60,7 @@ fn sign(xt: CheckedExtrinsic) -> UncheckedExtrinsic { fn new_test_ext(genesis_config: &GenesisConfig) -> TestExternalities { let mut test_ext = TestExternalities::new_with_code( - COMPACT_CODE, + COMPACT_CODE.unwrap(), genesis_config.build_storage().unwrap(), ); test_ext.ext().place_storage(well_known_keys::HEAP_PAGES.to_vec(), Some(HEAP_PAGES.encode())); @@ -94,7 +94,7 @@ fn construct_block( }; let runtime_code = RuntimeCode { - code_fetcher: &sp_core::traits::WrappedRuntimeCode(COMPACT_CODE.into()), + code_fetcher: &sp_core::traits::WrappedRuntimeCode(COMPACT_CODE.unwrap().into()), hash: vec![1, 2, 3], heap_pages: None, }; @@ -168,7 +168,7 @@ fn bench_execute_block(c: &mut Criterion) { c.bench_function_over_inputs( "execute blocks", |b, strategy| { - let genesis_config = node_testing::genesis::config(false, Some(COMPACT_CODE)); + let genesis_config = node_testing::genesis::config(false, Some(COMPACT_CODE.unwrap())); let (use_native, wasm_method) = match strategy { ExecutionMethod::Native => (true, WasmExecutionMethod::Interpreted), ExecutionMethod::Wasm(wasm_method) => (false, *wasm_method), @@ -176,7 +176,7 @@ fn bench_execute_block(c: &mut Criterion) { let executor = NativeExecutor::new(wasm_method, None, 8); let runtime_code = RuntimeCode { - code_fetcher: &sp_core::traits::WrappedRuntimeCode(COMPACT_CODE.into()), + code_fetcher: &sp_core::traits::WrappedRuntimeCode(COMPACT_CODE.unwrap().into()), hash: vec![1, 2, 3], heap_pages: None, }; From 423729032731e0269b50bdeff3e4ea7d653e6514 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sat, 4 Jul 2020 19:47:50 +0200 Subject: [PATCH 03/14] Fix wasm builder node-template version mismatch --- bin/node-template/node/src/chain_spec.rs | 12 ++++++------ bin/node-template/runtime/build.rs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/node-template/node/src/chain_spec.rs b/bin/node-template/node/src/chain_spec.rs index 8279ebb505331..a15360cae18b5 100644 --- a/bin/node-template/node/src/chain_spec.rs +++ b/bin/node-template/node/src/chain_spec.rs @@ -41,11 +41,11 @@ pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) { pub fn development_config() -> Result { let wasm_binary = WASM_BINARY.ok_or("Development wasm binary not available".to_string())?; - ChainSpec::from_genesis( + Ok(ChainSpec::from_genesis( "Development", "dev", ChainType::Development, - || testnet_genesis( + move || testnet_genesis( wasm_binary, vec![ authority_keys_from_seed("Alice"), @@ -64,17 +64,17 @@ pub fn development_config() -> Result { None, None, None, - ) + )) } pub fn local_testnet_config() -> Result { let wasm_binary = WASM_BINARY.ok_or("Development wasm binary not available".to_string())?; - ChainSpec::from_genesis( + Ok(ChainSpec::from_genesis( "Local Testnet", "local_testnet", ChainType::Local, - || testnet_genesis( + move || testnet_genesis( wasm_binary, vec![ authority_keys_from_seed("Alice"), @@ -102,7 +102,7 @@ pub fn local_testnet_config() -> Result { None, None, None, - ) + )) } fn testnet_genesis( diff --git a/bin/node-template/runtime/build.rs b/bin/node-template/runtime/build.rs index 1f40a41ff8ee6..1ef107b7463a8 100644 --- a/bin/node-template/runtime/build.rs +++ b/bin/node-template/runtime/build.rs @@ -3,7 +3,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates("1.0.11") + .with_wasm_builder_from_crates_or_path("1.0.11", "../../../utils/wasm-builder") .export_heap_base() .import_memory() .build() From 8d2de75ccff95d8c2238757846f0edf8fc0ed836 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sat, 4 Jul 2020 20:03:03 +0200 Subject: [PATCH 04/14] Fix load_spec error --- bin/node-template/node/src/command.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/node-template/node/src/command.rs b/bin/node-template/node/src/command.rs index 3391ad2c89289..3dd93ab14eb2f 100644 --- a/bin/node-template/node/src/command.rs +++ b/bin/node-template/node/src/command.rs @@ -47,8 +47,8 @@ impl SubstrateCli for Cli { fn load_spec(&self, id: &str) -> Result, String> { Ok(match id { - "dev" => Box::new(chain_spec::development_config()), - "" | "local" => Box::new(chain_spec::local_testnet_config()), + "dev" => Box::new(chain_spec::development_config()?), + "" | "local" => Box::new(chain_spec::local_testnet_config()?), path => Box::new(chain_spec::ChainSpec::from_json_file( std::path::PathBuf::from(path), )?), From 756310d1848f9c446206a090469f321d234d5f77 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 6 Jul 2020 23:27:15 +0200 Subject: [PATCH 05/14] Add , in parameter --- bin/node-template/node/src/chain_spec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/node-template/node/src/chain_spec.rs b/bin/node-template/node/src/chain_spec.rs index a15360cae18b5..3edef79468685 100644 --- a/bin/node-template/node/src/chain_spec.rs +++ b/bin/node-template/node/src/chain_spec.rs @@ -110,7 +110,7 @@ fn testnet_genesis( initial_authorities: Vec<(AuraId, GrandpaId)>, root_key: AccountId, endowed_accounts: Vec, - _enable_println: bool + _enable_println: bool, ) -> GenesisConfig { GenesisConfig { system: Some(SystemConfig { From 6616e3e45bb00673064eb6a35f3e75bacf1cb14d Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 7 Jul 2020 00:00:01 +0200 Subject: [PATCH 06/14] Add descrptive panic messages in tests --- bin/node/cli/src/chain_spec.rs | 4 +- bin/node/executor/tests/basic.rs | 11 ++++-- bin/node/executor/tests/common.rs | 5 ++- bin/node/executor/tests/fees.rs | 14 +++---- bin/node/executor/tests/submit_transaction.rs | 12 +++--- bin/node/runtime/src/lib.rs | 8 ++++ bin/node/testing/src/bench.rs | 2 +- bin/node/testing/src/genesis.rs | 4 +- client/executor/src/integration_tests/mod.rs | 16 ++++---- client/executor/src/lib.rs | 4 +- frame/system/src/tests.rs | 4 +- .../test-wasm-deprecated/src/lib.rs | 9 ++++- .../runtime-interface/test-wasm/src/lib.rs | 7 ++++ primitives/runtime-interface/test/src/lib.rs | 38 +++++++++---------- test-utils/runtime/src/genesismap.rs | 4 +- test-utils/runtime/src/lib.rs | 7 ++++ test-utils/runtime/src/system.rs | 8 ++-- 17 files changed, 96 insertions(+), 61 deletions(-) diff --git a/bin/node/cli/src/chain_spec.rs b/bin/node/cli/src/chain_spec.rs index e7b45ac7cc48f..e323f7956f169 100644 --- a/bin/node/cli/src/chain_spec.rs +++ b/bin/node/cli/src/chain_spec.rs @@ -25,7 +25,7 @@ use node_runtime::{ AuthorityDiscoveryConfig, BabeConfig, BalancesConfig, ContractsConfig, CouncilConfig, DemocracyConfig,GrandpaConfig, ImOnlineConfig, SessionConfig, SessionKeys, StakerStatus, StakingConfig, ElectionsConfig, IndicesConfig, SocietyConfig, SudoConfig, SystemConfig, - TechnicalCommitteeConfig, WASM_BINARY, + TechnicalCommitteeConfig, wasm_binary_unwrap, }; use node_runtime::Block; use node_runtime::constants::currency::*; @@ -241,7 +241,7 @@ pub fn testnet_genesis( GenesisConfig { frame_system: Some(SystemConfig { - code: WASM_BINARY.expect("Testing wasm binary not available").to_vec(), + code: wasm_binary_unwrap().to_vec(), changes_trie_config: Default::default(), }), pallet_balances: Some(BalancesConfig { diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index 227478c9082a1..8282444346ace 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -47,7 +47,10 @@ use self::common::{*, sign}; /// The idea here is to pass it as the current runtime code to the executor so the executor will /// have to execute provided wasm code instead of the native equivalent. This trick is used to /// test code paths that differ between native and wasm versions. -pub const BLOATY_CODE: Option<&[u8]> = node_runtime::WASM_BINARY_BLOATY; +pub fn bloaty_code_unwrap() -> &'static [u8] { + node_runtime::WASM_BINARY_BLOATY.expect("Development wasm binary is not available. \ + Testing is only supported with the flag disabled.") +} /// Default transfer fee. This will use the same logic that is implemented in transaction-payment module. /// @@ -158,7 +161,7 @@ fn block_with_size(time: u64, nonce: u32, size: usize) -> (Vec, Hash) { #[test] fn panic_execution_with_foreign_code_gives_error() { - let mut t = new_test_ext(BLOATY_CODE.unwrap(), false); + let mut t = new_test_ext(bloaty_code_unwrap(), false); t.insert( >::hashed_key_for(alice()), (69u128, 0u8, 0u128, 0u128, 0u128).encode() @@ -259,7 +262,7 @@ fn successful_execution_with_native_equivalent_code_gives_ok() { #[test] fn successful_execution_with_foreign_code_gives_ok() { - let mut t = new_test_ext(BLOATY_CODE.unwrap(), false); + let mut t = new_test_ext(bloaty_code_unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode() @@ -693,7 +696,7 @@ fn native_big_block_import_fails_on_fallback() { #[test] fn panic_execution_gives_error() { - let mut t = new_test_ext(BLOATY_CODE.unwrap(), false); + let mut t = new_test_ext(bloaty_code_unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 0 * DOLLARS, 0u128, 0u128, 0u128).encode() diff --git a/bin/node/executor/tests/common.rs b/bin/node/executor/tests/common.rs index b99a6a3fccbb8..efc54ebebf199 100644 --- a/bin/node/executor/tests/common.rs +++ b/bin/node/executor/tests/common.rs @@ -68,7 +68,10 @@ impl AppCrypto for TestAuthorityId { /// making the binary slimmer. There is a convention to use compact version of the runtime /// as canonical. This is why `native_executor_instance` also uses the compact version of the /// runtime. -pub const COMPACT_CODE: Option<&[u8]> = node_runtime::WASM_BINARY; +pub fn compact_code_unwrap() -> &'static [u8] { + node_runtime::WASM_BINARY.expect("Development wasm binary is not available. \ + Testing is only supported with the flag disabled.") +} pub const GENESIS_HASH: [u8; 32] = [69u8; 32]; diff --git a/bin/node/executor/tests/fees.rs b/bin/node/executor/tests/fees.rs index 82ca1b1f08a37..b39cf344e6034 100644 --- a/bin/node/executor/tests/fees.rs +++ b/bin/node/executor/tests/fees.rs @@ -36,7 +36,7 @@ use self::common::{*, sign}; #[test] fn fee_multiplier_increases_and_decreases_on_big_weight() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); // initial fee multiplier must be one. let mut prev_multiplier = Multiplier::one(); @@ -45,7 +45,7 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() { assert_eq!(TransactionPayment::next_fee_multiplier(), prev_multiplier); }); - let mut tt = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut tt = new_test_ext(compact_code_unwrap(), false); // big one in terms of weight. let block1 = construct_block( @@ -130,7 +130,7 @@ fn transaction_fee_is_correct() { // - 1 MILLICENTS in substrate node. // - 1 milli-dot based on current polkadot runtime. // (this baed on assigning 0.1 CENT to the cheapest tx with `weight = 100`) - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 100 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode() @@ -209,9 +209,9 @@ fn block_weight_capacity_report() { use node_primitives::Index; // execution ext. - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); // setup ext. - let mut tt = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut tt = new_test_ext(compact_code_unwrap(), false); let factor = 50; let mut time = 10; @@ -276,9 +276,9 @@ fn block_length_capacity_report() { use node_primitives::Index; // execution ext. - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); // setup ext. - let mut tt = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut tt = new_test_ext(compact_code_unwrap(), false); let factor = 256 * 1024; let mut time = 10; diff --git a/bin/node/executor/tests/submit_transaction.rs b/bin/node/executor/tests/submit_transaction.rs index abb3875b8020b..dd599a996a4a8 100644 --- a/bin/node/executor/tests/submit_transaction.rs +++ b/bin/node/executor/tests/submit_transaction.rs @@ -41,7 +41,7 @@ use self::common::*; #[test] fn should_submit_unsigned_transaction() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); let (pool, state) = TestTransactionPoolExt::new(); t.register_extension(TransactionPoolExt::new(pool)); @@ -67,7 +67,7 @@ const PHRASE: &str = "news slush supreme milk chapter athlete soap sausage put c #[test] fn should_submit_signed_transaction() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); let (pool, state) = TestTransactionPoolExt::new(); t.register_extension(TransactionPoolExt::new(pool)); @@ -92,7 +92,7 @@ fn should_submit_signed_transaction() { #[test] fn should_submit_signed_twice_from_the_same_account() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); let (pool, state) = TestTransactionPoolExt::new(); t.register_extension(TransactionPoolExt::new(pool)); @@ -136,7 +136,7 @@ fn should_submit_signed_twice_from_the_same_account() { #[test] fn should_submit_signed_twice_from_all_accounts() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); let (pool, state) = TestTransactionPoolExt::new(); t.register_extension(TransactionPoolExt::new(pool)); @@ -195,7 +195,7 @@ fn submitted_transaction_should_be_valid() { use sp_runtime::transaction_validity::{ValidTransaction, TransactionSource}; use sp_runtime::traits::StaticLookup; - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); let (pool, state) = TestTransactionPoolExt::new(); t.register_extension(TransactionPoolExt::new(pool)); @@ -216,7 +216,7 @@ fn submitted_transaction_should_be_valid() { // check that transaction is valid, but reset environment storage, // since CreateTransaction increments the nonce let tx0 = state.read().transactions[0].clone(); - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); t.execute_with(|| { let source = TransactionSource::External; let extrinsic = UncheckedExtrinsic::decode(&mut &*tx0).unwrap(); diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 7bec203f8c446..05227d9c8bec2 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -88,6 +88,14 @@ use constants::{time::*, currency::*}; #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); +#[cfg(feature = "std")] +/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics. +pub fn wasm_binary_unwrap() -> &'static [u8] { + WASM_BINARY.expect("Development wasm binary is not available. This means the client is \ + built with `BUILD_DUMMY_WASM_BINARY` flag and it is only usable for \ + production chains. Please rebuild with the flag disabled.") +} + /// Runtime version. pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("node"), diff --git a/bin/node/testing/src/bench.rs b/bin/node/testing/src/bench.rs index 54b597ce4ea40..ce066381bb16f 100644 --- a/bin/node/testing/src/bench.rs +++ b/bin/node/testing/src/bench.rs @@ -541,7 +541,7 @@ impl BenchKeyring { pub fn generate_genesis(&self) -> node_runtime::GenesisConfig { crate::genesis::config_endowed( false, - Some(node_runtime::WASM_BINARY.expect("Wasm binary must be built for testing")), + Some(node_runtime::wasm_binary_unwrap()), self.collect_account_ids(), ) } diff --git a/bin/node/testing/src/genesis.rs b/bin/node/testing/src/genesis.rs index 69fafcd342d21..6fa178ba4bcdd 100644 --- a/bin/node/testing/src/genesis.rs +++ b/bin/node/testing/src/genesis.rs @@ -22,7 +22,7 @@ use crate::keyring::*; use sp_keyring::{Ed25519Keyring, Sr25519Keyring}; use node_runtime::{ GenesisConfig, BalancesConfig, SessionConfig, StakingConfig, SystemConfig, - GrandpaConfig, IndicesConfig, ContractsConfig, SocietyConfig, WASM_BINARY, + GrandpaConfig, IndicesConfig, ContractsConfig, SocietyConfig, wasm_binary_unwrap, AccountId, StakerStatus, }; use node_runtime::constants::currency::*; @@ -61,7 +61,7 @@ pub fn config_endowed( digest_interval: 2, digest_levels: 2, }) } else { None }, - code: code.map(|x| x.to_vec()).unwrap_or_else(|| WASM_BINARY.expect("Wasm binary must be built for testing").to_vec()), + code: code.map(|x| x.to_vec()).unwrap_or_else(|| wasm_binary_unwrap().to_vec()), }), pallet_indices: Some(IndicesConfig { indices: vec![], diff --git a/client/executor/src/integration_tests/mod.rs b/client/executor/src/integration_tests/mod.rs index a819da94296a0..79eb2594c6fac 100644 --- a/client/executor/src/integration_tests/mod.rs +++ b/client/executor/src/integration_tests/mod.rs @@ -24,7 +24,7 @@ use sp_core::{ offchain::{OffchainExt, testing}, traits::{Externalities, CallInWasm}, }; -use sc_runtime_test::WASM_BINARY; +use sc_runtime_test::wasm_binary_unwrap; use sp_state_machine::TestExternalities as CoreTestExternalities; use test_case::test_case; use sp_trie::{TrieConfiguration, trie_types::Layout}; @@ -49,7 +49,7 @@ fn call_in_wasm( 8, ); executor.call_in_wasm( - &WASM_BINARY.unwrap()[..], + &wasm_binary_unwrap()[..], None, function, call_data, @@ -533,7 +533,7 @@ fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) { 8, ); executor.call_in_wasm( - &WASM_BINARY.unwrap()[..], + &wasm_binary_unwrap()[..], None, "test_exhaust_heap", &[0], @@ -548,7 +548,7 @@ fn returns_mutable_static(wasm_method: WasmExecutionMethod) { let runtime = crate::wasm_runtime::create_wasm_runtime_with_code( wasm_method, 1024, - &WASM_BINARY.unwrap()[..], + &wasm_binary_unwrap()[..], HostFunctions::host_functions(), true, ).expect("Creates runtime"); @@ -582,7 +582,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) { let runtime = crate::wasm_runtime::create_wasm_runtime_with_code( wasm_method, REQUIRED_MEMORY_PAGES, - &WASM_BINARY.unwrap()[..], + &wasm_binary_unwrap()[..], HostFunctions::host_functions(), true, ).expect("Creates runtime"); @@ -602,7 +602,7 @@ fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) { let runtime = crate::wasm_runtime::create_wasm_runtime_with_code( wasm_method, 1024, - &WASM_BINARY.unwrap()[..], + &wasm_binary_unwrap()[..], HostFunctions::host_functions(), true, ).expect("Creates runtime"); @@ -630,7 +630,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) { HostFunctions::host_functions(), 8, )); - let code_hash = blake2_256(WASM_BINARY.unwrap()).to_vec(); + let code_hash = blake2_256(wasm_binary_unwrap()).to_vec(); let threads: Vec<_> = (0..8).map(|_| { let executor = executor.clone(); @@ -640,7 +640,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) { let mut ext = ext.ext(); assert_eq!( executor.call_in_wasm( - &WASM_BINARY.unwrap()[..], + &wasm_binary_unwrap()[..], Some(code_hash.clone()), "test_twox_128", &[0], diff --git a/client/executor/src/lib.rs b/client/executor/src/lib.rs index 6b2d4bb55ab70..56a81b24b4076 100644 --- a/client/executor/src/lib.rs +++ b/client/executor/src/lib.rs @@ -65,7 +65,7 @@ pub trait RuntimeInfo { #[cfg(test)] mod tests { use super::*; - use sc_runtime_test::WASM_BINARY; + use sc_runtime_test::wasm_binary_unwrap; use sp_io::TestExternalities; use sp_wasm_interface::HostFunctions; use sp_core::traits::CallInWasm; @@ -82,7 +82,7 @@ mod tests { 8, ); let res = executor.call_in_wasm( - &WASM_BINARY.unwrap()[..], + &wasm_binary_unwrap()[..], None, "test_empty_return", &[], diff --git a/frame/system/src/tests.rs b/frame/system/src/tests.rs index 396247021d078..55286d951cc27 100644 --- a/frame/system/src/tests.rs +++ b/frame/system/src/tests.rs @@ -368,7 +368,7 @@ fn set_code_with_real_wasm_blob() { System::set_block_number(1); System::set_code( RawOrigin::Root.into(), - substrate_test_runtime_client::runtime::WASM_BINARY.unwrap().to_vec(), + substrate_test_runtime_client::runtime::wasm_binary_unwrap().to_vec(), ).unwrap(); assert_eq!( @@ -392,7 +392,7 @@ fn runtime_upgraded_with_set_storage() { RawOrigin::Root.into(), vec![( well_known_keys::CODE.to_vec(), - substrate_test_runtime_client::runtime::WASM_BINARY.unwrap().to_vec() + substrate_test_runtime_client::runtime::wasm_binary_unwrap().to_vec() )], ).unwrap(); }); diff --git a/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs b/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs index ad005bfb5f8b9..19b4aaececcc4 100644 --- a/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs +++ b/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs @@ -26,6 +26,13 @@ use sp_runtime_interface::runtime_interface; #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); +#[cfg(feature = "std")] +/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics. +pub fn wasm_binary_unwrap() -> &'static [u8] { + WASM_BINARY.expect("Development wasm binary is not available. Testing is only \ + supported with the flag disabled.") +} + /// This function is not used, but we require it for the compiler to include `sp-io`. /// `sp-io` is required for its panic and oom handler. #[no_mangle] @@ -50,4 +57,4 @@ wasm_export_functions! { assert!(!test_api::test_versionning(142)); assert!(!test_api::test_versionning(0)); } -} \ No newline at end of file +} diff --git a/primitives/runtime-interface/test-wasm/src/lib.rs b/primitives/runtime-interface/test-wasm/src/lib.rs index 90112046fcda4..28895df2214d1 100644 --- a/primitives/runtime-interface/test-wasm/src/lib.rs +++ b/primitives/runtime-interface/test-wasm/src/lib.rs @@ -30,6 +30,13 @@ use sp_core::{sr25519::Public, wasm_export_functions}; #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); +#[cfg(feature = "std")] +/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics. +pub fn wasm_binary_unwrap() -> &'static [u8] { + WASM_BINARY.expect("Development wasm binary is not available. Testing is only \ + supported with the flag disabled.") +} + /// Used in the `test_array_as_mutable_reference` test. const TEST_ARRAY: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; diff --git a/primitives/runtime-interface/test/src/lib.rs b/primitives/runtime-interface/test/src/lib.rs index 18b074fa613a7..c213c977829e6 100644 --- a/primitives/runtime-interface/test/src/lib.rs +++ b/primitives/runtime-interface/test/src/lib.rs @@ -22,8 +22,8 @@ use sp_runtime_interface::*; -use sp_runtime_interface_test_wasm::{WASM_BINARY, test_api::HostFunctions}; -use sp_runtime_interface_test_wasm_deprecated::WASM_BINARY as WASM_BINARY_DEPRECATED; +use sp_runtime_interface_test_wasm::{wasm_binary_unwrap, test_api::HostFunctions}; +use sp_runtime_interface_test_wasm_deprecated::wasm_binary_unwrap as wasm_binary_deprecated_unwrap; use sp_wasm_interface::HostFunctions as HostFunctionsT; use sc_executor::CallInWasm; @@ -64,17 +64,17 @@ fn call_wasm_method(binary: &[u8], method: &str) -> TestExte #[test] fn test_return_data() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_return_data"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_return_data"); } #[test] fn test_return_option_data() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_return_option_data"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_return_option_data"); } #[test] fn test_set_storage() { - let mut ext = call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_set_storage"); + let mut ext = call_wasm_method::(&wasm_binary_unwrap()[..], "test_set_storage"); let expected = "world"; assert_eq!(expected.as_bytes(), &ext.ext().storage("hello".as_bytes()).unwrap()[..]); @@ -82,27 +82,27 @@ fn test_set_storage() { #[test] fn test_return_value_into_mutable_reference() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_return_value_into_mutable_reference"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_return_value_into_mutable_reference"); } #[test] fn test_get_and_return_array() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_get_and_return_array"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_get_and_return_array"); } #[test] fn test_array_as_mutable_reference() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_array_as_mutable_reference"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_array_as_mutable_reference"); } #[test] fn test_return_input_public_key() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_return_input_public_key"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_return_input_public_key"); } #[test] fn host_function_not_found() { - let err = call_wasm_method_with_result::<()>(&WASM_BINARY.unwrap()[..], "test_return_data").unwrap_err(); + let err = call_wasm_method_with_result::<()>(&wasm_binary_unwrap()[..], "test_return_data").unwrap_err(); assert!(err.contains("Instantiation: Export ")); assert!(err.contains(" not found")); @@ -111,46 +111,46 @@ fn host_function_not_found() { #[test] #[should_panic(expected = "Invalid utf8 data provided")] fn test_invalid_utf8_data_should_return_an_error() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_invalid_utf8_data_should_return_an_error"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_invalid_utf8_data_should_return_an_error"); } #[test] fn test_overwrite_native_function_implementation() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_overwrite_native_function_implementation"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_overwrite_native_function_implementation"); } #[test] fn test_u128_i128_as_parameter_and_return_value() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_u128_i128_as_parameter_and_return_value"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_u128_i128_as_parameter_and_return_value"); } #[test] fn test_vec_return_value_memory_is_freed() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_vec_return_value_memory_is_freed"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_vec_return_value_memory_is_freed"); } #[test] fn test_encoded_return_value_memory_is_freed() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_encoded_return_value_memory_is_freed"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_encoded_return_value_memory_is_freed"); } #[test] fn test_array_return_value_memory_is_freed() { - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_array_return_value_memory_is_freed"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_array_return_value_memory_is_freed"); } #[test] fn test_versionining_with_new_host_works() { // We call to the new wasm binary with new host function. call_wasm_method::( - &WASM_BINARY.unwrap()[..], + &wasm_binary_unwrap()[..], "test_versionning_works", ); // we call to the old wasm binary with a new host functions // old versions of host functions should be called and test should be ok! call_wasm_method::( - &WASM_BINARY_DEPRECATED.unwrap()[..], + &wasm_binary_deprecated_unwrap()[..], "test_versionning_works", ); } @@ -192,7 +192,7 @@ fn test_tracing() { let _guard = tracing::subscriber::set_default(subscriber.clone()); // Call some method to generate a trace - call_wasm_method::(&WASM_BINARY.unwrap()[..], "test_return_data"); + call_wasm_method::(&wasm_binary_unwrap()[..], "test_return_data"); let inner = subscriber.0.lock().unwrap(); assert!(inner.spans.contains("return_input_version_1")); diff --git a/test-utils/runtime/src/genesismap.rs b/test-utils/runtime/src/genesismap.rs index 687e1b427d510..119e8c2eb2fe5 100644 --- a/test-utils/runtime/src/genesismap.rs +++ b/test-utils/runtime/src/genesismap.rs @@ -19,7 +19,7 @@ use std::collections::BTreeMap; use sp_io::hashing::{blake2_256, twox_128}; -use super::{AuthorityId, AccountId, WASM_BINARY, system}; +use super::{AuthorityId, AccountId, wasm_binary_unwrap, system}; use codec::{Encode, KeyedVec, Joiner}; use sp_core::{ChangesTrieConfiguration, map}; use sp_core::storage::{well_known_keys, Storage}; @@ -55,7 +55,7 @@ impl GenesisConfig { } pub fn genesis_map(&self) -> Storage { - let wasm_runtime = WASM_BINARY.expect("Wasm binary must be built for testing").to_vec(); + let wasm_runtime = wasm_binary_unwrap().to_vec(); let mut map: BTreeMap, Vec> = self.balances.iter() .map(|&(ref account, balance)| (account.to_keyed_vec(b"balance:"), vec![].and(&balance))) .map(|(k, v)| (blake2_256(&k[..])[..].to_vec(), v.to_vec())) diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 06054c1240f3b..65202615fbf14 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -61,6 +61,13 @@ pub type AuraId = sp_consensus_aura::sr25519::AuthorityId; #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); +#[cfg(feature = "std")] +/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics. +pub fn wasm_binary_unwrap() -> &'static [u8] { + WASM_BINARY.expect("Development wasm binary is not available. Testing is only \ + supported with the flag disabled.") +} + /// Test runtime version. pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("test"), diff --git a/test-utils/runtime/src/system.rs b/test-utils/runtime/src/system.rs index c666a4705dccc..818487a89e518 100644 --- a/test-utils/runtime/src/system.rs +++ b/test-utils/runtime/src/system.rs @@ -342,7 +342,7 @@ mod tests { use sp_io::TestExternalities; use substrate_test_runtime_client::{AccountKeyring, Sr25519Keyring}; - use crate::{Header, Transfer, WASM_BINARY}; + use crate::{Header, Transfer, wasm_binary_unwrap}; use sp_core::{NeverNativeValue, map, traits::{CodeExecutor, RuntimeCode}}; use sc_executor::{NativeExecutor, WasmExecutionMethod, native_executor_instance}; use sp_io::hashing::twox_128; @@ -365,7 +365,7 @@ mod tests { Sr25519Keyring::Charlie.to_raw_public() ]; TestExternalities::new_with_code( - WASM_BINARY.unwrap(), + wasm_binary_unwrap(), sp_core::storage::Storage { top: map![ twox_128(b"latest").to_vec() => vec![69u8; 32], @@ -407,7 +407,7 @@ mod tests { block_import_works(|b, ext| { let mut ext = ext.ext(); let runtime_code = RuntimeCode { - code_fetcher: &sp_core::traits::WrappedRuntimeCode(WASM_BINARY.unwrap().into()), + code_fetcher: &sp_core::traits::WrappedRuntimeCode(wasm_binary_unwrap().into()), hash: Vec::new(), heap_pages: None, }; @@ -507,7 +507,7 @@ mod tests { block_import_with_transaction_works(|b, ext| { let mut ext = ext.ext(); let runtime_code = RuntimeCode { - code_fetcher: &sp_core::traits::WrappedRuntimeCode(WASM_BINARY.unwrap().into()), + code_fetcher: &sp_core::traits::WrappedRuntimeCode(wasm_binary_unwrap().into()), hash: Vec::new(), heap_pages: None, }; From a6ed08b611d28c6d11956abc31a3dc39b31690a5 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 7 Jul 2020 00:01:47 +0200 Subject: [PATCH 07/14] Add descriptive tests in node/executor benches --- bin/node/executor/benches/bench.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bin/node/executor/benches/bench.rs b/bin/node/executor/benches/bench.rs index 144b1e3043704..168cff0ff4568 100644 --- a/bin/node/executor/benches/bench.rs +++ b/bin/node/executor/benches/bench.rs @@ -36,7 +36,10 @@ criterion_group!(benches, bench_execute_block); criterion_main!(benches); /// The wasm runtime code. -const COMPACT_CODE: Option<&[u8]> = node_runtime::WASM_BINARY; +pub fn compact_code_unwrap() -> &'static [u8] { + node_runtime::WASM_BINARY.expect("Development wasm binary is not available. \ + Testing is only supported with the flag disabled.") +} const GENESIS_HASH: [u8; 32] = [69u8; 32]; @@ -60,7 +63,7 @@ fn sign(xt: CheckedExtrinsic) -> UncheckedExtrinsic { fn new_test_ext(genesis_config: &GenesisConfig) -> TestExternalities { let mut test_ext = TestExternalities::new_with_code( - COMPACT_CODE.unwrap(), + compact_code_unwrap(), genesis_config.build_storage().unwrap(), ); test_ext.ext().place_storage(well_known_keys::HEAP_PAGES.to_vec(), Some(HEAP_PAGES.encode())); @@ -94,7 +97,7 @@ fn construct_block( }; let runtime_code = RuntimeCode { - code_fetcher: &sp_core::traits::WrappedRuntimeCode(COMPACT_CODE.unwrap().into()), + code_fetcher: &sp_core::traits::WrappedRuntimeCode(compact_code_unwrap().into()), hash: vec![1, 2, 3], heap_pages: None, }; @@ -168,7 +171,7 @@ fn bench_execute_block(c: &mut Criterion) { c.bench_function_over_inputs( "execute blocks", |b, strategy| { - let genesis_config = node_testing::genesis::config(false, Some(COMPACT_CODE.unwrap())); + let genesis_config = node_testing::genesis::config(false, Some(compact_code_unwrap())); let (use_native, wasm_method) = match strategy { ExecutionMethod::Native => (true, WasmExecutionMethod::Interpreted), ExecutionMethod::Wasm(wasm_method) => (false, *wasm_method), @@ -176,7 +179,7 @@ fn bench_execute_block(c: &mut Criterion) { let executor = NativeExecutor::new(wasm_method, None, 8); let runtime_code = RuntimeCode { - code_fetcher: &sp_core::traits::WrappedRuntimeCode(COMPACT_CODE.unwrap().into()), + code_fetcher: &sp_core::traits::WrappedRuntimeCode(compact_code_unwrap().into()), hash: vec![1, 2, 3], heap_pages: None, }; From 5de1c86eadaf0f10a505ac30c2b2be1ba8e608ab Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 7 Jul 2020 00:02:42 +0200 Subject: [PATCH 08/14] Fix missing compact_code_unwrap --- bin/node/executor/tests/basic.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index 8282444346ace..05d6360a25ff5 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -78,7 +78,7 @@ fn set_heap_pages(ext: &mut E, heap_pages: u64) { fn changes_trie_block() -> (Vec, Hash) { construct_block( - &mut new_test_ext(COMPACT_CODE.unwrap(), true), + &mut new_test_ext(compact_code_unwrap(), true), 1, GENESIS_HASH.into(), vec![ @@ -98,7 +98,7 @@ fn changes_trie_block() -> (Vec, Hash) { /// are not guaranteed to be deterministic) and to ensure that the correct state is propagated /// from block1's execution to block2 to derive the correct storage_root. fn blocks() -> ((Vec, Hash), (Vec, Hash)) { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); let block1 = construct_block( &mut t, 1, @@ -143,7 +143,7 @@ fn blocks() -> ((Vec, Hash), (Vec, Hash)) { fn block_with_size(time: u64, nonce: u32, size: usize) -> (Vec, Hash) { construct_block( - &mut new_test_ext(COMPACT_CODE.unwrap(), false), + &mut new_test_ext(compact_code_unwrap(), false), 1, GENESIS_HASH.into(), vec![ @@ -190,7 +190,7 @@ fn panic_execution_with_foreign_code_gives_error() { #[test] fn bad_extrinsic_with_native_equivalent_code_gives_error() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 69u128, 0u128, 0u128, 0u128).encode() @@ -219,7 +219,7 @@ fn bad_extrinsic_with_native_equivalent_code_gives_error() { #[test] fn successful_execution_with_native_equivalent_code_gives_ok() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode() @@ -305,7 +305,7 @@ fn successful_execution_with_foreign_code_gives_ok() { #[test] fn full_native_block_import_works() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); let (block1, block2) = blocks(); @@ -442,7 +442,7 @@ fn full_native_block_import_works() { #[test] fn full_wasm_block_import_works() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); let (block1, block2) = blocks(); @@ -589,7 +589,7 @@ fn deploying_wasm_contract_should_work() { ); let b = construct_block( - &mut new_test_ext(COMPACT_CODE.unwrap(), false), + &mut new_test_ext(compact_code_unwrap(), false), 1, GENESIS_HASH.into(), vec![ @@ -628,7 +628,7 @@ fn deploying_wasm_contract_should_work() { ] ); - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); executor_call:: _>( &mut t, @@ -652,7 +652,7 @@ fn deploying_wasm_contract_should_work() { #[test] fn wasm_big_block_import_fails() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); set_heap_pages(&mut t.ext(), 4); @@ -668,7 +668,7 @@ fn wasm_big_block_import_fails() { #[test] fn native_big_block_import_succeeds() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); executor_call:: _>( &mut t, @@ -681,7 +681,7 @@ fn native_big_block_import_succeeds() { #[test] fn native_big_block_import_fails_on_fallback() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); assert!( executor_call:: _>( @@ -725,7 +725,7 @@ fn panic_execution_gives_error() { #[test] fn successful_execution_gives_ok() { - let mut t = new_test_ext(COMPACT_CODE.unwrap(), false); + let mut t = new_test_ext(compact_code_unwrap(), false); t.insert( >::hashed_key_for(alice()), (0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode() @@ -778,7 +778,7 @@ fn full_native_block_import_works_with_changes_trie() { let block_data = block1.0; let block = Block::decode(&mut &block_data[..]).unwrap(); - let mut t = new_test_ext(COMPACT_CODE.unwrap(), true); + let mut t = new_test_ext(compact_code_unwrap(), true); executor_call:: _>( &mut t, "Core_execute_block", @@ -794,7 +794,7 @@ fn full_native_block_import_works_with_changes_trie() { fn full_wasm_block_import_works_with_changes_trie() { let block1 = changes_trie_block(); - let mut t = new_test_ext(COMPACT_CODE.unwrap(), true); + let mut t = new_test_ext(compact_code_unwrap(), true); executor_call:: _>( &mut t, "Core_execute_block", From d2af95924bd96d421d5cf904c68c834f8c50ac42 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 7 Jul 2020 00:38:05 +0200 Subject: [PATCH 09/14] Add missing wasm_binary_unwrap function for executor integration test --- client/executor/runtime-test/src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/client/executor/runtime-test/src/lib.rs b/client/executor/runtime-test/src/lib.rs index 4962c558eaa2d..1deb196352e8d 100644 --- a/client/executor/runtime-test/src/lib.rs +++ b/client/executor/runtime-test/src/lib.rs @@ -4,6 +4,13 @@ #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); +#[cfg(feature = "std")] +/// Wasm binary unwrapped. If built with `BUILD_DUMMY_WASM_BINARY`, the function panics. +pub fn wasm_binary_unwrap() -> &'static [u8] { + WASM_BINARY.expect("Development wasm binary is not available. Testing is only \ + supported with the flag disabled.") +} + #[cfg(not(feature = "std"))] use sp_std::{vec::Vec, vec}; From 0ca9fc7d6680070e1eac7de29887428e6fc809de Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 7 Jul 2020 02:34:26 +0200 Subject: [PATCH 10/14] Only define import_sp_io in no_std --- primitives/runtime-interface/test-wasm-deprecated/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs b/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs index 19b4aaececcc4..174cdb8cdf85a 100644 --- a/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs +++ b/primitives/runtime-interface/test-wasm-deprecated/src/lib.rs @@ -35,6 +35,7 @@ pub fn wasm_binary_unwrap() -> &'static [u8] { /// This function is not used, but we require it for the compiler to include `sp-io`. /// `sp-io` is required for its panic and oom handler. +#[cfg(not(feature = "std"))] #[no_mangle] pub fn import_sp_io() { sp_io::misc::print_utf8(&[]); From a08229247b8e27780afa8769d816b4830f135c5d Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 7 Jul 2020 08:59:32 +0200 Subject: [PATCH 11/14] Small Cargo.toml styling fix --- client/network/Cargo.toml | 2 -- frame/support/test/Cargo.toml | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index 495895c7401d0..9e486d349129f 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -9,11 +9,9 @@ homepage = "https://substrate.dev" repository = "https://github.com/paritytech/substrate/" documentation = "https://docs.rs/sc-network" - [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] - [build-dependencies] prost-build = "0.6.1" diff --git a/frame/support/test/Cargo.toml b/frame/support/test/Cargo.toml index 682001564bda2..313b6251970c4 100644 --- a/frame/support/test/Cargo.toml +++ b/frame/support/test/Cargo.toml @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] serde = { version = "1.0.101", default-features = false, features = ["derive"] } codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false, features = ["derive"] } -sp-io ={ version = "2.0.0-rc4", path = "../../../primitives/io", default-features = false } +sp-io = { version = "2.0.0-rc4", path = "../../../primitives/io", default-features = false } sp-state-machine = { version = "0.8.0-rc4", optional = true, path = "../../../primitives/state-machine" } frame-support = { version = "2.0.0-rc4", default-features = false, path = "../" } sp-inherents = { version = "2.0.0-rc4", default-features = false, path = "../../../primitives/inherents" } From b2f2601a451d2983e04b5c45373677caac70d787 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 13 Jul 2020 08:03:27 +0200 Subject: [PATCH 12/14] Bump wasm-builder to 2.0.0 --- Cargo.lock | 2 +- utils/wasm-builder/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0b24f9ef57218..243f98ca87e8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8510,7 +8510,7 @@ version = "2.0.0-rc4" [[package]] name = "substrate-wasm-builder" -version = "1.0.11" +version = "2.0.0" dependencies = [ "atty", "build-helper", diff --git a/utils/wasm-builder/Cargo.toml b/utils/wasm-builder/Cargo.toml index e46db432689e0..5e90625620508 100644 --- a/utils/wasm-builder/Cargo.toml +++ b/utils/wasm-builder/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-wasm-builder" -version = "1.0.11" +version = "2.0.0" authors = ["Parity Technologies "] description = "Utility for building WASM binaries" edition = "2018" From f3d54161875e811d550ae2dfb71de0100b721699 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 13 Jul 2020 08:04:57 +0200 Subject: [PATCH 13/14] Fix all `with_wasm_builder_from_crates` version in Substrate --- bin/node-template/runtime/build.rs | 2 +- bin/node/runtime/build.rs | 2 +- client/executor/runtime-test/build.rs | 2 +- primitives/runtime-interface/test-wasm-deprecated/build.rs | 2 +- primitives/runtime-interface/test-wasm/build.rs | 2 +- test-utils/runtime/build.rs | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/node-template/runtime/build.rs b/bin/node-template/runtime/build.rs index 1ef107b7463a8..0d66f7a64c7b9 100644 --- a/bin/node-template/runtime/build.rs +++ b/bin/node-template/runtime/build.rs @@ -3,7 +3,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("1.0.11", "../../../utils/wasm-builder") + .with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder") .export_heap_base() .import_memory() .build() diff --git a/bin/node/runtime/build.rs b/bin/node/runtime/build.rs index a4f323566006c..a2f09a460e69d 100644 --- a/bin/node/runtime/build.rs +++ b/bin/node/runtime/build.rs @@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("1.0.11", "../../../utils/wasm-builder") + .with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder") .export_heap_base() .import_memory() .build() diff --git a/client/executor/runtime-test/build.rs b/client/executor/runtime-test/build.rs index c5f1f2402bd8b..1ed5aa44bc5c4 100644 --- a/client/executor/runtime-test/build.rs +++ b/client/executor/runtime-test/build.rs @@ -19,7 +19,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("1.0.11", "../../../utils/wasm-builder") + .with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder") .export_heap_base() .import_memory() .build() diff --git a/primitives/runtime-interface/test-wasm-deprecated/build.rs b/primitives/runtime-interface/test-wasm-deprecated/build.rs index a4f323566006c..a2f09a460e69d 100644 --- a/primitives/runtime-interface/test-wasm-deprecated/build.rs +++ b/primitives/runtime-interface/test-wasm-deprecated/build.rs @@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("1.0.11", "../../../utils/wasm-builder") + .with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder") .export_heap_base() .import_memory() .build() diff --git a/primitives/runtime-interface/test-wasm/build.rs b/primitives/runtime-interface/test-wasm/build.rs index a4f323566006c..a2f09a460e69d 100644 --- a/primitives/runtime-interface/test-wasm/build.rs +++ b/primitives/runtime-interface/test-wasm/build.rs @@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("1.0.11", "../../../utils/wasm-builder") + .with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder") .export_heap_base() .import_memory() .build() diff --git a/test-utils/runtime/build.rs b/test-utils/runtime/build.rs index 69ff73a3ffc84..6082738de419d 100644 --- a/test-utils/runtime/build.rs +++ b/test-utils/runtime/build.rs @@ -20,7 +20,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("1.0.11", "../../utils/wasm-builder") + .with_wasm_builder_from_crates_or_path("2.0.0", "../../utils/wasm-builder") .export_heap_base() // Note that we set the stack-size to 1MB explicitly even though it is set // to this value by default. This is because some of our tests (`restoration_of_globals`) From 2103bed22b22640a9aaff6bbd82a46345ad04b39 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 13 Jul 2020 08:21:52 +0200 Subject: [PATCH 14/14] Use `with_wasm_builder_from_crates` for node-template --- bin/node-template/runtime/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/node-template/runtime/build.rs b/bin/node-template/runtime/build.rs index 0d66f7a64c7b9..52705043a2019 100644 --- a/bin/node-template/runtime/build.rs +++ b/bin/node-template/runtime/build.rs @@ -3,7 +3,7 @@ use wasm_builder_runner::WasmBuilder; fn main() { WasmBuilder::new() .with_current_project() - .with_wasm_builder_from_crates_or_path("2.0.0", "../../../utils/wasm-builder") + .with_wasm_builder_from_crates("2.0.0") .export_heap_base() .import_memory() .build()