Merge pull request #56 from sigp/validator_record_update
Validator record update
This commit is contained in:
commit
05f011d214
@ -1,27 +1,26 @@
|
|||||||
use super::honey_badger_split::SplitExt;
|
use super::honey_badger_split::SplitExt;
|
||||||
use super::types::{ShardAndCommittee, ValidatorRecord, ChainConfig};
|
use super::types::{
|
||||||
|
ShardAndCommittee,
|
||||||
|
ValidatorRecord,
|
||||||
|
ValidatorStatus,
|
||||||
|
ChainConfig,
|
||||||
|
};
|
||||||
use super::TransitionError;
|
use super::TransitionError;
|
||||||
use super::shuffle;
|
use super::shuffle;
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
|
|
||||||
type DelegatedCycle = Vec<Vec<ShardAndCommittee>>;
|
type DelegatedCycle = Vec<Vec<ShardAndCommittee>>;
|
||||||
|
|
||||||
/// Produce a vector of validators indicies where those validators start and end
|
/// Returns the indicies of each active validator in a given vec of validators.
|
||||||
/// dynasties are within the supplied `dynasty`.
|
fn active_validator_indicies(validators: &[ValidatorRecord])
|
||||||
fn active_validator_indicies(
|
|
||||||
dynasty: u64,
|
|
||||||
validators: &[ValidatorRecord])
|
|
||||||
-> Vec<usize>
|
-> Vec<usize>
|
||||||
{
|
{
|
||||||
validators.iter()
|
validators.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter_map(|(i, validator)| {
|
.filter_map(|(i, validator)| {
|
||||||
if (validator.start_dynasty >= dynasty) &
|
match validator.status {
|
||||||
(validator.end_dynasty < dynasty)
|
x if x == ValidatorStatus::Active as u8 => Some(i),
|
||||||
{
|
_ => None
|
||||||
Some(i)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
@ -35,13 +34,12 @@ fn active_validator_indicies(
|
|||||||
pub fn delegate_validators(
|
pub fn delegate_validators(
|
||||||
seed: &[u8],
|
seed: &[u8],
|
||||||
validators: &[ValidatorRecord],
|
validators: &[ValidatorRecord],
|
||||||
dynasty: u64,
|
|
||||||
crosslinking_shard_start: u16,
|
crosslinking_shard_start: u16,
|
||||||
config: &ChainConfig)
|
config: &ChainConfig)
|
||||||
-> Result<DelegatedCycle, TransitionError>
|
-> Result<DelegatedCycle, TransitionError>
|
||||||
{
|
{
|
||||||
let shuffled_validator_indices = {
|
let shuffled_validator_indices = {
|
||||||
let mut validator_indices = active_validator_indicies(dynasty, validators);
|
let mut validator_indices = active_validator_indicies(validators);
|
||||||
match shuffle(seed, validator_indices) {
|
match shuffle(seed, validator_indices) {
|
||||||
Ok(shuffled) => shuffled,
|
Ok(shuffled) => shuffled,
|
||||||
_ => return Err(TransitionError::InvalidInput(
|
_ => return Err(TransitionError::InvalidInput(
|
||||||
|
@ -16,7 +16,7 @@ pub mod validator_record;
|
|||||||
use self::ethereum_types::{
|
use self::ethereum_types::{
|
||||||
H256,
|
H256,
|
||||||
H160,
|
H160,
|
||||||
U256
|
U256,
|
||||||
};
|
};
|
||||||
use self::boolean_bitfield::BooleanBitfield;
|
use self::boolean_bitfield::BooleanBitfield;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@ -29,7 +29,7 @@ pub use beacon_block::BeaconBlock;
|
|||||||
pub use crosslink_record::CrosslinkRecord;
|
pub use crosslink_record::CrosslinkRecord;
|
||||||
pub use shard_and_committee::ShardAndCommittee;
|
pub use shard_and_committee::ShardAndCommittee;
|
||||||
pub use special_record::{ SpecialRecord, SpecialRecordKind };
|
pub use special_record::{ SpecialRecord, SpecialRecordKind };
|
||||||
pub use validator_record::ValidatorRecord;
|
pub use validator_record::{ ValidatorRecord, ValidatorStatus };
|
||||||
|
|
||||||
pub type Hash256 = H256;
|
pub type Hash256 = H256;
|
||||||
pub type Address = H160;
|
pub type Address = H160;
|
||||||
|
@ -1,21 +1,32 @@
|
|||||||
use super::{
|
use super::{
|
||||||
Hash256,
|
Hash256,
|
||||||
Address,
|
Address,
|
||||||
EthBalance,
|
|
||||||
};
|
};
|
||||||
use super::bls::{
|
use super::bls::{
|
||||||
PublicKey,
|
PublicKey,
|
||||||
Keypair
|
Keypair
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||||
|
pub enum ValidatorStatus {
|
||||||
|
PendingActivation = 0,
|
||||||
|
Active = 1,
|
||||||
|
PendingExit = 2,
|
||||||
|
PendingWithdraw = 3,
|
||||||
|
Withdrawn = 5,
|
||||||
|
Penalized = 127,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct ValidatorRecord {
|
pub struct ValidatorRecord {
|
||||||
pub pubkey: PublicKey,
|
pub pubkey: PublicKey,
|
||||||
pub withdrawal_shard: u16,
|
pub withdrawal_shard: u16,
|
||||||
pub withdrawal_address: Address,
|
pub withdrawal_address: Address,
|
||||||
pub randao_commitment: Hash256,
|
pub randao_commitment: Hash256,
|
||||||
pub balance: EthBalance,
|
pub randao_last_change: u64,
|
||||||
pub start_dynasty: u64,
|
pub balance: u64,
|
||||||
pub end_dynasty: u64,
|
pub status: u8,
|
||||||
|
pub exit_slot: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ValidatorRecord {
|
impl ValidatorRecord {
|
||||||
@ -30,24 +41,15 @@ impl ValidatorRecord {
|
|||||||
withdrawal_shard: 0,
|
withdrawal_shard: 0,
|
||||||
withdrawal_address: Address::zero(),
|
withdrawal_address: Address::zero(),
|
||||||
randao_commitment: Hash256::zero(),
|
randao_commitment: Hash256::zero(),
|
||||||
balance: EthBalance::zero(),
|
randao_last_change: 0,
|
||||||
start_dynasty: 0,
|
balance: 0,
|
||||||
end_dynasty: 0,
|
status: 0,
|
||||||
|
exit_slot: 0,
|
||||||
};
|
};
|
||||||
(s, keypair)
|
(s, keypair)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for ValidatorRecord {
|
|
||||||
fn clone(&self) -> ValidatorRecord {
|
|
||||||
ValidatorRecord {
|
|
||||||
pubkey: self.pubkey.clone(),
|
|
||||||
..*self
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
@ -56,12 +58,12 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_validator_record_zero_rand_keypair() {
|
fn test_validator_record_zero_rand_keypair() {
|
||||||
let (v, _kp) = ValidatorRecord::zero_with_thread_rand_keypair();
|
let (v, _kp) = ValidatorRecord::zero_with_thread_rand_keypair();
|
||||||
// TODO: check keys
|
|
||||||
assert_eq!(v.withdrawal_shard, 0);
|
assert_eq!(v.withdrawal_shard, 0);
|
||||||
assert!(v.withdrawal_address.is_zero());
|
assert!(v.withdrawal_address.is_zero());
|
||||||
assert!(v.randao_commitment.is_zero());
|
assert!(v.randao_commitment.is_zero());
|
||||||
assert!(v.balance.is_zero());
|
assert_eq!(v.randao_last_change, 0);
|
||||||
assert_eq!(v.start_dynasty, 0);
|
assert_eq!(v.balance, 0);
|
||||||
assert_eq!(v.end_dynasty, 0);
|
assert_eq!(v.status, 0);
|
||||||
|
assert_eq!(v.exit_slot, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user