Bring substrate-demo up to date (#658)

* Updating substrate-demo

* Consenus fixes

* Reverted toolchain change

* Adjusted timeout formula

* Simplfied proposal creation

* Fixed tests

* Fixed a few small issues

* 2017->2018

* Style

* More style

* Renamed demo executable to substrate

* Style

* Fixed compilation after merge

* Style
This commit is contained in:
Arkadiy Paronyan
2018-09-10 17:54:32 +02:00
committed by Gav Wood
parent bcc26dd30a
commit fea750511e
41 changed files with 2940 additions and 312 deletions
@@ -0,0 +1,94 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate Demo.
// Substrate Demo 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 Demo 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 Demo. If not, see <http://www.gnu.org/licenses/>.
//! Typesafe block interaction.
use super::{Call, Block, TIMESTAMP_SET_POSITION, NOTE_OFFLINE_POSITION};
use timestamp::Call as TimestampCall;
use consensus::Call as ConsensusCall;
/// Provides a type-safe wrapper around a structurally valid block.
pub struct CheckedBlock {
inner: Block,
file_line: Option<(&'static str, u32)>,
}
impl CheckedBlock {
/// Create a new checked block. Fails if the block is not structurally valid.
pub fn new(block: Block) -> Result<Self, Block> {
let has_timestamp = block.extrinsics.get(TIMESTAMP_SET_POSITION as usize).map_or(false, |xt| {
!xt.is_signed() && match xt.function {
Call::Timestamp(TimestampCall::set(_)) => true,
_ => false,
}
});
if !has_timestamp { return Err(block) }
Ok(CheckedBlock {
inner: block,
file_line: None,
})
}
// Creates a new checked block, asserting that it is valid.
#[doc(hidden)]
pub fn new_unchecked(block: Block, file: &'static str, line: u32) -> Self {
CheckedBlock {
inner: block,
file_line: Some((file, line)),
}
}
/// Extract the timestamp from the block.
pub fn timestamp(&self) -> ::demo_primitives::Timestamp {
let x = self.inner.extrinsics.get(TIMESTAMP_SET_POSITION as usize).and_then(|xt| match xt.function {
Call::Timestamp(TimestampCall::set(x)) => Some(x),
_ => None
});
match x {
Some(x) => x,
None => panic!("Invalid block asserted at {:?}", self.file_line),
}
}
/// Extract the noted missed proposal validator indices (if any) from the block.
pub fn noted_offline(&self) -> &[u32] {
self.inner.extrinsics.get(NOTE_OFFLINE_POSITION as usize).and_then(|xt| match xt.function {
Call::Consensus(ConsensusCall::note_offline(ref x)) => Some(&x[..]),
_ => None,
}).unwrap_or(&[])
}
/// Convert into inner block.
pub fn into_inner(self) -> Block { self.inner }
}
impl ::std::ops::Deref for CheckedBlock {
type Target = Block;
fn deref(&self) -> &Block { &self.inner }
}
/// Assert that a block is structurally valid. May lead to panic in the future
/// in case it isn't.
#[macro_export]
macro_rules! assert_demo_block {
($block: expr) => {
$crate::CheckedBlock::new_unchecked($block, file!(), line!())
}
}
+49 -10
View File
@@ -1,4 +1,4 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate Demo.
// Substrate Demo is free software: you can redistribute it and/or modify
@@ -40,6 +40,7 @@ extern crate substrate_primitives;
#[macro_use]
extern crate substrate_codec_derive;
#[cfg_attr(not(feature = "std"), macro_use)]
extern crate substrate_runtime_std as rstd;
extern crate substrate_runtime_balances as balances;
extern crate substrate_runtime_consensus as consensus;
@@ -55,7 +56,11 @@ extern crate substrate_runtime_treasury as treasury;
extern crate substrate_runtime_version as version;
extern crate demo_primitives;
use demo_primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, SessionKey, Signature};
#[cfg(feature = "std")]
mod checked_block;
use rstd::prelude::*;
use demo_primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, SessionKey, Signature, InherentData};
use runtime_primitives::generic;
use runtime_primitives::traits::{Convert, BlakeTwo256, DigestItem};
use version::RuntimeVersion;
@@ -63,7 +68,15 @@ use council::motions as council_motions;
use substrate_primitives::u32_trait::{_2, _4};
#[cfg(any(feature = "std", test))]
pub use runtime_primitives::{BuildStorage, Permill};
pub use runtime_primitives::BuildStorage;
pub use consensus::Call as ConsensusCall;
pub use timestamp::Call as TimestampCall;
pub use runtime_primitives::Permill;
#[cfg(any(feature = "std", test))]
pub use checked_block::CheckedBlock;
const TIMESTAMP_SET_POSITION: u32 = 0;
const NOTE_OFFLINE_POSITION: u32 = 1;
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -74,7 +87,7 @@ pub struct Runtime;
/// Runtime version.
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: ver_str!("demo"),
impl_name: ver_str!("parity-demo"),
impl_name: ver_str!("substrate-demo"),
authoring_version: 1,
spec_version: 1,
impl_version: 0,
@@ -107,7 +120,7 @@ impl balances::Trait for Runtime {
pub type Balances = balances::Module<Runtime>;
impl consensus::Trait for Runtime {
const NOTE_OFFLINE_POSITION: u32 = 1;
const NOTE_OFFLINE_POSITION: u32 = NOTE_OFFLINE_POSITION;
type Log = Log;
type SessionKey = SessionKey;
type OnOfflineValidator = Staking;
@@ -117,8 +130,7 @@ impl consensus::Trait for Runtime {
pub type Consensus = consensus::Module<Runtime>;
impl timestamp::Trait for Runtime {
const TIMESTAMP_SET_POSITION: u32 = 0;
const TIMESTAMP_SET_POSITION: u32 = TIMESTAMP_SET_POSITION;
type Moment = u64;
}
@@ -240,6 +252,8 @@ impl DigestItem for Log {
}
}
/// The address format for describing accounts.
pub use balances::address::Address as RawAddress;
/// The address format for describing accounts.
pub type Address = balances::Address<Runtime>;
/// Block header type as expected by this runtime.
@@ -254,18 +268,43 @@ pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Index, Call,
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Index, Call>;
/// Executive: handles dispatch to the various modules.
pub type Executive = executive::Executive<Runtime, Block, Balances, Balances,
((((((), Treasury), Council), Democracy), Staking), Session)>;
(((((((), Treasury), Council), Democracy), Staking), Session), Timestamp)>;
pub mod api {
impl_stubs!(
version => |()| super::VERSION,
authorities => |()| super::Consensus::authorities(),
events => |()| super::System::events(),
initialise_block => |header| super::Executive::initialise_block(&header),
apply_extrinsic => |extrinsic| super::Executive::apply_extrinsic(extrinsic),
execute_block => |block| super::Executive::execute_block(block),
finalise_block => |()| super::Executive::finalise_block(),
inherent_extrinsics => |(inherent, spec_version)| super::inherent_extrinsics(inherent, spec_version),
validator_count => |()| super::Session::validator_count(),
validators => |()| super::Session::validators()
validators => |()| super::Session::validators(),
timestamp => |()| super::Timestamp::get(),
random_seed => |()| super::System::random_seed(),
account_nonce => |account| super::System::account_nonce(&account),
lookup_address => |address| super::Balances::lookup_address(address)
);
}
/// Produces the list of inherent extrinsics.
fn inherent_extrinsics(data: InherentData, _spec_version: u32) -> Vec<UncheckedExtrinsic> {
let make_inherent = |function| UncheckedExtrinsic {
signature: Default::default(),
function,
index: 0,
};
let mut inherent = vec![
make_inherent(Call::Timestamp(TimestampCall::set(data.timestamp))),
];
if !data.offline_indices.is_empty() {
inherent.push(make_inherent(
Call::Consensus(ConsensusCall::note_offline(data.offline_indices))
));
}
inherent
}