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

Commit

Permalink
move node consensus into rhd
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed Oct 17, 2018
1 parent 649811c commit 733e826
Show file tree
Hide file tree
Showing 8 changed files with 612 additions and 653 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

13 changes: 8 additions & 5 deletions core/consensus/rhd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ futures = "0.1.17"
parity-codec = { version = "2.0" }
parity-codec-derive = { version = "2.0" }
substrate-primitives = { path = "../../primitives" }
substrate-consensus-common = { path = "../common" }
substrate-client = { path = "../../client" }
substrate-transaction-pool = { path = "../../transaction-pool" }
srml-support = { path = "../../../srml/support" }
srml-system = { path = "../../../srml/system" }
srml-consensus = { path = "../../../srml/consensus" }
sr-primitives = { path = "../../sr-primitives" }
sr-version = { path = "../../sr-version" }
sr-io = { path = "../../sr-io" }
srml-consensus = { path = "../../../srml/consensus" }
tokio = "0.1.7"
parking_lot = "0.4"
error-chain = "0.12"
log = "0.3"
log = "0.4"
rhododendron = { version = "0.4.0", features = ["codec"] }
serde = { version = "1.0", features = ["derive"] }
substrate-consensus-common = { path = "../common" }
exit-future = "0.1"


[dev-dependencies]
substrate-keyring = { path = "../../keyring" }
Expand All @@ -29,7 +33,6 @@ substrate-executor = { path = "../../executor" }
[features]
default = ["std"]
std = [
"serde/std",
"substrate-primitives/std",
"srml-support/std",
"sr-primitives/std",
Expand Down
32 changes: 30 additions & 2 deletions core/consensus/rhd/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,34 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Error types in the BFT service.
use consensus_common::error::{Error as CommonError, ErrorKind as CommonErrorKind};
//! Error types in the Rhodendron Consensus service.
use consensus::error::{Error as CommonError, ErrorKind as CommonErrorKind};
use primitives::AuthorityId;
use client;

error_chain! {
links {
Client(client::error::Error, client::error::ErrorKind);
Common(CommonError, CommonErrorKind);
}
errors {
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<::rhododendron::InputStreamConcluded> for Error {
Expand All @@ -34,3 +55,10 @@ impl From<CommonErrorKind> for Error {
CommonError::from(e).into()
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@
use super::MAX_TRANSACTIONS_SIZE;

use codec::{Decode, Encode};
use node_runtime::{Block as GenericBlock};
use node_primitives::{Hash, BlockNumber};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, As};

type BlockNumber = u64;

error_chain! {
errors {
BadProposalFormat {
description("Proposal provided not a block."),
display("Proposal provided not a block."),
}
WrongParentHash(expected: Hash, got: Hash) {
WrongParentHash(expected: String, got: String) {
description("Proposal had wrong parent hash."),
display("Proposal had wrong parent hash. Expected {:?}, got {:?}", expected, got),
}
WrongNumber(expected: BlockNumber, got: BlockNumber) {
description("Proposal had wrong number."),
display("Proposal had wrong number. Expected {:?}, got {:?}", expected, got),
display("Proposal had wrong number. Expected {}, got {}", expected, got),
}
ProposalTooLarge(size: usize) {
description("Proposal exceeded the maximum size."),
Expand All @@ -50,20 +49,17 @@ error_chain! {

/// Attempt to evaluate a substrate block as a node block, returning error
/// upon any initial validity checks failing.
pub fn evaluate_initial<Block: BlockT, Hash>(
pub fn evaluate_initial<Block: BlockT>(
proposal: &Block,
parent_hash: &Hash,
parent_hash: &<Block as BlockT>::Hash,
parent_number: <<Block as BlockT>::Header as HeaderT>::Number,
) -> Result<()>
where
Hash: PartialEq<<<GenericBlock as BlockT>::Header as HeaderT>::Hash>,
Hash: Into<self::Hash> + Clone,
{
) -> Result<()> {

let encoded = Encode::encode(proposal);
let proposal = GenericBlock::decode(&mut &encoded[..])
let proposal = Block::decode(&mut &encoded[..])
.ok_or_else(|| ErrorKind::BadProposalFormat)?;

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

Expand All @@ -72,11 +68,14 @@ where
}

if *parent_hash != *proposal.header().parent_hash() {
bail!(ErrorKind::WrongParentHash((*parent_hash).clone().into(), proposal.header.parent_hash));
bail!(ErrorKind::WrongParentHash(
format!("{:?}", *parent_hash),
format!("{:?}", proposal.header().parent_hash())
));
}

if parent_number.as_() + 1 != *proposal.header().number() {
bail!(ErrorKind::WrongNumber(parent_number.as_() + 1, proposal.header.number));
if parent_number.as_() + 1 != proposal.header().number().as_() {
bail!(ErrorKind::WrongNumber(parent_number.as_() + 1, proposal.header().number().as_()));
}

Ok(())
Expand Down
Loading

0 comments on commit 733e826

Please sign in to comment.