add test, start tendermint

This commit is contained in:
keorn 2016-08-23 12:58:40 +02:00
parent 3515a72fa0
commit 3aa862c9c2
8 changed files with 370 additions and 33 deletions

View File

@ -19,14 +19,16 @@
mod null_engine;
mod instant_seal;
mod basic_authority;
mod bft;
mod tendermint;
mod signed_vote;
mod propose_collect;
pub use self::null_engine::NullEngine;
pub use self::instant_seal::InstantSeal;
pub use self::basic_authority::BasicAuthority;
pub use self::bft::BFT;
pub use self::signed_vote::VoteError;
pub use self::tendermint::Tendermint;
pub use self::signed_vote::{SignedVote, VoteError};
pub use self::propose_collect::{ProposeCollect};
use common::{HashMap, SemanticVersion, Header, EnvInfo, Address, Builtin, BTreeMap, U256, Bytes, SignedTransaction, Error};
use account_provider::AccountProvider;

View File

@ -16,7 +16,9 @@
//! Voting on a hash, where each vote has to come from a set of addresses.
use common::{HashSet, RwLock, H256, Signature, Address, Error, ec, Hashable, AtomicBool};
use std::sync::atomic::{AtomicBool, Ordering};
use common::{HashSet, RwLock, H256, Signature, Address, Error, ec, Hashable};
use engines::VoteError;
/// Collect votes on a hash.
#[derive(Debug)]
@ -33,18 +35,11 @@ pub struct ProposeCollect {
is_won: AtomicBool
}
/// Voting errors.
#[derive(Debug)]
pub enum VoteError {
/// Voter is not in the voters set.
UnauthorisedVoter
}
impl SignedVote {
impl ProposeCollect {
/// Create a new instance of BFT engine
pub fn new(hash: H256, voters: HashSet<Address>, threshold: usize) -> Self {
assert!(voters.len() > threshold);
SignedVote {
ProposeCollect {
hash: hash,
voters: voters,
threshold: threshold,
@ -55,14 +50,14 @@ impl SignedVote {
/// Vote on hash using the signed hash, true if vote counted.
pub fn vote(&self, signature: &Signature) -> bool {
if self.votes.contains(signature) { return false; }
if self.votes.try_read().unwrap().contains(signature) { return false; }
if !self.can_vote(signature).is_ok() { return false; }
self.votes.try_write().unwrap().insert(signature);
self.votes.try_write().unwrap().insert(signature.clone());
true
}
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) {
false => try!(Err(VoteError::UnauthorisedVoter)),
true => Ok(()),
@ -71,11 +66,11 @@ impl SignedVote {
/// Some winner if voting threshold was reached.
pub fn winner(&self) -> Option<H256> {
let threshold_checker = || match self.votes.len() >= threshold {
let threshold_checker = || match self.votes.try_read().unwrap().len() >= self.threshold {
true => { self.is_won.store(true, Ordering::Relaxed); true },
false => false,
};
match self.is_won || threshold_checker() {
match self.is_won.load(Ordering::Relaxed) || threshold_checker() {
true => Some(self.hash),
false => None,
}

View File

@ -60,15 +60,7 @@ impl SignedVote {
let mut guard = self.votes.try_write().unwrap();
let set = guard.entry(bare_hash.clone()).or_insert_with(|| HashSet::new());
if !set.insert(signature.clone()) { return false; }
// let n = if let Some(mut old) = guard.get_mut(&bare_hash) {
// if !old.insert(signature.clone()) { return false; }
// old.len()
// } else {
// let mut new = HashSet::new();
// new.insert(signature.clone());
// assert!(guard.insert(bare_hash.clone(), new).is_none());
// 1
// };
// Set the winner if threshold is reached.
if set.len() >= self.threshold {
let mut guard = self.winner.try_write().unwrap();
*guard = Some(bare_hash);
@ -86,6 +78,11 @@ impl SignedVote {
/// Some winner if voting threshold was reached.
pub fn winner(&self) -> Option<H256> { self.winner.try_read().unwrap().clone() }
/// Get signatures backing given hash.
pub fn votes(&self, bare_hash: &H256) -> Option<HashSet<Signature>> {
self.votes.try_read().unwrap().get(bare_hash).cloned()
}
}
#[cfg(test)]

View File

@ -0,0 +1,284 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Tendermint BFT consensus engine with round robin proof-of-authority.
use common::*;
use account_provider::AccountProvider;
use block::*;
use spec::CommonParams;
use engines::{Engine, ProposeCollect};
use evm::Schedule;
use ethjson;
/// `Tendermint` params.
#[derive(Debug)]
pub struct TendermintParams {
/// Gas limit divisor.
pub gas_limit_bound_divisor: U256,
/// Block duration.
pub duration_limit: u64,
/// List of validators.
pub validators: Vec<Address>,
/// Number of validators.
pub validator_n: usize,
/// Consensus round.
r: u64,
/// Consensus step.
s: Step,
/// Used to swith proposer.
proposer_nonce: usize
}
#[derive(Debug)]
enum Step {
Propose,
Prevote(ProposeCollect),
Precommit(ProposeCollect),
Commit
}
impl From<ethjson::spec::TendermintParams> for TendermintParams {
fn from(p: ethjson::spec::TendermintParams) -> Self {
let val: Vec<_> = p.validators.into_iter().map(Into::into).collect();
let val_n = val.len();
TendermintParams {
gas_limit_bound_divisor: p.gas_limit_bound_divisor.into(),
duration_limit: p.duration_limit.into(),
validators: val,
validator_n: val_n,
r: 0,
s: Step::Propose,
proposer_nonce: 0
}
}
}
/// Engine using `Tendermint` consensus algorithm, suitable for EVM chain.
pub struct Tendermint {
params: CommonParams,
our_params: TendermintParams,
builtins: BTreeMap<Address, Builtin>,
}
impl Tendermint {
/// Create a new instance of Tendermint engine
pub fn new(params: CommonParams, our_params: TendermintParams, builtins: BTreeMap<Address, Builtin>) -> Self {
Tendermint {
params: params,
our_params: our_params,
builtins: builtins,
}
}
fn proposer(&self) -> Address {
let ref p = self.our_params;
p.validators.get(p.proposer_nonce%p.validator_n).unwrap().clone()
}
}
impl Engine for Tendermint {
fn name(&self) -> &str { "Tendermint" }
fn version(&self) -> SemanticVersion { SemanticVersion::new(1, 0, 0) }
/// Possibly signatures of all validators.
fn seal_fields(&self) -> usize { self.our_params.validator_n }
fn params(&self) -> &CommonParams { &self.params }
fn builtins(&self) -> &BTreeMap<Address, Builtin> { &self.builtins }
/// Additional engine-specific information for the user/developer concerning `header`.
fn extra_info(&self, _header: &Header) -> HashMap<String, String> { hash_map!["signature".to_owned() => "TODO".to_owned()] }
fn schedule(&self, _env_info: &EnvInfo) -> Schedule {
Schedule::new_homestead()
}
fn populate_from_parent(&self, header: &mut Header, parent: &Header, gas_floor_target: U256, _gas_ceil_target: U256) {
header.difficulty = parent.difficulty;
header.gas_limit = {
let gas_limit = parent.gas_limit;
let bound_divisor = self.our_params.gas_limit_bound_divisor;
if gas_limit < gas_floor_target {
min(gas_floor_target, gas_limit + gas_limit / bound_divisor - 1.into())
} else {
max(gas_floor_target, gas_limit - gas_limit / bound_divisor + 1.into())
}
};
header.note_dirty();
}
/// Apply the block reward on finalisation of the block.
/// This assumes that all uncles are valid uncles (i.e. of at least one generation before the current).
fn on_close_block(&self, _block: &mut ExecutedBlock) {}
/// Attempt to seal the block internally using all available signatures.
///
/// None is returned if not enough signatures can be collected.
fn generate_seal(&self, block: &ExecutedBlock, accounts: Option<&AccountProvider>) -> Option<Vec<Bytes>> {
accounts.and_then(|ap| {
let header = block.header();
if header.author() == &self.proposer() {
ap.sign(*header.author(), header.bare_hash())
.ok()
.and_then(|signature| Some(vec![encode(&(&*signature as &[u8])).to_vec()]))
} else {
None
}
})
}
fn handle_message(&self, sender: Address, message: Bytes) -> Option<Vec<u8>> {
match message[0] {
0 => println!("0"),
_ => println!("unknown"),
}
//let sig: Signature = message.into();
None
}
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
// check the seal fields.
// TODO: pull this out into common code.
if header.seal.len() != self.seal_fields() {
return Err(From::from(BlockError::InvalidSealArity(
Mismatch { expected: self.seal_fields(), found: header.seal.len() }
)));
}
Ok(())
}
fn verify_block_unordered(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
Ok(())
}
fn verify_block_family(&self, header: &Header, parent: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
// we should not calculate difficulty for genesis blocks
if header.number() == 0 {
return Err(From::from(BlockError::RidiculousNumber(OutOfBounds { min: Some(1), max: None, found: header.number() })));
}
// Check difficulty is correct given the two timestamps.
if header.difficulty() != parent.difficulty() {
return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: *parent.difficulty(), found: *header.difficulty() })))
}
let gas_limit_divisor = self.our_params.gas_limit_bound_divisor;
let min_gas = parent.gas_limit - parent.gas_limit / gas_limit_divisor;
let max_gas = parent.gas_limit + parent.gas_limit / gas_limit_divisor;
if header.gas_limit <= min_gas || header.gas_limit >= max_gas {
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { min: Some(min_gas), max: Some(max_gas), found: header.gas_limit })));
}
Ok(())
}
fn verify_transaction_basic(&self, t: &SignedTransaction, _header: &Header) -> result::Result<(), Error> {
try!(t.check_low_s());
Ok(())
}
fn verify_transaction(&self, t: &SignedTransaction, _header: &Header) -> Result<(), Error> {
t.sender().map(|_|()) // Perform EC recovery and cache sender
}
}
#[cfg(test)]
mod tests {
use common::*;
use block::*;
use tests::helpers::*;
use account_provider::AccountProvider;
use spec::Spec;
/// Create a new test chain spec with `Tendermint` consensus engine.
fn new_test_authority() -> Spec { Spec::load(include_bytes!("../../res/bft.json")) }
#[test]
fn has_valid_metadata() {
let engine = new_test_authority().engine;
assert!(!engine.name().is_empty());
assert!(engine.version().major >= 1);
}
#[test]
fn can_return_schedule() {
let engine = new_test_authority().engine;
let schedule = engine.schedule(&EnvInfo {
number: 10000000,
author: 0.into(),
timestamp: 0,
difficulty: 0.into(),
last_hashes: Arc::new(vec![]),
gas_used: 0.into(),
gas_limit: 0.into(),
});
assert!(schedule.stack_limit > 0);
}
#[test]
fn can_do_seal_verification_fail() {
let engine = new_test_authority().engine;
let header: Header = Header::default();
let verify_result = engine.verify_block_basic(&header, None);
match verify_result {
Err(Error::Block(BlockError::InvalidSealArity(_))) => {},
Err(_) => { panic!("should be block seal-arity mismatch error (got {:?})", verify_result); },
_ => { panic!("Should be error, got Ok"); },
}
}
#[test]
fn can_do_signature_verification_fail() {
let engine = new_test_authority().engine;
let mut header: Header = Header::default();
header.set_seal(vec![rlp::encode(&Signature::zero()).to_vec()]);
let verify_result = engine.verify_block_unordered(&header, None);
match verify_result {
Err(Error::Util(UtilError::Crypto(CryptoError::InvalidSignature))) => {},
Err(_) => { panic!("should be block difficulty error (got {:?})", verify_result); },
_ => { panic!("Should be error, got Ok"); },
}
}
#[test]
fn can_generate_seal() {
let tap = AccountProvider::transient_provider();
let addr = tap.insert_account("".sha3(), "").unwrap();
tap.unlock_account_permanently(addr, "".into()).unwrap();
let spec = new_test_authority();
let engine = &*spec.engine;
let genesis_header = spec.genesis_header();
let mut db_result = get_temp_journal_db();
let mut db = db_result.take();
spec.ensure_db_good(db.as_hashdb_mut()).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]);
let vm_factory = Default::default();
let b = OpenBlock::new(engine, &vm_factory, Default::default(), false, db, &genesis_header, last_hashes, addr, (3141562.into(), 31415620.into()), vec![]).unwrap();
let b = b.close_and_lock();
let seal = engine.generate_seal(b.block(), Some(&tap)).unwrap();
assert!(b.try_seal(engine, seal).is_ok());
}
#[test]
fn handle_message() {
false;
}
}

View File

@ -17,7 +17,7 @@
//! Parameters for a block chain.
use common::*;
use engines::{Engine, NullEngine, InstantSeal, BasicAuthority, BFT};
use engines::{Engine, NullEngine, InstantSeal, BasicAuthority, Tendermint};
use pod_state::*;
use account_db::*;
use super::genesis::Genesis;
@ -139,7 +139,7 @@ impl Spec {
ethjson::spec::Engine::InstantSeal => Arc::new(InstantSeal::new(params, builtins)),
ethjson::spec::Engine::Ethash(ethash) => Arc::new(ethereum::Ethash::new(params, From::from(ethash.params), builtins)),
ethjson::spec::Engine::BasicAuthority(basic_authority) => Arc::new(BasicAuthority::new(params, From::from(basic_authority.params), builtins)),
ethjson::spec::Engine::BFT(bft) => Arc::new(BFT::new(params, From::from(bft.params), builtins)),
ethjson::spec::Engine::Tendermint(tendermint) => Arc::new(Tendermint::new(params, From::from(tendermint.params), builtins)),
}
}

View File

@ -18,7 +18,7 @@
use spec::Ethash;
use spec::BasicAuthority;
use spec::BFT;
use spec::Tendermint;
/// Engine deserialization.
#[derive(Debug, PartialEq, Deserialize)]
@ -32,7 +32,7 @@ pub enum Engine {
/// BasicAuthority engine.
BasicAuthority(BasicAuthority),
/// Byzantine Fault Tolerant engine.
BFT(BFT)
Tendermint(Tendermint)
}
#[cfg(test)]

View File

@ -26,7 +26,7 @@ pub mod engine;
pub mod state;
pub mod ethash;
pub mod basic_authority;
pub mod bft;
pub mod tendermint;
pub use self::account::Account;
pub use self::builtin::{Builtin, Pricing, Linear};
@ -38,4 +38,4 @@ pub use self::engine::Engine;
pub use self::state::State;
pub use self::ethash::{Ethash, EthashParams};
pub use self::basic_authority::{BasicAuthority, BasicAuthorityParams};
pub use self::bft::{BFT, BFTParams};
pub use self::tendermint::{Tendermint, TendermintParams};

View File

@ -0,0 +1,59 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Tendermint params deserialization.
use uint::Uint;
use hash::Address;
/// Tendermint params deserialization.
#[derive(Debug, PartialEq, Deserialize)]
pub struct TendermintParams {
/// Gas limit divisor.
#[serde(rename="gasLimitBoundDivisor")]
pub gas_limit_bound_divisor: Uint,
/// Block duration.
#[serde(rename="durationLimit")]
pub duration_limit: Uint,
/// Valid authorities
pub validators: Vec<Address>,
}
/// Tendermint engine deserialization.
#[derive(Debug, PartialEq, Deserialize)]
pub struct Tendermint {
/// Ethash params.
pub params: TendermintParams,
}
#[cfg(test)]
mod tests {
use serde_json;
use spec::tendermint::Tendermint;
#[test]
fn basic_authority_deserialization() {
let s = r#"{
"params": {
"gasLimitBoundDivisor": "0x0400",
"durationLimit": "0x0d",
"validators" : ["0xc6d9d2cd449a754c494264e1809c50e34d64562b"]
}
}"#;
let _deserialized: Tendermint = serde_json::from_str(s).unwrap();
}
}