1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{
	error::{Error, Result},
	wasm_runtime::{RuntimeCache, WasmExecutionMethod},
	RuntimeVersionOf,
};

use std::{
	marker::PhantomData,
	panic::{AssertUnwindSafe, UnwindSafe},
	path::PathBuf,
	sync::Arc,
};

use codec::Encode;
use sc_executor_common::{
	runtime_blob::RuntimeBlob,
	wasm_runtime::{
		AllocationStats, HeapAllocStrategy, WasmInstance, WasmModule, DEFAULT_HEAP_ALLOC_STRATEGY,
	},
};
use sp_core::traits::{CallContext, CodeExecutor, Externalities, RuntimeCode};
use sp_version::{GetNativeVersion, NativeVersion, RuntimeVersion};
use sp_wasm_interface::{ExtendedHostFunctions, HostFunctions};

/// Set up the externalities and safe calling environment to execute runtime calls.
///
/// If the inner closure panics, it will be caught and return an error.
pub fn with_externalities_safe<F, U>(ext: &mut dyn Externalities, f: F) -> Result<U>
where
	F: UnwindSafe + FnOnce() -> U,
{
	sp_externalities::set_and_run_with_externalities(ext, move || {
		// Substrate uses custom panic hook that terminates process on panic. Disable
		// termination for the native call.
		let _guard = sp_panic_handler::AbortGuard::force_unwind();
		std::panic::catch_unwind(f).map_err(|e| {
			if let Some(err) = e.downcast_ref::<String>() {
				Error::RuntimePanicked(err.clone())
			} else if let Some(err) = e.downcast_ref::<&'static str>() {
				Error::RuntimePanicked(err.to_string())
			} else {
				Error::RuntimePanicked("Unknown panic".into())
			}
		})
	})
}

/// Delegate for dispatching a CodeExecutor call.
///
/// By dispatching we mean that we execute a runtime function specified by it's name.
pub trait NativeExecutionDispatch: Send + Sync {
	/// Host functions for custom runtime interfaces that should be callable from within the runtime
	/// besides the default Substrate runtime interfaces.
	type ExtendHostFunctions: HostFunctions;

	/// Dispatch a method in the runtime.
	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>>;

	/// Provide native runtime version.
	fn native_version() -> NativeVersion;
}

fn unwrap_heap_pages(pages: Option<HeapAllocStrategy>) -> HeapAllocStrategy {
	pages.unwrap_or_else(|| DEFAULT_HEAP_ALLOC_STRATEGY)
}

/// Builder for creating a [`WasmExecutor`] instance.
pub struct WasmExecutorBuilder<H = sp_io::SubstrateHostFunctions> {
	_phantom: PhantomData<H>,
	method: WasmExecutionMethod,
	onchain_heap_alloc_strategy: Option<HeapAllocStrategy>,
	offchain_heap_alloc_strategy: Option<HeapAllocStrategy>,
	ignore_onchain_heap_pages: bool,
	max_runtime_instances: usize,
	cache_path: Option<PathBuf>,
	allow_missing_host_functions: bool,
	runtime_cache_size: u8,
}

impl<H> WasmExecutorBuilder<H> {
	/// Create a new instance of `Self`
	///
	/// - `method`: The wasm execution method that should be used by the executor.
	pub fn new() -> Self {
		Self {
			_phantom: PhantomData,
			method: WasmExecutionMethod::default(),
			onchain_heap_alloc_strategy: None,
			offchain_heap_alloc_strategy: None,
			ignore_onchain_heap_pages: false,
			max_runtime_instances: 2,
			runtime_cache_size: 4,
			allow_missing_host_functions: false,
			cache_path: None,
		}
	}

	/// Create the wasm executor with execution method that should be used by the executor.
	pub fn with_execution_method(mut self, method: WasmExecutionMethod) -> Self {
		self.method = method;
		self
	}

