Clean up sr-io (#3609)

* Move trait `Printable` into `sr-primitives`

* Cleanup runtime io trie_root interfaces

* Remove last generic bits from sr-io interface

* Fix srml-sudo after master merge

* Fix benchmarks

* Runtime bump
This commit is contained in:
Bastian Köcher
2019-09-13 16:39:50 +02:00
committed by GitHub
parent 5cb8c0dc1c
commit 45d64a711c
34 changed files with 309 additions and 372 deletions
+23 -58
View File
@@ -29,12 +29,8 @@
use hash_db::Hasher;
use rstd::vec::Vec;
#[doc(hidden)]
pub use codec;
pub use primitives::Blake2Hasher;
use primitives::{
crypto::KeyTypeId, ed25519, sr25519,
crypto::KeyTypeId, ed25519, sr25519, H256,
offchain::{
Timestamp, HttpRequestId, HttpRequestStatus, HttpError, StorageKind, OpaqueNetworkState,
},
@@ -52,18 +48,6 @@ pub enum EcdsaVerifyError {
pub mod offchain;
/// Trait for things which can be printed.
pub trait Printable {
/// Print the object.
fn print(&self);
}
impl Printable for u8 {
fn print(&self) {
u64::from(*self).print()
}
}
/// 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 {
@@ -73,7 +57,6 @@ macro_rules! export_api {
$(
$( #[$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 ),+ )?;
@@ -84,18 +67,18 @@ macro_rules! export_api {
pub(crate) trait $trait_name {
$(
$( #[$attr] )*
fn $name $(< $( $g_name $( : $g_ty )? ),+ >)? ( $($arg : $arg_ty ),* ) $( -> $ret )?
fn $name ( $($arg : $arg_ty ),* ) $( -> $ret )?
$( where $( $w_name : $w_ty ),+ )?;
)*
}
$(
$( #[$attr] )*
pub fn $name $(< $( $g_name $( : $g_ty )? ),+ >)? ( $($arg : $arg_ty ),* ) $( -> $ret )?
pub fn $name ( $($arg : $arg_ty ),* ) $( -> $ret )?
$( where $( $w_name : $w_ty ),+ )?
{
#[allow(deprecated)]
<()>:: $name $(::< $( $g_name ),+ > )? ( $( $arg ),* )
<()>:: $name ( $( $arg ),* )
}
)*
}
@@ -160,26 +143,10 @@ export_api! {
fn storage_changes_root(parent_hash: [u8; 32]) -> Option<[u8; 32]>;
/// 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
;
fn blake2_256_trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256;
/// 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
;
fn blake2_256_ordered_trie_root(input: Vec<Vec<u8>>) -> H256;
}
}
@@ -188,12 +155,12 @@ export_api! {
/// The current relay chain identifier.
fn chain_id() -> u64;
/// Print a printable value.
fn print<T>(value: T)
where
T: Printable,
T: Sized
;
/// Print a number.
fn print_num(val: u64);
/// Print any valid `utf8` buffer.
fn print_utf8(utf8: &[u8]);
/// Print any `u8` slice as hex.
fn print_hex(data: &[u8]);
}
}
@@ -209,10 +176,10 @@ export_api! {
/// key type in the keystore.
///
/// Returns the raw signature.
fn ed25519_sign<M: AsRef<[u8]>>(
fn ed25519_sign(
id: KeyTypeId,
pubkey: &ed25519::Public,
msg: &M,
msg: &[u8],
) -> Option<ed25519::Signature>;
/// Verify an ed25519 signature.
///
@@ -229,10 +196,10 @@ export_api! {
/// key type in the keystore.
///
/// Returns the raw signature.
fn sr25519_sign<M: AsRef<[u8]>>(
fn sr25519_sign(
id: KeyTypeId,
pubkey: &sr25519::Public,
msg: &M,
msg: &[u8],
) -> Option<sr25519::Signature>;
/// Verify an sr25519 signature.
///
@@ -315,7 +282,7 @@ export_api! {
kind: StorageKind,
key: &[u8],
old_value: Option<&[u8]>,
new_value: &[u8]
new_value: &[u8],
) -> bool;
/// Gets a value from the local storage.
@@ -332,14 +299,14 @@ export_api! {
fn http_request_start(
method: &str,
uri: &str,
meta: &[u8]
meta: &[u8],
) -> Result<HttpRequestId, ()>;
/// Append header to the request.
fn http_request_add_header(
request_id: HttpRequestId,
name: &str,
value: &str
value: &str,
) -> Result<(), ()>;
/// Write a chunk of request body.
@@ -351,7 +318,7 @@ export_api! {
fn http_request_write_body(
request_id: HttpRequestId,
chunk: &[u8],
deadline: Option<Timestamp>
deadline: Option<Timestamp>,
) -> Result<(), HttpError>;
/// Block and wait for the responses for given requests.
@@ -363,16 +330,14 @@ export_api! {
/// Passing `None` as deadline blocks forever.
fn http_response_wait(
ids: &[HttpRequestId],
deadline: Option<Timestamp>
deadline: Option<Timestamp>,
) -> Vec<HttpRequestStatus>;
/// Read all response headers.
///
/// Returns a vector of pairs `(HeaderKey, HeaderValue)`.
/// NOTE response headers have to be read before response body.
fn http_response_headers(
request_id: HttpRequestId
) -> Vec<(Vec<u8>, Vec<u8>)>;
fn http_response_headers(request_id: HttpRequestId) -> Vec<(Vec<u8>, Vec<u8>)>;
/// Read a chunk of body response to given buffer.
///
@@ -385,7 +350,7 @@ export_api! {
fn http_response_read_body(
request_id: HttpRequestId,
buffer: &mut [u8],
deadline: Option<Timestamp>
deadline: Option<Timestamp>,
) -> Result<usize, HttpError>;
}
}
+30 -52
View File
@@ -15,15 +15,14 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use primitives::{
blake2_128, blake2_256, twox_128, twox_256, twox_64, ed25519, Blake2Hasher, sr25519, Pair,
traits::Externalities, child_storage_key::ChildStorageKey,
blake2_128, blake2_256, twox_128, twox_256, twox_64, ed25519, Blake2Hasher, sr25519, Pair, H256,
traits::Externalities, child_storage_key::ChildStorageKey, hexdisplay::HexDisplay, offchain,
};
// Switch to this after PoC-3
// pub use primitives::BlakeHasher;
pub use substrate_state_machine::{BasicExternalities, TestExternalities};
use environmental::environmental;
use primitives::{offchain, hexdisplay::HexDisplay, H256};
use trie::{TrieConfiguration, trie_types::Layout};
use std::{collections::HashMap, convert::TryFrom};
@@ -166,25 +165,12 @@ impl StorageApi for () {
).unwrap_or(Ok(None)).expect("Invalid parent hash passed to storage_changes_root")
}
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,
{
Layout::<H>::trie_root(input)
fn blake2_256_trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
Layout::<Blake2Hasher>::trie_root(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,
{
Layout::<H>::ordered_trie_root(input)
fn blake2_256_ordered_trie_root(input: Vec<Vec<u8>>) -> H256 {
Layout::<Blake2Hasher>::ordered_trie_root(input)
}
}
@@ -195,8 +181,18 @@ impl OtherApi for () {
).unwrap_or(0)
}
fn print<T: Printable + Sized>(value: T) {
value.print()
fn print_num(val: u64) {
println!("{}", val);
}
fn print_utf8(utf8: &[u8]) {
if let Ok(data) = std::str::from_utf8(utf8) {
println!("{}", data)
}
}
fn print_hex(data: &[u8]) {
println!("{}", HexDisplay::from(&data));
}
}
@@ -220,10 +216,10 @@ impl CryptoApi for () {
}).expect("`ed25519_generate` cannot be called outside of an Externalities-provided environment.")
}
fn ed25519_sign<M: AsRef<[u8]>>(
fn ed25519_sign(
id: KeyTypeId,
pubkey: &ed25519::Public,
msg: &M,
msg: &[u8],
) -> Option<ed25519::Signature> {
let pub_key = ed25519::Public::try_from(pubkey.as_ref()).ok()?;
@@ -232,7 +228,7 @@ impl CryptoApi for () {
.expect("No `keystore` associated for the current context!")
.read()
.ed25519_key_pair(id, &pub_key)
.map(|k| k.sign(msg.as_ref()))
.map(|k| k.sign(msg))
}).expect("`ed25519_sign` cannot be called outside of an Externalities-provided environment.")
}
@@ -259,10 +255,10 @@ impl CryptoApi for () {
}).expect("`sr25519_generate` cannot be called outside of an Externalities-provided environment.")
}
fn sr25519_sign<M: AsRef<[u8]>>(
fn sr25519_sign(
id: KeyTypeId,
pubkey: &sr25519::Public,
msg: &M,
msg: &[u8],
) -> Option<sr25519::Signature> {
let pub_key = sr25519::Public::try_from(pubkey.as_ref()).ok()?;
@@ -271,7 +267,7 @@ impl CryptoApi for () {
.expect("No `keystore` associated for the current context!")
.read()
.sr25519_key_pair(id, &pub_key)
.map(|k| k.sign(msg.as_ref()))
.map(|k| k.sign(msg))
}).expect("`sr25519_sign` cannot be called outside of an Externalities-provided environment.")
}
@@ -389,7 +385,7 @@ impl OffchainApi for () {
fn http_request_start(
method: &str,
uri: &str,
meta: &[u8]
meta: &[u8],
) -> Result<offchain::HttpRequestId, ()> {
with_offchain(|ext| {
ext.http_request_start(method, uri, meta)
@@ -399,7 +395,7 @@ impl OffchainApi for () {
fn http_request_add_header(
request_id: offchain::HttpRequestId,
name: &str,
value: &str
value: &str,
) -> Result<(), ()> {
with_offchain(|ext| {
ext.http_request_add_header(request_id, name, value)
@@ -409,7 +405,7 @@ impl OffchainApi for () {
fn http_request_write_body(
request_id: offchain::HttpRequestId,
chunk: &[u8],
deadline: Option<offchain::Timestamp>
deadline: Option<offchain::Timestamp>,
) -> Result<(), offchain::HttpError> {
with_offchain(|ext| {
ext.http_request_write_body(request_id, chunk, deadline)
@@ -418,7 +414,7 @@ impl OffchainApi for () {
fn http_response_wait(
ids: &[offchain::HttpRequestId],
deadline: Option<offchain::Timestamp>
deadline: Option<offchain::Timestamp>,
) -> Vec<offchain::HttpRequestStatus> {
with_offchain(|ext| {
ext.http_response_wait(ids, deadline)
@@ -426,7 +422,7 @@ impl OffchainApi for () {
}
fn http_response_headers(
request_id: offchain::HttpRequestId
request_id: offchain::HttpRequestId,
) -> Vec<(Vec<u8>, Vec<u8>)> {
with_offchain(|ext| {
ext.http_response_headers(request_id)
@@ -436,7 +432,7 @@ impl OffchainApi for () {
fn http_response_read_body(
request_id: offchain::HttpRequestId,
buffer: &mut [u8],
deadline: Option<offchain::Timestamp>
deadline: Option<offchain::Timestamp>,
) -> Result<usize, offchain::HttpError> {
with_offchain(|ext| {
ext.http_response_read_body(request_id, buffer, deadline)
@@ -477,24 +473,6 @@ pub fn with_storage<R, F: FnOnce() -> R>(
r
}
impl<'a> Printable for &'a [u8] {
fn print(&self) {
println!("Runtime: {}", HexDisplay::from(self));
}
}
impl<'a> Printable for &'a str {
fn print(&self) {
println!("Runtime: {}", self);
}
}
impl Printable for u64 {
fn print(&self) {
println!("Runtime: {}", self);
}
}
#[cfg(test)]
mod std_tests {
use super::*;
+60 -96
View File
@@ -126,44 +126,6 @@ pub mod ext {
}
}
/// Ensures we use the right crypto when calling into native
pub trait ExternTrieCrypto: Hasher {
/// A trie root formed from the enumerated items.
fn ordered_trie_root<
A: AsRef<[u8]>,
I: IntoIterator<Item = A>
>(values: I) -> Self::Out;
}
/// Additional bounds for Hasher trait for without_std.
pub trait HasherBounds: ExternTrieCrypto {}
impl<T: ExternTrieCrypto + Hasher> HasherBounds for T {}
// Ensures we use a Blake2_256-flavored Hasher when calling into native
impl ExternTrieCrypto for Blake2Hasher {
fn ordered_trie_root<
A: AsRef<[u8]>,
I: IntoIterator<Item = A>
>(items: I) -> Self::Out {
let mut values = Vec::new();
let mut lengths = Vec::new();
for v in items.into_iter() {
values.extend_from_slice(v.as_ref());
lengths.push((v.as_ref().len() as u32).to_le());
}
let mut result: [u8; 32] = Default::default();
unsafe {
ext_blake2_256_enumerated_trie_root.get()(
values.as_ptr(),
lengths.as_ptr(),
lengths.len() as u32,
result.as_mut_ptr()
);
}
result.into()
}
}
/// Declare extern functions
macro_rules! extern_functions {
(
@@ -228,7 +190,7 @@ pub mod ext {
storage_key_data: *const u8,
storage_key_len: u32,
prefix_data: *const u8,
prefix_len: u32
prefix_len: u32,
);
/// Gets the value of the given key from storage.
///
@@ -255,7 +217,7 @@ pub mod ext {
key_len: u32,
value_data: *mut u8,
value_len: u32,
value_offset: u32
value_offset: u32,
) -> u32;
/// Gets the trie root of the storage.
fn ext_storage_root(result: *mut u8);
@@ -266,7 +228,10 @@ pub mod ext {
/// - `1` if the change trie root was found.
/// - `0` if the change trie root was not found.
fn ext_storage_changes_root(
parent_hash_data: *const u8, parent_hash_len: u32, result: *mut u8) -> u32;
parent_hash_data: *const u8,
parent_hash_len: u32,
result: *mut u8,
) -> u32;
/// A child storage function.
///
@@ -279,7 +244,7 @@ pub mod ext {
key_data: *const u8,
key_len: u32,
value_data: *const u8,
value_len: u32
value_len: u32,
);
/// A child storage function.
///
@@ -290,7 +255,7 @@ pub mod ext {
storage_key_data: *const u8,
storage_key_len: u32,
key_data: *const u8,
key_len: u32
key_len: u32,
);
/// A child storage function.
///
@@ -301,7 +266,7 @@ pub mod ext {
storage_key_data: *const u8,
storage_key_len: u32,
key_data: *const u8,
key_len: u32
key_len: u32,
) -> u32;
/// A child storage function.
///
@@ -319,7 +284,7 @@ pub mod ext {
storage_key_len: u32,
key_data: *const u8,
key_len: u32,
written_out: *mut u32
written_out: *mut u32,
) -> *mut u8;
/// A child storage function.
///
@@ -333,7 +298,7 @@ pub mod ext {
key_len: u32,
value_data: *mut u8,
value_len: u32,
value_offset: u32
value_offset: u32,
) -> u32;
/// Commits all changes and calculates the child-storage root.
///
@@ -498,7 +463,7 @@ pub mod ext {
old_value: *const u8,
old_value_len: u32,
new_value: *const u8,
new_value_len: u32
new_value_len: u32,
) -> u32;
/// Read a value from local storage.
@@ -526,7 +491,7 @@ pub mod ext {
url: *const u8,
url_len: u32,
meta: *const u8,
meta_len: u32
meta_len: u32,
) -> u32;
/// Add a header to the request.
@@ -540,7 +505,7 @@ pub mod ext {
name: *const u8,
name_len: u32,
value: *const u8,
value_len: u32
value_len: u32,
) -> u32;
/// Write a chunk of request body.
@@ -556,7 +521,7 @@ pub mod ext {
request_id: u32,
chunk: *const u8,
chunk_len: u32,
deadline: u64
deadline: u64,
) -> u32;
/// Block and wait for the responses for given requests.
@@ -570,7 +535,7 @@ pub mod ext {
ids: *const u32,
ids_len: u32,
statuses: *mut u32,
deadline: u64
deadline: u64,
);
/// Read all response headers.
@@ -583,7 +548,7 @@ pub mod ext {
/// - In case invalid `id` is passed it returns a pointer to parity-encoded empty vector.
fn ext_http_response_headers(
id: u32,
written_out: *mut u32
written_out: *mut u32,
) -> *mut u8;
/// Read a chunk of body response to given buffer.
@@ -605,7 +570,7 @@ pub mod ext {
id: u32,
buffer: *mut u8,
buffer_len: u32,
deadline: u64
deadline: u64,
) -> u32;
}
}
@@ -777,21 +742,28 @@ impl StorageApi for () {
}
}
fn trie_root<
H: Hasher + ExternTrieCrypto,
I: IntoIterator<Item = (A, B)>,
A: AsRef<[u8]> + Ord,
B: AsRef<[u8]>,
>(_input: I) -> H::Out {
fn blake2_256_trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
unimplemented!()
}
fn ordered_trie_root<
H: Hasher + ExternTrieCrypto,
I: IntoIterator<Item = A>,
A: AsRef<[u8]>
>(values: I) -> H::Out {
H::ordered_trie_root(values)
fn blake2_256_ordered_trie_root(input: Vec<Vec<u8>>) -> H256 {
let mut values = Vec::new();
let mut lengths = Vec::new();
for v in input {
values.extend_from_slice(&v);
lengths.push((v.len() as u32).to_le());
}
let mut result: [u8; 32] = Default::default();
unsafe {
ext_blake2_256_enumerated_trie_root.get()(
values.as_ptr(),
lengths.as_ptr(),
lengths.len() as u32,
result.as_mut_ptr(),
);
}
result.into()
}
}
@@ -802,10 +774,23 @@ impl OtherApi for () {
}
}
fn print<T: Printable + Sized>(value: T) {
value.print()
fn print_num(val: u64) {
unsafe {
ext_print_num.get()(val);
}
}
fn print_utf8(utf8: &[u8]) {
unsafe {
ext_print_utf8.get()(utf8.as_ptr(), utf8.len() as u32);
}
}
fn print_hex(data: &[u8]) {
unsafe {
ext_print_hex.get()(data.as_ptr(), data.len() as u32);
}
}
}
impl HashingApi for () {
@@ -876,18 +861,18 @@ impl CryptoApi for () {
ed25519::Public(res)
}
fn ed25519_sign<M: AsRef<[u8]>>(
fn ed25519_sign(
id: KeyTypeId,
pubkey: &ed25519::Public,
msg: &M,
msg: &[u8],
) -> Option<ed25519::Signature> {
let mut res = [0u8; 64];
let success = unsafe {
ext_ed25519_sign.get()(
id.0.as_ptr(),
pubkey.0.as_ptr(),
msg.as_ref().as_ptr(),
msg.as_ref().len() as u32,
msg.as_ptr(),
msg.len() as u32,
res.as_mut_ptr(),
) == 0
};
@@ -927,18 +912,18 @@ impl CryptoApi for () {
sr25519::Public(res)
}
fn sr25519_sign<M: AsRef<[u8]>>(
fn sr25519_sign(
id: KeyTypeId,
pubkey: &sr25519::Public,
msg: &M,
msg: &[u8],
) -> Option<sr25519::Signature> {
let mut res = [0u8; 64];
let success = unsafe {
ext_sr25519_sign.get()(
id.0.as_ptr(),
pubkey.0.as_ptr(),
msg.as_ref().as_ptr(),
msg.as_ref().len() as u32,
msg.as_ptr(),
msg.len() as u32,
res.as_mut_ptr(),
) == 0
};
@@ -1218,24 +1203,3 @@ unsafe fn from_raw_parts(ptr: *mut u8, len: u32) -> Option<Vec<u8>> {
impl Api for () {}
impl<'a> Printable for &'a [u8] {
fn print(&self) {
unsafe {
ext_print_hex.get()(self.as_ptr(), self.len() as u32);
}
}
}
impl<'a> Printable for &'a str {
fn print(&self) {
unsafe {
ext_print_utf8.get()(self.as_ptr() as *const u8, self.len() as u32);
}
}
}
impl Printable for u64 {
fn print(&self) {
unsafe { ext_print_num.get()(*self); }
}
}