Added/modified types and constants according to specs

This commit is contained in:
Kirk Baird 2019-01-16 10:39:02 +11:00
parent 0b47b81a6c
commit 8dec3c1dc7
No known key found for this signature in database
GPG Key ID: BF864B7ED0BEA33F
9 changed files with 160 additions and 48 deletions

View File

@ -1,17 +1,15 @@
use bls::{Signature, BLS_AGG_SIG_BYTE_SIZE};
use spec::ChainSpec;
use ssz::{encode::encode_length, Decodable, LENGTH_BYTES};
use types::{BeaconBlock, BeaconBlockBody, Hash256};
/// Generate a genesis BeaconBlock.
pub fn genesis_beacon_block(state_root: Hash256, spec: &ChainSpec) -> BeaconBlock {
BeaconBlock {
slot: spec.initial_slot_number,
slot: spec.genesis_slot_number,
parent_root: spec.zero_hash,
state_root,
randao_reveal: spec.zero_hash,
candidate_pow_receipt_root: spec.zero_hash,
signature: genesis_signature(),
signature: spec.empty_signature.clone(),
body: BeaconBlockBody {
proposer_slashings: vec![],
casper_slashings: vec![],
@ -25,19 +23,11 @@ pub fn genesis_beacon_block(state_root: Hash256, spec: &ChainSpec) -> BeaconBloc
}
}
fn genesis_signature() -> Signature {
let mut bytes = encode_length(BLS_AGG_SIG_BYTE_SIZE, LENGTH_BYTES);
bytes.append(&mut vec![0; BLS_AGG_SIG_BYTE_SIZE]);
let (signature, _) = match Signature::ssz_decode(&bytes, 0) {
Ok(sig) => sig,
Err(_) => unreachable!(),
};
signature
}
#[cfg(test)]
mod tests {
use super::*;
use bls::{Signature};
#[test]
fn test_genesis() {
@ -101,5 +91,6 @@ mod tests {
for item in raw_sig_bytes.iter() {
assert!(*item == 0);
}
assert_eq!(genesis_block.signature, Signature::empty_sig());
}
}

View File

@ -21,7 +21,7 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result<BeaconState, Error> {
};
let initial_crosslink = CrosslinkRecord {
slot: spec.initial_slot_number,
slot: spec.genesis_slot_number,
shard_block_root: spec.zero_hash,
};
@ -29,19 +29,19 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result<BeaconState, Error> {
/*
* Misc
*/
slot: spec.initial_slot_number,
slot: spec.genesis_slot_number,
genesis_time: spec.genesis_time,
fork_data: ForkData {
pre_fork_version: spec.initial_fork_version,
post_fork_version: spec.initial_fork_version,
fork_slot: spec.initial_slot_number,
pre_fork_version: spec.genesis_fork_version,
post_fork_version: spec.genesis_fork_version,
fork_slot: spec.genesis_slot_number,
},
/*
* Validator registry
*/
validator_registry: spec.initial_validators.clone(),
validator_balances: spec.initial_balances.clone(),
validator_registry_latest_change_slot: spec.initial_slot_number,
validator_registry_latest_change_slot: spec.genesis_slot_number,
validator_registry_exit_count: 0,
validator_registry_delta_chain_tip: spec.zero_hash,
/*
@ -52,7 +52,12 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result<BeaconState, Error> {
spec.zero_hash;
(spec.latest_randao_mixes_length / spec.epoch_length) as usize
],
shard_committees_at_slots: vec![],
previous_epoch_start_shard: spec.genesis_start_shard,
current_epoch_start_shard: spec.genesis_start_shard,
previous_epoch_calculation_slot: spec.genesis_slot_number,
current_epoch_calculation_slot: spec.genesis_slot_number,
previous_epoch_randao_mix: spec.zero_hash,
current_epoch_randao_mix: spec.zero_hash,
/*
* Custody challenges
*/
@ -60,10 +65,10 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result<BeaconState, Error> {
/*
* Finality
*/
previous_justified_slot: spec.initial_slot_number,
justified_slot: spec.initial_slot_number,
previous_justified_slot: spec.genesis_slot_number,
justified_slot: spec.genesis_slot_number,
justification_bitfield: 0,
finalized_slot: spec.initial_slot_number,
finalized_slot: spec.genesis_slot_number,
/*
* Recent state
*/

View File

@ -1,5 +1,5 @@
use super::ChainSpec;
use bls::{Keypair, PublicKey, SecretKey};
use bls::{Keypair, PublicKey, SecretKey, Signature};
use types::{Address, Hash256, ValidatorRecord};
@ -17,12 +17,11 @@ impl ChainSpec {
* Misc
*/
shard_count: 1_024,
target_committee_size: 256,
target_committee_size: 128,
ejection_balance: 16,
max_balance_churn_quotient: 32,
gwei_per_eth: u64::pow(10, 9),
beacon_chain_shard_number: u64::max_value(),
bls_withdrawal_prefix_byte: 0x00,
max_casper_votes: 1_024,
latest_block_roots_length: 8_192,
latest_randao_mixes_length: 8_192,
@ -38,35 +37,58 @@ impl ChainSpec {
/*
* Initial Values
*/
initial_fork_version: 0,
initial_slot_number: 0,
genesis_fork_version: 0,
genesis_slot_number: 0,
genesis_start_shard: 0,
far_future_slot: u64::max_value(),
zero_hash: Hash256::zero(),
empty_signature: Signature::empty_sig(),
bls_withdrawal_prefix_byte: 0x00,
/*
* Time parameters
*/
slot_duration: 6,
min_attestation_inclusion_delay: 4,
epoch_length: 64,
seed_lookahead: 64,
min_validator_registry_change_interval: 256,
pow_receipt_root_voting_period: 1_024,
shard_persistent_committee_change_period: u64::pow(2, 17),
collective_penalty_calculation_period: u64::pow(2, 20),
zero_balance_validator_ttl: u64::pow(2, 22),
min_validator_withdrawal_time: u64::pow(2, 14),
shard_persistent_committee_change_period: u64::pow(2, 17), // old
collective_penalty_calculation_period: u64::pow(2, 20), // old
zero_balance_validator_ttl: u64::pow(2, 22), // old
/*
* Reward and penalty quotients
*/
base_reward_quotient: 2_048,
base_reward_quotient: 1_024,
whistleblower_reward_quotient: 512,
includer_reward_quotient: 8,
inactivity_penalty_quotient: u64::pow(2, 34),
inactivity_penalty_quotient: u64::pow(2, 24),
/*
* Status flags
*/
initiated_exit: 1,
withdrawable: 2,
/*
* Max operations per block
*/
max_proposer_slashings: 16,
max_casper_slashings: 15,
max_casper_slashings: 16,
max_attestations: 128,
max_deposits: 16,
max_exits: 16,
/*
* Validator registry delta flags
*/
activation: 0,
exit: 1,
/*
* Signature domains
*/
domain_deposit: 0,
domain_attestation: 1,
domain_proposal: 2,
domain_exit: 3,
/*
* Intialization parameters
*/
@ -107,6 +129,10 @@ fn initial_validators_for_testing() -> Vec<ValidatorRecord> {
withdrawal_credentials: Hash256::zero(),
randao_commitment: Hash256::zero(),
randao_layers: 0,
activation_slot: u64::max_value(),
exit_slot: u64::max_value(),
withdrawal_slot: u64::max_value(),
penalized_slot: u64::max_value(),
status: From::from(0),
latest_status_change_slot: 0,
exit_count: 0,

View File

@ -3,6 +3,7 @@ extern crate types;
mod foundation;
use bls::Signature;
use types::{Address, Hash256, ValidatorRecord};
#[derive(PartialEq, Debug)]
@ -16,7 +17,6 @@ pub struct ChainSpec {
pub max_balance_churn_quotient: u64,
pub gwei_per_eth: u64,
pub beacon_chain_shard_number: u64,
pub bls_withdrawal_prefix_byte: u8,
pub max_casper_votes: u64,
pub latest_block_roots_length: u64,
pub latest_randao_mixes_length: u64,
@ -32,20 +32,26 @@ pub struct ChainSpec {
/*
* Initial Values
*/
pub initial_fork_version: u64,
pub initial_slot_number: u64,
pub genesis_fork_version: u64,
pub genesis_slot_number: u64,
pub genesis_start_shard: u64,
pub far_future_slot: u64,
pub zero_hash: Hash256,
pub empty_signature: Signature,
pub bls_withdrawal_prefix_byte: u8,
/*
* Time parameters
*/
pub slot_duration: u64,
pub min_attestation_inclusion_delay: u64,
pub epoch_length: u64,
pub min_validator_registry_change_interval: u64,
pub pow_receipt_root_voting_period: u64,
pub shard_persistent_committee_change_period: u64,
pub collective_penalty_calculation_period: u64,
pub zero_balance_validator_ttl: u64,
pub seed_lookahead: u64,
pub min_validator_registry_change_interval: u64, // a.k.a. entry_exit_delay
pub pow_receipt_root_voting_period: u64, // a.k. deposit_root_voting_period
pub min_validator_withdrawal_time: u64,
pub shard_persistent_committee_change_period: u64, //old
pub collective_penalty_calculation_period: u64, // old
pub zero_balance_validator_ttl: u64, // old
/*
* Reward and penalty quotients
*/
@ -53,6 +59,11 @@ pub struct ChainSpec {
pub whistleblower_reward_quotient: u64,
pub includer_reward_quotient: u64,
pub inactivity_penalty_quotient: u64,
/*
* Status flags
*/
pub initiated_exit: u64,
pub withdrawable: u64,
/*
* Max operations per block
*/
@ -61,6 +72,18 @@ pub struct ChainSpec {
pub max_attestations: u64,
pub max_deposits: u64,
pub max_exits: u64,
/*
* Validator registry delta flags
*/
pub activation: u64,
pub exit: u64,
/*
* Signature domains
*/
pub domain_deposit: u64,
pub domain_attestation: u64,
pub domain_proposal: u64,
pub domain_exit: u64,
/*
* Intialization parameters
*/

View File

@ -2,7 +2,6 @@ use super::candidate_pow_receipt_root_record::CandidatePoWReceiptRootRecord;
use super::crosslink_record::CrosslinkRecord;
use super::fork_data::ForkData;
use super::pending_attestation_record::PendingAttestationRecord;
use super::shard_committee::ShardCommittee;
use super::validator_record::ValidatorRecord;
use super::Hash256;
use crate::test_utils::TestRandom;
@ -30,7 +29,12 @@ pub struct BeaconState {
// Randomness and committees
pub latest_randao_mixes: Vec<Hash256>,
pub latest_vdf_outputs: Vec<Hash256>,
pub shard_committees_at_slots: Vec<Vec<ShardCommittee>>,
pub previous_epoch_start_shard: u64,
pub current_epoch_start_shard: u64,
pub previous_epoch_calculation_slot: u64,
pub current_epoch_calculation_slot: u64,
pub previous_epoch_randao_mix: Hash256,
pub current_epoch_randao_mix: Hash256,
// Custody challenges
pub custody_challenges: Vec<CustodyChallenge>,
@ -73,7 +77,12 @@ impl Encodable for BeaconState {
s.append(&self.validator_registry_delta_chain_tip);
s.append(&self.latest_randao_mixes);
s.append(&self.latest_vdf_outputs);
s.append(&self.shard_committees_at_slots);
s.append(&self.previous_epoch_start_shard);
s.append(&self.current_epoch_start_shard);
s.append(&self.previous_epoch_calculation_slot);
s.append(&self.current_epoch_calculation_slot);
s.append(&self.previous_epoch_randao_mix);
s.append(&self.current_epoch_randao_mix);
s.append(&self.custody_challenges);
s.append(&self.previous_justified_slot);
s.append(&self.justified_slot);
@ -101,7 +110,12 @@ impl Decodable for BeaconState {
let (validator_registry_delta_chain_tip, i) = <_>::ssz_decode(bytes, i)?;
let (latest_randao_mixes, i) = <_>::ssz_decode(bytes, i)?;
let (latest_vdf_outputs, i) = <_>::ssz_decode(bytes, i)?;
let (shard_committees_at_slots, i) = <_>::ssz_decode(bytes, i)?;
let (previous_epoch_start_shard, i) = <_>::ssz_decode(bytes, i)?;
let (current_epoch_start_shard, i) = <_>::ssz_decode(bytes, i)?;
let (previous_epoch_calculation_slot, i) = <_>::ssz_decode(bytes, i)?;
let (current_epoch_calculation_slot, i) = <_>::ssz_decode(bytes, i)?;
let (previous_epoch_randao_mix, i) = <_>::ssz_decode(bytes, i)?;
let (current_epoch_randao_mix, i) = <_>::ssz_decode(bytes, i)?;
let (custody_challenges, i) = <_>::ssz_decode(bytes, i)?;
let (previous_justified_slot, i) = <_>::ssz_decode(bytes, i)?;
let (justified_slot, i) = <_>::ssz_decode(bytes, i)?;
@ -127,7 +141,12 @@ impl Decodable for BeaconState {
validator_registry_delta_chain_tip,
latest_randao_mixes,
latest_vdf_outputs,
shard_committees_at_slots,
previous_epoch_start_shard,
current_epoch_start_shard,
previous_epoch_calculation_slot,
current_epoch_calculation_slot,
previous_epoch_randao_mix,
current_epoch_randao_mix,
custody_challenges,
previous_justified_slot,
justified_slot,
@ -159,7 +178,12 @@ impl<T: RngCore> TestRandom<T> for BeaconState {
validator_registry_delta_chain_tip: <_>::random_for_test(rng),
latest_randao_mixes: <_>::random_for_test(rng),
latest_vdf_outputs: <_>::random_for_test(rng),
shard_committees_at_slots: <_>::random_for_test(rng),
previous_epoch_start_shard: <_>::random_for_test(rng),
current_epoch_start_shard: <_>::random_for_test(rng),
previous_epoch_calculation_slot: <_>::random_for_test(rng),
current_epoch_calculation_slot: <_>::random_for_test(rng),
previous_epoch_randao_mix: <_>::random_for_test(rng),
current_epoch_randao_mix: <_>::random_for_test(rng),
custody_challenges: <_>::random_for_test(rng),
previous_justified_slot: <_>::random_for_test(rng),
justified_slot: <_>::random_for_test(rng),

View File

@ -3,6 +3,7 @@ use super::Hash256;
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,

View File

@ -35,6 +35,10 @@ pub struct ValidatorRecord {
pub withdrawal_credentials: Hash256,
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 status: ValidatorStatus,
pub latest_status_change_slot: u64,
pub exit_count: u64,
@ -99,6 +103,10 @@ impl Encodable for ValidatorRecord {
s.append(&self.withdrawal_credentials);
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.status);
s.append(&self.latest_status_change_slot);
s.append(&self.exit_count);
@ -114,6 +122,10 @@ impl Decodable for ValidatorRecord {
let (withdrawal_credentials, 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 (status, i) = <_>::ssz_decode(bytes, i)?;
let (latest_status_change_slot, i) = <_>::ssz_decode(bytes, i)?;
let (exit_count, i) = <_>::ssz_decode(bytes, i)?;
@ -127,6 +139,10 @@ impl Decodable for ValidatorRecord {
withdrawal_credentials,
randao_commitment,
randao_layers,
activation_slot,
exit_slot,
withdrawal_slot,
penalized_slot,
status,
latest_status_change_slot,
exit_count,
@ -146,6 +162,10 @@ impl<T: RngCore> TestRandom<T> for ValidatorRecord {
withdrawal_credentials: <_>::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),
status: <_>::random_for_test(rng),
latest_status_change_slot: <_>::random_for_test(rng),
exit_count: <_>::random_for_test(rng),

View File

@ -35,6 +35,12 @@ impl Signature {
pub fn as_raw(&self) -> &RawSignature {
&self.0
}
/// Returns a new empty signature.
pub fn empty_sig() -> Self {
let empty: Vec<u8> = vec![0; 97];
Signature(RawSignature::from_bytes(&empty).unwrap())
}
}
impl Encodable for Signature {
@ -68,4 +74,16 @@ mod tests {
assert_eq!(original, decoded);
}
#[test]
pub fn test_empty_sig() {
let sig = Signature::empty_sig();
let sig_as_bytes: Vec<u8> = sig.as_raw().as_bytes();
assert_eq!(sig_as_bytes.len(), 97);
for one_byte in sig_as_bytes.iter() {
assert_eq!(*one_byte, 0);
}
}
}

View File

@ -44,6 +44,10 @@ pub fn process_deposit(
withdrawal_credentials: deposit_input.withdrawal_credentials,
randao_commitment: deposit_input.randao_commitment,
randao_layers: 0,
activation_slot: spec.far_future_slot,
exit_slot: spec.far_future_slot,
withdrawal_slot: spec.far_future_slot,
penalized_slot: spec.far_future_slot,
status: ValidatorStatus::PendingActivation,
latest_status_change_slot: state.validator_registry_latest_change_slot,
exit_count: 0,