From 240e269f2fcc1b1dabdf7cfa5e2a042ffa4f35bf Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Mon, 13 May 2019 17:01:30 +1000 Subject: [PATCH] types: more v0.6.1 updates --- eth2/types/src/attestation_data.rs | 6 +- eth2/types/src/beacon_state.rs | 9 ++- eth2/types/src/chain_spec.rs | 6 +- eth2/types/src/crosslink.rs | 1 + eth2/types/src/deposit_data.rs | 1 + eth2/types/src/fork.rs | 14 ++-- eth2/types/src/indexed_attestation.rs | 73 ++++++++----------- eth2/types/src/pending_attestation.rs | 10 +-- .../test_utils/testing_attestation_builder.rs | 2 +- .../testing_attestation_data_builder.rs | 8 +- .../testing_attester_slashing_builder.rs | 11 +-- .../testing_beacon_state_builder.rs | 2 +- .../testing_pending_attestation_builder.rs | 5 +- 13 files changed, 73 insertions(+), 75 deletions(-) diff --git a/eth2/types/src/attestation_data.rs b/eth2/types/src/attestation_data.rs index d09217ef9..e80a01efd 100644 --- a/eth2/types/src/attestation_data.rs +++ b/eth2/types/src/attestation_data.rs @@ -1,5 +1,5 @@ use crate::test_utils::TestRandom; -use crate::{Epoch, Hash256, Slot}; +use crate::{Epoch, Hash256}; use rand::RngCore; use serde_derive::{Deserialize, Serialize}; use ssz_derive::{Decode, Encode}; @@ -9,7 +9,7 @@ use tree_hash_derive::{CachedTreeHash, SignedRoot, TreeHash}; /// The data upon which an attestation is based. /// -/// Spec v0.6.0 +/// Spec v0.6.1 #[derive( Debug, Clone, @@ -27,12 +27,12 @@ use tree_hash_derive::{CachedTreeHash, SignedRoot, TreeHash}; )] pub struct AttestationData { // LMD GHOST vote - pub slot: Slot, pub beacon_block_root: Hash256, // FFG Vote pub source_epoch: Epoch, pub source_root: Hash256, + pub target_epoch: Epoch, pub target_root: Hash256, // Crosslink Vote diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 4ac0c1f71..f0e5647ad 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -250,9 +250,14 @@ impl BeaconState { /// /// If the current epoch is the genesis epoch, the genesis_epoch is returned. /// - /// Spec v0.5.1 + /// Spec v0.6.1 pub fn previous_epoch(&self, spec: &ChainSpec) -> Epoch { - self.current_epoch(&spec) - 1 + let current_epoch = self.current_epoch(spec); + if current_epoch > spec.genesis_epoch { + current_epoch - 1 + } else { + current_epoch + } } /// The epoch following `self.current_epoch()`. diff --git a/eth2/types/src/chain_spec.rs b/eth2/types/src/chain_spec.rs index c219c05ee..11d2318cb 100644 --- a/eth2/types/src/chain_spec.rs +++ b/eth2/types/src/chain_spec.rs @@ -122,7 +122,7 @@ pub struct ChainSpec { impl ChainSpec { /// Return the number of committees in one epoch. /// - /// Spec v0.5.1 + /// Spec v0.6.1 pub fn get_epoch_committee_count(&self, active_validator_count: usize) -> u64 { std::cmp::max( 1, @@ -319,11 +319,11 @@ mod tests { fn test_get_domain() { let spec = ChainSpec::foundation(); - test_domain(Domain::BeaconBlock, spec.domain_beacon_block, &spec); + test_domain(Domain::BeaconProposer, spec.domain_beacon_proposer, &spec); test_domain(Domain::Randao, spec.domain_randao, &spec); test_domain(Domain::Attestation, spec.domain_attestation, &spec); test_domain(Domain::Deposit, spec.domain_deposit, &spec); - test_domain(Domain::Exit, spec.domain_exit, &spec); + test_domain(Domain::VoluntaryExit, spec.domain_voluntary_exit, &spec); test_domain(Domain::Transfer, spec.domain_transfer, &spec); } } diff --git a/eth2/types/src/crosslink.rs b/eth2/types/src/crosslink.rs index e6d353452..ed20f4af7 100644 --- a/eth2/types/src/crosslink.rs +++ b/eth2/types/src/crosslink.rs @@ -13,6 +13,7 @@ use tree_hash_derive::{CachedTreeHash, TreeHash}; Debug, Clone, PartialEq, + Eq, Default, Serialize, Deserialize, diff --git a/eth2/types/src/deposit_data.rs b/eth2/types/src/deposit_data.rs index 930836c70..08bdccc41 100644 --- a/eth2/types/src/deposit_data.rs +++ b/eth2/types/src/deposit_data.rs @@ -76,6 +76,7 @@ mod tests { let mut deposit_input = DepositData { pubkey: keypair.pk.clone(), + amount: 0, withdrawal_credentials: Hash256::zero(), signature: Signature::empty_signature(), }; diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index 83d4f5dc6..c095abbce 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -2,7 +2,6 @@ use crate::{ test_utils::{fork_from_hex_str, TestRandom}, ChainSpec, Epoch, }; -use int_to_bytes::int_to_bytes4; use rand::RngCore; use serde_derive::{Deserialize, Serialize}; use ssz_derive::{Decode, Encode}; @@ -11,7 +10,7 @@ use tree_hash_derive::{CachedTreeHash, TreeHash}; /// Specifies a fork of the `BeaconChain`, to prevent replay attacks. /// -/// Spec v0.5.1 +/// Spec v0.6.1 #[derive( Debug, Clone, @@ -36,10 +35,11 @@ pub struct Fork { impl Fork { /// Initialize the `Fork` from the genesis parameters in the `spec`. /// - /// Spec v0.5.1 + /// Spec v0.6.1 pub fn genesis(spec: &ChainSpec) -> Self { - let mut current_version: [u8; 4] = [0; 4]; - current_version.copy_from_slice(&int_to_bytes4(spec.genesis_fork_version)); + let current_version: [u8; 4] = [0; 4]; + // FIXME(sproul): 0 fork? + // current_version.copy_from_slice(&int_to_bytes4(spec.genesis_fork_version)); Self { previous_version: current_version, @@ -50,7 +50,7 @@ impl Fork { /// Return the fork version of the given ``epoch``. /// - /// Spec v0.5.1 + /// Spec v0.6.1 pub fn get_fork_version(&self, epoch: Epoch) -> [u8; 4] { if epoch < self.epoch { return self.previous_version; @@ -66,10 +66,10 @@ mod tests { ssz_tests!(Fork); cached_tree_hash_tests!(Fork); + // FIXME(sproul): dunno fn test_genesis(version: u32, epoch: Epoch) { let mut spec = ChainSpec::foundation(); - spec.genesis_fork_version = version; spec.genesis_epoch = epoch; let fork = Fork::genesis(&spec); diff --git a/eth2/types/src/indexed_attestation.rs b/eth2/types/src/indexed_attestation.rs index 5b56f34df..390dc082e 100644 --- a/eth2/types/src/indexed_attestation.rs +++ b/eth2/types/src/indexed_attestation.rs @@ -1,4 +1,4 @@ -use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfield, ChainSpec}; +use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfield}; use rand::RngCore; use serde_derive::{Deserialize, Serialize}; use ssz_derive::{Decode, Encode}; @@ -37,99 +37,88 @@ pub struct IndexedAttestation { impl IndexedAttestation { /// Check if ``attestation_data_1`` and ``attestation_data_2`` have the same target. /// - /// Spec v0.5.1 - pub fn is_double_vote(&self, other: &IndexedAttestation, spec: &ChainSpec) -> bool { - self.data.slot.epoch(spec.slots_per_epoch) == other.data.slot.epoch(spec.slots_per_epoch) + /// Spec v0.6.1 + pub fn is_double_vote(&self, other: &IndexedAttestation) -> bool { + self.data.target_epoch == other.data.target_epoch && self.data != other.data } /// Check if ``attestation_data_1`` surrounds ``attestation_data_2``. /// - /// Spec v0.5.1 - pub fn is_surround_vote(&self, other: &IndexedAttestation, spec: &ChainSpec) -> bool { - let source_epoch_1 = self.data.source_epoch; - let source_epoch_2 = other.data.source_epoch; - let target_epoch_1 = self.data.slot.epoch(spec.slots_per_epoch); - let target_epoch_2 = other.data.slot.epoch(spec.slots_per_epoch); - - (source_epoch_1 < source_epoch_2) & (target_epoch_2 < target_epoch_1) + /// Spec v0.6.1 + pub fn is_surround_vote(&self, other: &IndexedAttestation) -> bool { + self.data.source_epoch < other.data.source_epoch + && other.data.target_epoch < self.data.target_epoch } } #[cfg(test)] mod tests { use super::*; - use crate::chain_spec::ChainSpec; - use crate::slot_epoch::{Epoch, Slot}; + use crate::slot_epoch::Epoch; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_is_double_vote_true() { - let spec = ChainSpec::foundation(); - let indexed_vote_first = create_indexed_attestation(1, 1, &spec); - let indexed_vote_second = create_indexed_attestation(1, 1, &spec); + let indexed_vote_first = create_indexed_attestation(1, 1); + let indexed_vote_second = create_indexed_attestation(1, 1); assert_eq!( - indexed_vote_first.is_double_vote(&indexed_vote_second, &spec), + indexed_vote_first.is_double_vote(&indexed_vote_second), true ) } #[test] pub fn test_is_double_vote_false() { - let spec = ChainSpec::foundation(); - let indexed_vote_first = create_indexed_attestation(1, 1, &spec); - let indexed_vote_second = create_indexed_attestation(2, 1, &spec); + let indexed_vote_first = create_indexed_attestation(1, 1); + let indexed_vote_second = create_indexed_attestation(2, 1); assert_eq!( - indexed_vote_first.is_double_vote(&indexed_vote_second, &spec), + indexed_vote_first.is_double_vote(&indexed_vote_second), false ); } #[test] pub fn test_is_surround_vote_true() { - let spec = ChainSpec::foundation(); - let indexed_vote_first = create_indexed_attestation(2, 1, &spec); - let indexed_vote_second = create_indexed_attestation(1, 2, &spec); + let indexed_vote_first = create_indexed_attestation(2, 1); + let indexed_vote_second = create_indexed_attestation(1, 2); assert_eq!( - indexed_vote_first.is_surround_vote(&indexed_vote_second, &spec), + indexed_vote_first.is_surround_vote(&indexed_vote_second), true ); } #[test] pub fn test_is_surround_vote_true_realistic() { - let spec = ChainSpec::foundation(); - let indexed_vote_first = create_indexed_attestation(4, 1, &spec); - let indexed_vote_second = create_indexed_attestation(3, 2, &spec); + let indexed_vote_first = create_indexed_attestation(4, 1); + let indexed_vote_second = create_indexed_attestation(3, 2); assert_eq!( - indexed_vote_first.is_surround_vote(&indexed_vote_second, &spec), + indexed_vote_first.is_surround_vote(&indexed_vote_second), true ); } #[test] pub fn test_is_surround_vote_false_source_epoch_fails() { - let spec = ChainSpec::foundation(); - let indexed_vote_first = create_indexed_attestation(2, 2, &spec); - let indexed_vote_second = create_indexed_attestation(1, 1, &spec); + let indexed_vote_first = create_indexed_attestation(2, 2); + let indexed_vote_second = create_indexed_attestation(1, 1); assert_eq!( - indexed_vote_first.is_surround_vote(&indexed_vote_second, &spec), + indexed_vote_first.is_surround_vote(&indexed_vote_second), false ); } #[test] pub fn test_is_surround_vote_false_target_epoch_fails() { - let spec = ChainSpec::foundation(); - let indexed_vote_first = create_indexed_attestation(1, 1, &spec); - let indexed_vote_second = create_indexed_attestation(2, 2, &spec); + let indexed_vote_first = create_indexed_attestation(1, 1); + let indexed_vote_second = create_indexed_attestation(2, 2); assert_eq!( - indexed_vote_first.is_surround_vote(&indexed_vote_second, &spec), + indexed_vote_first.is_surround_vote(&indexed_vote_second), false ); } @@ -137,16 +126,12 @@ mod tests { ssz_tests!(IndexedAttestation); cached_tree_hash_tests!(IndexedAttestation); - fn create_indexed_attestation( - slot_factor: u64, - source_epoch: u64, - spec: &ChainSpec, - ) -> IndexedAttestation { + fn create_indexed_attestation(target_epoch: u64, source_epoch: u64) -> IndexedAttestation { let mut rng = XorShiftRng::from_seed([42; 16]); let mut indexed_vote = IndexedAttestation::random_for_test(&mut rng); - indexed_vote.data.slot = Slot::new(slot_factor * spec.slots_per_epoch); indexed_vote.data.source_epoch = Epoch::new(source_epoch); + indexed_vote.data.target_epoch = Epoch::new(target_epoch); indexed_vote } } diff --git a/eth2/types/src/pending_attestation.rs b/eth2/types/src/pending_attestation.rs index e2ad23716..b40e9b10d 100644 --- a/eth2/types/src/pending_attestation.rs +++ b/eth2/types/src/pending_attestation.rs @@ -1,5 +1,5 @@ use crate::test_utils::TestRandom; -use crate::{Attestation, AttestationData, Bitfield, Slot}; +use crate::{Attestation, AttestationData, Bitfield}; use rand::RngCore; use serde_derive::{Deserialize, Serialize}; use ssz_derive::{Decode, Encode}; @@ -8,7 +8,7 @@ use tree_hash_derive::{CachedTreeHash, TreeHash}; /// An attestation that has been included in the state but not yet fully processed. /// -/// Spec v0.6.0 +/// Spec v0.6.1 #[derive( Debug, Clone, @@ -24,7 +24,7 @@ use tree_hash_derive::{CachedTreeHash, TreeHash}; pub struct PendingAttestation { pub aggregation_bitfield: Bitfield, pub data: AttestationData, - pub inclusion_slot: Slot, + pub inclusion_delay: u64, pub proposer_index: u64, } @@ -32,13 +32,13 @@ impl PendingAttestation { /// Create a `PendingAttestation` from an `Attestation`. pub fn from_attestation( attestation: &Attestation, - inclusion_slot: Slot, + inclusion_delay: u64, proposer_index: u64, ) -> Self { PendingAttestation { data: attestation.data.clone(), aggregation_bitfield: attestation.aggregation_bitfield.clone(), - inclusion_slot, + inclusion_delay, proposer_index, } } diff --git a/eth2/types/src/test_utils/testing_attestation_builder.rs b/eth2/types/src/test_utils/testing_attestation_builder.rs index b41bf6f8d..ab5cfa97f 100644 --- a/eth2/types/src/test_utils/testing_attestation_builder.rs +++ b/eth2/types/src/test_utils/testing_attestation_builder.rs @@ -77,7 +77,7 @@ impl TestingAttestationBuilder { .tree_hash_root(); let domain = spec.get_domain( - self.attestation.data.slot.epoch(spec.slots_per_epoch), + self.attestation.data.target_epoch, Domain::Attestation, fork, ); diff --git a/eth2/types/src/test_utils/testing_attestation_data_builder.rs b/eth2/types/src/test_utils/testing_attestation_data_builder.rs index 9fde5202c..13668380a 100644 --- a/eth2/types/src/test_utils/testing_attestation_data_builder.rs +++ b/eth2/types/src/test_utils/testing_attestation_data_builder.rs @@ -23,6 +23,12 @@ impl TestingAttestationDataBuilder { state.current_justified_epoch }; + let target_epoch = if is_previous_epoch { + state.previous_epoch(spec) + } else { + state.current_epoch(spec) + }; + let target_root = if is_previous_epoch { *state .get_block_root(previous_epoch.start_slot(spec.slots_per_epoch), spec) @@ -39,12 +45,12 @@ impl TestingAttestationDataBuilder { let data = AttestationData { // LMD GHOST vote - slot, beacon_block_root: *state.get_block_root(slot, spec).unwrap(), // FFG Vote source_epoch, source_root, + target_epoch, target_root, // Crosslink vote diff --git a/eth2/types/src/test_utils/testing_attester_slashing_builder.rs b/eth2/types/src/test_utils/testing_attester_slashing_builder.rs index 91615d258..9a8c145de 100644 --- a/eth2/types/src/test_utils/testing_attester_slashing_builder.rs +++ b/eth2/types/src/test_utils/testing_attester_slashing_builder.rs @@ -21,17 +21,17 @@ impl TestingAttesterSlashingBuilder { where F: Fn(u64, &[u8], Epoch, Domain) -> Signature, { - let double_voted_slot = Slot::new(0); let shard = 0; - let epoch = Epoch::new(0); + let epoch_1 = Epoch::new(1); + let epoch_2 = Epoch::new(2); let hash_1 = Hash256::from_low_u64_le(1); let hash_2 = Hash256::from_low_u64_le(2); let data_1 = AttestationData { - slot: double_voted_slot, beacon_block_root: hash_1, - source_epoch: epoch, + source_epoch: epoch_1, source_root: hash_1, + target_epoch: epoch_2, target_root: hash_1, shard, previous_crosslink_root: hash_1, @@ -69,7 +69,8 @@ impl TestingAttesterSlashingBuilder { for (i, validator_index) in validator_indices.iter().enumerate() { attestation.custody_bitfield.set(i, false); - let signature = signer(*validator_index, &message[..], epoch, Domain::Attestation); + let signature = + signer(*validator_index, &message[..], epoch_2, Domain::Attestation); attestation.signature.add(&signature); } }; diff --git a/eth2/types/src/test_utils/testing_beacon_state_builder.rs b/eth2/types/src/test_utils/testing_beacon_state_builder.rs index 7987a256f..497186841 100644 --- a/eth2/types/src/test_utils/testing_beacon_state_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_state_builder.rs @@ -246,7 +246,7 @@ impl TestingBeaconStateBuilder { builder.add_committee_participation(signers); let attestation = builder.build(); - if attestation.data.slot.epoch(spec.slots_per_epoch) < state.current_epoch(spec) { + if attestation.data.target_epoch < state.current_epoch(spec) { state.previous_epoch_attestations.push(attestation) } else { state.current_epoch_attestations.push(attestation) diff --git a/eth2/types/src/test_utils/testing_pending_attestation_builder.rs b/eth2/types/src/test_utils/testing_pending_attestation_builder.rs index 831cda0f8..4b282851c 100644 --- a/eth2/types/src/test_utils/testing_pending_attestation_builder.rs +++ b/eth2/types/src/test_utils/testing_pending_attestation_builder.rs @@ -11,8 +11,7 @@ pub struct TestingPendingAttestationBuilder { impl TestingPendingAttestationBuilder { /// Create a new valid* `PendingAttestation` for the given parameters. /// - /// The `inclusion_slot` will be set to be the earliest possible slot the `Attestation` could - /// have been included (`slot + MIN_ATTESTATION_INCLUSION_DELAY`). + /// The `inclusion_delay` will be set to `MIN_ATTESTATION_INCLUSION_DELAY`. /// /// * The aggregation and custody bitfields will all be empty, they need to be set with /// `Self::add_committee_participation`. @@ -22,7 +21,7 @@ impl TestingPendingAttestationBuilder { let pending_attestation = PendingAttestation { aggregation_bitfield: Bitfield::new(), data: data_builder.build(), - inclusion_slot: slot + spec.min_attestation_inclusion_delay, + inclusion_delay: spec.min_attestation_inclusion_delay, // FIXME(sproul) proposer_index: 0, };