fix error propagation
This commit is contained in:
parent
1cb3c164da
commit
77f06be7fb
@ -1,39 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "TestBFT",
|
|
||||||
"engine": {
|
|
||||||
"BFT": {
|
|
||||||
"params": {
|
|
||||||
"gasLimitBoundDivisor": "0x0400",
|
|
||||||
"durationLimit": "0x0d",
|
|
||||||
"validators" : ["0x9cce34f7ab185c7aba1b7c8140d620b4bda941d6"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"params": {
|
|
||||||
"accountStartNonce": "0x0100000",
|
|
||||||
"maximumExtraDataSize": "0x20",
|
|
||||||
"minGasLimit": "0x1388",
|
|
||||||
"networkID" : "0x69"
|
|
||||||
},
|
|
||||||
"genesis": {
|
|
||||||
"seal": {
|
|
||||||
"generic": {
|
|
||||||
"fields": 1,
|
|
||||||
"rlp": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"difficulty": "0x20000",
|
|
||||||
"author": "0x0000000000000000000000000000000000000000",
|
|
||||||
"timestamp": "0x00",
|
|
||||||
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
||||||
"extraData": "0x",
|
|
||||||
"gasLimit": "0x2fefd8"
|
|
||||||
},
|
|
||||||
"accounts": {
|
|
||||||
"0000000000000000000000000000000000000001": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } },
|
|
||||||
"0000000000000000000000000000000000000002": { "balance": "1", "nonce": "1048576", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } },
|
|
||||||
"0000000000000000000000000000000000000003": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } },
|
|
||||||
"0000000000000000000000000000000000000004": { "balance": "1", "nonce": "1048576", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } },
|
|
||||||
"9cce34f7ab185c7aba1b7c8140d620b4bda941d6": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "1048576" }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1024,16 +1024,18 @@ impl BlockChainClient for Client {
|
|||||||
|
|
||||||
fn queue_infinity_message(&self, message: Bytes) {
|
fn queue_infinity_message(&self, message: Bytes) {
|
||||||
let full_rlp = UntrustedRlp::new(&message);
|
let full_rlp = UntrustedRlp::new(&message);
|
||||||
let signature = full_rlp.val_at(0).unwrap_or_else(|| return);
|
if let Ok(signature) = full_rlp.val_at(0) {
|
||||||
let message: Vec<_> = full_rlp.val_at(1).unwrap_or_else(|| return);
|
if let Ok(message) = full_rlp.val_at::<Vec<_>>(1) {
|
||||||
let message_rlp = UntrustedRlp::new(&message);
|
let message_rlp = UntrustedRlp::new(&message);
|
||||||
let pub_key = recover(&signature, &message.sha3()).unwrap_or_else(|| return);
|
if let Ok(pub_key) = recover(&signature, &message.sha3()) {
|
||||||
if let Some(new_message) = self.engine.handle_message(pub_key.sha3().into(), message_rlp)
|
if let Ok(new_message) = self.engine.handle_message(pub_key.sha3().into(), message_rlp) {
|
||||||
{
|
|
||||||
self.notify(|notify| notify.broadcast(&new_message));
|
self.notify(|notify| notify.broadcast(&new_message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl MiningBlockChainClient for Client {
|
impl MiningBlockChainClient for Client {
|
||||||
fn prepare_open_block(&self, author: Address, gas_range_target: (U256, U256), extra_data: Bytes) -> OpenBlock {
|
fn prepare_open_block(&self, author: Address, gas_range_target: (U256, U256), extra_data: Bytes) -> OpenBlock {
|
||||||
|
@ -38,7 +38,7 @@ use spec::Spec;
|
|||||||
use block_queue::BlockQueueInfo;
|
use block_queue::BlockQueueInfo;
|
||||||
use block::{OpenBlock, SealedBlock};
|
use block::{OpenBlock, SealedBlock};
|
||||||
use executive::Executed;
|
use executive::Executed;
|
||||||
use error::CallError;
|
use error::{Error, CallError};
|
||||||
use trace::LocalizedTrace;
|
use trace::LocalizedTrace;
|
||||||
|
|
||||||
/// Test client.
|
/// Test client.
|
||||||
|
@ -183,7 +183,7 @@ pub trait BlockChainClient : Sync + Send {
|
|||||||
fn queue_transactions(&self, transactions: Vec<Bytes>);
|
fn queue_transactions(&self, transactions: Vec<Bytes>);
|
||||||
|
|
||||||
/// Queue packet
|
/// Queue packet
|
||||||
fn queue_infinity_message(&self, packet: Bytes);
|
fn queue_infinity_message(&self, message: Bytes);
|
||||||
|
|
||||||
/// list all transactions
|
/// list all transactions
|
||||||
fn pending_transactions(&self) -> Vec<SignedTransaction>;
|
fn pending_transactions(&self) -> Vec<SignedTransaction>;
|
||||||
|
@ -27,8 +27,8 @@ pub use self::null_engine::NullEngine;
|
|||||||
pub use self::instant_seal::InstantSeal;
|
pub use self::instant_seal::InstantSeal;
|
||||||
pub use self::basic_authority::BasicAuthority;
|
pub use self::basic_authority::BasicAuthority;
|
||||||
pub use self::tendermint::Tendermint;
|
pub use self::tendermint::Tendermint;
|
||||||
pub use self::signed_vote::{SignedVote, VoteError};
|
pub use self::signed_vote::SignedVote;
|
||||||
pub use self::propose_collect::{ProposeCollect};
|
pub use self::propose_collect::ProposeCollect;
|
||||||
|
|
||||||
use common::{HashMap, SemanticVersion, Header, EnvInfo, Address, Builtin, BTreeMap, U256, Bytes, SignedTransaction, Error, UntrustedRlp};
|
use common::{HashMap, SemanticVersion, Header, EnvInfo, Address, Builtin, BTreeMap, U256, Bytes, SignedTransaction, Error, UntrustedRlp};
|
||||||
use account_provider::AccountProvider;
|
use account_provider::AccountProvider;
|
||||||
@ -36,6 +36,19 @@ use block::ExecutedBlock;
|
|||||||
use spec::CommonParams;
|
use spec::CommonParams;
|
||||||
use evm::Schedule;
|
use evm::Schedule;
|
||||||
|
|
||||||
|
/// Voting errors.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum EngineError {
|
||||||
|
/// Voter is not in the voters set.
|
||||||
|
UnauthorisedVoter,
|
||||||
|
/// Message pertaining incorrect consensus step.
|
||||||
|
WrongStep,
|
||||||
|
/// Message pertaining unknown consensus step.
|
||||||
|
UnknownStep,
|
||||||
|
/// Message was not expected.
|
||||||
|
UnexpectedMessage
|
||||||
|
}
|
||||||
|
|
||||||
/// A consensus mechanism for the chain. Generally either proof-of-work or proof-of-stake-based.
|
/// A consensus mechanism for the chain. Generally either proof-of-work or proof-of-stake-based.
|
||||||
/// Provides hooks into each of the major parts of block import.
|
/// Provides hooks into each of the major parts of block import.
|
||||||
pub trait Engine : Sync + Send {
|
pub trait Engine : Sync + Send {
|
||||||
@ -121,7 +134,7 @@ pub trait Engine : Sync + Send {
|
|||||||
|
|
||||||
/// Handle any potential consensus messages;
|
/// Handle any potential consensus messages;
|
||||||
/// updating consensus state and potentially issuing a new one.
|
/// updating consensus state and potentially issuing a new one.
|
||||||
fn handle_message(&self, sender: Address, message: UntrustedRlp) -> Option<Bytes> { None }
|
fn handle_message(&self, sender: Address, message: UntrustedRlp) -> Result<Bytes, Error> { Err(EngineError::UnexpectedMessage.into()) }
|
||||||
|
|
||||||
// TODO: builtin contract routing - to do this properly, it will require removing the built-in configuration-reading logic
|
// TODO: builtin contract routing - to do this properly, it will require removing the built-in configuration-reading logic
|
||||||
// from Spec into here and removing the Spec::builtins field.
|
// from Spec into here and removing the Spec::builtins field.
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use common::{HashSet, RwLock, H256, Signature, Address, Error, ec, Hashable};
|
use common::{HashSet, RwLock, H256, Signature, Address, Error, ec, Hashable};
|
||||||
use engines::VoteError;
|
use super::EngineError;
|
||||||
|
|
||||||
/// Collect votes on a hash.
|
/// Collect votes on a hash.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -59,7 +59,7 @@ impl ProposeCollect {
|
|||||||
fn can_vote(&self, signature: &Signature) -> Result<(), Error> {
|
fn can_vote(&self, signature: &Signature) -> Result<(), Error> {
|
||||||
let signer = Address::from(try!(ec::recover(&signature, &self.hash)).sha3());
|
let signer = Address::from(try!(ec::recover(&signature, &self.hash)).sha3());
|
||||||
match self.voters.contains(&signer) {
|
match self.voters.contains(&signer) {
|
||||||
false => try!(Err(VoteError::UnauthorisedVoter)),
|
false => try!(Err(EngineError::UnauthorisedVoter)),
|
||||||
true => Ok(()),
|
true => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
//! Voting on hashes, where each vote has to come from a set of public keys.
|
//! Voting on hashes, where each vote has to come from a set of public keys.
|
||||||
|
|
||||||
|
use super::EngineError;
|
||||||
use common::{HashSet, HashMap, RwLock, H256, Signature, Address, Error, ec, Hashable};
|
use common::{HashSet, HashMap, RwLock, H256, Signature, Address, Error, ec, Hashable};
|
||||||
|
|
||||||
/// Signed voting on hashes.
|
/// Signed voting on hashes.
|
||||||
@ -33,13 +34,6 @@ pub struct SignedVote {
|
|||||||
winner: RwLock<Option<H256>>
|
winner: RwLock<Option<H256>>
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Voting errors.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum VoteError {
|
|
||||||
/// Voter is not in the voters set.
|
|
||||||
UnauthorisedVoter
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SignedVote {
|
impl SignedVote {
|
||||||
/// Create a new instance of BFT engine
|
/// Create a new instance of BFT engine
|
||||||
pub fn new(voters: HashSet<Address>, threshold: usize) -> Self {
|
pub fn new(voters: HashSet<Address>, threshold: usize) -> Self {
|
||||||
@ -71,7 +65,7 @@ impl SignedVote {
|
|||||||
fn can_vote(&self, bare_hash: &H256, signature: &Signature) -> Result<(), Error> {
|
fn can_vote(&self, bare_hash: &H256, signature: &Signature) -> Result<(), Error> {
|
||||||
let signer = Address::from(try!(ec::recover(&signature, bare_hash)).sha3());
|
let signer = Address::from(try!(ec::recover(&signature, bare_hash)).sha3());
|
||||||
match self.voters.contains(&signer) {
|
match self.voters.contains(&signer) {
|
||||||
false => try!(Err(VoteError::UnauthorisedVoter)),
|
false => try!(Err(EngineError::UnauthorisedVoter)),
|
||||||
true => Ok(()),
|
true => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ use common::*;
|
|||||||
use account_provider::AccountProvider;
|
use account_provider::AccountProvider;
|
||||||
use block::*;
|
use block::*;
|
||||||
use spec::CommonParams;
|
use spec::CommonParams;
|
||||||
use engines::{Engine, ProposeCollect};
|
use engines::{Engine, EngineError, ProposeCollect};
|
||||||
use evm::Schedule;
|
use evm::Schedule;
|
||||||
use ethjson;
|
use ethjson;
|
||||||
|
|
||||||
@ -89,22 +89,22 @@ impl Tendermint {
|
|||||||
p.validators.get(p.proposer_nonce%p.validator_n).unwrap().clone()
|
p.validators.get(p.proposer_nonce%p.validator_n).unwrap().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn propose_message(&self, message: UntrustedRlp) -> Option<Bytes> {
|
fn propose_message(&self, message: UntrustedRlp) -> Result<Bytes, Error> {
|
||||||
match *self.our_params.s.try_read().unwrap() {
|
match *self.our_params.s.try_read().unwrap() {
|
||||||
Step::Propose => (),
|
Step::Propose => (),
|
||||||
_ => return None,
|
_ => try!(Err(EngineError::WrongStep)),
|
||||||
}
|
}
|
||||||
let proposal = message.val_at(0).unwrap_or_else(|| return None);
|
let proposal = try!(message.val_at(0));
|
||||||
let vote = ProposeCollect::new(proposal,
|
let vote = ProposeCollect::new(proposal,
|
||||||
self.our_params.validators.iter().cloned().collect(),
|
self.our_params.validators.iter().cloned().collect(),
|
||||||
self.threshold());
|
self.threshold());
|
||||||
let mut guard = self.our_params.s.try_write().unwrap();
|
let mut guard = self.our_params.s.write();
|
||||||
*guard = Step::Prevote(vote);
|
*guard = Step::Prevote(vote);
|
||||||
Some(message.as_raw().to_vec())
|
Ok(message.as_raw().to_vec())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prevote_message(&self, sender: Address, message: UntrustedRlp) -> Option<Bytes> {
|
fn prevote_message(&self, sender: Address, message: UntrustedRlp) -> Result<Bytes, Error> {
|
||||||
None
|
try!(Err(EngineError::WrongStep))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn threshold(&self) -> usize {
|
fn threshold(&self) -> usize {
|
||||||
@ -162,11 +162,11 @@ impl Engine for Tendermint {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_message(&self, sender: Address, message: UntrustedRlp) -> Option<Bytes> {
|
fn handle_message(&self, sender: Address, message: UntrustedRlp) -> Result<Bytes, Error> {
|
||||||
match message.val_at(0).unwrap_or_else(|| return None) {
|
match try!(message.val_at(0)) {
|
||||||
0u8 if sender == self.proposer() => self.propose_message(message),
|
0u8 if sender == self.proposer() => self.propose_message(message),
|
||||||
1 => self.prevote_message(sender, message),
|
1 => self.prevote_message(sender, message),
|
||||||
_ => None,
|
_ => try!(Err(EngineError::UnknownStep)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,18 +224,18 @@ mod tests {
|
|||||||
use spec::Spec;
|
use spec::Spec;
|
||||||
|
|
||||||
/// Create a new test chain spec with `Tendermint` consensus engine.
|
/// Create a new test chain spec with `Tendermint` consensus engine.
|
||||||
fn new_test_authority() -> Spec { Spec::load(include_bytes!("../../res/bft.json")) }
|
fn new_test_tendermint() -> Spec { Spec::load(include_bytes!("../../res/tendermint.json")) }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn has_valid_metadata() {
|
fn has_valid_metadata() {
|
||||||
let engine = new_test_authority().engine;
|
let engine = new_test_tendermint().engine;
|
||||||
assert!(!engine.name().is_empty());
|
assert!(!engine.name().is_empty());
|
||||||
assert!(engine.version().major >= 1);
|
assert!(engine.version().major >= 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_return_schedule() {
|
fn can_return_schedule() {
|
||||||
let engine = new_test_authority().engine;
|
let engine = new_test_tendermint().engine;
|
||||||
let schedule = engine.schedule(&EnvInfo {
|
let schedule = engine.schedule(&EnvInfo {
|
||||||
number: 10000000,
|
number: 10000000,
|
||||||
author: 0.into(),
|
author: 0.into(),
|
||||||
@ -251,7 +251,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_do_seal_verification_fail() {
|
fn can_do_seal_verification_fail() {
|
||||||
let engine = new_test_authority().engine;
|
let engine = new_test_tendermint().engine;
|
||||||
let header: Header = Header::default();
|
let header: Header = Header::default();
|
||||||
|
|
||||||
let verify_result = engine.verify_block_basic(&header, None);
|
let verify_result = engine.verify_block_basic(&header, None);
|
||||||
@ -265,7 +265,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_do_signature_verification_fail() {
|
fn can_do_signature_verification_fail() {
|
||||||
let engine = new_test_authority().engine;
|
let engine = new_test_tendermint().engine;
|
||||||
let mut header: Header = Header::default();
|
let mut header: Header = Header::default();
|
||||||
header.set_seal(vec![rlp::encode(&Signature::zero()).to_vec()]);
|
header.set_seal(vec![rlp::encode(&Signature::zero()).to_vec()]);
|
||||||
|
|
||||||
@ -284,7 +284,7 @@ mod tests {
|
|||||||
let addr = tap.insert_account("".sha3(), "").unwrap();
|
let addr = tap.insert_account("".sha3(), "").unwrap();
|
||||||
tap.unlock_account_permanently(addr, "".into()).unwrap();
|
tap.unlock_account_permanently(addr, "".into()).unwrap();
|
||||||
|
|
||||||
let spec = new_test_authority();
|
let spec = new_test_tendermint();
|
||||||
let engine = &*spec.engine;
|
let engine = &*spec.engine;
|
||||||
let genesis_header = spec.genesis_header();
|
let genesis_header = spec.genesis_header();
|
||||||
let mut db_result = get_temp_journal_db();
|
let mut db_result = get_temp_journal_db();
|
||||||
@ -298,6 +298,15 @@ mod tests {
|
|||||||
assert!(b.try_seal(engine, seal).is_ok());
|
assert!(b.try_seal(engine, seal).is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn propose_step(){
|
||||||
|
let engine = new_test_tendermint().engine;
|
||||||
|
let tap = AccountProvider::transient_provider();
|
||||||
|
let addr = tap.insert_account("1".sha3(), "1").unwrap();
|
||||||
|
println!("{:?}", addr);
|
||||||
|
false;
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn handle_message() {
|
fn handle_message() {
|
||||||
false;
|
false;
|
||||||
|
@ -24,7 +24,7 @@ use client::Error as ClientError;
|
|||||||
use ipc::binary::{BinaryConvertError, BinaryConvertable};
|
use ipc::binary::{BinaryConvertError, BinaryConvertable};
|
||||||
use types::block_import_error::BlockImportError;
|
use types::block_import_error::BlockImportError;
|
||||||
use snapshot::Error as SnapshotError;
|
use snapshot::Error as SnapshotError;
|
||||||
use engines::VoteError;
|
use engines::EngineError;
|
||||||
|
|
||||||
pub use types::executed::{ExecutionError, CallError};
|
pub use types::executed::{ExecutionError, CallError};
|
||||||
|
|
||||||
@ -240,7 +240,7 @@ pub enum Error {
|
|||||||
/// Snapshot error.
|
/// Snapshot error.
|
||||||
Snapshot(SnapshotError),
|
Snapshot(SnapshotError),
|
||||||
/// Consensus vote error.
|
/// Consensus vote error.
|
||||||
Vote(VoteError),
|
Engine(EngineError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
@ -261,7 +261,7 @@ impl fmt::Display for Error {
|
|||||||
Error::StdIo(ref err) => err.fmt(f),
|
Error::StdIo(ref err) => err.fmt(f),
|
||||||
Error::Snappy(ref err) => err.fmt(f),
|
Error::Snappy(ref err) => err.fmt(f),
|
||||||
Error::Snapshot(ref err) => err.fmt(f),
|
Error::Snapshot(ref err) => err.fmt(f),
|
||||||
Error::Vote(ref err) =>
|
Error::Engine(ref err) =>
|
||||||
f.write_fmt(format_args!("Bad vote: {:?}", err)),
|
f.write_fmt(format_args!("Bad vote: {:?}", err)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -366,10 +366,10 @@ impl From<SnapshotError> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<VoteError> for Error {
|
impl From<EngineError> for Error {
|
||||||
fn from(err: VoteError) -> Error {
|
fn from(err: EngineError) -> Error {
|
||||||
match err {
|
match err {
|
||||||
other => Error::Vote(other),
|
other => Error::Engine(other),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user