Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ members = [
"substrate/runtime/staking",
"substrate/runtime/system",
"substrate/runtime/timestamp",
"substrate/runtime/version",
"substrate/serializer",
"substrate/state-db",
"substrate/state-machine",
Expand Down
2 changes: 1 addition & 1 deletion demo/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extern crate triehash;
#[cfg(test)] extern crate substrate_runtime_system as system;
#[cfg(test)] #[macro_use] extern crate hex_literal;

native_executor_instance!(pub Executor, demo_runtime::api::dispatch, include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm"));
native_executor_instance!(pub Executor, demo_runtime::api::dispatch, demo_runtime::VERSION, include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm"));

#[cfg(test)]
mod tests {
Expand Down
2 changes: 2 additions & 0 deletions demo/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ substrate-runtime-session = { path = "../../substrate/runtime/session" }
substrate-runtime-staking = { path = "../../substrate/runtime/staking" }
substrate-runtime-system = { path = "../../substrate/runtime/system" }
substrate-runtime-timestamp = { path = "../../substrate/runtime/timestamp" }
substrate-runtime-version = { path = "../../substrate/runtime/version" }
demo-primitives = { path = "../primitives" }

[features]
Expand All @@ -44,6 +45,7 @@ std = [
"substrate-runtime-staking/std",
"substrate-runtime-system/std",
"substrate-runtime-timestamp/std",
"substrate-runtime-version/std",
"demo-primitives/std",
"serde_derive",
"serde/std",
Expand Down
20 changes: 20 additions & 0 deletions demo/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ 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;
#[macro_use]
extern crate substrate_runtime_version as version;
extern crate demo_primitives;

use rstd::prelude::*;
use demo_primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, SessionKey, Signature};
use runtime_primitives::generic;
use runtime_primitives::traits::{Convert, HasPublicAux, BlakeTwo256};
use version::RuntimeVersion;

#[cfg(any(feature = "std", test))]
pub use runtime_primitives::BuildStorage;
Expand All @@ -59,6 +62,22 @@ pub use runtime_primitives::BuildStorage;
/// Concrete runtime type used to parameterize the various modules.
pub struct Concrete;

/// Runtime version.
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: ver_str!("demo"),
impl_name: ver_str!("parity-demo"),
authoring_version: 0,
spec_version: 0,
impl_version: 0,
};

/// Version module for this concrete runtime.
pub type Version = version::Module<Concrete>;

impl version::Trait for Concrete {
const VERSION: RuntimeVersion = VERSION;
}

impl HasPublicAux for Concrete {
type PublicAux = AccountId;
}
Expand Down Expand Up @@ -189,6 +208,7 @@ impl_outer_config! {

pub mod api {
impl_stubs!(
version => |()| super::Version::version(),
authorities => |()| super::Consensus::authorities(),
initialise_block => |header| super::Executive::initialise_block(&header),
apply_extrinsic => |extrinsic| super::Executive::apply_extrinsic(extrinsic),
Expand Down
12 changes: 12 additions & 0 deletions demo/runtime/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions demo/runtime/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ substrate-runtime-session = { path = "../../../substrate/runtime/session", defau
substrate-runtime-staking = { path = "../../../substrate/runtime/staking", default-features = false }
substrate-runtime-system = { path = "../../../substrate/runtime/system", default-features = false }
substrate-runtime-timestamp = { path = "../../../substrate/runtime/timestamp", default-features = false }
substrate-runtime-version = { path = "../../../substrate/runtime/version", default-features = false }
demo-primitives = { path = "../../primitives", default-features = false }

[features]
Expand All @@ -43,6 +44,7 @@ std = [
"substrate-runtime-staking/std",
"substrate-runtime-system/std",
"substrate-runtime-timestamp/std",
"substrate-runtime-version/std",
"demo-primitives/std",
]

Expand Down
Binary file not shown.
Binary file not shown.
69 changes: 22 additions & 47 deletions polkadot/api/src/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,21 @@ use client::backend::{Backend, LocalBackend};
use client::block_builder::BlockBuilder as ClientBlockBuilder;
use client::{Client, LocalCallExecutor};
use polkadot_executor::Executor as LocalDispatch;
use substrate_executor::{NativeExecutionDispatch, NativeExecutor};
use substrate_executor::NativeExecutor;
use state_machine;

use runtime::Address;
use runtime_primitives::traits::AuxLookup;
use primitives::{AccountId, Block, Header, BlockId, Hash, Index, SessionKey, Timestamp, UncheckedExtrinsic};
use primitives::parachain::{CandidateReceipt, DutyRoster, Id as ParaId};

use {CheckedBlockId, BlockBuilder, PolkadotApi, LocalPolkadotApi, ErrorKind, Error, Result};

/// A checked block ID used for the substrate-client implementation of CheckedBlockId;
#[derive(Debug, Clone, Copy)]
pub struct CheckedId(pub(crate) BlockId);

impl CheckedBlockId for CheckedId {
fn block_id(&self) -> &BlockId {
&self.0
}
}
use {BlockBuilder, PolkadotApi, LocalPolkadotApi, ErrorKind, Error, Result};

// set up the necessary scaffolding to execute a set of calls to the runtime.
// this creates a new block on top of the given ID and initialises it.
macro_rules! with_runtime {
($client: ident, $at: expr, $exec: expr) => {{
let parent = $at.block_id();
let parent = $at;
let header = Header {
parent_hash: $client.block_hash_from_id(&parent)?
.ok_or_else(|| ErrorKind::UnknownBlock(format!("{:?}", parent)))?,
Expand Down Expand Up @@ -83,39 +73,29 @@ impl<B: LocalBackend<Block>> BlockBuilder for ClientBlockBuilder<B, LocalCallExe
impl<B: LocalBackend<Block>> PolkadotApi for Client<B, LocalCallExecutor<B, NativeExecutor<LocalDispatch>>, Block>
where ::client::error::Error: From<<<B as Backend<Block>>::State as state_machine::backend::Backend>::Error>
{
type CheckedBlockId = CheckedId;
type BlockBuilder = ClientBlockBuilder<B, LocalCallExecutor<B, NativeExecutor<LocalDispatch>>, Block>;

fn check_id(&self, id: BlockId) -> Result<CheckedId> {
// bail if the code is not the same as the natively linked.
if self.code_at(&id.into())? != LocalDispatch::native_equivalent() {
bail!("This node is out of date. Block authoring may not work correctly. Bailing.")
}

Ok(CheckedId(id))
}

fn session_keys(&self, at: &CheckedId) -> Result<Vec<SessionKey>> {
fn session_keys(&self, at: &BlockId) -> Result<Vec<SessionKey>> {
with_runtime!(self, at, ::runtime::Consensus::authorities)
}

fn validators(&self, at: &CheckedId) -> Result<Vec<AccountId>> {
fn validators(&self, at: &BlockId) -> Result<Vec<AccountId>> {
with_runtime!(self, at, ::runtime::Session::validators)
}

fn random_seed(&self, at: &CheckedId) -> Result<Hash> {
fn random_seed(&self, at: &BlockId) -> Result<Hash> {
with_runtime!(self, at, ::runtime::System::random_seed)
}

fn duty_roster(&self, at: &CheckedId) -> Result<DutyRoster> {
fn duty_roster(&self, at: &BlockId) -> Result<DutyRoster> {
with_runtime!(self, at, ::runtime::Parachains::calculate_duty_roster)
}

fn timestamp(&self, at: &CheckedId) -> Result<Timestamp> {
fn timestamp(&self, at: &BlockId) -> Result<Timestamp> {
with_runtime!(self, at, ::runtime::Timestamp::get)
}

fn evaluate_block(&self, at: &CheckedId, block: Block) -> Result<bool> {
fn evaluate_block(&self, at: &BlockId, block: Block) -> Result<bool> {
use substrate_executor::error::ErrorKind as ExecErrorKind;
use codec::Slicable;
use runtime::Block as RuntimeBlock;
Expand All @@ -136,36 +116,36 @@ impl<B: LocalBackend<Block>> PolkadotApi for Client<B, LocalCallExecutor<B, Nati
}
}

fn index(&self, at: &CheckedId, account: AccountId) -> Result<Index> {
fn index(&self, at: &BlockId, account: AccountId) -> Result<Index> {
with_runtime!(self, at, || ::runtime::System::account_nonce(account))
}

fn lookup(&self, at: &Self::CheckedBlockId, address: Address) -> Result<Option<AccountId>> {
fn lookup(&self, at: &BlockId, address: Address) -> Result<Option<AccountId>> {
with_runtime!(self, at, || <::runtime::Staking as AuxLookup>::lookup(address).ok())
}

fn active_parachains(&self, at: &CheckedId) -> Result<Vec<ParaId>> {
fn active_parachains(&self, at: &BlockId) -> Result<Vec<ParaId>> {
with_runtime!(self, at, ::runtime::Parachains::active_parachains)
}

fn parachain_code(&self, at: &CheckedId, parachain: ParaId) -> Result<Option<Vec<u8>>> {
fn parachain_code(&self, at: &BlockId, parachain: ParaId) -> Result<Option<Vec<u8>>> {
with_runtime!(self, at, || ::runtime::Parachains::parachain_code(parachain))
}

fn parachain_head(&self, at: &CheckedId, parachain: ParaId) -> Result<Option<Vec<u8>>> {
fn parachain_head(&self, at: &BlockId, parachain: ParaId) -> Result<Option<Vec<u8>>> {
with_runtime!(self, at, || ::runtime::Parachains::parachain_head(parachain))
}

fn build_block(&self, at: &CheckedId, timestamp: Timestamp, new_heads: Vec<CandidateReceipt>) -> Result<Self::BlockBuilder> {
let mut block_builder = self.new_block_at(at.block_id())?;
fn build_block(&self, at: &BlockId, timestamp: Timestamp, new_heads: Vec<CandidateReceipt>) -> Result<Self::BlockBuilder> {
let mut block_builder = self.new_block_at(at)?;
for inherent in self.inherent_extrinsics(at, timestamp, new_heads)? {
block_builder.push(inherent)?;
}

Ok(block_builder)
}

fn inherent_extrinsics(&self, at: &Self::CheckedBlockId, timestamp: Timestamp, new_heads: Vec<CandidateReceipt>) -> Result<Vec<UncheckedExtrinsic>> {
fn inherent_extrinsics(&self, at: &BlockId, timestamp: Timestamp, new_heads: Vec<CandidateReceipt>) -> Result<Vec<UncheckedExtrinsic>> {
use codec::Slicable;

with_runtime!(self, at, || {
Expand Down Expand Up @@ -231,7 +211,7 @@ mod tests {
#[test]
fn gets_session_and_validator_keys() {
let client = client();
let id = client.check_id(BlockId::number(0)).unwrap();
let id = BlockId::number(0);
assert_eq!(client.session_keys(&id).unwrap(), session_keys());
assert_eq!(client.validators(&id).unwrap(), validators());
}
Expand All @@ -240,7 +220,7 @@ mod tests {
fn build_block_implicit_succeeds() {
let client = client();

let id = client.check_id(BlockId::number(0)).unwrap();
let id = BlockId::number(0);
let block_builder = client.build_block(&id, 1_000_000, Vec::new()).unwrap();
let block = block_builder.bake().unwrap();

Expand All @@ -252,10 +232,10 @@ mod tests {
fn build_block_with_inherent_succeeds() {
let client = client();

let id = client.check_id(BlockId::number(0)).unwrap();
let id = BlockId::number(0);
let inherent = client.inherent_extrinsics(&id, 1_000_000, Vec::new()).unwrap();

let mut block_builder = client.new_block_at(id.block_id()).unwrap();
let mut block_builder = client.new_block_at(&id).unwrap();
for extrinsic in inherent {
block_builder.push(extrinsic).unwrap();
}
Expand All @@ -266,16 +246,11 @@ mod tests {
assert!(block.header.extrinsics_root != Default::default());
}

#[test]
fn fails_to_check_id_for_unknown_block() {
assert!(client().check_id(BlockId::number(100)).is_err());
}

#[test]
fn gets_random_seed_with_genesis() {
let client = client();

let id = client.check_id(BlockId::number(0)).unwrap();
let id = BlockId::number(0);
assert!(client.random_seed(&id).is_ok());
}
}
Loading