**Update:** Pushed additional changes based on the review comments. **This pull request fixes various spelling mistakes in this repository.** Most of the changes are contained in the first **3** commits: - `Fix spelling mistakes in comments and docs` - `Fix spelling mistakes in test names` - `Fix spelling mistakes in error messages, panic messages, logs and tracing` Other source code spelling mistakes are separated into individual commits for easier reviewing: - `Fix the spelling of 'authority'` - `Fix the spelling of 'REASONABLE_HEADERS_IN_JUSTIFICATION_ANCESTRY'` - `Fix the spelling of 'prev_enqueud_messages'` - `Fix the spelling of 'endpoint'` - `Fix the spelling of 'children'` - `Fix the spelling of 'PenpalSiblingSovereignAccount'` - `Fix the spelling of 'PenpalSudoAccount'` - `Fix the spelling of 'insufficient'` - `Fix the spelling of 'PalletXcmExtrinsicsBenchmark'` - `Fix the spelling of 'subtracted'` - `Fix the spelling of 'CandidatePendingAvailability'` - `Fix the spelling of 'exclusive'` - `Fix the spelling of 'until'` - `Fix the spelling of 'discriminator'` - `Fix the spelling of 'nonexistent'` - `Fix the spelling of 'subsystem'` - `Fix the spelling of 'indices'` - `Fix the spelling of 'committed'` - `Fix the spelling of 'topology'` - `Fix the spelling of 'response'` - `Fix the spelling of 'beneficiary'` - `Fix the spelling of 'formatted'` - `Fix the spelling of 'UNKNOWN_PROOF_REQUEST'` - `Fix the spelling of 'succeeded'` - `Fix the spelling of 'reopened'` - `Fix the spelling of 'proposer'` - `Fix the spelling of 'InstantiationNonce'` - `Fix the spelling of 'depositor'` - `Fix the spelling of 'expiration'` - `Fix the spelling of 'phantom'` - `Fix the spelling of 'AggregatedKeyValue'` - `Fix the spelling of 'randomness'` - `Fix the spelling of 'defendant'` - `Fix the spelling of 'AquaticMammal'` - `Fix the spelling of 'transactions'` - `Fix the spelling of 'PassingTracingSubscriber'` - `Fix the spelling of 'TxSignaturePayload'` - `Fix the spelling of 'versioning'` - `Fix the spelling of 'descendant'` - `Fix the spelling of 'overridden'` - `Fix the spelling of 'network'` Let me know if this structure is adequate. **Note:** The usage of the words `Merkle`, `Merkelize`, `Merklization`, `Merkelization`, `Merkleization`, is somewhat inconsistent but I left it as it is. ~~**Note:** In some places the term `Receival` is used to refer to message reception, IMO `Reception` is the correct word here, but I left it as it is.~~ ~~**Note:** In some places the term `Overlayed` is used instead of the more acceptable version `Overlaid` but I also left it as it is.~~ ~~**Note:** In some places the term `Applyable` is used instead of the correct version `Applicable` but I also left it as it is.~~ **Note:** Some usage of British vs American english e.g. `judgement` vs `judgment`, `initialise` vs `initialize`, `optimise` vs `optimize` etc. are both present in different places, but I suppose that's understandable given the number of contributors. ~~**Note:** There is a spelling mistake in `.github/CODEOWNERS` but it triggers errors in CI when I make changes to it, so I left it as it is.~~
Substrate runtime interface
This crate provides types, traits and macros around runtime interfaces. A runtime interface is a fixed interface between a Substrate runtime and a Substrate node. For a native runtime the interface maps to a direct function call of the implementation. For a wasm runtime the interface maps to an external function call. These external functions are exported by the wasm executor and they map to the same implementation as the native calls.
Using a type in a runtime interface
Any type that should be used in a runtime interface as argument or return value needs to implement [RIType]. The
associated type
FFIType is
the type that is used in the FFI function to represent the actual type. For example [T] is represented by an u64.
The slice pointer and the length will be mapped to an u64 value. For more information see this
table. The FFI function
definition is used when calling from the wasm runtime into the node.
Traits are used to convert from a type to the corresponding
RIType::FFIType.
Depending on where and how a type should be used in a function signature, a combination of the following traits need to
be implemented:
- Pass as function argument: [
wasm::IntoFFIValue] and [host::FromFFIValue] - As function return value: [
wasm::FromFFIValue] and [host::IntoFFIValue] - Pass as mutable function argument: [
host::IntoPreallocatedFFIValue]
The traits are implemented for most of the common types like [T], Vec<T>, arrays and primitive types.
For custom types, we provide the
PassBy trait and strategies that
define how a type is passed between the wasm runtime and the node. Each strategy also provides a derive macro to
simplify the implementation.
Performance
To not waste any more performance when calling into the node, not all types are SCALE encoded when being passed as
arguments between the wasm runtime and the node. For most types that are raw bytes like Vec<u8>, [u8] or [u8; N]
we pass them directly, without SCALE encoding them in front of. The implementation of [RIType] each type provides more
information on how the data is passed.
Declaring a runtime interface
Declaring a runtime interface is similar to declaring a trait in Rust:
#[sp_runtime_interface::runtime_interface]
trait RuntimeInterface {
fn some_function(value: &[u8]) -> bool {
value.iter().all(|v| *v > 125)
}
}
For more information on declaring a runtime interface, see
#[runtime_interface].
FFI type and conversion
The following table documents how values of types are passed between the wasm and the host side and how they are converted into the corresponding type.
| Type | FFI type | Conversion |
|---|---|---|
u8 |
u8 |
Identity |
u16 |
u16 |
Identity |
u32 |
u32 |
Identity |
u64 |
u64 |
Identity |
i128 |
u32 |
v.as_ptr() (pointer to a 16 byte array) |
i8 |
i8 |
Identity |
i16 |
i16 |
Identity |
i32 |
i32 |
Identity |
i64 |
i64 |
Identity |
u128 |
u32 |
v.as_ptr() (pointer to a 16 byte array) |
bool |
u8 |
if v { 1 } else { 0 } |
&str |
u64 |
v.len() 32bit << 32 | v.as_ptr() 32bit |
&[u8] |
u64 |
v.len() 32bit << 32 | v.as_ptr() 32bit |
Vec<u8> |
u64 |
v.len() 32bit << 32 | v.as_ptr() 32bit |
Vec<T> where T: Encode |
u64 |
let e = v.encode();e.len() 32bit << 32 | e.as_ptr() 32bit |
&[T] where T: Encode |
u64 |
let e = v.encode();e.len() 32bit << 32 | e.as_ptr() 32bit |
[u8; N] |
u32 |
v.as_ptr() |
*const T |
u32 |
Identity |
Option<T> |
u64 |
let e = v.encode();e.len() 32bit << 32 | e.as_ptr() 32bit |
T where T: PassBy<PassBy=Inner> |
Depends on inner | Depends on inner |
T where T: PassBy<PassBy=Codec> |
u64 |
v.len() 32bit << 32 | v.as_ptr() 32bit |
Identity means that the value is converted directly into the corresponding FFI type.
License: Apache-2.0