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 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a83059a
Completely rework dispatch mechanism into something modular.
gavofyork Mar 13, 2018
82a178d
Council vote tests.
gavofyork Mar 13, 2018
1d28d9a
Fix tests.
gavofyork Mar 13, 2018
69c88c3
whitespace.
gavofyork Mar 13, 2018
b12a708
Fix demo runtime tests.
gavofyork Mar 14, 2018
e48e86d
Merge branch 'gav-demo' into gav-dispatch
gavofyork Mar 14, 2018
8e3cc51
Fix up tests.
gavofyork Mar 14, 2018
53e2fdf
Merge branch 'gav-demo' into gav-dispatch
gavofyork Mar 14, 2018
27ecd6f
Merge branch 'master' into gav-dispatch
gavofyork Mar 14, 2018
5eca74a
Remove dead code.
gavofyork Mar 14, 2018
7cece3b
Merge branch 'master' into gav-dispatch
gavofyork Mar 14, 2018
8c2396d
Timestamp uses new storage API.
gavofyork Mar 14, 2018
6b6c240
Move over system module to new API.
gavofyork Mar 14, 2018
a79dab2
Much nicer storage API, moved over staking module.
gavofyork Mar 15, 2018
1fd6b3e
More refactoring.
gavofyork Mar 15, 2018
51b4a8c
Democracy uses new storage API.
gavofyork Mar 15, 2018
8ada9f7
Council uses new RPC.
gavofyork Mar 16, 2018
c4f5f42
Fix more tests.
gavofyork Mar 16, 2018
d11f5ca
Use match for Id
gavofyork Mar 16, 2018
faa0a44
Use match for Id
gavofyork Mar 16, 2018
7912de1
Make PrivPass better protected.
gavofyork Mar 19, 2018
aac3899
Address other grumbles.
gavofyork Mar 19, 2018
26519c6
Give PrivPass a private member.
gavofyork Mar 19, 2018
9f32ea7
Testing PrivPass.
gavofyork Mar 19, 2018
8966951
Add docs.
gavofyork Mar 19, 2018
91d5e75
Merge branch 'gav-dispatch' into gav-storage-revamp
gavofyork Mar 19, 2018
7ee81e6
Recompile binaries after merge.
gavofyork Mar 19, 2018
33215e4
Merge branch 'master' into gav-storage-revamp
gavofyork Mar 19, 2018
cf9659e
Remove duplicated code.
gavofyork Mar 19, 2018
b8c0832
New binaries.
gavofyork Mar 19, 2018
57a9a79
Docs
gavofyork Mar 19, 2018
59c7a1a
Docs
gavofyork Mar 19, 2018
6f8f456
avoid use of (arguably) confusing terminology.
gavofyork Mar 19, 2018
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
Prev Previous commit
Next Next commit
Merge branch 'gav-demo' into gav-dispatch
  • Loading branch information
gavofyork committed Mar 14, 2018
commit e48e86dcb7f7afcc8594241b17bf8aafbe75164e
13 changes: 13 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 @@ -30,6 +30,7 @@ members = [
"substrate/executor",
"substrate/keyring",
"substrate/network",
"substrate/misbehavior-check",
"substrate/primitives",
"substrate/rpc-servers",
"substrate/rpc",
Expand Down
5 changes: 2 additions & 3 deletions demo/runtime/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

//! Block and header type definitions.

#[cfg(feature = "std")]
use rstd::vec::Vec;
use rstd::prelude::*;
use codec::{Input, Slicable};
use transaction::UncheckedTransaction;

Expand All @@ -38,7 +37,7 @@ pub struct Block {

impl Slicable for Block {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
let (header, transactions) = try_opt!(Slicable::decode(input));
let (header, transactions) = Slicable::decode(input)?;
Some(Block { header, transactions })
}

Expand Down
9 changes: 2 additions & 7 deletions demo/runtime/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@

//! Dispatch system. Just dispatches calls.

use demo_primitives::AccountId;
use runtime::{staking, system, session, democracy, council, council_vote};
use runtime::staking::public::Dispatch as staking_d;
use runtime::staking::privileged::Dispatch as staking_pd;
use runtime::timestamp::public::Dispatch as timestamp_d;

use runtime::{staking, system};
pub use rstd::prelude::Vec;
pub use codec::{Slicable, Input, NonTrivialSlicable};

Expand Down Expand Up @@ -105,7 +100,7 @@ macro_rules! impl_dispatch {
})
}

