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
25 commits
Select commit Hold shift + click to select a range
482a074
reshuffle consensus libraries
rphmeier Feb 8, 2018
917b092
polkadot-useful type definitions for statement table
rphmeier Feb 8, 2018
8e2fd3c
begin BftService
rphmeier Feb 10, 2018
776cf13
Merge branch 'master' into rh-split-bft-table
rphmeier Feb 10, 2018
6abfed4
primary selection logic
rphmeier Feb 12, 2018
fc18524
bft service implementation without I/O
rphmeier Feb 12, 2018
017fd51
extract out `BlockImport` trait
rphmeier Feb 12, 2018
25990ee
Merge branch 'master' into rh-split-bft-table
rphmeier Feb 12, 2018
c33c3ff
allow bft primitives to compile on wasm
rphmeier Feb 12, 2018
acab9a3
Block builder (substrate)
gavofyork Feb 12, 2018
1830fa7
take polkadot-consensus down to the core.
rphmeier Feb 12, 2018
767a9d9
test for preemption
rphmeier Feb 12, 2018
7fc4b4d
fix test build
rphmeier Feb 12, 2018
9acd3f9
Fix wasm build
gavofyork Feb 12, 2018
ca5900f
Bulid on any block
gavofyork Feb 13, 2018
d11cfe1
Test for block builder.
gavofyork Feb 13, 2018
b973ccc
Block import tests for client.
gavofyork Feb 13, 2018
ec61865
Tidy ups
gavofyork Feb 13, 2018
23638cd
clean up block builder instantiation
rphmeier Feb 15, 2018
dda6d24
Merge branch 'rh-split-bft-table' into rh-justification-verification
rphmeier Feb 15, 2018
340ce39
justification verification logic
rphmeier Feb 15, 2018
170b0d1
JustifiedHeader and import
rphmeier Feb 15, 2018
6a1a851
Propert block generation for tests
arkpar Feb 15, 2018
a1247bd
Fixed rpc tests
arkpar Feb 15, 2018
673fc2c
Merge branch 'master' into rh-justification-verification
rphmeier Feb 15, 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
test for preemption
  • Loading branch information
rphmeier committed Feb 12, 2018
commit 767a9d95508e2e159e1149e922115d2d7f47f40f
3 changes: 3 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions substrate/bft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ ed25519 = { path = "../ed25519" }
tokio-timer = "0.1.2"
parking_lot = "0.4"
error-chain = "0.11"

[dev-dependencies]
substrate-keyring = { path = "../keyring" }
substrate-executor = { path = "../executor" }
tokio-core = "0.1.12"
119 changes: 110 additions & 9 deletions substrate/bft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use primitives::block::{Block, Header, HeaderHash};
use primitives::AuthorityId;
use state_machine::CodeExecutor;

