57 releases (37 major breaking)

new 39.0.0 Jan 7, 2025
38.0.0 Jul 18, 2024
37.0.0 Jun 21, 2024
36.0.0 May 23, 2024
2.0.0-alpha.5 Mar 24, 2020

#225 in Magic Beans

Download history 88042/week @ 2024-09-21 89499/week @ 2024-09-28 99421/week @ 2024-10-05 99969/week @ 2024-10-12 111158/week @ 2024-10-19 87582/week @ 2024-10-26 119438/week @ 2024-11-02 94809/week @ 2024-11-09 88307/week @ 2024-11-16 93063/week @ 2024-11-23 74555/week @ 2024-11-30 103609/week @ 2024-12-07 77405/week @ 2024-12-14 24493/week @ 2024-12-21 37668/week @ 2024-12-28 62279/week @ 2025-01-04

218,428 downloads per month
Used in 744 crates (295 directly)

Apache-2.0

655KB
13K SLoC

I/O host interface for Substrate runtime.

License: Apache-2.0

Release

Polkadot SDK Stable 2412


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

~15–28MB
~440K SLoC