2018-10-23 21:45:54 +00:00
|
|
|
extern crate active_validators;
|
|
|
|
extern crate bytes;
|
|
|
|
extern crate hashing;
|
|
|
|
extern crate types;
|
|
|
|
|
|
|
|
use active_validators::validator_is_active;
|
2018-11-04 14:35:00 +00:00
|
|
|
use bytes::{BufMut, BytesMut};
|
2018-10-23 21:45:54 +00:00
|
|
|
use hashing::canonical_hash;
|
|
|
|
use std::cmp::max;
|
2018-11-04 14:35:00 +00:00
|
|
|
use types::{Hash256, ValidatorRecord, ValidatorStatus};
|
2018-10-23 21:45:54 +00:00
|
|
|
|
|
|
|
pub enum UpdateValidatorSetError {
|
|
|
|
ArithmeticOverflow,
|
|
|
|
}
|
|
|
|
|
|
|
|
const VALIDATOR_FLAG_ENTRY: u8 = 0;
|
2018-11-04 14:35:00 +00:00
|
|
|
const VALIDATOR_FLAG_EXIT: u8 = 1;
|
2018-10-23 21:45:54 +00:00
|
|
|
|
|
|
|
pub fn update_validator_set(
|
|
|
|
validators: &mut Vec<ValidatorRecord>,
|
|
|
|
hash_chain: Hash256,
|
|
|
|
present_slot: u64,
|
|
|
|
deposit_size_gwei: u64,
|
2018-11-04 14:35:00 +00:00
|
|
|
max_validator_churn_quotient: u64,
|
|
|
|
) -> Result<(), UpdateValidatorSetError> {
|
2018-10-23 21:45:54 +00:00
|
|
|
/*
|
|
|
|
* Total balance of all active validators.
|
|
|
|
*
|
|
|
|
* Return an error if an overflow occurs.
|
|
|
|
*/
|
|
|
|
let total_balance = {
|
|
|
|
let mut bal: u64 = 0;
|
|
|
|
for v in validators.iter() {
|
|
|
|
if validator_is_active(&v) {
|
2018-11-04 14:35:00 +00:00
|
|
|
bal = bal
|
|
|
|
.checked_add(v.balance)
|
2018-10-23 21:45:54 +00:00
|
|
|
.ok_or(UpdateValidatorSetError::ArithmeticOverflow)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bal
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: this is not the maximum allowable change, it can actually be higher.
|
|
|
|
*/
|
|
|
|
let max_allowable_change = {
|
2018-11-04 14:35:00 +00:00
|
|
|
let double_deposit_size = deposit_size_gwei
|
|
|
|
.checked_mul(2)
|
2018-10-23 21:45:54 +00:00
|
|
|
.ok_or(UpdateValidatorSetError::ArithmeticOverflow)?;
|
2018-11-04 14:35:00 +00:00
|
|
|
max(
|
|
|
|
double_deposit_size,
|
|
|
|
total_balance / max_validator_churn_quotient,
|
|
|
|
)
|
2018-10-23 21:45:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut hasher = ValidatorChangeHashChain {
|
|
|
|
bytes: hash_chain.to_vec(),
|
|
|
|
};
|
|
|
|
let mut total_changed: u64 = 0;
|
|
|
|
for (i, v) in validators.iter_mut().enumerate() {
|
|
|
|
match v.status {
|
|
|
|
/*
|
|
|
|
* Validator is pending activiation.
|
|
|
|
*/
|
2018-12-11 23:17:55 +00:00
|
|
|
x if x == ValidatorStatus::PendingActivation => {
|
2018-11-04 14:35:00 +00:00
|
|
|
let new_total_changed = total_changed
|
|
|
|
.checked_add(deposit_size_gwei)
|
2018-10-23 21:45:54 +00:00
|
|
|
.ok_or(UpdateValidatorSetError::ArithmeticOverflow)?;
|
|
|
|
/*
|
|
|
|
* If entering this validator would not exceed the max balance delta,
|
|
|
|
* activate the validator.
|
|
|
|
*/
|
|
|
|
if new_total_changed <= max_allowable_change {
|
2018-12-11 23:17:55 +00:00
|
|
|
v.status = ValidatorStatus::Active;
|
2018-10-23 21:45:54 +00:00
|
|
|
hasher.extend(i, &v.pubkey.as_bytes(), VALIDATOR_FLAG_ENTRY);
|
|
|
|
total_changed = new_total_changed;
|
|
|
|
} else {
|
|
|
|
// Entering the validator would exceed the balance delta.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Validator is pending exit.
|
|
|
|
*/
|
2018-12-11 23:17:55 +00:00
|
|
|
x if x == ValidatorStatus::PendingExit => {
|
2018-11-04 14:35:00 +00:00
|
|
|
let new_total_changed = total_changed
|
|
|
|
.checked_add(v.balance)
|
2018-10-23 21:45:54 +00:00
|
|
|
.ok_or(UpdateValidatorSetError::ArithmeticOverflow)?;
|
|
|
|
/*
|
|
|
|
* If exiting this validator would not exceed the max balance delta,
|
|
|
|
* exit the validator
|
|
|
|
*/
|
|
|
|
if new_total_changed <= max_allowable_change {
|
2018-12-11 23:17:55 +00:00
|
|
|
v.status = ValidatorStatus::PendingWithdraw;
|
2018-10-23 21:45:54 +00:00
|
|
|
v.exit_slot = present_slot;
|
|
|
|
hasher.extend(i, &v.pubkey.as_bytes(), VALIDATOR_FLAG_EXIT);
|
|
|
|
total_changed = new_total_changed;
|
|
|
|
} else {
|
|
|
|
// Exiting the validator would exceed the balance delta.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-04 14:35:00 +00:00
|
|
|
_ => (),
|
2018-10-23 21:45:54 +00:00
|
|
|
};
|
|
|
|
if total_changed >= max_allowable_change {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ValidatorChangeHashChain {
|
|
|
|
bytes: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ValidatorChangeHashChain {
|
2018-11-04 14:35:00 +00:00
|
|
|
pub fn extend(&mut self, index: usize, pubkey: &Vec<u8>, flag: u8) {
|
2018-10-23 21:45:54 +00:00
|
|
|
let mut message = self.bytes.clone();
|
|
|
|
message.append(&mut serialize_validator_change_record(index, pubkey, flag));
|
|
|
|
self.bytes = canonical_hash(&message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-04 14:35:00 +00:00
|
|
|
fn serialize_validator_change_record(index: usize, pubkey: &Vec<u8>, flag: u8) -> Vec<u8> {
|
2018-10-23 21:45:54 +00:00
|
|
|
let mut buf = BytesMut::with_capacity(68);
|
|
|
|
buf.put_u8(flag);
|
|
|
|
let index_bytes = {
|
|
|
|
let mut buf = BytesMut::with_capacity(8);
|
|
|
|
buf.put_u64_be(index as u64);
|
|
|
|
buf.take()[8 - 3..8].to_vec()
|
|
|
|
};
|
|
|
|
buf.put(index_bytes);
|
|
|
|
buf.put(pubkey);
|
|
|
|
buf.take().to_vec()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn it_works() {
|
|
|
|
assert_eq!(2 + 2, 4);
|
|
|
|
}
|
|
|
|
}
|