use futures::{stream, task, Async, Sink, Future};
use futures::{stream, task, Async, Sink, Future, IntoFuture};
use futures::future::Executor;
use futures::sync::oneshot;
use tokio_timer::Timer;
Expand Down Expand Up @@ -83,7 +83,7 @@ pub type Communication = generic::Communication<Block, HeaderHash, AuthorityId,
/// This will encapsulate creation and evaluation of proposals at a specific
/// block.
pub trait Proposer: Sized {
type CreateProposal: Future<Item=Block,Error=Error>;
type CreateProposal: IntoFuture<Item=Block,Error=Error>;

/// Initialize the proposal logic on top of a specific header.
// TODO: provide state context explicitly?
Expand Down Expand Up @@ -147,15 +147,15 @@ impl<P: Proposer> generic::Context for BftInstance<P> {
type Digest = HeaderHash;
type Signature = Signature;
type Candidate = Block;
type RoundTimeout = Box<Future<Item=(),Error=Error>>;
type CreateProposal = P::CreateProposal;
type RoundTimeout = Box<Future<Item=(),Error=Error> + Send>;
type CreateProposal = <P::CreateProposal as IntoFuture>::Future;

fn local_id(&self) -> AuthorityId {
self.key.public().0
}

fn proposal(&self) -> P::CreateProposal {
self.proposer.propose()
fn proposal(&self) -> Self::CreateProposal {
self.proposer.propose().into_future()
}

fn candidate_digest(&self, proposal: &Block) -> HeaderHash {
Expand Down Expand Up @@ -319,7 +319,6 @@ impl<P, E, I> BftService<P, E, I>
/// This will begin the consensus process to build a block on top of it.
/// If the executor fails to run the future, an error will be returned.
pub fn build_upon(&self, header: &Header) -> Result<(), Error> {
let parent_hash = header.parent_hash.clone();
let hash = header.hash();
let mut _preempted_consensus = None;

Expand All @@ -332,7 +331,7 @@ impl<P, E, I> BftService<P, E, I>

let bft_instance = BftInstance {
proposer,
parent_hash,
parent_hash: hash,
round_timeout_multiplier: self.round_timeout_multiplier,
timer: self.timer.clone(),
key: self.key.clone(),
Expand Down Expand Up @@ -364,9 +363,111 @@ impl<P, E, I> BftService<P, E, I>
cancel,
});

_preempted_consensus = live.remove(&parent_hash);
// cancel any agreements attempted to build upon this block's parent
// as clearly agreement has already been reached.
_preempted_consensus = live.remove(&header.parent_hash);
}

Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
use primitives::block;
use self::tokio_core::reactor::{Core, Handle};
use self::keyring::Keyring;

extern crate substrate_keyring as keyring;
extern crate tokio_core;

struct FakeClient {
authorities: Vec<AuthorityId>,
imported_heights: Mutex<HashSet<block::Number>>
}

impl BlockImport for FakeClient {
fn import_block(&self, block: Block, _justification: Justification) {
assert!(self.imported_heights.lock().insert(block.header.number))
}
}

impl Authorities for FakeClient {
fn authorities(&self, _at: &BlockId) -> Result<Vec<AuthorityId>, Error> {
Ok(self.authorities.clone())
}
}

struct DummyProposer(block::Number);

impl Proposer for DummyProposer {
type CreateProposal = Result<Block, Error>;

fn init(parent_header: &Header, _sign_with: Arc<ed25519::Pair>) -> Self {
DummyProposer(parent_header.number + 1)
}

fn propose(&self) -> Result<Block, Error> {
Ok(Block {
header: Header::from_block_number(self.0),
transactions: Default::default()
})
}

fn evaluate(&self, proposal: &Block) -> bool {
proposal.header.number == self.0
}
}

fn make_service(client: FakeClient, handle: Handle)
-> BftService<DummyProposer, Handle, FakeClient>
{
BftService {
client: Arc::new(client),
executor: handle,
live_agreements: Mutex::new(HashMap::new()),
timer: Timer::default(),
round_timeout_multiplier: 4,
key: Arc::new(Keyring::One.into()),
_marker: Default::default(),
}
}

#[test]
fn future_gets_preempted() {
let client = FakeClient {
authorities: vec![
Keyring::One.to_raw_public(),
Keyring::Two.to_raw_public(),
Keyring::Alice.to_raw_public(),
Keyring::Eve.to_raw_public(),
],
imported_heights: Mutex::new(HashSet::new()),
};

let mut core = Core::new().unwrap();

let service = make_service(client, core.handle());

let first = Header::from_block_number(2);
let first_hash = first.hash();

let mut second = Header::from_block_number(3);
second.parent_hash = first_hash;
let second_hash = second.hash();

service.build_upon(&first).unwrap();
assert!(service.live_agreements.lock().contains_key(&first_hash));

// turn the core so the future gets polled and sends its task to the
// service. otherwise it deadlocks.
core.turn(Some(::std::time::Duration::from_millis(100)));
service.build_upon(&second).unwrap();
assert!(!service.live_agreements.lock().contains_key(&first_hash));
assert!(service.live_agreements.lock().contains_key(&second_hash));

core.turn(Some(::std::time::Duration::from_millis(100)));
}
}