2020-01-17 14:27:28 +01:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
2019-01-07 11:33:07 +01:00
|
|
|
// This file is part of Parity Ethereum.
|
2016-05-03 17:23:53 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-05-03 17:23:53 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-05-03 17:23:53 +02:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-03 17:23:53 +02:00
|
|
|
|
|
|
|
//! A blockchain engine that supports a basic, non-BFT proof-of-authority.
|
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
use std::sync::Weak;
|
2019-08-22 18:25:49 +02:00
|
|
|
|
|
|
|
use common_types::{
|
2019-07-18 12:27:08 +02:00
|
|
|
header::Header,
|
|
|
|
engines::{
|
2019-08-15 17:59:22 +02:00
|
|
|
Headers,
|
|
|
|
PendingTransitionStore,
|
2019-07-18 12:27:08 +02:00
|
|
|
SealingState,
|
2019-08-15 17:59:22 +02:00
|
|
|
Seal,
|
2019-07-18 12:27:08 +02:00
|
|
|
params::CommonParams,
|
|
|
|
machine::{AuxiliaryData, Call},
|
|
|
|
},
|
|
|
|
errors::{EngineError, BlockError, EthcoreError as Error},
|
|
|
|
};
|
2019-08-22 18:25:49 +02:00
|
|
|
use client_traits::EngineClient;
|
|
|
|
use ethereum_types::{H256, H520};
|
|
|
|
use parking_lot::RwLock;
|
|
|
|
use engine::{Engine, ConstructedVerifier, signer::EngineSigner};
|
2019-10-23 13:03:46 +02:00
|
|
|
use parity_crypto::publickey::Signature;
|
2019-08-22 18:25:49 +02:00
|
|
|
use log::trace;
|
|
|
|
use machine::{Machine, executed_block::ExecutedBlock};
|
|
|
|
use rlp::Rlp;
|
|
|
|
use 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 {
|
|
|
|
/// 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 {
|
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 {
|
|
|
|
list: SimpleList,
|
|
|
|
}
|
2017-04-13 20:24:21 +02:00
|
|
|
|
2019-08-15 17:59:22 +02:00
|
|
|
impl engine::EpochVerifier for EpochVerifier {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 18:48:35 +02:00
|
|
|
fn verify_external(header: &Header, validators: &dyn ValidatorSet) -> Result<(), Error> {
|
2017-04-13 20:24:21 +02:00
|
|
|
// Check if the signature belongs to a validator, can depend on parent state.
|
2018-04-16 15:52:12 +02:00
|
|
|
let sig = Rlp::new(&header.seal()[0]).as_val::<H520>()?;
|
2019-10-23 13:03:46 +02:00
|
|
|
let signer = parity_crypto::publickey::public_to_address(&parity_crypto::publickey::recover(&sig.into(), &header.bare_hash())?);
|
2017-04-13 20:24:21 +02:00
|
|
|
|
2017-06-12 11:54:34 +02:00
|
|
|
if *header.author() != signer {
|
|
|
|
return Err(EngineError::NotAuthorized(*header.author()).into())
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:24:21 +02:00
|
|
|
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 {
|
2019-06-26 14:16:05 +02:00
|
|
|
machine: Machine,
|
2019-06-14 18:48:35 +02:00
|
|
|
signer: RwLock<Option<Box<dyn EngineSigner>>>,
|
|
|
|
validators: Box<dyn ValidatorSet>,
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BasicAuthority {
|
|
|
|
/// Create a new instance of BasicAuthority engine
|
2019-06-26 14:16:05 +02:00
|
|
|
pub fn new(our_params: BasicAuthorityParams, machine: Machine) -> Self {
|
2016-05-03 17:23:53 +02:00
|
|
|
BasicAuthority {
|
2017-09-26 14:19:08 +02:00
|
|
|
machine: machine,
|
2019-02-07 14:34:24 +01:00
|
|
|
signer: RwLock::new(None),
|
2017-09-26 14:19:08 +02:00
|
|
|
validators: new_validator_set(our_params.validators),
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:16:05 +02:00
|
|
|
impl Engine for BasicAuthority {
|
2016-05-03 17:23:53 +02:00
|
|
|
fn name(&self) -> &str { "BasicAuthority" }
|
|
|
|
|
2019-06-26 14:16:05 +02:00
|
|
|
fn machine(&self) -> &Machine { &self.machine }
|
2016-05-03 17:23:53 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
// One field - the signature
|
2018-02-15 01:39:29 +01:00
|
|
|
fn seal_fields(&self, _header: &Header) -> usize { 1 }
|
2016-05-03 17:23:53 +02:00
|
|
|
|
2019-05-24 10:30:31 +02:00
|
|
|
fn sealing_state(&self) -> SealingState {
|
|
|
|
if self.signer.read().is_some() {
|
|
|
|
SealingState::Ready
|
|
|
|
} else {
|
|
|
|
SealingState::NotReady
|
|
|
|
}
|
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.
|
2017-12-07 12:17:11 +01:00
|
|
|
fn generate_seal(&self, block: &ExecutedBlock, _parent: &Header) -> Seal {
|
2019-03-15 13:22:47 +01:00
|
|
|
let header = &block.header;
|
2017-01-18 18:49:50 +01:00
|
|
|
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-07-13 09:48:00 +02:00
|
|
|
if let Ok(signature) = self.sign(header.bare_hash()) {
|
2019-08-22 18:25:49 +02:00
|
|
|
return Seal::Regular(vec![rlp::encode(&(H520::from(signature).as_bytes()))]);
|
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
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn verify_local_seal(&self, _header: &Header) -> Result<(), Error> {
|
2016-05-03 17:23:53 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn verify_block_external(&self, header: &Header) -> Result<(), Error> {
|
2017-04-13 20:24:21 +02:00
|
|
|
verify_external(header, &*self.validators)
|
|
|
|
}
|
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
fn genesis_epoch_data(&self, header: &Header, call: &Call) -> Result<Vec<u8>, String> {
|
|
|
|
self.validators.genesis_epoch_data(header, call)
|
2017-04-12 18:55:38 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 18:25:49 +02:00
|
|
|
#[cfg(not(any(test, feature = "test-helpers")))]
|
2019-08-15 17:59:22 +02:00
|
|
|
fn signals_epoch_end(&self, _header: &Header, _auxiliary: AuxiliaryData) -> engine::EpochChange {
|
2017-06-28 13:17:36 +02:00
|
|
|
// don't bother signalling even though a contract might try.
|
2019-08-15 17:59:22 +02:00
|
|
|
engine::EpochChange::No
|
2017-04-12 18:55:38 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 18:25:49 +02:00
|
|
|
#[cfg(any(test, feature = "test-helpers"))]
|
2019-08-15 17:59:22 +02:00
|
|
|
fn signals_epoch_end(&self, header: &Header, auxiliary: AuxiliaryData) -> engine::EpochChange {
|
2017-06-28 13:17:36 +02:00
|
|
|
// in test mode, always signal even though they don't be finalized.
|
|
|
|
let first = header.number() == 0;
|
2017-09-26 14:19:08 +02:00
|
|
|
self.validators.signals_epoch_end(first, header, auxiliary)
|
2017-06-28 13:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_epoch_end(
|
|
|
|
&self,
|
|
|
|
chain_head: &Header,
|
2018-10-25 17:33:41 +02:00
|
|
|
_finalized: &[H256],
|
2019-08-15 17:59:22 +02:00
|
|
|
_chain: &Headers<Header>,
|
|
|
|
_transition_store: &PendingTransitionStore,
|
2017-06-28 13:17:36 +02:00
|
|
|
) -> Option<Vec<u8>> {
|
|
|
|
let first = chain_head.number() == 0;
|
|
|
|
|
|
|
|
// finality never occurs so only apply immediate transitions.
|
|
|
|
self.validators.is_epoch_end(first, chain_head)
|
|
|
|
}
|
2017-04-13 20:24:21 +02:00
|
|
|
|
2018-10-25 17:33:41 +02:00
|
|
|
fn is_epoch_end_light(
|
|
|
|
&self,
|
|
|
|
chain_head: &Header,
|
2019-08-15 17:59:22 +02:00
|
|
|
chain: &Headers<Header>,
|
|
|
|
transition_store: &PendingTransitionStore,
|
2018-10-25 17:33:41 +02:00
|
|
|
) -> Option<Vec<u8>> {
|
|
|
|
self.is_epoch_end(chain_head, &[], chain, transition_store)
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:16:05 +02:00
|
|
|
fn epoch_verifier<'a>(&self, header: &Header, proof: &'a [u8]) -> ConstructedVerifier<'a> {
|
2017-06-28 13:17:36 +02:00
|
|
|
let first = header.number() == 0;
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
match self.validators.epoch_set(first, &self.machine, header.number(), proof) {
|
2017-06-28 13:17:36 +02:00
|
|
|
Ok((list, finalize)) => {
|
2019-08-15 17:59:22 +02:00
|
|
|
let verifier = Box::new(EpochVerifier { list });
|
2017-06-28 13:17:36 +02:00
|
|
|
|
|
|
|
// our epoch verifier will ensure no unverified verifier is ever verified.
|
|
|
|
match finalize {
|
|
|
|
Some(finalize) => ConstructedVerifier::Unconfirmed(verifier, proof, finalize),
|
|
|
|
None => ConstructedVerifier::Trusted(verifier),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => ConstructedVerifier::Err(e),
|
|
|
|
}
|
2017-04-13 20:24:21 +02:00
|
|
|
}
|
|
|
|
|
2019-10-09 14:42:51 +02:00
|
|
|
fn set_signer(&self, signer: Option<Box<dyn EngineSigner>>) {
|
|
|
|
*self.signer.write() = signer;
|
2016-12-05 18:08:16 +01:00
|
|
|
}
|
2017-01-24 10:03:58 +01:00
|
|
|
|
|
|
|
fn sign(&self, hash: H256) -> Result<Signature, Error> {
|
2019-02-07 14:34:24 +01:00
|
|
|
Ok(self.signer.read()
|
|
|
|
.as_ref()
|
2019-10-23 13:03:46 +02:00
|
|
|
.ok_or_else(|| parity_crypto::publickey::Error::InvalidAddress)?
|
2019-02-07 14:34:24 +01:00
|
|
|
.sign(hash)?
|
|
|
|
)
|
2017-01-24 10:03:58 +01:00
|
|
|
}
|
2017-05-17 12:41:33 +02:00
|
|
|
|
2019-08-22 18:25:49 +02:00
|
|
|
fn register_client(&self, client: Weak<dyn EngineClient>) {
|
|
|
|
self.validators.register_client(client);
|
2017-05-17 12:41:33 +02:00
|
|
|
}
|
2019-07-18 12:27:08 +02:00
|
|
|
|
|
|
|
fn params(&self) -> &CommonParams {
|
|
|
|
self.machine.params()
|
|
|
|
}
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2019-08-22 18:25:49 +02:00
|
|
|
use keccak_hash::keccak;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::H520;
|
2019-08-22 18:25:49 +02:00
|
|
|
use ethcore::{
|
|
|
|
block::*,
|
|
|
|
test_helpers::get_temp_state_db
|
|
|
|
};
|
2019-02-07 14:34:24 +01:00
|
|
|
use accounts::AccountProvider;
|
2016-07-28 20:32:20 +02:00
|
|
|
use spec::Spec;
|
2019-08-22 18:25:49 +02:00
|
|
|
use common_types::{
|
|
|
|
header::Header,
|
|
|
|
engines::{Seal, SealingState}
|
|
|
|
};
|
2018-03-14 12:26:20 +01:00
|
|
|
use tempdir::TempDir;
|
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 {
|
2019-08-22 18:25:49 +02:00
|
|
|
let bytes: &[u8] = include_bytes!("../res/basic_authority.json");
|
2018-03-14 12:26:20 +01:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
Spec::load(&tempdir.path(), bytes).expect("invalid chain spec")
|
2016-09-05 17:41:34 +02:00
|
|
|
}
|
2016-05-03 17:23:53 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn has_valid_metadata() {
|
|
|
|
let engine = new_test_authority().engine;
|
2019-08-22 18:25:49 +02:00
|
|
|
assert_eq!(engine.name(), "BasicAuthority");
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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_signature_verification_fail() {
|
|
|
|
let engine = new_test_authority().engine;
|
|
|
|
let mut header: Header = Header::default();
|
2019-08-22 18:25:49 +02:00
|
|
|
header.set_seal(vec![rlp::encode(&H520::default())]);
|
2016-05-03 17:23:53 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let verify_result = engine.verify_block_external(&header);
|
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();
|
2018-06-22 15:09:15 +02:00
|
|
|
let addr = tap.insert_account(keccak("").into(), &"".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;
|
2019-10-09 14:42:51 +02:00
|
|
|
engine.set_signer(Some(Box::new((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()]);
|
2019-07-04 13:43:20 +02:00
|
|
|
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, addr, (3141562.into(), 31415620.into()), vec![], false).unwrap();
|
2018-07-16 13:53:55 +02:00
|
|
|
let b = b.close_and_lock().unwrap();
|
2019-03-15 13:22:47 +01:00
|
|
|
if let Seal::Regular(seal) = engine.generate_seal(&b, &genesis_header) {
|
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]
|
2019-05-24 10:30:31 +02:00
|
|
|
fn sealing_state() {
|
2016-09-14 00:00:26 +02:00
|
|
|
let tap = AccountProvider::transient_provider();
|
2018-06-22 15:09:15 +02:00
|
|
|
let authority = tap.insert_account(keccak("").into(), &"".into()).unwrap();
|
2016-09-14 00:00:26 +02:00
|
|
|
|
|
|
|
let engine = new_test_authority().engine;
|
2019-05-24 10:30:31 +02:00
|
|
|
assert_eq!(SealingState::NotReady, engine.sealing_state());
|
2019-10-09 14:42:51 +02:00
|
|
|
engine.set_signer(Some(Box::new((Arc::new(tap), authority, "".into()))));
|
2019-05-24 10:30:31 +02:00
|
|
|
assert_eq!(SealingState::Ready, engine.sealing_state());
|
2019-10-09 14:42:51 +02:00
|
|
|
engine.set_signer(None);
|
|
|
|
assert_eq!(SealingState::NotReady, engine.sealing_state());
|
2016-09-14 00:00:26 +02:00
|
|
|
}
|
2016-05-03 17:23:53 +02:00
|
|
|
}
|