types: del SlashableAttestation, fix fork defaults

This commit is contained in:
Michael Sproul 2019-05-14 16:37:19 +10:00
parent 2727590762
commit c568dd01fe
No known key found for this signature in database
GPG Key ID: 77B1309D2E54E914
3 changed files with 9 additions and 170 deletions

View File

@ -37,13 +37,9 @@ impl Fork {
///
/// Spec v0.6.1
pub fn genesis(spec: &ChainSpec) -> Self {
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,
current_version,
previous_version: [0; 4],
current_version: [0; 4],
epoch: spec.genesis_epoch,
}
}
@ -66,8 +62,7 @@ mod tests {
ssz_tests!(Fork);
cached_tree_hash_tests!(Fork);
// FIXME(sproul): dunno
fn test_genesis(version: u32, epoch: Epoch) {
fn test_genesis(epoch: Epoch) {
let mut spec = ChainSpec::foundation();
spec.genesis_epoch = epoch;
@ -79,19 +74,14 @@ mod tests {
fork.previous_version, fork.current_version,
"previous and current are not identical"
);
assert_eq!(
fork.current_version,
version.to_le_bytes(),
"current version incorrect"
);
}
#[test]
fn genesis() {
test_genesis(0, Epoch::new(0));
test_genesis(9, Epoch::new(11));
test_genesis(2_u32.pow(31), Epoch::new(2_u64.pow(63)));
test_genesis(u32::max_value(), Epoch::max_value());
test_genesis(Epoch::new(0));
test_genesis(Epoch::new(11));
test_genesis(Epoch::new(2_u64.pow(63)));
test_genesis(Epoch::max_value());
}
#[test]

View File

@ -58,8 +58,8 @@ mod tests {
#[test]
pub fn test_is_double_vote_true() {
let indexed_vote_first = create_indexed_attestation(1, 1);
let indexed_vote_second = create_indexed_attestation(1, 1);
let indexed_vote_first = create_indexed_attestation(3, 1);
let indexed_vote_second = create_indexed_attestation(3, 2);
assert_eq!(
indexed_vote_first.is_double_vote(&indexed_vote_second),

View File

@ -1,151 +0,0 @@
use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfield, ChainSpec};
use serde_derive::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode};
use test_random_derive::TestRandom;
use tree_hash::TreeHash;
use tree_hash_derive::{CachedTreeHash, SignedRoot, TreeHash};
/// Details an attestation that can be slashable.
///
/// To be included in an `AttesterSlashing`.
///
/// Spec v0.5.1
#[derive(
Debug,
PartialEq,
Clone,
Serialize,
Deserialize,
Encode,
Decode,
TreeHash,
CachedTreeHash,
TestRandom,
SignedRoot,
)]
pub struct SlashableAttestation {
/// Lists validator registry indices, not committee indices.
pub validator_indices: Vec<u64>,
pub data: AttestationData,
pub custody_bitfield: Bitfield,
#[signed_root(skip_hashing)]
pub aggregate_signature: AggregateSignature,
}
impl SlashableAttestation {
/// Check if ``attestation_data_1`` and ``attestation_data_2`` have the same target.
///
/// Spec v0.5.1
pub fn is_double_vote(&self, other: &SlashableAttestation, spec: &ChainSpec) -> bool {
self.data.slot.epoch(spec.slots_per_epoch) == other.data.slot.epoch(spec.slots_per_epoch)
}
/// Check if ``attestation_data_1`` surrounds ``attestation_data_2``.
///
/// Spec v0.5.1
pub fn is_surround_vote(&self, other: &SlashableAttestation, 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)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::chain_spec::ChainSpec;
use crate::slot_epoch::{Epoch, Slot};
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
#[test]
pub fn test_is_double_vote_true() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_attestation(1, 1, &spec);
let slashable_vote_second = create_slashable_attestation(1, 1, &spec);
assert_eq!(
slashable_vote_first.is_double_vote(&slashable_vote_second, &spec),
true
)
}
#[test]
pub fn test_is_double_vote_false() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_attestation(1, 1, &spec);
let slashable_vote_second = create_slashable_attestation(2, 1, &spec);
assert_eq!(
slashable_vote_first.is_double_vote(&slashable_vote_second, &spec),
false
);
}
#[test]
pub fn test_is_surround_vote_true() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_attestation(2, 1, &spec);
let slashable_vote_second = create_slashable_attestation(1, 2, &spec);
assert_eq!(
slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec),
true
);
}
#[test]
pub fn test_is_surround_vote_true_realistic() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_attestation(4, 1, &spec);
let slashable_vote_second = create_slashable_attestation(3, 2, &spec);
assert_eq!(
slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec),
true
);
}
#[test]
pub fn test_is_surround_vote_false_source_epoch_fails() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_attestation(2, 2, &spec);
let slashable_vote_second = create_slashable_attestation(1, 1, &spec);
assert_eq!(
slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec),
false
);
}
#[test]
pub fn test_is_surround_vote_false_target_epoch_fails() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_attestation(1, 1, &spec);
let slashable_vote_second = create_slashable_attestation(2, 2, &spec);
assert_eq!(
slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec),
false
);
}
ssz_tests!(SlashableAttestation);
cached_tree_hash_tests!(SlashableAttestation);
fn create_slashable_attestation(
slot_factor: u64,
source_epoch: u64,
spec: &ChainSpec,
) -> SlashableAttestation {
let mut rng = XorShiftRng::from_seed([42; 16]);
let mut slashable_vote = SlashableAttestation::random_for_test(&mut rng);
slashable_vote.data.slot = Slot::new(slot_factor * spec.slots_per_epoch);
slashable_vote.data.source_epoch = Epoch::new(source_epoch);
slashable_vote
}
}