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
|
|
|
|
|
|
|
//! Validator set deserialization.
|
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
use hash::Address;
|
2017-03-23 13:19:28 +01:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use uint::Uint;
|
2017-01-10 12:23:59 +01:00
|
|
|
|
|
|
|
/// Different ways of specifying validators.
|
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
2018-11-27 23:21:31 +01:00
|
|
|
#[serde(deny_unknown_fields)]
|
2018-10-29 16:49:04 +01:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2017-01-10 12:23:59 +01:00
|
|
|
pub enum ValidatorSet {
|
2020-08-05 06:08:03 +02:00
|
|
|
/// A simple list of authorities.
|
|
|
|
List(Vec<Address>),
|
|
|
|
/// Address of a contract that indicates the list of authorities.
|
|
|
|
SafeContract(Address),
|
|
|
|
/// Address of a contract that indicates the list of authorities and enables reporting of theor misbehaviour using transactions.
|
|
|
|
Contract(Address),
|
|
|
|
/// A map of starting blocks for each validator set.
|
|
|
|
Multi(BTreeMap<Uint, ValidatorSet>),
|
2017-01-10 12:23:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-05 06:08:03 +02:00
|
|
|
use ethereum_types::{H160, U256};
|
|
|
|
use hash::Address;
|
|
|
|
use serde_json;
|
|
|
|
use spec::validator_set::ValidatorSet;
|
|
|
|
use uint::Uint;
|
2017-01-10 12:23:59 +01:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
#[test]
|
|
|
|
fn validator_set_deserialization() {
|
|
|
|
let s = r#"[{
|
2017-03-23 13:19:28 +01:00
|
|
|
"list": ["0xc6d9d2cd449a754c494264e1809c50e34d64562b"]
|
2017-01-24 10:03:58 +01:00
|
|
|
}, {
|
2017-03-23 13:19:28 +01:00
|
|
|
"safeContract": "0xc6d9d2cd449a754c494264e1809c50e34d64562b"
|
2017-01-10 12:23:59 +01:00
|
|
|
}, {
|
2017-03-23 13:19:28 +01:00
|
|
|
"contract": "0xc6d9d2cd449a754c494264e1809c50e34d64562b"
|
|
|
|
}, {
|
|
|
|
"multi": {
|
|
|
|
"0": { "list": ["0xc6d9d2cd449a754c494264e1809c50e34d64562b"] },
|
|
|
|
"10": { "list": ["0xd6d9d2cd449a754c494264e1809c50e34d64562b"] },
|
|
|
|
"20": { "contract": "0xc6d9d2cd449a754c494264e1809c50e34d64562b" }
|
|
|
|
}
|
2017-01-10 12:23:59 +01:00
|
|
|
}]"#;
|
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
let deserialized: Vec<ValidatorSet> = serde_json::from_str(s).unwrap();
|
|
|
|
assert_eq!(deserialized.len(), 4);
|
2017-05-12 17:46:29 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
assert_eq!(
|
|
|
|
deserialized[0],
|
|
|
|
ValidatorSet::List(vec![Address(H160::from(
|
|
|
|
"0xc6d9d2cd449a754c494264e1809c50e34d64562b"
|
|
|
|
))])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
deserialized[1],
|
|
|
|
ValidatorSet::SafeContract(Address(H160::from(
|
|
|
|
"0xc6d9d2cd449a754c494264e1809c50e34d64562b"
|
|
|
|
)))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
deserialized[2],
|
|
|
|
ValidatorSet::Contract(Address(H160::from(
|
|
|
|
"0xc6d9d2cd449a754c494264e1809c50e34d64562b"
|
|
|
|
)))
|
|
|
|
);
|
|
|
|
match deserialized[3] {
|
|
|
|
ValidatorSet::Multi(ref map) => {
|
|
|
|
assert_eq!(map.len(), 3);
|
|
|
|
assert!(map.contains_key(&Uint(U256::from(0))));
|
|
|
|
assert!(map.contains_key(&Uint(U256::from(10))));
|
|
|
|
assert!(map.contains_key(&Uint(U256::from(20))));
|
|
|
|
}
|
|
|
|
_ => assert!(false),
|
|
|
|
}
|
|
|
|
}
|
2017-01-10 12:23:59 +01:00
|
|
|
}
|