Skip to content

Commit f2fe6a4

Browse files
yjhmelodybkchrmichalkucharczyk
authored
Improve CodeExecutor (paritytech#2358)
Since `sp-state-machine` and `GenesisConfigBuilderRuntimeCaller` always set `use_native` to be false. We should remove this param and make `NativeElseWasmExecutor` behave like its name. It could make the above components use the correct execution strategy. Maybe polkadot do not need about `NativeElseWasmExecutor` anymore. But it is still needed by other chains and it's useful for debugging. --------- Co-authored-by: Bastian Köcher <git@kchr.de> Co-authored-by: command-bot <> Co-authored-by: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com>
1 parent 63ac247 commit f2fe6a4

9 files changed

Lines changed: 56 additions & 81 deletions

File tree

substrate/bin/node/cli/benches/executor.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,7 @@ fn construct_block<E: Externalities>(
103103

104104
// execute the block to get the real header.
105105
executor
106-
.call(
107-
ext,
108-
&runtime_code,
109-
"Core_initialize_block",
110-
&header.encode(),
111-
true,
112-
CallContext::Offchain,
113-
)
106+
.call(ext, &runtime_code, "Core_initialize_block", &header.encode(), CallContext::Offchain)
114107
.0
115108
.unwrap();
116109

@@ -121,7 +114,6 @@ fn construct_block<E: Externalities>(
121114
&runtime_code,
122115
"BlockBuilder_apply_extrinsic",
123116
&i.encode(),
124-
true,
125117
CallContext::Offchain,
126118
)
127119
.0
@@ -135,7 +127,6 @@ fn construct_block<E: Externalities>(
135127
&runtime_code,
136128
"BlockBuilder_finalize_block",
137129
&[0u8; 0],
138-
true,
139130
CallContext::Offchain,
140131
)
141132
.0
@@ -200,7 +191,6 @@ fn bench_execute_block(c: &mut Criterion) {
200191
&runtime_code,
201192
"Core_execute_block",
202193
&block.0,
203-
false,
204194
CallContext::Offchain,
205195
)
206196
.0

substrate/bin/node/cli/tests/basic.rs

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,9 @@ fn panic_execution_with_foreign_code_gives_error() {
193193
t.insert(<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(), 69_u128.encode());
194194
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
195195

196-
let r =
197-
executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32)), true)
198-
.0;
196+
let r = executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32))).0;
199197
assert!(r.is_ok());
200-
let v = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt()), true)
198+
let v = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt()))
201199
.0
202200
.unwrap();
203201
let r = ApplyExtrinsicResult::decode(&mut &v[..]).unwrap();
@@ -219,11 +217,9 @@ fn bad_extrinsic_with_native_equivalent_code_gives_error() {
219217
t.insert(<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(), 69u128.encode());
220218
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
221219

222-
let r =
223-
executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32)), true)
224-
.0;
220+
let r = executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32))).0;
225221
assert!(r.is_ok());
226-
let v = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt()), true)
222+
let v = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt()))
227223
.0
228224
.unwrap();
229225
let r = ApplyExtrinsicResult::decode(&mut &v[..]).unwrap();
@@ -256,14 +252,12 @@ fn successful_execution_with_native_equivalent_code_gives_ok() {
256252
);
257253
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
258254

259-
let r =
260-
executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32)), true)
261-
.0;
255+
let r = executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32))).0;
262256
assert!(r.is_ok());
263257

264258
let fees = t.execute_with(|| transfer_fee(&xt()));
265259

266-
let r = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt()), true).0;
260+
let r = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt())).0;
267261
assert!(r.is_ok());
268262

269263
t.execute_with(|| {
@@ -298,14 +292,12 @@ fn successful_execution_with_foreign_code_gives_ok() {
298292
);
299293
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
300294

301-
let r =
302-
executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32)), true)
303-
.0;
295+
let r = executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32))).0;
304296
assert!(r.is_ok());
305297

306298
let fees = t.execute_with(|| transfer_fee(&xt()));
307299

308-
let r = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt()), true).0;
300+
let r = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt())).0;
309301
assert!(r.is_ok());
310302