	/// Create the wasm executor with the given number of `heap_alloc_strategy` for onchain runtime
	/// calls.
	pub fn with_onchain_heap_alloc_strategy(
		mut self,
		heap_alloc_strategy: HeapAllocStrategy,
	) -> Self {
		self.onchain_heap_alloc_strategy = Some(heap_alloc_strategy);
		self
	}

	/// Create the wasm executor with the given number of `heap_alloc_strategy` for offchain runtime
	/// calls.
	pub fn with_offchain_heap_alloc_strategy(
		mut self,
		heap_alloc_strategy: HeapAllocStrategy,
	) -> Self {
		self.offchain_heap_alloc_strategy = Some(heap_alloc_strategy);
		self
	}

	/// Create the wasm executor and follow/ignore onchain heap pages value.
	///
	/// By default this the onchain heap pages value is followed.
	pub fn with_ignore_onchain_heap_pages(mut self, ignore_onchain_heap_pages: bool) -> Self {
		self.ignore_onchain_heap_pages = ignore_onchain_heap_pages;
		self
	}

	/// Create the wasm executor with the given maximum number of `instances`.
	///
	/// The number of `instances` defines how many different instances of a runtime the cache is
	/// storing.
	///
	/// By default the maximum number of `instances` is `2`.
	pub fn with_max_runtime_instances(mut self, instances: usize) -> Self {
		self.max_runtime_instances = instances;
		self
	}

	/// Create the wasm executor with the given `cache_path`.
	///
	/// The `cache_path` is A path to a directory where the executor can place its files for
	/// purposes of caching. This may be important in cases when there are many different modules
	/// with the compiled execution method is used.
	///
	/// By default there is no `cache_path` given.
	pub fn with_cache_path(mut self, cache_path: impl Into<PathBuf>) -> Self {
		self.cache_path = Some(cache_path.into());
		self
	}

	/// Create the wasm executor and allow/forbid missing host functions.
	///
	/// If missing host functions are forbidden, the instantiation of a wasm blob will fail
	/// for imported host functions that the executor is not aware of. If they are allowed,
	/// a stub is generated that will return an error when being called while executing the wasm.
	///
	/// By default missing host functions are forbidden.
	pub fn with_allow_missing_host_functions(mut self, allow: bool) -> Self {
		self.allow_missing_host_functions = allow;
		self
	}

	/// Create the wasm executor with the given `runtime_cache_size`.
	///
	/// Defines the number of different runtimes/instantiated wasm blobs the cache stores.
	/// Runtimes/wasm blobs are differentiated based on the hash and the number of heap pages.
	///
	/// By default this value is set to `4`.
	pub fn with_runtime_cache_size(mut self, runtime_cache_size: u8) -> Self {
		self.runtime_cache_size = runtime_cache_size;
		self
	}

	/// Build the configured [`WasmExecutor`].
	pub fn build(self) -> WasmExecutor<H> {
		WasmExecutor {
			method: self.method,
			default_offchain_heap_alloc_strategy: unwrap_heap_pages(
				self.offchain_heap_alloc_strategy,
			),
			default_onchain_heap_alloc_strategy: unwrap_heap_pages(
				self.onchain_heap_alloc_strategy,
			),
			ignore_onchain_heap_pages: self.ignore_onchain_heap_pages,
			cache: Arc::new(RuntimeCache::new(
				self.max_runtime_instances,
				self.cache_path.clone(),
				self.runtime_cache_size,
			)),
			cache_path: self.cache_path,
			allow_missing_host_functions: self.allow_missing_host_functions,
			phantom: PhantomData,
		}
	}
}

