Merge remote-tracking branch 'parity/master' into bft
This commit is contained in:
42
ethcore/res/authority_round.json
Normal file
42
ethcore/res/authority_round.json
Normal file
@@ -0,0 +1,42 @@
|
||||
{
|
||||
"name": "TestAuthorityRound",
|
||||
"engine": {
|
||||
"AuthorityRound": {
|
||||
"params": {
|
||||
"gasLimitBoundDivisor": "0x0400",
|
||||
"stepDuration": "1",
|
||||
"authorities" : [
|
||||
"0x7d577a597b2742b498cb5cf0c26cdcd726d39e6e",
|
||||
"0x82a978b3f5962a5b0957d9ee9eef472ee55b42f1"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"params": {
|
||||
"accountStartNonce": "0x0",
|
||||
"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" }
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "TestAuthority",
|
||||
"name": "TestBasicAuthority",
|
||||
"engine": {
|
||||
"BasicAuthority": {
|
||||
"params": {
|
||||
@@ -131,10 +131,11 @@
|
||||
"0x807640a13483f8ac783c557fcdf27be11ea4ac7a"
|
||||
],
|
||||
"eip150Transition": "0x259518",
|
||||
"eip155Transition": "0x7fffffffffffffff",
|
||||
"eip160Transition": "0x7fffffffffffffff",
|
||||
"eip161abcTransition": "0x7fffffffffffffff",
|
||||
"eip161dTransition": "0x7fffffffffffffff"
|
||||
"eip155Transition": 2675000,
|
||||
"eip160Transition": 2675000,
|
||||
"eip161abcTransition": 2675000,
|
||||
"eip161dTransition": 2675000,
|
||||
"maxCodeSize": 24576
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -11,10 +11,10 @@
|
||||
"registrar": "0x52dff57a8a1532e6afb3dc07e2af58bb9eb05b3d",
|
||||
"homesteadTransition": "0x789b0",
|
||||
"eip150Transition": "0x1b34d8",
|
||||
"eip155Transition": "0x7fffffffffffffff",
|
||||
"eip160Transition": "0x7fffffffffffffff",
|
||||
"eip161abcTransition": "0x7fffffffffffffff",
|
||||
"eip161dTransition": "0x7fffffffffffffff"
|
||||
"eip155Transition": 1885000,
|
||||
"eip160Transition": 1885000,
|
||||
"eip161abcTransition": 1885000,
|
||||
"eip161dTransition": 1885000
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -556,6 +556,7 @@ impl Client {
|
||||
|
||||
/// Import transactions from the IO queue
|
||||
pub fn import_queued_transactions(&self, transactions: &[Bytes]) -> usize {
|
||||
trace!(target: "external_tx", "Importing queued");
|
||||
let _timer = PerfTimer::new("import_queued_transactions");
|
||||
self.queue_transactions.fetch_sub(transactions.len(), AtomicOrdering::SeqCst);
|
||||
let txs = transactions.iter().filter_map(|bytes| UntrustedRlp::new(bytes).as_val().ok()).collect();
|
||||
@@ -563,6 +564,11 @@ impl Client {
|
||||
results.len()
|
||||
}
|
||||
|
||||
/// Used by PoA to try sealing on period change.
|
||||
pub fn update_sealing(&self) {
|
||||
self.miner.update_sealing(self)
|
||||
}
|
||||
|
||||
/// Attempt to get a copy of a specific block's final state.
|
||||
///
|
||||
/// This will not fail if given BlockID::Latest.
|
||||
@@ -1195,7 +1201,9 @@ impl BlockChainClient for Client {
|
||||
}
|
||||
|
||||
fn queue_transactions(&self, transactions: Vec<Bytes>) {
|
||||
if self.queue_transactions.load(AtomicOrdering::Relaxed) > MAX_TX_QUEUE_SIZE {
|
||||
let queue_size = self.queue_transactions.load(AtomicOrdering::Relaxed);
|
||||
trace!(target: "external_tx", "Queue size: {}", queue_size);
|
||||
if queue_size > MAX_TX_QUEUE_SIZE {
|
||||
debug!("Ignoring {} transactions: queue is full", transactions.len());
|
||||
} else {
|
||||
let len = transactions.len();
|
||||
|
||||
@@ -119,6 +119,16 @@ impl TestBlockChainClient {
|
||||
/// Creates new test client with specified extra data for each block
|
||||
pub fn new_with_extra_data(extra_data: Bytes) -> Self {
|
||||
let spec = Spec::new_test();
|
||||
TestBlockChainClient::new_with_spec_and_extra(spec, extra_data)
|
||||
}
|
||||
|
||||
/// Create test client with custom spec.
|
||||
pub fn new_with_spec(spec: Spec) -> Self {
|
||||
TestBlockChainClient::new_with_spec_and_extra(spec, Bytes::new())
|
||||
}
|
||||
|
||||
/// Create test client with custom spec and extra data.
|
||||
pub fn new_with_spec_and_extra(spec: Spec, extra_data: Bytes) -> Self {
|
||||
let mut client = TestBlockChainClient {
|
||||
blocks: RwLock::new(HashMap::new()),
|
||||
numbers: RwLock::new(HashMap::new()),
|
||||
@@ -315,7 +325,7 @@ pub fn get_temp_state_db() -> GuardedTempResult<StateDB> {
|
||||
|
||||
impl MiningBlockChainClient for TestBlockChainClient {
|
||||
fn latest_schedule(&self) -> Schedule {
|
||||
Schedule::new_post_eip150(true, true, true)
|
||||
Schedule::new_post_eip150(24576, true, true, true)
|
||||
}
|
||||
|
||||
fn prepare_open_block(&self, author: Address, gas_range_target: (U256, U256), extra_data: Bytes) -> OpenBlock {
|
||||
|
||||
429
ethcore/src/engines/authority_round.rs
Normal file
429
ethcore/src/engines/authority_round.rs
Normal file
@@ -0,0 +1,429 @@
|
||||
// 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/>.
|
||||
|
||||
//! A blockchain engine that supports a non-instant BFT proof-of-authority.
|
||||
|
||||
use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering as AtomicOrdering};
|
||||
use std::sync::Weak;
|
||||
use std::time::{UNIX_EPOCH, Duration};
|
||||
use util::*;
|
||||
use ethkey::{verify_address, Signature};
|
||||
use rlp::{UntrustedRlp, View, encode};
|
||||
use account_provider::AccountProvider;
|
||||
use block::*;
|
||||
use spec::CommonParams;
|
||||
use engines::Engine;
|
||||
use header::Header;
|
||||
use error::{Error, BlockError};
|
||||
use evm::Schedule;
|
||||
use ethjson;
|
||||
use io::{IoContext, IoHandler, TimerToken, IoService, IoChannel};
|
||||
use service::ClientIoMessage;
|
||||
use transaction::SignedTransaction;
|
||||
use env_info::EnvInfo;
|
||||
use builtin::Builtin;
|
||||
|
||||
/// `AuthorityRound` params.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct AuthorityRoundParams {
|
||||
/// Gas limit divisor.
|
||||
pub gas_limit_bound_divisor: U256,
|
||||
/// Time to wait before next block or authority switching.
|
||||
pub step_duration: Duration,
|
||||
/// Valid authorities.
|
||||
pub authorities: Vec<Address>,
|
||||
/// Number of authorities.
|
||||
pub authority_n: usize,
|
||||
}
|
||||
|
||||
impl From<ethjson::spec::AuthorityRoundParams> for AuthorityRoundParams {
|
||||
fn from(p: ethjson::spec::AuthorityRoundParams) -> Self {
|
||||
AuthorityRoundParams {
|
||||
gas_limit_bound_divisor: p.gas_limit_bound_divisor.into(),
|
||||
step_duration: Duration::from_secs(p.step_duration.into()),
|
||||
authority_n: p.authorities.len(),
|
||||
authorities: p.authorities.into_iter().map(Into::into).collect::<Vec<_>>(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Engine using `AuthorityRound` proof-of-work consensus algorithm, suitable for Ethereum
|
||||
/// mainnet chains in the Olympic, Frontier and Homestead eras.
|
||||
pub struct AuthorityRound {
|
||||
params: CommonParams,
|
||||
our_params: AuthorityRoundParams,
|
||||
builtins: BTreeMap<Address, Builtin>,
|
||||
transition_service: IoService<BlockArrived>,
|
||||
message_channel: Mutex<Option<IoChannel<ClientIoMessage>>>,
|
||||
step: AtomicUsize,
|
||||
proposed: AtomicBool,
|
||||
}
|
||||
|
||||
fn header_step(header: &Header) -> Result<usize, ::rlp::DecoderError> {
|
||||
UntrustedRlp::new(&header.seal()[0]).as_val()
|
||||
}
|
||||
|
||||
fn header_signature(header: &Header) -> Result<Signature, ::rlp::DecoderError> {
|
||||
UntrustedRlp::new(&header.seal()[1]).as_val::<H520>().map(Into::into)
|
||||
}
|
||||
|
||||
trait AsMillis {
|
||||
fn as_millis(&self) -> u64;
|
||||
}
|
||||
|
||||
impl AsMillis for Duration {
|
||||
fn as_millis(&self) -> u64 {
|
||||
self.as_secs()*1_000 + (self.subsec_nanos()/1_000_000) as u64
|
||||
}
|
||||
}
|
||||
|
||||
impl AuthorityRound {
|
||||
/// Create a new instance of AuthorityRound engine.
|
||||
pub fn new(params: CommonParams, our_params: AuthorityRoundParams, builtins: BTreeMap<Address, Builtin>) -> Result<Arc<Self>, Error> {
|
||||
let initial_step = (unix_now().as_secs() / our_params.step_duration.as_secs()) as usize;
|
||||
let engine = Arc::new(
|
||||
AuthorityRound {
|
||||
params: params,
|
||||
our_params: our_params,
|
||||
builtins: builtins,
|
||||
transition_service: try!(IoService::<BlockArrived>::start()),
|
||||
message_channel: Mutex::new(None),
|
||||
step: AtomicUsize::new(initial_step),
|
||||
proposed: AtomicBool::new(false)
|
||||
});
|
||||
let handler = TransitionHandler { engine: Arc::downgrade(&engine) };
|
||||
try!(engine.transition_service.register_handler(Arc::new(handler)));
|
||||
Ok(engine)
|
||||
}
|
||||
|
||||
fn step(&self) -> usize {
|
||||
self.step.load(AtomicOrdering::SeqCst)
|
||||
}
|
||||
|
||||
fn remaining_step_duration(&self) -> Duration {
|
||||
let now = unix_now();
|
||||
let step_end = self.our_params.step_duration * (self.step() as u32 + 1);
|
||||
if step_end > now {
|
||||
step_end - now
|
||||
} else {
|
||||
Duration::from_secs(0)
|
||||
}
|
||||
}
|
||||
|
||||
fn step_proposer(&self, step: usize) -> &Address {
|
||||
let ref p = self.our_params;
|
||||
p.authorities.get(step % p.authority_n).expect("There are authority_n authorities; taking number modulo authority_n gives number in authority_n range; qed")
|
||||
}
|
||||
|
||||
fn is_step_proposer(&self, step: usize, address: &Address) -> bool {
|
||||
self.step_proposer(step) == address
|
||||
}
|
||||
}
|
||||
|
||||
fn unix_now() -> Duration {
|
||||
UNIX_EPOCH.elapsed().expect("Valid time has to be set in your system.")
|
||||
}
|
||||
|
||||
struct TransitionHandler {
|
||||
engine: Weak<AuthorityRound>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct BlockArrived;
|
||||
|
||||
const ENGINE_TIMEOUT_TOKEN: TimerToken = 23;
|
||||
|
||||
impl IoHandler<BlockArrived> for TransitionHandler {
|
||||
fn initialize(&self, io: &IoContext<BlockArrived>) {
|
||||
if let Some(engine) = self.engine.upgrade() {
|
||||
io.register_timer_once(ENGINE_TIMEOUT_TOKEN, engine.remaining_step_duration().as_millis())
|
||||
.unwrap_or_else(|e| warn!(target: "poa", "Failed to start consensus step timer: {}.", e))
|
||||
}
|
||||
}
|
||||
|
||||
fn timeout(&self, io: &IoContext<BlockArrived>, timer: TimerToken) {
|
||||
if timer == ENGINE_TIMEOUT_TOKEN {
|
||||
if let Some(engine) = self.engine.upgrade() {
|
||||
engine.step.fetch_add(1, AtomicOrdering::SeqCst);
|
||||
engine.proposed.store(false, AtomicOrdering::SeqCst);
|
||||
if let Some(ref channel) = *engine.message_channel.lock() {
|
||||
match channel.send(ClientIoMessage::UpdateSealing) {
|
||||
Ok(_) => trace!(target: "poa", "timeout: UpdateSealing message sent for step {}.", engine.step.load(AtomicOrdering::Relaxed)),
|
||||
Err(err) => trace!(target: "poa", "timeout: Could not send a sealing message {} for step {}.", err, engine.step.load(AtomicOrdering::Relaxed)),
|
||||
}
|
||||
}
|
||||
io.register_timer_once(ENGINE_TIMEOUT_TOKEN, engine.remaining_step_duration().as_millis())
|
||||
.unwrap_or_else(|e| warn!(target: "poa", "Failed to restart consensus step timer: {}.", e))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Engine for AuthorityRound {
|
||||
fn name(&self) -> &str { "AuthorityRound" }
|
||||
fn version(&self) -> SemanticVersion { SemanticVersion::new(1, 0, 0) }
|
||||
/// Two fields - consensus step and the corresponding proposer signature.
|
||||
fn seal_fields(&self) -> usize { 2 }
|
||||
|
||||
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) -> BTreeMap<String, String> {
|
||||
map![
|
||||
"step".into() => header_step(header).as_ref().map(ToString::to_string).unwrap_or("".into()),
|
||||
"signature".into() => header_signature(header).as_ref().map(ToString::to_string).unwrap_or("".into())
|
||||
]
|
||||
}
|
||||
|
||||
fn schedule(&self, _env_info: &EnvInfo) -> Schedule {
|
||||
Schedule::new_post_eip150(usize::max_value(), true, true, true)
|
||||
}
|
||||
|
||||
fn populate_from_parent(&self, header: &mut Header, parent: &Header, gas_floor_target: U256, _gas_ceil_target: U256) {
|
||||
header.set_difficulty(parent.difficulty().clone());
|
||||
header.set_gas_limit({
|
||||
let gas_limit = parent.gas_limit().clone();
|
||||
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())
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// 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) {}
|
||||
|
||||
fn is_sealer(&self, author: &Address) -> Option<bool> {
|
||||
let ref p = self.our_params;
|
||||
Some(p.authorities.contains(author))
|
||||
}
|
||||
|
||||
/// Attempt to seal the block internally.
|
||||
///
|
||||
/// This operation is synchronous and may (quite reasonably) not be available, in which `false` will
|
||||
/// be returned.
|
||||
fn generate_seal(&self, block: &ExecutedBlock, accounts: Option<&AccountProvider>) -> Option<Vec<Bytes>> {
|
||||
if self.proposed.load(AtomicOrdering::SeqCst) { return None; }
|
||||
let header = block.header();
|
||||
let step = self.step();
|
||||
if self.is_step_proposer(step, header.author()) {
|
||||
if let Some(ap) = accounts {
|
||||
// Account should be permanently unlocked, otherwise sealing will fail.
|
||||
if let Ok(signature) = ap.sign(*header.author(), None, header.bare_hash()) {
|
||||
trace!(target: "poa", "generate_seal: Issuing a block for step {}.", step);
|
||||
self.proposed.store(true, AtomicOrdering::SeqCst);
|
||||
return Some(vec![encode(&step).to_vec(), encode(&(&*signature as &[u8])).to_vec()]);
|
||||
} else {
|
||||
warn!(target: "poa", "generate_seal: FAIL: Accounts secret key unavailable.");
|
||||
}
|
||||
} else {
|
||||
warn!(target: "poa", "generate_seal: FAIL: Accounts not provided.");
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Check the number of seal fields.
|
||||
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
|
||||
if header.seal().len() != self.seal_fields() {
|
||||
trace!(target: "poa", "verify_block_basic: wrong number of seal fields");
|
||||
Err(From::from(BlockError::InvalidSealArity(
|
||||
Mismatch { expected: self.seal_fields(), found: header.seal().len() }
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the signature belongs to the correct proposer.
|
||||
fn verify_block_unordered(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
|
||||
let header_step = try!(header_step(header));
|
||||
// Give one step slack if step is lagging, double vote is still not possible.
|
||||
if header_step <= self.step() + 1 {
|
||||
let proposer_signature = try!(header_signature(header));
|
||||
let ok_sig = try!(verify_address(self.step_proposer(header_step), &proposer_signature, &header.bare_hash()));
|
||||
if ok_sig {
|
||||
Ok(())
|
||||
} else {
|
||||
trace!(target: "poa", "verify_block_unordered: invalid seal signature");
|
||||
try!(Err(BlockError::InvalidSeal))
|
||||
}
|
||||
} else {
|
||||
trace!(target: "poa", "verify_block_unordered: block from the future");
|
||||
try!(Err(BlockError::InvalidSeal))
|
||||
}
|
||||
}
|
||||
|
||||
fn verify_block_family(&self, header: &Header, parent: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
|
||||
// Don't calculate difficulty for genesis blocks.
|
||||
if header.number() == 0 {
|
||||
return Err(From::from(BlockError::RidiculousNumber(OutOfBounds { min: Some(1), max: None, found: header.number() })));
|
||||
}
|
||||
|
||||
let step = try!(header_step(header));
|
||||
// Check if parent is from a previous step.
|
||||
if step == try!(header_step(parent)) {
|
||||
trace!(target: "poa", "Multiple blocks proposed for step {}.", step);
|
||||
try!(Err(BlockError::DoubleVote(header.author().clone())));
|
||||
}
|
||||
|
||||
// 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().clone() - parent.gas_limit().clone() / gas_limit_divisor;
|
||||
let max_gas = parent.gas_limit().clone() + parent.gas_limit().clone() / 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().clone() })));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn verify_transaction_basic(&self, t: &SignedTransaction, _header: &Header) -> 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
|
||||
}
|
||||
|
||||
fn register_message_channel(&self, message_channel: IoChannel<ClientIoMessage>) {
|
||||
let mut guard = self.message_channel.lock();
|
||||
*guard = Some(message_channel);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use util::*;
|
||||
use env_info::EnvInfo;
|
||||
use header::Header;
|
||||
use error::{Error, BlockError};
|
||||
use rlp::encode;
|
||||
use block::*;
|
||||
use tests::helpers::*;
|
||||
use account_provider::AccountProvider;
|
||||
use spec::Spec;
|
||||
use std::time::UNIX_EPOCH;
|
||||
|
||||
#[test]
|
||||
fn has_valid_metadata() {
|
||||
let engine = Spec::new_test_round().engine;
|
||||
assert!(!engine.name().is_empty());
|
||||
assert!(engine.version().major >= 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_return_schedule() {
|
||||
let engine = Spec::new_test_round().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 verification_fails_on_short_seal() {
|
||||
let engine = Spec::new_test_round().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 = Spec::new_test_round().engine;
|
||||
let mut header: Header = Header::default();
|
||||
header.set_seal(vec![encode(&H520::default()).to_vec()]);
|
||||
|
||||
let verify_result = engine.verify_block_unordered(&header, None);
|
||||
assert!(verify_result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generates_seal_and_does_not_double_propose() {
|
||||
let tap = AccountProvider::transient_provider();
|
||||
let addr1 = tap.insert_account("1".sha3(), "1").unwrap();
|
||||
tap.unlock_account_permanently(addr1, "1".into()).unwrap();
|
||||
let addr2 = tap.insert_account("2".sha3(), "2").unwrap();
|
||||
tap.unlock_account_permanently(addr2, "2".into()).unwrap();
|
||||
|
||||
let spec = Spec::new_test_round();
|
||||
let engine = &*spec.engine;
|
||||
let genesis_header = spec.genesis_header();
|
||||
let mut db1 = get_temp_state_db().take();
|
||||
spec.ensure_db_good(&mut db1).unwrap();
|
||||
let mut db2 = get_temp_state_db().take();
|
||||
spec.ensure_db_good(&mut db2).unwrap();
|
||||
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
||||
let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![]).unwrap();
|
||||
let b1 = b1.close_and_lock();
|
||||
let b2 = OpenBlock::new(engine, Default::default(), false, db2, &genesis_header, last_hashes, addr2, (3141562.into(), 31415620.into()), vec![]).unwrap();
|
||||
let b2 = b2.close_and_lock();
|
||||
|
||||
if let Some(seal) = engine.generate_seal(b1.block(), Some(&tap)) {
|
||||
assert!(b1.clone().try_seal(engine, seal).is_ok());
|
||||
// Second proposal is forbidden.
|
||||
assert!(engine.generate_seal(b1.block(), Some(&tap)).is_none());
|
||||
}
|
||||
|
||||
if let Some(seal) = engine.generate_seal(b2.block(), Some(&tap)) {
|
||||
assert!(b2.clone().try_seal(engine, seal).is_ok());
|
||||
// Second proposal is forbidden.
|
||||
assert!(engine.generate_seal(b2.block(), Some(&tap)).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn proposer_switching() {
|
||||
let mut header: Header = Header::default();
|
||||
let tap = AccountProvider::transient_provider();
|
||||
let addr = tap.insert_account("0".sha3(), "0").unwrap();
|
||||
|
||||
header.set_author(addr);
|
||||
|
||||
let engine = Spec::new_test_round().engine;
|
||||
|
||||
let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap();
|
||||
let mut step = UNIX_EPOCH.elapsed().unwrap().as_secs();
|
||||
header.set_seal(vec![encode(&step).to_vec(), encode(&(&*signature as &[u8])).to_vec()]);
|
||||
let first_ok = engine.verify_block_seal(&header).is_ok();
|
||||
step = step + 1;
|
||||
header.set_seal(vec![encode(&step).to_vec(), encode(&(&*signature as &[u8])).to_vec()]);
|
||||
let second_ok = engine.verify_block_seal(&header).is_ok();
|
||||
|
||||
assert!(first_ok ^ second_ok);
|
||||
}
|
||||
}
|
||||
@@ -181,13 +181,6 @@ impl Engine for BasicAuthority {
|
||||
}
|
||||
}
|
||||
|
||||
impl Header {
|
||||
/// Get the none field of the header.
|
||||
pub fn signature(&self) -> H520 {
|
||||
::rlp::decode(&self.seal()[0])
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use util::*;
|
||||
@@ -201,7 +194,7 @@ mod tests {
|
||||
|
||||
/// Create a new test chain spec with `BasicAuthority` consensus engine.
|
||||
fn new_test_authority() -> Spec {
|
||||
let bytes: &[u8] = include_bytes!("../../res/test_authority.json");
|
||||
let bytes: &[u8] = include_bytes!("../../res/basic_authority.json");
|
||||
Spec::load(bytes).expect("invalid chain spec")
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ impl Engine for InstantSeal {
|
||||
}
|
||||
|
||||
fn schedule(&self, _env_info: &EnvInfo) -> Schedule {
|
||||
Schedule::new_post_eip150(false, false, false)
|
||||
Schedule::new_post_eip150(usize::max_value(), false, false, false)
|
||||
}
|
||||
|
||||
fn is_sealer(&self, _author: &Address) -> Option<bool> { Some(true) }
|
||||
|
||||
@@ -19,13 +19,16 @@
|
||||
mod null_engine;
|
||||
mod instant_seal;
|
||||
mod basic_authority;
|
||||
mod authority_round;
|
||||
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::authority_round::AuthorityRound;
|
||||
pub use self::tendermint::Tendermint;
|
||||
pub use self::signed_vote::SignedVote;
|
||||
pub use self::propose_collect::ProposeCollect;
|
||||
@@ -39,6 +42,8 @@ use env_info::EnvInfo;
|
||||
use error::Error;
|
||||
use spec::CommonParams;
|
||||
use evm::Schedule;
|
||||
use io::IoChannel;
|
||||
use service::ClientIoMessage;
|
||||
use header::Header;
|
||||
use transaction::SignedTransaction;
|
||||
use ethereum::ethash;
|
||||
@@ -171,6 +176,9 @@ pub trait Engine : Sync + Send {
|
||||
self.builtins().get(a).expect("attempted to execute nonexistent builtin").execute(input, output);
|
||||
}
|
||||
|
||||
/// Add a channel for communication with Client which can be used for sealing.
|
||||
fn register_message_channel(&self, _message_channel: IoChannel<ClientIoMessage>) {}
|
||||
|
||||
/// Check if new block should be chosen as the one in chain.
|
||||
fn is_new_best_block(&self, best_total_difficulty: U256, _best_header: HeaderView, parent_details: &BlockDetails, new_header: &HeaderView) -> bool {
|
||||
ethash::is_new_best_block(best_total_difficulty, parent_details, new_header)
|
||||
|
||||
@@ -23,19 +23,19 @@ use rlp::{View, DecoderError, Decodable, Decoder, Encodable, RlpStream, Stream};
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct ConsensusMessage {
|
||||
pub signature: H520,
|
||||
height: Height,
|
||||
round: Round,
|
||||
pub height: Height,
|
||||
pub round: Round,
|
||||
pub step: Step,
|
||||
block_hash: Option<BlockHash>
|
||||
pub block_hash: Option<BlockHash>
|
||||
}
|
||||
|
||||
impl ConsensusMessage {
|
||||
fn is_round(&self, height: Height, round: Round) -> bool {
|
||||
pub fn is_round(&self, height: Height, round: Round) -> bool {
|
||||
self.height == height && self.round == round
|
||||
}
|
||||
|
||||
fn is_step(&self, height: Height, round: Round, step: Step) -> bool {
|
||||
self.height == height && self.round == round && self.step == step
|
||||
pub fn is_step(&self, height: Height, round: Round, step: &Step) -> bool {
|
||||
self.height == height && self.round == round && &self.step == step
|
||||
}
|
||||
|
||||
pub fn is_aligned(&self, height: Height, round: Round, block_hash: Option<H256>) -> bool {
|
||||
|
||||
@@ -73,12 +73,12 @@ pub struct Tendermint {
|
||||
round: AtomicUsize,
|
||||
/// Consensus step.
|
||||
step: RwLock<Step>,
|
||||
/// Current step timeout in ms.
|
||||
timeout: AtomicMs,
|
||||
/// Used to swith proposer.
|
||||
proposer_nonce: AtomicUsize,
|
||||
/// Vote accumulator.
|
||||
votes: VoteCollector
|
||||
votes: VoteCollector,
|
||||
/// Channel for updating the sealing.
|
||||
message_channel: Mutex<Option<IoChannel<ClientIoMessage>>>
|
||||
}
|
||||
|
||||
impl Tendermint {
|
||||
@@ -87,7 +87,6 @@ impl Tendermint {
|
||||
let engine = Arc::new(
|
||||
Tendermint {
|
||||
params: params,
|
||||
timeout: AtomicUsize::new(our_params.timeouts.propose),
|
||||
our_params: our_params,
|
||||
builtins: builtins,
|
||||
timeout_service: try!(IoService::<NextStep>::start()),
|
||||
@@ -96,7 +95,8 @@ impl Tendermint {
|
||||
round: AtomicUsize::new(0),
|
||||
step: RwLock::new(Step::Propose),
|
||||
proposer_nonce: AtomicUsize::new(0),
|
||||
votes: VoteCollector::new()
|
||||
votes: VoteCollector::new(),
|
||||
message_channel: Mutex::new(None)
|
||||
});
|
||||
let handler = TimerHandler { engine: Arc::downgrade(&engine) };
|
||||
try!(engine.timeout_service.register_handler(Arc::new(handler)));
|
||||
@@ -116,95 +116,21 @@ impl Tendermint {
|
||||
self.our_params.authorities.contains(address)
|
||||
}
|
||||
|
||||
fn new_vote(&self, proposal: BlockHash) -> ProposeCollect {
|
||||
ProposeCollect::new(proposal,
|
||||
self.our_params.authorities.iter().cloned().collect(),
|
||||
self.threshold())
|
||||
}
|
||||
|
||||
fn to_step(&self, step: Step) {
|
||||
let mut guard = self.step.try_write().unwrap();
|
||||
*guard = step;
|
||||
}
|
||||
|
||||
fn to_propose(&self) {
|
||||
trace!(target: "poa", "step: entering propose");
|
||||
println!("step: entering propose");
|
||||
self.proposer_nonce.fetch_add(1, AtomicOrdering::Relaxed);
|
||||
self.to_step(Step::Propose);
|
||||
}
|
||||
|
||||
fn propose_message(&self, message: UntrustedRlp) -> Result<Bytes, Error> {
|
||||
// Check if message is for correct step.
|
||||
match *self.step.try_read().unwrap() {
|
||||
Step::Propose => (),
|
||||
_ => try!(Err(EngineError::WrongStep)),
|
||||
}
|
||||
let proposal = try!(message.as_val());
|
||||
self.to_prevote(proposal);
|
||||
Ok(message.as_raw().to_vec())
|
||||
}
|
||||
|
||||
fn to_prevote(&self, proposal: BlockHash) {
|
||||
trace!(target: "poa", "step: entering prevote");
|
||||
println!("step: entering prevote");
|
||||
// Proceed to the prevote step.
|
||||
self.to_step(Step::Prevote(self.new_vote(proposal)));
|
||||
}
|
||||
|
||||
fn prevote_message(&self, sender: Address, message: UntrustedRlp) -> Result<Bytes, Error> {
|
||||
// Check if message is for correct step.
|
||||
let hash = match *self.step.try_write().unwrap() {
|
||||
Step::Prevote => {
|
||||
// Vote if message is about the right block.
|
||||
if vote.hash == try!(message.as_val()) {
|
||||
vote.vote(sender);
|
||||
// Move to next step is prevote is won.
|
||||
if vote.is_won() {
|
||||
// If won assign a hash used for precommit.
|
||||
vote.hash.clone()
|
||||
} else {
|
||||
// Just propoagate the message if not won yet.
|
||||
return Ok(message.as_raw().to_vec());
|
||||
}
|
||||
} else {
|
||||
try!(Err(EngineError::WrongVote))
|
||||
}
|
||||
},
|
||||
_ => try!(Err(EngineError::WrongStep)),
|
||||
};
|
||||
self.to_precommit(hash);
|
||||
Ok(message.as_raw().to_vec())
|
||||
}
|
||||
|
||||
fn to_precommit(&self, proposal: BlockHash) {
|
||||
trace!(target: "poa", "step: entering precommit");
|
||||
println!("step: entering precommit");
|
||||
self.to_step(Step::Precommit(self.new_vote(proposal), Vec::new()));
|
||||
}
|
||||
|
||||
/// Move to commit step, when valid block is known and being distributed.
|
||||
pub fn to_commit(&self, block_hash: H256, seal: Vec<Bytes>) {
|
||||
trace!(target: "poa", "step: entering commit");
|
||||
println!("step: entering commit");
|
||||
self.to_step(Step::Commit(block_hash, seal));
|
||||
}
|
||||
|
||||
fn threshold(&self) -> usize {
|
||||
self.our_params.authority_n * 2/3
|
||||
}
|
||||
|
||||
fn next_timeout(&self) -> u64 {
|
||||
self.timeout.load(AtomicOrdering::Relaxed) as u64
|
||||
}
|
||||
|
||||
/// Round proposer switching.
|
||||
fn is_proposer(&self, address: &Address) -> bool {
|
||||
self.is_nonce_proposer(self.proposer_nonce.load(AtomicOrdering::SeqCst), address)
|
||||
}
|
||||
|
||||
fn is_current(&self, message: &ConsensusMessage) -> bool {
|
||||
message.is_step(self.height.load(AtomicOrdering::SeqCst), self.round.load(AtomicOrdering::SeqCst), self.step.load(AtomicOrdering::SeqCst))
|
||||
message.is_step(self.height.load(AtomicOrdering::SeqCst), self.round.load(AtomicOrdering::SeqCst), &self.step.read())
|
||||
}
|
||||
|
||||
fn has_enough_any_votes(&self) -> bool {
|
||||
self.votes.count_step_votes(self.height.load(AtomicOrdering::SeqCst), self.round.load(AtomicOrdering::SeqCst), &self.step.read()) > self.threshold()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,12 +139,12 @@ fn block_hash(header: &Header) -> H256 {
|
||||
header.rlp(Seal::WithSome(1)).sha3()
|
||||
}
|
||||
|
||||
fn proposer_signature(header: &Header) -> H520 {
|
||||
try!(UntrustedRlp::new(header.seal()[1].as_slice()).as_val())
|
||||
fn proposer_signature(header: &Header) -> Result<H520, ::rlp::DecoderError> {
|
||||
UntrustedRlp::new(header.seal()[1].as_slice()).as_val()
|
||||
}
|
||||
|
||||
fn consensus_round(header: &Header) -> Round {
|
||||
try!(UntrustedRlp::new(header.seal()[0].as_slice()).as_val())
|
||||
fn consensus_round(header: &Header) -> Result<Round, ::rlp::DecoderError> {
|
||||
UntrustedRlp::new(header.seal()[0].as_slice()).as_val()
|
||||
}
|
||||
|
||||
impl Engine for Tendermint {
|
||||
@@ -233,10 +159,10 @@ impl Engine for Tendermint {
|
||||
/// Additional engine-specific information for the user/developer concerning `header`.
|
||||
fn extra_info(&self, header: &Header) -> BTreeMap<String, String> {
|
||||
map![
|
||||
"signature".into() => proposer_signature(header).to_string(),
|
||||
"signature".into() => proposer_signature(header).as_ref().map(ToString::to_string).unwrap_or("".into()),
|
||||
"height".into() => header.number().to_string(),
|
||||
"round".into() => consensus_round(header).to_string(),
|
||||
"block_hash".into() => block_hash(header).to_string().unwrap_or("".into())
|
||||
"round".into() => consensus_round(header).as_ref().map(ToString::to_string).unwrap_or("".into()),
|
||||
"block_hash".into() => block_hash(header).to_string()
|
||||
]
|
||||
}
|
||||
|
||||
@@ -326,10 +252,10 @@ impl Engine for Tendermint {
|
||||
}
|
||||
|
||||
// Check if the message affects the current step.
|
||||
if self.is_current(message) {
|
||||
match self.step.load(AtomicOrdering::SeqCst) {
|
||||
if self.is_current(&message) {
|
||||
match *self.step.read() {
|
||||
Step::Prevote => {
|
||||
let votes = aligned_signatures(message);
|
||||
let votes = self.votes.aligned_signatures(&message);
|
||||
if votes.len() > self.threshold() {
|
||||
}
|
||||
},
|
||||
@@ -352,14 +278,15 @@ impl Engine for Tendermint {
|
||||
|
||||
/// Also transitions to Prevote if verifying Proposal.
|
||||
fn verify_block_unordered(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
|
||||
let proposer = public_to_address(&try!(recover(&proposal_signature(header).into(), &block_hash(header))));
|
||||
let signature = try!(proposer_signature(header));
|
||||
let proposer = public_to_address(&try!(recover(&signature.into(), &block_hash(header))));
|
||||
if !self.is_proposer(&proposer) {
|
||||
try!(Err(BlockError::InvalidSeal))
|
||||
}
|
||||
let proposal = ConsensusMessage {
|
||||
signature: proposal_signature,
|
||||
signature: signature,
|
||||
height: header.number() as Height,
|
||||
round: consensus_round(header),
|
||||
round: try!(consensus_round(header)),
|
||||
step: Step::Propose,
|
||||
block_hash: Some(block_hash(header))
|
||||
};
|
||||
@@ -368,7 +295,7 @@ impl Engine for Tendermint {
|
||||
for rlp in votes_rlp.iter() {
|
||||
let sig: H520 = try!(rlp.as_val());
|
||||
let address = public_to_address(&try!(recover(&sig.into(), &block_hash(header))));
|
||||
if !self.our_params.authorities.contains(a) {
|
||||
if !self.our_params.authorities.contains(&address) {
|
||||
try!(Err(BlockError::InvalidSeal))
|
||||
}
|
||||
}
|
||||
@@ -408,6 +335,11 @@ impl Engine for Tendermint {
|
||||
let best_signatures = best_header.seal().get(2).expect("Tendermint seal should have three elements.").len();
|
||||
new_signatures > best_signatures
|
||||
}
|
||||
|
||||
fn register_message_channel(&self, message_channel: IoChannel<ClientIoMessage>) {
|
||||
let mut guard = self.message_channel.lock();
|
||||
*guard = Some(message_channel);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
use ethjson;
|
||||
use super::timeout::TendermintTimeouts;
|
||||
use util::{Address, U256};
|
||||
use time::Duration;
|
||||
|
||||
/// `Tendermint` params.
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -41,25 +42,30 @@ impl Default for TendermintParams {
|
||||
gas_limit_bound_divisor: 0x0400.into(),
|
||||
authorities: authorities,
|
||||
authority_n: val_n,
|
||||
timeouts: DefaultTimeouts::default()
|
||||
timeouts: TendermintTimeouts::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn to_duration(ms: ethjson::uint::Uint) -> Duration {
|
||||
let ms: usize = ms.into();
|
||||
Duration::milliseconds(ms as i64)
|
||||
}
|
||||
|
||||
impl From<ethjson::spec::TendermintParams> for TendermintParams {
|
||||
fn from(p: ethjson::spec::TendermintParams) -> Self {
|
||||
let val: Vec<_> = p.authorities.into_iter().map(Into::into).collect();
|
||||
let val_n = val.len();
|
||||
let dt = TendermintTimeouts::default();
|
||||
TendermintParams {
|
||||
gas_limit_bound_divisor: p.gas_limit_bound_divisor.into(),
|
||||
authorities: val,
|
||||
authority_n: val_n,
|
||||
let dt = TendermintTimeouts::default();
|
||||
timeouts: TendermintTimeouts {
|
||||
propose: p.timeout_propose.unwrap_or(dt.propose),
|
||||
prevote: p.timeout_prevote.unwrap_or(dt.prevote),
|
||||
precommit: p.timeout_precommit.unwrap_or(dt.precommit),
|
||||
commit: p.timeout_commit.unwrap_or(dt.commit)
|
||||
propose: p.timeout_propose.map_or(dt.propose, to_duration),
|
||||
prevote: p.timeout_prevote.map_or(dt.prevote, to_duration),
|
||||
precommit: p.timeout_precommit.map_or(dt.precommit, to_duration),
|
||||
commit: p.timeout_commit.map_or(dt.commit, to_duration)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,14 +30,14 @@ pub struct TimerHandler {
|
||||
/// Base timeout of each step in ms.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TendermintTimeouts {
|
||||
propose: Duration,
|
||||
prevote: Duartion,
|
||||
precommit: Duration,
|
||||
commit: Duration
|
||||
pub propose: Duration,
|
||||
pub prevote: Duration,
|
||||
pub precommit: Duration,
|
||||
pub commit: Duration
|
||||
}
|
||||
|
||||
impl TendermintTimeouts {
|
||||
pub fn for_step(step: Step) -> Duration {
|
||||
pub fn for_step(&self, step: Step) -> Duration {
|
||||
match step {
|
||||
Step::Propose => self.propose,
|
||||
Step::Prevote => self.prevote,
|
||||
@@ -49,7 +49,7 @@ impl TendermintTimeouts {
|
||||
|
||||
impl Default for TendermintTimeouts {
|
||||
fn default() -> Self {
|
||||
DefaultTimeouts {
|
||||
TendermintTimeouts {
|
||||
propose: Duration::milliseconds(1000),
|
||||
prevote: Duration::milliseconds(1000),
|
||||
precommit: Duration::milliseconds(1000),
|
||||
@@ -64,38 +64,55 @@ pub struct NextStep;
|
||||
/// Timer token representing the consensus step timeouts.
|
||||
pub const ENGINE_TIMEOUT_TOKEN: TimerToken = 23;
|
||||
|
||||
fn set_timeout(io: &IoContext<NextStep>, timeout: Duration) {
|
||||
io.register_timer_once(ENGINE_TIMEOUT_TOKEN, timeout.num_milliseconds() as u64)
|
||||
.unwrap_or_else(|e| warn!(target: "poa", "Failed to set consensus step timeout: {}.", e))
|
||||
}
|
||||
|
||||
fn update_sealing(io_channel: Mutex<Option<IoChannel<ClientIoMessage>>>) {
|
||||
if let Some(ref channel) = *io_channel.lock() {
|
||||
match channel.send(ClientIoMessage::UpdateSealing) {
|
||||
Ok(_) => trace!(target: "poa", "timeout: UpdateSealing message sent for round {}.", engine.round.load(AtomicOrdering::SeqCst)),
|
||||
Err(err) => trace!(target: "poa", "timeout: Could not send a sealing message {} for round {}.", err, engine.round.load(AtomicOrdering::SeqCst)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IoHandler<NextStep> for TimerHandler {
|
||||
fn initialize(&self, io: &IoContext<NextStep>) {
|
||||
if let Some(engine) = self.engine.upgrade() {
|
||||
io.register_timer_once(ENGINE_TIMEOUT_TOKEN, engine.remaining_step_duration().as_millis())
|
||||
.unwrap_or_else(|e| warn!(target: "poa", "Failed to start consensus step timer: {}.", e))
|
||||
set_timeout(io, engine.our_params.timeouts.propose)
|
||||
}
|
||||
}
|
||||
|
||||
fn timeout(&self, io: &IoContext<NextStep>, timer: TimerToken) {
|
||||
if timer == ENGINE_TIMEOUT_TOKEN {
|
||||
if let Some(engine) = self.engine.upgrade() {
|
||||
engine.step.fetch_add(1, AtomicOrdering::SeqCst);
|
||||
engine.proposed.store(false, AtomicOrdering::SeqCst);
|
||||
let next_step = match *engine.step.try_read().unwrap() {
|
||||
Step::Propose => Step::Prevote,
|
||||
Step::Prevote => Step::Precommit,
|
||||
Step::Precommit => Step::Propose,
|
||||
Step::Commit => {
|
||||
engine.round.fetch_add(1, AtomicOrdering::Relaxed);
|
||||
Step::Propose
|
||||
let next_step = match *engine.step.read() {
|
||||
Step::Propose => {
|
||||
set_timeout(io, engine.our_params.timeouts.prevote);
|
||||
Some(Step::Prevote)
|
||||
},
|
||||
Step::Prevote if engine.has_enough_any_votes() => {
|
||||
set_timeout(io, engine.our_params.timeouts.precommit);
|
||||
Some(Step::Precommit)
|
||||
},
|
||||
Step::Precommit if engine.has_enough_any_votes() => {
|
||||
set_timeout(io, engine.our_params.timeouts.propose);
|
||||
engine.round.fetch_add(1, AtomicOrdering::SeqCst);
|
||||
Some(Step::Propose)
|
||||
},
|
||||
Step::Commit => {
|
||||
set_timeout(io, engine.our_params.timeouts.propose);
|
||||
engine.round.store(0, AtomicOrdering::SeqCst);
|
||||
engine.height.fetch_add(1, AtomicOrdering::SeqCst);
|
||||
Some(Step::Propose)
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
|
||||
|
||||
io.register_timer_once(ENGINE_TIMEOUT_TOKEN, engine.next_timeout().as_millis())
|
||||
.unwrap_or_else(|e| warn!(target: "poa", "Failed to restart consensus step timer: {}.", e))
|
||||
|
||||
if let Some(ref channel) = *engine.message_channel.lock() {
|
||||
match channel.send(ClientIoMessage::UpdateSealing) {
|
||||
Ok(_) => trace!(target: "poa", "timeout: UpdateSealing message sent for step {}.", engine.step.),
|
||||
Err(err) => trace!(target: "poa", "timeout: Could not send a sealing message {} for step {}.", err, engine.step.load(AtomicOrdering::Relaxed)),
|
||||
}
|
||||
if let Some(step) = next_step {
|
||||
*engine.step.write() = step
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,9 +33,7 @@ impl VoteCollector {
|
||||
}
|
||||
|
||||
pub fn vote(&self, message: ConsensusMessage, voter: Address) {
|
||||
if let Some(mut guard) = self.votes.write() {
|
||||
*guard.insert(message, voter);
|
||||
}
|
||||
self.votes.write().insert(message, voter);
|
||||
}
|
||||
|
||||
pub fn seal_signatures(&self, height: Height, round: Round, block_hash: Option<H256>) -> Vec<H520> {
|
||||
@@ -52,13 +50,11 @@ impl VoteCollector {
|
||||
self.seal_signatures(message.height, message.round, message.block_hash)
|
||||
}
|
||||
|
||||
pub fn count_signatures(&self, height: Height, round: Round) -> usize {
|
||||
pub fn count_step_votes(&self, height: Height, round: Round, step: &Step) -> usize {
|
||||
self.votes
|
||||
.read()
|
||||
.keys()
|
||||
// Get only Propose and Precommits.
|
||||
.filter(|m| m.is_round(height, round) && m.step != Step::Prevote)
|
||||
.map(|m| m.signature)
|
||||
.collect()
|
||||
.filter(|m| m.is_step(height, round, step))
|
||||
.count()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,6 +168,8 @@ pub enum BlockError {
|
||||
UnknownParent(H256),
|
||||
/// Uncle parent given is unknown.
|
||||
UnknownUncleParent(H256),
|
||||
/// The same author issued different votes at the same step.
|
||||
DoubleVote(H160),
|
||||
}
|
||||
|
||||
impl fmt::Display for BlockError {
|
||||
@@ -201,6 +203,7 @@ impl fmt::Display for BlockError {
|
||||
RidiculousNumber(ref oob) => format!("Implausible block number. {}", oob),
|
||||
UnknownParent(ref hash) => format!("Unknown parent: {}", hash),
|
||||
UnknownUncleParent(ref hash) => format!("Unknown uncle parent: {}", hash),
|
||||
DoubleVote(ref address) => format!("Author {} issued too many blocks.", address),
|
||||
};
|
||||
|
||||
f.write_fmt(format_args!("Block error ({})", msg))
|
||||
|
||||
@@ -75,7 +75,9 @@ pub struct EthashParams {
|
||||
/// Number of first block where ECIP-1010 begins.
|
||||
pub ecip1010_pause_transition: u64,
|
||||
/// Number of first block where ECIP-1010 ends.
|
||||
pub ecip1010_continue_transition: u64
|
||||
pub ecip1010_continue_transition: u64,
|
||||
/// Maximum amount of code that can be deploying into a contract.
|
||||
pub max_code_size: u64,
|
||||
}
|
||||
|
||||
impl From<ethjson::spec::EthashParams> for EthashParams {
|
||||
@@ -89,19 +91,20 @@ impl From<ethjson::spec::EthashParams> for EthashParams {
|
||||
block_reward: p.block_reward.into(),
|
||||
registrar: p.registrar.map_or_else(Address::new, Into::into),
|
||||
homestead_transition: p.homestead_transition.map_or(0, Into::into),
|
||||
dao_hardfork_transition: p.dao_hardfork_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
dao_hardfork_transition: p.dao_hardfork_transition.map_or(u64::max_value(), Into::into),
|
||||
dao_hardfork_beneficiary: p.dao_hardfork_beneficiary.map_or_else(Address::new, Into::into),
|
||||
dao_hardfork_accounts: p.dao_hardfork_accounts.unwrap_or_else(Vec::new).into_iter().map(Into::into).collect(),
|
||||
difficulty_hardfork_transition: p.difficulty_hardfork_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
difficulty_hardfork_transition: p.difficulty_hardfork_transition.map_or(u64::max_value(), Into::into),
|
||||
difficulty_hardfork_bound_divisor: p.difficulty_hardfork_bound_divisor.map_or(p.difficulty_bound_divisor.into(), Into::into),
|
||||
bomb_defuse_transition: p.bomb_defuse_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
bomb_defuse_transition: p.bomb_defuse_transition.map_or(u64::max_value(), Into::into),
|
||||
eip150_transition: p.eip150_transition.map_or(0, Into::into),
|
||||
eip155_transition: p.eip155_transition.map_or(0, Into::into),
|
||||
eip160_transition: p.eip160_transition.map_or(0, Into::into),
|
||||
eip161abc_transition: p.eip161abc_transition.map_or(0, Into::into),
|
||||
eip161d_transition: p.eip161d_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
ecip1010_pause_transition: p.ecip1010_pause_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
ecip1010_continue_transition: p.ecip1010_continue_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
eip161d_transition: p.eip161d_transition.map_or(u64::max_value(), Into::into),
|
||||
ecip1010_pause_transition: p.ecip1010_pause_transition.map_or(u64::max_value(), Into::into),
|
||||
ecip1010_continue_transition: p.ecip1010_continue_transition.map_or(u64::max_value(), Into::into),
|
||||
max_code_size: p.max_code_size.map_or(u64::max_value(), Into::into),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,6 +157,7 @@ impl Engine for Ethash {
|
||||
Schedule::new_homestead()
|
||||
} else {
|
||||
Schedule::new_post_eip150(
|
||||
self.ethash_params.max_code_size as usize,
|
||||
env_info.number >= self.ethash_params.eip160_transition,
|
||||
env_info.number >= self.ethash_params.eip161abc_transition,
|
||||
env_info.number >= self.ethash_params.eip161d_transition
|
||||
|
||||
@@ -70,6 +70,8 @@ pub struct Schedule {
|
||||
pub quad_coeff_div: usize,
|
||||
/// Cost for contract length when executing `CREATE`
|
||||
pub create_data_gas: usize,
|
||||
/// Maximum code size when creating a contract.
|
||||
pub create_data_limit: usize,
|
||||
/// Transaction cost
|
||||
pub tx_gas: usize,
|
||||
/// `CREATE` transaction cost
|
||||
@@ -111,7 +113,7 @@ impl Schedule {
|
||||
}
|
||||
|
||||
/// Schedule for the post-EIP-150-era of the Ethereum main net.
|
||||
pub fn new_post_eip150(fix_exp: bool, no_empty: bool, kill_empty: bool) -> Schedule {
|
||||
pub fn new_post_eip150(max_code_size: usize, fix_exp: bool, no_empty: bool, kill_empty: bool) -> Schedule {
|
||||
Schedule {
|
||||
exceptional_failed_code_deposit: true,
|
||||
have_delegate_call: true,
|
||||
@@ -139,6 +141,7 @@ impl Schedule {
|
||||
memory_gas: 3,
|
||||
quad_coeff_div: 512,
|
||||
create_data_gas: 200,
|
||||
create_data_limit: max_code_size,
|
||||
tx_gas: 21000,
|
||||
tx_create_gas: 53000,
|
||||
tx_data_zero_gas: 4,
|
||||
@@ -183,6 +186,7 @@ impl Schedule {
|
||||
memory_gas: 3,
|
||||
quad_coeff_div: 512,
|
||||
create_data_gas: 200,
|
||||
create_data_limit: usize::max_value(),
|
||||
tx_gas: 21000,
|
||||
tx_create_gas: tcg,
|
||||
tx_data_zero_gas: 4,
|
||||
|
||||
@@ -242,7 +242,7 @@ impl<'a, T, V> Ext for Externalities<'a, T, V> where T: 'a + Tracer, V: 'a + VMT
|
||||
},
|
||||
OutputPolicy::InitContract(ref mut copy) => {
|
||||
let return_cost = U256::from(data.len()) * U256::from(self.schedule.create_data_gas);
|
||||
if return_cost > *gas {
|
||||
if return_cost > *gas || data.len() > self.schedule.create_data_limit {
|
||||
return match self.schedule.exceptional_failed_code_deposit {
|
||||
true => Err(evm::Error::OutOfGas),
|
||||
false => Ok(*gas)
|
||||
|
||||
@@ -212,7 +212,8 @@ pub struct Miner {
|
||||
sealing_block_last_request: Mutex<u64>,
|
||||
// for sealing...
|
||||
options: MinerOptions,
|
||||
seals_internally: bool,
|
||||
/// Does the node perform internal (without work) sealing.
|
||||
pub seals_internally: bool,
|
||||
|
||||
gas_range_target: RwLock<(U256, U256)>,
|
||||
author: RwLock<Address>,
|
||||
@@ -267,6 +268,11 @@ impl Miner {
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates new instance of miner with accounts and with given spec.
|
||||
pub fn with_spec_and_accounts(spec: &Spec, accounts: Option<Arc<AccountProvider>>) -> Miner {
|
||||
Miner::new_raw(Default::default(), GasPricer::new_fixed(20_000_000_000u64.into()), spec, accounts)
|
||||
}
|
||||
|
||||
/// Creates new instance of miner without accounts, but with given spec.
|
||||
pub fn with_spec(spec: &Spec) -> Miner {
|
||||
Miner::new_raw(Default::default(), GasPricer::new_fixed(20_000_000_000u64.into()), spec, None)
|
||||
@@ -429,6 +435,7 @@ impl Miner {
|
||||
let last_request = *self.sealing_block_last_request.lock();
|
||||
let should_disable_sealing = !self.forced_sealing()
|
||||
&& !has_local_transactions
|
||||
&& !self.seals_internally
|
||||
&& best_block > last_request
|
||||
&& best_block - last_request > SEALING_TIMEOUT_IN_BLOCKS;
|
||||
|
||||
@@ -472,9 +479,10 @@ impl Miner {
|
||||
|
||||
/// Uses Engine to seal the block internally and then imports it to chain.
|
||||
fn seal_and_import_block_internally(&self, chain: &MiningBlockChainClient, block: ClosedBlock) -> bool {
|
||||
if !block.transactions().is_empty() {
|
||||
if !block.transactions().is_empty() || self.forced_sealing() {
|
||||
if let Ok(sealed) = self.seal_block_internally(block) {
|
||||
if chain.import_block(sealed.rlp_bytes()).is_ok() {
|
||||
trace!(target: "miner", "import_block_internally: imported internally sealed block");
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -773,7 +781,7 @@ impl MinerService for Miner {
|
||||
chain: &MiningBlockChainClient,
|
||||
transactions: Vec<SignedTransaction>
|
||||
) -> Vec<Result<TransactionImportResult, Error>> {
|
||||
|
||||
trace!(target: "external_tx", "Importing external transactions");
|
||||
let results = {
|
||||
let mut transaction_queue = self.transaction_queue.lock();
|
||||
self.add_transactions_to_queue(
|
||||
|
||||
@@ -48,6 +48,8 @@ pub enum ClientIoMessage {
|
||||
FeedBlockChunk(H256, Bytes),
|
||||
/// Take a snapshot for the block with given number.
|
||||
TakeSnapshot(u64),
|
||||
/// Trigger sealing update (useful for internal sealing).
|
||||
UpdateSealing,
|
||||
}
|
||||
|
||||
/// Client service setup. Creates and registers client and network services with the IO subsystem.
|
||||
@@ -111,6 +113,8 @@ impl ClientService {
|
||||
});
|
||||
try!(io_service.register_handler(client_io));
|
||||
|
||||
spec.engine.register_message_channel(io_service.channel());
|
||||
|
||||
let stop_guard = ::devtools::StopGuard::new();
|
||||
run_ipc(ipc_path, client.clone(), snapshot.clone(), stop_guard.share());
|
||||
|
||||
@@ -213,8 +217,11 @@ impl IoHandler<ClientIoMessage> for ClientIoHandler {
|
||||
if let Err(e) = res {
|
||||
debug!(target: "snapshot", "Failed to initialize periodic snapshot thread: {:?}", e);
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
ClientIoMessage::UpdateSealing => {
|
||||
trace!(target: "authorityround", "message: UpdateSealing");
|
||||
self.client.update_sealing()
|
||||
},
|
||||
_ => {} // ignore other messages
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
use util::*;
|
||||
use builtin::Builtin;
|
||||
use engines::{Engine, NullEngine, InstantSeal, BasicAuthority, Tendermint};
|
||||
use engines::{Engine, NullEngine, InstantSeal, BasicAuthority, AuthorityRound, Tendermint};
|
||||
use pod_state::*;
|
||||
use account_db::*;
|
||||
use header::{BlockNumber, Header};
|
||||
@@ -150,6 +150,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::AuthorityRound(authority_round) => AuthorityRound::new(params, From::from(authority_round.params), builtins).expect("Failed to start AuthorityRound consensus engine."),
|
||||
ethjson::spec::Engine::Tendermint(tendermint) => Tendermint::new(params, From::from(tendermint.params), builtins).expect("Failed to start the Tendermint consensus engine."),
|
||||
}
|
||||
}
|
||||
@@ -282,6 +283,10 @@ impl Spec {
|
||||
/// Create a new Spec with InstantSeal consensus which does internal sealing (not requiring work).
|
||||
pub fn new_instant() -> Spec { load_bundled!("instant_seal") }
|
||||
|
||||
/// Create a new Spec with AuthorityRound consensus which does internal sealing (not requiring work).
|
||||
/// Accounts with secrets "1".sha3() and "2".sha3() are the authorities.
|
||||
pub fn new_test_round() -> Self { load_bundled!("authority_round") }
|
||||
|
||||
/// Create a new Spec with Tendermint consensus which does internal sealing (not requiring work).
|
||||
/// Account "0".sha3() and "1".sha3() are a authorities.
|
||||
pub fn new_test_tendermint() -> Self { load_bundled!("tendermint") }
|
||||
|
||||
@@ -433,18 +433,19 @@ pub fn get_default_ethash_params() -> EthashParams{
|
||||
block_reward: U256::from(0),
|
||||
registrar: "0000000000000000000000000000000000000001".into(),
|
||||
homestead_transition: 1150000,
|
||||
dao_hardfork_transition: 0x7fffffffffffffff,
|
||||
dao_hardfork_transition: u64::max_value(),
|
||||
dao_hardfork_beneficiary: "0000000000000000000000000000000000000001".into(),
|
||||
dao_hardfork_accounts: vec![],
|
||||
difficulty_hardfork_transition: 0x7fffffffffffffff,
|
||||
difficulty_hardfork_transition: u64::max_value(),
|
||||
difficulty_hardfork_bound_divisor: U256::from(0),
|
||||
bomb_defuse_transition: 0x7fffffffffffffff,
|
||||
eip150_transition: 0x7fffffffffffffff,
|
||||
eip155_transition: 0x7fffffffffffffff,
|
||||
eip160_transition: 0x7fffffffffffffff,
|
||||
eip161abc_transition: 0x7fffffffffffffff,
|
||||
eip161d_transition: 0x7fffffffffffffff,
|
||||
ecip1010_pause_transition: 0x7fffffffffffffff,
|
||||
ecip1010_continue_transition: 0x7fffffffffffffff
|
||||
bomb_defuse_transition: u64::max_value(),
|
||||
eip150_transition: u64::max_value(),
|
||||
eip155_transition: u64::max_value(),
|
||||
eip160_transition: u64::max_value(),
|
||||
eip161abc_transition: u64::max_value(),
|
||||
eip161d_transition: u64::max_value(),
|
||||
ecip1010_pause_transition: u64::max_value(),
|
||||
ecip1010_continue_transition: u64::max_value(),
|
||||
max_code_size: u64::max_value(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user