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.
|
2017-03-23 13:19:28 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-03-23 13:19:28 +01: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-03-23 13:19:28 +01: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-03-23 13:19:28 +01:00
|
|
|
|
|
|
|
/// Validator set changing at fork blocks.
|
|
|
|
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::sync::Weak;
|
2019-01-04 14:05:46 +01:00
|
|
|
|
2019-08-22 18:25:49 +02:00
|
|
|
use common_types::{
|
2019-07-18 12:27:08 +02:00
|
|
|
BlockNumber,
|
|
|
|
header::Header,
|
|
|
|
ids::BlockId,
|
|
|
|
errors::EthcoreError,
|
|
|
|
engines::machine::{Call, AuxiliaryData},
|
|
|
|
};
|
2019-08-15 17:59:22 +02:00
|
|
|
use client_traits::EngineClient;
|
2019-08-22 18:25:49 +02:00
|
|
|
use ethereum_types::{H256, Address};
|
|
|
|
use log::{debug, trace};
|
|
|
|
use parity_bytes::Bytes;
|
|
|
|
use parking_lot::RwLock;
|
2019-07-18 12:27:08 +02:00
|
|
|
use machine::Machine;
|
2019-08-22 18:25:49 +02:00
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
use super::{SystemCall, ValidatorSet};
|
2017-03-23 13:19:28 +01:00
|
|
|
|
2019-06-14 18:48:35 +02:00
|
|
|
type BlockNumberLookup = Box<dyn Fn(BlockId) -> Result<BlockNumber, String> + Send + Sync + 'static>;
|
2017-03-23 13:19:28 +01:00
|
|
|
|
|
|
|
pub struct Multi {
|
2019-06-14 18:48:35 +02:00
|
|
|
sets: BTreeMap<BlockNumber, Box<dyn ValidatorSet>>,
|
2017-03-23 13:19:28 +01:00
|
|
|
block_number: RwLock<BlockNumberLookup>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Multi {
|
2019-06-14 18:48:35 +02:00
|
|
|
pub fn new(set_map: BTreeMap<BlockNumber, Box<dyn ValidatorSet>>) -> Self {
|
2017-03-23 13:19:28 +01:00
|
|
|
assert!(set_map.get(&0u64).is_some(), "ValidatorSet has to be specified from block 0.");
|
|
|
|
Multi {
|
|
|
|
sets: set_map,
|
|
|
|
block_number: RwLock::new(Box::new(move |_| Err("No client!".into()))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 18:48:35 +02:00
|
|
|
fn correct_set(&self, id: BlockId) -> Option<&dyn ValidatorSet> {
|
2017-04-12 18:55:38 +02:00
|
|
|
match self.block_number.read()(id).map(|parent_block| self.correct_set_by_number(parent_block)) {
|
2017-04-18 14:19:10 +02:00
|
|
|
Ok((_, set)) => Some(set),
|
2017-03-23 13:19:28 +01:00
|
|
|
Err(e) => {
|
|
|
|
debug!(target: "engine", "ValidatorSet could not be recovered: {}", e);
|
|
|
|
None
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2017-04-12 18:55:38 +02:00
|
|
|
|
2017-04-18 14:19:10 +02:00
|
|
|
// get correct set by block number, along with block number at which
|
|
|
|
// this set was activated.
|
2019-06-14 18:48:35 +02:00
|
|
|
fn correct_set_by_number(&self, parent_block: BlockNumber) -> (BlockNumber, &dyn ValidatorSet) {
|
2017-04-12 18:55:38 +02:00
|
|
|
let (block, set) = self.sets.iter()
|
|
|
|
.rev()
|
|
|
|
.find(|&(block, _)| *block <= parent_block + 1)
|
|
|
|
.expect("constructor validation ensures that there is at least one validator set for block 0;
|
|
|
|
block 0 is less than any uint;
|
|
|
|
qed");
|
|
|
|
|
|
|
|
trace!(target: "engine", "Multi ValidatorSet retrieved for block {}.", block);
|
2017-04-18 14:19:10 +02:00
|
|
|
(*block, &**set)
|
2017-04-12 18:55:38 +02:00
|
|
|
}
|
2017-03-23 13:19:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ValidatorSet for Multi {
|
2017-04-12 16:42:19 +02:00
|
|
|
fn default_caller(&self, block_id: BlockId) -> Box<Call> {
|
|
|
|
self.correct_set(block_id).map(|set| set.default_caller(block_id))
|
2019-03-06 15:30:35 +01:00
|
|
|
.unwrap_or_else(|| Box::new(|_, _| Err("No validator set for given ID.".into())))
|
2017-03-23 13:19:28 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 12:27:08 +02:00
|
|
|
fn on_epoch_begin(&self, _first: bool, header: &Header, call: &mut SystemCall) -> Result<(), EthcoreError> {
|
2017-04-19 14:58:19 +02:00
|
|
|
let (set_block, set) = self.correct_set_by_number(header.number());
|
2017-06-28 13:17:36 +02:00
|
|
|
let first = set_block == header.number();
|
2017-04-19 14:58:19 +02:00
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
set.on_epoch_begin(first, header, call)
|
2017-04-12 18:55:38 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
fn genesis_epoch_data(&self, header: &Header, call: &Call) -> Result<Vec<u8>, String> {
|
|
|
|
self.correct_set_by_number(0).1.genesis_epoch_data(header, call)
|
|
|
|
}
|
2017-05-17 12:41:33 +02:00
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
fn is_epoch_end(&self, _first: bool, chain_head: &Header) -> Option<Vec<u8>> {
|
|
|
|
let (set_block, set) = self.correct_set_by_number(chain_head.number());
|
|
|
|
let first = set_block == chain_head.number();
|
2017-05-17 12:41:33 +02:00
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
set.is_epoch_end(first, chain_head)
|
2017-04-12 18:55:38 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
fn signals_epoch_end(&self, _first: bool, header: &Header, aux: AuxiliaryData)
|
2019-08-15 17:59:22 +02:00
|
|
|
-> engine::EpochChange
|
2017-06-28 13:17:36 +02:00
|
|
|
{
|
2017-04-18 14:19:10 +02:00
|
|
|
let (set_block, set) = self.correct_set_by_number(header.number());
|
2017-06-28 13:17:36 +02:00
|
|
|
let first = set_block == header.number();
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
set.signals_epoch_end(first, header, aux)
|
2017-06-28 13:17:36 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 12:27:08 +02:00
|
|
|
fn epoch_set(&self, _first: bool, machine: &Machine, number: BlockNumber, proof: &[u8]) -> Result<(super::SimpleList, Option<H256>), EthcoreError> {
|
2017-06-28 13:17:36 +02:00
|
|
|
let (set_block, set) = self.correct_set_by_number(number);
|
|
|
|
let first = set_block == number;
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
set.epoch_set(first, machine, number, proof)
|
2017-04-13 20:24:21 +02:00
|
|
|
}
|
|
|
|
|
2017-04-12 16:42:19 +02:00
|
|
|
fn contains_with_caller(&self, bh: &H256, address: &Address, caller: &Call) -> bool {
|
|
|
|
self.correct_set(BlockId::Hash(*bh))
|
|
|
|
.map_or(false, |set| set.contains_with_caller(bh, address, caller))
|
2017-03-23 13:19:28 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 16:42:19 +02:00
|
|
|
fn get_with_caller(&self, bh: &H256, nonce: usize, caller: &Call) -> Address {
|
|
|
|
self.correct_set(BlockId::Hash(*bh))
|
|
|
|
.map_or_else(Default::default, |set| set.get_with_caller(bh, nonce, caller))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn count_with_caller(&self, bh: &H256, caller: &Call) -> usize {
|
|
|
|
self.correct_set(BlockId::Hash(*bh))
|
|
|
|
.map_or_else(usize::max_value, |set| set.count_with_caller(bh, caller))
|
2017-03-23 13:19:28 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
fn report_malicious(&self, validator: &Address, set_block: BlockNumber, block: BlockNumber, proof: Bytes) {
|
|
|
|
self.correct_set_by_number(set_block).1.report_malicious(validator, set_block, block, proof);
|
2017-03-23 13:19:28 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
fn report_benign(&self, validator: &Address, set_block: BlockNumber, block: BlockNumber) {
|
|
|
|
self.correct_set_by_number(set_block).1.report_benign(validator, set_block, block);
|
2017-03-23 13:19:28 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 18:48:35 +02:00
|
|
|
fn register_client(&self, client: Weak<dyn EngineClient>) {
|
2017-03-23 13:19:28 +01:00
|
|
|
for set in self.sets.values() {
|
2017-09-05 17:54:05 +02:00
|
|
|
set.register_client(client.clone());
|
2017-03-23 13:19:28 +01:00
|
|
|
}
|
2017-04-12 16:42:19 +02:00
|
|
|
*self.block_number.write() = Box::new(move |id| client
|
2017-03-23 13:19:28 +01:00
|
|
|
.upgrade()
|
2017-12-21 14:50:58 +01:00
|
|
|
.ok_or_else(|| "No client!".into())
|
2019-03-06 15:30:35 +01:00
|
|
|
.and_then(|c| c.block_number(id).ok_or_else(|| "Unknown block".into())));
|
2017-03-23 13:19:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::collections::BTreeMap;
|
2019-08-22 18:25:49 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
use accounts::AccountProvider;
|
2019-08-22 18:25:49 +02:00
|
|
|
use common_types::{
|
|
|
|
header::Header,
|
2019-08-15 17:59:22 +02:00
|
|
|
ids::BlockId,
|
|
|
|
verification::Unverified,
|
|
|
|
};
|
2019-12-17 11:34:14 +01:00
|
|
|
use client_traits::{
|
|
|
|
BlockChainClient, BlockInfo, ChainInfo, ImportBlock, EngineClient, ForceUpdateSealing, TransactionRequest
|
|
|
|
};
|
2019-08-22 18:25:49 +02:00
|
|
|
use engine::EpochChange;
|
|
|
|
use ethcore::{
|
|
|
|
miner::{self, MinerService},
|
2019-12-17 11:34:14 +01:00
|
|
|
test_helpers::generate_dummy_client_with_spec,
|
2019-08-22 18:25:49 +02:00
|
|
|
};
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::Address;
|
2019-10-23 13:03:46 +02:00
|
|
|
use parity_crypto::publickey::Secret;
|
2019-08-22 18:25:49 +02:00
|
|
|
use keccak_hash::keccak;
|
2019-08-23 15:32:58 +02:00
|
|
|
use spec;
|
2017-06-28 13:17:36 +02:00
|
|
|
|
2019-08-22 18:25:49 +02:00
|
|
|
use crate::ValidatorSet;
|
2017-06-28 13:17:36 +02:00
|
|
|
use super::Multi;
|
2017-03-23 13:19:28 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uses_current_set() {
|
|
|
|
let tap = Arc::new(AccountProvider::transient_provider());
|
2017-08-30 19:18:28 +02:00
|
|
|
let s0: Secret = keccak("0").into();
|
2018-06-22 15:09:15 +02:00
|
|
|
let v0 = tap.insert_account(s0.clone(), &"".into()).unwrap();
|
|
|
|
let v1 = tap.insert_account(keccak("1").into(), &"".into()).unwrap();
|
2019-08-07 16:52:48 +02:00
|
|
|
let client = generate_dummy_client_with_spec(spec::new_validator_multi);
|
2017-09-05 17:54:05 +02:00
|
|
|
client.engine().register_client(Arc::downgrade(&client) as _);
|
2017-03-23 13:19:28 +01:00
|
|
|
|
|
|
|
// Make sure txs go through.
|
2018-04-13 17:34:27 +02:00
|
|
|
client.miner().set_gas_range_target((1_000_000.into(), 1_000_000.into()));
|
2017-03-23 13:19:28 +01:00
|
|
|
|
|
|
|
// Wrong signer for the first block.
|
2019-02-07 14:34:24 +01:00
|
|
|
let signer = Box::new((tap.clone(), v1, "".into()));
|
|
|
|
client.miner().set_author(miner::Author::Sealer(signer));
|
2019-12-17 11:34:14 +01:00
|
|
|
client.transact(TransactionRequest::call(Default::default(), Default::default())).unwrap();
|
2019-11-10 11:41:31 +01:00
|
|
|
EngineClient::update_sealing(&*client, ForceUpdateSealing::No);
|
2017-03-23 13:19:28 +01:00
|
|
|
assert_eq!(client.chain_info().best_block_number, 0);
|
|
|
|
// Right signer for the first block.
|
2019-02-07 14:34:24 +01:00
|
|
|
let signer = Box::new((tap.clone(), v0, "".into()));
|
|
|
|
client.miner().set_author(miner::Author::Sealer(signer));
|
2019-11-10 11:41:31 +01:00
|
|
|
EngineClient::update_sealing(&*client, ForceUpdateSealing::No);
|
2017-03-23 13:19:28 +01:00
|
|
|
assert_eq!(client.chain_info().best_block_number, 1);
|
|
|
|
// This time v0 is wrong.
|
2019-12-17 11:34:14 +01:00
|
|
|
client.transact(TransactionRequest::call(Default::default(), Default::default())).unwrap();
|
2019-11-10 11:41:31 +01:00
|
|
|
EngineClient::update_sealing(&*client, ForceUpdateSealing::No);
|
2017-03-23 13:19:28 +01:00
|
|
|
assert_eq!(client.chain_info().best_block_number, 1);
|
2019-02-07 14:34:24 +01:00
|
|
|
let signer = Box::new((tap.clone(), v1, "".into()));
|
|
|
|
client.miner().set_author(miner::Author::Sealer(signer));
|
2019-11-10 11:41:31 +01:00
|
|
|
EngineClient::update_sealing(&*client, ForceUpdateSealing::No);
|
2017-03-23 13:19:28 +01:00
|
|
|
assert_eq!(client.chain_info().best_block_number, 2);
|
|
|
|
// v1 is still good.
|
2019-12-17 11:34:14 +01:00
|
|
|
client.transact(TransactionRequest::call(Default::default(), Default::default())).unwrap();
|
2019-11-10 11:41:31 +01:00
|
|
|
EngineClient::update_sealing(&*client, ForceUpdateSealing::No);
|
2017-03-23 13:19:28 +01:00
|
|
|
assert_eq!(client.chain_info().best_block_number, 3);
|
|
|
|
|
|
|
|
// Check syncing.
|
2019-12-17 11:34:14 +01:00
|
|
|
let sync_client = generate_dummy_client_with_spec(spec::new_validator_multi);
|
2017-09-05 17:54:05 +02:00
|
|
|
sync_client.engine().register_client(Arc::downgrade(&sync_client) as _);
|
2017-03-23 13:19:28 +01:00
|
|
|
for i in 1..4 {
|
2018-08-02 11:20:46 +02:00
|
|
|
sync_client.import_block(Unverified::from_rlp(client.block(BlockId::Number(i)).unwrap().into_inner()).unwrap()).unwrap();
|
2017-03-23 13:19:28 +01:00
|
|
|
}
|
|
|
|
sync_client.flush_queue();
|
|
|
|
assert_eq!(sync_client.chain_info().best_block_number, 3);
|
|
|
|
}
|
2017-06-28 13:17:36 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn transition_to_fixed_list_instant() {
|
|
|
|
use super::super::SimpleList;
|
|
|
|
|
2019-06-14 18:48:35 +02:00
|
|
|
let mut map: BTreeMap<_, Box<dyn ValidatorSet>> = BTreeMap::new();
|
2017-06-28 13:17:36 +02:00
|
|
|
let list1: Vec<_> = (0..10).map(|_| Address::random()).collect();
|
|
|
|
let list2 = {
|
|
|
|
let mut list = list1.clone();
|
|
|
|
list.push(Address::random());
|
|
|
|
list
|
|
|
|
};
|
|
|
|
|
|
|
|
map.insert(0, Box::new(SimpleList::new(list1)));
|
|
|
|
map.insert(500, Box::new(SimpleList::new(list2)));
|
|
|
|
|
|
|
|
let multi = Multi::new(map);
|
|
|
|
|
|
|
|
let mut header = Header::new();
|
|
|
|
header.set_number(499);
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
match multi.signals_epoch_end(false, &header, Default::default()) {
|
2017-06-28 13:17:36 +02:00
|
|
|
EpochChange::No => {},
|
|
|
|
_ => panic!("Expected no epoch signal change."),
|
|
|
|
}
|
|
|
|
assert!(multi.is_epoch_end(false, &header).is_none());
|
|
|
|
|
|
|
|
header.set_number(500);
|
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
match multi.signals_epoch_end(false, &header, Default::default()) {
|
2017-06-28 13:17:36 +02:00
|
|
|
EpochChange::No => {},
|
|
|
|
_ => panic!("Expected no epoch signal change."),
|
|
|
|
}
|
|
|
|
assert!(multi.is_epoch_end(false, &header).is_some());
|
|
|
|
}
|
2017-03-23 13:19:28 +01:00
|
|
|
}
|