better proposal block handling
This commit is contained in:
@@ -25,7 +25,7 @@ use rlp::{UntrustedRlp, Rlp, View, encode};
|
||||
use account_provider::AccountProvider;
|
||||
use block::*;
|
||||
use spec::CommonParams;
|
||||
use engines::{Engine, EngineError};
|
||||
use engines::{Engine, Seal, EngineError};
|
||||
use header::Header;
|
||||
use error::{Error, BlockError};
|
||||
use blockchain::extras::BlockDetails;
|
||||
@@ -218,8 +218,8 @@ impl Engine for AuthorityRound {
|
||||
///
|
||||
/// This operation is synchronous and may (quite reasonably) not be available, in which `false` will
|
||||
/// be returned.
|
||||
fn generate_seal(&self, block: &ExecutedBlock) -> Option<Vec<Bytes>> {
|
||||
if self.proposed.load(AtomicOrdering::SeqCst) { return None; }
|
||||
fn generate_seal(&self, block: &ExecutedBlock) -> Seal {
|
||||
if self.proposed.load(AtomicOrdering::SeqCst) { return Seal::None; }
|
||||
let header = block.header();
|
||||
let step = self.step();
|
||||
if self.is_step_proposer(step, header.author()) {
|
||||
@@ -228,7 +228,8 @@ impl Engine for AuthorityRound {
|
||||
if let Ok(signature) = ap.sign(*header.author(), self.password.read().clone(), 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()]);
|
||||
let rlps = vec![encode(&step).to_vec(), encode(&(&*signature as &[u8])).to_vec()];
|
||||
return Seal::Regular(rlps);
|
||||
} else {
|
||||
warn!(target: "poa", "generate_seal: FAIL: Accounts secret key unavailable.");
|
||||
}
|
||||
@@ -236,7 +237,7 @@ impl Engine for AuthorityRound {
|
||||
warn!(target: "poa", "generate_seal: FAIL: Accounts not provided.");
|
||||
}
|
||||
}
|
||||
None
|
||||
Seal::None
|
||||
}
|
||||
|
||||
/// Check the number of seal fields.
|
||||
@@ -339,6 +340,7 @@ mod tests {
|
||||
use account_provider::AccountProvider;
|
||||
use spec::Spec;
|
||||
use std::time::UNIX_EPOCH;
|
||||
use engines::Seal;
|
||||
|
||||
#[test]
|
||||
fn has_valid_metadata() {
|
||||
@@ -408,17 +410,17 @@ mod tests {
|
||||
let b2 = b2.close_and_lock();
|
||||
|
||||
engine.set_signer(addr1, "1".into());
|
||||
if let Some(seal) = engine.generate_seal(b1.block()) {
|
||||
if let Seal::Regular(seal) = engine.generate_seal(b1.block()) {
|
||||
assert!(b1.clone().try_seal(engine, seal).is_ok());
|
||||
// Second proposal is forbidden.
|
||||
assert!(engine.generate_seal(b1.block()).is_none());
|
||||
assert!(engine.generate_seal(b1.block()) == Seal::None);
|
||||
}
|
||||
|
||||
engine.set_signer(addr2, "2".into());
|
||||
if let Some(seal) = engine.generate_seal(b2.block()) {
|
||||
if let Seal::Regular(seal) = engine.generate_seal(b2.block()) {
|
||||
assert!(b2.clone().try_seal(engine, seal).is_ok());
|
||||
// Second proposal is forbidden.
|
||||
assert!(engine.generate_seal(b2.block()).is_none());
|
||||
assert!(engine.generate_seal(b2.block()) == Seal::None);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ use account_provider::AccountProvider;
|
||||
use block::*;
|
||||
use builtin::Builtin;
|
||||
use spec::CommonParams;
|
||||
use engines::Engine;
|
||||
use engines::{Engine, Seal};
|
||||
use env_info::EnvInfo;
|
||||
use error::{BlockError, Error};
|
||||
use evm::Schedule;
|
||||
@@ -112,20 +112,20 @@ impl Engine for BasicAuthority {
|
||||
///
|
||||
/// This operation is synchronous and may (quite reasonably) not be available, in which `false` will
|
||||
/// be returned.
|
||||
fn generate_seal(&self, block: &ExecutedBlock) -> Option<Vec<Bytes>> {
|
||||
fn generate_seal(&self, block: &ExecutedBlock) -> Seal {
|
||||
if let Some(ref ap) = *self.account_provider.lock() {
|
||||
let header = block.header();
|
||||
let message = header.bare_hash();
|
||||
// account should be pernamently unlocked, otherwise sealing will fail
|
||||
if let Ok(signature) = ap.sign(*block.header().author(), self.password.read().clone(), message) {
|
||||
return Some(vec![::rlp::encode(&(&*signature as &[u8])).to_vec()]);
|
||||
return Seal::Regular(vec![::rlp::encode(&(&*signature as &[u8])).to_vec()]);
|
||||
} else {
|
||||
trace!(target: "basicauthority", "generate_seal: FAIL: accounts secret key unavailable");
|
||||
}
|
||||
} else {
|
||||
trace!(target: "basicauthority", "generate_seal: FAIL: accounts not provided");
|
||||
}
|
||||
None
|
||||
Seal::None
|
||||
}
|
||||
|
||||
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
|
||||
@@ -199,6 +199,7 @@ mod tests {
|
||||
use account_provider::AccountProvider;
|
||||
use header::Header;
|
||||
use spec::Spec;
|
||||
use engines::Seal;
|
||||
|
||||
/// Create a new test chain spec with `BasicAuthority` consensus engine.
|
||||
fn new_test_authority() -> Spec {
|
||||
@@ -269,8 +270,9 @@ mod tests {
|
||||
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
||||
let b = OpenBlock::new(engine, 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()).unwrap();
|
||||
assert!(b.try_seal(engine, seal).is_ok());
|
||||
if let Seal::Regular(seal) = engine.generate_seal(b.block()) {
|
||||
assert!(b.try_seal(engine, seal).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -17,12 +17,11 @@
|
||||
use std::collections::BTreeMap;
|
||||
use util::Address;
|
||||
use builtin::Builtin;
|
||||
use engines::Engine;
|
||||
use engines::{Engine, Seal};
|
||||
use env_info::EnvInfo;
|
||||
use spec::CommonParams;
|
||||
use evm::Schedule;
|
||||
use block::ExecutedBlock;
|
||||
use util::Bytes;
|
||||
|
||||
/// An engine which does not provide any consensus mechanism, just seals blocks internally.
|
||||
pub struct InstantSeal {
|
||||
@@ -59,8 +58,8 @@ impl Engine for InstantSeal {
|
||||
|
||||
fn is_sealer(&self, _author: &Address) -> Option<bool> { Some(true) }
|
||||
|
||||
fn generate_seal(&self, _block: &ExecutedBlock) -> Option<Vec<Bytes>> {
|
||||
Some(Vec::new())
|
||||
fn generate_seal(&self, _block: &ExecutedBlock) -> Seal {
|
||||
Seal::Regular(Vec::new())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,6 +71,7 @@ mod tests {
|
||||
use spec::Spec;
|
||||
use header::Header;
|
||||
use block::*;
|
||||
use engines::Seal;
|
||||
|
||||
#[test]
|
||||
fn instant_can_seal() {
|
||||
@@ -84,8 +84,9 @@ mod tests {
|
||||
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
||||
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::default(), (3141562.into(), 31415620.into()), vec![]).unwrap();
|
||||
let b = b.close_and_lock();
|
||||
let seal = engine.generate_seal(b.block()).unwrap();
|
||||
assert!(b.try_seal(engine, seal).is_ok());
|
||||
if let Seal::Regular(seal) = engine.generate_seal(b.block()) {
|
||||
assert!(b.try_seal(engine, seal).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -49,11 +49,11 @@ use views::HeaderView;
|
||||
#[derive(Debug)]
|
||||
pub enum EngineError {
|
||||
/// Signature does not belong to an authority.
|
||||
NotAuthorized(H160),
|
||||
NotAuthorized(Address),
|
||||
/// The same author issued different votes at the same step.
|
||||
DoubleVote(H160),
|
||||
DoubleVote(Address),
|
||||
/// The received block is from an incorrect proposer.
|
||||
NotProposer(Mismatch<H160>),
|
||||
NotProposer(Mismatch<Address>),
|
||||
/// Message was not expected.
|
||||
UnexpectedMessage,
|
||||
/// Seal field has an unexpected size.
|
||||
@@ -75,6 +75,17 @@ impl fmt::Display for EngineError {
|
||||
}
|
||||
}
|
||||
|
||||
/// Seal type.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum Seal {
|
||||
/// Proposal seal; should be broadcasted, but not inserted into blockchain.
|
||||
Proposal(Vec<Bytes>),
|
||||
/// Regular block seal; should be part of the blockchain.
|
||||
Regular(Vec<Bytes>),
|
||||
/// Engine does generate seal for this block right now.
|
||||
None,
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub trait Engine : Sync + Send {
|
||||
@@ -127,7 +138,7 @@ pub trait Engine : Sync + Send {
|
||||
///
|
||||
/// This operation is synchronous and may (quite reasonably) not be available, in which None will
|
||||
/// be returned.
|
||||
fn generate_seal(&self, _block: &ExecutedBlock) -> Option<Vec<Bytes>> { None }
|
||||
fn generate_seal(&self, _block: &ExecutedBlock) -> Seal { Seal::None }
|
||||
|
||||
/// Phase 1 quick block verification. Only does checks that are cheap. `block` (the header's full block)
|
||||
/// may be provided for additional checks. Returns either a null `Ok` or a general error detailing the problem with import.
|
||||
@@ -189,6 +200,10 @@ pub trait Engine : Sync + Send {
|
||||
ethash::is_new_best_block(best_total_difficulty, parent_details, new_header)
|
||||
}
|
||||
|
||||
/// Find out if the block is a proposal block and should not be inserted into the DB.
|
||||
/// Takes a header of a fully verified block.
|
||||
fn is_proposal(&self, _verified_header: &Header) -> bool { false }
|
||||
|
||||
/// Register an account which signs consensus messages.
|
||||
fn set_signer(&self, _address: Address, _password: String) {}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ use ethkey::{recover, public_to_address};
|
||||
use account_provider::AccountProvider;
|
||||
use block::*;
|
||||
use spec::CommonParams;
|
||||
use engines::{Engine, EngineError};
|
||||
use engines::{Engine, Seal, EngineError};
|
||||
use blockchain::extras::BlockDetails;
|
||||
use views::HeaderView;
|
||||
use evm::Schedule;
|
||||
@@ -408,14 +408,14 @@ impl Engine for Tendermint {
|
||||
Some(self.is_authority(address))
|
||||
}
|
||||
|
||||
/// Attempt to seal the block internally using all available signatures.
|
||||
fn generate_seal(&self, block: &ExecutedBlock) -> Option<Vec<Bytes>> {
|
||||
/// Attempt to seal generate a proposal seal.
|
||||
fn generate_seal(&self, block: &ExecutedBlock) -> Seal {
|
||||
if let Some(ref ap) = *self.account_provider.lock() {
|
||||
let header = block.header();
|
||||
let author = header.author();
|
||||
// Only proposer can generate seal if None was generated.
|
||||
if self.is_proposer(author).is_err() && self.proposal.read().is_none() {
|
||||
return None;
|
||||
return Seal::None;
|
||||
}
|
||||
|
||||
let height = header.number() as Height;
|
||||
@@ -428,18 +428,18 @@ impl Engine for Tendermint {
|
||||
self.votes.vote(ConsensusMessage::new(signature, height, round, Step::Propose, bh), *author);
|
||||
// Remember proposal for later seal submission.
|
||||
*self.proposal.write() = bh;
|
||||
Some(vec![
|
||||
Seal::Proposal(vec![
|
||||
::rlp::encode(&round).to_vec(),
|
||||
::rlp::encode(&signature).to_vec(),
|
||||
::rlp::EMPTY_LIST_RLP.to_vec()
|
||||
])
|
||||
} else {
|
||||
warn!(target: "poa", "generate_seal: FAIL: accounts secret key unavailable");
|
||||
None
|
||||
Seal::None
|
||||
}
|
||||
} else {
|
||||
warn!(target: "poa", "generate_seal: FAIL: accounts not provided");
|
||||
None
|
||||
Seal::None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -526,11 +526,6 @@ impl Engine for Tendermint {
|
||||
})));
|
||||
}
|
||||
try!(self.is_round_proposer(proposal.height, proposal.round, &proposer));
|
||||
if self.is_round(&proposal) {
|
||||
debug!(target: "poa", "Received a new proposal for height {}, round {} from {}.", proposal.height, proposal.round, proposer);
|
||||
*self.proposal.write() = proposal.block_hash.clone();
|
||||
self.votes.vote(proposal, proposer);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -547,17 +542,6 @@ impl Engine for Tendermint {
|
||||
try!(Err(BlockError::InvalidGasLimit(OutOfBounds { min: Some(min_gas), max: Some(max_gas), found: header.gas_limit().clone() })));
|
||||
}
|
||||
|
||||
// Commit is longer than empty signature list.
|
||||
let parent_signature_len = parent.seal()[2].len();
|
||||
if parent_signature_len <= 1 {
|
||||
try!(Err(EngineError::BadSealFieldSize(OutOfBounds {
|
||||
// One signature.
|
||||
min: Some(69),
|
||||
max: None,
|
||||
found: parent_signature_len
|
||||
})));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -599,6 +583,22 @@ impl Engine for Tendermint {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_proposal(&self, header: &Header) -> bool {
|
||||
let signatures_len = header.seal()[2].len();
|
||||
// Signatures have to be an empty list rlp.
|
||||
if signatures_len != 1 {
|
||||
return false;
|
||||
}
|
||||
let proposal = ConsensusMessage::new_proposal(header).expect("block went through full verification; this Engine verifies new_proposal creation; qed");
|
||||
let proposer = proposal.verify().expect("block went through full verification; this Engine tries verify; qed");
|
||||
debug!(target: "poa", "Received a new proposal for height {}, round {} from {}.", proposal.height, proposal.round, proposer);
|
||||
if self.is_round(&proposal) {
|
||||
*self.proposal.write() = proposal.block_hash.clone();
|
||||
}
|
||||
self.votes.vote(proposal, proposer);
|
||||
true
|
||||
}
|
||||
|
||||
fn register_message_channel(&self, message_channel: IoChannel<ClientIoMessage>) {
|
||||
trace!(target: "poa", "register_message_channel: Register the IoChannel.");
|
||||
*self.message_channel.lock() = Some(message_channel);
|
||||
@@ -624,7 +624,7 @@ mod tests {
|
||||
use io::IoService;
|
||||
use service::ClientIoMessage;
|
||||
use spec::Spec;
|
||||
use engines::{Engine, EngineError};
|
||||
use engines::{Engine, EngineError, Seal};
|
||||
use super::*;
|
||||
use super::message::*;
|
||||
|
||||
@@ -644,8 +644,11 @@ mod tests {
|
||||
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
||||
let b = OpenBlock::new(spec.engine.as_ref(), Default::default(), false, db.boxed_clone(), &genesis_header, last_hashes, proposer, (3141562.into(), 31415620.into()), vec![]).unwrap();
|
||||
let b = b.close_and_lock();
|
||||
let seal = spec.engine.generate_seal(b.block()).unwrap();
|
||||
(b, seal)
|
||||
if let Seal::Proposal(seal) = spec.engine.generate_seal(b.block()) {
|
||||
(b, seal)
|
||||
} else {
|
||||
panic!()
|
||||
}
|
||||
}
|
||||
|
||||
fn vote<F>(engine: &Arc<Engine>, signer: F, height: usize, round: usize, step: Step, block_hash: Option<H256>) where F: FnOnce(H256) -> Result<H520, ::account_provider::Error> {
|
||||
@@ -737,6 +740,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn allows_correct_proposer() {
|
||||
let (spec, tap) = setup();
|
||||
let engine = spec.engine;
|
||||
@@ -825,7 +829,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn step_transitioning() {
|
||||
::env_logger::init().unwrap();
|
||||
//::env_logger::init().unwrap();
|
||||
let (spec, tap) = setup();
|
||||
let engine = spec.engine.clone();
|
||||
let mut db_result = get_temp_state_db();
|
||||
|
||||
Reference in New Issue
Block a user