Commit bde1b099 authored by Gav Wood's avatar Gav Wood Committed by Arkadiy Paronyan
Browse files

Bring runtime API up to date with Substrate master (#17)

* Fixups for 646

* Fixes for API

* For for #678

* Fix runtime

* Update and build

* Tests build

* Fix tests
parent c2938ed9
......@@ -17,8 +17,8 @@
//! Basic parachain that adds a number as part of its state.
#[macro_use]
extern crate substrate_codec_derive;
extern crate substrate_codec as codec;
extern crate parity_codec_derive;
extern crate parity_codec as codec;
extern crate polkadot_parachain as parachain;
extern crate tiny_keccak;
......
......@@ -6,11 +6,11 @@ authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
serde = { version = "1.0", default_features = false }
serde_derive = { version = "1.0", optional = true }
substrate-codec = { git = "https://github.com/paritytech/substrate", default_features = false }
substrate-codec-derive = { git = "https://github.com/paritytech/substrate", default_features = false }
parity-codec = { git = "https://github.com/paritytech/substrate", default_features = false }
parity-codec-derive = { git = "https://github.com/paritytech/substrate", default_features = false }
substrate-primitives = { git = "https://github.com/paritytech/substrate", default_features = false }
substrate-runtime-std = { git = "https://github.com/paritytech/substrate", default_features = false }
substrate-runtime-primitives = { git = "https://github.com/paritytech/substrate", default_features = false }
sr-std = { git = "https://github.com/paritytech/substrate", default_features = false }
sr-primitives = { git = "https://github.com/paritytech/substrate", default_features = false }
[dev-dependencies]
substrate-serializer = { git = "https://github.com/paritytech/substrate" }
......@@ -19,10 +19,10 @@ pretty_assertions = "0.4"
[features]
default = ["std"]
std = [
"substrate-codec/std",
"parity-codec/std",
"substrate-primitives/std",
"substrate-runtime-std/std",
"substrate-runtime-primitives/std",
"sr-std/std",
"sr-primitives/std",
"serde_derive",
"serde/std",
]
......@@ -21,16 +21,16 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc))]
extern crate substrate_codec as codec;
extern crate parity_codec as codec;
extern crate substrate_primitives as primitives;
extern crate substrate_runtime_primitives as runtime_primitives;
extern crate substrate_runtime_std as rstd;
extern crate sr_primitives as runtime_primitives;
extern crate sr_std as rstd;
#[cfg(test)]
extern crate substrate_serializer;
#[macro_use]
extern crate substrate_codec_derive;
extern crate parity_codec_derive;
#[cfg(feature = "std")]
#[macro_use]
......@@ -39,27 +39,12 @@ extern crate serde_derive;
#[cfg(feature = "std")]
extern crate serde;
#[cfg(feature = "std")]
use primitives::bytes;
use rstd::prelude::*;
use runtime_primitives::traits::BlakeTwo256;
use runtime_primitives::generic;
use runtime_primitives::{generic, traits::BlakeTwo256};
pub mod parachain;
/// Block header type as expected by this runtime.
pub type Header = generic::Header<BlockNumber, BlakeTwo256, Log>;
/// Opaque, encoded, unchecked extrinsic.
pub type UncheckedExtrinsic = Vec<u8>;
/// A "future-proof" block type for Polkadot. This will be resilient to upgrades in transaction
/// format, because it doesn't attempt to decode extrinsics.
///
/// Specialized code needs to link to (at least one version of) the runtime directly
/// in order to handle the extrinsics within.
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
#[cfg(feature = "std")]
use primitives::bytes;
/// An index to a block.
/// 32-bits will allow for 136 years of blocks assuming 1 block per second.
......@@ -88,7 +73,7 @@ pub type Index = u32;
/// Alias to 512-bit hash when used in the context of a signature on the relay chain.
/// Equipped with logic for possibly "unsigned" messages.
pub type Signature = runtime_primitives::MaybeUnsigned<runtime_primitives::Ed25519Signature>;
pub type Signature = runtime_primitives::Ed25519Signature;
/// A timestamp: seconds since the unix epoch.
pub type Timestamp = u64;
......@@ -102,14 +87,17 @@ pub type Timestamp = u64;
/// that 32 bits may be multiplied with a balance in 128 bits without worrying about overflow.
pub type Balance = u128;
/// "generic" block ID for the future-proof block type.
// TODO: parameterize blockid only as necessary.
/// Header type.
pub type Header = generic::Header<BlockNumber, BlakeTwo256, generic::DigestItem<()>>;
/// Block type.
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
/// Block ID.
pub type BlockId = generic::BlockId<Block>;
/// A log entry in the block.
/// Opaque, encoded, unchecked extrinsic.
#[derive(PartialEq, Eq, Clone, Default, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct Log(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
pub struct UncheckedExtrinsic(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
/// Inherent data to include in a block.
#[derive(Encode, Decode)]
......
......@@ -10,25 +10,26 @@ serde = { version = "1.0", default_features = false }
serde_derive = { version = "1.0", optional = true }
safe-mix = { version = "1.0", default_features = false}
polkadot-primitives = { path = "../primitives", default_features = false }
substrate-codec = { git = "https://github.com/paritytech/substrate" }
substrate-codec-derive = { git = "https://github.com/paritytech/substrate" }
parity-codec = { git = "https://github.com/paritytech/substrate" }
parity-codec-derive = { git = "https://github.com/paritytech/substrate" }
substrate-serializer = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-std = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-io = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-support = { git = "https://github.com/paritytech/substrate" }
sr-std = { git = "https://github.com/paritytech/substrate" }
sr-io = { git = "https://github.com/paritytech/substrate" }
srml-support = { git = "https://github.com/paritytech/substrate" }
substrate-primitives = { git = "https://github.com/paritytech/substrate" }
substrate-keyring = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-balances = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-consensus = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-council = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-democracy = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-executive = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-primitives = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-session = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-staking = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-system = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-timestamp = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-version = { git = "https://github.com/paritytech/substrate" }
srml-balances = { git = "https://github.com/paritytech/substrate" }
srml-consensus = { git = "https://github.com/paritytech/substrate" }
srml-council = { git = "https://github.com/paritytech/substrate" }
srml-democracy = { git = "https://github.com/paritytech/substrate" }
srml-executive = { git = "https://github.com/paritytech/substrate" }
sr-primitives = { git = "https://github.com/paritytech/substrate" }
srml-session = { git = "https://github.com/paritytech/substrate" }
srml-staking = { git = "https://github.com/paritytech/substrate" }
srml-system = { git = "https://github.com/paritytech/substrate" }
srml-timestamp = { git = "https://github.com/paritytech/substrate" }
srml-treasury = { git = "https://github.com/paritytech/substrate" }
sr-version = { git = "https://github.com/paritytech/substrate" }
[dev-dependencies]
hex-literal = "0.1.0"
......@@ -37,23 +38,24 @@ hex-literal = "0.1.0"
default = ["std"]
std = [
"polkadot-primitives/std",
"substrate-codec/std",
"substrate-codec-derive/std",
"parity-codec/std",
"parity-codec-derive/std",
"substrate-primitives/std",
"substrate-runtime-std/std",
"substrate-runtime-io/std",
"substrate-runtime-support/std",
"substrate-runtime-balances/std",
"substrate-runtime-consensus/std",
"substrate-runtime-council/std",
"substrate-runtime-democracy/std",
"substrate-runtime-executive/std",
"substrate-runtime-primitives/std",
"substrate-runtime-session/std",
"substrate-runtime-staking/std",
"substrate-runtime-system/std",
"substrate-runtime-timestamp/std",
"substrate-runtime-version/std",
"sr-std/std",
"sr-io/std",
"srml-support/std",
"srml-balances/std",
"srml-consensus/std",
"srml-council/std",
"srml-democracy/std",
"srml-executive/std",
"sr-primitives/std",
"srml-session/std",
"srml-staking/std",
"srml-system/std",
"srml-timestamp/std",
"srml-treasury/std",
"sr-version/std",
"serde_derive",
"serde/std",
"log",
......
......@@ -32,7 +32,7 @@ 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.extrinsic.function {
!xt.is_signed() && match xt.function {
Call::Timestamp(TimestampCall::set(_)) => true,
_ => false,
}
......@@ -41,7 +41,7 @@ impl CheckedBlock {
if !has_timestamp { return Err(block) }
let has_heads = block.extrinsics.get(PARACHAINS_SET_POSITION as usize).map_or(false, |xt| {
!xt.is_signed() && match xt.extrinsic.function {
!xt.is_signed() && match xt.function {
Call::Parachains(ParachainsCall::set_heads(_)) => true,
_ => false,
}
......@@ -66,7 +66,7 @@ impl CheckedBlock {
/// Extract the timestamp from the block.
pub fn timestamp(&self) -> ::primitives::Timestamp {
let x = self.inner.extrinsics.get(TIMESTAMP_SET_POSITION as usize).and_then(|xt| match xt.extrinsic.function {
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
});
......@@ -79,7 +79,7 @@ impl CheckedBlock {
/// Extract the parachain heads from the block.
pub fn parachain_heads(&self) -> &[CandidateReceipt] {
let x = self.inner.extrinsics.get(PARACHAINS_SET_POSITION as usize).and_then(|xt| match xt.extrinsic.function {
let x = self.inner.extrinsics.get(PARACHAINS_SET_POSITION as usize).and_then(|xt| match xt.function {
Call::Parachains(ParachainsCall::set_heads(ref x)) => Some(&x[..]),
_ => None
});
......@@ -92,7 +92,7 @@ impl CheckedBlock {
/// 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.extrinsic.function {
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(&[])
......
......@@ -26,13 +26,13 @@ extern crate serde_derive;
extern crate serde;
#[macro_use]
extern crate substrate_runtime_io as runtime_io;
extern crate sr_io as runtime_io;
#[macro_use]
extern crate substrate_runtime_support;
extern crate srml_support;
#[macro_use]
extern crate substrate_runtime_primitives as runtime_primitives;
extern crate sr_primitives as runtime_primitives;
#[cfg(test)]
#[macro_use]
......@@ -44,23 +44,24 @@ extern crate substrate_serializer;
extern crate substrate_primitives;
#[macro_use]
extern crate substrate_runtime_std as rstd;
extern crate sr_std as rstd;
#[macro_use]
extern crate substrate_codec_derive;
extern crate parity_codec_derive;
extern crate polkadot_primitives as primitives;
extern crate substrate_codec as codec;
extern crate substrate_runtime_balances as balances;
extern crate substrate_runtime_consensus as consensus;
extern crate substrate_runtime_council as council;
extern crate substrate_runtime_democracy as democracy;
extern crate substrate_runtime_executive as executive;
extern crate substrate_runtime_session as session;
extern crate substrate_runtime_staking as staking;
extern crate substrate_runtime_system as system;
extern crate substrate_runtime_timestamp as timestamp;
extern crate parity_codec as codec;
extern crate srml_balances as balances;
extern crate srml_consensus as consensus;
extern crate srml_council as council;
extern crate srml_democracy as democracy;
extern crate srml_executive as executive;
extern crate srml_session as session;
extern crate srml_staking as staking;
extern crate srml_system as system;
extern crate srml_timestamp as timestamp;
extern crate srml_treasury as treasury;
#[macro_use]
extern crate substrate_runtime_version as version;
extern crate sr_version as version;
#[cfg(feature = "std")]
mod checked_block;
......@@ -72,9 +73,13 @@ pub use checked_block::CheckedBlock;
pub use utils::{inherent_extrinsics, check_extrinsic};
pub use balances::address::Address as RawAddress;
use primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, Log, SessionKey, Signature};
use runtime_primitives::{generic, traits::{HasPublicAux, BlakeTwo256, Convert}};
use rstd::prelude::*;
use codec::{Encode, Decode, Input};
use substrate_primitives::u32_trait::{_2, _4};
use primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, SessionKey, Signature};
use runtime_primitives::{generic, traits::{Convert, BlakeTwo256, DigestItem}};
use version::RuntimeVersion;
use council::{motions as council_motions, voting as council_voting};
#[cfg(feature = "std")]
pub use runtime_primitives::BuildStorage;
......@@ -82,7 +87,6 @@ pub use runtime_primitives::BuildStorage;
pub use consensus::Call as ConsensusCall;
pub use timestamp::Call as TimestampCall;
pub use parachains::Call as ParachainsCall;
pub use primitives::Header;
/// The position of the timestamp set extrinsic.
pub const TIMESTAMP_SET_POSITION: u32 = 0;
......@@ -91,24 +95,24 @@ pub const PARACHAINS_SET_POSITION: u32 = 1;
/// The position of the note_offline in the block, if it exists.
pub const NOTE_OFFLINE_POSITION: u32 = 2;
/// Block header type as expected by this runtime.
pub type Header = generic::Header<BlockNumber, BlakeTwo256, Log>;
/// The address format for describing accounts.
pub type Address = balances::Address<Concrete>;
pub type Address = balances::Address<Runtime>;
/// Block Id type for this block.
pub type BlockId = generic::BlockId<Block>;
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Index, Call, Signature>;
/// Extrinsic type as expected by this runtime. This is not the type that is signed.
pub type Extrinsic = generic::Extrinsic<Address, Index, Call>;
/// Extrinsic type that is signed.
pub type BareExtrinsic = generic::Extrinsic<AccountId, Index, Call>;
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Index, Call>;
/// Block type as expected by this runtime.
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
/// Concrete runtime type used to parameterize the various modules.
/// Runtime runtime type used to parameterize the various modules.
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub struct Concrete;
pub struct Runtime;
/// Polkadot runtime version.
pub const VERSION: RuntimeVersion = RuntimeVersion {
......@@ -119,19 +123,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 0,
};
impl version::Trait for Concrete {
const VERSION: RuntimeVersion = VERSION;
}
/// Version module for this concrete runtime.
pub type Version = version::Module<Concrete>;
impl HasPublicAux for Concrete {
type PublicAux = AccountId; // TODO: Option<AccountId>
}
impl system::Trait for Concrete {
type PublicAux = <Concrete as HasPublicAux>::PublicAux;
impl system::Trait for Runtime {
type Origin = Origin;
type Index = Index;
type BlockNumber = BlockNumber;
type Hash = Hash;
......@@ -142,9 +135,9 @@ impl system::Trait for Concrete {
type Event = Event;
}
/// System module for this concrete runtime.
pub type System = system::Module<Concrete>;
pub type System = system::Module<Runtime>;
impl balances::Trait for Concrete {
impl balances::Trait for Runtime {
type Balance = Balance;
type AccountIndex = AccountIndex;
type OnFreeBalanceZero = Staking;
......@@ -152,22 +145,23 @@ impl balances::Trait for Concrete {
type Event = Event;
}
/// Staking module for this concrete runtime.
pub type Balances = balances::Module<Concrete>;
pub type Balances = balances::Module<Runtime>;
impl consensus::Trait for Concrete {
impl consensus::Trait for Runtime {
const NOTE_OFFLINE_POSITION: u32 = NOTE_OFFLINE_POSITION;
type Log = Log;
type SessionKey = SessionKey;
type OnOfflineValidator = Staking;
}
/// Consensus module for this concrete runtime.
pub type Consensus = consensus::Module<Concrete>;
pub type Consensus = consensus::Module<Runtime>;
impl timestamp::Trait for Concrete {
impl timestamp::Trait for Runtime {
const TIMESTAMP_SET_POSITION: u32 = TIMESTAMP_SET_POSITION;
type Moment = u64;
}
/// Timestamp module for this concrete runtime.
pub type Timestamp = timestamp::Module<Concrete>;
pub type Timestamp = timestamp::Module<Runtime>;
/// Session key conversion.
pub struct SessionKeyConversion;
......@@ -177,82 +171,111 @@ impl Convert<AccountId, SessionKey> for SessionKeyConversion {
}
}
impl session::Trait for Concrete {
impl session::Trait for Runtime {
type ConvertAccountIdToSessionKey = SessionKeyConversion;
type OnSessionChange = Staking;
type Event = Event;
}
/// Session module for this concrete runtime.
pub type Session = session::Module<Concrete>;
pub type Session = session::Module<Runtime>;
impl staking::Trait for Concrete {
impl staking::Trait for Runtime {
type OnRewardMinted = Treasury;
type Event = Event;
}
/// Staking module for this concrete runtime.
pub type Staking = staking::Module<Concrete>;
pub type Staking = staking::Module<Runtime>;
impl democracy::Trait for Concrete {
type Proposal = PrivCall;
impl democracy::Trait for Runtime {
type Proposal = Call;
type Event = Event;
}
/// Democracy module for this concrete runtime.
pub type Democracy = democracy::Module<Concrete>;
pub type Democracy = democracy::Module<Runtime>;
impl council::Trait for Runtime {
type Event = Event;
}
impl council::Trait for Concrete {}
/// Council module for this concrete runtime.
pub type Council = council::Module<Concrete>;
pub type Council = council::Module<Runtime>;
impl council::voting::Trait for Runtime {
type Event = Event;
}
/// Council voting module for this concrete runtime.
pub type CouncilVoting = council::voting::Module<Concrete>;
pub type CouncilVoting = council::voting::Module<Runtime>;
impl parachains::Trait for Concrete {
const SET_POSITION: u32 = PARACHAINS_SET_POSITION;
impl council::motions::Trait for Runtime {
type Origin = Origin;
type Proposal = Call;
type Event = Event;
}
/// Council motions module for this concrete runtime.
pub type CouncilMotions = council_motions::Module<Runtime>;
type PublicAux = <Concrete as HasPublicAux>::PublicAux;
impl treasury::Trait for Runtime {
type ApproveOrigin = council_motions::EnsureMembers<_4>;
type RejectOrigin = council_motions::EnsureMembers<_2>;
type Event = Event;
}
pub type Parachains = parachains::Module<Concrete>;
/// Treasury module for this concrete runtime.
pub type Treasury = treasury::Module<Runtime>;
impl parachains::Trait for Runtime {
const SET_POSITION: u32 = PARACHAINS_SET_POSITION;
}
pub type Parachains = parachains::Module<Runtime>;
impl_outer_event! {
pub enum Event for Concrete {
balances, session, staking
pub enum Event for Runtime {
//consensus,
balances,
//timetstamp,
session,
staking,
democracy,
council,
council_voting,
council_motions,
treasury
}
}
impl_outer_dispatch! {
/// Call type for polkadot transactions.
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub enum Call where aux: <Concrete as HasPublicAux>::PublicAux {
Consensus = 0,
Balances = 1,
Session = 2,
Staking = 3,
Timestamp = 4,
Democracy = 5,
Council = 6,
CouncilVoting = 7,
Parachains = 8,
impl_outer_log! {
pub enum Log(InternalLog: DigestItem<SessionKey>) for Runtime {
consensus(AuthoritiesChange)
}
}
/// Internal calls.
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub enum PrivCall {
Consensus = 0,
Balances = 1,
Session = 2,
Staking = 3,
Democracy = 5,
Council = 6,
CouncilVoting = 7,
Parachains = 8,
impl_outer_origin! {
pub enum Origin for Runtime {
council_motions
}
}
/// Executive: handles dispatch to the various modules.
pub type Executive = executive::Executive<Concrete, Block, Balances, Balances,
(((((((), Parachains), Council), Democracy), Staking), Session), Timestamp)>;
impl_outer_dispatch! {
/// Call type for polkadot transactions.
pub enum Call where origin: <Runtime as system::Trait>::Origin {
Consensus,
Balances,
Session,
Staking,
Timestamp,
Democracy,
Council,
CouncilVoting,
CouncilMotions,
Parachains,
Treasury,
}
}
impl_outer_config! {
pub struct GenesisConfig for Concrete {
pub struct GenesisConfig for Runtime {
ConsensusConfig => consensus,
SystemConfig => system,
BalancesConfig => balances,
......@@ -261,13 +284,58 @@ impl_outer_config! {
DemocracyConfig => democracy,
CouncilConfig => council,
TimestampConfig => timestamp,
TreasuryConfig => treasury,
ParachainsConfig => parachains,
}
}
type AllModules = (
Consensus,
Balances,
Session,
Staking,
Timestamp,
Democracy,
Council,
CouncilVoting,
CouncilMotions,
Parachains,
Treasury,
);
impl_json_metadata!(
for Runtime with modules
system::Module with Storage,
consensus::Module with Storage,
balances::Module with Storage,
timestamp::Module with Storage,
session::Module with Storage,
staking::Module with Storage,
democracy::Module with Storage,
council::Module with Storage,
council_voting::Module with Storage,
council_motions::Module with Storage,
treasury::Module with Storage,
parachains::Module with Storage,
);