/// An abstraction over Wasm code executor. Supports selecting execution backend and
/// manages runtime cache.
pub struct WasmExecutor<H = sp_io::SubstrateHostFunctions> {
	/// Method used to execute fallback Wasm code.
	method: WasmExecutionMethod,
	/// The heap allocation strategy for onchain Wasm calls.
	default_onchain_heap_alloc_strategy: HeapAllocStrategy,
	/// The heap allocation strategy for offchain Wasm calls.
	default_offchain_heap_alloc_strategy: HeapAllocStrategy,
	/// Ignore onchain heap pages value.
	ignore_onchain_heap_pages: bool,
	/// WASM runtime cache.
	cache: Arc<RuntimeCache>,
	/// The path to a directory which the executor can leverage for a file cache, e.g. put there
	/// compiled artifacts.
	cache_path: Option<PathBuf>,
	/// Ignore missing function imports.
	allow_missing_host_functions: bool,
	phantom: PhantomData<H>,
}

impl<H> Clone for WasmExecutor<H> {
	fn clone(&self) -> Self {
		Self {
			method: self.method,
			default_onchain_heap_alloc_strategy: self.default_onchain_heap_alloc_strategy,
			default_offchain_heap_alloc_strategy: self.default_offchain_heap_alloc_strategy,
			ignore_onchain_heap_pages: self.ignore_onchain_heap_pages,
			cache: self.cache.clone(),
			cache_path: self.cache_path.clone(),
			allow_missing_host_functions: self.allow_missing_host_functions,
			phantom: self.phantom,
		}
	}
}

impl Default for WasmExecutor<sp_io::SubstrateHostFunctions> {
	fn default() -> Self {
		WasmExecutorBuilder::new().build()
	}
}

impl<H> WasmExecutor<H> {
	/// Create new instance.
	///
	/// # Parameters
	///
	/// `method` - Method used to execute Wasm code.
	///
	/// `default_heap_pages` - Number of 64KB pages to allocate for Wasm execution. Internally this
	/// will be mapped as [`HeapAllocStrategy::Static`] where `default_heap_pages` represent the
	/// static number of heap pages to allocate. Defaults to `DEFAULT_HEAP_ALLOC_STRATEGY` if `None`
	/// is provided.
	///
	/// `max_runtime_instances` - The number of runtime instances to keep in memory ready for reuse.
	///
	/// `cache_path` - A path to a directory where the executor can place its files for purposes of
	///   caching. This may be important in cases when there are many different modules with the
	///   compiled execution method is used.
	///
	/// `runtime_cache_size` - The capacity of runtime cache.
	#[deprecated(note = "use `Self::builder` method instead of it")]
	pub fn new(
		method: WasmExecutionMethod,
		default_heap_pages: Option<u64>,
		max_runtime_instances: usize,
		cache_path: Option<PathBuf>,
		runtime_cache_size: u8,
	) -> Self {
		WasmExecutor {
			method,
			default_onchain_heap_alloc_strategy: unwrap_heap_pages(
				default_heap_pages.map(|h| HeapAllocStrategy::Static { extra_pages: h as _ }),
			),
			default_offchain_heap_alloc_strategy: unwrap_heap_pages(
				default_heap_pages.map(|h| HeapAllocStrategy::Static { extra_pages: h as _ }),
			),
			ignore_onchain_heap_pages: false,
			cache: Arc::new(RuntimeCache::new(
				max_runtime_instances,
				cache_path.clone(),
				runtime_cache_size,
			)),
			cache_path,
			allow_missing_host_functions: false,
			phantom: PhantomData,
		}
	}

	/// Instantiate a builder for creating an instance of `Self`.
	pub fn builder() -> WasmExecutorBuilder<H> {
		WasmExecutorBuilder::new()
	}

	/// Ignore missing function imports if set true.
	#[deprecated(note = "use `Self::builder` method instead of it")]
	pub fn allow_missing_host_functions(&mut self, allow_missing_host_functions: bool) {
		self.allow_missing_host_functions = allow_missing_host_functions
	}
}

