52 releases (32 major breaking)

new 34.0.0 Apr 8, 2024
33.0.0 Mar 18, 2024
32.0.0 Feb 26, 2024
31.0.0 Feb 13, 2024
2.0.0-alpha.5 Mar 24, 2020

#1092 in Magic Beans

Download history 6062/week @ 2023-12-18 3736/week @ 2023-12-25 5845/week @ 2024-01-01 10118/week @ 2024-01-08 7455/week @ 2024-01-15 8990/week @ 2024-01-22 7426/week @ 2024-01-29 10097/week @ 2024-02-05 11065/week @ 2024-02-12 8278/week @ 2024-02-19 10293/week @ 2024-02-26 9340/week @ 2024-03-04 8195/week @ 2024-03-11 11967/week @ 2024-03-18 9060/week @ 2024-03-25 12521/week @ 2024-04-01

42,496 downloads per month
Used in 674 crates (270 directly)

Apache-2.0

650KB
12K SLoC

I/O host interface for Substrate runtime.

License: Apache-2.0


lib.rs:

Substrate Primitives: IO

This crate contains interfaces for the runtime to communicate with the outside world, ergo io. In other context, such interfaces are referred to as "host functions".

Each set of host functions are defined with an instance of the sp_runtime_interface::runtime_interface macro.

Most notably, this crate contains host functions for:

All of the default host functions provided by this crate, and by default contained in all substrate-based clients are amalgamated in SubstrateHostFunctions.

Externalities

Host functions go hand in hand with the concept of externalities. Externalities are an environment in which host functions are provided, and thus can be accessed. Some host functions are only accessible in an externality environment that provides it.

A typical error for substrate developers is the following:

use sp_io::storage::get;
let data = get(b"hello world");

This code will panic with the following error:

thread 'main' panicked at '`get_version_1` called outside of an Externalities-provided environment.'

Such error messages should always be interpreted as "code accessing host functions accessed outside of externalities".

An externality is any type that implements sp_externalities::Externalities. A simple example of which is TestExternalities, which is commonly used in tests and is exported from this crate.

use sp_io::{storage::get, TestExternalities};
TestExternalities::default().execute_with(|| {
	let data = get(b"hello world");
});

Dependencies

~12–24MB
~373K SLoC