Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Builds. Yey.
Browse files Browse the repository at this point in the history
  • Loading branch information
gavofyork committed Apr 4, 2018
1 parent 971ad01 commit 9fb03ea
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 945 deletions.
6 changes: 5 additions & 1 deletion Cargo.lock

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

31 changes: 16 additions & 15 deletions demo/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,6 @@ impl HasPublicAux for Concrete {
type PublicAux = AccountId;
}

impl timestamp::Trait for Concrete {
type Value = u64;
}

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

impl consensus::Trait for Concrete {
type SessionKey = SessionKey;
}

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

impl system::Trait for Concrete {
type Index = Index;
type BlockNumber = BlockNumber;
Expand All @@ -81,8 +67,22 @@ impl system::Trait for Concrete {
/// System module for this concrete runtime.
pub type System = system::Module<Concrete>;

impl session::Trait for Concrete {
impl consensus::Trait for Concrete {
type PublicAux = <Self as HasPublicAux>::PublicAux;
type SessionKey = SessionKey;
}

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

impl timestamp::Trait for Concrete {
type Value = u64;
}

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

impl session::Trait for Concrete {
type ConvertAccountIdToSessionKey = Identity;
}

Expand Down Expand Up @@ -113,6 +113,7 @@ pub type CouncilVoting = council::voting::Module<Concrete>;

impl_outer_dispatch! {
pub enum Call where aux: <Concrete as HasPublicAux>::PublicAux {
Consensus = 0,
Session = 1,
Staking = 2,
Timestamp = 3,
Expand Down
1 change: 1 addition & 0 deletions polkadot/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ log = "0.4"
polkadot-api = { path = "../api" }
polkadot-collator = { path = "../collator" }
polkadot-primitives = { path = "../primitives" }
polkadot-runtime = { path = "../runtime" }
polkadot-statement-table = { path = "../statement-table" }
polkadot-transaction-pool = { path = "../transaction-pool" }
substrate-bft = { path = "../../substrate/bft" }
Expand Down
55 changes: 26 additions & 29 deletions polkadot/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern crate polkadot_collator as collator;
extern crate polkadot_statement_table as table;
extern crate polkadot_primitives;
extern crate polkadot_transaction_pool as transaction_pool;
extern crate polkadot_runtime;
extern crate substrate_bft as bft;
extern crate substrate_codec as codec;
extern crate substrate_primitives as primitives;
Expand All @@ -62,11 +63,11 @@ use table::generic::Statement as GenericStatement;
use runtime_support::Hashable;
use polkadot_api::{PolkadotApi, BlockBuilder};
use polkadot_primitives::{Hash, Timestamp};
use polkadot_primitives::block::Block as PolkadotBlock;
use polkadot_primitives::parachain::{Id as ParaId, DutyRoster, BlockData, Extrinsic, CandidateReceipt};
use polkadot_runtime::Block as PolkadotGenericBlock;
use primitives::block::{Block as SubstrateBlock, Header as SubstrateHeader, HeaderHash, Id as BlockId, Number as BlockNumber};
use primitives::AuthorityId;
use transaction_pool::{Ready, TransactionPool};
use transaction_pool::{Ready, TransactionPool, PolkadotBlock};

use futures::prelude::*;
use futures::future;
Expand Down Expand Up @@ -152,7 +153,7 @@ impl TableContext {
}

fn sign_statement(&self, statement: table::Statement) -> table::SignedStatement {
let signature = sign_table_statement(&statement, &self.key, &self.parent_hash);
let signature = sign_table_statement(&statement, &self.key, &self.parent_hash).into();
let local_id = self.key.public().0;

table::SignedStatement {
Expand Down Expand Up @@ -552,7 +553,7 @@ impl<C: PolkadotApi, R: TableRouter> bft::Proposer for Proposer<C, R> {

if pending_size + pending.encoded_size() >= MAX_TRANSACTIONS_SIZE { break }

match block_builder.push_transaction(pending.as_transaction().clone()) {
match block_builder.push_extrinsic(pending.as_transaction().clone()) {
Ok(()) => {
pending_size += pending.encoded_size();
}
Expand Down Expand Up @@ -582,23 +583,23 @@ impl<C: PolkadotApi, R: TableRouter> bft::Proposer for Proposer<C, R> {
fn import_misbehavior(&self, misbehavior: Vec<(AuthorityId, bft::Misbehavior)>) {
use bft::generic::Misbehavior as GenericMisbehavior;
use primitives::bft::{MisbehaviorKind, MisbehaviorReport};
use polkadot_primitives::transaction::{Function, Transaction, UncheckedTransaction};
use polkadot_runtime::{Call, Extrinsic, UncheckedExtrinsic, ConsensusCall};

let local_id = self.local_key.public().0;
let mut pool = self.transaction_pool.lock();
let mut next_nonce = {
let mut next_index = {
let readiness_evaluator = Ready::create(self.parent_id.clone(), &*self.client);

let cur_nonce = pool.pending(readiness_evaluator)
.filter(|tx| tx.as_transaction().transaction.signed == local_id)
let cur_index = pool.pending(readiness_evaluator)
.filter(|tx| tx.as_ref().as_ref().signed == local_id)
.last()
.map(|tx| Ok(tx.as_transaction().transaction.nonce))
.unwrap_or_else(|| self.client.nonce(&self.parent_id, local_id));
.map(|tx| Ok(tx.as_ref().as_ref().index))
.unwrap_or_else(|| self.client.index(&self.parent_id, local_id));

match cur_nonce {
Ok(cur_nonce) => cur_nonce + 1,
match cur_index {
Ok(cur_index) => cur_index + 1,
Err(e) => {
warn!(target: "consensus", "Error computing next transaction nonce: {}", e);
warn!(target: "consensus", "Error computing next transaction index: {}", e);
return;
}
}
Expand All @@ -618,23 +619,18 @@ impl<C: PolkadotApi, R: TableRouter> bft::Proposer for Proposer<C, R> {
=> MisbehaviorKind::BftDoubleCommit(round as u32, (h1, s1.signature), (h2, s2.signature)),
}
};

let tx = Transaction {
let extrinsic = Extrinsic {
signed: local_id,
nonce: next_nonce,
function: Function::ReportMisbehavior(report),
index: next_index,
function: Call::Consensus(ConsensusCall::report_misbehavior(report)),
};

next_nonce += 1;
next_index += 1;

let message = tx.encode();
let signature = self.local_key.sign(&message);
let tx = UncheckedTransaction {
transaction: tx,
signature,
};
let signature = self.local_key.sign(&extrinsic.encode()).into();
let uxt = UncheckedExtrinsic { extrinsic, signature };

pool.import(tx).expect("locally signed transaction is valid; qed");
pool.import(uxt).expect("locally signed extrinsic is valid; qed");
}
}
}
Expand All @@ -649,10 +645,11 @@ fn evaluate_proposal<C: PolkadotApi>(
const MAX_TIMESTAMP_DRIFT: Timestamp = 4;

let encoded = Slicable::encode(proposal);
let proposal = PolkadotBlock::decode(&mut &encoded[..])
let proposal = PolkadotGenericBlock::decode(&mut &encoded[..])
.and_then(|b| PolkadotBlock::from(b).ok())
.ok_or_else(|| ErrorKind::ProposalNotForPolkadot)?;

let transactions_size = proposal.body.transactions.iter().fold(0, |a, tx| {
let transactions_size = proposal.extrinsics.iter().fold(0, |a, tx| {
a + Slicable::encode(tx).len()
});

Expand All @@ -668,14 +665,14 @@ fn evaluate_proposal<C: PolkadotApi>(
// a) we assume the parent is valid.
// b) the runtime checks that `proposal.parent_hash` == `block_hash(proposal.number - 1)`

let block_timestamp = proposal.body.timestamp;
let block_timestamp = proposal.timestamp();

// TODO: just defer using `tokio_timer` to delay prepare vote.
if block_timestamp > now + MAX_TIMESTAMP_DRIFT {
bail!(ErrorKind::TimestampInFuture)
}

// execute the block.
client.evaluate_block(parent_id, proposal)?;
client.evaluate_block(parent_id, proposal.into())?;
Ok(true)
}
Loading

0 comments on commit 9fb03ea

Please sign in to comment.