Merge branch 'docker-env' into v0.6.1

This commit is contained in:
Paul Hauner
2019-06-13 10:37:35 -04:00
142 changed files with 3186 additions and 2062 deletions

View File

@@ -25,8 +25,8 @@ pub fn get_genesis_beacon_state<T: EthSpec>(
// Process genesis activations.
for validator in &mut state.validator_registry {
if validator.effective_balance >= spec.max_effective_balance {
validator.activation_eligibility_epoch = spec.genesis_epoch;
validator.activation_epoch = spec.genesis_epoch;
validator.activation_eligibility_epoch = T::genesis_epoch();
validator.activation_epoch = T::genesis_epoch();
}
}

View File

@@ -142,7 +142,7 @@ pub fn verify_block_signature<T: EthSpec>(
[state.get_beacon_proposer_index(block.slot, RelativeEpoch::Current, spec)?];
let domain = spec.get_domain(
block.slot.epoch(spec.slots_per_epoch),
block.slot.epoch(T::slots_per_epoch()),
Domain::BeaconProposer,
&state.fork,
);
@@ -174,7 +174,7 @@ pub fn process_randao<T: EthSpec>(
block.body.randao_reveal.verify(
&state.current_epoch().tree_hash_root()[..],
spec.get_domain(
block.slot.epoch(spec.slots_per_epoch),
block.slot.epoch(T::slots_per_epoch()),
Domain::Randao,
&state.fork
),

View File

@@ -22,8 +22,8 @@ impl<T: EthSpec> BlockProcessingBuilder<T> {
}
}
pub fn set_slot(&mut self, slot: Slot, spec: &ChainSpec) {
self.state_builder.teleport_to_slot(slot, &spec);
pub fn set_slot(&mut self, slot: Slot) {
self.state_builder.teleport_to_slot(slot);
}
pub fn build_caches(&mut self, spec: &ChainSpec) {
@@ -55,11 +55,13 @@ impl<T: EthSpec> BlockProcessingBuilder<T> {
let keypair = &keypairs[proposer_index];
match randao_sk {
Some(sk) => builder.set_randao_reveal(&sk, &state.fork, spec),
None => builder.set_randao_reveal(&keypair.sk, &state.fork, spec),
Some(sk) => builder.set_randao_reveal::<T>(&sk, &state.fork, spec),
None => builder.set_randao_reveal::<T>(&keypair.sk, &state.fork, spec),
}
let block = self.block_builder.build(&keypair.sk, &state.fork, spec);
let block = self
.block_builder
.build::<T>(&keypair.sk, &state.fork, spec);
(block, state)
}

View File

@@ -9,7 +9,7 @@ pub const VALIDATOR_COUNT: usize = 10;
#[test]
fn valid_block_ok() {
let spec = FoundationEthSpec::spec();
let spec = MainnetEthSpec::default_spec();
let builder = get_builder(&spec);
let (block, mut state) = builder.build(None, None, &spec);
@@ -20,7 +20,7 @@ fn valid_block_ok() {
#[test]
fn invalid_block_header_state_slot() {
let spec = FoundationEthSpec::spec();
let spec = MainnetEthSpec::default_spec();
let builder = get_builder(&spec);
let (mut block, mut state) = builder.build(None, None, &spec);
@@ -39,7 +39,7 @@ fn invalid_block_header_state_slot() {
#[test]
fn invalid_parent_block_root() {
let spec = FoundationEthSpec::spec();
let spec = MainnetEthSpec::default_spec();
let builder = get_builder(&spec);
let invalid_parent_root = Hash256::from([0xAA; 32]);
let (block, mut state) = builder.build(None, Some(invalid_parent_root), &spec);
@@ -59,14 +59,14 @@ fn invalid_parent_block_root() {
#[test]
fn invalid_block_signature() {
let spec = FoundationEthSpec::spec();
let spec = MainnetEthSpec::default_spec();
let builder = get_builder(&spec);
let (mut block, mut state) = builder.build(None, None, &spec);
// sign the block with a keypair that is not the expected proposer
let keypair = Keypair::random();
let message = block.signed_root();
let epoch = block.slot.epoch(spec.slots_per_epoch);
let epoch = block.slot.epoch(MainnetEthSpec::slots_per_epoch());
let domain = spec.get_domain(epoch, Domain::BeaconProposer, &state.fork);
block.signature = Signature::new(&message, domain, &keypair.sk);
@@ -82,7 +82,7 @@ fn invalid_block_signature() {
#[test]
fn invalid_randao_reveal_signature() {
let spec = FoundationEthSpec::spec();
let spec = MainnetEthSpec::default_spec();
let builder = get_builder(&spec);
// sign randao reveal with random keypair
@@ -100,12 +100,13 @@ fn invalid_randao_reveal_signature() {
);
}
fn get_builder(spec: &ChainSpec) -> (BlockProcessingBuilder<FoundationEthSpec>) {
fn get_builder(spec: &ChainSpec) -> (BlockProcessingBuilder<MainnetEthSpec>) {
let mut builder = BlockProcessingBuilder::new(VALIDATOR_COUNT, &spec);
// Set the state and block to be in the last slot of the 4th epoch.
let last_slot_of_epoch = (spec.genesis_epoch + 4).end_slot(spec.slots_per_epoch);
builder.set_slot(last_slot_of_epoch, &spec);
let last_slot_of_epoch =
(MainnetEthSpec::genesis_epoch() + 4).end_slot(MainnetEthSpec::slots_per_epoch());
builder.set_slot(last_slot_of_epoch);
builder.build_caches(&spec);
(builder)

View File

@@ -68,7 +68,7 @@ fn validate_attestation_parametric<T: EthSpec>(
}
);
verify!(
state.slot <= attestation_slot + spec.slots_per_epoch,
state.slot <= attestation_slot + T::slots_per_epoch(),
Invalid::IncludedTooLate {
state: state.slot,
attestation: attestation_slot

View File

@@ -49,7 +49,7 @@ fn verify_indexed_attestation_parametric<T: EthSpec>(
);
// Check that nobody signed with custody bit 1 (to be removed in phase 1)
if custody_bit_1_indices.len() > 0 {
if !custody_bit_1_indices.is_empty() {
invalid!(Invalid::CustodyBitfieldHasSetBits);
}
@@ -96,7 +96,7 @@ where
state
.validator_registry
.get(validator_idx as usize)
.ok_or(Error::Invalid(Invalid::UnknownValidator(validator_idx)))
.ok_or_else(|| Error::Invalid(Invalid::UnknownValidator(validator_idx)))
.map(|validator| {
aggregate_pubkey.add(&validator.pubkey);
aggregate_pubkey

View File

@@ -21,8 +21,8 @@ pub fn verify_proposer_slashing<T: EthSpec>(
})?;
verify!(
proposer_slashing.header_1.slot.epoch(spec.slots_per_epoch)
== proposer_slashing.header_2.slot.epoch(spec.slots_per_epoch),
proposer_slashing.header_1.slot.epoch(T::slots_per_epoch())
== proposer_slashing.header_2.slot.epoch(T::slots_per_epoch()),
Invalid::ProposalEpochMismatch(
proposer_slashing.header_1.slot,
proposer_slashing.header_2.slot
@@ -40,7 +40,7 @@ pub fn verify_proposer_slashing<T: EthSpec>(
);
verify!(
verify_header_signature(
verify_header_signature::<T>(
&proposer_slashing.header_1,
&proposer.pubkey,
&state.fork,
@@ -49,7 +49,7 @@ pub fn verify_proposer_slashing<T: EthSpec>(
Invalid::BadProposal1Signature
);
verify!(
verify_header_signature(
verify_header_signature::<T>(
&proposer_slashing.header_2,
&proposer.pubkey,
&state.fork,
@@ -66,7 +66,7 @@ pub fn verify_proposer_slashing<T: EthSpec>(
/// Returns `true` if the signature is valid.
///
/// Spec v0.6.1
fn verify_header_signature(
fn verify_header_signature<T: EthSpec>(
header: &BeaconBlockHeader,
pubkey: &PublicKey,
fork: &Fork,
@@ -74,7 +74,7 @@ fn verify_header_signature(
) -> bool {
let message = header.signed_root();
let domain = spec.get_domain(
header.slot.epoch(spec.slots_per_epoch),
header.slot.epoch(T::slots_per_epoch()),
Domain::BeaconProposer,
fork,
);

View File

@@ -101,7 +101,7 @@ fn verify_transfer_parametric<T: EthSpec>(
.get(transfer.sender as usize)
.ok_or_else(|| Error::Invalid(Invalid::FromValidatorUnknown(transfer.sender)))?;
let epoch = state.slot.epoch(spec.slots_per_epoch);
let epoch = state.slot.epoch(T::slots_per_epoch());
// Ensure one of the following is met:
//
@@ -136,7 +136,7 @@ fn verify_transfer_parametric<T: EthSpec>(
// Verify the transfer signature.
let message = transfer.signed_root();
let domain = spec.get_domain(
transfer.slot.epoch(spec.slots_per_epoch),
transfer.slot.epoch(T::slots_per_epoch()),
Domain::Transfer,
&state.fork,
);

View File

@@ -42,7 +42,7 @@ pub fn per_epoch_processing<T: EthSpec>(
validator_statuses.process_attestations(&state, spec)?;
// Justification and finalization.
process_justification_and_finalization(state, &validator_statuses.total_balances, spec)?;
process_justification_and_finalization(state, &validator_statuses.total_balances)?;
// Crosslinks.
let winning_root_for_shards = process_crosslinks(state, spec)?;
@@ -84,9 +84,8 @@ pub fn per_epoch_processing<T: EthSpec>(
pub fn process_justification_and_finalization<T: EthSpec>(
state: &mut BeaconState<T>,
total_balances: &TotalBalances,
spec: &ChainSpec,
) -> Result<(), Error> {
if state.current_epoch() == spec.genesis_epoch {
if state.current_epoch() == T::genesis_epoch() {
return Ok(());
}
@@ -104,14 +103,14 @@ pub fn process_justification_and_finalization<T: EthSpec>(
if total_balances.previous_epoch_target_attesters * 3 >= total_balances.previous_epoch * 2 {
state.current_justified_epoch = previous_epoch;
state.current_justified_root =
*state.get_block_root_at_epoch(state.current_justified_epoch, spec)?;
*state.get_block_root_at_epoch(state.current_justified_epoch)?;
state.justification_bitfield |= 2;
}
// If the current epoch gets justified, fill the last bit.
if total_balances.current_epoch_target_attesters * 3 >= total_balances.current_epoch * 2 {
state.current_justified_epoch = current_epoch;
state.current_justified_root =
*state.get_block_root_at_epoch(state.current_justified_epoch, spec)?;
*state.get_block_root_at_epoch(state.current_justified_epoch)?;
state.justification_bitfield |= 1;
}
@@ -120,22 +119,22 @@ pub fn process_justification_and_finalization<T: EthSpec>(
// The 2nd/3rd/4th most recent epochs are all justified, the 2nd using the 4th as source.
if (bitfield >> 1) % 8 == 0b111 && old_previous_justified_epoch == current_epoch - 3 {
state.finalized_epoch = old_previous_justified_epoch;
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch, spec)?;
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch)?;
}
// The 2nd/3rd most recent epochs are both justified, the 2nd using the 3rd as source.
if (bitfield >> 1) % 4 == 0b11 && state.previous_justified_epoch == current_epoch - 2 {
state.finalized_epoch = old_previous_justified_epoch;
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch, spec)?;
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch)?;
}
// The 1st/2nd/3rd most recent epochs are all justified, the 1st using the 2nd as source.
if bitfield % 8 == 0b111 && state.current_justified_epoch == current_epoch - 2 {
state.finalized_epoch = old_current_justified_epoch;
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch, spec)?;
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch)?;
}
// The 1st/2nd most recent epochs are both justified, the 1st using the 2nd as source.
if bitfield % 4 == 0b11 && state.current_justified_epoch == current_epoch - 1 {
state.finalized_epoch = old_current_justified_epoch;
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch, spec)?;
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch)?;
}
Ok(())
@@ -157,7 +156,7 @@ pub fn process_crosslinks<T: EthSpec>(
state.previous_crosslinks = state.current_crosslinks.clone();
for relative_epoch in vec![RelativeEpoch::Previous, RelativeEpoch::Current] {
for &relative_epoch in &[RelativeEpoch::Previous, RelativeEpoch::Current] {
let epoch = relative_epoch.into_epoch(state.current_epoch());
for offset in 0..state.get_epoch_committee_count(relative_epoch)? {
let shard =
@@ -212,7 +211,7 @@ pub fn process_final_updates<T: EthSpec>(
}
// Update start shard.
state.latest_start_shard = state.next_epoch_start_shard()?;
state.latest_start_shard = state.next_epoch_start_shard(spec)?;
// This is a hack to allow us to update index roots and slashed balances for the next epoch.
//
@@ -241,7 +240,7 @@ pub fn process_final_updates<T: EthSpec>(
state.slot -= 1;
}
if next_epoch.as_u64() % (T::SlotsPerHistoricalRoot::to_u64() / spec.slots_per_epoch) == 0 {
if next_epoch.as_u64() % (T::SlotsPerHistoricalRoot::to_u64() / T::slots_per_epoch()) == 0 {
let historical_batch = state.historical_batch();
state
.historical_roots

View File

@@ -39,7 +39,7 @@ pub fn process_rewards_and_penalties<T: EthSpec>(
winning_root_for_shards: &WinningRootHashSet,
spec: &ChainSpec,
) -> Result<(), Error> {
if state.current_epoch() == spec.genesis_epoch {
if state.current_epoch() == T::genesis_epoch() {
return Ok(());
}

View File

@@ -8,13 +8,14 @@ use types::*;
fn runs_without_error() {
Builder::from_env(Env::default().default_filter_or("error")).init();
let spec = FewValidatorsEthSpec::spec();
let spec = MinimalEthSpec::default_spec();
let mut builder: TestingBeaconStateBuilder<FewValidatorsEthSpec> =
let mut builder: TestingBeaconStateBuilder<MinimalEthSpec> =
TestingBeaconStateBuilder::from_deterministic_keypairs(8, &spec);
let target_slot = (spec.genesis_epoch + 4).end_slot(spec.slots_per_epoch);
builder.teleport_to_slot(target_slot, &spec);
let target_slot =
(MinimalEthSpec::genesis_epoch() + 4).end_slot(MinimalEthSpec::slots_per_epoch());
builder.teleport_to_slot(target_slot);
let (mut state, _keypairs) = builder.build();

View File

@@ -223,7 +223,7 @@ impl ValidatorStatuses {
if is_from_epoch(a, state.current_epoch()) {
status.is_current_epoch_attester = true;
if target_matches_epoch_start_block(a, state, state.current_epoch(), spec)? {
if target_matches_epoch_start_block(a, state, state.current_epoch())? {
status.is_current_epoch_target_attester = true;
}
} else if is_from_epoch(a, state.previous_epoch()) {
@@ -233,7 +233,7 @@ impl ValidatorStatuses {
let attestation_slot = state.get_attestation_slot(&a.data)?;
let inclusion_slot = attestation_slot + a.inclusion_delay;
let relative_epoch =
RelativeEpoch::from_slot(state.slot, inclusion_slot, spec.slots_per_epoch)?;
RelativeEpoch::from_slot(state.slot, inclusion_slot, T::slots_per_epoch())?;
status.inclusion_info = Some(InclusionInfo {
slot: inclusion_slot,
distance: a.inclusion_delay,
@@ -244,7 +244,7 @@ impl ValidatorStatuses {
)?,
});
if target_matches_epoch_start_block(a, state, state.previous_epoch(), spec)? {
if target_matches_epoch_start_block(a, state, state.previous_epoch())? {
status.is_previous_epoch_target_attester = true;
}
@@ -297,7 +297,7 @@ impl ValidatorStatuses {
spec: &ChainSpec,
) -> Result<(), BeaconStateError> {
// Loop through each slot in the previous epoch.
for slot in state.previous_epoch().slot_iter(spec.slots_per_epoch) {
for slot in state.previous_epoch().slot_iter(T::slots_per_epoch()) {
let crosslink_committees_at_slot = state.get_crosslink_committees_at_slot(slot)?;
// Loop through each committee in the slot.
@@ -336,9 +336,8 @@ fn target_matches_epoch_start_block<T: EthSpec>(
a: &PendingAttestation,
state: &BeaconState<T>,
epoch: Epoch,
spec: &ChainSpec,
) -> Result<bool, BeaconStateError> {
let slot = epoch.start_slot(spec.slots_per_epoch);
let slot = epoch.start_slot(T::slots_per_epoch());
let state_boundary_root = *state.get_block_root(slot)?;
Ok(a.data.target_root == state_boundary_root)

View File

@@ -1,5 +1,4 @@
use crate::*;
use tree_hash::SignedRoot;
use types::*;
#[derive(Debug, PartialEq)]
@@ -17,7 +16,7 @@ pub fn per_slot_processing<T: EthSpec>(
) -> Result<(), Error> {
cache_state(state, spec)?;
if (state.slot > spec.genesis_slot) && ((state.slot + 1) % spec.slots_per_epoch == 0) {
if (state.slot > spec.genesis_slot) && ((state.slot + 1) % T::slots_per_epoch() == 0) {
per_epoch_processing(state, spec)?;
}
@@ -44,7 +43,7 @@ fn cache_state<T: EthSpec>(state: &mut BeaconState<T>, spec: &ChainSpec) -> Resu
// Store the previous slot's post state transition root.
state.set_state_root(previous_slot, previous_slot_state_root)?;
let latest_block_root = Hash256::from_slice(&state.latest_block_header.signed_root()[..]);
let latest_block_root = state.latest_block_header.canonical_root();
state.set_block_root(previous_slot, latest_block_root)?;
// Set the state slot back to what it should be.