Skip to content
Snippets Groups Projects
user avatar
Benjamin Kampmann authored
* implement events handling, implement parent_id for spans & events

* add events to sp_io::storage

* update test

* add tests

* adjust limit

* let tracing crate handle parent_ids

* re-enable current-id tracking

* add test for threads with CurrentSpan

* fix log level

* remove redundant check for non wasm traces

* remove duplicate definition in test

* Adding conditional events API

* prefer explicit parent_id over current,

enhance test

* limit changes to client::tracing event implementation

* remove From impl due to fallback required on parent_id

* make tracing codecable

* replace with global tracing

* new tracing interface

* impl TracingSubscriber in client

* implement access to global TracingSubscriber from primitives

* span for wasm

* increment towards Wasm Tracing Subscriber implementation

* increment, remove sp-tracing from runtime-interface

* increment, it compiles

* attained original functionality with new mechanism

* implement remaining TracingSubscriber functions

* remove spans from decl_module

* add handling for encoded values

* Revert "replace with global tracing"

This reverts commit 8824a60deea54d9b437407a21c8ceaf6a1902ee5.

* Wasm Side Tracing

* tracing on wasm

* enable tracing wasm on node-runtime

* export all the macros in std

* tracing subscriber on wasm-side only

* pass spans and events over and record them

* reactivate previous code and  cleanup

* further cleaning up

* extend the span macros, activate through executive

* tracking the actual extrinsic, too

* style

* fixing tests

* spaces -> tabs

* attempting to reactivate params

* activate our tests in CI

* some passing

* tests passing

* with core lazy

* global tracer for wasm side with pass over

* fixing metadata referencing

* remove const_fn feature requirement

* reenable dispatch traces

* reset client tracing

* further cleaning up

* fixing runtime-test

* move tracing-build setup into runtime-test

* Merge DebugWriter from tracing and frame-support, move to sp-std

* remove dangling fixme

* Docs for tracing primitives

* cleaning up a bit more

* Wasm interface docs

* optimise docs.rs setup

* adding tracing flags to uncomment

* remove brace

* fixing imports

* fixing broken syntax

* add required modules

* nicer formatting

* better target management

* adding low level storage tracing events into frame

* add custom Debug impl for WasmMetadata

* cloning profiler

* adding info about cloning profiler

* using in-scope for within calls

* proper time tracing, cleaning up println

* allow to disable tracing on runtime_interface-macro

* disable tracing for wasm-tracing-interface

* simplify wasm-tracing-api

* update client to new interface

* fixing docs and tests for sp-tracing

* update integration tests

* re-activating enter_span

* dropping FIXME, it's documented

* fix formatting

* fix formatting

* fix imports

* more debug info

* inform wasm about it being disabled by returning 1

* only one tracer, but enabled multi-all support

* make trait pub again for tests

* Apply suggestions from code review

Co-authored-by: default avatarNiklas Adolfsson <niklasadolfsson1@gmail.com>

* fixing wasm doc tests for proper usage

* remove unnecessary import

* fixing formatting

* minor style fixes

* downgrading wabt

* update error message for UI

* Fix interface test

* next attempt to fix macros

* geee

* revert tracing on hashed for future PR

* remove local macros, use originals

* we are able to convert to static items

* implement more WasmValue types

* adding support to convert str, debug and encoded values

* more minor fixes

* revert unsafe 'static making

* fix indentation

* remove commented lines

* bump all them tracing versions

* cleaning up docs and info

* document new flag

* the new layered system handles span cloning better

* Apply suggestions from code review

Co-authored-by: default avatarDavid <dvdplm@gmail.com>

Co-authored-by: default avatarMatt Rutherford <mattrutherford@users.noreply.github.com>
Co-authored-by: default avatarNiklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: default avatarDavid <dvdplm@gmail.com>
a9c73113

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:

  1. Pass as function argument: [wasm::IntoFFIValue] and [host::FromFFIValue]
  2. As function return value: [wasm::FromFFIValue] and [host::IntoFFIValue]
  3. 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