Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

WasmExecutor flag to ignore onchain heappages value #14508

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions client/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub struct WasmExecutorBuilder<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,
Expand All @@ -104,6 +105,7 @@ impl<H> WasmExecutorBuilder<H> {
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,
Expand Down Expand Up @@ -137,6 +139,14 @@ impl<H> WasmExecutorBuilder<H> {
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
Expand Down Expand Up @@ -193,6 +203,7 @@ impl<H> WasmExecutorBuilder<H> {
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(),
Expand All @@ -214,6 +225,8 @@ pub struct WasmExecutor<H> {
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
Expand All @@ -230,6 +243,7 @@ impl<H> Clone for WasmExecutor<H> {
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,
Expand Down Expand Up @@ -276,6 +290,7 @@ where
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(),
Expand Down Expand Up @@ -486,10 +501,14 @@ where
"Executing function",
);

let on_chain_heap_alloc_strategy = runtime_code
.heap_pages
.map(|h| HeapAllocStrategy::Static { extra_pages: h as _ })
.unwrap_or_else(|| self.default_onchain_heap_alloc_strategy);
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,
Expand Down Expand Up @@ -518,10 +537,14 @@ where
ext: &mut dyn Externalities,
runtime_code: &RuntimeCode,
) -> Result<RuntimeVersion> {
let on_chain_heap_pages = runtime_code
.heap_pages
.map(|h| HeapAllocStrategy::Static { extra_pages: h as _ })
.unwrap_or_else(|| self.default_onchain_heap_alloc_strategy);
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,
Expand Down Expand Up @@ -631,10 +654,14 @@ impl<D: NativeExecutionDispatch + 'static> CodeExecutor for NativeElseWasmExecut
"Executing function",
);

let on_chain_heap_alloc_strategy = runtime_code
.heap_pages
.map(|h| HeapAllocStrategy::Static { extra_pages: h as _ })
.unwrap_or_else(|| self.wasm.default_onchain_heap_alloc_strategy);
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,
Expand Down