2019-01-07 11:33:07 +01:00
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity Ethereum.
2017-01-10 12:23:59 +01:00
2019-01-07 11:33:07 +01:00
// Parity Ethereum is free software: you can redistribute it and/or modify
2017-01-10 12:23:59 +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-01-10 12:23:59 +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-01-10 12:23:59 +01:00
2018-01-10 13:35:18 +01:00
use ethereum_types ::{ Address , H256 } ;
2017-01-10 12:23:59 +01:00
/// Preconfigured validator list.
2017-08-30 16:04:47 +02:00
use heapsize ::HeapSizeOf ;
2017-04-12 16:42:19 +02:00
2017-01-10 12:23:59 +01:00
use super ::ValidatorSet ;
2017-09-26 14:19:08 +02:00
use machine ::{ AuxiliaryData , Call , EthereumMachine } ;
2019-01-04 14:05:46 +01:00
use types ::{ header ::Header , BlockNumber } ;
2017-01-10 12:23:59 +01:00
2017-04-13 20:24:21 +02:00
/// Validator set containing a known set of addresses.
#[ derive(Clone, Debug, PartialEq, Eq, Default) ]
2017-01-10 12:23:59 +01:00
pub struct SimpleList {
validators : Vec < Address > ,
}
impl SimpleList {
2017-04-12 18:55:38 +02:00
/// Create a new `SimpleList`.
2017-01-10 12:23:59 +01:00
pub fn new ( validators : Vec < Address > ) -> Self {
SimpleList {
validators : validators ,
}
}
2020-08-05 06:08:03 +02:00
2017-04-12 18:55:38 +02:00
/// Convert into inner representation.
pub fn into_inner ( self ) -> Vec < Address > {
self . validators
}
2017-01-10 12:23:59 +01:00
}
2017-06-28 13:17:36 +02:00
impl ::std ::ops ::Deref for SimpleList {
type Target = [ Address ] ;
fn deref ( & self ) -> & [ Address ] {
& self . validators
}
}
impl From < Vec < Address > > for SimpleList {
fn from ( validators : Vec < Address > ) -> Self {
SimpleList {
validators : validators ,
}
}
}
2017-03-08 14:41:24 +01:00
impl HeapSizeOf for SimpleList {
fn heap_size_of_children ( & self ) -> usize {
2017-04-12 12:46:25 +02:00
self . validators . heap_size_of_children ( )
2017-03-08 14:41:24 +01:00
}
}
2017-01-10 12:23:59 +01:00
impl ValidatorSet for SimpleList {
2019-01-04 14:05:46 +01:00
fn default_caller ( & self , _block_id : ::types ::ids ::BlockId ) -> Box < Call > {
2017-04-12 16:42:19 +02:00
Box ::new ( | _ , _ | Err ( " Simple list doesn't require calls. " . into ( ) ) )
}
2020-08-05 06:08:03 +02:00
2017-06-28 13:17:36 +02:00
fn is_epoch_end ( & self , first : bool , _chain_head : & Header ) -> Option < Vec < u8 > > {
match first {
true = > Some ( Vec ::new ( ) ) , // allow transition to fixed list, and instantly
false = > None ,
2020-08-05 06:08:03 +02:00
}
2017-06-28 13:17:36 +02:00
}
2020-08-05 06:08:03 +02:00
2017-09-26 14:19:08 +02:00
fn signals_epoch_end (
& self ,
_ : bool ,
_ : & Header ,
_ : AuxiliaryData ,
) -> ::engines ::EpochChange < EthereumMachine > {
2017-04-18 14:19:10 +02:00
::engines ::EpochChange ::No
2017-04-12 18:55:38 +02:00
}
2020-08-05 06:08:03 +02:00
2017-09-26 14:19:08 +02:00
fn epoch_set (
& self ,
_first : bool ,
_ : & EthereumMachine ,
_ : BlockNumber ,
_ : & [ u8 ] ,
) -> Result < ( SimpleList , Option < H256 > ) , ::error ::Error > {
2017-06-28 13:17:36 +02:00
Ok ( ( self . clone ( ) , None ) )
2017-04-13 20:24:21 +02:00
}
2020-08-05 06:08:03 +02:00
2017-04-12 16:42:19 +02:00
fn contains_with_caller ( & self , _bh : & H256 , address : & Address , _ : & Call ) -> bool {
2017-01-10 12:23:59 +01:00
self . validators . contains ( address )
}
2020-08-05 06:08:03 +02:00
2017-04-12 16:42:19 +02:00
fn get_with_caller ( & self , _bh : & H256 , nonce : usize , _ : & Call ) -> Address {
2017-04-12 12:46:25 +02:00
let validator_n = self . validators . len ( ) ;
2020-08-05 06:08:03 +02:00
2017-06-28 13:17:36 +02:00
if validator_n = = 0 {
panic! ( " Cannot operate with an empty validator set. " ) ;
}
2020-08-05 06:08:03 +02:00
2017-04-12 12:46:25 +02:00
self . validators . get ( nonce % validator_n ) . expect ( " There are validator_n authorities; taking number modulo validator_n gives number in validator_n range; qed " ) . clone ( )
2017-01-10 12:23:59 +01:00
}
2020-08-05 06:08:03 +02:00
2017-04-12 16:42:19 +02:00
fn count_with_caller ( & self , _bh : & H256 , _ : & Call ) -> usize {
2017-04-12 12:46:25 +02:00
self . validators . len ( )
2017-01-10 12:23:59 +01:00
}
}
2020-07-29 10:36:15 +02:00
impl AsRef < dyn ValidatorSet > for SimpleList {
fn as_ref ( & self ) -> & dyn ValidatorSet {
2018-07-06 11:43:58 +02:00
self
}
}
2017-01-10 12:23:59 +01:00
#[ cfg(test) ]
mod tests {
use super ::{ super ::ValidatorSet , SimpleList } ;
2018-01-10 13:35:18 +01:00
use ethereum_types ::Address ;
2017-01-10 12:23:59 +01:00
use std ::str ::FromStr ;
2020-08-05 06:08:03 +02:00
2017-01-10 12:23:59 +01:00
#[ test ]
fn simple_list ( ) {
let a1 = Address ::from_str ( " cd1722f3947def4cf144679da39c4c32bdc35681 " ) . unwrap ( ) ;
let a2 = Address ::from_str ( " 0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6 " ) . unwrap ( ) ;
let list = SimpleList ::new ( vec! [ a1 . clone ( ) , a2 . clone ( ) ] ) ;
2017-03-08 14:41:24 +01:00
assert! ( list . contains ( & Default ::default ( ) , & a1 ) ) ;
assert_eq! ( list . get ( & Default ::default ( ) , 0 ) , a1 ) ;
assert_eq! ( list . get ( & Default ::default ( ) , 1 ) , a2 ) ;
assert_eq! ( list . get ( & Default ::default ( ) , 2 ) , a1 ) ;
2017-01-10 12:23:59 +01:00
}
}