2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-05-03 17:23:53 +02:00
|
|
|
// 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 basic, non-BFT proof-of-authority.
|
|
|
|
|
2017-01-10 12:23:59 +01:00
|
|
|
use std::sync::Weak;
|
|
|
|
use util::*;
|
2017-01-24 10:03:58 +01:00
|
|
|
use ethkey::{recover, public_to_address, Signature};
|
2016-06-20 00:10:34 +02:00
|
|
|
use account_provider::AccountProvider;
|
2016-05-03 17:23:53 +02:00
|
|
|
use block::*;
|
2016-10-24 18:35:25 +02:00
|
|
|
use builtin::Builtin;
|
2016-07-28 20:32:20 +02:00
|
|
|
use spec::CommonParams;
|
2017-04-18 14:19:10 +02:00
|
|
|
use engines::{Engine, EngineError, Seal, Call, EpochChange};
|
2016-10-24 18:35:25 +02:00
|
|
|
use error::{BlockError, Error};
|
2016-05-19 00:44:49 +02:00
|
|
|
use evm::Schedule;
|
2016-05-03 17:23:53 +02:00
|
|
|
use ethjson;
|
2017-04-19 14:30:00 +02:00
|
|
|
use header::{Header, BlockNumber};
|
2017-01-10 12:23:59 +01:00
|
|
|
use client::Client;
|
2017-01-20 13:25:17 +01:00
|
|
|
use super::signer::EngineSigner;
|
2017-04-13 20:24:21 +02:00
|
|
|
use super::validator_set::{ValidatorSet, SimpleList, new_validator_set};
|
2016-05-03 17:23:53 +02:00
|
|
|
|
2016-05-04 15:22:22 +02:00
|
|
|
/// `BasicAuthority` params.
|
2016-05-03 17:23:53 +02:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct BasicAuthorityParams {
|
|
|
|
/// Gas limit divisor.
|
|
|
|
pub gas_limit_bound_divisor: U256,
|
|
|
|
/// Valid signatories.
|
2017-01-10 12:23:59 +01:00
|
|
|
pub validators: ethjson::spec::ValidatorSet,
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ethjson::spec::BasicAuthorityParams> for BasicAuthorityParams {
|
|
|
|
fn from(p: ethjson::spec::BasicAuthorityParams) -> Self {
|
|
|
|
BasicAuthorityParams {
|
|
|
|
gas_limit_bound_divisor: p.gas_limit_bound_divisor.into(),
|
2017-01-10 12:23:59 +01:00
|
|
|
validators: p.validators,
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 14:19:10 +02:00
|
|
|
struct EpochVerifier {
|
2017-04-19 14:58:19 +02:00
|
|
|
epoch_number: u64,
|
2017-04-18 14:19:10 +02:00
|
|
|
list: SimpleList,
|
|
|
|
}
|
2017-04-13 20:24:21 +02:00
|
|
|
|
2017-04-18 14:19:10 +02:00
|
|
|
impl super::EpochVerifier for EpochVerifier {
|
2017-04-19 14:58:19 +02:00
|
|
|
fn epoch_number(&self) -> u64 { self.epoch_number.clone() }
|
2017-04-13 20:24:21 +02:00
|
|
|
fn verify_light(&self, header: &Header) -> Result<(), Error> {
|
2017-04-18 14:19:10 +02:00
|
|
|
verify_external(header, &self.list)
|
2017-04-13 20:24:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify_external(header: &Header, validators: &ValidatorSet) -> Result<(), Error> {
|
|
|
|
use rlp::UntrustedRlp;
|
|
|
|
|
|
|
|
// Check if the signature belongs to a validator, can depend on parent state.
|
|
|
|
let sig = UntrustedRlp::new(&header.seal()[0]).as_val::<H520>()?;
|
|
|
|
let signer = public_to_address(&recover(&sig.into(), &header.bare_hash())?);
|
|
|
|
|
|
|
|
match validators.contains(header.parent_hash(), &signer) {
|
|
|
|
false => Err(BlockError::InvalidSeal.into()),
|
|
|
|
true => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 18:55:38 +02:00
|
|
|
/// Engine using `BasicAuthority`, trivial proof-of-authority consensus.
|
2016-05-03 17:23:53 +02:00
|
|
|
pub struct BasicAuthority {
|
|
|
|
params: CommonParams,
|
2017-01-10 12:23:59 +01:00
|
|
|
gas_limit_bound_divisor: U256,
|
2016-05-03 17:23:53 +02:00
|
|
|
builtins: BTreeMap<Address, Builtin>,
|
2017-01-20 13:25:17 +01:00
|
|
|
signer: EngineSigner,
|
2017-03-23 13:19:28 +01:00
|
|
|
validators: Box<ValidatorSet>,
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BasicAuthority {
|
|
|
|
/// Create a new instance of BasicAuthority engine
|
|
|
|
pub fn new(params: CommonParams, our_params: BasicAuthorityParams, builtins: BTreeMap<Address, Builtin>) -> Self {
|
|
|
|
BasicAuthority {
|
|
|
|
params: params,
|
2017-01-10 12:23:59 +01:00
|
|
|
gas_limit_bound_divisor: our_params.gas_limit_bound_divisor,
|
2016-05-03 17:23:53 +02:00
|
|
|
builtins: builtins,
|
2017-01-10 12:23:59 +01:00
|
|
|
validators: new_validator_set(our_params.validators),
|
2017-01-20 13:25:17 +01:00
|
|
|
signer: Default::default(),
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Engine for BasicAuthority {
|
|
|
|
fn name(&self) -> &str { "BasicAuthority" }
|
|
|
|
fn version(&self) -> SemanticVersion { SemanticVersion::new(1, 0, 0) }
|
|
|
|
// One field - the signature
|
|
|
|
fn seal_fields(&self) -> usize { 1 }
|
|
|
|
|
|
|
|
fn params(&self) -> &CommonParams { &self.params }
|
|
|
|
fn builtins(&self) -> &BTreeMap<Address, Builtin> { &self.builtins }
|
|
|
|
|
|
|
|
/// Additional engine-specific information for the user/developer concerning `header`.
|
2016-11-04 17:35:02 +01:00
|
|
|
fn extra_info(&self, _header: &Header) -> BTreeMap<String, String> { map!["signature".to_owned() => "TODO".to_owned()] }
|
2016-05-03 17:23:53 +02:00
|
|
|
|
2017-04-19 14:30:00 +02:00
|
|
|
fn schedule(&self, _block_number: BlockNumber) -> Schedule {
|
2016-05-03 17:23:53 +02:00
|
|
|
Schedule::new_homestead()
|
|
|
|
}
|
|
|
|
|
2016-06-23 14:29:16 +02:00
|
|
|
fn populate_from_parent(&self, header: &mut Header, parent: &Header, gas_floor_target: U256, _gas_ceil_target: U256) {
|
2016-08-29 11:35:24 +02:00
|
|
|
header.set_difficulty(parent.difficulty().clone());
|
|
|
|
header.set_gas_limit({
|
|
|
|
let gas_limit = parent.gas_limit().clone();
|
2017-01-10 12:23:59 +01:00
|
|
|
let bound_divisor = self.gas_limit_bound_divisor;
|
2016-05-03 17:23:53 +02:00
|
|
|
if gas_limit < gas_floor_target {
|
2016-05-31 16:59:01 +02:00
|
|
|
min(gas_floor_target, gas_limit + gas_limit / bound_divisor - 1.into())
|
2016-05-03 17:23:53 +02:00
|
|
|
} else {
|
2016-05-31 16:59:01 +02:00
|
|
|
max(gas_floor_target, gas_limit - gas_limit / bound_divisor + 1.into())
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
2016-08-29 11:35:24 +02:00
|
|
|
});
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
2017-02-20 16:35:53 +01:00
|
|
|
fn seals_internally(&self) -> Option<bool> {
|
2017-03-08 14:41:24 +01:00
|
|
|
Some(self.signer.address() != Address::default())
|
2016-09-14 00:00:26 +02:00
|
|
|
}
|
2016-09-12 11:07:40 +02:00
|
|
|
|
2016-05-03 17:23:53 +02:00
|
|
|
/// Attempt to seal the block internally.
|
2016-12-08 12:03:34 +01:00
|
|
|
fn generate_seal(&self, block: &ExecutedBlock) -> Seal {
|
2017-01-18 18:49:50 +01:00
|
|
|
let header = block.header();
|
|
|
|
let author = header.author();
|
2017-03-08 14:41:24 +01:00
|
|
|
if self.validators.contains(header.parent_hash(), author) {
|
2017-01-18 18:49:50 +01:00
|
|
|
// account should be pernamently unlocked, otherwise sealing will fail
|
2017-01-20 13:25:17 +01:00
|
|
|
if let Ok(signature) = self.signer.sign(header.bare_hash()) {
|
|
|
|
return Seal::Regular(vec![::rlp::encode(&(&H520::from(signature) as &[u8])).to_vec()]);
|
2017-01-18 18:49:50 +01:00
|
|
|
} else {
|
|
|
|
trace!(target: "basicauthority", "generate_seal: FAIL: accounts secret key unavailable");
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-08 12:03:34 +01:00
|
|
|
Seal::None
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
|
|
|
|
// check the seal fields.
|
|
|
|
// TODO: pull this out into common code.
|
2016-08-29 11:35:24 +02:00
|
|
|
if header.seal().len() != self.seal_fields() {
|
2016-05-03 17:23:53 +02:00
|
|
|
return Err(From::from(BlockError::InvalidSealArity(
|
2016-08-29 11:35:24 +02:00
|
|
|
Mismatch { expected: self.seal_fields(), found: header.seal().len() }
|
2016-05-03 17:23:53 +02:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:41:24 +01:00
|
|
|
fn verify_block_unordered(&self, _header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-09-01 14:29:59 +02:00
|
|
|
|
2017-03-08 14:41:24 +01:00
|
|
|
fn verify_block_family(&self, header: &Header, parent: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
|
|
|
|
// Do not calculate difficulty for genesis blocks.
|
2016-05-03 17:23:53 +02:00
|
|
|
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() })))
|
|
|
|
}
|
2017-01-10 12:23:59 +01:00
|
|
|
let gas_limit_divisor = self.gas_limit_bound_divisor;
|
2016-08-29 11:35:24 +02:00
|
|
|
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() })));
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:24:21 +02:00
|
|
|
fn verify_block_external(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
|
|
|
|
verify_external(header, &*self.validators)
|
|
|
|
}
|
|
|
|
|
2017-04-12 18:55:38 +02:00
|
|
|
// the proofs we need just allow us to get the full validator set.
|
2017-04-18 14:19:10 +02:00
|
|
|
fn epoch_proof(&self, header: &Header, caller: &Call) -> Result<Bytes, Error> {
|
|
|
|
self.validators.epoch_proof(header, caller)
|
2017-04-12 18:55:38 +02:00
|
|
|
.map_err(|e| EngineError::InsufficientProof(e).into())
|
|
|
|
}
|
|
|
|
|
2017-04-18 14:19:10 +02:00
|
|
|
fn is_epoch_end(&self, header: &Header, block: Option<&[u8]>, receipts: Option<&[::receipt::Receipt]>)
|
|
|
|
-> EpochChange
|
2017-04-12 18:55:38 +02:00
|
|
|
{
|
2017-04-18 14:19:10 +02:00
|
|
|
self.validators.is_epoch_end(header, block, receipts)
|
2017-04-12 18:55:38 +02:00
|
|
|
}
|
|
|
|
|
2017-04-18 14:19:10 +02:00
|
|
|
fn epoch_verifier(&self, header: &Header, proof: &[u8]) -> Result<Box<super::EpochVerifier>, Error> {
|
2017-04-13 20:24:21 +02:00
|
|
|
// extract a simple list from the proof.
|
2017-04-18 14:19:10 +02:00
|
|
|
let (num, simple_list) = self.validators.epoch_set(header, proof)?;
|
2017-04-13 20:24:21 +02:00
|
|
|
|
2017-04-18 14:19:10 +02:00
|
|
|
Ok(Box::new(EpochVerifier {
|
|
|
|
epoch_number: num,
|
|
|
|
list: simple_list,
|
|
|
|
}))
|
2017-04-13 20:24:21 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 12:23:59 +01:00
|
|
|
fn register_client(&self, client: Weak<Client>) {
|
2017-01-24 10:03:58 +01:00
|
|
|
self.validators.register_contract(client);
|
2017-01-10 12:23:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 13:25:17 +01:00
|
|
|
fn set_signer(&self, ap: Arc<AccountProvider>, address: Address, password: String) {
|
|
|
|
self.signer.set(ap, address, password);
|
2016-12-05 18:08:16 +01:00
|
|
|
}
|
2017-01-24 10:03:58 +01:00
|
|
|
|
|
|
|
fn sign(&self, hash: H256) -> Result<Signature, Error> {
|
|
|
|
self.signer.sign(hash).map_err(Into::into)
|
|
|
|
}
|
2017-05-17 12:41:33 +02:00
|
|
|
|
|
|
|
fn snapshot_components(&self) -> Option<Box<::snapshot::SnapshotComponents>> {
|
|
|
|
Some(Box::new(::snapshot::PoaSnapshot))
|
|
|
|
}
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-10-24 18:35:25 +02:00
|
|
|
use util::*;
|
2016-05-03 17:23:53 +02:00
|
|
|
use block::*;
|
2016-10-24 18:35:25 +02:00
|
|
|
use error::{BlockError, Error};
|
2016-05-03 17:23:53 +02:00
|
|
|
use tests::helpers::*;
|
2016-06-20 00:10:34 +02:00
|
|
|
use account_provider::AccountProvider;
|
2016-10-24 18:35:25 +02:00
|
|
|
use header::Header;
|
2016-07-28 20:32:20 +02:00
|
|
|
use spec::Spec;
|
2016-12-08 12:03:34 +01:00
|
|
|
use engines::Seal;
|
2016-07-28 20:32:20 +02:00
|
|
|
|
|
|
|
/// Create a new test chain spec with `BasicAuthority` consensus engine.
|
2016-09-05 17:41:34 +02:00
|
|
|
fn new_test_authority() -> Spec {
|
2016-09-08 16:27:54 +02:00
|
|
|
let bytes: &[u8] = include_bytes!("../../res/basic_authority.json");
|
2016-09-05 17:41:34 +02:00
|
|
|
Spec::load(bytes).expect("invalid chain spec")
|
|
|
|
}
|
2016-05-03 17:23:53 +02:00
|
|
|
|
|
|
|
#[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;
|
2017-04-19 14:30:00 +02:00
|
|
|
let schedule = engine.schedule(10000000);
|
2016-05-03 17:23:53 +02:00
|
|
|
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();
|
2016-09-01 14:29:59 +02:00
|
|
|
header.set_seal(vec![::rlp::encode(&H520::default()).to_vec()]);
|
2016-05-03 17:23:53 +02:00
|
|
|
|
2017-03-08 14:41:24 +01:00
|
|
|
let verify_result = engine.verify_block_family(&header, &Default::default(), None);
|
2016-08-24 18:35:21 +02:00
|
|
|
assert!(verify_result.is_err());
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_generate_seal() {
|
2016-06-20 00:10:34 +02:00
|
|
|
let tap = AccountProvider::transient_provider();
|
2017-05-19 17:06:36 +02:00
|
|
|
let addr = tap.insert_account("".sha3().into(), "").unwrap();
|
2016-05-03 17:23:53 +02:00
|
|
|
|
|
|
|
let spec = new_test_authority();
|
2016-08-10 16:29:40 +02:00
|
|
|
let engine = &*spec.engine;
|
2017-01-18 18:49:50 +01:00
|
|
|
engine.set_signer(Arc::new(tap), addr, "".into());
|
2016-05-03 17:23:53 +02:00
|
|
|
let genesis_header = spec.genesis_header();
|
2017-04-06 19:26:17 +02:00
|
|
|
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
|
2016-08-03 22:03:40 +02:00
|
|
|
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
2016-08-24 16:53:36 +02:00
|
|
|
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, addr, (3141562.into(), 31415620.into()), vec![]).unwrap();
|
2016-05-03 17:23:53 +02:00
|
|
|
let b = b.close_and_lock();
|
2016-12-08 12:03:34 +01:00
|
|
|
if let Seal::Regular(seal) = engine.generate_seal(b.block()) {
|
2017-04-12 14:41:19 +02:00
|
|
|
assert!(b.try_seal(engine, seal).is_ok());
|
2016-12-08 12:03:34 +01:00
|
|
|
}
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
2016-09-14 00:00:26 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn seals_internally() {
|
|
|
|
let tap = AccountProvider::transient_provider();
|
2017-05-19 17:06:36 +02:00
|
|
|
let authority = tap.insert_account("".sha3().into(), "").unwrap();
|
2016-09-14 00:00:26 +02:00
|
|
|
|
|
|
|
let engine = new_test_authority().engine;
|
2017-02-20 16:35:53 +01:00
|
|
|
assert!(!engine.seals_internally().unwrap());
|
|
|
|
engine.set_signer(Arc::new(tap), authority, "".into());
|
|
|
|
assert!(engine.seals_internally().unwrap());
|
2016-09-14 00:00:26 +02:00
|
|
|
}
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|