Merge branch 'master' into governance

This commit is contained in:
Gav
2018-01-23 20:52:47 +01:00
22 changed files with 105 additions and 60 deletions
+13 -5
View File
@@ -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;
+6 -6
View File
@@ -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
}
@@ -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 }
@@ -53,3 +54,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>() }
}
}
@@ -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>`
@@ -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.
@@ -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;
@@ -35,7 +36,7 @@ pub trait Slicable: Sized {
fn to_vec(&self) -> Vec<u8> {
self.as_slice_then(|s| s.to_vec())
}
fn set_as_slice<F: Fn(&mut[u8], usize) -> bool>(set_slice: &F) -> Option<Self>;
fn set_as_slice<F: Fn(&mut [u8], usize) -> bool>(set_slice: &F) -> Option<Self>;
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.to_vec())
}
@@ -46,11 +47,12 @@ pub trait Slicable: Sized {
pub trait NonTrivialSlicable: Slicable {}
impl<T: EndianSensitive> Slicable for T {
fn set_as_slice<F: Fn(&mut[u8], usize) -> bool>(fill_slice: &F) -> Option<Self> {
let size = size_of::<T>();
let mut result: T = unsafe { uninitialized() };
fn set_as_slice<F: Fn(&mut [u8], usize) -> 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, 0) {
Some(result.from_le())
@@ -59,16 +61,17 @@ 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>())
}
}
@@ -76,7 +79,7 @@ impl Slicable for Vec<u8> {
fn from_slice(value: &[u8]) -> Option<Self> {
Some(value[4..].to_vec())
}
fn set_as_slice<F: Fn(&mut[u8], usize) -> bool>(fill_slice: &F) -> Option<Self> {
fn set_as_slice<F: Fn(&mut [u8], usize) -> bool>(fill_slice: &F) -> Option<Self> {
u32::set_as_slice(fill_slice).and_then(|len| {
let mut v = Vec::with_capacity(len as usize);
unsafe { v.set_len(len as usize); }
@@ -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,
+1 -1
View File
@@ -34,7 +34,7 @@ pub use support::{primitives, function, proposal, environment, storable};
#[cfg(test)]
pub use support::{testing, statichex};
use runtime_support::Vec;
use runtime_support::prelude::*;
use slicable::Slicable;
use primitives::{Block, UncheckedTransaction};
@@ -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;
@@ -25,7 +25,7 @@
//! At the end of the era, all validators approvals are tallied and if there are sufficient to pass
//! the proposal then it is enacted. All items in storage concerning the proposal are reset.
use runtime_support::Vec;
use runtime_support::prelude::*;
use keyedvec::KeyedVec;
use storable::{Storable, StorageVec, kill};
use primitives::{AccountID, Hash, BlockNumber};
@@ -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};
@@ -16,7 +16,8 @@
//! Staking manager: Handles balances and periodically determines the best set of validators.
use runtime_support::{Vec, RefCell};
use runtime_support::prelude::*;
use runtime_support::cell::RefCell;
use keyedvec::KeyedVec;
use storable::{Storable, StorageVec};
use primitives::{BlockNumber, AccountID};
@@ -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;
@@ -47,7 +48,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;
});
@@ -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)]
@@ -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.
@@ -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.
@@ -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;
@@ -86,7 +86,7 @@ impl Slicable for Header {
})
}
fn set_as_slice<F: Fn(&mut[u8], usize) -> bool>(_fill_slice: &F) -> Option<Self> {
fn set_as_slice<F: Fn(&mut [u8], usize) -> bool>(_fill_slice: &F) -> Option<Self> {
unimplemented!();
}
@@ -100,7 +100,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)
}
@@ -132,7 +132,7 @@ impl Slicable for Transaction {
})
}
fn set_as_slice<F: Fn(&mut[u8], usize) -> bool>(_fill_slice: &F) -> Option<Self> {
fn set_as_slice<F: Fn(&mut [u8], usize) -> bool>(_fill_slice: &F) -> Option<Self> {
unimplemented!();
}
@@ -145,7 +145,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)
}
@@ -210,7 +210,7 @@ impl Slicable for UncheckedTransaction {
})
}
fn set_as_slice<F: Fn(&mut[u8], usize) -> bool>(_fill_slice: &F) -> Option<Self> {
fn set_as_slice<F: Fn(&mut [u8], usize) -> bool>(_fill_slice: &F) -> Option<Self> {
unimplemented!();
}
@@ -221,7 +221,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)
}
@@ -247,7 +247,7 @@ impl Slicable for Block {
})
}
fn set_as_slice<F: Fn(&mut[u8], usize) -> bool>(_fill_slice: &F) -> Option<Self> {
fn set_as_slice<F: Fn(&mut [u8], usize) -> bool>(_fill_slice: &F) -> Option<Self> {
unimplemented!();
}
@@ -281,7 +281,7 @@ impl<T: NonTrivialSlicable> Slicable for Vec<T> {
Some(r)
}
fn set_as_slice<F: Fn(&mut[u8], usize) -> bool>(_fill_slice: &F) -> Option<Self> {
fn set_as_slice<F: Fn(&mut [u8], usize) -> bool>(_fill_slice: &F) -> Option<Self> {
unimplemented!();
}
@@ -17,7 +17,8 @@
//! Proposal: This describes a combination of a function ID and data that can be used to call into
//! an internal function.
use runtime_support::{size_of, Vec};
use runtime_support::prelude::*;
use runtime_support::mem;
use slicable::Slicable;
use joiner::Joiner;
use streamreader::StreamReader;
@@ -74,7 +75,7 @@ impl Slicable for Proposal {
}
fn size_of(data: &[u8]) -> Option<usize> {
let first_part = size_of::<u8>();
let first_part = mem::size_of::<u8>();
let second_part = <Vec<u8>>::size_of(&data[first_part..])?;
Some(first_part + second_part)
}
@@ -18,7 +18,8 @@
use slicable::Slicable;
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 {
+19 -11
View File
@@ -4,14 +4,22 @@
#![cfg_attr(feature = "strict", deny(warnings))]
#![feature(alloc)]
//#[macro_use]
extern crate alloc;
pub use alloc::vec::Vec;
pub use alloc::boxed::Box;
pub use alloc::rc::Rc;
pub use core::mem::{transmute, size_of, uninitialized, swap};
pub use alloc::vec;
pub use alloc::boxed;
pub use alloc::rc;
pub use core::mem;
pub use core::slice;
pub use core::cell::{RefCell, Ref, RefMut};
pub use core::cell;
/// Common re-exports that are useful to have in scope.
pub mod prelude {
pub use alloc::vec::Vec;
pub use alloc::boxed::Box;
}
use alloc::vec::Vec;
extern crate pwasm_libc;
extern crate pwasm_alloc;
@@ -68,7 +76,7 @@ pub fn chain_id() -> u64 {
/// Conduct a 256-bit Blake2 hash.
pub fn blake2_256(data: &[u8]) -> [u8; 32] {
unsafe {
let mut result: [u8; 32] = uninitialized();
let mut result: [u8; 32] = Default::default();
// guaranteed to write into result.
ext_blake2_256(&data[0], data.len() as u32, &mut result[0]);
result
@@ -78,7 +86,7 @@ pub fn blake2_256(data: &[u8]) -> [u8; 32] {
/// Conduct four XX hashes to give a 256-bit result.
pub fn twox_256(data: &[u8]) -> [u8; 32] {
unsafe {
let mut result: [u8; 32] = uninitialized();
let mut result: [u8; 32] = Default::default();
// guaranteed to write into result.
ext_twox_256(&data[0], data.len() as u32, &mut result[0]);
result
@@ -88,7 +96,7 @@ pub fn twox_256(data: &[u8]) -> [u8; 32] {
/// Conduct two XX hashes to give a 256-bit result.
pub fn twox_128(data: &[u8]) -> [u8; 16] {
unsafe {
let mut result: [u8; 16] = uninitialized();
let mut result: [u8; 16] = Default::default();
// guaranteed to write into result.
ext_twox_128(&data[0], data.len() as u32, &mut result[0]);
result
@@ -109,7 +117,7 @@ pub trait Printable {
impl<'a> Printable for &'a [u8] {
fn print(self) {
unsafe {
ext_print(&self[0] as *const u8, self.len() as u32);
ext_print(self.as_ptr(), self.len() as u32);
}
}
}
@@ -132,7 +140,7 @@ macro_rules! impl_stubs {
#[no_mangle]
pub fn $name(input_data: *mut u8, input_len: usize) -> u64 {
let input = unsafe {
$crate::Vec::from_raw_parts(input_data, input_len, input_len)
$crate::vec::Vec::from_raw_parts(input_data, input_len, input_len)
};
let output = super::$name(input);