Upgradeable validation functions (#918)

* upgrade primitives to allow changing validation function

* set up storage schema for old parachains code

* fix compilation errors

* fix test compilation

* add some tests for past code meta

* most of the runtime logic for code upgrades

* implement old-code pruning

* add a couple tests

* clean up remaining TODOs

* add a whole bunch of tests for runtime functionality

* remove unused function

* fix runtime compilation

* extract some primitives to parachain crate

* add validation-code upgrades to validation params and result

* extend validation params with code upgrade fields

* provide maximums to validation params

* port test-parachains

* add a code-upgrader test-parachain and tests

* fix collator tests

* move test-parachains to own folder to work around compilation errors

* fix test compilation

* update the Cargo.lock

* fix parachains tests

* remove dbg! invocation

* use new pool in code-upgrader

* bump lockfile

* link TODO to issue
This commit is contained in:
Robert Habermeier
2020-04-06 10:43:19 -04:00
committed by GitHub
parent b31b52dddf
commit 10cec3b591
43 changed files with 1830 additions and 444 deletions
+11 -10
View File
@@ -6,13 +6,18 @@ description = "Types and utilities for creating and working with parachains"
edition = "2018"
[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = [ "derive" ] }
derive_more = { version = "0.99.2", optional = true }
serde = { version = "1.0.102", default-features = false, features = [ "derive" ], optional = true }
# note: special care is taken to avoid inclusion of `sp-io` externals when compiling
# this crate for WASM. This is critical to avoid forcing all parachain WASM into implementing
# various unnecessary Substrate-specific endpoints.
codec = { package = "parity-scale-codec", version = "1.1.0", default-features = false, features = [ "derive" ] }
sp-std = { package = "sp-std", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime-interface = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-wasm-interface = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
# all optional crates.
derive_more = { version = "0.99.2", optional = true }
serde = { version = "1.0.102", default-features = false, features = [ "derive" ], optional = true }
sp-runtime-interface = { git = "https://github.com/paritytech/substrate", branch = "master", optional = true, default-features = false }
sp-externalities = { git = "https://github.com/paritytech/substrate", branch = "master", optional = true }
sc-executor = { git = "https://github.com/paritytech/substrate", branch = "master", optional = true }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", optional = true }
@@ -22,14 +27,9 @@ log = { version = "0.4.8", optional = true }
[target.'cfg(not(target_os = "unknown"))'.dependencies]
shared_memory = { version = "0.10.0", optional = true }
[dev-dependencies]
tiny-keccak = "1.5.0"
adder = { path = "../test-parachains/adder" }
halt = { path = "../test-parachains/halt" }
[features]
default = ["std"]
wasm-api = []
wasm-api = ["sp-runtime-interface"]
std = [
"codec/std",
"derive_more",
@@ -39,6 +39,7 @@ std = [
"sp-core/std",
"parking_lot",
"log",
"sp-runtime-interface",
"sp-runtime-interface/std",
"sp-externalities",
"sc-executor",
+1 -174
View File
@@ -45,182 +45,9 @@
#[cfg(feature = "std")]
pub mod wasm_executor;
pub mod primitives;
mod wasm_api;
use sp_std::vec::Vec;
use codec::{Encode, Decode, CompactAs};
use sp_core::{RuntimeDebug, TypeId};
#[cfg(all(not(feature = "std"), feature = "wasm-api"))]
pub use wasm_api::*;
/// Validation parameters for evaluating the parachain validity function.
// TODO: balance downloads (https://github.com/paritytech/polkadot/issues/220)
#[derive(PartialEq, Eq, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Encode))]
pub struct ValidationParams {
/// The collation body.
pub block_data: Vec<u8>,
/// Previous head-data.
pub parent_head: Vec<u8>,
}
/// The result of parachain validation.
// TODO: egress and balance uploads (https://github.com/paritytech/polkadot/issues/220)
#[derive(PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Debug, Decode))]
pub struct ValidationResult {
/// New head data that should be included in the relay chain state.
pub head_data: Vec<u8>,
}
/// Unique identifier of a parachain.
#[derive(
Clone, CompactAs, Copy, Decode, Default, Encode, Eq,
Hash, Ord, PartialEq, PartialOrd, RuntimeDebug,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize, derive_more::Display))]
pub struct Id(u32);
impl TypeId for Id {
const TYPE_ID: [u8; 4] = *b"para";
}
/// Type for determining the active set of parachains.
pub trait ActiveThreads {
/// Return the current ordered set of `Id`s of active parathreads.
fn active_threads() -> Vec<Id>;
}
impl From<Id> for u32 {
fn from(x: Id) -> Self { x.0 }
}
impl From<u32> for Id {
fn from(x: u32) -> Self { Id(x) }
}
const USER_INDEX_START: u32 = 1000;
/// The ID of the first user (non-system) parachain.
pub const LOWEST_USER_ID: Id = Id(USER_INDEX_START);
impl Id {
/// Create an `Id`.
pub const fn new(id: u32) -> Self {
Self(id)
}
/// Returns `true` if this parachain runs with system-level privileges.
pub fn is_system(&self) -> bool { self.0 < USER_INDEX_START }
}
impl sp_std::ops::Add<u32> for Id {
type Output = Self;
fn add(self, other: u32) -> Self {
Self(self.0 + other)
}
}
// TODO: Remove all of this, move sp-runtime::AccountIdConversion to own crate and and use that.
// #360
struct TrailingZeroInput<'a>(&'a [u8]);
impl<'a> codec::Input for TrailingZeroInput<'a> {
fn remaining_len(&mut self) -> Result<Option<usize>, codec::Error> {
Ok(None)
}
fn read(&mut self, into: &mut [u8]) -> Result<(), codec::Error> {
let len = into.len().min(self.0.len());
into[..len].copy_from_slice(&self.0[..len]);
for i in &mut into[len..] {
*i = 0;
}
self.0 = &self.0[len..];
Ok(())
}
}
/// This type can be converted into and possibly from an AccountId (which itself is generic).
pub trait AccountIdConversion<AccountId>: Sized {
/// Convert into an account ID. This is infallible.
fn into_account(&self) -> AccountId;
/// Try to convert an account ID into this type. Might not succeed.
fn try_from_account(a: &AccountId) -> Option<Self>;
}
/// Format is b"para" ++ encode(parachain ID) ++ 00.... where 00... is indefinite trailing
/// zeroes to fill AccountId.
impl<T: Encode + Decode + Default> AccountIdConversion<T> for Id {
fn into_account(&self) -> T {
(b"para", self).using_encoded(|b|
T::decode(&mut TrailingZeroInput(b))
).unwrap_or_default()
}
fn try_from_account(x: &T) -> Option<Self> {
x.using_encoded(|d| {
if &d[0..4] != b"para" { return None }
let mut cursor = &d[4..];
let result = Decode::decode(&mut cursor).ok()?;
if cursor.iter().all(|x| *x == 0) {
Some(result)
} else {
None
}
})
}
}
/// Which origin a parachain's message to the relay chain should be dispatched from.
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
#[repr(u8)]
pub enum ParachainDispatchOrigin {
/// As a simple `Origin::Signed`, using `ParaId::account_id` as its value. This is good when
/// interacting with standard modules such as `balances`.
Signed,
/// As the special `Origin::Parachain(ParaId)`. This is good when interacting with parachain-
/// aware modules which need to succinctly verify that the origin is a parachain.
Parachain,
/// As the simple, superuser `Origin::Root`. This can only be done on specially permissioned
/// parachains.
Root,
}
impl sp_std::convert::TryFrom<u8> for ParachainDispatchOrigin {
type Error = ();
fn try_from(x: u8) -> core::result::Result<ParachainDispatchOrigin, ()> {
const SIGNED: u8 = ParachainDispatchOrigin::Signed as u8;
const PARACHAIN: u8 = ParachainDispatchOrigin::Parachain as u8;
Ok(match x {
SIGNED => ParachainDispatchOrigin::Signed,
PARACHAIN => ParachainDispatchOrigin::Parachain,
_ => return Err(()),
})
}
}
/// A message from a parachain to its Relay Chain.
#[derive(Clone, PartialEq, Eq, Encode, Decode, sp_runtime_interface::pass_by::PassByCodec)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct UpwardMessage {
/// The origin for the message to be sent from.
pub origin: ParachainDispatchOrigin,
/// The message data.
pub data: Vec<u8>,
}
/// An incoming message.
#[derive(PartialEq, Eq, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Encode))]
pub struct IncomingMessage {
/// The source parachain.
pub source: Id,
/// The data of the message.
pub data: Vec<u8>,
}
+221
View File
@@ -0,0 +1,221 @@
// Copyright 2020 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/>.
//! Primitive types which are strictly necessary from a parachain-execution point
//! of view.
use sp_std::vec::Vec;
use codec::{Encode, Decode, CompactAs};
use sp_core::{RuntimeDebug, TypeId};
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
#[cfg(feature = "std")]
use sp_core::bytes;
/// The block number of the relay chain.
/// 32-bits will allow for 136 years of blocks assuming 1 block per second.
pub type RelayChainBlockNumber = u32;
/// Parachain head data included in the chain.
#[derive(PartialEq, Eq, Clone, PartialOrd, Ord, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug, Default))]
pub struct HeadData(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
/// Parachain validation code.
#[derive(Default, PartialEq, Eq, Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct ValidationCode(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
/// Parachain block data.
///
/// Contains everything required to validate para-block, may contain block and witness data.
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct BlockData(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
/// Unique identifier of a parachain.
#[derive(
Clone, CompactAs, Copy, Decode, Default, Encode, Eq,
Hash, Ord, PartialEq, PartialOrd, RuntimeDebug,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize, derive_more::Display))]
pub struct Id(u32);
impl TypeId for Id {
const TYPE_ID: [u8; 4] = *b"para";
}
impl From<Id> for u32 {
fn from(x: Id) -> Self { x.0 }
}
impl From<u32> for Id {
fn from(x: u32) -> Self { Id(x) }
}
const USER_INDEX_START: u32 = 1000;
/// The ID of the first user (non-system) parachain.
pub const LOWEST_USER_ID: Id = Id(USER_INDEX_START);
impl Id {
/// Create an `Id`.
pub const fn new(id: u32) -> Self {
Self(id)
}
/// Returns `true` if this parachain runs with system-level privileges.
pub fn is_system(&self) -> bool { self.0 < USER_INDEX_START }
}
impl sp_std::ops::Add<u32> for Id {
type Output = Self;
fn add(self, other: u32) -> Self {
Self(self.0 + other)
}
}
/// This type can be converted into and possibly from an AccountId (which itself is generic).
pub trait AccountIdConversion<AccountId>: Sized {
/// Convert into an account ID. This is infallible.
fn into_account(&self) -> AccountId;
/// Try to convert an account ID into this type. Might not succeed.
fn try_from_account(a: &AccountId) -> Option<Self>;
}
// TODO: Remove all of this, move sp-runtime::AccountIdConversion to own crate and and use that.
// #360
struct TrailingZeroInput<'a>(&'a [u8]);
impl<'a> codec::Input for TrailingZeroInput<'a> {
fn remaining_len(&mut self) -> Result<Option<usize>, codec::Error> {
Ok(None)
}
fn read(&mut self, into: &mut [u8]) -> Result<(), codec::Error> {
let len = into.len().min(self.0.len());
into[..len].copy_from_slice(&self.0[..len]);
for i in &mut into[len..] {
*i = 0;
}
self.0 = &self.0[len..];
Ok(())
}
}
/// Format is b"para" ++ encode(parachain ID) ++ 00.... where 00... is indefinite trailing
/// zeroes to fill AccountId.
impl<T: Encode + Decode + Default> AccountIdConversion<T> for Id {
fn into_account(&self) -> T {
(b"para", self).using_encoded(|b|
T::decode(&mut TrailingZeroInput(b))
).unwrap_or_default()
}
fn try_from_account(x: &T) -> Option<Self> {
x.using_encoded(|d| {
if &d[0..4] != b"para" { return None }
let mut cursor = &d[4..];
let result = Decode::decode(&mut cursor).ok()?;
if cursor.iter().all(|x| *x == 0) {
Some(result)
} else {
None
}
})
}
}
/// Which origin a parachain's message to the relay chain should be dispatched from.
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
#[repr(u8)]
pub enum ParachainDispatchOrigin {
/// As a simple `Origin::Signed`, using `ParaId::account_id` as its value. This is good when
/// interacting with standard modules such as `balances`.
Signed,
/// As the special `Origin::Parachain(ParaId)`. This is good when interacting with parachain-
/// aware modules which need to succinctly verify that the origin is a parachain.
Parachain,
/// As the simple, superuser `Origin::Root`. This can only be done on specially permissioned
/// parachains.
Root,
}
impl sp_std::convert::TryFrom<u8> for ParachainDispatchOrigin {
type Error = ();
fn try_from(x: u8) -> core::result::Result<ParachainDispatchOrigin, ()> {
const SIGNED: u8 = ParachainDispatchOrigin::Signed as u8;
const PARACHAIN: u8 = ParachainDispatchOrigin::Parachain as u8;
Ok(match x {
SIGNED => ParachainDispatchOrigin::Signed,
PARACHAIN => ParachainDispatchOrigin::Parachain,
_ => return Err(()),
})
}
}
/// A message from a parachain to its Relay Chain.
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(
any(feature = "std", feature = "wasm-api"),
derive(sp_runtime_interface::pass_by::PassByCodec,
))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct UpwardMessage {
/// The origin for the message to be sent from.
pub origin: ParachainDispatchOrigin,
/// The message data.
pub data: Vec<u8>,
}
/// Validation parameters for evaluating the parachain validity function.
// TODO: balance downloads (https://github.com/paritytech/polkadot/issues/220)
#[derive(PartialEq, Eq, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Encode))]
pub struct ValidationParams {
/// The collation body.
pub block_data: BlockData,
/// Previous head-data.
pub parent_head: HeadData,
/// The maximum code size permitted, in bytes.
pub max_code_size: u32,
/// The maximum head-data size permitted, in bytes.
pub max_head_data_size: u32,
/// The current relay-chain block number.
pub relay_chain_height: RelayChainBlockNumber,
/// Whether a code upgrade is allowed or not, and at which height the upgrade
/// would be applied after, if so. The parachain logic should apply any upgrade
/// issued in this block after the first block
/// with `relay_chain_height` at least this value, if `Some`. if `None`, issue
/// no upgrade.
pub code_upgrade_allowed: Option<RelayChainBlockNumber>,
}
/// The result of parachain validation.
// TODO: egress and balance uploads (https://github.com/paritytech/polkadot/issues/220)
#[derive(PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Debug, Decode))]
pub struct ValidationResult {
/// New head data that should be included in the relay chain state.
pub head_data: HeadData,
/// An update to the validation code that should be scheduled in the relay chain.
pub new_validation_code: Option<ValidationCode>,
}
+7 -3
View File
@@ -16,7 +16,9 @@
//! Utilities for writing parachain WASM.
use crate::UpwardMessage;
#[cfg(any(feature = "std", all(not(feature = "std"), feature = "wasm-api")))]
use crate::primitives::UpwardMessage;
#[cfg(any(feature = "std", all(not(feature = "std"), feature = "wasm-api")))]
use sp_runtime_interface::runtime_interface;
#[cfg(feature = "std")]
use sp_externalities::ExternalitiesExt;
@@ -42,7 +44,9 @@ pub trait Parachain {
/// Offset and length must have been provided by the validation
/// function's entry point.
#[cfg(not(feature = "std"))]
pub unsafe fn load_params(params: *const u8, len: usize) -> crate::ValidationParams {
pub unsafe fn load_params(params: *const u8, len: usize)
-> crate::primitives::ValidationParams
{
let mut slice = sp_std::slice::from_raw_parts(params, len);
codec::Decode::decode(&mut slice).expect("Invalid input data")
@@ -53,6 +57,6 @@ pub unsafe fn load_params(params: *const u8, len: usize) -> crate::ValidationPar
/// As described in the crate docs, this is a pointer to the appended length
/// of the vector.
#[cfg(not(feature = "std"))]
pub fn write_result(result: &crate::ValidationResult) -> u64 {
pub fn write_result(result: &crate::primitives::ValidationResult) -> u64 {
sp_core::to_substrate_wasm_fn_return_value(&result)
}
+1 -1
View File
@@ -21,7 +21,7 @@
//! a WASM VM for re-execution of a parachain candidate.
use std::any::{TypeId, Any};
use crate::{ValidationParams, ValidationResult, UpwardMessage};
use crate::primitives::{ValidationParams, ValidationResult, UpwardMessage};
use codec::{Decode, Encode};
use sp_core::storage::{ChildStorageKey, ChildInfo};
use sp_core::traits::CallInWasm;
@@ -18,7 +18,7 @@
use std::{process, env, sync::Arc, sync::atomic, mem};
use codec::{Decode, Encode, EncodeAppend};
use crate::{ValidationParams, ValidationResult, UpwardMessage};
use crate::primitives::{ValidationParams, ValidationResult, UpwardMessage};
use super::{validate_candidate_internal, Error, Externalities};
use super::{MAX_CODE_MEM, MAX_RUNTIME_MEM};
use shared_memory::{SharedMem, SharedMemConf, EventState, WriteLockable, EventWait, EventSet};
@@ -0,0 +1,2 @@
target/
Cargo.lock
@@ -0,0 +1,23 @@
[package]
name = "test-parachains"
version = "0.7.22"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Integration tests using the test-parachains"
edition = "2018"
[dependencies]
tiny-keccak = "1.5.0"
codec = { package = "parity-scale-codec", version = "1.1.0", default-features = false, features = ["derive"] }
parachain = { package = "polkadot-parachain", path = ".." }
adder = { package = "test-parachain-adder", path = "adder" }
halt = { package = "test-parachain-halt", path = "halt" }
code-upgrader = { package = "test-parachain-code-upgrader", path = "code-upgrader" }
[features]
default = [ "std" ]
std = [
"adder/std",
"halt/std",
"code-upgrader/std",
]
@@ -0,0 +1,3 @@
# Test Parachains
Each parachain consists of three parts: a `#![no_std]` library with the main execution logic, a WASM crate which wraps this logic, and a collator node.
@@ -0,0 +1,23 @@
[package]
name = "test-parachain-adder"
version = "0.7.29-pre1"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Test parachain which adds to a number as its state transition"
edition = "2018"
build = "build.rs"
[dependencies]
parachain = { package = "polkadot-parachain", path = "../../", default-features = false, features = [ "wasm-api" ] }
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = ["derive"] }
tiny-keccak = "1.5.0"
dlmalloc = { version = "0.1.3", features = [ "global" ] }
# We need to make sure the global allocator is disabled until we have support of full substrate externalities
runtime-io = { package = "sp-io", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, features = [ "disable_allocator" ] }
[build-dependencies]
wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.5" }
[features]
default = [ "std" ]
std = ["parachain/std"]
@@ -0,0 +1,25 @@
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Substrate 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.
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
use wasm_builder_runner::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_git("https://github.com/paritytech/substrate.git", "8c672e107789ed10973d937ba8cac245404377e2")
.export_heap_base()
.build()
}
@@ -0,0 +1,17 @@
[package]
name = "test-parachain-adder-collator"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
adder = { package = "test-parachain-adder", path = ".." }
parachain = { package = "polkadot-parachain", path = "../../.." }
collator = { package = "polkadot-collator", path = "../../../../collator" }
primitives = { package = "polkadot-primitives", path = "../../../../primitives" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
client = { package = "sc-client", git = "https://github.com/paritytech/substrate", branch = "master" }
client-api = { package = "sc-client-api", git = "https://github.com/paritytech/substrate", branch = "master" }
parking_lot = "0.10.0"
codec = { package = "parity-scale-codec", version = "1.2.0" }
futures = "0.3.4"
@@ -0,0 +1,149 @@
// Copyright 2018-2020 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/>.
//! Collator for polkadot
use std::collections::HashMap;
use std::sync::Arc;
use adder::{HeadData as AdderHead, BlockData as AdderBody};
use sp_core::Pair;
use codec::{Encode, Decode};
use primitives::{
Hash,
parachain::{HeadData, BlockData, Id as ParaId, LocalValidationData, GlobalValidationSchedule},
};
use collator::{
InvalidHead, ParachainContext, Network, BuildParachainContext, load_spec, Configuration,
};
use parking_lot::Mutex;
use futures::future::{Ready, ok, err};
const GENESIS: AdderHead = AdderHead {
number: 0,
parent_hash: [0; 32],
post_state: [
1, 27, 77, 3, 221, 140, 1, 241, 4, 145, 67, 207, 156, 76, 129, 126, 75,
22, 127, 29, 27, 131, 229, 198, 240, 241, 13, 137, 186, 30, 123, 206
],
};
const GENESIS_BODY: AdderBody = AdderBody {
state: 0,
add: 0,
};
#[derive(Clone)]
struct AdderContext {
db: Arc<Mutex<HashMap<AdderHead, AdderBody>>>,
/// We store it here to make sure that our interfaces require the correct bounds.
_network: Option<Arc<dyn Network>>,
}
/// The parachain context.
impl ParachainContext for AdderContext {
type ProduceCandidate = Ready<Result<(BlockData, HeadData), InvalidHead>>;
fn produce_candidate(
&mut self,
_relay_parent: Hash,
_global_validation: GlobalValidationSchedule,
local_validation: LocalValidationData,
) -> Self::ProduceCandidate
{
let adder_head = match AdderHead::decode(&mut &local_validation.parent_head.0[..]) {
Ok(adder_head) => adder_head,
Err(_) => return err(InvalidHead)
};
let mut db = self.db.lock();
let last_body = if adder_head == GENESIS {
GENESIS_BODY
} else {
db.get(&adder_head)
.expect("All past bodies stored since this is the only collator")
.clone()
};
let next_body = AdderBody {
state: last_body.state.overflowing_add(last_body.add).0,
add: adder_head.number % 100,
};
let next_head = adder::execute(adder_head.hash(), adder_head, &next_body)
.expect("good execution params; qed");
let encoded_head = HeadData(next_head.encode());
let encoded_body = BlockData(next_body.encode());
println!("Created collation for #{}, post-state={}",
next_head.number, next_body.state.overflowing_add(next_body.add).0);
db.insert(next_head.clone(), next_body);
ok((encoded_body, encoded_head))
}
}
impl BuildParachainContext for AdderContext {
type ParachainContext = Self;
fn build<B, E, R, SP, Extrinsic>(
self,
_: Arc<collator::PolkadotClient<B, E, R>>,
_: SP,
network: impl Network + Clone + 'static,
) -> Result<Self::ParachainContext, ()> {
Ok(Self { _network: Some(Arc::new(network)), ..self })
}
}
fn main() {
let key = Arc::new(Pair::from_seed(&[1; 32]));
let id: ParaId = 100.into();
println!("Starting adder collator with genesis: ");
{
let encoded = GENESIS.encode();
println!("Dec: {:?}", encoded);
print!("Hex: 0x");
for byte in encoded {
print!("{:02x}", byte);
}
println!();
}
let context = AdderContext {
db: Arc::new(Mutex::new(HashMap::new())),
_network: None,
};
let mut config = Configuration::default();
config.chain_spec = Some(load_spec("dev", false).unwrap());
let res = collator::run_collator(
context,
id,
key,
config,
);
if let Err(e) = res {
println!("{}", e);
}
}
@@ -0,0 +1,90 @@
// Copyright 2017-2020 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/>.
//! Basic parachain that adds a number as part of its state.
#![no_std]
#![cfg_attr(not(feature = "std"), feature(core_intrinsics, lang_items, core_panic_info, alloc_error_handler))]
use codec::{Encode, Decode};
#[cfg(not(feature = "std"))]
mod wasm_validation;
#[cfg(not(feature = "std"))]
#[global_allocator]
static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc;
// Make the WASM binary available.
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
/// Head data for this parachain.
#[derive(Default, Clone, Hash, Eq, PartialEq, Encode, Decode)]
pub struct HeadData {
/// Block number
pub number: u64,
/// parent block keccak256
pub parent_hash: [u8; 32],
/// hash of post-execution state.
pub post_state: [u8; 32],
}
impl HeadData {
pub fn hash(&self) -> [u8; 32] {
tiny_keccak::keccak256(&self.encode())
}
}
/// Block data for this parachain.
#[derive(Default, Clone, Encode, Decode)]
pub struct BlockData {
/// State to begin from.
pub state: u64,
/// Amount to add (overflowing)
pub add: u64,
}
pub fn hash_state(state: u64) -> [u8; 32] {
tiny_keccak::keccak256(state.encode().as_slice())
}
/// Start state mismatched with parent header's state hash.
#[derive(Debug)]
pub struct StateMismatch;
/// Execute a block body on top of given parent head, producing new parent head
/// if valid.
pub fn execute(
parent_hash: [u8; 32],
parent_head: HeadData,
block_data: &BlockData,
) -> Result<HeadData, StateMismatch> {
debug_assert_eq!(parent_hash, parent_head.hash());
if hash_state(block_data.state) != parent_head.post_state {
return Err(StateMismatch);
}
let new_state = block_data.state.overflowing_add(block_data.add).0;
Ok(HeadData {
number: parent_head.number + 1,
parent_hash,
post_state: hash_state(new_state),
})
}
@@ -0,0 +1,60 @@
// Copyright 2019-2020 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/>.
//! WASM validation for adder parachain.
use crate::{HeadData, BlockData};
use core::{intrinsics, panic};
use parachain::primitives::{ValidationResult, HeadData as GenericHeadData};
use codec::{Encode, Decode};
#[panic_handler]
#[no_mangle]
pub fn panic(_info: &panic::PanicInfo) -> ! {
unsafe {
intrinsics::abort()
}
}
#[alloc_error_handler]
#[no_mangle]
pub fn oom(_: core::alloc::Layout) -> ! {
unsafe {
intrinsics::abort();
}
}
#[no_mangle]
pub extern fn validate_block(params: *const u8, len: usize) -> u64 {
let params = unsafe { parachain::load_params(params, len) };
let parent_head = HeadData::decode(&mut &params.parent_head.0[..])
.expect("invalid parent head format.");
let block_data = BlockData::decode(&mut &params.block_data.0[..])
.expect("invalid block data format.");
let parent_hash = tiny_keccak::keccak256(&params.parent_head.0[..]);
match crate::execute(parent_hash, parent_head, &block_data) {
Ok(new_head) => parachain::write_result(
&ValidationResult {
head_data: GenericHeadData(new_head.encode()),
new_validation_code: None,
}
),
Err(_) => panic!("execution failure"),
}
}
@@ -0,0 +1,23 @@
[package]
name = "test-parachain-code-upgrader"
version = "0.7.22"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Test parachain which can upgrade code"
edition = "2018"
build = "build.rs"
[dependencies]
parachain = { package = "polkadot-parachain", path = "../../", default-features = false, features = [ "wasm-api" ] }
codec = { package = "parity-scale-codec", version = "1.1.0", default-features = false, features = ["derive"] }
tiny-keccak = "1.5.0"
dlmalloc = { version = "0.1.3", features = [ "global" ] }
# We need to make sure the global allocator is disabled until we have support of full substrate externalities
runtime-io = { package = "sp-io", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, features = [ "disable_allocator" ] }
[build-dependencies]
wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.5" }
[features]
default = [ "std" ]
std = ["parachain/std"]
@@ -0,0 +1,25 @@
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Substrate 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.
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
use wasm_builder_runner::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_git("https://github.com/paritytech/substrate.git", "8c672e107789ed10973d937ba8cac245404377e2")
.export_heap_base()
.build()
}
@@ -0,0 +1,156 @@
// Copyright 2020 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/>.
//! Test parachain WASM which implements code ugprades.
#![no_std]
#![cfg_attr(not(feature = "std"), feature(core_intrinsics, lang_items, core_panic_info, alloc_error_handler))]
use codec::{Encode, Decode};
use parachain::primitives::{RelayChainBlockNumber, ValidationCode};
#[cfg(not(feature = "std"))]
mod wasm_validation;
#[cfg(not(feature = "std"))]
#[global_allocator]
static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc;
// Make the WASM binary available.
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
#[derive(Encode, Decode, Clone, Default)]
pub struct State {
/// The current code that is "active" in this chain.
pub code: ValidationCode,
/// Code upgrade that is pending.
pub pending_code: Option<(ValidationCode, RelayChainBlockNumber)>,
}
/// Head data for this parachain.
#[derive(Default, Clone, Hash, Eq, PartialEq, Encode, Decode)]
pub struct HeadData {
/// Block number
pub number: u64,
/// parent block keccak256
pub parent_hash: [u8; 32],
/// hash of post-execution state.
pub post_state: [u8; 32],
}
impl HeadData {
pub fn hash(&self) -> [u8; 32] {
tiny_keccak::keccak256(&self.encode())
}
}
/// Block data for this parachain.
#[derive(Default, Clone, Encode, Decode)]
pub struct BlockData {
/// State to begin from.
pub state: State,
/// Code to upgrade to.
pub new_validation_code: Option<ValidationCode>,
}
pub fn hash_state(state: &State) -> [u8; 32] {
tiny_keccak::keccak256(state.encode().as_slice())
}
#[derive(Debug)]
pub enum Error {
/// Start state mismatched with parent header's state hash.
StateMismatch,
/// New validation code too large.
NewCodeTooLarge,
/// Code upgrades not allowed at this time.
CodeUpgradeDisallowed,
}
pub struct ValidationResult {
/// The new head data.
pub head_data: HeadData,
/// The new validation code.
pub new_validation_code: Option<ValidationCode>,
}
pub struct RelayChainParams {
/// Whether a code upgrade is allowed and at what relay-chain block number
/// to process it after.
pub code_upgrade_allowed: Option<RelayChainBlockNumber>,
/// The maximum code size allowed for an upgrade.
pub max_code_size: u32,
/// The relay-chain block number.
pub relay_chain_block_number: RelayChainBlockNumber,
}
/// Execute a block body on top of given parent head, producing new parent head
/// if valid.
pub fn execute(
parent_hash: [u8; 32],
parent_head: HeadData,
block_data: BlockData,
relay_params: &RelayChainParams,
) -> Result<ValidationResult, Error> {
debug_assert_eq!(parent_hash, parent_head.hash());
if hash_state(&block_data.state) != parent_head.post_state {
return Err(Error::StateMismatch);
}
let mut new_state = block_data.state;
if let Some((pending_code, after)) = new_state.pending_code.take() {
if after <= relay_params.relay_chain_block_number {
// code applied.
new_state.code = pending_code;
} else {
// reinstate.
new_state.pending_code = Some((pending_code, after));
}
}
let new_validation_code = if let Some(ref new_validation_code) = block_data.new_validation_code {
if new_validation_code.0.len() as u32 > relay_params.max_code_size {
return Err(Error::NewCodeTooLarge);
}
// replace the code if allowed and we don't have an upgrade pending.
match (new_state.pending_code.is_some(), relay_params.code_upgrade_allowed) {
(_, None) => return Err(Error::CodeUpgradeDisallowed),
(false, Some(after)) => {
new_state.pending_code = Some((new_validation_code.clone(), after));
Some(new_validation_code.clone())
}
(true, Some(_)) => None,
}
} else {
None
};
let head_data = HeadData {
number: parent_head.number + 1,
parent_hash,
post_state: hash_state(&new_state),
};
Ok(ValidationResult {
head_data,
new_validation_code: new_validation_code,
})
}
@@ -0,0 +1,71 @@
// Copyright 2019-2020 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/>.
//! WASM validation for adder parachain.
use crate::{HeadData, BlockData, RelayChainParams};
use core::{intrinsics, panic};
use parachain::primitives::{ValidationResult, HeadData as GenericHeadData};
use codec::{Encode, Decode};
#[panic_handler]
#[no_mangle]
pub fn panic(_info: &panic::PanicInfo) -> ! {
unsafe {
intrinsics::abort()
}
}
#[alloc_error_handler]
#[no_mangle]
pub fn oom(_: core::alloc::Layout) -> ! {
unsafe {
intrinsics::abort();
}
}
#[no_mangle]
pub extern fn validate_block(params: *const u8, len: usize) -> u64 {
let params = unsafe { parachain::load_params(params, len) };
let parent_head = HeadData::decode(&mut &params.parent_head.0[..])
.expect("invalid parent head format.");
let block_data = BlockData::decode(&mut &params.block_data.0[..])
.expect("invalid block data format.");
let parent_hash = tiny_keccak::keccak256(&params.parent_head.0[..]);
let res = crate::execute(
parent_hash,
parent_head,
block_data,
&RelayChainParams {
code_upgrade_allowed: params.code_upgrade_allowed,
max_code_size: params.max_code_size,
relay_chain_block_number: params.relay_chain_height,
},
);
match res {
Ok(output) => parachain::write_result(
&ValidationResult {
head_data: GenericHeadData(output.head_data.encode()),
new_validation_code: output.new_validation_code,
}
),
Err(_) => panic!("execution failure"),
}
}
@@ -0,0 +1,16 @@
[package]
name = "test-parachain-halt"
version = "0.7.29-pre1"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Test parachain which executes forever"
edition = "2018"
build = "build.rs"
[dependencies]
[build-dependencies]
wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.5" }
[features]
default = [ "std" ]
std = []
@@ -0,0 +1,25 @@
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Substrate 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.
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
use wasm_builder_runner::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_git("https://github.com/paritytech/substrate.git", "8c672e107789ed10973d937ba8cac245404377e2")
.export_heap_base()
.build()
}
@@ -0,0 +1,49 @@
// Copyright 2019-2020 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/>.
//! Basic parachain that executes forever.
#![no_std]
#![cfg_attr(not(feature = "std"), feature(core_intrinsics, lang_items, core_panic_info, alloc_error_handler))]
// Make the WASM binary available.
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
#[cfg(not(feature = "std"))]
#[panic_handler]
#[no_mangle]
pub fn panic(_info: &core::panic::PanicInfo) -> ! {
unsafe {
core::intrinsics::abort()
}
}
#[cfg(not(feature = "std"))]
#[alloc_error_handler]
#[no_mangle]
pub fn oom(_: core::alloc::Layout) -> ! {
unsafe {
core::intrinsics::abort();
}
}
#[cfg(not(feature = "std"))]
#[no_mangle]
pub extern fn validate_block(params: *const u8, len: usize) -> usize {
loop {}
}
@@ -0,0 +1,17 @@
// Copyright 2020 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/>.
//! Stub - the fundamental logic of this crate is the integration tests.
@@ -16,9 +16,16 @@
//! Basic parachain that adds a number as part of its state.
use polkadot_parachain as parachain;
use crate::{DummyExt, parachain::ValidationParams};
use crate::{
DummyExt,
parachain,
parachain::primitives::{
RelayChainBlockNumber,
BlockData as GenericBlockData,
HeadData as GenericHeadData,
ValidationParams,
},
};
use codec::{Decode, Encode};
/// Head data for this parachain.
@@ -41,12 +48,6 @@ struct BlockData {
add: u64,
}
#[derive(Encode, Decode)]
struct AddMessage {
/// amount to add.
amount: u64,
}
const TEST_CODE: &[u8] = adder::WASM_BINARY;
fn hash_state(state: u64) -> [u8; 32] {
@@ -75,14 +76,18 @@ pub fn execute_good_on_parent() {
let ret = parachain::wasm_executor::validate_candidate(
TEST_CODE,
ValidationParams {
parent_head: parent_head.encode(),
block_data: block_data.encode(),
parent_head: GenericHeadData(parent_head.encode()),
block_data: GenericBlockData(block_data.encode()),
max_code_size: 1024,
max_head_data_size: 1024,
relay_chain_height: 1,
code_upgrade_allowed: None,
},
DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest(&pool),
).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data[..]).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data.0[..]).unwrap();
assert_eq!(new_head.number, 1);
assert_eq!(new_head.parent_hash, hash_head(&parent_head));
@@ -111,14 +116,18 @@ fn execute_good_chain_on_parent() {
let ret = parachain::wasm_executor::validate_candidate(
TEST_CODE,
ValidationParams {
parent_head: parent_head.encode(),
block_data: block_data.encode(),
parent_head: GenericHeadData(parent_head.encode()),
block_data: GenericBlockData(block_data.encode()),
max_code_size: 1024,
max_head_data_size: 1024,
relay_chain_height: number as RelayChainBlockNumber + 1,
code_upgrade_allowed: None,
},
DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest(&pool),
).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data[..]).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data.0[..]).unwrap();
assert_eq!(new_head.number, number + 1);
assert_eq!(new_head.parent_hash, hash_head(&parent_head));
@@ -148,8 +157,12 @@ fn execute_bad_on_parent() {
let _ret = parachain::wasm_executor::validate_candidate(
TEST_CODE,
ValidationParams {
parent_head: parent_head.encode(),
block_data: block_data.encode(),
parent_head: GenericHeadData(parent_head.encode()),
block_data: GenericBlockData(block_data.encode()),
max_code_size: 1024,
max_head_data_size: 1024,
relay_chain_height: 1,
code_upgrade_allowed: None,
},
DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest(&pool),
@@ -0,0 +1,224 @@
// Copyright 2017-2020 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/>.
//! Basic parachain that adds a number as part of its state.
use parachain;
use crate::{
DummyExt,
parachain::primitives::{
BlockData as GenericBlockData,
HeadData as GenericHeadData,
ValidationParams, ValidationCode,
},
};
use codec::{Decode, Encode};
use code_upgrader::{hash_state, HeadData, BlockData, State};
const TEST_CODE: &[u8] = code_upgrader::WASM_BINARY;
#[test]
pub fn execute_good_no_upgrade() {
let pool = parachain::wasm_executor::ValidationPool::new();
let parent_head = HeadData {
number: 0,
parent_hash: [0; 32],
post_state: hash_state(&State::default()),
};
let block_data = BlockData {
state: State::default(),
new_validation_code: None,
};
let ret = parachain::wasm_executor::validate_candidate(
TEST_CODE,
ValidationParams {
parent_head: GenericHeadData(parent_head.encode()),
block_data: GenericBlockData(block_data.encode()),
max_code_size: 1024,
max_head_data_size: 1024,
relay_chain_height: 1,
code_upgrade_allowed: None,
},
DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest(&pool),
).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data.0[..]).unwrap();
assert!(ret.new_validation_code.is_none());
assert_eq!(new_head.number, 1);
assert_eq!(new_head.parent_hash, parent_head.hash());
assert_eq!(new_head.post_state, hash_state(&State::default()));
}
#[test]
pub fn execute_good_with_upgrade() {
let pool = parachain::wasm_executor::ValidationPool::new();
let parent_head = HeadData {
number: 0,
parent_hash: [0; 32],
post_state: hash_state(&State::default()),
};
let block_data = BlockData {
state: State::default(),
new_validation_code: Some(ValidationCode(vec![1, 2, 3])),
};
let ret = parachain::wasm_executor::validate_candidate(
TEST_CODE,
ValidationParams {
parent_head: GenericHeadData(parent_head.encode()),
block_data: GenericBlockData(block_data.encode()),
max_code_size: 1024,
max_head_data_size: 1024,
relay_chain_height: 1,
code_upgrade_allowed: Some(20),
},
DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest(&pool),
).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data.0[..]).unwrap();
assert_eq!(ret.new_validation_code.unwrap(), ValidationCode(vec![1, 2, 3]));
assert_eq!(new_head.number, 1);
assert_eq!(new_head.parent_hash, parent_head.hash());
assert_eq!(
new_head.post_state,
hash_state(&State {
code: ValidationCode::default(),
pending_code: Some((ValidationCode(vec![1, 2, 3]), 20)),
}),
);
}
#[test]
#[should_panic]
pub fn code_upgrade_not_allowed() {
let pool = parachain::wasm_executor::ValidationPool::new();
let parent_head = HeadData {
number: 0,
parent_hash: [0; 32],
post_state: hash_state(&State::default()),
};
let block_data = BlockData {
state: State::default(),
new_validation_code: Some(ValidationCode(vec![1, 2, 3])),
};
parachain::wasm_executor::validate_candidate(
TEST_CODE,
ValidationParams {
parent_head: GenericHeadData(parent_head.encode()),
block_data: GenericBlockData(block_data.encode()),
max_code_size: 1024,
max_head_data_size: 1024,
relay_chain_height: 1,
code_upgrade_allowed: None,
},
DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest(&pool),
).unwrap();
}
#[test]
pub fn applies_code_upgrade_after_delay() {
let pool = parachain::wasm_executor::ValidationPool::new();
let (new_head, state) = {
let parent_head = HeadData {
number: 0,
parent_hash: [0; 32],
post_state: hash_state(&State::default()),
};
let block_data = BlockData {
state: State::default(),
new_validation_code: Some(ValidationCode(vec![1, 2, 3])),
};
let ret = parachain::wasm_executor::validate_candidate(
TEST_CODE,
ValidationParams {
parent_head: GenericHeadData(parent_head.encode()),
block_data: GenericBlockData(block_data.encode()),
max_code_size: 1024,
max_head_data_size: 1024,
relay_chain_height: 1,
code_upgrade_allowed: Some(2),
},
DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest(&pool),
).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data.0[..]).unwrap();
let parent_hash = parent_head.hash();
let state = State {
code: ValidationCode::default(),
pending_code: Some((ValidationCode(vec![1, 2, 3]), 2)),
};
assert_eq!(ret.new_validation_code.unwrap(), ValidationCode(vec![1, 2, 3]));
assert_eq!(new_head.number, 1);
assert_eq!(new_head.parent_hash, parent_hash);
assert_eq!(new_head.post_state, hash_state(&state));
(new_head, state)
};
{
let parent_head = new_head;
let block_data = BlockData {
state,
new_validation_code: None,
};
let ret = parachain::wasm_executor::validate_candidate(
TEST_CODE,
ValidationParams {
parent_head: GenericHeadData(parent_head.encode()),
block_data: GenericBlockData(block_data.encode()),
max_code_size: 1024,
max_head_data_size: 1024,
relay_chain_height: 2,
code_upgrade_allowed: None,
},
DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest(&pool),
).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data.0[..]).unwrap();
assert!(ret.new_validation_code.is_none());
assert_eq!(new_head.number, 2);
assert_eq!(new_head.parent_hash, parent_head.hash());
assert_eq!(
new_head.post_state,
hash_state(&State {
code: ValidationCode(vec![1, 2, 3]),
pending_code: None,
}),
);
}
}
@@ -15,11 +15,11 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
mod adder;
mod code_upgrader;
mod wasm_executor;
use polkadot_parachain as parachain;
use crate::parachain::{
UpwardMessage, wasm_executor::{Externalities, run_worker},
use parachain::{
self, primitives::UpwardMessage, wasm_executor::{Externalities, run_worker},
};
struct DummyExt;
@@ -16,9 +16,12 @@
//! Basic parachain that adds a number as part of its state.
use polkadot_parachain as parachain;
use parachain;
use crate::{adder, DummyExt};
use crate::parachain::{ValidationParams, wasm_executor::EXECUTION_TIMEOUT_SEC};
use crate::parachain::{
primitives::{BlockData, ValidationParams},
wasm_executor::EXECUTION_TIMEOUT_SEC,
};
// Code that exposes `validate_block` and loops infinitely
const INFINITE_LOOP_CODE: &[u8] = halt::WASM_BINARY;
@@ -30,8 +33,12 @@ fn terminates_on_timeout() {
let result = parachain::wasm_executor::validate_candidate(
INFINITE_LOOP_CODE,
ValidationParams {
block_data: BlockData(Vec::new()),
parent_head: Default::default(),
block_data: Vec::new(),
max_code_size: 1024,
max_head_data_size: 1024,
relay_chain_height: 1,
code_upgrade_allowed: None,
},
DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest(&pool),
@@ -56,8 +63,12 @@ fn parallel_execution() {
parachain::wasm_executor::validate_candidate(
INFINITE_LOOP_CODE,
ValidationParams {
block_data: BlockData(Vec::new()),
parent_head: Default::default(),
block_data: Vec::new(),
max_code_size: 1024,
max_head_data_size: 1024,
relay_chain_height: 1,
code_upgrade_allowed: None,
},
DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest(&pool2),
@@ -65,8 +76,12 @@ fn parallel_execution() {
let _ = parachain::wasm_executor::validate_candidate(
INFINITE_LOOP_CODE,
ValidationParams {
block_data: BlockData(Vec::new()),
parent_head: Default::default(),
block_data: Vec::new(),
max_code_size: 1024,
max_head_data_size: 1024,
relay_chain_height: 1,
code_upgrade_allowed: None,
},
DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest(&pool),