Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Prev Previous commit
Next Next commit
tuple implementation for slicable
  • Loading branch information
rphmeier committed Feb 8, 2018
commit 07cd25fe9009766214bbb262aa2891997f840676
83 changes: 83 additions & 0 deletions codec/src/slicable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,73 @@ impl<T: NonTrivialSlicable> Slicable for Vec<T> {
}
}

macro_rules! try_decode {
($t:ty, $initial: expr, $current: expr) => {
match Slicable::from_slice($current) {
Some(x) => x,
None => {
*$current = $initial;
return None;
}
}
}
}

macro_rules! tuple_impl {
($one:ident,) => {
impl<$one: Slicable> Slicable for ($one,) {
fn from_slice(value: &mut &[u8]) -> Option<Self> {
match $one::from_slice(value) {
None => None,
Some($one) => Some(($one,)),
}
}

fn as_slice_then<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.as_slice_then(f)
}
}
};
($first:ident, $($rest:ident,)+) => {
impl<$first: Slicable, $($rest: Slicable),+>
Slicable for
($first, $($rest),+) {
fn from_slice(value: &mut &[u8]) -> Option<Self> {
let initial = *value;
Some((
try_decode!($first, initial, value),
$(try_decode!($rest, initial, value),)+
))
}

fn as_slice_then<RETURN, PROCESS>(&self, f: PROCESS) -> RETURN
where PROCESS: FnOnce(&[u8]) -> RETURN
{
let mut v = Vec::new();

let (
ref $first,
$(ref $rest),+
) = *self;

$first.as_slice_then(|s| v.extend(s));
$($rest.as_slice_then(|s| v.extend(s));)+

f(v.as_slice())
}
}

tuple_impl!($($rest,)+);
}
}

#[allow(non_snake_case)]
mod inner_tuple_impl {
use super::Slicable;
tuple_impl!(A, B, C, D, E, F, G, H, I, J, K,);
}


#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -139,4 +206,20 @@ mod tests {
assert_eq!(slice, &b"\x0b\0\0\0Hello world")
);
}

#[test]
fn failed_tuple_decode_does_not_munch() {
let encoded = (vec![1u8, 3, 5, 9], 6u64).to_vec();
let mut data = &encoded[..];
assert!(<(Vec<u8>, u64, Vec<u8>)>::from_slice(&mut data).is_none());

// failure to decode shouldn't munch anything.
assert_eq!(data.len(), encoded.len());

// full decoding should have munched everything.
let decoded = <(Vec<u8>, u64)>::from_slice(&mut data).unwrap();
assert!(data.is_empty());

assert_eq!(decoded, (vec![1, 3, 5,9], 6));
}
}
15 changes: 15 additions & 0 deletions wasm-runtime/polkadot/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
8 changes: 5 additions & 3 deletions wasm-runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extern crate hex_literal;
#[macro_use]
pub mod support;
pub mod runtime;
pub mod api;

use rstd::prelude::*;
use codec::Slicable;
Expand Down Expand Up @@ -88,22 +89,23 @@ pub fn execute_block(mut input: &[u8]) -> Vec<u8> {
Vec::new()
}

/// Execute a given, serialised, transaction. Returns the empty vector.
/// Execute a transaction. Input data is the concatenation of a serialized header and
/// transaction. Returns the new header.
pub fn execute_transaction(mut input: &[u8]) -> Vec<u8> {
let header = Header::from_slice(&mut input).unwrap();
let utx = UncheckedTransaction::from_slice(&mut input).unwrap();
let header = runtime::system::internal::execute_transaction(utx, header);
header.to_vec()
}

/// Execute a given, serialised, transaction. Returns the empty vector.
/// Finalize a block, given its header.
pub fn finalise_block(mut input: &[u8]) -> Vec<u8> {
let header = Header::from_slice(&mut input).unwrap();
let header = runtime::system::internal::finalise_block(header);
header.to_vec()
}

/// Run whatever tests we have.
/// Run whatever tests we have on a full block.
pub fn run_tests(mut input: &[u8]) -> Vec<u8> {
use runtime_io::print;

Expand Down