impl<H> WasmExecutor<H>
where
	H: HostFunctions,
{
	/// Execute the given closure `f` with the latest runtime (based on `runtime_code`).
	///
	/// The closure `f` is expected to return `Err(_)` when there happened a `panic!` in native code
	/// while executing the runtime in Wasm. If a `panic!` occurred, the runtime is invalidated to
	/// prevent any poisoned state. Native runtime execution does not need to report back
	/// any `panic!`.
	///
	/// # Safety
	///
	/// `runtime` and `ext` are given as `AssertUnwindSafe` to the closure. As described above, the
	/// runtime is invalidated on any `panic!` to prevent a poisoned state. `ext` is already
	/// implicitly handled as unwind safe, as we store it in a global variable while executing the
	/// native runtime.
	pub fn with_instance<R, F>(
		&self,
		runtime_code: &RuntimeCode,
		ext: &mut dyn Externalities,
		heap_alloc_strategy: HeapAllocStrategy,
		f: F,
	) -> Result<R>
	where
		F: FnOnce(
			AssertUnwindSafe<&dyn WasmModule>,
			AssertUnwindSafe<&mut dyn WasmInstance>,
			Option<&RuntimeVersion>,
			AssertUnwindSafe<&mut dyn Externalities>,
		) -> Result<Result<R>>,
	{
		match self.cache.with_instance::<H, _, _>(
			runtime_code,
			ext,
			self.method,
			heap_alloc_strategy,
			self.allow_missing_host_functions,
			|module, instance, version, ext| {
				let module = AssertUnwindSafe(module);
				let instance = AssertUnwindSafe(instance);
				let ext = AssertUnwindSafe(ext);
				f(module, instance, version, ext)
			},
		)? {
			Ok(r) => r,
			Err(e) => Err(e),
		}
	}

	/// Perform a call into the given runtime.
	///
	/// The runtime is passed as a [`RuntimeBlob`]. The runtime will be instantiated with the
	/// parameters this `WasmExecutor` was initialized with.
	///
	/// In case of problems with during creation of the runtime or instantiation, a `Err` is
	/// returned. that describes the message.
	#[doc(hidden)] // We use this function for tests across multiple crates.
	pub fn uncached_call(
		&self,
		runtime_blob: RuntimeBlob,
		ext: &mut dyn Externalities,
		allow_missing_host_functions: bool,
		export_name: &str,
		call_data: &[u8],
	) -> std::result::Result<Vec<u8>, Error> {
		self.uncached_call_impl(
			runtime_blob,
			ext,
			allow_missing_host_functions,
			export_name,
			call_data,
			&mut None,
		)
	}

	/// Same as `uncached_call`, except it also returns allocation statistics.
	#[doc(hidden)] // We use this function in tests.
	pub fn uncached_call_with_allocation_stats(
		&self,
		runtime_blob: RuntimeBlob,
		ext: &mut dyn Externalities,
		allow_missing_host_functions: bool,
		export_name: &str,
		call_data: &[u8],
	) -> (std::result::Result<Vec<u8>, Error>, Option<AllocationStats>) {
		let mut allocation_stats = None;
		let result = self.uncached_call_impl(
			runtime_blob,
			ext,
			allow_missing_host_functions,
			export_name,
			call_data,
			&mut allocation_stats,
		);
		(result, allocation_stats)
	}

	fn uncached_call_impl(
		&self,
		runtime_blob: RuntimeBlob,
		ext: &mut dyn Externalities,
		allow_missing_host_functions: bool,
		export_name: &str,
		call_data: &[u8],
		allocation_stats_out: &mut Option<AllocationStats>,
	) -> std::result::Result<Vec<u8>, Error> {
		let module = crate::wasm_runtime::create_wasm_runtime_with_code::<H>(
			self.method,
			self.default_onchain_heap_alloc_strategy,
			runtime_blob,
			allow_missing_host_functions,
			self.cache_path.as_deref(),
		)
		.map_err(|e| format!("Failed to create module: {}", e))?;

		let instance =
			module.new_instance().map_err(|e| format!("Failed to create instance: {}", e))?;

		let mut instance = AssertUnwindSafe(instance);
		let mut ext = AssertUnwindSafe(ext);
		let mut allocation_stats_out = AssertUnwindSafe(allocation_stats_out);

		with_externalities_safe(&mut **ext, move || {
			let (result, allocation_stats) =
				instance.call_with_allocation_stats(export_name.into(), call_data);
			**allocation_stats_out = allocation_stats;
			result
		})
		.and_then(|r| r)
	}
}

