2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-05-22 08:21:34 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-05-22 08:21:34 +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,
|
2017-05-22 08:21:34 +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/>.
|
2017-05-22 08:21:34 +02:00
|
|
|
|
|
|
|
/// Used for Engine testing.
|
|
|
|
|
|
|
|
use std::str::FromStr;
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2017-05-22 08:21:34 +02:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};
|
2019-01-04 14:05:46 +01:00
|
|
|
|
2019-08-22 18:25:49 +02:00
|
|
|
use log::trace;
|
|
|
|
use parity_util_mem::MallocSizeOf;
|
|
|
|
use common_types::{
|
2019-07-18 12:27:08 +02:00
|
|
|
BlockNumber,
|
2019-08-22 18:25:49 +02:00
|
|
|
ids::BlockId,
|
2019-07-18 12:27:08 +02:00
|
|
|
header::Header,
|
|
|
|
errors::EthcoreError,
|
|
|
|
engines::machine::{Call, AuxiliaryData},
|
|
|
|
};
|
2019-08-22 18:25:49 +02:00
|
|
|
use ethereum_types::{H256, Address};
|
2019-07-18 12:27:08 +02:00
|
|
|
use machine::Machine;
|
2019-08-22 18:25:49 +02:00
|
|
|
use parity_bytes::Bytes;
|
|
|
|
|
2017-05-22 08:21:34 +02:00
|
|
|
use super::{ValidatorSet, SimpleList};
|
|
|
|
|
|
|
|
/// Set used for testing with a single validator.
|
2019-10-14 12:56:38 +02:00
|
|
|
#[derive(Clone, MallocSizeOf, Debug)]
|
2017-05-22 08:21:34 +02:00
|
|
|
pub struct TestSet {
|
|
|
|
validator: SimpleList,
|
2019-06-19 13:54:05 +02:00
|
|
|
#[ignore_malloc_size_of = "zero sized"]
|
2017-05-22 08:21:34 +02:00
|
|
|
last_malicious: Arc<AtomicUsize>,
|
2019-06-19 13:54:05 +02:00
|
|
|
#[ignore_malloc_size_of = "zero sized"]
|
2017-05-22 08:21:34 +02:00
|
|
|
last_benign: Arc<AtomicUsize>,
|
|
|
|
}
|
|
|
|
|
2018-12-10 19:58:38 +01:00
|
|
|
impl Default for TestSet {
|
|
|
|
fn default() -> Self {
|
2019-06-25 13:15:00 +02:00
|
|
|
TestSet::new(
|
|
|
|
Default::default(),
|
|
|
|
Default::default(),
|
|
|
|
vec![Address::from_str("7d577a597b2742b498cb5cf0c26cdcd726d39e6e").unwrap()]
|
|
|
|
)
|
2018-12-10 19:58:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 08:21:34 +02:00
|
|
|
impl TestSet {
|
2019-06-25 13:15:00 +02:00
|
|
|
pub fn new(last_malicious: Arc<AtomicUsize>, last_benign: Arc<AtomicUsize>, validators: Vec<Address>) -> Self {
|
2017-05-22 08:21:34 +02:00
|
|
|
TestSet {
|
2019-06-25 13:15:00 +02:00
|
|
|
validator: SimpleList::new(validators),
|
2018-12-10 19:58:38 +01:00
|
|
|
last_malicious,
|
|
|
|
last_benign,
|
2017-05-22 08:21:34 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-14 12:56:38 +02:00
|
|
|
|
|
|
|
pub fn from_validators(validators: Vec<Address>) -> Self {
|
|
|
|
TestSet::new(Default::default(), Default::default(), validators)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_malicious(&self) -> usize {
|
|
|
|
self.last_malicious.load(AtomicOrdering::SeqCst)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_benign(&self) -> usize {
|
|
|
|
self.last_benign.load(AtomicOrdering::SeqCst)
|
|
|
|
}
|
2017-05-22 08:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ValidatorSet for TestSet {
|
2019-08-22 18:25:49 +02:00
|
|
|
fn default_caller(&self, _block_id: BlockId) -> Box<Call> {
|
2017-05-22 08:21:34 +02:00
|
|
|
Box::new(|_, _| Err("Test set doesn't require calls.".into()))
|
|
|
|
}
|
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
fn is_epoch_end(&self, _first: bool, _chain_head: &Header) -> Option<Vec<u8>> { None }
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn signals_epoch_end(&self, _: bool, _: &Header, _: AuxiliaryData)
|
2019-08-15 17:59:22 +02:00
|
|
|
-> engine::EpochChange
|
2017-05-22 08:21:34 +02:00
|
|
|
{
|
2019-08-15 17:59:22 +02:00
|
|
|
engine::EpochChange::No
|
2017-05-22 08:21:34 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 12:27:08 +02:00
|
|
|
fn epoch_set(&self, _: bool, _: &Machine, _: BlockNumber, _: &[u8]) -> Result<(SimpleList, Option<H256>), EthcoreError> {
|
2017-06-28 13:17:36 +02:00
|
|
|
Ok((self.validator.clone(), None))
|
2017-05-22 08:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn contains_with_caller(&self, bh: &H256, address: &Address, _: &Call) -> bool {
|
|
|
|
self.validator.contains(bh, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_with_caller(&self, bh: &H256, nonce: usize, _: &Call) -> Address {
|
|
|
|
self.validator.get(bh, nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn count_with_caller(&self, _bh: &H256, _: &Call) -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
fn report_malicious(&self, _validator: &Address, _set_block: BlockNumber, block: BlockNumber, _proof: Bytes) {
|
2017-05-22 08:21:34 +02:00
|
|
|
self.last_malicious.store(block as usize, AtomicOrdering::SeqCst)
|
|
|
|
}
|
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
fn report_benign(&self, _validator: &Address, _set_block: BlockNumber, block: BlockNumber) {
|
2019-07-04 18:03:22 +02:00
|
|
|
trace!(target: "engine", "test validator set recording benign misbehaviour");
|
2017-05-22 08:21:34 +02:00
|
|
|
self.last_benign.store(block as usize, AtomicOrdering::SeqCst)
|
|
|
|
}
|
|
|
|
}
|