Skip to main content

smoldot/
executor.rs

1// Smoldot
2// Copyright (C) 2019-2022  Parity Technologies (UK) Ltd.
3// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
4
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14
15// You should have received a copy of the GNU General Public License
16// along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18//! WebAssembly runtime code execution.
19//!
20//! WebAssembly (often abbreviated *Wasm*) plays a big role in Substrate/Polkadot. The storage of
21//! each block in the chain has a special key named `:code` which contains the WebAssembly code
22//! of what we call *the runtime*.
23//!
24//! The runtime is a program (in WebAssembly) that decides, amongst other things, whether
25//! transactions are valid and how to apply them on the storage, and whether blocks themselves are
26//! valid.
27//!
28//! This module contains everything necessary to execute runtime code. The highest-level
29//! sub-module is [`runtime_call`].
30
31mod allocator; // TODO: make public after refactoring
32pub mod host;
33pub mod runtime_call;
34pub mod storage_diff;
35pub mod trie_root_calculator;
36pub mod vm;
37
38pub use host::{CoreVersion, CoreVersionError, CoreVersionRef};
39
40/// Default number of heap pages if the storage doesn't specify otherwise.
41///
42/// # Context
43///
44/// In order to initialize a [`host::HostVmPrototype`], one needs to pass a certain number of
45/// heap pages that are available to the runtime.
46///
47/// This number is normally found in the storage, at the key `:heappages`. But if it is not
48/// specified, then the value of this constant must be used.
49pub const DEFAULT_HEAP_PAGES: vm::HeapPages = vm::HeapPages::new(2048);
50
51/// Converts a value of the key `:heappages` found in the storage to an actual number of heap
52/// pages.
53pub fn storage_heap_pages_to_value(
54    storage_value: Option<&[u8]>,
55) -> Result<vm::HeapPages, InvalidHeapPagesError> {
56    if let Some(storage_value) = storage_value {
57        let bytes =
58            <[u8; 8]>::try_from(storage_value).map_err(|_| InvalidHeapPagesError::WrongLen)?;
59        let num = u64::from_le_bytes(bytes);
60        let num = u32::try_from(num).map_err(|_| InvalidHeapPagesError::TooLarge)?;
61        Ok(vm::HeapPages::from(num))
62    } else {
63        Ok(DEFAULT_HEAP_PAGES)
64    }
65}
66
67/// Error potentially returned by [`storage_heap_pages_to_value`].
68#[derive(Debug, derive_more::Display, derive_more::Error, Clone)]
69pub enum InvalidHeapPagesError {
70    /// Storage value has the wrong length.
71    WrongLen,
72    /// Number of heap pages is too large.
73    TooLarge,
74}