impl<H> sp_core::traits::ReadRuntimeVersion for WasmExecutor<H>
where
	H: HostFunctions,
{
	fn read_runtime_version(
		&self,
		wasm_code: &[u8],
		ext: &mut dyn Externalities,
	) -> std::result::Result<Vec<u8>, String> {
		let runtime_blob = RuntimeBlob::uncompress_if_needed(wasm_code)
			.map_err(|e| format!("Failed to create runtime blob: {:?}", e))?;

		if let Some(version) = crate::wasm_runtime::read_embedded_version(&runtime_blob)
			.map_err(|e| format!("Failed to read the static section: {:?}", e))
			.map(|v| v.map(|v| v.encode()))?
		{
			return Ok(version)
		}

		// If the blob didn't have embedded runtime version section, we fallback to the legacy
		// way of fetching the version: i.e. instantiating the given instance and calling
		// `Core_version` on it.

		self.uncached_call(
			runtime_blob,
			ext,
			// If a runtime upgrade introduces new host functions that are not provided by
			// the node, we should not fail at instantiation. Otherwise nodes that are
			// updated could run this successfully and it could lead to a storage root
			// mismatch when importing this block.
			true,
			"Core_version",
			&[],
		)
		.map_err(|e| e.to_string())
	}
}

impl<H> CodeExecutor for WasmExecutor<H>
where
	H: HostFunctions,
{
	type Error = Error;

	fn call(
		&self,
		ext: &mut dyn Externalities,
		runtime_code: &RuntimeCode,
		method: &str,
		data: &[u8],
		context: CallContext,
	) -> (Result<Vec<u8>>, bool) {
		tracing::trace!(
			target: "executor",
			%method,
			"Executing function",
		);

		let on_chain_heap_alloc_strategy = if self.ignore_onchain_heap_pages {
			self.default_onchain_heap_alloc_strategy
		} else {
			runtime_code
				.heap_pages
				.map(|h| HeapAllocStrategy::Static { extra_pages: h as _ })
				.unwrap_or_else(|| self.default_onchain_heap_alloc_strategy)
		};

		let heap_alloc_strategy = match context {
			CallContext::Offchain => self.default_offchain_heap_alloc_strategy,
			CallContext::Onchain => on_chain_heap_alloc_strategy,
		};

		let result = self.with_instance(
			runtime_code,
			ext,
			heap_alloc_strategy,
			|_, mut instance, _on_chain_version, mut ext| {
				with_externalities_safe(&mut **ext, move || instance.call_export(method, data))
			},
		);

		(result, false)
	}
}

impl<H> RuntimeVersionOf for WasmExecutor<H>
where
	H: HostFunctions,
{
	fn runtime_version(
		&self,
		ext: &mut dyn Externalities,
		runtime_code: &RuntimeCode,
	) -> Result<RuntimeVersion> {
		let on_chain_heap_pages = if self.ignore_onchain_heap_pages {
			self.default_onchain_heap_alloc_strategy
		} else {
			runtime_code
				.heap_pages
				.map(|h| HeapAllocStrategy::Static { extra_pages: h as _ })
				.unwrap_or_else(|| self.default_onchain_heap_alloc_strategy)
		};

		self.with_instance(
			runtime_code,
			ext,
			on_chain_heap_pages,
			|_module, _instance, version, _ext| {
				Ok(version.cloned().ok_or_else(|| Error::ApiError("Unknown version".into())))
			},
		)
	}
}

