Unify & enforce same interface of sr-io (std & without-std) (#2381)

* WiP: HTTP Apis.

* Working on the API.

* Add docs, clean up the API.

* Expose ext_ stuff as well.

* Implement HTTP helpers for offchain sr-io.

* Remove HTTP stuff.

* Remove spurious leading `::`

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Rename in toml.

* Add issue number.

* Bump version.
This commit is contained in:
Tomasz Drwięga
2019-04-25 15:49:34 +02:00
committed by Bastian Köcher
parent 07d495d905
commit f0202aa425
11 changed files with 969 additions and 801 deletions
+2 -2
View File
@@ -745,10 +745,10 @@ fn init_logger(pattern: &str) {
builder.filter(None, log::LevelFilter::Info);
if let Ok(lvl) = std::env::var("RUST_LOG") {
builder.parse(&lvl);
builder.parse_filters(&lvl);
}
builder.parse(pattern);
builder.parse_filters(pattern);
let isatty = atty::is(atty::Stream::Stderr);
let enable_color = isatty;
+1 -1
View File
@@ -92,7 +92,7 @@ impl_stubs!(
[sr25519_verify(&sig, &msg[..], &pubkey) as u8].to_vec()
},
test_enumerated_trie_root => |_| {
enumerated_trie_root::<substrate_primitives::Blake2Hasher>(&[&b"zero"[..], &b"one"[..], &b"two"[..]]).to_vec()
enumerated_trie_root::<substrate_primitives::Blake2Hasher>(&[&b"zero"[..], &b"one"[..], &b"two"[..]]).as_ref().to_vec()
},
test_sandbox => |code: &[u8]| {
let ok = execute_sandboxed(code, &[]).is_ok();
-1
View File
@@ -22,7 +22,6 @@ mod slots;
use std::collections::VecDeque;
use futures::{prelude::*, sync::mpsc, try_ready};
use libp2p::PeerId;
use linked_hash_map::LinkedHashMap;
use log::trace;
use lru_cache::LruCache;
use slots::{SlotType, SlotState, Slots};
+3 -3
View File
@@ -11,7 +11,7 @@ rustc_version = "0.2"
[dependencies]
rstd = { package = "sr-std", path = "../sr-std", default-features = false }
primitives = { package = "substrate-primitives", path = "../primitives", default-features = false }
parity-codec = { version = "3.3", default-features = false }
codec = { package="parity-codec", version = "3.3", default-features = false }
hash-db = { version = "0.12", default-features = false }
libsecp256k1 = { version = "0.2.1", optional = true }
tiny-keccak = { version = "1.4.2", optional = true }
@@ -23,7 +23,7 @@ trie = { package = "substrate-trie", path = "../trie", optional = true }
default = ["std"]
std = [
"primitives/std",
"parity-codec/std",
"codec/std",
"rstd/std",
"hash-db/std",
"trie",
@@ -34,4 +34,4 @@ std = [
]
nightly = []
strict = []
wasm-nice-panic-message = []
wasm-nice-panic-message = []
+222 -3
View File
@@ -16,6 +16,8 @@
//! This is part of the Substrate runtime.
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(lang_items))]
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
@@ -25,14 +27,231 @@
#![cfg_attr(feature = "std", doc = "Substrate runtime standard library as compiled when linked with Rust's standard library.")]
#![cfg_attr(not(feature = "std"), doc = "Substrate's runtime standard library as compiled without Rust's standard library.")]
use hash_db::Hasher;
use rstd::vec::Vec;
#[doc(hidden)]
pub use codec;
pub use primitives::Blake2Hasher;
/// Error verifying ECDSA signature
pub enum EcdsaVerifyError {
/// Incorrect value of R or S
BadRS,
/// Incorrect value of V
BadV,
/// Invalid signature
BadSignature,
}
#[cfg(feature = "std")]
include!("../with_std.rs");
/// Trait for things which can be printed.
pub trait Printable {
/// Print the object.
fn print(self);
}
/// Converts a public trait definition into a private trait and set of public functions
/// that assume the trait is implemented for `()` for ease of calling.
macro_rules! export_api {
(
$( #[$trait_attr:meta] )*
pub(crate) trait $trait_name:ident {
$(
$( #[$attr:meta] )*
fn $name:ident
$(< $( $g_name:ident $( : $g_ty:path )? ),+ >)?
( $( $arg:ident : $arg_ty:ty ),* )
$( -> $ret:ty )?
$( where $( $w_name:path : $w_ty:path ),+ )?;
)*
}
) => {
$( #[$trait_attr] )*
pub(crate) trait $trait_name {
$(
$( #[$attr] )*
fn $name $(< $( $g_name $( : $g_ty )? ),+ >)? ( $($arg : $arg_ty ),* ) $( -> $ret )?
$( where $( $w_name : $w_ty ),+ )?;
)*
}
$(
$( #[$attr] )*
pub fn $name $(< $( $g_name $( : $g_ty )? ),+ >)? ( $($arg : $arg_ty ),* ) $( -> $ret )?
$( where $( $w_name : $w_ty ),+ )?
{
#[allow(deprecated)]
<()>:: $name $(::< $( $g_name ),+ > )? ( $( $arg ),* )
}
)*
}
}
export_api! {
pub(crate) trait StorageApi {
/// Get `key` from storage and return a `Vec`, empty if there's a problem.
fn storage(key: &[u8]) -> Option<Vec<u8>>;
/// Get `key` from child storage and return a `Vec`, empty if there's a problem.
fn child_storage(storage_key: &[u8], key: &[u8]) -> Option<Vec<u8>>;
/// Get `key` from storage, placing the value into `value_out` (as much of it as possible) and return
/// the number of bytes that the entry in storage had beyond the offset or None if the storage entry
/// doesn't exist at all. Note that if the buffer is smaller than the storage entry length, the returned
/// number of bytes is not equal to the number of bytes written to the `value_out`.
fn read_storage(key: &[u8], value_out: &mut [u8], value_offset: usize) -> Option<usize>;
/// Get `key` from child storage, placing the value into `value_out` (as much of it as possible) and return
/// the number of bytes that the entry in storage had beyond the offset or None if the storage entry
/// doesn't exist at all. Note that if the buffer is smaller than the storage entry length, the returned
/// number of bytes is not equal to the number of bytes written to the `value_out`.
fn read_child_storage(storage_key: &[u8], key: &[u8], value_out: &mut [u8], value_offset: usize) -> Option<usize>;
/// Set the storage of some particular key to Some value.
fn set_storage(key: &[u8], value: &[u8]);
/// Set the child storage of some particular key to Some value.
fn set_child_storage(storage_key: &[u8], key: &[u8], value: &[u8]);
/// Clear the storage of a key.
fn clear_storage(key: &[u8]);
/// Clear the storage of a key.
fn clear_child_storage(storage_key: &[u8], key: &[u8]);
/// Clear an entire child storage.
fn kill_child_storage(storage_key: &[u8]);
/// Check whether a given `key` exists in storage.
fn exists_storage(key: &[u8]) -> bool;
/// Check whether a given `key` exists in storage.
fn exists_child_storage(storage_key: &[u8], key: &[u8]) -> bool;
/// Clear the storage entries with a key that starts with the given prefix.
fn clear_prefix(prefix: &[u8]);
/// "Commit" all existing operations and compute the resultant storage root.
fn storage_root() -> [u8; 32];
/// "Commit" all existing operations and compute the resultant child storage root.
fn child_storage_root(storage_key: &[u8]) -> Vec<u8>;
/// "Commit" all existing operations and get the resultant storage change root.
fn storage_changes_root(parent_hash: [u8; 32], parent_num: u64) -> Option<[u8; 32]>;
/// A trie root formed from the enumerated items.
/// TODO [#2382] remove (just use `ordered_trie_root` (NOTE currently not implemented for without_std))
fn enumerated_trie_root<H>(input: &[&[u8]]) -> H::Out
where
H: Hasher,
H: self::imp::HasherBounds,
H::Out: Ord
;
/// A trie root formed from the iterated items.
fn trie_root<H, I, A, B>(input: I) -> H::Out
where
I: IntoIterator<Item = (A, B)>,
A: AsRef<[u8]>,
A: Ord,
B: AsRef<[u8]>,
H: Hasher,
H: self::imp::HasherBounds,
H::Out: Ord
;
/// A trie root formed from the enumerated items.
fn ordered_trie_root<H, I, A>(input: I) -> H::Out
where
I: IntoIterator<Item = A>,
A: AsRef<[u8]>,
H: Hasher,
H: self::imp::HasherBounds,
H::Out: Ord
;
}
}
export_api! {
pub(crate) trait OtherApi {
/// The current relay chain identifier.
fn chain_id() -> u64;
/// Print a printable value.
fn print<T>(value: T)
where
T: Printable,
T: Sized
;
}
}
export_api! {
pub(crate) trait CryptoApi {
/// Verify a ed25519 signature.
fn ed25519_verify<P: AsRef<[u8]>>(sig: &[u8; 64], msg: &[u8], pubkey: P) -> bool;
/// Verify an sr25519 signature.
fn sr25519_verify<P: AsRef<[u8]>>(sig: &[u8; 64], msg: &[u8], pubkey: P) -> bool;
/// Verify and recover a SECP256k1 ECDSA signature.
/// - `sig` is passed in RSV format. V should be either 0/1 or 27/28.
/// - returns `Err` if the signature is bad, otherwise the 64-byte pubkey (doesn't include the 0x04 prefix).
fn secp256k1_ecdsa_recover(sig: &[u8; 65], msg: &[u8; 32]) -> Result<[u8; 64], EcdsaVerifyError>;
}
}
export_api! {
pub(crate) trait HashingApi {
/// Conduct a 256-bit Keccak hash.
fn keccak_256(data: &[u8]) -> [u8; 32] ;
/// Conduct a 128-bit Blake2 hash.
fn blake2_128(data: &[u8]) -> [u8; 16];
/// Conduct a 256-bit Blake2 hash.
fn blake2_256(data: &[u8]) -> [u8; 32];
/// Conduct four XX hashes to give a 256-bit result.
fn twox_256(data: &[u8]) -> [u8; 32];
/// Conduct two XX hashes to give a 128-bit result.
fn twox_128(data: &[u8]) -> [u8; 16];
/// Conduct two XX hashes to give a 64-bit result.
fn twox_64(data: &[u8]) -> [u8; 8];
}
}
export_api! {
pub(crate) trait OffchainApi {
/// Submit extrinsic from the runtime.
///
/// Depending on the kind of extrinsic it will either be:
/// 1. scheduled to be included in the next produced block (inherent)
/// 2. added to the pool and propagated (transaction)
fn submit_extrinsic<T: codec::Encode>(data: &T);
}
}
/// API trait that should cover all other APIs.
///
/// Implement this to make sure you implement all APIs.
trait Api: StorageApi + OtherApi + CryptoApi + HashingApi + OffchainApi {}
mod imp {
use super::*;
#[cfg(feature = "std")]
include!("../with_std.rs");
#[cfg(not(feature = "std"))]
include!("../without_std.rs");
}
#[cfg(feature = "std")]
pub use self::imp::{StorageOverlay, ChildrenStorageOverlay, with_storage, with_externalities, TestExternalities};
#[cfg(not(feature = "std"))]
include!("../without_std.rs");
pub use self::imp::ext::*;
+209 -212
View File
@@ -14,14 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
#[doc(hidden)]
pub use parity_codec as codec;
// re-export hashing functions.
pub use primitives::{
use primitives::{
blake2_128, blake2_256, twox_128, twox_256, twox_64, ed25519, Blake2Hasher,
sr25519, Pair
};
pub use tiny_keccak::keccak256 as keccak_256;
// Switch to this after PoC-3
// pub use primitives::BlakeHasher;
pub use substrate_state_machine::{
@@ -33,18 +29,15 @@ pub use substrate_state_machine::{
use environmental::environmental;
use primitives::{hexdisplay::HexDisplay, H256};
use hash_db::Hasher;
#[cfg(feature = "std")]
use std::collections::HashMap;
environmental!(ext: trait Externalities<Blake2Hasher>);
/// A set of key value pairs for storage.
pub type StorageOverlay = HashMap<Vec<u8>, Vec<u8>>;
/// A set of key value pairs for children storage;
pub type ChildrenStorageOverlay = HashMap<Vec<u8>, StorageOverlay>;
/// Additional bounds for `Hasher` trait for with_std.
pub trait HasherBounds {}
impl<T: Hasher> HasherBounds for T {}
/// Returns a `ChildStorageKey` if the given `storage_key` slice is a valid storage
/// key or panics otherwise.
@@ -58,208 +51,216 @@ fn child_storage_key_or_panic(storage_key: &[u8]) -> ChildStorageKey<Blake2Hashe
}
}
/// Get `key` from storage and return a `Vec`, empty if there's a problem.
pub fn storage(key: &[u8]) -> Option<Vec<u8>> {
ext::with(|ext| ext.storage(key).map(|s| s.to_vec()))
impl StorageApi for () {
fn storage(key: &[u8]) -> Option<Vec<u8>> {
ext::with(|ext| ext.storage(key).map(|s| s.to_vec()))
.expect("storage cannot be called outside of an Externalities-provided environment.")
}
fn read_storage(key: &[u8], value_out: &mut [u8], value_offset: usize) -> Option<usize> {
ext::with(|ext| ext.storage(key).map(|value| {
let value = &value[value_offset..];
let written = std::cmp::min(value.len(), value_out.len());
value_out[..written].copy_from_slice(&value[..written]);
value.len()
})).expect("read_storage cannot be called outside of an Externalities-provided environment.")
}
fn child_storage(storage_key: &[u8], key: &[u8]) -> Option<Vec<u8>> {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.child_storage(storage_key, key).map(|s| s.to_vec())
})
.expect("storage cannot be called outside of an Externalities-provided environment.")
}
fn set_storage(key: &[u8], value: &[u8]) {
ext::with(|ext|
ext.set_storage(key.to_vec(), value.to_vec())
);
}
fn read_child_storage(
storage_key: &[u8],
key: &[u8],
value_out: &mut [u8],
value_offset: usize,
) -> Option<usize> {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.child_storage(storage_key, key)
.map(|value| {
let value = &value[value_offset..];
let written = std::cmp::min(value.len(), value_out.len());
value_out[..written].copy_from_slice(&value[..written]);
value.len()
})
})
.expect("read_child_storage cannot be called outside of an Externalities-provided environment.")
}
fn set_child_storage(storage_key: &[u8], key: &[u8], value: &[u8]) {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.set_child_storage(storage_key, key.to_vec(), value.to_vec())
});
}
fn clear_storage(key: &[u8]) {
ext::with(|ext|
ext.clear_storage(key)
);
}
fn clear_child_storage(storage_key: &[u8], key: &[u8]) {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.clear_child_storage(storage_key, key)
});
}
fn kill_child_storage(storage_key: &[u8]) {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.kill_child_storage(storage_key)
});
}
fn exists_storage(key: &[u8]) -> bool {
ext::with(|ext|
ext.exists_storage(key)
).unwrap_or(false)
}
fn exists_child_storage(storage_key: &[u8], key: &[u8]) -> bool {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.exists_child_storage(storage_key, key)
}).unwrap_or(false)
}
fn clear_prefix(prefix: &[u8]) {
ext::with(|ext|
ext.clear_prefix(prefix)
);
}
fn storage_root() -> [u8; 32] {
ext::with(|ext|
ext.storage_root()
).unwrap_or(H256::zero()).into()
}
fn child_storage_root(storage_key: &[u8]) -> Vec<u8> {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.child_storage_root(storage_key)
}).expect("child_storage_root cannot be called outside of an Externalities-provided environment.")
}
fn storage_changes_root(parent_hash: [u8; 32], parent_num: u64) -> Option<[u8; 32]> {
ext::with(|ext|
ext.storage_changes_root(parent_hash.into(), parent_num).map(Into::into)
).unwrap_or(None)
}
fn enumerated_trie_root<H>(input: &[&[u8]]) -> H::Out
where
H: Hasher,
H::Out: Ord,
{
trie::ordered_trie_root::<H, _, _>(input.iter())
}
fn trie_root<H, I, A, B>(input: I) -> H::Out
where
I: IntoIterator<Item = (A, B)>,
A: AsRef<[u8]> + Ord,
B: AsRef<[u8]>,
H: Hasher,
H::Out: Ord,
{
trie::trie_root::<H, _, _, _>(input)
}
fn ordered_trie_root<H, I, A>(input: I) -> H::Out
where
I: IntoIterator<Item = A>,
A: AsRef<[u8]>,
H: Hasher,
H::Out: Ord,
{
trie::ordered_trie_root::<H, _, _>(input)
}
}
/// Get `key` from child storage and return a `Vec`, empty if there's a problem.
pub fn child_storage(storage_key: &[u8], key: &[u8]) -> Option<Vec<u8>> {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.child_storage(storage_key, key).map(|s| s.to_vec())
})
.expect("storage cannot be called outside of an Externalities-provided environment.")
impl OtherApi for () {
fn chain_id() -> u64 {
ext::with(|ext|
ext.chain_id()
).unwrap_or(0)
}
fn print<T: Printable + Sized>(value: T) {
value.print()
}
}
/// Get `key` from storage, placing the value into `value_out` (as much of it as possible) and return
/// the number of bytes that the entry in storage had beyond the offset or None if the storage entry
/// doesn't exist at all. Note that if the buffer is smaller than the storage entry length, the returned
/// number of bytes is not equal to the number of bytes written to the `value_out`.
pub fn read_storage(key: &[u8], value_out: &mut [u8], value_offset: usize) -> Option<usize> {
ext::with(|ext| ext.storage(key).map(|value| {
let value = &value[value_offset..];
let written = ::std::cmp::min(value.len(), value_out.len());
value_out[..written].copy_from_slice(&value[..written]);
value.len()
})).expect("read_storage cannot be called outside of an Externalities-provided environment.")
impl CryptoApi for () {
fn ed25519_verify<P: AsRef<[u8]>>(sig: &[u8; 64], msg: &[u8], pubkey: P) -> bool {
ed25519::Pair::verify_weak(sig, msg, pubkey)
}
fn sr25519_verify<P: AsRef<[u8]>>(sig: &[u8; 64], msg: &[u8], pubkey: P) -> bool {
sr25519::Pair::verify_weak(sig, msg, pubkey)
}
fn secp256k1_ecdsa_recover(sig: &[u8; 65], msg: &[u8; 32]) -> Result<[u8; 64], EcdsaVerifyError> {
let rs = secp256k1::Signature::parse_slice(&sig[0..64]).map_err(|_| EcdsaVerifyError::BadRS)?;
let v = secp256k1::RecoveryId::parse(if sig[64] > 26 { sig[64] - 27 } else { sig[64] } as u8).map_err(|_| EcdsaVerifyError::BadV)?;
let pubkey = secp256k1::recover(&secp256k1::Message::parse(msg), &rs, &v).map_err(|_| EcdsaVerifyError::BadSignature)?;
let mut res = [0u8; 64];
res.copy_from_slice(&pubkey.serialize()[1..65]);
Ok(res)
}
}
/// Get `key` from child storage, placing the value into `value_out` (as much of it as possible) and return
/// the number of bytes that the entry in storage had beyond the offset or None if the storage entry
/// doesn't exist at all. Note that if the buffer is smaller than the storage entry length, the returned
/// number of bytes is not equal to the number of bytes written to the `value_out`.
pub fn read_child_storage(
storage_key: &[u8],
key: &[u8],
value_out: &mut [u8],
value_offset: usize,
) -> Option<usize> {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.child_storage(storage_key, key)
.map(|value| {
let value = &value[value_offset..];
let written = ::std::cmp::min(value.len(), value_out.len());
value_out[..written].copy_from_slice(&value[..written]);
value.len()
})
})
.expect("read_child_storage cannot be called outside of an Externalities-provided environment.")
impl HashingApi for () {
fn keccak_256(data: &[u8]) -> [u8; 32] {
tiny_keccak::keccak256(data)
}
fn blake2_128(data: &[u8]) -> [u8; 16] {
blake2_128(data)
}
fn blake2_256(data: &[u8]) -> [u8; 32] {
blake2_256(data)
}
fn twox_256(data: &[u8]) -> [u8; 32] {
twox_256(data)
}
fn twox_128(data: &[u8]) -> [u8; 16] {
twox_128(data)
}
fn twox_64(data: &[u8]) -> [u8; 8] {
twox_64(data)
}
}
/// Set the storage of a key to some value.
pub fn set_storage(key: &[u8], value: &[u8]) {
ext::with(|ext|
ext.set_storage(key.to_vec(), value.to_vec())
);
impl OffchainApi for () {
fn submit_extrinsic<T: codec::Encode>(data: &T) {
ext::with(|ext| ext
.submit_extrinsic(codec::Encode::encode(data))
.expect("submit_extrinsic can be called only in offchain worker context")
).expect("submit_extrinsic cannot be called outside of an Externalities-provided environment.")
}
}
/// Set the child storage of a key to some value.
pub fn set_child_storage(storage_key: &[u8], key: &[u8], value: &[u8]) {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.set_child_storage(storage_key, key.to_vec(), value.to_vec())
});
}
/// Clear the storage of a key.
pub fn clear_storage(key: &[u8]) {
ext::with(|ext|
ext.clear_storage(key)
);
}
/// Clear the storage of a key.
pub fn clear_child_storage(storage_key: &[u8], key: &[u8]) {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.clear_child_storage(storage_key, key)
});
}
/// Check whether a given `key` exists in storage.
pub fn exists_storage(key: &[u8]) -> bool {
ext::with(|ext|
ext.exists_storage(key)
).unwrap_or(false)
}
/// Check whether a given `key` exists in storage.
pub fn exists_child_storage(storage_key: &[u8], key: &[u8]) -> bool {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.exists_child_storage(storage_key, key)
}).unwrap_or(false)
}
/// Clear the storage entries with a key that starts with the given prefix.
pub fn clear_prefix(prefix: &[u8]) {
ext::with(|ext|
ext.clear_prefix(prefix)
);
}
/// Clear an entire child storage.
pub fn kill_child_storage(storage_key: &[u8]) {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.kill_child_storage(storage_key)
});
}
/// The current relay chain identifier.
pub fn chain_id() -> u64 {
ext::with(|ext|
ext.chain_id()
).unwrap_or(0)
}
/// "Commit" all existing operations and compute the resultant storage root.
pub fn storage_root() -> H256 {
ext::with(|ext|
ext.storage_root()
).unwrap_or(H256::zero())
}
/// "Commit" all existing operations and compute the resultant child storage root.
pub fn child_storage_root(storage_key: &[u8]) -> Vec<u8> {
ext::with(|ext| {
let storage_key = child_storage_key_or_panic(storage_key);
ext.child_storage_root(storage_key)
}).expect("child_storage_root cannot be called outside of an Externalities-provided environment.")
}
/// "Commit" all existing operations and get the resultant storage change root.
pub fn storage_changes_root(parent_hash: [u8; 32], parent_num: u64) -> Option<H256> {
ext::with(|ext|
ext.storage_changes_root(parent_hash.into(), parent_num)
).unwrap_or(None)
}
/// A trie root formed from the enumerated items.
// TODO: remove (just use `ordered_trie_root`)
pub fn enumerated_trie_root<H>(input: &[&[u8]]) -> H::Out
where
H: Hasher,
H::Out: Ord,
{
trie::ordered_trie_root::<H, _, _>(input.iter())
}
/// A trie root formed from the iterated items.
pub fn trie_root<H, I, A, B>(input: I) -> H::Out
where
I: IntoIterator<Item = (A, B)>,
A: AsRef<[u8]> + Ord,
B: AsRef<[u8]>,
H: Hasher,
<H as Hasher>::Out: Ord,
{
trie::trie_root::<H, _, _, _>(input)
}
/// A trie root formed from the enumerated items.
pub fn ordered_trie_root<H, I, A>(input: I) -> H::Out
where
I: IntoIterator<Item = A> + Iterator<Item = A>,
A: AsRef<[u8]>,
H: Hasher,
<H as Hasher>::Out: Ord,
{
trie::ordered_trie_root::<H, _, _>(input)
}
/// Verify a ed25519 signature.
pub fn ed25519_verify<P: AsRef<[u8]>>(sig: &[u8; 64], msg: &[u8], pubkey: P) -> bool {
ed25519::Pair::verify_weak(sig, msg, pubkey)
}
/// Verify an sr25519 signature.
pub fn sr25519_verify<P: AsRef<[u8]>>(sig: &[u8; 64], msg: &[u8], pubkey: P) -> bool {
sr25519::Pair::verify_weak(sig, msg, pubkey)
}
/// Verify and recover a SECP256k1 ECDSA signature.
/// - `sig` is passed in RSV format. V should be either 0/1 or 27/28.
/// - returns `Err` if the signature is bad, otherwise the 64-byte pubkey (doesn't include the 0x04 prefix).
pub fn secp256k1_ecdsa_recover(sig: &[u8; 65], msg: &[u8; 32]) -> Result<[u8; 64], EcdsaVerifyError> {
let rs = secp256k1::Signature::parse_slice(&sig[0..64]).map_err(|_| EcdsaVerifyError::BadRS)?;
let v = secp256k1::RecoveryId::parse(if sig[64] > 26 { sig[64] - 27 } else { sig[64] } as u8).map_err(|_| EcdsaVerifyError::BadV)?;
let pubkey = secp256k1::recover(&secp256k1::Message::parse(msg), &rs, &v).map_err(|_| EcdsaVerifyError::BadSignature)?;
let mut res = [0u8; 64];
res.copy_from_slice(&pubkey.serialize()[1..65]);
Ok(res)
}
/// Submit extrinsic.
pub fn submit_extrinsic<T: codec::Encode>(data: &T) {
ext::with(|ext| ext
.submit_extrinsic(codec::Encode::encode(data))
.expect("submit_extrinsic can be called only in offchain worker context")
).expect("submit_extrinsic cannot be called outside of an Externalities-provided environment.")
}
impl Api for () {}
/// Execute the given closure with global function available whose functionality routes into the
/// externalities `ext`. Forwards the value that the closure returns.
@@ -268,6 +269,12 @@ pub fn with_externalities<R, F: FnOnce() -> R>(ext: &mut Externalities<Blake2Has
ext::using(ext, f)
}
/// A set of key value pairs for storage.
pub type StorageOverlay = HashMap<Vec<u8>, Vec<u8>>;
/// A set of key value pairs for children storage;
pub type ChildrenStorageOverlay = HashMap<Vec<u8>, StorageOverlay>;
/// Execute the given closure with global functions available whose functionality routes into
/// externalities that draw from and populate `storage`. Forwards the value that the closure returns.
pub fn with_storage<R, F: FnOnce() -> R>(storage: &mut StorageOverlay, f: F) -> R {
@@ -279,11 +286,6 @@ pub fn with_storage<R, F: FnOnce() -> R>(storage: &mut StorageOverlay, f: F) ->
r
}
/// Trait for things which can be printed.
pub trait Printable {
fn print(self);
}
impl<'a> Printable for &'a [u8] {
fn print(self) {
println!("Runtime: {}", HexDisplay::from(&self));
@@ -302,11 +304,6 @@ impl Printable for u64 {
}
}
/// Print a printable value.
pub fn print<T: Printable + Sized>(value: T) {
value.print();
}
#[cfg(test)]
mod std_tests {
use super::*;
File diff suppressed because it is too large Load Diff
-6
View File
@@ -2232,7 +2232,6 @@ name = "srml-support"
version = "1.0.0"
dependencies = [
"bitmask 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"once_cell 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"paste 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2280,7 +2279,6 @@ dependencies = [
name = "srml-system"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2488,7 +2486,6 @@ dependencies = [
name = "substrate-keyring"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
"strum 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2522,7 +2519,6 @@ dependencies = [
"hash-db 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hash256-std-hasher 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"impl-serde 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"primitive-types 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2555,7 +2551,6 @@ version = "1.0.0"
dependencies = [
"hash-db 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)",
"heapsize 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2588,7 +2583,6 @@ name = "substrate-test-runtime"
version = "1.0.0"
dependencies = [
"cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"memory-db 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
-13
View File
@@ -2242,7 +2242,6 @@ dependencies = [
name = "srml-aura"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
@@ -2259,7 +2258,6 @@ dependencies = [
name = "srml-balances"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2274,7 +2272,6 @@ dependencies = [
name = "srml-consensus"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
@@ -2302,7 +2299,6 @@ dependencies = [
name = "srml-indices"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2329,7 +2325,6 @@ dependencies = [
name = "srml-session"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2345,7 +2340,6 @@ dependencies = [
name = "srml-staking"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2363,7 +2357,6 @@ dependencies = [
name = "srml-sudo"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
@@ -2378,7 +2371,6 @@ name = "srml-support"
version = "1.0.0"
dependencies = [
"bitmask 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"once_cell 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"paste 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2426,7 +2418,6 @@ dependencies = [
name = "srml-system"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2441,7 +2432,6 @@ dependencies = [
name = "srml-timestamp"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
@@ -2648,7 +2638,6 @@ dependencies = [
name = "substrate-keyring"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
"strum 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2682,7 +2671,6 @@ dependencies = [
"hash-db 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hash256-std-hasher 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"impl-serde 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"primitive-types 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2715,7 +2703,6 @@ version = "1.0.0"
dependencies = [
"hash-db 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)",
"heapsize 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
+1 -1
View File
@@ -59,7 +59,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("substrate-node"),
authoring_version: 10,
spec_version: 66,
impl_version: 66,
impl_version: 67,
apis: RUNTIME_API_VERSIONS,
};
-17
View File
@@ -1367,7 +1367,6 @@ dependencies = [
name = "node-runtime"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"integer-sqrt 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"node-primitives 1.0.0",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2286,7 +2285,6 @@ dependencies = [
name = "srml-aura"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
@@ -2303,7 +2301,6 @@ dependencies = [
name = "srml-balances"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2318,7 +2315,6 @@ dependencies = [
name = "srml-consensus"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
@@ -2368,7 +2364,6 @@ dependencies = [
name = "srml-democracy"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2396,7 +2391,6 @@ dependencies = [
name = "srml-finality-tracker"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
@@ -2427,7 +2421,6 @@ dependencies = [
name = "srml-indices"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2454,7 +2447,6 @@ dependencies = [
name = "srml-session"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2470,7 +2462,6 @@ dependencies = [
name = "srml-staking"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2488,7 +2479,6 @@ dependencies = [
name = "srml-sudo"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
@@ -2503,7 +2493,6 @@ name = "srml-support"
version = "1.0.0"
dependencies = [
"bitmask 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"once_cell 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"paste 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2551,7 +2540,6 @@ dependencies = [
name = "srml-system"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2566,7 +2554,6 @@ dependencies = [
name = "srml-timestamp"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
@@ -2580,7 +2567,6 @@ dependencies = [
name = "srml-treasury"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
@@ -2798,7 +2784,6 @@ dependencies = [
name = "substrate-keyring"
version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
"strum 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2832,7 +2817,6 @@ dependencies = [
"hash-db 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hash256-std-hasher 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"impl-serde 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"primitive-types 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2865,7 +2849,6 @@ version = "1.0.0"
dependencies = [
"hash-db 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)",
"heapsize 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",