311303
t.execute_with(|| {
@@ -337,7 +329,7 @@ fn full_native_block_import_works() {
337329
.base_extrinsic,
338330
);
339331

340-
executor_call(&mut t, "Core_execute_block", &block1.0, true).0.unwrap();
332+
executor_call(&mut t, "Core_execute_block", &block1.0).0.unwrap();
341333

342334
t.execute_with(|| {
343335
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - fees);
@@ -412,7 +404,7 @@ fn full_native_block_import_works() {
412404
fees = t.execute_with(|| transfer_fee(&xt()));
413405
let pot = t.execute_with(|| Treasury::pot());
414406

415-
executor_call(&mut t, "Core_execute_block", &block2.0, true).0.unwrap();
407+
executor_call(&mut t, "Core_execute_block", &block2.0).0.unwrap();
416408

417409
t.execute_with(|| {
418410
assert_eq!(
@@ -554,7 +546,7 @@ fn full_wasm_block_import_works() {
554546
let mut alice_last_known_balance: Balance = Default::default();
555547
let mut fees = t.execute_with(|| transfer_fee(&xt()));
556548

557-
executor_call(&mut t, "Core_execute_block", &block1.0, false).0.unwrap();
549+
executor_call(&mut t, "Core_execute_block", &block1.0).0.unwrap();
558550

559551
t.execute_with(|| {
560552
assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - fees);
@@ -564,7 +556,7 @@ fn full_wasm_block_import_works() {
564556

565557
fees = t.execute_with(|| transfer_fee(&xt()));
566558

567-
executor_call(&mut t, "Core_execute_block", &block2.0, false).0.unwrap();
559+
executor_call(&mut t, "Core_execute_block", &block2.0).0.unwrap();
568560

569561
t.execute_with(|| {
570562
assert_eq!(
@@ -717,7 +709,7 @@ fn deploying_wasm_contract_should_work() {
717709

718710
let mut t = new_test_ext(compact_code_unwrap());
719711

720-
executor_call(&mut t, "Core_execute_block", &b.0, false).0.unwrap();
712+
executor_call(&mut t, "Core_execute_block", &b.0).0.unwrap();
721713

722714
t.execute_with(|| {
723715
// Verify that the contract does exist by querying some of its storage items
@@ -732,16 +724,15 @@ fn wasm_big_block_import_fails() {
732724

733725
set_heap_pages(&mut t.ext(), 4);
734726

735-
let result =
736-
executor_call(&mut t, "Core_execute_block", &block_with_size(42, 0, 120_000).0, false).0;
727+
let result = executor_call(&mut t, "Core_execute_block", &block_with_size(42, 0, 120_000).0).0;
737728
assert!(result.is_err()); // Err(Wasmi(Trap(Trap { kind: Host(AllocatorOutOfSpace) })))
738729
}
739730

740731
#[test]
741732
fn native_big_block_import_succeeds() {
742733
let mut t = new_test_ext(compact_code_unwrap());
743734

744-
executor_call(&mut t, "Core_execute_block", &block_with_size(42, 0, 120_000).0, true)
735+
executor_call(&mut t, "Core_execute_block", &block_with_size(42, 0, 120_000).0)
745736
.0
746737
.unwrap();
747738
}
@@ -754,11 +745,9 @@ fn native_big_block_import_fails_on_fallback() {
754745
// block.
755746
set_heap_pages(&mut t.ext(), 8);
756747

757-
assert!(
758-
executor_call(&mut t, "Core_execute_block", &block_with_size(42, 0, 120_000).0, false,)
759-
.0
760-
.is_err()
761-
);
748+
assert!(executor_call(&mut t, "Core_execute_block", &block_with_size(42, 0, 120_000).0)
749+
.0
750+
.is_err());
762751
}
763752

764753
#[test]
@@ -775,15 +764,9 @@ fn panic_execution_gives_error() {
775764
t.insert(<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(), 0_u128.encode());
776765
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
777766

778-
let r = executor_call(
779-
&mut t,
780-
"Core_initialize_block",
781-
&vec![].and(&from_block_number(1u32)),
782-
false,
783-
)
784-
.0;
767+
let r = executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32))).0;
785768
assert!(r.is_ok());
786-
let r = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt()), false)
769+
let r = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt()))
787770
.0
788771
.unwrap();
789772
let r = ApplyExtrinsicResult::decode(&mut &r[..]).unwrap();
@@ -816,21 +799,15 @@ fn successful_execution_gives_ok() {
816799
);
817800
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
818801

819-
let r = executor_call(
820-
&mut t,
821-
"Core_initialize_block",
822-
&vec![].and(&from_block_number(1u32)),
823-
false,
824-
)
825-
.0;
802+
let r = executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32))).0;
826803
assert!(r.is_ok());
827804
t.execute_with(|| {
828805
assert_eq!(Balances::total_balance(&alice()), 111 * DOLLARS);
829806
});
830807

831808
let fees = t.execute_with(|| transfer_fee(&xt()));
832809

833-
let r = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt()), false)
810+
let r = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt()))
834811
.0
835812
.unwrap();
836813
ApplyExtrinsicResult::decode(&mut &r[..])
@@ -861,7 +838,7 @@ fn should_import_block_with_test_client() {
861838
#[test]
862839
fn default_config_as_json_works() {
863840
let mut t = new_test_ext(compact_code_unwrap());
864-
let r = executor_call(&mut t, "GenesisBuilder_create_default_config", &vec![], false)
841+
let r = executor_call(&mut t, "GenesisBuilder_create_default_config", &vec![])
865842
.0
866843
.unwrap();
867844
let r = Vec::<u8>::decode(&mut &r[..]).unwrap();

substrate/bin/node/cli/tests/common.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ pub fn executor_call(
105105
t: &mut TestExternalities<BlakeTwo256>,
106106
method: &str,
107107
data: &[u8],
108-
use_native: bool,
109108
) -> (Result<Vec<u8>>, bool) {
110109
let mut t = t.ext();
111110

@@ -117,7 +116,7 @@ pub fn executor_call(
117116
heap_pages: heap_pages.and_then(|hp| Decode::decode(&mut &hp[..]).ok()),
118117
};
119118
sp_tracing::try_init_simple();
120-
executor().call(&mut t, &runtime_code, method, data, use_native, CallContext::Onchain)
119+
executor().call(&mut t, &runtime_code, method, data, CallContext::Onchain)
121120
}
122121

123122
pub fn new_test_ext(code: &[u8]) -> TestExternalities<BlakeTwo256> {
@@ -168,12 +167,12 @@ pub fn construct_block(
168167
};
169168

170169
// execute the block to get the real header.
171-
executor_call(env, "Core_initialize_block", &header.encode(), true).0.unwrap();
170+
executor_call(env, "Core_initialize_block", &header.encode()).0.unwrap();
172171

173172
for extrinsic in extrinsics.iter() {
174173
// Try to apply the `extrinsic`. It should be valid, in the sense that it passes
175174
// all pre-inclusion checks.
176-
let r = executor_call(env, "BlockBuilder_apply_extrinsic", &extrinsic.encode(), true)
175+
let r = executor_call(env, "BlockBuilder_apply_extrinsic", &extrinsic.encode())
177176
.0
178177
.expect("application of an extrinsic failed");
179178

@@ -186,7 +185,7 @@ pub fn construct_block(
186185
}
187186

188187
let header = Header::decode(
189-
&mut &executor_call(env, "BlockBuilder_finalize_block", &[0u8; 0], true).0.unwrap()[..],
188+
&mut &executor_call(env, "BlockBuilder_finalize_block", &[0u8; 0]).0.unwrap()[..],
190189
)
191190
.unwrap();
192191

substrate/bin/node/cli/tests/fees.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() {
9595
);
9696

9797
// execute a big block.
98-
executor_call(&mut t, "Core_execute_block", &block1.0, true).0.unwrap();
98+
executor_call(&mut t, "Core_execute_block", &block1.0).0.unwrap();
9999

100100
// weight multiplier is increased for next block.
101101
t.execute_with(|| {
@@ -106,7 +106,7 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() {
106106
});
107107

108108
// execute a big block.
109-
executor_call(&mut t, "Core_execute_block", &block2.0, true).0.unwrap();
109+
executor_call(&mut t, "Core_execute_block", &block2.0).0.unwrap();
110110

111111
// weight multiplier is increased for next block.
112112
t.execute_with(|| {
@@ -151,12 +151,10 @@ fn transaction_fee_is_correct() {
151151
function: RuntimeCall::Balances(default_transfer_call()),
152152
});
153153

154-
let r =
155-
executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32)), true)
156-
.0;
154+
let r = executor_call(&mut t, "Core_initialize_block", &vec![].and(&from_block_number(1u32))).0;
157155

158156
assert!(r.is_ok());
159-
let r = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt.clone()), true).0;
157+
let r = executor_call(&mut t, "BlockBuilder_apply_extrinsic", &vec![].and(&xt.clone())).0;
160158
assert!(r.is_ok());
161159

162160
t.execute_with(|| {
@@ -247,7 +245,7 @@ fn block_weight_capacity_report() {
247245
len / 1024 / 1024,
248246
);
249247

250-
let r = executor_call(&mut t, "Core_execute_block", &block.0, true).0;
248+
let r = executor_call(&mut t, "Core_execute_block", &block.0).0;
251249

252250
println!(" || Result = {:?}", r);
253251
assert!(r.is_ok());
@@ -310,7 +308,7 @@ fn block_length_capacity_report() {
310308
len / 1024 / 1024,
311309
);
312310

313-
let r = executor_call(&mut t, "Core_execute_block", &block.0, true).0;
311+
let r = executor_call(&mut t, "Core_execute_block", &block.0).0;
314312

315313
println!(" || Result = {:?}", r);
316314
assert!(r.is_ok());

substrate/client/chain-spec/src/genesis_config_builder.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ where
7676
&RuntimeCode { heap_pages: None, code_fetcher: self, hash: self.code_hash.clone() },
7777
method,
7878
data,
79-
false,
8079
CallContext::Offchain,
8180
)
8281
.0

substrate/client/executor/src/executor.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,6 @@ where
492492
runtime_code: &RuntimeCode,
493493
method: &str,
494494
data: &[u8],
495-
_use_native: bool,
496495
context: CallContext,
497496
) -> (Result<Vec<u8>>, bool) {
498497
tracing::trace!(
@@ -565,6 +564,8 @@ pub struct NativeElseWasmExecutor<D: NativeExecutionDispatch> {
565564
/// Fallback wasm executor.
566565
wasm:
567566
WasmExecutor<ExtendedHostFunctions<sp_io::SubstrateHostFunctions, D::ExtendHostFunctions>>,
567+
568+
use_native: bool,
568569
}
569570

570571
impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
@@ -601,7 +602,7 @@ impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
601602
.with_runtime_cache_size(runtime_cache_size)
602603
.build();
603604

604-
NativeElseWasmExecutor { native_version: D::native_version(), wasm }
605+
NativeElseWasmExecutor { native_version: D::native_version(), wasm, use_native: true }
605606
}
606607

607608
/// Create a new instance using the given [`WasmExecutor`].
@@ -610,7 +611,14 @@ impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
610611
ExtendedHostFunctions<sp_io::SubstrateHostFunctions, D::ExtendHostFunctions>,
611612
>,
612613
) -> Self {
613-
Self { native_version: D::native_version(), wasm: executor }
614+
Self { native_version: D::native_version(), wasm: executor, use_native: true }
615+
}
616+
617+
/// Disable to use native runtime when possible just behave like `WasmExecutor`.
618+
///
619+
/// Default to enabled.
620+
pub fn disable_use_native(&mut self) {
621+
self.use_native = false;
614622
}
615623

616624
/// Ignore missing function imports if set true.
@@ -645,9 +653,10 @@ impl<D: NativeExecutionDispatch + 'static> CodeExecutor for NativeElseWasmExecut
645653
runtime_code: &RuntimeCode,
646654
method: &str,
647655
data: &[u8],
648-
use_native: bool,
649656
context: CallContext,
650657
) -> (Result<Vec<u8>>, bool) {
658+
let use_native = self.use_native;
659+
651660
tracing::trace!(
652661
target: "executor",
653662
function = %method,
@@ -711,7 +720,11 @@ impl<D: NativeExecutionDispatch + 'static> CodeExecutor for NativeElseWasmExecut
711720

712721
impl<D: NativeExecutionDispatch> Clone for NativeElseWasmExecutor<D> {
713722
fn clone(&self) -> Self {
714-
NativeElseWasmExecutor { native_version: D::native_version(), wasm: self.wasm.clone() }
723+
NativeElseWasmExecutor {
724+
native_version: D::native_version(),
725+
wasm: self.wasm.clone(),
726+
use_native: self.use_native,
727+
}
715728
}
716729
}
717730

0 commit comments

Comments
 (0)