fn encode(&self) -> Vec<u8> {
fn encode(&self) -> $crate::dispatch::Vec<u8> {
let mut v = $crate::dispatch::Vec::new();
match *self {
$(
Expand Down
4 changes: 0 additions & 4 deletions demo/runtime/src/runtime/council.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,18 +504,14 @@ fn finalise_tally() {
let leaderboard: Vec<(Balance, AccountId)> = storage::take(LEADERBOARD).unwrap_or_default();
let new_expiry = system::block_number() + term_duration();

println!("Finalising tally {} {}, {:?}", system::block_number(), coming, leaderboard);

// return bond to winners.
let candidacy_bond = candidacy_bond();
for &(_, ref w) in leaderboard.iter()
.rev()
.take_while(|&&(b, _)| b != 0)
.take(coming as usize)
{
println!("Refunding by {:?}, {:?}", candidacy_bond, staking::balance(w));
staking::internal::refund(w, candidacy_bond);
println!("Refunded to {:?}", staking::balance(w));
}

// set the new council.
Expand Down
1 change: 0 additions & 1 deletion demo/runtime/src/runtime/council_vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ pub mod internal {
pub fn end_block(now: BlockNumber) {
while let Some((proposal, proposal_hash)) = take_proposal_if_expiring_at(now) {
let tally = take_tally(&proposal_hash);
println!("Executing proposal {:?} {:?}", proposal, tally);
if let &Proposal::Democracy(democracy::privileged::Call::cancel_referendum(ref_index)) = &proposal {
if let (_, 0, 0) = tally {
democracy::internal::cancel_referendum(ref_index);
Expand Down
20 changes: 9 additions & 11 deletions demo/runtime/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@

//! Transaction type.

use rstd::vec::Vec;
use rstd::prelude::*;
use rstd::ops;
use codec::{Input, Slicable, NonTrivialSlicable};
use demo_primitives::{AccountId, TxOrder, SessionKey, Signature};
use codec::{Input, Slicable};
use demo_primitives::{AccountId, TxOrder, Signature};
use dispatch::PubCall;

#[cfg(feature = "std")]
use std::fmt;

use block::Number as BlockNumber;

/// A vetted and verified transaction from the external world.
#[derive(PartialEq, Eq, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
Expand All @@ -42,9 +40,9 @@ pub struct Transaction {
impl Slicable for Transaction {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(Transaction {
signed: try_opt!(Slicable::decode(input)),
nonce: try_opt!(Slicable::decode(input)),
function: try_opt!(Slicable::decode(input)),
signed: Slicable::decode(input)?,
nonce: Slicable::decode(input)?,
function: Slicable::decode(input)?,
})
}

Expand Down Expand Up @@ -77,11 +75,11 @@ impl Slicable for UncheckedTransaction {
// with substrate's generic `Vec<u8>` type. Basically this just means accepting that there
// will be a prefix of u32, which has the total number of bytes following (we don't need
// to use this).
let _length_do_not_remove_me_see_above: u32 = try_opt!(Slicable::decode(input));
let _length_do_not_remove_me_see_above: u32 = Slicable::decode(input)?;

Some(UncheckedTransaction {
transaction: try_opt!(Slicable::decode(input)),
signature: try_opt!(Slicable::decode(input)),
transaction: Slicable::decode(input)?,
signature: Slicable::decode(input)?,
})
}

Expand Down
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions polkadot/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ parking_lot = "0.4"
tokio-timer = "0.1.2"
ed25519 = { path = "../../substrate/ed25519" }
error-chain = "0.11"
log = "0.4"
polkadot-api = { path = "../api" }
polkadot-collator = { path = "../collator" }
polkadot-primitives = { path = "../primitives" }
Expand Down
78 changes: 71 additions & 7 deletions polkadot/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ extern crate substrate_primitives as primitives;
#[macro_use]
extern crate error_chain;

#[macro_use]
extern crate log;

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

Expand All @@ -55,9 +58,9 @@ 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 primitives::block::{Block as SubstrateBlock, Header as SubstrateHeader, HeaderHash, Id as BlockId};
use primitives::block::{Block as SubstrateBlock, Header as SubstrateHeader, HeaderHash, Id as BlockId, Number as BlockNumber};
use primitives::AuthorityId;
use transaction_pool::TransactionPool;
use transaction_pool::{Ready, TransactionPool};

use futures::prelude::*;
use futures::future;
Expand Down Expand Up @@ -477,17 +480,19 @@ impl<C: PolkadotApi, N: Network> bft::ProposerFactory for ProposerFactory<C, N>
let duty_roster = self.client.duty_roster(&checked_id)?;

let group_info = make_group_info(duty_roster, authorities)?;
let table = Arc::new(SharedTable::new(group_info, sign_with, parent_hash));
let table = Arc::new(SharedTable::new(group_info, sign_with.clone(), parent_hash));
let router = self.network.table_router(table.clone());

// TODO [PoC-2]: kick off collation process.
Ok(Proposer {
parent_hash,
parent_number: parent_header.number,
parent_id: checked_id,
_table: table,
_router: router,
local_key: sign_with,
client: self.client.clone(),
transaction_pool: self.transaction_pool.clone(),
_table: table,
_router: router,
})
}
}
Expand All @@ -503,8 +508,10 @@ fn current_timestamp() -> Timestamp {
/// The Polkadot proposer logic.
pub struct Proposer<C: PolkadotApi, R> {
parent_hash: HeaderHash,
parent_number: BlockNumber,
parent_id: C::CheckedBlockId,
client: Arc<C>,
local_key: Arc<ed25519::Pair>,
transaction_pool: Arc<Mutex<TransactionPool>>,
_table: Arc<SharedTable>,
_router: R,
Expand All @@ -516,8 +523,6 @@ impl<C: PolkadotApi, R: TableRouter> bft::Proposer for Proposer<C, R> {
type Evaluate = Result<bool, Error>;

fn propose(&self) -> Result<SubstrateBlock, Error> {
use transaction_pool::Ready;

// TODO: handle case when current timestamp behind that in state.
let mut block_builder = self.client.build_block(
&self.parent_id,
Expand Down Expand Up @@ -565,6 +570,65 @@ impl<C: PolkadotApi, R: TableRouter> bft::Proposer for Proposer<C, R> {
fn evaluate(&self, proposal: &SubstrateBlock) -> Result<bool, Error> {
evaluate_proposal(proposal, &*self.client, current_timestamp(), &self.parent_hash, &self.parent_id)
}

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};

let local_id = self.local_key.public().0;
let mut pool = self.transaction_pool.lock();
let mut next_nonce = {
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)
.last()
.map(|tx| Ok(tx.as_transaction().transaction.nonce))
.unwrap_or_else(|| self.client.nonce(&self.parent_id, local_id));

match cur_nonce {
Ok(cur_nonce) => cur_nonce + 1,
Err(e) => {
warn!(target: "consensus", "Error computing next transaction nonce: {}", e);
return;
}
}
};

for (target, misbehavior) in misbehavior {
let report = MisbehaviorReport {
parent_hash: self.parent_hash,
parent_number: self.parent_number,
target,
misbehavior: match misbehavior {
GenericMisbehavior::ProposeOutOfTurn(_, _, _) => continue,
GenericMisbehavior::DoublePropose(_, _, _) => continue,
GenericMisbehavior::DoublePrepare(round, (h1, s1), (h2, s2))
=> MisbehaviorKind::BftDoublePrepare(round as u32, (h1, s1.signature), (h2, s2.signature)),
GenericMisbehavior::DoubleCommit(round, (h1, s1), (h2, s2))
=> MisbehaviorKind::BftDoubleCommit(round as u32, (h1, s1.signature), (h2, s2.signature)),
}
};

let tx = Transaction {
signed: local_id,
nonce: next_nonce,
function: Function::ReportMisbehavior(report),
};

next_nonce += 1;

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

pool.import(tx).expect("locally signed transaction is valid; qed");
}
}
}

fn evaluate_proposal<C: PolkadotApi>(
Expand Down
2 changes: 1 addition & 1 deletion polkadot/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ mod tests {
construct_block(
2,
block1().1,
hex!("c8776c92e8012bf6b3f206448eda3f00bca26d77f220f4714c81cbc92a30e1e2").into(),
hex!("5604fe023cd6effd93aec9b4a008398abdd32afb3fec988a19aa853ab0424a7c").into(),
200_000,
vec![
Transaction {
Expand Down
6 changes: 6 additions & 0 deletions polkadot/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ pub type Signature = primitives::hash::H512;

/// A timestamp: seconds since the unix epoch.
pub type Timestamp = u64;

/// The balance of an account.
pub type Balance = u64;

/// The amount of bonding period left in an account. Measured in eras.
pub type Bondage = u64;
23 changes: 20 additions & 3 deletions polkadot/primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use rstd::vec::Vec;
use codec::{Input, Slicable};
use primitives::bft::MisbehaviorReport;
use ::Signature;

#[cfg(feature = "std")]
Expand Down Expand Up @@ -168,6 +169,8 @@ enum FunctionId {
StakingUnstake = 0x21,
/// Staking subsystem: transfer stake.
StakingTransfer = 0x22,
/// Report misbehavior.
StakingReportMisbehavior = 0x23,
/// Make a proposal for the governance system.
GovernancePropose = 0x30,
/// Approve a proposal for the governance system.
Expand All @@ -178,9 +181,16 @@ impl FunctionId {
/// Derive `Some` value from a `u8`, or `None` if it's invalid.
fn from_u8(value: u8) -> Option<FunctionId> {
use self::*;
let functions = [FunctionId::StakingStake, FunctionId::StakingUnstake,
FunctionId::StakingTransfer, FunctionId::SessionSetKey, FunctionId::TimestampSet,
FunctionId::GovernancePropose, FunctionId::GovernanceApprove];
let functions = [
FunctionId::StakingStake,
FunctionId::StakingUnstake,
FunctionId::StakingTransfer,
FunctionId::StakingReportMisbehavior,
FunctionId::SessionSetKey,
FunctionId::TimestampSet,
FunctionId::GovernancePropose,
FunctionId::GovernanceApprove,
];
functions.iter().map(|&f| f).find(|&f| value == f as u8)
}
}
Expand Down Expand Up @@ -222,6 +232,8 @@ pub enum Function {
StakingUnstake,
/// Staking subsystem: transfer stake.
StakingTransfer(::AccountId, u64),
/// Staking subsystem: report misbehavior of a validator.
ReportMisbehavior(MisbehaviorReport),
/// Make a proposal for the governance system.
GovernancePropose(Proposal),
/// Approve a proposal for the governance system.
Expand Down Expand Up @@ -269,6 +281,7 @@ impl Slicable for Function {

Function::StakingTransfer(to, amount)
}
FunctionId::StakingReportMisbehavior => Function::ReportMisbehavior(MisbehaviorReport::decode(input)?),
FunctionId::GovernancePropose =>
Function::GovernancePropose(try_opt!(Slicable::decode(input))),
FunctionId::GovernanceApprove =>
Expand All @@ -293,6 +306,10 @@ impl Slicable for Function {
Function::StakingUnstake => {
(FunctionId::StakingUnstake as u8).using_encoded(|s| v.extend(s));
}
Function::ReportMisbehavior(ref report) => {
(FunctionId::StakingReportMisbehavior as u8).using_encoded(|s| v.extend(s));
report.using_encoded(|s| v.extend(s));
}
Function::StakingTransfer(ref to, ref amount) => {
(FunctionId::StakingTransfer as u8).using_encoded(|s| v.extend(s));
to.using_encoded(|s| v.extend(s));
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ substrate-runtime-std = { path = "../../substrate/runtime-std" }
substrate-runtime-io = { path = "../../substrate/runtime-io" }
substrate-runtime-support = { path = "../../substrate/runtime-support" }
substrate-primitives = { path = "../../substrate/primitives" }
substrate-misbehavior-check = { path = "../../substrate/misbehavior-check" }
polkadot-primitives = { path = "../primitives" }

[dev-dependencies]
Expand All @@ -25,6 +26,7 @@ std = [
"substrate-runtime-io/std",
"substrate-runtime-support/std",
"substrate-primitives/std",
"substrate-misbehavior-check/std",
"polkadot-primitives/std",
"log"
]
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.