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
+59 -39
View File
@@ -10,34 +10,6 @@ dependencies = [
"regex",
]
[[package]]
name = "adder"
version = "0.7.29-pre1"
dependencies = [
"dlmalloc",
"parity-scale-codec",
"polkadot-parachain",
"sp-io",
"substrate-wasm-builder-runner 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"tiny-keccak 1.5.0",
]
[[package]]
name = "adder-collator"
version = "0.1.0"
dependencies = [
"adder",
"futures 0.3.4",
"parity-scale-codec",
"parking_lot 0.10.0",
"polkadot-collator",
"polkadot-parachain",
"polkadot-primitives",
"sc-client",
"sc-client-api",
"sp-core",
]
[[package]]
name = "adler32"
version = "1.0.4"
@@ -1778,13 +1750,6 @@ dependencies = [
"tokio-util",
]
[[package]]
name = "halt"
version = "0.7.29-pre1"
dependencies = [
"substrate-wasm-builder-runner 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "hash-db"
version = "0.15.2"
@@ -4294,9 +4259,7 @@ dependencies = [
name = "polkadot-parachain"
version = "0.7.29-pre1"
dependencies = [
"adder",
"derive_more 0.99.3",
"halt",
"log 0.4.8",
"parity-scale-codec",
"parking_lot 0.10.0",
@@ -4309,7 +4272,6 @@ dependencies = [
"sp-runtime-interface",
"sp-std",
"sp-wasm-interface",
"tiny-keccak 1.5.0",
]
[[package]]
@@ -4317,7 +4279,6 @@ name = "polkadot-primitives"
version = "0.7.29-pre1"
dependencies = [
"bitvec",
"pallet-babe",
"parity-scale-codec",
"polkadot-parachain",
"pretty_assertions",
@@ -7524,6 +7485,65 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "test-parachain-adder"
version = "0.7.29-pre1"
dependencies = [
"dlmalloc",
"parity-scale-codec",
"polkadot-parachain",
"sp-io",
"substrate-wasm-builder-runner 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"tiny-keccak 1.5.0",
]
[[package]]
name = "test-parachain-adder-collator"
version = "0.1.0"
dependencies = [
"futures 0.3.4",
"parity-scale-codec",
"parking_lot 0.10.0",
"polkadot-collator",
"polkadot-parachain",
"polkadot-primitives",
"sc-client",
"sc-client-api",
"sp-core",
"test-parachain-adder",
]
[[package]]
name = "test-parachain-code-upgrader"
version = "0.7.22"
dependencies = [
"dlmalloc",
"parity-scale-codec",
"polkadot-parachain",
"sp-io",
"substrate-wasm-builder-runner 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"tiny-keccak 1.5.0",
]
[[package]]
name = "test-parachain-halt"
version = "0.7.29-pre1"
dependencies = [
"substrate-wasm-builder-runner 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "test-parachains"
version = "0.7.22"
dependencies = [
"parity-scale-codec",
"polkadot-parachain",
"test-parachain-adder",
"test-parachain-code-upgrader",
"test-parachain-halt",
"tiny-keccak 1.5.0",
]
[[package]]
name = "textwrap"
version = "0.11.0"
+5 -3
View File
@@ -41,13 +41,15 @@ members = [
"service",
"validation",
"test-parachains/adder",
"test-parachains/adder/collator",
"parachain/test-parachains",
"parachain/test-parachains/adder",
"parachain/test-parachains/adder/collator",
"parachain/test-parachains/code-upgrader",
]
exclude = [
"runtime/polkadot/wasm",
"runtime/kusama/wasm",
"test-parachains/adder/wasm",
"parachain/test-parachains/adder/wasm",
]
[badges]
+8 -2
View File
@@ -59,7 +59,7 @@ use polkadot_primitives::{
BlockId, Hash, Block,
parachain::{
self, BlockData, DutyRoster, HeadData, Id as ParaId,
PoVBlock, ValidatorId, CollatorPair, LocalValidationData
PoVBlock, ValidatorId, CollatorPair, LocalValidationData, GlobalValidationSchedule,
}
};
use polkadot_cli::{
@@ -154,7 +154,8 @@ pub trait ParachainContext: Clone {
fn produce_candidate(
&mut self,
relay_parent: Hash,
status: LocalValidationData,
global_validation: GlobalValidationSchedule,
local_validation: LocalValidationData,
) -> Self::ProduceCandidate;
}
@@ -162,6 +163,7 @@ pub trait ParachainContext: Clone {
pub async fn collate<P>(
relay_parent: Hash,
local_id: ParaId,
global_validation: GlobalValidationSchedule,
local_validation_data: LocalValidationData,
mut para_context: P,
key: Arc<CollatorPair>,
@@ -173,6 +175,7 @@ pub async fn collate<P>(
{
let (block_data, head_data) = para_context.produce_candidate(
relay_parent,
global_validation,
local_validation_data,
).map_err(Error::Collator).await?;
@@ -281,6 +284,7 @@ fn build_collator_service<S, P, Extrinsic>(
let work = future::lazy(move |_| {
let api = client.runtime_api();
let global_validation = try_fr!(api.global_validation_schedule(&id));
let local_validation = match try_fr!(api.local_validation_data(&id, para_id)) {
Some(local_validation) => local_validation,
None => return future::Either::Left(future::ok(())),
@@ -297,6 +301,7 @@ fn build_collator_service<S, P, Extrinsic>(
let collation_work = collate(
relay_parent,
para_id,
global_validation,
local_validation,
parachain_context,
key,
@@ -427,6 +432,7 @@ mod tests {
fn produce_candidate(
&mut self,
_relay_parent: Hash,
_global: GlobalValidationSchedule,
_local_validation: LocalValidationData,
) -> Self::ProduceCandidate {
// send messages right back.
+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,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",
]
@@ -1,5 +1,5 @@
[package]
name = "adder"
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"
@@ -7,7 +7,7 @@ edition = "2018"
build = "build.rs"
[dependencies]
parachain = { package = "polkadot-parachain", path = "../../parachain/", default-features = false, features = [ "wasm-api" ] }
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" ] }
@@ -20,4 +20,4 @@ wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.
[features]
default = [ "std" ]
std = []
std = ["parachain/std"]
@@ -1,14 +1,14 @@
[package]
name = "adder-collator"
name = "test-parachain-adder-collator"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
adder = { path = ".." }
parachain = { package = "polkadot-parachain", path = "../../../parachain" }
collator = { package = "polkadot-collator", path = "../../../collator" }
primitives = { package = "polkadot-primitives", path = "../../../primitives" }
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" }
@@ -24,7 +24,7 @@ use sp_core::Pair;
use codec::{Encode, Decode};
use primitives::{
Hash,
parachain::{HeadData, BlockData, Id as ParaId, LocalValidationData},
parachain::{HeadData, BlockData, Id as ParaId, LocalValidationData, GlobalValidationSchedule},
};
use collator::{
InvalidHead, ParachainContext, Network, BuildParachainContext, load_spec, Configuration,
@@ -60,6 +60,7 @@ impl ParachainContext for AdderContext {
fn produce_candidate(
&mut self,
_relay_parent: Hash,
_global_validation: GlobalValidationSchedule,
local_validation: LocalValidationData,
) -> Self::ProduceCandidate
{
@@ -83,7 +84,7 @@ impl ParachainContext for AdderContext {
add: adder_head.number % 100,
};
let next_head = ::adder::execute(adder_head.hash(), adder_head, &next_body)
let next_head = adder::execute(adder_head.hash(), adder_head, &next_body)
.expect("good execution params; qed");
let encoded_head = HeadData(next_head.encode());
@@ -63,27 +63,10 @@ pub fn hash_state(state: u64) -> [u8; 32] {
tiny_keccak::keccak256(state.encode().as_slice())
}
#[derive(Default, Encode, Decode)]
pub struct AddMessage {
/// The amount to add based on this message.
pub amount: u64,
}
/// Start state mismatched with parent header's state hash.
#[derive(Debug)]
pub struct StateMismatch;
/// Process all incoming messages, yielding the amount of addition from messages.
///
/// Ignores unknown message kinds.
pub fn process_messages<I, T>(iterable: I) -> u64
where I: IntoIterator<Item=T>, T: AsRef<[u8]>
{
iterable.into_iter()
.filter_map(|data| AddMessage::decode(&mut data.as_ref()).ok())
.fold(0u64, |a, c| a.overflowing_add(c.amount).0)
}
/// Execute a block body on top of given parent head, producing new parent head
/// if valid.
pub fn execute(
@@ -18,7 +18,7 @@
use crate::{HeadData, BlockData};
use core::{intrinsics, panic};
use parachain::ValidationResult;
use parachain::primitives::{ValidationResult, HeadData as GenericHeadData};
use codec::{Encode, Decode};
#[panic_handler]
@@ -40,17 +40,20 @@ pub fn oom(_: core::alloc::Layout) -> ! {
#[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[..])
let parent_head = HeadData::decode(&mut &params.parent_head.0[..])
.expect("invalid parent head format.");
let block_data = BlockData::decode(&mut &params.block_data[..])
let block_data = BlockData::decode(&mut &params.block_data.0[..])
.expect("invalid block data format.");
let parent_hash = tiny_keccak::keccak256(&params.parent_head[..]);
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: new_head.encode() }
&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,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"),
}
}
@@ -1,5 +1,5 @@
[package]
name = "halt"
name = "test-parachain-halt"
version = "0.7.29-pre1"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Test parachain which executes forever"
@@ -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 @@
// 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),
-2
View File
@@ -18,7 +18,6 @@ runtime_primitives = { package = "sp-runtime", git = "https://github.com/parityt
polkadot-parachain = { path = "../parachain", default-features = false }
trie = { package = "sp-trie", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
bitvec = { version = "0.17.4", default-features = false, features = ["alloc"] }
babe = { package = "pallet-babe", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[dev-dependencies]
sp-serializer = { git = "https://github.com/paritytech/substrate", branch = "master" }
@@ -39,5 +38,4 @@ std = [
"serde",
"polkadot-parachain/std",
"bitvec/std",
"babe/std"
]
+1 -2
View File
@@ -28,8 +28,7 @@ pub mod parachain;
pub use parity_scale_codec::Compact;
/// An index to a block.
/// 32-bits will allow for 136 years of blocks assuming 1 block per second.
pub type BlockNumber = u32;
pub type BlockNumber = polkadot_parachain::primitives::RelayChainBlockNumber;
/// An instant or duration in time.
pub type Moment = u64;
+24 -31
View File
@@ -14,13 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Polkadot parachain types.
//! Primitives which are necessary for parachain execution from a relay-chain
//! perspective.
use sp_std::prelude::*;
use sp_std::cmp::Ordering;
use parity_scale_codec::{Encode, Decode};
use bitvec::vec::BitVec;
use super::{Hash, Balance};
use super::{Hash, Balance, BlockNumber};
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
@@ -32,8 +33,9 @@ use runtime_primitives::traits::Block as BlockT;
use inherents::InherentIdentifier;
use application_crypto::KeyTypeId;
pub use polkadot_parachain::{
Id, ParachainDispatchOrigin, LOWEST_USER_ID, UpwardMessage,
pub use polkadot_parachain::primitives::{
Id, ParachainDispatchOrigin, LOWEST_USER_ID, UpwardMessage, HeadData, BlockData,
ValidationCode,
};
/// The key type ID for a collator key.
@@ -174,6 +176,8 @@ pub struct GlobalValidationSchedule {
pub max_code_size: u32,
/// The maximum head-data size permitted, in bytes.
pub max_head_data_size: u32,
/// The relay-chain block number this is in the context of.
pub block_number: BlockNumber,
}
/// Extra data that is needed along with the other fields in a `CandidateReceipt`
@@ -185,6 +189,18 @@ pub struct LocalValidationData {
pub parent_head: HeadData,
/// The balance of the parachain at the moment of validation.
pub balance: Balance,
/// Whether the parachain is allowed to upgrade its validation code.
///
/// This is `Some` if so, and contains the number of the minimum relay-chain
/// height at which the upgrade will be applied, if an upgrade is signaled
/// now.
///
/// A parachain should enact its side of the upgrade at the end of the first
/// parablock executing in the context of a relay-chain block with at least this
/// height. This may be equal to the current perceived relay-chain block height, in
/// which case the code upgrade should be applied at the end of the signaling
/// block.
pub code_upgrade_allowed: Option<BlockNumber>,
}
/// Commitments made in a `CandidateReceipt`. Many of these are outputs of validation.
@@ -197,6 +213,8 @@ pub struct CandidateCommitments {
pub upward_messages: Vec<UpwardMessage>,
/// The root of a block's erasure encoding Merkle tree.
pub erasure_root: Hash,
/// New validation code.
pub new_validation_code: Option<Vec<u8>>,
}
/// Get a collator signature payload on a relay-parent, block-data combo.
@@ -532,13 +550,6 @@ pub struct AvailableData {
// In the future, outgoing messages as well.
}
/// 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>);
/// A chunk of erasure-encoded block data.
#[derive(PartialEq, Eq, Clone, Encode, Decode, Default)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
@@ -551,29 +562,11 @@ pub struct ErasureChunk {
pub proof: Vec<Vec<u8>>,
}
impl BlockData {
/// Compute hash of block data.
#[cfg(feature = "std")]
pub fn hash(&self) -> Hash {
use runtime_primitives::traits::{BlakeTwo256, Hash};
BlakeTwo256::hash(&self.0[..])
}
}
/// Parachain header raw bytes wrapper type.
#[derive(PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct Header(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
/// 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(PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct ValidationCode(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
/// Activity bit field.
#[derive(PartialEq, Eq, Clone, Default, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
@@ -648,13 +641,13 @@ impl AttestedCandidate {
pub struct FeeSchedule {
/// The base fee charged for all messages.
pub base: Balance,
/// The per-byte fee charged on top of that.
/// The per-byte fee for messages charged on top of that.
pub per_byte: Balance,
}
impl FeeSchedule {
/// Compute the fee for a message of given size.
pub fn compute_fee(&self, n_bytes: usize) -> Balance {
pub fn compute_message_fee(&self, n_bytes: usize) -> Balance {
use sp_std::mem;
debug_assert!(mem::size_of::<Balance>() >= mem::size_of::<usize>());
+5 -1
View File
@@ -582,7 +582,7 @@ mod tests {
};
use frame_support::traits::Contains;
use sp_core::H256;
use primitives::parachain::{Info as ParaInfo, Id as ParaId};
use primitives::parachain::{Info as ParaInfo, Id as ParaId, Scheduling};
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are requried.
use sp_runtime::{
@@ -698,6 +698,10 @@ mod tests {
code_size <= MAX_CODE_SIZE
}
fn para_info(_id: ParaId) -> Option<ParaInfo> {
Some(ParaInfo { scheduling: Scheduling::Always })
}
fn register_para(
id: ParaId,
_info: ParaInfo,
File diff suppressed because it is too large Load Diff
+25 -15
View File
@@ -52,6 +52,9 @@ pub trait Registrar<AccountId> {
/// Checks whether the given validation code falls within the limit.
fn code_size_allowed(code_size: u32) -> bool;
/// Fetches metadata for a para by ID, if any.
fn para_info(id: ParaId) -> Option<ParaInfo>;
/// Register a parachain with given `code` and `initial_head_data`. `id` must not yet be registered or it will
/// result in a error.
///
@@ -83,6 +86,10 @@ impl<T: Trait> Registrar<T::AccountId> for Module<T> {
code_size <= <T as parachains::Trait>::MaxCodeSize::get()
}
fn para_info(id: ParaId) -> Option<ParaInfo> {
Self::paras(&id)
}
fn register_para(
id: ParaId,
info: ParaInfo,
@@ -653,15 +660,15 @@ mod tests {
traits::{
BlakeTwo256, IdentityLookup, Dispatchable,
AccountIdConversion,
}, testing::{UintAuthorityId, Header, TestXt}, KeyTypeId, Perbill, curve::PiecewiseLinear,
}, testing::{UintAuthorityId, TestXt}, KeyTypeId, Perbill, curve::PiecewiseLinear,
};
use primitives::{
parachain::{
ValidatorId, Info as ParaInfo, Scheduling, LOWEST_USER_ID, AttestedCandidate,
CandidateReceipt, HeadData, ValidityAttestation, Statement, Chain,
CollatorPair, CandidateCommitments, GlobalValidationSchedule, LocalValidationData,
CollatorPair, CandidateCommitments,
},
Balance, BlockNumber,
Balance, BlockNumber, Header,
};
use frame_support::{
traits::{KeyOwnerProofSystem, OnInitialize, OnFinalize},
@@ -710,7 +717,7 @@ mod tests {
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = u64;
type BlockNumber = BlockNumber;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
@@ -741,8 +748,8 @@ mod tests {
}
parameter_types!{
pub const LeasePeriod: u64 = 10;
pub const EndingPeriod: u64 = 3;
pub const LeasePeriod: BlockNumber = 10;
pub const EndingPeriod: BlockNumber = 3;
}
impl slots::Trait for Test {
@@ -791,6 +798,10 @@ mod tests {
parameter_types! {
pub const MaxHeadDataSize: u32 = 100;
pub const MaxCodeSize: u32 = 100;
pub const ValidationUpgradeFrequency: BlockNumber = 10;
pub const ValidationUpgradeDelay: BlockNumber = 2;
pub const SlashPeriod: BlockNumber = 50;
pub const ElectionLookahead: BlockNumber = 0;
}
@@ -830,11 +841,15 @@ mod tests {
type Origin = Origin;
type Call = Call;
type ParachainCurrency = balances::Module<Test>;
type BlockNumberConversion = sp_runtime::traits::Identity;
type ActiveParachains = Registrar;
type Registrar = Registrar;
type Randomness = RandomnessCollectiveFlip;
type MaxCodeSize = MaxCodeSize;
type MaxHeadDataSize = MaxHeadDataSize;
type ValidationUpgradeFrequency = ValidationUpgradeFrequency;
type ValidationUpgradeDelay = ValidationUpgradeDelay;
type SlashPeriod = SlashPeriod;
type Proof = session::historical::Proof;
type KeyOwnerProofSystem = session::historical::Module<Test>;
type IdentificationTuple = <Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(KeyTypeId, Vec<u8>)>>::IdentificationTuple;
@@ -929,7 +944,7 @@ mod tests {
Slots::on_initialize(System::block_number());
}
fn run_to_block(n: u64) {
fn run_to_block(n: BlockNumber) {
println!("Running until block {}", n);
while System::block_number() < n {
if System::block_number() > 1 {
@@ -972,18 +987,13 @@ mod tests {
collator: collator.public(),
signature: pov_block_hash.using_encoded(|d| collator.sign(d)),
pov_block_hash,
global_validation: GlobalValidationSchedule {
max_code_size: <Test as parachains::Trait>::MaxCodeSize::get(),
max_head_data_size: <Test as parachains::Trait>::MaxHeadDataSize::get(),
},
local_validation: LocalValidationData {
balance: Balances::free_balance(&id.into_account()),
parent_head: HeadData(Parachains::parachain_head(&id).unwrap()),
},
global_validation: Parachains::global_validation_schedule(),
local_validation: Parachains::current_local_validation_data(&id).unwrap(),
commitments: CandidateCommitments {
fees: 0,
upward_messages: vec![],
erasure_root: [1; 32].into(),
new_validation_code: None,
},
};
let (candidate, _) = candidate.abridge();
+15 -10
View File
@@ -878,7 +878,7 @@ mod tests {
use sp_core::H256;
use sp_runtime::{
Perbill, testing::Header,
Perbill,
traits::{BlakeTwo256, Hash, IdentityLookup},
};
use frame_support::{
@@ -886,7 +886,8 @@ mod tests {
traits::{OnInitialize, OnFinalize}
};
use balances;
use primitives::parachain::{Id as ParaId, Info as ParaInfo};
use primitives::{BlockNumber, Header};
use primitives::parachain::{Id as ParaId, Info as ParaInfo, Scheduling};
impl_outer_origin! {
pub enum Origin for Test {}
@@ -907,7 +908,7 @@ mod tests {
type Origin = Origin;
type Call = ();
type Index = u64;
type BlockNumber = u64;
type BlockNumber = BlockNumber;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
@@ -963,6 +964,10 @@ mod tests {
code_size <= MAX_CODE_SIZE
}
fn para_info(_id: ParaId) -> Option<ParaInfo> {
Some(ParaInfo { scheduling: Scheduling::Always })
}
fn register_para(
id: ParaId,
_info: ParaInfo,
@@ -997,8 +1002,8 @@ mod tests {
}
parameter_types!{
pub const LeasePeriod: u64 = 10;
pub const EndingPeriod: u64 = 3;
pub const LeasePeriod: BlockNumber = 10;
pub const EndingPeriod: BlockNumber = 3;
}
impl Trait for Test {
@@ -1025,7 +1030,7 @@ mod tests {
t.into()
}
fn run_to_block(n: u64) {
fn run_to_block(n: BlockNumber) {
while System::block_number() < n {
Slots::on_finalize(System::block_number());
Balances::on_finalize(System::block_number());
@@ -1453,8 +1458,8 @@ mod tests {
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
for i in 1..6 {
run_to_block(i);
for i in 1..6u64 {
run_to_block(i as _);
assert_ok!(Slots::bid(Origin::signed(i), 0, 1, 1, 4, i));
for j in 1..6 {
assert_eq!(Balances::reserved_balance(j), if j == i { j } else { 0 });
@@ -1481,8 +1486,8 @@ mod tests {
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
for i in 1..6 {
run_to_block(i + 3);
for i in 1..6u64 {
run_to_block((i + 3) as _);
assert_ok!(Slots::bid(Origin::signed(i), 0, 1, 1, 4, i));
for j in 1..6 {
assert_eq!(Balances::reserved_balance(j), if j == i { j } else { 0 });
+10 -1
View File
@@ -499,17 +499,26 @@ impl attestations::Trait for Runtime {
parameter_types! {
pub const MaxCodeSize: u32 = 10 * 1024 * 1024; // 10 MB
pub const MaxHeadDataSize: u32 = 20 * 1024; // 20 KB
pub const ValidationUpgradeFrequency: BlockNumber = 2 * DAYS;
pub const ValidationUpgradeDelay: BlockNumber = 8 * HOURS;
pub const SlashPeriod: BlockNumber = 7 * DAYS;
}
impl parachains::Trait for Runtime {
type Origin = Origin;
type Call = Call;
type ParachainCurrency = Balances;
type BlockNumberConversion = sp_runtime::traits::Identity;
type Randomness = RandomnessCollectiveFlip;
type ActiveParachains = Registrar;
type Registrar = Registrar;
type MaxCodeSize = MaxCodeSize;
type MaxHeadDataSize = MaxHeadDataSize;
type ValidationUpgradeFrequency = ValidationUpgradeFrequency;
type ValidationUpgradeDelay = ValidationUpgradeDelay;
type SlashPeriod = SlashPeriod;
type Proof = session::historical::Proof;
type KeyOwnerProofSystem = session::historical::Module<Self>;
type IdentificationTuple = <Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(KeyTypeId, Vec<u8>)>>::IdentificationTuple;
@@ -821,7 +830,7 @@ sp_api::impl_runtime_apis! {
Parachains::global_validation_schedule()
}
fn local_validation_data(id: parachain::Id) -> Option<parachain::LocalValidationData> {
Parachains::local_validation_data(&id)
Parachains::current_local_validation_data(&id)
}
fn parachain_code(id: parachain::Id) -> Option<Vec<u8>> {
Parachains::parachain_code(&id)
+11 -1
View File
@@ -507,17 +507,27 @@ impl attestations::Trait for Runtime {
parameter_types! {
pub const MaxCodeSize: u32 = 10 * 1024 * 1024; // 10 MB
pub const MaxHeadDataSize: u32 = 20 * 1024; // 20 KB
pub const ValidationUpgradeFrequency: BlockNumber = 7 * DAYS;
pub const ValidationUpgradeDelay: BlockNumber = 1 * DAYS;
pub const SlashPeriod: BlockNumber = 28 * DAYS;
}
impl parachains::Trait for Runtime {
type Origin = Origin;
type Call = Call;
type ParachainCurrency = Balances;
type BlockNumberConversion = sp_runtime::traits::Identity;
type Randomness = RandomnessCollectiveFlip;
type ActiveParachains = Registrar;
type Registrar = Registrar;
type MaxCodeSize = MaxCodeSize;
type MaxHeadDataSize = MaxHeadDataSize;
type ValidationUpgradeFrequency = ValidationUpgradeFrequency;
type ValidationUpgradeDelay = ValidationUpgradeDelay;
type SlashPeriod = SlashPeriod;
type Proof = session::historical::Proof;
type KeyOwnerProofSystem = session::historical::Module<Self>;
type IdentificationTuple = <Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(KeyTypeId, Vec<u8>)>>::IdentificationTuple;
@@ -741,7 +751,7 @@ sp_api::impl_runtime_apis! {
Parachains::global_validation_schedule()
}
fn local_validation_data(id: parachain::Id) -> Option<parachain::LocalValidationData> {
Parachains::local_validation_data(&id)
Parachains::current_local_validation_data(&id)
}
fn parachain_code(id: parachain::Id) -> Option<Vec<u8>> {
Parachains::parachain_code(&id)
+11 -1
View File
@@ -310,17 +310,27 @@ impl attestations::Trait for Runtime {
parameter_types! {
pub const MaxCodeSize: u32 = 10 * 1024 * 1024; // 10 MB
pub const MaxHeadDataSize: u32 = 20 * 1024; // 20 KB
pub const ValidationUpgradeFrequency: BlockNumber = 2;
pub const ValidationUpgradeDelay: BlockNumber = 1;
pub const SlashPeriod: BlockNumber = 1 * MINUTES;
}
impl parachains::Trait for Runtime {
type Origin = Origin;
type Call = Call;
type ParachainCurrency = Balances;
type BlockNumberConversion = sp_runtime::traits::Identity;
type Randomness = RandomnessCollectiveFlip;
type ActiveParachains = Registrar;
type Registrar = Registrar;
type MaxCodeSize = MaxCodeSize;
type MaxHeadDataSize = MaxHeadDataSize;
type ValidationUpgradeFrequency = ValidationUpgradeFrequency;
type ValidationUpgradeDelay = ValidationUpgradeDelay;
type SlashPeriod = SlashPeriod;
type Proof = session::historical::Proof;
type KeyOwnerProofSystem = session::historical::Module<Self>;
type IdentificationTuple = <
@@ -528,7 +538,7 @@ sp_api::impl_runtime_apis! {
Parachains::global_validation_schedule()
}
fn local_validation_data(id: parachain::Id) -> Option<parachain::LocalValidationData> {
Parachains::local_validation_data(&id)
Parachains::current_local_validation_data(&id)
}
fn parachain_code(id: parachain::Id) -> Option<Vec<u8>> {
Parachains::parachain_code(&id)
+18 -19
View File
@@ -23,13 +23,13 @@ use codec::Encode;
use polkadot_erasure_coding as erasure;
use polkadot_primitives::parachain::{
CollationInfo, PoVBlock, LocalValidationData, GlobalValidationSchedule, OmittedValidationData,
AvailableData, FeeSchedule, CandidateCommitments, ErasureChunk, HeadData, ParachainHost,
Id as ParaId, AbridgedCandidateReceipt,
AvailableData, FeeSchedule, CandidateCommitments, ErasureChunk, ParachainHost,
Id as ParaId, AbridgedCandidateReceipt
};
use polkadot_primitives::{Block, BlockId, Balance, Hash};
use parachain::{
wasm_executor::{self, ExecutionMode},
UpwardMessage, ValidationParams,
primitives::{UpwardMessage, ValidationParams},
};
use runtime_primitives::traits::{BlakeTwo256, Hash as HashT};
use sp_api::ProvideRuntimeApi;
@@ -95,7 +95,7 @@ impl ExternalitiesInner {
}
fn apply_message_fee(&mut self, message_len: usize) -> Result<(), String> {
let fee = self.fee_schedule.compute_fee(message_len);
let fee = self.fee_schedule.compute_message_fee(message_len);
let new_fees_charged = self.fees_charged.saturating_add(fee);
if new_fees_charged > self.free_balance {
Err("could not cover fee.".into())
@@ -160,8 +160,7 @@ impl FullOutput {
pub struct ValidatedCandidate<'a> {
pov_block: &'a PoVBlock,
global_validation: &'a GlobalValidationSchedule,
parent_head: &'a HeadData,
balance: Balance,
local_validation: &'a LocalValidationData,
upward_messages: Vec<UpwardMessage>,
fees: Balance,
}
@@ -173,18 +172,14 @@ impl<'a> ValidatedCandidate<'a> {
let ValidatedCandidate {
pov_block,
global_validation,
parent_head,
balance,
local_validation,
upward_messages,
fees,
} = self;
let omitted_validation = OmittedValidationData {
global_validation: global_validation.clone(),
local_validation: LocalValidationData {
parent_head: parent_head.clone(),
balance,
},
local_validation: local_validation.clone(),
};
let available_data = AvailableData {
@@ -216,6 +211,7 @@ impl<'a> ValidatedCandidate<'a> {
upward_messages,
fees,
erasure_root,
new_validation_code: None,
};
Ok(FullOutput {
@@ -244,8 +240,12 @@ pub fn validate<'a>(
}
let params = ValidationParams {
parent_head: local_validation.parent_head.0.clone(),
block_data: pov_block.block_data.0.clone(),
parent_head: local_validation.parent_head.clone(),
block_data: pov_block.block_data.clone(),
max_code_size: global_validation.max_code_size,
max_head_data_size: global_validation.max_head_data_size,
relay_chain_height: global_validation.block_number,
code_upgrade_allowed: local_validation.code_upgrade_allowed,
};
// TODO: remove when ext does not do this.
@@ -266,7 +266,7 @@ pub fn validate<'a>(
execution_mode,
) {
Ok(result) => {
if result.head_data == collation.head_data.0 {
if result.head_data == collation.head_data {
let (upward_messages, fees) = Arc::try_unwrap(ext.0)
.map_err(|_| "<non-unique>")
.expect("Wasm executor drops passed externalities on completion; \
@@ -277,8 +277,7 @@ pub fn validate<'a>(
Ok(ValidatedCandidate {
pov_block,
global_validation,
parent_head: &local_validation.parent_head,
balance: local_validation.balance,
local_validation,
upward_messages,
fees,
})
@@ -352,13 +351,13 @@ pub fn full_output_validation_with_api<P>(
mod tests {
use super::*;
use parachain::wasm_executor::Externalities as ExternalitiesTrait;
use parachain::ParachainDispatchOrigin;
use parachain::primitives::ParachainDispatchOrigin;
#[test]
fn ext_checks_fees_and_updates_correctly() {
let mut ext = ExternalitiesInner {
upward: vec![
UpwardMessage{ data: vec![42], origin: ParachainDispatchOrigin::Parachain },
UpwardMessage { data: vec![42], origin: ParachainDispatchOrigin::Parachain },
],
fees_charged: 0,
free_balance: 1_000_000,