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
+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); }
}
}