/// A generic `CodeExecutor` implementation that uses a delegate to determine wasm code equivalence
/// and dispatch to native code when possible, falling back on `WasmExecutor` when not.
#[deprecated(
	note = "Native execution will be deprecated, please replace with `WasmExecutor`. Will be removed at end of 2024."
)]
pub struct NativeElseWasmExecutor<D: NativeExecutionDispatch> {
	/// Native runtime version info.
	native_version: NativeVersion,
	/// Fallback wasm executor.
	wasm:
		WasmExecutor<ExtendedHostFunctions<sp_io::SubstrateHostFunctions, D::ExtendHostFunctions>>,

	use_native: bool,
}

#[allow(deprecated)]
impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
	///
	/// Create new instance.
	///
	/// # Parameters
	///
	/// `fallback_method` - Method used to execute fallback Wasm code.
	///
	/// `default_heap_pages` - Number of 64KB pages to allocate for Wasm execution. Internally this
	/// will be mapped as [`HeapAllocStrategy::Static`] where `default_heap_pages` represent the
	/// static number of heap pages to allocate. Defaults to `DEFAULT_HEAP_ALLOC_STRATEGY` if `None`
	/// is provided.
	///
	/// `max_runtime_instances` - The number of runtime instances to keep in memory ready for reuse.
	///
	/// `runtime_cache_size` - The capacity of runtime cache.
	#[deprecated(note = "use `Self::new_with_wasm_executor` method instead of it")]
	pub fn new(
		fallback_method: WasmExecutionMethod,
		default_heap_pages: Option<u64>,
		max_runtime_instances: usize,
		runtime_cache_size: u8,
	) -> Self {
		let heap_pages = default_heap_pages.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |h| {
			HeapAllocStrategy::Static { extra_pages: h as _ }
		});
		let wasm = WasmExecutor::builder()
			.with_execution_method(fallback_method)
			.with_onchain_heap_alloc_strategy(heap_pages)
			.with_offchain_heap_alloc_strategy(heap_pages)
			.with_max_runtime_instances(max_runtime_instances)
			.with_runtime_cache_size(runtime_cache_size)
			.build();

		NativeElseWasmExecutor { native_version: D::native_version(), wasm, use_native: true }
	}

	/// Create a new instance using the given [`WasmExecutor`].
	pub fn new_with_wasm_executor(
		executor: WasmExecutor<
			ExtendedHostFunctions<sp_io::SubstrateHostFunctions, D::ExtendHostFunctions>,
		>,
	) -> Self {
		Self { native_version: D::native_version(), wasm: executor, use_native: true }
	}

	/// Disable to use native runtime when possible just behave like `WasmExecutor`.
	///
	/// Default to enabled.
	pub fn disable_use_native(&mut self) {
		self.use_native = false;
	}

	/// Ignore missing function imports if set true.
	#[deprecated(note = "use `Self::new_with_wasm_executor` method instead of it")]
	pub fn allow_missing_host_functions(&mut self, allow_missing_host_functions: bool) {
		self.wasm.allow_missing_host_functions = allow_missing_host_functions
	}
}

#[allow(deprecated)]
impl<D: NativeExecutionDispatch> RuntimeVersionOf for NativeElseWasmExecutor<D> {
	fn runtime_version(
		&self,
		ext: &mut dyn Externalities,
		runtime_code: &RuntimeCode,
	) -> Result<RuntimeVersion> {
		self.wasm.runtime_version(ext, runtime_code)
	}
}

#[allow(deprecated)]
impl<D: NativeExecutionDispatch> GetNativeVersion for NativeElseWasmExecutor<D> {
	fn native_version(&self) -> &NativeVersion {
		&self.native_version
	}
}

