From 560dbe4ae16e8fa86d21254e6dbc055c78be200a Mon Sep 17 00:00:00 2001 From: Kirk Baird Date: Wed, 23 Jan 2019 15:04:40 +1100 Subject: [PATCH] Update constants / types to match specs as of 23.1.19 --- eth2/genesis/src/beacon_block.rs | 10 ++- eth2/genesis/src/beacon_state.rs | 11 +-- eth2/spec/src/foundation.rs | 18 ++-- eth2/spec/src/lib.rs | 7 +- eth2/types/src/beacon_block.rs | 12 +-- eth2/types/src/beacon_state.rs | 25 +++--- ...ow_receipt_root_record.rs => eth1_data.rs} | 32 +++---- eth2/types/src/eth1_data_vote.rs | 60 +++++++++++++ eth2/types/src/lib.rs | 7 +- eth2/types/src/validator_registration.rs | 12 --- .../src/validator_registry_delta_block.rs | 89 +++++++++++++++++++ validator_client/src/block_producer/grpc.rs | 7 +- 12 files changed, 219 insertions(+), 71 deletions(-) rename eth2/types/src/{candidate_pow_receipt_root_record.rs => eth1_data.rs} (53%) create mode 100644 eth2/types/src/eth1_data_vote.rs delete mode 100644 eth2/types/src/validator_registration.rs create mode 100644 eth2/types/src/validator_registry_delta_block.rs diff --git a/eth2/genesis/src/beacon_block.rs b/eth2/genesis/src/beacon_block.rs index 3fefff6a04..eabd524087 100644 --- a/eth2/genesis/src/beacon_block.rs +++ b/eth2/genesis/src/beacon_block.rs @@ -1,5 +1,5 @@ use spec::ChainSpec; -use types::{BeaconBlock, BeaconBlockBody, Hash256}; +use types::{BeaconBlock, BeaconBlockBody, Eth1Data, Hash256}; /// Generate a genesis BeaconBlock. pub fn genesis_beacon_block(state_root: Hash256, spec: &ChainSpec) -> BeaconBlock { @@ -8,7 +8,10 @@ pub fn genesis_beacon_block(state_root: Hash256, spec: &ChainSpec) -> BeaconBloc parent_root: spec.zero_hash, state_root, randao_reveal: spec.zero_hash, - candidate_pow_receipt_root: spec.zero_hash, + eth1_data: Eth1Data { + deposit_root: Hash256::zero(), + block_hash: Hash256::zero(), + }, signature: spec.empty_signature.clone(), body: BeaconBlockBody { proposer_slashings: vec![], @@ -48,7 +51,8 @@ mod tests { assert!(genesis_block.slot == 0); assert!(genesis_block.parent_root.is_zero()); assert!(genesis_block.randao_reveal.is_zero()); - assert!(genesis_block.candidate_pow_receipt_root.is_zero()); // aka deposit_root + assert!(genesis_block.eth1_data.deposit_root.is_zero()); + assert!(genesis_block.eth1_data.block_hash.is_zero()); } #[test] diff --git a/eth2/genesis/src/beacon_state.rs b/eth2/genesis/src/beacon_state.rs index 54cf1388d0..3254594004 100644 --- a/eth2/genesis/src/beacon_state.rs +++ b/eth2/genesis/src/beacon_state.rs @@ -80,8 +80,8 @@ pub fn genesis_beacon_state(spec: &ChainSpec) -> Result { /* * PoW receipt root */ - processed_pow_receipt_root: spec.processed_pow_receipt_root, - candidate_pow_receipt_roots: vec![], + latest_eth1_data: spec.intial_eth1_data.clone(), + eth1_data_votes: vec![], }) } @@ -213,10 +213,7 @@ mod tests { let state = genesis_beacon_state(&spec).unwrap(); - assert_eq!( - state.processed_pow_receipt_root, - spec.processed_pow_receipt_root - ); - assert!(state.candidate_pow_receipt_roots.is_empty()); + assert_eq!(&state.latest_eth1_data, &spec.intial_eth1_data); + assert!(state.eth1_data_votes.is_empty()); } } diff --git a/eth2/spec/src/foundation.rs b/eth2/spec/src/foundation.rs index 81bee7c4ab..af0f1743bd 100644 --- a/eth2/spec/src/foundation.rs +++ b/eth2/spec/src/foundation.rs @@ -1,7 +1,7 @@ use super::ChainSpec; use bls::{Keypair, PublicKey, SecretKey, Signature}; -use types::{Address, Hash256, ValidatorRecord}; +use types::{Address, Eth1Data, Hash256, ValidatorRecord}; /// The size of a validators deposit in GWei. pub const DEPOSIT_GWEI: u64 = 32_000_000_000; @@ -18,9 +18,8 @@ impl ChainSpec { */ shard_count: 1_024, target_committee_size: 128, - ejection_balance: 16, + ejection_balance: 16 * u64::pow(10, 9), max_balance_churn_quotient: 32, - gwei_per_eth: u64::pow(10, 9), beacon_chain_shard_number: u64::max_value(), max_casper_votes: 1_024, latest_block_roots_length: 8_192, @@ -32,8 +31,8 @@ impl ChainSpec { */ deposit_contract_address: Address::from("TBD".as_bytes()), deposit_contract_tree_depth: 32, - min_deposit: 1, - max_deposit: 32, + min_deposit: 1 * u64::pow(10, 9), + max_deposit: 32 * u64::pow(10, 9), /* * Initial Values */ @@ -52,12 +51,12 @@ impl ChainSpec { epoch_length: 64, seed_lookahead: 64, entry_exit_delay: 256, - pow_receipt_root_voting_period: 1_024, + eth1_data_voting_period: 1_024, min_validator_withdrawal_time: u64::pow(2, 14), /* * Reward and penalty quotients */ - base_reward_quotient: 1_024, + base_reward_quotient: 32, whistleblower_reward_quotient: 512, includer_reward_quotient: 8, inactivity_penalty_quotient: u64::pow(2, 24), @@ -75,7 +74,10 @@ impl ChainSpec { initial_validators: initial_validators_for_testing(), initial_balances: initial_balances_for_testing(), genesis_time: 1_544_672_897, - processed_pow_receipt_root: Hash256::from("pow_root".as_bytes()), + intial_eth1_data: Eth1Data { + deposit_root: Hash256::from("deposit_root".as_bytes()), + block_hash: Hash256::from("block_hash".as_bytes()), + }, } } } diff --git a/eth2/spec/src/lib.rs b/eth2/spec/src/lib.rs index fbe82794d1..fb5fb0c0a5 100644 --- a/eth2/spec/src/lib.rs +++ b/eth2/spec/src/lib.rs @@ -4,7 +4,7 @@ extern crate types; mod foundation; use bls::Signature; -use types::{Address, Hash256, ValidatorRecord}; +use types::{Address, Eth1Data, Hash256, ValidatorRecord}; #[derive(PartialEq, Debug)] pub struct ChainSpec { @@ -15,7 +15,6 @@ pub struct ChainSpec { pub target_committee_size: u64, pub ejection_balance: u64, pub max_balance_churn_quotient: u64, - pub gwei_per_eth: u64, pub beacon_chain_shard_number: u64, pub max_casper_votes: u64, pub latest_block_roots_length: u64, @@ -47,7 +46,7 @@ pub struct ChainSpec { pub epoch_length: u64, pub seed_lookahead: u64, pub entry_exit_delay: u64, - pub pow_receipt_root_voting_period: u64, // a.k.a. deposit_root_voting_period + pub eth1_data_voting_period: u64, pub min_validator_withdrawal_time: u64, /* * Reward and penalty quotients @@ -70,5 +69,5 @@ pub struct ChainSpec { pub initial_validators: Vec, pub initial_balances: Vec, pub genesis_time: u64, - pub processed_pow_receipt_root: Hash256, + pub intial_eth1_data: Eth1Data, } diff --git a/eth2/types/src/beacon_block.rs b/eth2/types/src/beacon_block.rs index 30a1bd83b0..8f86ec9743 100644 --- a/eth2/types/src/beacon_block.rs +++ b/eth2/types/src/beacon_block.rs @@ -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: Hash256, - 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 TestRandom 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), } diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index c55ce8ec9c..3be8c24e8a 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -1,5 +1,6 @@ -use super::candidate_pow_receipt_root_record::CandidatePoWReceiptRootRecord; use super::crosslink_record::CrosslinkRecord; +use super::eth1_data::Eth1Data; +use super::eth1_data_vote::Eth1DataVote; use super::fork_data::ForkData; use super::pending_attestation_record::PendingAttestationRecord; use super::validator_record::ValidatorRecord; @@ -52,9 +53,9 @@ pub struct BeaconState { pub latest_attestations: Vec, pub batched_block_roots: Vec, - // PoW receipt root (a.k.a. deposit root) - pub processed_pow_receipt_root: Hash256, - pub candidate_pow_receipt_roots: Vec, + // Ethereum 1.0 chain data + pub latest_eth1_data: Eth1Data, + pub eth1_data_votes: Vec, } impl BeaconState { @@ -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); } } @@ -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 { @@ -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, )) @@ -194,8 +195,8 @@ impl TestRandom 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), } } } diff --git a/eth2/types/src/candidate_pow_receipt_root_record.rs b/eth2/types/src/eth1_data.rs similarity index 53% rename from eth2/types/src/candidate_pow_receipt_root_record.rs rename to eth2/types/src/eth1_data.rs index 9648763f39..a20559e184 100644 --- a/eth2/types/src/candidate_pow_receipt_root_record.rs +++ b/eth2/types/src/eth1_data.rs @@ -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 TestRandom for CandidatePoWReceiptRootRecord { +impl TestRandom 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(); diff --git a/eth2/types/src/eth1_data_vote.rs b/eth2/types/src/eth1_data_vote.rs new file mode 100644 index 0000000000..c4d9d01afe --- /dev/null +++ b/eth2/types/src/eth1_data_vote.rs @@ -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 TestRandom 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); + } +} diff --git a/eth2/types/src/lib.rs b/eth2/types/src/lib.rs index f00c19d037..580554ec16 100644 --- a/eth2/types/src/lib.rs +++ b/eth2/types/src/lib.rs @@ -10,12 +10,13 @@ pub mod attestation_data; 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 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; @@ -27,6 +28,7 @@ pub mod slashable_vote_data; pub mod special_record; pub mod validator_record; pub mod validator_registry; +pub mod validator_registry_delta_block; pub mod readers; @@ -43,6 +45,8 @@ pub use crate::crosslink_record::CrosslinkRecord; 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; @@ -52,6 +56,7 @@ 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_registry_delta_block::ValidatorRegistryDeltaBlock; pub type Hash256 = H256; pub type Address = H160; diff --git a/eth2/types/src/validator_registration.rs b/eth2/types/src/validator_registration.rs deleted file mode 100644 index 964b80b8af..0000000000 --- a/eth2/types/src/validator_registration.rs +++ /dev/null @@ -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, -} diff --git a/eth2/types/src/validator_registry_delta_block.rs b/eth2/types/src/validator_registry_delta_block.rs new file mode 100644 index 0000000000..11e07ee0fb --- /dev/null +++ b/eth2/types/src/validator_registry_delta_block.rs @@ -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" `ValidatorRecord`. 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 TestRandom 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); + } +} diff --git a/validator_client/src/block_producer/grpc.rs b/validator_client/src/block_producer/grpc.rs index 033965b169..44293c513a 100644 --- a/validator_client/src/block_producer/grpc.rs +++ b/validator_client/src/block_producer/grpc.rs @@ -4,7 +4,7 @@ use protos::services::{ }; use protos::services_grpc::BeaconBlockServiceClient; use ssz::{ssz_encode, Decodable}; -use types::{BeaconBlock, BeaconBlockBody, Hash256, Signature}; +use types::{BeaconBlock, BeaconBlockBody, Eth1Data, Hash256, Signature}; impl BeaconNode for BeaconBlockServiceClient { /// Request a Beacon Node (BN) to produce a new block at the supplied slot. @@ -31,7 +31,10 @@ impl BeaconNode for BeaconBlockServiceClient { parent_root: Hash256::zero(), state_root: Hash256::zero(), randao_reveal: Hash256::from(block.get_randao_reveal()), - candidate_pow_receipt_root: Hash256::zero(), + eth1_data: Eth1Data { + deposit_root: Hash256::zero(), + block_hash: Hash256::zero(), + }, signature, body: BeaconBlockBody { proposer_slashings: vec![],