Skip to content

Commit

Permalink
Feature/consensus (paritytech#8)
Browse files Browse the repository at this point in the history
* add consensus module

* Fix runtime build error

* Add api module

* build all ok
  • Loading branch information
gguoss committed Sep 5, 2018
1 parent a1e7152 commit 82a63db
Show file tree
Hide file tree
Showing 17 changed files with 1,404 additions and 70 deletions.
115 changes: 82 additions & 33 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ exit-future = "0.1"
[workspace]
members = [
"primitives",
"runtime",
"consensus",
"executor",
"rpc",
"runtime",
"pool",
"rpc",
"api",
]
21 changes: 21 additions & 0 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "chainx-api"
version = "0.1.0"
authors = ["Chainpool <http://www.chainx.org>"]

[dependencies]
error-chain = "0.12"
chainx-executor = { path = "../executor" }
chainx-runtime = { path = "../runtime" }
chainx-primitives = { path = "../primitives" }
substrate-codec = { git = "https://github.com/chainx-org/substrate" }
substrate-runtime-io = { git = "https://github.com/chainx-org/substrate" }
substrate-runtime-executive = { git = "https://github.com/chainx-org/substrate" }
substrate-runtime-primitives = { git = "https://github.com/chainx-org/substrate" }
substrate-client = { git = "https://github.com/chainx-org/substrate" }
substrate-primitives = { git = "https://github.com/chainx-org/substrate" }
substrate-executor = { git = "https://github.com/chainx-org/substrate" }
substrate-state-machine = { git = "https://github.com/chainx-org/substrate" }

[dev-dependencies]
substrate-keyring = { git = "https://github.com/chainx-org/substrate" }
115 changes: 115 additions & 0 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2018 chainpool.

extern crate chainx_executor;
extern crate chainx_primitives as primitives;
extern crate chainx_runtime as runtime;
extern crate substrate_codec as codec;
extern crate substrate_runtime_io as runtime_io;
extern crate substrate_client as client;
extern crate substrate_executor as substrate_executor;
extern crate substrate_runtime_executive;
extern crate substrate_primitives;
extern crate substrate_runtime_primitives as runtime_primitives;
extern crate substrate_state_machine as state_machine;

#[macro_use]
extern crate error_chain;

#[cfg(test)]
extern crate substrate_keyring as keyring;


use primitives::{
AccountId, Block, BlockId, Hash, Index, SessionKey, Timestamp,
UncheckedExtrinsic, InherentData,
};
use runtime::Address;

error_chain! {
errors {
/// Unknown runtime code.
UnknownRuntime {
description("Unknown runtime code")
display("Unknown runtime code")
}
/// Unknown block ID.
UnknownBlock(b: String) {
description("Unknown block")
display("Unknown block {}", b)
}
/// Execution error.
Execution(e: String) {
description("Execution error")
display("Execution error: {}", e)
}
/// Some other error.
// TODO: allow to be specified as associated type of ChainXApi
Other(e: Box<::std::error::Error + Send>) {
description("Other error")
display("Other error: {}", e.description())
}
}
}

impl From<client::error::Error> for Error {
fn from(e: client::error::Error) -> Error {
match e {
client::error::Error(client::error::ErrorKind::UnknownBlock(b), _) => Error::from_kind(ErrorKind::UnknownBlock(b)),
client::error::Error(client::error::ErrorKind::Execution(e), _) =>
Error::from_kind(ErrorKind::Execution(format!("{}", e))),
other => Error::from_kind(ErrorKind::Other(Box::new(other) as Box<_>)),
}
}
}

/// Build new blocks.
pub trait BlockBuilder {
/// Push an extrinsic onto the block. Fails if the extrinsic is invalid.
fn push_extrinsic(&mut self, extrinsic: UncheckedExtrinsic) -> Result<()>;

/// Bake the block with provided extrinsics.
fn bake(self) -> Result<Block>;
}

/// Trait encapsulating the ChainX API.
///
/// All calls should fail when the exact runtime is unknown.
pub trait ChainXApi {
/// The block builder for this API type.
type BlockBuilder: BlockBuilder;

/// Get session keys at a given block.
fn session_keys(&self, at: &BlockId) -> Result<Vec<SessionKey>>;

/// Get validators at a given block.
fn validators(&self, at: &BlockId) -> Result<Vec<AccountId>>;

/// Get the value of the randomness beacon at a given block.
fn random_seed(&self, at: &BlockId) -> Result<Hash>;

/// Get the timestamp registered at a block.
fn timestamp(&self, at: &BlockId) -> Result<Timestamp>;

/// Get the nonce (né index) of an account at a block.
fn index(&self, at: &BlockId, account: AccountId) -> Result<Index>;

/// Get the account id of an address at a block.
fn lookup(&self, at: &BlockId, address: Address) -> Result<Option<AccountId>>;

/// Evaluate a block. Returns true if the block is good, false if it is known to be bad,
/// and an error if we can't evaluate for some reason.
fn evaluate_block(&self, at: &BlockId, block: Block) -> Result<bool>;

/// Build a block on top of the given, with inherent extrinsics pre-pushed.
fn build_block(&self, at: &BlockId, inherent_data: InherentData) -> Result<Self::BlockBuilder>;

/// Attempt to produce the (encoded) inherent extrinsics for a block being built upon the given.
/// This may vary by runtime and will fail if a runtime doesn't follow the same API.
fn inherent_extrinsics(&self, at: &BlockId, inherent_data: InherentData) -> Result<Vec<UncheckedExtrinsic>>;
}

/// Mark for all ChainX API implementations, that are making use of state data, stored locally.
pub trait LocalChainXApi: ChainXApi {}

/// Mark for all ChainX API implementations, that are fetching required state data from remote nodes.
pub trait RemoteChainXApi: ChainXApi {}
29 changes: 29 additions & 0 deletions consensus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "chainx-consensus"
version = "0.1.0"
authors = ["Chainpool <http://www.chainx.org>"]

[dependencies]
futures = "0.1.17"
parking_lot = "0.4"
tokio = "0.1.7"
ed25519 = { git = "https://github.com/chainx-org/substrate" }
error-chain = "0.12"
log = "0.3"
exit-future = "0.1"
rhododendron = "0.3"
substrate-bft = { git = "https://github.com/chainx-org/substrate" }
substrate-codec = { git = "https://github.com/chainx-org/substrate" }
substrate-primitives = { git = "https://github.com/chainx-org/substrate" }
substrate-runtime-support = { git = "https://github.com/chainx-org/substrate" }
substrate-client = { git = "https://github.com/chainx-org/substrate" }
substrate-runtime-primitives = { git = "https://github.com/chainx-org/substrate" }
substrate-network = { git = "https://github.com/chainx-org/substrate" }
substrate-extrinsic-pool = { git = "https://github.com/chainx-org/substrate" }
chainx-primitives = {path = "../primitives" }
chainx-runtime = {path = "../runtime" }
chainx-api = {path = "../api" }
chainx-pool = {path = "../pool" }

[dev-dependencies]
substrate-keyring = { git = "https://github.com/chainx-org/substrate" }
41 changes: 41 additions & 0 deletions consensus/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2018 Chainpool.

//! Errors that can occur during the consensus process.

use primitives::AuthorityId;

error_chain! {
links {
ChainXApi(::chainx_api::Error, ::chainx_api::ErrorKind);
Bft(::bft::Error, ::bft::ErrorKind);
}

errors {
InvalidDutyRosterLength(expected: usize, got: usize) {
description("Duty Roster had invalid length"),
display("Invalid duty roster length: expected {}, got {}", expected, got),
}
NotValidator(id: AuthorityId) {
description("Local account ID not a validator at this block."),
display("Local account ID ({:?}) not a validator at this block.", id),
}
PrematureDestruction {
description("Proposer destroyed before finishing proposing or evaluating"),
display("Proposer destroyed before finishing proposing or evaluating"),
}
Timer(e: ::tokio::timer::Error) {
description("Failed to register or resolve async timer."),
display("Timer failed: {}", e),
}
Executor(e: ::futures::future::ExecuteErrorKind) {
description("Unable to dispatch agreement future"),
display("Unable to dispatch agreement future: {:?}", e),
}
}
}

impl From<::bft::InputStreamConcluded> for Error {
fn from(err: ::bft::InputStreamConcluded) -> Self {
::bft::Error::from(err).into()
}
}
Loading

0 comments on commit 82a63db

Please sign in to comment.