extract out runtime-codec to a separate crate

This commit is contained in:
Robert Habermeier
2018-01-30 18:51:12 +01:00
parent 8e554129ec
commit a68b187f4d
27 changed files with 82 additions and 40 deletions
+6
View File
@@ -553,6 +553,7 @@ dependencies = [
name = "native-runtime" name = "native-runtime"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"polkadot-runtime-codec 0.1.0",
"runtime-std 0.1.0", "runtime-std 0.1.0",
"rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@@ -730,6 +731,7 @@ dependencies = [
"native-runtime 0.1.0", "native-runtime 0.1.0",
"parity-wasm 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)", "parity-wasm 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)",
"polkadot-primitives 0.1.0", "polkadot-primitives 0.1.0",
"polkadot-runtime-codec 0.1.0",
"polkadot-serializer 0.1.0", "polkadot-serializer 0.1.0",
"polkadot-state-machine 0.1.0", "polkadot-state-machine 0.1.0",
"runtime-std 0.1.0", "runtime-std 0.1.0",
@@ -780,6 +782,10 @@ dependencies = [
"polkadot-rpc 0.1.0", "polkadot-rpc 0.1.0",
] ]
[[package]]
name = "polkadot-runtime-codec"
version = "0.1.0"
[[package]] [[package]]
name = "polkadot-serializer" name = "polkadot-serializer"
version = "0.1.0" version = "0.1.0"
+1
View File
@@ -18,6 +18,7 @@ members = [
"rpc", "rpc",
"rpc_servers", "rpc_servers",
"native-runtime", "native-runtime",
"runtime-codec",
"serializer", "serializer",
"state_machine", "state_machine",
"validator", "validator",
+1
View File
@@ -5,6 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
[dependencies] [dependencies]
error-chain = "0.11" error-chain = "0.11"
polkadot-runtime-codec = { path = "../runtime-codec", version = "0.1" }
polkadot-primitives = { path = "../primitives", version = "0.1" } polkadot-primitives = { path = "../primitives", version = "0.1" }
polkadot-serializer = { path = "../serializer", version = "0.1" } polkadot-serializer = { path = "../serializer", version = "0.1" }
polkadot-state-machine = { path = "../state_machine" , version = "0.1" } polkadot-state-machine = { path = "../state_machine" , version = "0.1" }
+1
View File
@@ -27,6 +27,7 @@
#![warn(missing_docs)] #![warn(missing_docs)]
extern crate polkadot_runtime_codec as codec;
extern crate polkadot_primitives as primitives; extern crate polkadot_primitives as primitives;
extern crate polkadot_serializer as serializer; extern crate polkadot_serializer as serializer;
extern crate polkadot_state_machine as state_machine; extern crate polkadot_state_machine as state_machine;
+2 -2
View File
@@ -41,8 +41,8 @@ impl CodeExecutor for NativeExecutor {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use native_runtime::codec::KeyedVec; use codec::KeyedVec;
use native_runtime::support::{TestExternalities, one, two, StaticHexInto}; use native_runtime::support::{one, two, TestExternalities, StaticHexInto};
use native_runtime::runtime::staking::balance; use native_runtime::runtime::staking::balance;
use primitives::twox_128; use primitives::twox_128;
+1 -1
View File
@@ -280,8 +280,8 @@ mod tests {
use rustc_hex::FromHex; use rustc_hex::FromHex;
use primitives::{blake2_256, twox_128}; use primitives::{blake2_256, twox_128};
use runtime_std; use runtime_std;
use codec::KeyedVec;
use native_runtime::support::{one, two, StaticHexInto, TestExternalities}; use native_runtime::support::{one, two, StaticHexInto, TestExternalities};
use native_runtime::codec::KeyedVec;
use native_runtime::runtime::staking::balance; use native_runtime::runtime::staking::balance;
#[test] #[test]
+6 -5
View File
@@ -3,11 +3,12 @@ name = "native-runtime"
version = "0.1.0" version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
polkadot-runtime-codec = { path = "../runtime-codec", version = "0.1" }
runtime-std = { path = "./std", version = "0.1" }
rustc-hex = "1.0"
[features] [features]
default = ["with-std"] default = ["with-std"]
with-std = [] with-std = []
without-std = [] without-std = ["polkadot-runtime-codec/no-std"]
[dependencies]
runtime-std = { path = "./std", version = "0.1" }
rustc-hex = "1.0"
+11
View File
@@ -0,0 +1,11 @@
[package]
name = "polkadot-runtime-codec"
description = "Serialization and deserialization codec for runtime values"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
[features]
no-std = []
default = []
@@ -14,19 +14,20 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>. // along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Vec<u8> serialiser. //! Trait
use runtime_std::prelude::*; use std::iter::Extend;
use super::slicable::Slicable; use super::slicable::Slicable;
/// Trait to allow itself to be serialised into a `Vec<u8>` /// Trait to allow itself to be serialised into a value which can be extended
/// by bytes.
pub trait Joiner { pub trait Joiner {
fn join<T: Slicable + Sized>(self, value: &T) -> Self; fn join<V: Slicable + Sized>(self, value: &V) -> Self;
} }
impl Joiner for Vec<u8> { impl<T> Joiner for T where T: for<'a> Extend<&'a u8> {
fn join<T: Slicable + Sized>(mut self, value: &T) -> Vec<u8> { fn join<V: Slicable + Sized>(mut self, value: &V) -> Self {
value.as_slice_then(|s| self.extend_from_slice(s)); value.as_slice_then(|s| self.extend(s));
self self
} }
} }
@@ -16,8 +16,9 @@
//! Serialiser and prepender. //! Serialiser and prepender.
use runtime_std::prelude::*; use slicable::Slicable;
use super::slicable::Slicable; use std::iter::Extend;
use std::vec::Vec;
/// Trait to allow itselg to be serialised and prepended by a given slice. /// Trait to allow itselg to be serialised and prepended by a given slice.
pub trait KeyedVec { pub trait KeyedVec {
@@ -29,7 +30,7 @@ macro_rules! impl_non_endians {
impl KeyedVec for $t { impl KeyedVec for $t {
fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8> { fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8> {
let mut r = prepend_key.to_vec(); let mut r = prepend_key.to_vec();
r.extend_from_slice(&self[..]); r.extend(&self[..]);
r r
} }
} }
@@ -42,7 +43,7 @@ macro_rules! impl_endians {
fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8> { fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8> {
self.as_slice_then(|slice| { self.as_slice_then(|slice| {
let mut r = prepend_key.to_vec(); let mut r = prepend_key.to_vec();
r.extend_from_slice(slice); r.extend(slice);
r r
}) })
} }
@@ -14,7 +14,11 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>. // along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Codec utils. //! Implements the serialization and deserialization codec for polkadot runtime
//! values.
#![cfg_attr(feature = "no-std", no_std)]
#![cfg_attr(feature = "no-std", feature(alloc))]
mod endiansensitive; mod endiansensitive;
mod slicable; mod slicable;
@@ -27,3 +31,11 @@ pub use self::slicable::{Slicable, NonTrivialSlicable};
pub use self::streamreader::StreamReader; pub use self::streamreader::StreamReader;
pub use self::joiner::Joiner; pub use self::joiner::Joiner;
pub use self::keyedvec::KeyedVec; pub use self::keyedvec::KeyedVec;
#[cfg(feature = "no-std")]
mod std {
extern crate alloc;
pub use core::*;
pub use self::alloc::vec;
}
@@ -16,15 +16,16 @@
//! Serialisation. //! Serialisation.
use runtime_std::prelude::*; use std::{mem, slice};
use runtime_std::{mem, slice}; use std::vec::Vec;
use super::joiner::Joiner; use super::joiner::Joiner;
use super::endiansensitive::EndianSensitive; use super::endiansensitive::EndianSensitive;
/// Trait that allows zero-copy read/write of value-references to/from slices in LE format. /// Trait that allows zero-copy read/write of value-references to/from slices in LE format.
pub trait Slicable: Sized { pub trait Slicable: Sized {
/// Attempt to deserialise the value from a slice.
fn from_slice(value: &[u8]) -> Option<Self> { fn from_slice(value: &[u8]) -> Option<Self> {
Self::set_as_slice(&|out, offset| if value.len() >= out.len() + offset { Self::set_as_slice(|out, offset| if value.len() >= out.len() + offset {
let value = &value[offset..]; let value = &value[offset..];
let len = out.len(); let len = out.len();
out.copy_from_slice(&value[0..len]); out.copy_from_slice(&value[0..len]);
@@ -36,7 +37,7 @@ pub trait Slicable: Sized {
fn to_vec(&self) -> Vec<u8> { fn to_vec(&self) -> Vec<u8> {
self.as_slice_then(|s| s.to_vec()) 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>(fill_slice: F) -> Option<Self>;
fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R { fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.to_vec()) f(&self.to_vec())
} }
@@ -47,7 +48,7 @@ pub trait Slicable: Sized {
pub trait NonTrivialSlicable: Slicable {} pub trait NonTrivialSlicable: Slicable {}
impl<T: EndianSensitive> Slicable for T { impl<T: EndianSensitive> Slicable for T {
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> {
let size = mem::size_of::<T>(); let size = mem::size_of::<T>();
assert!(size > 0, "EndianSensitive can never be implemented for a zero-sized type."); assert!(size > 0, "EndianSensitive can never be implemented for a zero-sized type.");
let mut result: T = unsafe { mem::zeroed() }; let mut result: T = unsafe { mem::zeroed() };
@@ -81,8 +82,8 @@ impl Slicable for Vec<u8> {
fn from_slice(value: &[u8]) -> Option<Self> { fn from_slice(value: &[u8]) -> Option<Self> {
Some(value[4..].to_vec()) 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| { u32::set_as_slice(&fill_slice).and_then(|len| {
let mut v = Vec::with_capacity(len as usize); let mut v = Vec::with_capacity(len as usize);
unsafe { v.set_len(len as usize); } unsafe { v.set_len(len as usize); }
if fill_slice(&mut v, 4) { if fill_slice(&mut v, 4) {
@@ -117,7 +118,7 @@ impl<T: NonTrivialSlicable> Slicable for Vec<T> {
Some(r) 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!(); unimplemented!();
} }
@@ -16,7 +16,7 @@
//! Deserialiser. //! Deserialiser.
use super::slicable::Slicable; use slicable::Slicable;
/// Simple deserialiser. /// Simple deserialiser.
pub struct StreamReader<'a> { pub struct StreamReader<'a> {
+5
View File
@@ -1,3 +1,7 @@
[[package]]
name = "polkadot-runtime-codec"
version = "0.1.0"
[[package]] [[package]]
name = "pwasm-alloc" name = "pwasm-alloc"
version = "0.1.0" version = "0.1.0"
@@ -13,6 +17,7 @@ version = "0.1.0"
name = "runtime-polkadot" name = "runtime-polkadot"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"polkadot-runtime-codec 0.1.0",
"runtime-std 0.1.0", "runtime-std 0.1.0",
] ]
+2 -1
View File
@@ -7,9 +7,10 @@ authors = ["Parity Technologies <admin@parity.io>"]
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
polkadot-runtime-codec = { path = "../../runtime-codec", version = "0.1" }
runtime-std = { path = "../std", version = "0.1" } runtime-std = { path = "../std", version = "0.1" }
[features] [features]
default = ["without-std"] default = ["without-std"]
with-std = [] with-std = []
without-std = [] without-std = ["polkadot-runtime-codec/no-std"]
+2 -2
View File
@@ -17,7 +17,6 @@
//! The Polkadot runtime. This can be compiled with #[no_std], ready for Wasm. //! The Polkadot runtime. This can be compiled with #[no_std], ready for Wasm.
#![cfg_attr(feature = "without-std", no_std)] #![cfg_attr(feature = "without-std", no_std)]
#![cfg_attr(feature = "strict", deny(warnings))]
#[macro_use] #[macro_use]
extern crate runtime_std; extern crate runtime_std;
@@ -25,7 +24,8 @@ extern crate runtime_std;
#[cfg(feature = "with-std")] #[cfg(feature = "with-std")]
extern crate rustc_hex; extern crate rustc_hex;
pub mod codec; extern crate polkadot_runtime_codec as codec;
#[macro_use] #[macro_use]
pub mod support; pub mod support;
pub mod primitives; pub mod primitives;
@@ -38,7 +38,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!(); unimplemented!();
} }
@@ -49,7 +49,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!(); unimplemented!();
} }
@@ -65,10 +65,10 @@ pub struct Proposal {
} }
impl Slicable for Proposal { impl Slicable for Proposal {
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> {
Some(Proposal { Some(Proposal {
function: InternalFunction::from_u8(Slicable::set_as_slice(fill_slice)?)?, function: InternalFunction::from_u8(Slicable::set_as_slice(&fill_slice)?)?,
input_data: Slicable::set_as_slice(&|s, o| fill_slice(s, o + 1))?, input_data: Slicable::set_as_slice(|s, o| fill_slice(s, o + 1))?,
}) })
} }
@@ -45,7 +45,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!(); unimplemented!();
} }
@@ -63,7 +63,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!(); unimplemented!();
} }
@@ -24,7 +24,7 @@ use codec::{Slicable, KeyedVec};
/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry. /// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
pub fn get<T: Slicable + Sized>(key: &[u8]) -> Option<T> { pub fn get<T: Slicable + Sized>(key: &[u8]) -> Option<T> {
Slicable::set_as_slice(&|out, offset| Slicable::set_as_slice(|out, offset|
runtime_std::read_storage(&twox_128(key)[..], out, offset) >= out.len() runtime_std::read_storage(&twox_128(key)[..], out, offset) >= out.len()
) )
} }