mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-19 13:58:28 +00:00
Merge branch 'master' into lmd-ghost
This commit is contained in:
@@ -7,9 +7,9 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = {
|
||||
8 + // slot
|
||||
8 + // shard
|
||||
32 + // beacon_block_hash
|
||||
32 + // epoch_boundary_hash
|
||||
32 + // shard_block_root
|
||||
32 + // latest_crosslink_root
|
||||
32 + // epoch_boundary_root
|
||||
32 + // shard_block_hash
|
||||
32 + // latest_crosslink_hash
|
||||
8 + // justified_slot
|
||||
32 // justified_block_root
|
||||
};
|
||||
@@ -18,8 +18,8 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = {
|
||||
pub struct AttestationData {
|
||||
pub slot: u64,
|
||||
pub shard: u64,
|
||||
pub beacon_block_hash: Hash256,
|
||||
pub epoch_boundary_hash: Hash256,
|
||||
pub beacon_block_root: Hash256,
|
||||
pub epoch_boundary_root: Hash256,
|
||||
pub shard_block_root: Hash256,
|
||||
pub latest_crosslink_root: Hash256,
|
||||
pub justified_slot: u64,
|
||||
@@ -31,8 +31,8 @@ impl AttestationData {
|
||||
Self {
|
||||
slot: 0,
|
||||
shard: 0,
|
||||
beacon_block_hash: Hash256::zero(),
|
||||
epoch_boundary_hash: Hash256::zero(),
|
||||
beacon_block_root: Hash256::zero(),
|
||||
epoch_boundary_root: Hash256::zero(),
|
||||
shard_block_root: Hash256::zero(),
|
||||
latest_crosslink_root: Hash256::zero(),
|
||||
justified_slot: 0,
|
||||
@@ -51,8 +51,8 @@ impl Encodable for AttestationData {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.slot);
|
||||
s.append(&self.shard);
|
||||
s.append(&self.beacon_block_hash);
|
||||
s.append(&self.epoch_boundary_hash);
|
||||
s.append(&self.beacon_block_root);
|
||||
s.append(&self.epoch_boundary_root);
|
||||
s.append(&self.shard_block_root);
|
||||
s.append(&self.latest_crosslink_root);
|
||||
s.append(&self.justified_slot);
|
||||
@@ -64,8 +64,8 @@ impl Decodable for AttestationData {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (slot, i) = u64::ssz_decode(bytes, i)?;
|
||||
let (shard, i) = u64::ssz_decode(bytes, i)?;
|
||||
let (beacon_block_hash, i) = Hash256::ssz_decode(bytes, i)?;
|
||||
let (epoch_boundary_hash, i) = Hash256::ssz_decode(bytes, i)?;
|
||||
let (beacon_block_root, i) = Hash256::ssz_decode(bytes, i)?;
|
||||
let (epoch_boundary_root, i) = Hash256::ssz_decode(bytes, i)?;
|
||||
let (shard_block_root, i) = Hash256::ssz_decode(bytes, i)?;
|
||||
let (latest_crosslink_root, i) = Hash256::ssz_decode(bytes, i)?;
|
||||
let (justified_slot, i) = u64::ssz_decode(bytes, i)?;
|
||||
@@ -74,8 +74,8 @@ impl Decodable for AttestationData {
|
||||
let attestation_data = AttestationData {
|
||||
slot,
|
||||
shard,
|
||||
beacon_block_hash,
|
||||
epoch_boundary_hash,
|
||||
beacon_block_root,
|
||||
epoch_boundary_root,
|
||||
shard_block_root,
|
||||
latest_crosslink_root,
|
||||
justified_slot,
|
||||
@@ -90,8 +90,8 @@ impl<T: RngCore> TestRandom<T> for AttestationData {
|
||||
Self {
|
||||
slot: <_>::random_for_test(rng),
|
||||
shard: <_>::random_for_test(rng),
|
||||
beacon_block_hash: <_>::random_for_test(rng),
|
||||
epoch_boundary_hash: <_>::random_for_test(rng),
|
||||
beacon_block_root: <_>::random_for_test(rng),
|
||||
epoch_boundary_root: <_>::random_for_test(rng),
|
||||
shard_block_root: <_>::random_for_test(rng),
|
||||
latest_crosslink_root: <_>::random_for_test(rng),
|
||||
justified_slot: <_>::random_for_test(rng),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::{BeaconBlockBody, Hash256};
|
||||
use super::{BeaconBlockBody, Eth1Data, Hash256};
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::Signature;
|
||||
use hashing::canonical_hash;
|
||||
@@ -11,7 +11,7 @@ pub struct BeaconBlock {
|
||||
pub parent_root: Hash256,
|
||||
pub state_root: Hash256,
|
||||
pub randao_reveal: Signature,
|
||||
pub candidate_pow_receipt_root: Hash256,
|
||||
pub eth1_data: Eth1Data,
|
||||
pub signature: Signature,
|
||||
pub body: BeaconBlockBody,
|
||||
}
|
||||
@@ -30,7 +30,7 @@ impl Encodable for BeaconBlock {
|
||||
s.append(&self.parent_root);
|
||||
s.append(&self.state_root);
|
||||
s.append(&self.randao_reveal);
|
||||
s.append(&self.candidate_pow_receipt_root);
|
||||
s.append(&self.eth1_data);
|
||||
s.append(&self.signature);
|
||||
s.append(&self.body);
|
||||
}
|
||||
@@ -42,7 +42,7 @@ impl Decodable for BeaconBlock {
|
||||
let (parent_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (state_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (randao_reveal, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (candidate_pow_receipt_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (eth1_data, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (signature, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (body, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
@@ -52,7 +52,7 @@ impl Decodable for BeaconBlock {
|
||||
parent_root,
|
||||
state_root,
|
||||
randao_reveal,
|
||||
candidate_pow_receipt_root,
|
||||
eth1_data,
|
||||
signature,
|
||||
body,
|
||||
},
|
||||
@@ -68,7 +68,7 @@ impl<T: RngCore> TestRandom<T> for BeaconBlock {
|
||||
parent_root: <_>::random_for_test(rng),
|
||||
state_root: <_>::random_for_test(rng),
|
||||
randao_reveal: <_>::random_for_test(rng),
|
||||
candidate_pow_receipt_root: <_>::random_for_test(rng),
|
||||
eth1_data: <_>::random_for_test(rng),
|
||||
signature: <_>::random_for_test(rng),
|
||||
body: <_>::random_for_test(rng),
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use crate::candidate_pow_receipt_root_record::CandidatePoWReceiptRootRecord;
|
||||
use crate::crosslink_record::CrosslinkRecord;
|
||||
use crate::fork_data::ForkData;
|
||||
use crate::pending_attestation_record::PendingAttestationRecord;
|
||||
use super::crosslink::Crosslink;
|
||||
use super::eth1_data::Eth1Data;
|
||||
use super::eth1_data_vote::Eth1DataVote;
|
||||
use super::fork::Fork;
|
||||
use super::pending_attestation::PendingAttestation;
|
||||
use super::validator::Validator;
|
||||
use super::Hash256;
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::validator_record::ValidatorRecord;
|
||||
use crate::Hash256;
|
||||
use hashing::canonical_hash;
|
||||
use rand::RngCore;
|
||||
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
|
||||
@@ -17,12 +18,12 @@ pub struct BeaconState {
|
||||
// Misc
|
||||
pub slot: u64,
|
||||
pub genesis_time: u64,
|
||||
pub fork_data: ForkData,
|
||||
pub fork_data: Fork,
|
||||
|
||||
// Validator registry
|
||||
pub validator_registry: Vec<ValidatorRecord>,
|
||||
pub validator_registry: Vec<Validator>,
|
||||
pub validator_balances: Vec<u64>,
|
||||
pub validator_registry_latest_change_slot: u64,
|
||||
pub validator_registry_update_slot: u64,
|
||||
pub validator_registry_exit_count: u64,
|
||||
pub validator_registry_delta_chain_tip: Hash256,
|
||||
|
||||
@@ -46,15 +47,15 @@ pub struct BeaconState {
|
||||
pub finalized_slot: u64,
|
||||
|
||||
// Recent state
|
||||
pub latest_crosslinks: Vec<CrosslinkRecord>,
|
||||
pub latest_crosslinks: Vec<Crosslink>,
|
||||
pub latest_block_roots: Vec<Hash256>,
|
||||
pub latest_penalized_exit_balances: Vec<u64>,
|
||||
pub latest_attestations: Vec<PendingAttestationRecord>,
|
||||
pub latest_attestations: Vec<PendingAttestation>,
|
||||
pub batched_block_roots: Vec<Hash256>,
|
||||
|
||||
// PoW receipt root (a.k.a. deposit root)
|
||||
pub processed_pow_receipt_root: Hash256,
|
||||
pub candidate_pow_receipt_roots: Vec<CandidatePoWReceiptRootRecord>,
|
||||
// Ethereum 1.0 chain data
|
||||
pub latest_eth1_data: Eth1Data,
|
||||
pub eth1_data_votes: Vec<Eth1DataVote>,
|
||||
}
|
||||
|
||||
impl BeaconState {
|
||||
@@ -72,7 +73,7 @@ impl Encodable for BeaconState {
|
||||
s.append(&self.fork_data);
|
||||
s.append(&self.validator_registry);
|
||||
s.append(&self.validator_balances);
|
||||
s.append(&self.validator_registry_latest_change_slot);
|
||||
s.append(&self.validator_registry_update_slot);
|
||||
s.append(&self.validator_registry_exit_count);
|
||||
s.append(&self.validator_registry_delta_chain_tip);
|
||||
s.append(&self.latest_randao_mixes);
|
||||
@@ -93,8 +94,8 @@ impl Encodable for BeaconState {
|
||||
s.append(&self.latest_penalized_exit_balances);
|
||||
s.append(&self.latest_attestations);
|
||||
s.append(&self.batched_block_roots);
|
||||
s.append(&self.processed_pow_receipt_root);
|
||||
s.append(&self.candidate_pow_receipt_roots);
|
||||
s.append(&self.latest_eth1_data);
|
||||
s.append(&self.eth1_data_votes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +106,7 @@ impl Decodable for BeaconState {
|
||||
let (fork_data, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (validator_registry, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (validator_balances, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (validator_registry_latest_change_slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (validator_registry_update_slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (validator_registry_exit_count, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (validator_registry_delta_chain_tip, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (latest_randao_mixes, i) = <_>::ssz_decode(bytes, i)?;
|
||||
@@ -126,8 +127,8 @@ impl Decodable for BeaconState {
|
||||
let (latest_penalized_exit_balances, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (latest_attestations, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (batched_block_roots, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (processed_pow_receipt_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (candidate_pow_receipt_roots, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (latest_eth1_data, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (eth1_data_votes, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
Self {
|
||||
@@ -136,7 +137,7 @@ impl Decodable for BeaconState {
|
||||
fork_data,
|
||||
validator_registry,
|
||||
validator_balances,
|
||||
validator_registry_latest_change_slot,
|
||||
validator_registry_update_slot,
|
||||
validator_registry_exit_count,
|
||||
validator_registry_delta_chain_tip,
|
||||
latest_randao_mixes,
|
||||
@@ -157,8 +158,8 @@ impl Decodable for BeaconState {
|
||||
latest_penalized_exit_balances,
|
||||
latest_attestations,
|
||||
batched_block_roots,
|
||||
processed_pow_receipt_root,
|
||||
candidate_pow_receipt_roots,
|
||||
latest_eth1_data,
|
||||
eth1_data_votes,
|
||||
},
|
||||
i,
|
||||
))
|
||||
@@ -173,7 +174,7 @@ impl<T: RngCore> TestRandom<T> for BeaconState {
|
||||
fork_data: <_>::random_for_test(rng),
|
||||
validator_registry: <_>::random_for_test(rng),
|
||||
validator_balances: <_>::random_for_test(rng),
|
||||
validator_registry_latest_change_slot: <_>::random_for_test(rng),
|
||||
validator_registry_update_slot: <_>::random_for_test(rng),
|
||||
validator_registry_exit_count: <_>::random_for_test(rng),
|
||||
validator_registry_delta_chain_tip: <_>::random_for_test(rng),
|
||||
latest_randao_mixes: <_>::random_for_test(rng),
|
||||
@@ -194,8 +195,8 @@ impl<T: RngCore> TestRandom<T> for BeaconState {
|
||||
latest_penalized_exit_balances: <_>::random_for_test(rng),
|
||||
latest_attestations: <_>::random_for_test(rng),
|
||||
batched_block_roots: <_>::random_for_test(rng),
|
||||
processed_pow_receipt_root: <_>::random_for_test(rng),
|
||||
candidate_pow_receipt_roots: <_>::random_for_test(rng),
|
||||
latest_eth1_data: <_>::random_for_test(rng),
|
||||
eth1_data_votes: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@ use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct CrosslinkRecord {
|
||||
pub struct Crosslink {
|
||||
pub slot: u64,
|
||||
pub shard_block_root: Hash256,
|
||||
}
|
||||
|
||||
impl CrosslinkRecord {
|
||||
impl Crosslink {
|
||||
/// Generates a new instance where `dynasty` and `hash` are both zero.
|
||||
pub fn zero() -> Self {
|
||||
Self {
|
||||
@@ -19,14 +19,14 @@ impl CrosslinkRecord {
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for CrosslinkRecord {
|
||||
impl Encodable for Crosslink {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.slot);
|
||||
s.append(&self.shard_block_root);
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for CrosslinkRecord {
|
||||
impl Decodable for Crosslink {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (shard_block_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||
@@ -41,7 +41,7 @@ impl Decodable for CrosslinkRecord {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for CrosslinkRecord {
|
||||
impl<T: RngCore> TestRandom<T> for Crosslink {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
slot: <_>::random_for_test(rng),
|
||||
@@ -59,7 +59,7 @@ mod tests {
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = CrosslinkRecord::random_for_test(&mut rng);
|
||||
let original = Crosslink::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
@@ -5,30 +5,30 @@ use rand::RngCore;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct DepositData {
|
||||
pub deposit_input: DepositInput,
|
||||
pub value: u64,
|
||||
pub amount: u64,
|
||||
pub timestamp: u64,
|
||||
pub deposit_input: DepositInput,
|
||||
}
|
||||
|
||||
impl Encodable for DepositData {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.deposit_input);
|
||||
s.append(&self.value);
|
||||
s.append(&self.amount);
|
||||
s.append(&self.timestamp);
|
||||
s.append(&self.deposit_input);
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for DepositData {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (deposit_input, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (value, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (amount, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (timestamp, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (deposit_input, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
Self {
|
||||
deposit_input,
|
||||
value,
|
||||
amount,
|
||||
timestamp,
|
||||
deposit_input,
|
||||
},
|
||||
i,
|
||||
))
|
||||
@@ -38,9 +38,9 @@ impl Decodable for DepositData {
|
||||
impl<T: RngCore> TestRandom<T> for DepositData {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
deposit_input: <_>::random_for_test(rng),
|
||||
value: <_>::random_for_test(rng),
|
||||
amount: <_>::random_for_test(rng),
|
||||
timestamp: <_>::random_for_test(rng),
|
||||
deposit_input: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@ use rand::RngCore;
|
||||
pub struct DepositInput {
|
||||
pub pubkey: PublicKey,
|
||||
pub withdrawal_credentials: Hash256,
|
||||
pub randao_commitment: Hash256,
|
||||
pub custody_commitment: Hash256,
|
||||
pub proof_of_possession: Signature,
|
||||
}
|
||||
|
||||
@@ -17,8 +15,6 @@ impl Encodable for DepositInput {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.pubkey);
|
||||
s.append(&self.withdrawal_credentials);
|
||||
s.append(&self.randao_commitment);
|
||||
s.append(&self.custody_commitment);
|
||||
s.append(&self.proof_of_possession);
|
||||
}
|
||||
}
|
||||
@@ -27,16 +23,12 @@ impl Decodable for DepositInput {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (pubkey, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (withdrawal_credentials, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (custody_commitment, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (proof_of_possession, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
Self {
|
||||
pubkey,
|
||||
withdrawal_credentials,
|
||||
randao_commitment,
|
||||
custody_commitment,
|
||||
proof_of_possession,
|
||||
},
|
||||
i,
|
||||
@@ -49,8 +41,6 @@ impl<T: RngCore> TestRandom<T> for DepositInput {
|
||||
Self {
|
||||
pubkey: <_>::random_for_test(rng),
|
||||
withdrawal_credentials: <_>::random_for_test(rng),
|
||||
randao_commitment: <_>::random_for_test(rng),
|
||||
custody_commitment: <_>::random_for_test(rng),
|
||||
proof_of_possession: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,39 +4,39 @@ use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
// Note: this is refer to as DepositRootVote in specs
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct CandidatePoWReceiptRootRecord {
|
||||
pub candidate_pow_receipt_root: Hash256,
|
||||
pub votes: u64,
|
||||
#[derive(Debug, PartialEq, Clone, Default)]
|
||||
pub struct Eth1Data {
|
||||
pub deposit_root: Hash256,
|
||||
pub block_hash: Hash256,
|
||||
}
|
||||
|
||||
impl Encodable for CandidatePoWReceiptRootRecord {
|
||||
impl Encodable for Eth1Data {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.candidate_pow_receipt_root);
|
||||
s.append(&self.votes);
|
||||
s.append(&self.deposit_root);
|
||||
s.append(&self.block_hash);
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for CandidatePoWReceiptRootRecord {
|
||||
impl Decodable for Eth1Data {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (candidate_pow_receipt_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (votes, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (deposit_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (block_hash, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
Self {
|
||||
candidate_pow_receipt_root,
|
||||
votes,
|
||||
deposit_root,
|
||||
block_hash,
|
||||
},
|
||||
i,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for CandidatePoWReceiptRootRecord {
|
||||
impl<T: RngCore> TestRandom<T> for Eth1Data {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
candidate_pow_receipt_root: <_>::random_for_test(rng),
|
||||
votes: <_>::random_for_test(rng),
|
||||
deposit_root: <_>::random_for_test(rng),
|
||||
block_hash: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ mod tests {
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = CandidatePoWReceiptRootRecord::random_for_test(&mut rng);
|
||||
let original = Eth1Data::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
60
eth2/types/src/eth1_data_vote.rs
Normal file
60
eth2/types/src/eth1_data_vote.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::Eth1Data;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
// Note: this is refer to as DepositRootVote in specs
|
||||
#[derive(Debug, PartialEq, Clone, Default)]
|
||||
pub struct Eth1DataVote {
|
||||
pub eth1_data: Eth1Data,
|
||||
pub vote_count: u64,
|
||||
}
|
||||
|
||||
impl Encodable for Eth1DataVote {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.eth1_data);
|
||||
s.append(&self.vote_count);
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for Eth1DataVote {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (eth1_data, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (vote_count, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
Self {
|
||||
eth1_data,
|
||||
vote_count,
|
||||
},
|
||||
i,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for Eth1DataVote {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
eth1_data: <_>::random_for_test(rng),
|
||||
vote_count: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = Eth1DataVote::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,13 @@ use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct ForkData {
|
||||
pub struct Fork {
|
||||
pub pre_fork_version: u64,
|
||||
pub post_fork_version: u64,
|
||||
pub fork_slot: u64,
|
||||
}
|
||||
|
||||
impl Encodable for ForkData {
|
||||
impl Encodable for Fork {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.pre_fork_version);
|
||||
s.append(&self.post_fork_version);
|
||||
@@ -17,7 +17,7 @@ impl Encodable for ForkData {
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for ForkData {
|
||||
impl Decodable for Fork {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (pre_fork_version, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (post_fork_version, i) = <_>::ssz_decode(bytes, i)?;
|
||||
@@ -34,7 +34,7 @@ impl Decodable for ForkData {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for ForkData {
|
||||
impl<T: RngCore> TestRandom<T> for Fork {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
pre_fork_version: <_>::random_for_test(rng),
|
||||
@@ -53,7 +53,7 @@ mod tests {
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = ForkData::random_for_test(&mut rng);
|
||||
let original = Fork::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
@@ -11,23 +11,25 @@ pub mod attestation_data_and_custody_bit;
|
||||
pub mod beacon_block;
|
||||
pub mod beacon_block_body;
|
||||
pub mod beacon_state;
|
||||
pub mod candidate_pow_receipt_root_record;
|
||||
pub mod casper_slashing;
|
||||
pub mod crosslink_record;
|
||||
pub mod crosslink;
|
||||
pub mod deposit;
|
||||
pub mod deposit_data;
|
||||
pub mod deposit_input;
|
||||
pub mod eth1_data;
|
||||
pub mod eth1_data_vote;
|
||||
pub mod exit;
|
||||
pub mod fork_data;
|
||||
pub mod pending_attestation_record;
|
||||
pub mod fork;
|
||||
pub mod pending_attestation;
|
||||
pub mod proposal_signed_data;
|
||||
pub mod proposer_slashing;
|
||||
pub mod shard_committee;
|
||||
pub mod shard_reassignment_record;
|
||||
pub mod slashable_vote_data;
|
||||
pub mod special_record;
|
||||
pub mod validator_record;
|
||||
pub mod validator;
|
||||
pub mod validator_registry;
|
||||
pub mod validator_registry_delta_block;
|
||||
|
||||
pub mod readers;
|
||||
|
||||
@@ -41,19 +43,22 @@ pub use crate::beacon_block::BeaconBlock;
|
||||
pub use crate::beacon_block_body::BeaconBlockBody;
|
||||
pub use crate::beacon_state::BeaconState;
|
||||
pub use crate::casper_slashing::CasperSlashing;
|
||||
pub use crate::crosslink_record::CrosslinkRecord;
|
||||
pub use crate::crosslink::Crosslink;
|
||||
pub use crate::deposit::Deposit;
|
||||
pub use crate::deposit_data::DepositData;
|
||||
pub use crate::deposit_input::DepositInput;
|
||||
pub use crate::eth1_data::Eth1Data;
|
||||
pub use crate::eth1_data_vote::Eth1DataVote;
|
||||
pub use crate::exit::Exit;
|
||||
pub use crate::fork_data::ForkData;
|
||||
pub use crate::pending_attestation_record::PendingAttestationRecord;
|
||||
pub use crate::fork::Fork;
|
||||
pub use crate::pending_attestation::PendingAttestation;
|
||||
pub use crate::proposal_signed_data::ProposalSignedData;
|
||||
pub use crate::proposer_slashing::ProposerSlashing;
|
||||
pub use crate::shard_committee::ShardCommittee;
|
||||
pub use crate::slashable_vote_data::SlashableVoteData;
|
||||
pub use crate::special_record::{SpecialRecord, SpecialRecordKind};
|
||||
pub use crate::validator_record::{StatusFlags as ValidatorStatusFlags, ValidatorRecord};
|
||||
pub use crate::validator::{StatusFlags as ValidatorStatusFlags, Validator};
|
||||
pub use crate::validator_registry_delta_block::ValidatorRegistryDeltaBlock;
|
||||
|
||||
pub type Hash256 = H256;
|
||||
pub type Address = H160;
|
||||
|
||||
@@ -4,14 +4,14 @@ use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct PendingAttestationRecord {
|
||||
pub struct PendingAttestation {
|
||||
pub data: AttestationData,
|
||||
pub aggregation_bitfield: Bitfield,
|
||||
pub custody_bitfield: Bitfield,
|
||||
pub slot_included: u64,
|
||||
}
|
||||
|
||||
impl Encodable for PendingAttestationRecord {
|
||||
impl Encodable for PendingAttestation {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.data);
|
||||
s.append(&self.aggregation_bitfield);
|
||||
@@ -20,7 +20,7 @@ impl Encodable for PendingAttestationRecord {
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for PendingAttestationRecord {
|
||||
impl Decodable for PendingAttestation {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (data, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (aggregation_bitfield, i) = <_>::ssz_decode(bytes, i)?;
|
||||
@@ -39,7 +39,7 @@ impl Decodable for PendingAttestationRecord {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for PendingAttestationRecord {
|
||||
impl<T: RngCore> TestRandom<T> for PendingAttestation {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
data: <_>::random_for_test(rng),
|
||||
@@ -59,7 +59,7 @@ mod tests {
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = PendingAttestationRecord::random_for_test(&mut rng);
|
||||
let original = PendingAttestation::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
@@ -6,16 +6,16 @@ use rand::RngCore;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct SlashableVoteData {
|
||||
pub aggregate_signature_poc_0_indices: Vec<u32>,
|
||||
pub aggregate_signature_poc_1_indices: Vec<u32>,
|
||||
pub custody_bit_0_indices: Vec<u32>,
|
||||
pub custody_bit_1_indices: Vec<u32>,
|
||||
pub data: AttestationData,
|
||||
pub aggregate_signature: AggregateSignature,
|
||||
}
|
||||
|
||||
impl Encodable for SlashableVoteData {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append_vec(&self.aggregate_signature_poc_0_indices);
|
||||
s.append_vec(&self.aggregate_signature_poc_1_indices);
|
||||
s.append_vec(&self.custody_bit_0_indices);
|
||||
s.append_vec(&self.custody_bit_1_indices);
|
||||
s.append(&self.data);
|
||||
s.append(&self.aggregate_signature);
|
||||
}
|
||||
@@ -23,15 +23,15 @@ impl Encodable for SlashableVoteData {
|
||||
|
||||
impl Decodable for SlashableVoteData {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (aggregate_signature_poc_0_indices, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (aggregate_signature_poc_1_indices, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (custody_bit_0_indices, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (custody_bit_1_indices, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (data, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (aggregate_signature, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
SlashableVoteData {
|
||||
aggregate_signature_poc_0_indices,
|
||||
aggregate_signature_poc_1_indices,
|
||||
custody_bit_0_indices,
|
||||
custody_bit_1_indices,
|
||||
data,
|
||||
aggregate_signature,
|
||||
},
|
||||
@@ -43,8 +43,8 @@ impl Decodable for SlashableVoteData {
|
||||
impl<T: RngCore> TestRandom<T> for SlashableVoteData {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
aggregate_signature_poc_0_indices: <_>::random_for_test(rng),
|
||||
aggregate_signature_poc_1_indices: <_>::random_for_test(rng),
|
||||
custody_bit_0_indices: <_>::random_for_test(rng),
|
||||
custody_bit_1_indices: <_>::random_for_test(rng),
|
||||
data: <_>::random_for_test(rng),
|
||||
aggregate_signature: <_>::random_for_test(rng),
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ impl From<StatusFlagsDecodeError> for DecodeError {
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles the serialization logic for the `status_flags` field of the `ValidatorRecord`.
|
||||
/// Handles the serialization logic for the `status_flags` field of the `Validator`.
|
||||
fn status_flag_to_byte(flag: Option<StatusFlags>) -> u8 {
|
||||
if let Some(flag) = flag {
|
||||
match flag {
|
||||
@@ -33,7 +33,7 @@ fn status_flag_to_byte(flag: Option<StatusFlags>) -> u8 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles the deserialization logic for the `status_flags` field of the `ValidatorRecord`.
|
||||
/// Handles the deserialization logic for the `status_flags` field of the `Validator`.
|
||||
fn status_flag_from_byte(flag: u8) -> Result<Option<StatusFlags>, StatusFlagsDecodeError> {
|
||||
match flag {
|
||||
0 => Ok(None),
|
||||
@@ -44,46 +44,40 @@ fn status_flag_from_byte(flag: u8) -> Result<Option<StatusFlags>, StatusFlagsDec
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct ValidatorRecord {
|
||||
pub struct Validator {
|
||||
pub pubkey: PublicKey,
|
||||
pub withdrawal_credentials: Hash256,
|
||||
pub proposer_slots: u64,
|
||||
pub randao_commitment: Hash256,
|
||||
pub randao_layers: u64,
|
||||
pub activation_slot: u64,
|
||||
pub exit_slot: u64,
|
||||
pub withdrawal_slot: u64,
|
||||
pub penalized_slot: u64,
|
||||
pub exit_count: u64,
|
||||
pub status_flags: Option<StatusFlags>,
|
||||
pub custody_commitment: Hash256,
|
||||
pub latest_custody_reseed_slot: u64,
|
||||
pub penultimate_custody_reseed_slot: u64,
|
||||
}
|
||||
|
||||
impl ValidatorRecord {
|
||||
impl Validator {
|
||||
/// This predicate indicates if the validator represented by this record is considered "active" at `slot`.
|
||||
pub fn is_active_at(&self, slot: u64) -> bool {
|
||||
self.activation_slot <= slot && slot < self.exit_slot
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ValidatorRecord {
|
||||
/// Yields a "default" `ValidatorRecord`. Primarily used for testing.
|
||||
impl Default for Validator {
|
||||
/// Yields a "default" `Validator`. Primarily used for testing.
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
pubkey: PublicKey::default(),
|
||||
withdrawal_credentials: Hash256::default(),
|
||||
proposer_slots: 0,
|
||||
randao_commitment: Hash256::default(),
|
||||
randao_layers: 0,
|
||||
activation_slot: std::u64::MAX,
|
||||
exit_slot: std::u64::MAX,
|
||||
withdrawal_slot: std::u64::MAX,
|
||||
penalized_slot: std::u64::MAX,
|
||||
exit_count: 0,
|
||||
status_flags: None,
|
||||
custody_commitment: Hash256::default(),
|
||||
latest_custody_reseed_slot: 0, // NOTE: is `GENESIS_SLOT`
|
||||
penultimate_custody_reseed_slot: 0, // NOTE: is `GENESIS_SLOT`
|
||||
}
|
||||
@@ -97,39 +91,33 @@ impl<T: RngCore> TestRandom<T> for StatusFlags {
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for ValidatorRecord {
|
||||
impl Encodable for Validator {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.pubkey);
|
||||
s.append(&self.withdrawal_credentials);
|
||||
s.append(&self.proposer_slots);
|
||||
s.append(&self.randao_commitment);
|
||||
s.append(&self.randao_layers);
|
||||
s.append(&self.activation_slot);
|
||||
s.append(&self.exit_slot);
|
||||
s.append(&self.withdrawal_slot);
|
||||
s.append(&self.penalized_slot);
|
||||
s.append(&self.exit_count);
|
||||
s.append(&status_flag_to_byte(self.status_flags));
|
||||
s.append(&self.custody_commitment);
|
||||
s.append(&self.latest_custody_reseed_slot);
|
||||
s.append(&self.penultimate_custody_reseed_slot);
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for ValidatorRecord {
|
||||
impl Decodable for Validator {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (pubkey, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (withdrawal_credentials, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (proposer_slots, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (randao_layers, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (activation_slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (exit_slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (withdrawal_slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (penalized_slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (exit_count, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (status_flags_byte, i): (u8, usize) = <_>::ssz_decode(bytes, i)?;
|
||||
let (custody_commitment, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (latest_custody_reseed_slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (penultimate_custody_reseed_slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
@@ -140,15 +128,12 @@ impl Decodable for ValidatorRecord {
|
||||
pubkey,
|
||||
withdrawal_credentials,
|
||||
proposer_slots,
|
||||
randao_commitment,
|
||||
randao_layers,
|
||||
activation_slot,
|
||||
exit_slot,
|
||||
withdrawal_slot,
|
||||
penalized_slot,
|
||||
exit_count,
|
||||
status_flags,
|
||||
custody_commitment,
|
||||
latest_custody_reseed_slot,
|
||||
penultimate_custody_reseed_slot,
|
||||
},
|
||||
@@ -157,21 +142,18 @@ impl Decodable for ValidatorRecord {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for ValidatorRecord {
|
||||
impl<T: RngCore> TestRandom<T> for Validator {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
pubkey: <_>::random_for_test(rng),
|
||||
withdrawal_credentials: <_>::random_for_test(rng),
|
||||
proposer_slots: <_>::random_for_test(rng),
|
||||
randao_commitment: <_>::random_for_test(rng),
|
||||
randao_layers: <_>::random_for_test(rng),
|
||||
activation_slot: <_>::random_for_test(rng),
|
||||
exit_slot: <_>::random_for_test(rng),
|
||||
withdrawal_slot: <_>::random_for_test(rng),
|
||||
penalized_slot: <_>::random_for_test(rng),
|
||||
exit_count: <_>::random_for_test(rng),
|
||||
status_flags: Some(<_>::random_for_test(rng)),
|
||||
custody_commitment: <_>::random_for_test(rng),
|
||||
latest_custody_reseed_slot: <_>::random_for_test(rng),
|
||||
penultimate_custody_reseed_slot: <_>::random_for_test(rng),
|
||||
}
|
||||
@@ -187,7 +169,7 @@ mod tests {
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = ValidatorRecord::random_for_test(&mut rng);
|
||||
let original = Validator::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
@@ -198,7 +180,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_validator_can_be_active() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let mut validator = ValidatorRecord::random_for_test(&mut rng);
|
||||
let mut validator = Validator::random_for_test(&mut rng);
|
||||
|
||||
let activation_slot = u64::random_for_test(&mut rng);
|
||||
let exit_slot = activation_slot + 234;
|
||||
@@ -1,12 +0,0 @@
|
||||
use super::{Address, Hash256};
|
||||
use bls::{PublicKey, Signature};
|
||||
|
||||
/// The information gathered from the PoW chain validator registration function.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct ValidatorRegistration {
|
||||
pub pubkey: PublicKey,
|
||||
pub withdrawal_shard: u64,
|
||||
pub withdrawal_address: Address,
|
||||
pub randao_commitment: Hash256,
|
||||
pub proof_of_possession: Signature,
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
/// Contains logic to manipulate a `&[ValidatorRecord]`.
|
||||
/// Contains logic to manipulate a `&[Validator]`.
|
||||
/// For now, we avoid defining a newtype and just have flat functions here.
|
||||
use super::validator_record::*;
|
||||
use super::validator::*;
|
||||
|
||||
/// Given an indexed sequence of `validators`, return the indices corresponding to validators that are active at `slot`.
|
||||
pub fn get_active_validator_indices(validators: &[ValidatorRecord], slot: u64) -> Vec<usize> {
|
||||
pub fn get_active_validator_indices(validators: &[Validator], slot: u64) -> Vec<usize> {
|
||||
validators
|
||||
.iter()
|
||||
.enumerate()
|
||||
@@ -38,7 +38,7 @@ mod tests {
|
||||
let mut validators = vec![];
|
||||
let count_validators = 10;
|
||||
for _ in 0..count_validators {
|
||||
validators.push(ValidatorRecord::default())
|
||||
validators.push(Validator::default())
|
||||
}
|
||||
|
||||
let some_slot = u64::random_for_test(&mut rng);
|
||||
@@ -55,7 +55,7 @@ mod tests {
|
||||
let mut validators = (0..count_validators)
|
||||
.into_iter()
|
||||
.map(|_| {
|
||||
let mut validator = ValidatorRecord::default();
|
||||
let mut validator = Validator::default();
|
||||
|
||||
let activation_offset = u64::random_for_test(&mut rng);
|
||||
let exit_offset = u64::random_for_test(&mut rng);
|
||||
@@ -79,7 +79,7 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
fn set_validators_to_default_entry_exit(validators: &mut [ValidatorRecord]) {
|
||||
fn set_validators_to_default_entry_exit(validators: &mut [Validator]) {
|
||||
for validator in validators.iter_mut() {
|
||||
validator.activation_slot = std::u64::MAX;
|
||||
validator.exit_slot = std::u64::MAX;
|
||||
@@ -87,7 +87,7 @@ mod tests {
|
||||
}
|
||||
|
||||
// sets all `validators` to be active as of some slot prior to `slot`. returns the activation slot.
|
||||
fn set_validators_to_activated(validators: &mut [ValidatorRecord], slot: u64) -> u64 {
|
||||
fn set_validators_to_activated(validators: &mut [Validator], slot: u64) -> u64 {
|
||||
let activation_slot = slot - 10;
|
||||
for validator in validators.iter_mut() {
|
||||
validator.activation_slot = activation_slot;
|
||||
@@ -96,11 +96,7 @@ mod tests {
|
||||
}
|
||||
|
||||
// sets all `validators` to be exited as of some slot before `slot`.
|
||||
fn set_validators_to_exited(
|
||||
validators: &mut [ValidatorRecord],
|
||||
slot: u64,
|
||||
activation_slot: u64,
|
||||
) {
|
||||
fn set_validators_to_exited(validators: &mut [Validator], slot: u64, activation_slot: u64) {
|
||||
assert!(activation_slot < slot);
|
||||
let mut exit_slot = activation_slot + 10;
|
||||
while exit_slot >= slot {
|
||||
@@ -123,7 +119,7 @@ mod tests {
|
||||
let mut validators = (0..COUNT_VALIDATORS)
|
||||
.into_iter()
|
||||
.map(|_| {
|
||||
let mut validator = ValidatorRecord::default();
|
||||
let mut validator = Validator::default();
|
||||
|
||||
let activation_offset = u64::random_for_test(&mut rng);
|
||||
let exit_offset = u64::random_for_test(&mut rng);
|
||||
|
||||
89
eth2/types/src/validator_registry_delta_block.rs
Normal file
89
eth2/types/src/validator_registry_delta_block.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
use super::Hash256;
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::PublicKey;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
|
||||
// The information gathered from the PoW chain validator registration function.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct ValidatorRegistryDeltaBlock {
|
||||
pub latest_registry_delta_root: Hash256,
|
||||
pub validator_index: u32,
|
||||
pub pubkey: PublicKey,
|
||||
pub slot: u64,
|
||||
pub flag: u64,
|
||||
}
|
||||
|
||||
impl Default for ValidatorRegistryDeltaBlock {
|
||||
/// Yields a "default" `Validator`. Primarily used for testing.
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
latest_registry_delta_root: Hash256::zero(),
|
||||
validator_index: std::u32::MAX,
|
||||
pubkey: PublicKey::default(),
|
||||
slot: std::u64::MAX,
|
||||
flag: std::u64::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for ValidatorRegistryDeltaBlock {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.latest_registry_delta_root);
|
||||
s.append(&self.validator_index);
|
||||
s.append(&self.pubkey);
|
||||
s.append(&self.slot);
|
||||
s.append(&self.flag);
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for ValidatorRegistryDeltaBlock {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (latest_registry_delta_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (validator_index, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (pubkey, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (flag, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
Self {
|
||||
latest_registry_delta_root,
|
||||
validator_index,
|
||||
pubkey,
|
||||
slot,
|
||||
flag,
|
||||
},
|
||||
i,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for ValidatorRegistryDeltaBlock {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
latest_registry_delta_root: <_>::random_for_test(rng),
|
||||
validator_index: <_>::random_for_test(rng),
|
||||
pubkey: <_>::random_for_test(rng),
|
||||
slot: <_>::random_for_test(rng),
|
||||
flag: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = ValidatorRegistryDeltaBlock::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user