Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions native-runtime/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ extern crate polkadot_primitives as primitives;
use std::fmt;
use primitives::ed25519;

pub use std::vec::Vec;
pub use std::rc::Rc;
pub use std::cell::RefCell;
pub use std::boxed::Box;
pub use std::vec;
pub use std::rc;
pub use std::cell;
pub use std::boxed;
pub use std::slice;
pub use std::mem::{size_of, transmute, swap, uninitialized};
pub use std::mem;

/// Prelude of common useful imports.
///
/// This should include only things which are in the normal std prelude.
pub mod prelude {
pub use std::vec::Vec;
pub use std::boxed::Box;
}

pub use polkadot_state_machine::Externalities;

Expand Down
12 changes: 6 additions & 6 deletions primitives/src/hashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@ use blake2_rfc;
use twox_hash;

/// Do a Blake2 512-bit hash and place result in `dest`.
pub fn blake2_512_into(data: &[u8], dest: &mut[u8; 64]) {
pub fn blake2_512_into(data: &[u8], dest: &mut [u8; 64]) {
dest.copy_from_slice(blake2_rfc::blake2b::blake2b(64, &[], data).as_bytes());
}

/// Do a Blake2 512-bit hash and return result.
pub fn blake2_512(data: &[u8]) -> [u8; 64] {
let mut r: [u8; 64] = unsafe { ::std::mem::uninitialized() };
let mut r = [0; 64];
blake2_512_into(data, &mut r);
r
}

/// Do a Blake2 256-bit hash and place result in `dest`.
pub fn blake2_256_into(data: &[u8], dest: &mut[u8; 32]) {
pub fn blake2_256_into(data: &[u8], dest: &mut [u8; 32]) {
dest.copy_from_slice(blake2_rfc::blake2b::blake2b(32, &[], data).as_bytes());
}

/// Do a Blake2 256-bit hash and return result.
pub fn blake2_256(data: &[u8]) -> [u8; 32] {
let mut r: [u8; 32] = unsafe { ::std::mem::uninitialized() };
let mut r = [0; 32];
blake2_256_into(data, &mut r);
r
}

/// Do a Blake2 128-bit hash and place result in `dest`.
pub fn blake2_128_into(data: &[u8], dest: &mut[u8; 16]) {
pub fn blake2_128_into(data: &[u8], dest: &mut [u8; 16]) {
dest.copy_from_slice(blake2_rfc::blake2b::blake2b(16, &[], data).as_bytes());
}

/// Do a Blake2 128-bit hash and return result.
pub fn blake2_128(data: &[u8]) -> [u8; 16] {
let mut r: [u8; 16] = unsafe { ::std::mem::uninitialized() };
let mut r = [0; 16];
blake2_128_into(data, &mut r);
r
}
Expand Down
20 changes: 19 additions & 1 deletion wasm-runtime/polkadot/src/codec/endiansensitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
//! Endian manager.

/// Trait to allow conversion to a know endian representation when sensitive.
pub trait EndianSensitive: Sized {
// note: the copy bound and static lifetimes are necessary for safety of `Slicable` blanket implementation.
pub trait EndianSensitive: Copy + 'static {
fn to_le(self) -> Self { self }
fn to_be(self) -> Self { self }
fn from_le(self) -> Self { self }
Expand Down Expand Up @@ -48,3 +49,20 @@ impl_endians!(u16, u32, u64, usize, i16, i32, i64, isize);
impl_non_endians!(u8, i8, [u8; 1], [u8; 2], [u8; 3], [u8; 4], [u8; 5], [u8; 6], [u8; 7], [u8; 8],
[u8; 10], [u8; 12], [u8; 14], [u8; 16], [u8; 20], [u8; 24], [u8; 28], [u8; 32], [u8; 40],
[u8; 48], [u8; 56], [u8; 64], [u8; 80], [u8; 96], [u8; 112], [u8; 128]);

#[cfg(test)]
mod tests {
use super::EndianSensitive;

#[test]
fn endian_sensitive_is_copy() {
fn _takes_copy<T: Copy>() { }
fn _takes_endian_sensitive<T: EndianSensitive>() { _takes_copy::<T>() }
}

#[test]
fn endian_sensitive_outlives_static() {
fn _takes_static<T: 'static>() { }
fn _takes_endian_sensitive<T: EndianSensitive>() { _takes_static::<T>() }
}
}
2 changes: 1 addition & 1 deletion wasm-runtime/polkadot/src/codec/joiner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Vec<u8> serialiser.

use runtime_support::Vec;
use runtime_support::prelude::*;
use slicable::Slicable;

/// Trait to allow itself to be serialised into a `Vec<u8>`
Expand Down
2 changes: 1 addition & 1 deletion wasm-runtime/polkadot/src/codec/keyedvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Serialiser and prepender.

use runtime_support::Vec;
use runtime_support::prelude::*;
use slicable::Slicable;

/// Trait to allow itselg to be serialised and prepended by a given slice.
Expand Down
23 changes: 13 additions & 10 deletions wasm-runtime/polkadot/src/codec/slicable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

//! Serialisation.

use runtime_support::{Vec, size_of, transmute, uninitialized, slice};
use runtime_support::prelude::*;
use runtime_support::{mem, slice};
use joiner::Joiner;
use endiansensitive::EndianSensitive;

Expand All @@ -33,7 +34,7 @@ pub trait Slicable: Sized {
fn to_vec(&self) -> Vec<u8> {
self.as_slice_then(|s| s.to_vec())
}
fn set_as_slice<F: FnOnce(&mut[u8]) -> bool>(set_slice: F) -> Option<Self>;
fn set_as_slice<F: FnOnce(&mut [u8]) -> bool>(set_slice: F) -> Option<Self>;
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.to_vec())
}
Expand All @@ -44,11 +45,12 @@ pub trait Slicable: Sized {
pub trait NonTrivialSlicable: Slicable {}

impl<T: EndianSensitive> Slicable for T {
fn set_as_slice<F: FnOnce(&mut[u8]) -> bool>(fill_slice: F) -> Option<Self> {
let size = size_of::<T>();
let mut result: T = unsafe { uninitialized() };
fn set_as_slice<F: FnOnce(&mut [u8]) -> bool>(fill_slice: F) -> Option<Self> {
let size = mem::size_of::<T>();
let mut result: T = unsafe { mem::zeroed() };
let result_slice = unsafe {
slice::from_raw_parts_mut(transmute::<*mut T, *mut u8>(&mut result), size)
let ptr = &mut result as *mut _ as *mut u8;
slice::from_raw_parts_mut(ptr, size)
};
if fill_slice(result_slice) {
Some(result.from_le())
Expand All @@ -57,24 +59,25 @@ impl<T: EndianSensitive> Slicable for T {
}
}
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let size = size_of::<Self>();
let size = mem::size_of::<Self>();
self.as_le_then(|le| {
let value_slice = unsafe {
slice::from_raw_parts(transmute::<*const Self, *const u8>(le), size)
let ptr = le as *const _ as *const u8;
slice::from_raw_parts(ptr, size)
};
f(value_slice)
})
}
fn size_of(_value: &[u8]) -> Option<usize> {
Some(size_of::<Self>())
Some(mem::size_of::<Self>())
}
}

impl Slicable for Vec<u8> {
fn from_slice(value: &[u8]) -> Option<Self> {
Some(value[4..].to_vec())
}
fn set_as_slice<F: FnOnce(&mut[u8]) -> bool>(_fill_slice: F) -> Option<Self> {
fn set_as_slice<F: FnOnce(&mut [u8]) -> bool>(_fill_slice: F) -> Option<Self> {
unimplemented!();
}
fn to_vec(&self) -> Vec<u8> {
Expand Down
4 changes: 2 additions & 2 deletions wasm-runtime/polkadot/src/codec/streamreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ use slicable::Slicable;

/// Simple deserialiser.
pub struct StreamReader<'a> {
data: &'a[u8],
data: &'a [u8],
offset: usize,
}

impl<'a> StreamReader<'a> {
/// Create a new deserialiser based on the `data`.
pub fn new(data: &'a[u8]) -> Self {
pub fn new(data: &'a [u8]) -> Self {
StreamReader {
data: data,
offset: 0,
Expand Down
2 changes: 1 addition & 1 deletion wasm-runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub use support::{primitives, function, environment, storable};
#[cfg(test)]
pub use support::{testing, statichex};

use runtime_support::Vec;
use runtime_support::prelude::*;
use slicable::Slicable;
use primitives::{Block, UncheckedTransaction};

Expand Down
2 changes: 1 addition & 1 deletion wasm-runtime/polkadot/src/runtime/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Conensus module for runtime; manages the authority set ready for the native code.

use runtime_support::Vec;
use runtime_support::prelude::*;
use storable::StorageVec;
use primitives::SessionKey;

Expand Down
2 changes: 1 addition & 1 deletion wasm-runtime/polkadot/src/runtime/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! Session manager: is told the validators and allows them to manage their session keys for the
//! consensus module.

use runtime_support::Vec;
use runtime_support::prelude::*;
use keyedvec::KeyedVec;
use storable::{kill, Storable, StorageVec};
use primitives::{AccountID, SessionKey, BlockNumber};
Expand Down
2 changes: 1 addition & 1 deletion wasm-runtime/polkadot/src/runtime/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Staking manager: Handles balances and periodically determines the best set of validators.

use runtime_support::Vec;
use runtime_support::prelude::*;
use keyedvec::KeyedVec;
use storable::{Storable, StorageVec};
use primitives::{BlockNumber, AccountID};
Expand Down
5 changes: 3 additions & 2 deletions wasm-runtime/polkadot/src/runtime/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
//! and depositing logs.

use primitives::{Block, BlockNumber, Hash, UncheckedTransaction, TxOrder, Hashable};
use runtime_support::{Vec, swap};
use runtime_support::mem;
use runtime_support::prelude::*;
use storable::Storable;
use keyedvec::KeyedVec;
use environment::with_env;
Expand Down Expand Up @@ -46,7 +47,7 @@ pub fn execute_block(mut block: Block) {
// populate environment from header.
with_env(|e| {
e.block_number = block.header.number;
swap(&mut e.digest, &mut block.header.digest);
mem::swap(&mut e.digest, &mut block.header.digest);
e.next_log_index = 0;
});

Expand Down
10 changes: 7 additions & 3 deletions wasm-runtime/polkadot/src/support/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

//! Environment API: Allows certain information to be accessed throughout the runtime.

use runtime_support::{Rc, RefCell, transmute, Box};
use runtime_support::boxed::Box;
use runtime_support::mem;
use runtime_support::cell::RefCell;
use runtime_support::rc::Rc;

use primitives::{BlockNumber, Digest};

#[derive(Default)]
Expand Down Expand Up @@ -48,7 +52,7 @@ fn env() -> Rc<RefCell<Environment>> {
let singleton: Rc<RefCell<Environment>> = Rc::new(RefCell::new(Default::default()));

// Put it in the heap so it can outlive this call
SINGLETON = transmute(Box::new(singleton));
SINGLETON = mem::transmute(Box::new(singleton));
}

// Now we give out a copy of the data that is safe to use concurrently.
Expand All @@ -69,7 +73,7 @@ fn env() -> Rc<RefCell<Environment>> {
let singleton: Rc<RefCell<Environment>> = Rc::new(RefCell::new(Default::default()));

// Put it in the heap so it can outlive this call
*s.borrow_mut() = transmute(Box::new(singleton));
*s.borrow_mut() = mem::transmute(Box::new(singleton));
}

// Now we give out a copy of the data that is safe to use concurrently.
Expand Down
20 changes: 10 additions & 10 deletions wasm-runtime/polkadot/src/support/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

//! Primitive types.

use runtime_support::Vec;
use runtime_support::prelude::*;
use streamreader::StreamReader;
use joiner::Joiner;
use slicable::{Slicable, NonTrivialSlicable};
use function::Function;
use runtime_support::{size_of, blake2_256, twox_128, twox_256, ed25519_verify};
use runtime_support::{mem, blake2_256, twox_128, twox_256, ed25519_verify};

#[cfg(test)]
use std::fmt;
Expand Down Expand Up @@ -76,7 +76,7 @@ impl Slicable for Header {
})
}

fn set_as_slice<F: FnOnce(&mut[u8]) -> bool>(_fill_slice: F) -> Option<Self> {
fn set_as_slice<F: FnOnce(&mut [u8]) -> bool>(_fill_slice: F) -> Option<Self> {
unimplemented!();
}

Expand All @@ -90,7 +90,7 @@ impl Slicable for Header {
}

fn size_of(data: &[u8]) -> Option<usize> {
let first_part = size_of::<Hash>() + size_of::<BlockNumber>() + size_of::<Hash>() + size_of::<Hash>();
let first_part = mem::size_of::<Hash>() + mem::size_of::<BlockNumber>() + mem::size_of::<Hash>() + mem::size_of::<Hash>();
let second_part = <Vec<Vec<u8>>>::size_of(&data[first_part..])?;
Some(first_part + second_part)
}
Expand Down Expand Up @@ -122,7 +122,7 @@ impl Slicable for Transaction {
})
}

fn set_as_slice<F: FnOnce(&mut[u8]) -> bool>(_fill_slice: F) -> Option<Self> {
fn set_as_slice<F: FnOnce(&mut [u8]) -> bool>(_fill_slice: F) -> Option<Self> {
unimplemented!();
}

Expand All @@ -135,7 +135,7 @@ impl Slicable for Transaction {
}

fn size_of(data: &[u8]) -> Option<usize> {
let first_part = size_of::<AccountID>() + size_of::<TxOrder>() + size_of::<u8>();
let first_part = mem::size_of::<AccountID>() + mem::size_of::<TxOrder>() + mem::size_of::<u8>();
let second_part = <Vec<u8>>::size_of(&data[first_part..])?;
Some(first_part + second_part)
}
Expand Down Expand Up @@ -200,7 +200,7 @@ impl Slicable for UncheckedTransaction {
})
}

fn set_as_slice<F: FnOnce(&mut[u8]) -> bool>(_fill_slice: F) -> Option<Self> {
fn set_as_slice<F: FnOnce(&mut [u8]) -> bool>(_fill_slice: F) -> Option<Self> {
unimplemented!();
}

Expand All @@ -211,7 +211,7 @@ impl Slicable for UncheckedTransaction {
}

fn size_of(data: &[u8]) -> Option<usize> {
let first_part = size_of::<[u8; 64]>();
let first_part = mem::size_of::<[u8; 64]>();
let second_part = <Transaction>::size_of(&data[first_part..])?;
Some(first_part + second_part)
}
Expand All @@ -237,7 +237,7 @@ impl Slicable for Block {
})
}

fn set_as_slice<F: FnOnce(&mut[u8]) -> bool>(_fill_slice: F) -> Option<Self> {
fn set_as_slice<F: FnOnce(&mut [u8]) -> bool>(_fill_slice: F) -> Option<Self> {
unimplemented!();
}

Expand Down Expand Up @@ -271,7 +271,7 @@ impl<T: NonTrivialSlicable> Slicable for Vec<T> {
Some(r)
}

fn set_as_slice<F: FnOnce(&mut[u8]) -> bool>(_fill_slice: F) -> Option<Self> {
fn set_as_slice<F: FnOnce(&mut [u8]) -> bool>(_fill_slice: F) -> Option<Self> {
unimplemented!();
}

Expand Down
5 changes: 3 additions & 2 deletions wasm-runtime/polkadot/src/support/storable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
use slicable::Slicable;
use endiansensitive::EndianSensitive;
use keyedvec::KeyedVec;
use runtime_support::{self, twox_128, Vec};
use runtime_support::prelude::*;
use runtime_support::{self, twox_128};

/// Trait for a value which may be stored in the storage DB.
pub trait Storable {
Expand All @@ -36,7 +37,7 @@ pub trait Storable {
/// Remove `key` from storage.
pub fn kill(key: &[u8]) { runtime_support::set_storage(&twox_128(key)[..], b""); }

impl<T: Default + Sized + EndianSensitive> Storable for T {
impl<T: Default + EndianSensitive> Storable for T {
fn lookup(key: &[u8]) -> Option<Self> {
Slicable::set_as_slice(|out| runtime_support::read_storage(&twox_128(key)[..], out) == out.len())
}
Expand Down
Loading