This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
Refactor sp-sandbox
to accomodate new wasmi
API
#11752
Labels
J0-enhancement
An additional feature request.
Z4-involved
Can be fixed by an expert coder with good knowledge of the codebase.
Background
Within substrate we have two places where
wasmi
is used:sc-executor
This is the part of substrate which is responsible for executing the runtime. Hence
sc-executor
is part of the client and is never compiled to wasm as it is the part that actually executes the wasm. Another responsibility ofsc-executor
is to provide sandboxing functionality to the runtime via a set of host functions:substrate/primitives/io/src/lib.rs
Lines 1567 to 1658 in ee3eb8f
Please note that with sandboxing we specifically mean exposing and implementing this interface. Executing the runtime itself is not what we mean by this. These two functionalities are simply conflated within
sc-executor
.Within
sc-executor
wasmi is used for both use cases: It can be used to execute the runtime and it can also be used to provide sandboxing. Apart from wasmisc-executor
can also execute the runtime with wasmtime (this is the default and wasmi is way too slow for production use).Additionally, the sandboxing can also be accomplished with wasmer singlepass (by passing the experimental
wasmer-sandbox
feature). Sandbox does not support wasmtime as we assume the sandboxed code to be untrusted and hence cannot be compiled with an optimizing compiler (like wasmtime). Wasmtime support for sandboxing could be useful for debugging (wasmtime is more advanced in that regard) but cannot be used in production for the discussed reason.sp-sandbox
We discussed that
sc-executor
provides sandboxing support via a host interface. This means it can be used by the runtime to spawn wasm instances within the client to run some code in (read contracts).sp-sandbox
is the abstraction around this host interface.pallet-contracts
(part of the runtime) depends on this crate in order to run its contracts through the host interface.In addition to providing access to the host interface,
sp-sandbox
also includes what we call an embedded executor. This means that instead of calling into the host it uses a executor directly compiled into this crate to provide its functionality. We use wasmi as the embedded executor. Hencesp-sandbox
directly depends on wasmi and becomes compiled to wasm and straight into the runtime (yes we run a wasm interpreter as wasm). The part ofsp-sandbox
that uses the host interface is called the host executor.Please note that we cannot use wasmer as embedded executor as compilers cannot be part of the runtime as they are inherently platform depended (they emit native code) and the runtime needs to be independent of the platform.
As of right now we only use the embedded executor for production as we do not want to depend on the availability of the sandboxing host functions on relay chain validators and hence ossify their API, yet.
sp-sandbox
provides its functionality by exporting three modules:embedded_executor
host_executor
default_executor
Those modules contain functions needed to spawn and interact with sandboxes.
pallet-contracts
as the only user usesdefault_executor
which is just a re-export ofembedded_executor
in the production case (wasmer-sandbox
is experimental and forces the use of the host executor because this is where wasmer lives):substrate/primitives/sandbox/src/lib.rs
Lines 53 to 57 in ee3eb8f
As you can see the presence of
std
will also force us to use the embedded executor. This is because in thestd
casesp-sandbox
is compiled for use in the native runtime where the sandboxing host interface isn't available.Motivation
What we want to to is to upgrade the embedded executor to use the latest wasmi version. We do this because we want to improve the performance of our contracts. We only care about the embedded executor because this is the only executor we use for now for contracts.
Upgrading the wasmi within
sc-executor
is not required because it is not used:Running the runtime with the newest wasmer could be interesting in order to perform a burnin, though.
Implementation
Refactor executor decision logic
The re-export of the
default_executor
is a bad way of selecting which executor is to be used by the users ofsp-sandbox
: The choice of the executor is made by some upstream substrate crate when doing it like this. We want to leave this decision to the users of substrate (runtime authors). Hence we want to refactorsp-sandbox
(andpallet-contracts
) so that the setup looks like this:sp-sandbox
exports a singleSandbox
trait.sp-sandbox
exports two types implementingSandbox
:HostExecutor
,EmbeddedExecutor
.pallet-contracts
adds atype Executor: Sandbox
associated type to itstrait Config
.Executor
.sp-sandbox
does not implement anyDefaultExecutor
type. The runtime needs to make this decision.Update
EmbeddedExecutor
to use the latest wasmiThe latest updates to wasmi changed its API. It is now more like the wasmtime API. The current abstractions within
sp-sandbox
do not work with the new API. They need a refactoring. This is why we conflate it with the other refactoring. Those traits need to be re-organized anyways.TODO
EmbeddedExecutor
to use the latest wasmiThe text was updated successfully, but these errors were encountered: