Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ON-HOLD] KPR-139: Update 19 01 08 #7

Closed
wants to merge 10 commits into from
Closed
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
1,328 changes: 829 additions & 499 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ substrate-consensus-aura = { git = "https://github.com/paritytech/substrate" }
substrate-client = { git = "https://github.com/paritytech/substrate", default-features = false }
substrate-finality-grandpa = { git = "https://github.com/paritytech/substrate" }
template-node-runtime = { path = "runtime" }
node-executor = { git = "https://github.com/paritytech/substrate" }
structopt = "0.2.13"

[build-dependencies]
Expand Down
4 changes: 1 addition & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

set -e

echo "obtain the project folder"
PROJECT_ROOT=`pwd`
#PROJECT_ROOT=`git rev-parse --show-toplevel`
PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

export CARGO_INCREMENTAL=0

Expand Down
7 changes: 5 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ substrate-primitives = { git = "https://github.com/paritytech/substrate" }
substrate-keyring = { git = "https://github.com/paritytech/substrate" }
srml-balances = { git = "https://github.com/paritytech/substrate" }
srml-consensus = { git = "https://github.com/paritytech/substrate" }
srml-aura = { git = "https://github.com/paritytech/substrate" }
srml-executive = { git = "https://github.com/paritytech/substrate" }
sr-primitives = { git = "https://github.com/paritytech/substrate" }
srml-system = { git = "https://github.com/paritytech/substrate" }
srml-timestamp = { git = "https://github.com/paritytech/substrate" }
srml-upgrade-key = { git = "https://github.com/paritytech/substrate" }
substrate-client = { git = "https://github.com/paritytech/substrate", optional = true }
sr-version = { git = "https://github.com/paritytech/substrate" }
substrate-consensus-aura-primitives = { git = "https://github.com/paritytech/substrate" }

[features]
default = ["std"]
Expand All @@ -36,8 +38,8 @@ std = [
"sr-io/std",
"srml-support/std",
"srml-balances/std",
"srml-consensus/std",
"srml-executive/std",
"srml-aura/std",
"sr-primitives/std",
"srml-system/std",
"srml-timestamp/std",
Expand All @@ -46,5 +48,6 @@ std = [
"serde_derive",
"serde/std",
"safe-mix/std",
"substrate-client"
"substrate-client",
"substrate-consensus-aura-primitives/std",
]
1 change: 1 addition & 0 deletions runtime/src/ctype.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// initialise with:
// post({sender: runtime.balances.ss58Decode('F7Gh'), call: calls.demo.setPayment(1000)}).tie(console.log)

use rstd::prelude::*;
use runtime_primitives::codec::Codec;
use runtime_primitives::verify_encoded_lazy;
use srml_support::{dispatch::Result, StorageMap};
Expand Down
233 changes: 54 additions & 179 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,23 @@ extern crate srml_executive as executive;
extern crate srml_system as system;
extern crate srml_timestamp as timestamp;
extern crate srml_upgrade_key as upgrade_key;
extern crate srml_aura as aura;
extern crate substrate_consensus_aura_primitives as consensus_aura;

#[cfg(feature = "std")]
use parity_codec::{Encode, Decode};
use rstd::prelude::*;
#[cfg(feature = "std")]
use primitives::bytes;
use primitives::AuthorityId;
use primitives::OpaqueMetadata;
use runtime_primitives::{ApplyResult, transaction_validity::TransactionValidity,
Ed25519Signature, generic, traits::{self, BlakeTwo256, Block as BlockT}
use primitives::{AuthorityId, OpaqueMetadata};
use runtime_primitives::{
ApplyResult, transaction_validity::TransactionValidity, Ed25519Signature, generic,
traits::{self, BlakeTwo256, Block as BlockT, ProvideInherent},
BasicInherentData, CheckInherentError
};
#[cfg(feature = "std")]
use runtime_primitives::traits::ApiRef;
use client::{block_builder::api::runtime::*, runtime_api::{runtime::*, id::*}};
#[cfg(feature = "std")]
use client::runtime_api::ApiExt;
use client::{block_builder::api as block_builder_api, runtime_api};
use version::RuntimeVersion;
#[cfg(feature = "std")]
use version::NativeVersion;
use consensus_aura::api as aura_api;

// A few exports that help ease life for downstream crates.
pub use balances::Call as BalancesCall;
Expand Down Expand Up @@ -97,20 +95,18 @@ pub mod opaque {
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
/// Opaque block identifier type.
pub type BlockId = generic::BlockId<Block>;
/// Opaque session key type.
pub type SessionKey = AuthorityId;
}

/// This runtime version.
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: ver_str!("template-node"),
impl_name: ver_str!("template-node"),
spec_name: create_runtime_str!("template-node"),
impl_name: create_runtime_str!("template-node"),
authoring_version: 1,
spec_version: 2,
impl_version: 0,
apis: apis_vec!([
(BLOCK_BUILDER, 1),
(TAGGED_TRANSACTION_QUEUE, 1),
(METADATA, 1)
]),
apis: RUNTIME_API_VERSIONS,
};

/// The version infromation used to identify this runtime when compiled natively.
Expand Down Expand Up @@ -145,13 +141,18 @@ impl system::Trait for Runtime {
type Origin = Origin;
}

impl aura::Trait for Runtime {
type HandleReport = ();
}

impl consensus::Trait for Runtime {
/// The position in the block's extrinsics that the note-offline inherent must be placed.
const NOTE_OFFLINE_POSITION: u32 = 1;
/// The identifier we use to refer to authorities.
type SessionKey = AuthorityId;
/// No action in case an authority was determined to be offline.
type OnOfflineValidator = ();
// The aura module handles offline-reports internally
// rather than using an explicit report system.
type InherentOfflineReport = ();
/// The ubiquitous log type.
type Log = Log;
}
Expand All @@ -161,6 +162,7 @@ impl timestamp::Trait for Runtime {
const TIMESTAMP_SET_POSITION: u32 = 0;
/// A timestamp: seconds since the unix epoch.
type Moment = u64;
type OnTimestampSet = Aura;
}

impl balances::Trait for Runtime {
Expand Down Expand Up @@ -200,11 +202,13 @@ impl ctype::Trait for Runtime {
construct_runtime!(
pub enum Runtime with Log(InternalLog: DigestItem<Hash, AuthorityId>) where
Block = Block,
UncheckedExtrinsic = UncheckedExtrinsic
NodeBlock = opaque::Block,
InherentData = BasicInherentData
{
System: system::{default, Log(ChangesTrieRoot)},
Timestamp: timestamp::{Module, Call, Storage, Config<T>, Inherent},
Consensus: consensus::{Module, Call, Storage, Config<T>, Log(AuthoritiesChange), Inherent},
Aura: aura::{Module},
Balances: balances,
UpgradeKey: upgrade_key,
Ctype: ctype::{Module, Call, Storage},
Expand All @@ -230,159 +234,9 @@ pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Nonce, Call>;
/// Executive: handles dispatch to the various modules.
pub type Executive = executive::Executive<Runtime, Block, Context, Balances, AllModules>;

#[cfg(feature = "std")]
use opaque::Block as GBlock;

#[cfg(feature = "std")]
pub struct ClientWithApi {
call: ::std::ptr::NonNull<client::runtime_api::CallApiAt<GBlock>>,
commit_on_success: ::std::cell::RefCell<bool>,
initialised_block: ::std::cell::RefCell<Option<GBlockId>>,
changes: ::std::cell::RefCell<client::runtime_api::OverlayedChanges>,
}

#[cfg(feature = "std")]
unsafe impl Send for ClientWithApi {}
#[cfg(feature = "std")]
unsafe impl Sync for ClientWithApi {}

#[cfg(feature = "std")]
impl ApiExt for ClientWithApi {
fn map_api_result<F: FnOnce(&Self) -> Result<R, E>, R, E>(&self, map_call: F) -> Result<R, E> {
*self.commit_on_success.borrow_mut() = false;
let res = map_call(self);
*self.commit_on_success.borrow_mut() = true;

self.commit_on_ok(&res);

res
}
}

#[cfg(feature = "std")]
impl client::runtime_api::ConstructRuntimeApi<GBlock> for ClientWithApi {
fn construct_runtime_api<'a, T: client::runtime_api::CallApiAt<GBlock>>(call: &'a T) -> ApiRef<'a, Self> {
ClientWithApi {
call: unsafe {
::std::ptr::NonNull::new_unchecked(
::std::mem::transmute(
call as &client::runtime_api::CallApiAt<GBlock>
)
)
},
commit_on_success: true.into(),
initialised_block: None.into(),
changes: Default::default(),
}.into()
}
}

#[cfg(feature = "std")]
impl ClientWithApi {
fn call_api_at<A: Encode, R: Decode>(
&self,
at: &GBlockId,
function: &'static str,
args: &A
) -> client::error::Result<R> {
let res = unsafe {
self.call.as_ref().call_api_at(
at,
function,
args.encode(),
&mut *self.changes.borrow_mut(),
&mut *self.initialised_block.borrow_mut()
).and_then(|r|
R::decode(&mut &r[..])
.ok_or_else(||
client::error::ErrorKind::CallResultDecode(function).into()
)
)
};

self.commit_on_ok(&res);
res
}

fn commit_on_ok<R, E>(&self, res: &Result<R, E>) {
if *self.commit_on_success.borrow() {
if res.is_err() {
self.changes.borrow_mut().discard_prospective();
} else {
self.changes.borrow_mut().commit_prospective();
}
}
}
}

#[cfg(feature = "std")]
type GBlockId = generic::BlockId<GBlock>;

#[cfg(feature = "std")]
impl client::runtime_api::Core<GBlock> for ClientWithApi {
fn version(&self, at: &GBlockId) -> Result<RuntimeVersion, client::error::Error> {
self.call_api_at(at, "version", &())
}

fn authorities(&self, at: &GBlockId) -> Result<Vec<AuthorityId>, client::error::Error> {
self.call_api_at(at, "authorities", &())
}

fn execute_block(&self, at: &GBlockId, block: &GBlock) -> Result<(), client::error::Error> {
self.call_api_at(at, "execute_block", block)
}

fn initialise_block(&self, at: &GBlockId, header: &<GBlock as BlockT>::Header) -> Result<(), client::error::Error> {
self.call_api_at(at, "initialise_block", header)
}
}

#[cfg(feature = "std")]
impl client::block_builder::api::BlockBuilder<GBlock> for ClientWithApi {
fn apply_extrinsic(&self, at: &GBlockId, extrinsic: &<GBlock as BlockT>::Extrinsic) -> Result<ApplyResult, client::error::Error> {
self.call_api_at(at, "apply_extrinsic", extrinsic)
}

fn finalise_block(&self, at: &GBlockId) -> Result<<GBlock as BlockT>::Header, client::error::Error> {
self.call_api_at(at, "finalise_block", &())
}

fn inherent_extrinsics<Inherent: Decode + Encode, Unchecked: Decode + Encode>(
&self, at: &GBlockId, inherent: &Inherent
) -> Result<Vec<Unchecked>, client::error::Error> {
self.call_api_at(at, "inherent_extrinsics", inherent)
}

fn check_inherents<Inherent: Decode + Encode, Error: Decode + Encode>(&self, at: &GBlockId, block: &GBlock, inherent: &Inherent) -> Result<Result<(), Error>, client::error::Error> {
self.call_api_at(at, "check_inherents", &(block, inherent))
}

fn random_seed(&self, at: &GBlockId) -> Result<<GBlock as BlockT>::Hash, client::error::Error> {
self.call_api_at(at, "random_seed", &())
}
}

#[cfg(feature = "std")]
impl client::runtime_api::TaggedTransactionQueue<GBlock> for ClientWithApi {
fn validate_transaction(
&self,
at: &GBlockId,
utx: &<GBlock as BlockT>::Extrinsic
) -> Result<TransactionValidity, client::error::Error> {
self.call_api_at(at, "validate_transaction", utx)
}
}

#[cfg(feature = "std")]
impl client::runtime_api::Metadata<GBlock> for ClientWithApi {
fn metadata(&self, at: &GBlockId) -> Result<OpaqueMetadata, client::error::Error> {
self.call_api_at(at, "metadata", &())
}
}

// Implement our runtime API endpoints. This is just a bunch of proxying.
impl_runtime_apis! {
impl Core<Block> for Runtime {
impl runtime_api::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
VERSION
}
Expand All @@ -400,13 +254,13 @@ impl_runtime_apis! {
}
}

impl Metadata for Runtime {
impl runtime_api::Metadata<Block> for Runtime {
fn metadata() -> OpaqueMetadata {
Runtime::metadata().into()
}
}

impl BlockBuilder<Block, InherentData, UncheckedExtrinsic, InherentData, InherentError> for Runtime {
impl block_builder_api::BlockBuilder<Block, BasicInherentData> for Runtime {
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyResult {
Executive::apply_extrinsic(extrinsic)
}
Expand All @@ -415,22 +269,43 @@ impl_runtime_apis! {
Executive::finalise_block()
}

fn inherent_extrinsics(data: InherentData) -> Vec<UncheckedExtrinsic> {
data.create_inherent_extrinsics()
fn inherent_extrinsics(data: BasicInherentData) -> Vec<<Block as BlockT>::Extrinsic> {
let mut inherent = Vec::new();

inherent.extend(
Timestamp::create_inherent_extrinsics(data.timestamp)
.into_iter()
.map(|v| (v.0, UncheckedExtrinsic::new_unsigned(Call::Timestamp(v.1))))
);

inherent.extend(
Consensus::create_inherent_extrinsics(data.consensus)
.into_iter()
.map(|v| (v.0, UncheckedExtrinsic::new_unsigned(Call::Consensus(v.1))))
);

inherent.as_mut_slice().sort_unstable_by_key(|v| v.0);
inherent.into_iter().map(|v| v.1).collect()
}

fn check_inherents(block: Block, data: InherentData) -> Result<(), InherentError> {
data.check_inherents(block)
fn check_inherents(block: Block, data: BasicInherentData) -> Result<(), CheckInherentError> {
Runtime::check_inherents(block, data)
}

fn random_seed() -> <Block as BlockT>::Hash {
System::random_seed()
}
}

impl TaggedTransactionQueue<Block> for Runtime {
impl runtime_api::TaggedTransactionQueue<Block> for Runtime {
fn validate_transaction(tx: <Block as BlockT>::Extrinsic) -> TransactionValidity {
Executive::validate_transaction(tx)
}
}

impl aura_api::AuraApi<Block> for Runtime {
fn slot_duration() -> u64 {
Aura::slot_duration()
}
}
}
Loading