#[allow(deprecated)]
impl<D: NativeExecutionDispatch + 'static> CodeExecutor for NativeElseWasmExecutor<D> {
	type Error = Error;

	fn call(
		&self,
		ext: &mut dyn Externalities,
		runtime_code: &RuntimeCode,
		method: &str,
		data: &[u8],
		context: CallContext,
	) -> (Result<Vec<u8>>, bool) {
		let use_native = self.use_native;

		tracing::trace!(
			target: "executor",
			function = %method,
			"Executing function",
		);

		let on_chain_heap_alloc_strategy = if self.wasm.ignore_onchain_heap_pages {
			self.wasm.default_onchain_heap_alloc_strategy
		} else {
			runtime_code
				.heap_pages
				.map(|h| HeapAllocStrategy::Static { extra_pages: h as _ })
				.unwrap_or_else(|| self.wasm.default_onchain_heap_alloc_strategy)
		};

		let heap_alloc_strategy = match context {
			CallContext::Offchain => self.wasm.default_offchain_heap_alloc_strategy,
			CallContext::Onchain => on_chain_heap_alloc_strategy,
		};

		let mut used_native = false;
		let result = self.wasm.with_instance(
			runtime_code,
			ext,
			heap_alloc_strategy,
			|_, mut instance, on_chain_version, mut ext| {
				let on_chain_version =
					on_chain_version.ok_or_else(|| Error::ApiError("Unknown version".into()))?;

				let can_call_with =
					on_chain_version.can_call_with(&self.native_version.runtime_version);

				if use_native && can_call_with {
					tracing::trace!(
						target: "executor",
						native = %self.native_version.runtime_version,
						chain = %on_chain_version,
						"Request for native execution succeeded",
					);

					used_native = true;
					Ok(with_externalities_safe(&mut **ext, move || D::dispatch(method, data))?
						.ok_or_else(|| Error::MethodNotFound(method.to_owned())))
				} else {
					if !can_call_with {
						tracing::trace!(
							target: "executor",
							native = %self.native_version.runtime_version,
							chain = %on_chain_version,
							"Request for native execution failed",
						);
					}

					with_externalities_safe(&mut **ext, move || instance.call_export(method, data))
				}
			},
		);
		(result, used_native)
	}
}

#[allow(deprecated)]
impl<D: NativeExecutionDispatch> Clone for NativeElseWasmExecutor<D> {
	fn clone(&self) -> Self {
		NativeElseWasmExecutor {
			native_version: D::native_version(),
			wasm: self.wasm.clone(),
			use_native: self.use_native,
		}
	}
}

#[allow(deprecated)]
impl<D: NativeExecutionDispatch> sp_core::traits::ReadRuntimeVersion for NativeElseWasmExecutor<D> {
	fn read_runtime_version(
		&self,
		wasm_code: &[u8],
		ext: &mut dyn Externalities,
	) -> std::result::Result<Vec<u8>, String> {
		self.wasm.read_runtime_version(wasm_code, ext)
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use sp_runtime_interface::runtime_interface;

	#[runtime_interface]
	trait MyInterface {
		fn say_hello_world(data: &str) {
			println!("Hello world from: {}", data);
		}
	}

	pub struct MyExecutorDispatch;

	impl NativeExecutionDispatch for MyExecutorDispatch {
		type ExtendHostFunctions = (my_interface::HostFunctions, my_interface::HostFunctions);

		fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
			substrate_test_runtime::api::dispatch(method, data)
		}

		fn native_version() -> NativeVersion {
			substrate_test_runtime::native_version()
		}
	}

	#[test]
	#[allow(deprecated)]
	fn native_executor_registers_custom_interface() {
		let executor = NativeElseWasmExecutor::<MyExecutorDispatch>::new_with_wasm_executor(
			WasmExecutor::builder().build(),
		);

		fn extract_host_functions<H>(
			_: &WasmExecutor<H>,
		) -> Vec<&'static dyn sp_wasm_interface::Function>
		where
			H: HostFunctions,
		{
			H::host_functions()
		}

		my_interface::HostFunctions::host_functions().iter().for_each(|function| {
			assert_eq!(
				extract_host_functions(&executor.wasm).iter().filter(|f| f == &function).count(),
				2
			);
		});

		my_interface::say_hello_world("hey");
	}
}