Skip to content

Commit

Permalink
cargo fmt and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
realbigsean committed May 5, 2022
1 parent 24a5c16 commit 24c6273
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 92 deletions.
4 changes: 3 additions & 1 deletion beacon_node/beacon_chain/src/schema_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ mod migration_schema_v9;
mod types;

use crate::beacon_chain::{BeaconChainTypes, FORK_CHOICE_DB_KEY, OP_POOL_DB_KEY};
use crate::persisted_fork_choice::{PersistedForkChoiceV1, PersistedForkChoiceV7, PersistedForkChoiceV8};
use crate::persisted_fork_choice::{
PersistedForkChoiceV1, PersistedForkChoiceV7, PersistedForkChoiceV8,
};
use crate::validator_pubkey_cache::ValidatorPubkeyCache;
use operation_pool::{PersistedOperationPool, PersistedOperationPoolBase};
use slog::{warn, Logger};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ pub(crate) fn update_fork_choice<T: BeaconChainTypes>(
update_store_justified_checkpoint(persisted_fork_choice, &mut fork_choice)
.map_err(StoreError::SchemaMigrationError)?;


// Need to downgrade the SSZ container to V7 so that all migrations can be applied in sequence.
let ssz_container = SszContainer::from(&fork_choice);
let ssz_container_v7 = SszContainerV7::from(ssz_container);
Expand Down
39 changes: 20 additions & 19 deletions beacon_node/beacon_chain/src/schema_change/migration_schema_v9.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
use std::collections::HashMap;
use crate::persisted_fork_choice::PersistedForkChoiceV8;
use crate::schema_change::{
types::{SszContainerV7, SszContainerV9},
StoreError,
};
use proto_array::core::SszContainer;
use proto_array::ProtoArrayForkChoice;
use ssz::{Decode, Encode};
use crate::beacon_fork_choice_store::PersistedForkChoiceStoreV8;
use crate::persisted_fork_choice::PersistedForkChoiceV8;
use crate::schema_change::{StoreError, types::{SszContainerV6, SszContainerV7, SszContainerV9}};

pub fn update_fork_choice(
mut fork_choice: PersistedForkChoiceV8,
) -> Result<PersistedForkChoiceV8, StoreError> {
let ssz_container_v7 =
SszContainerV7::from_ssz_bytes(&fork_choice.fork_choice.proto_array_bytes)
.map_err(|e| {
StoreError::SchemaMigrationError(format!(
"Failed to decode ProtoArrayForkChoice during schema migration: {:?}",
e
))
})?;
let ssz_container_v7 = SszContainerV7::from_ssz_bytes(
&fork_choice.fork_choice.proto_array_bytes,
)
.map_err(|e| {
StoreError::SchemaMigrationError(format!(
"Failed to decode ProtoArrayForkChoice during schema migration: {:?}",
e
))
})?;

// These transformations instantiate `node.unrealized_justified_checkpoint` and
// `node.unrealized_finalized_checkpoint` to `None`.
let ssz_container_v9: SszContainerV9 = ssz_container_v7.into();
let ssz_container: SszContainer = ssz_container_v9.into();
// These transformations instantiate `node.unrealized_justified_checkpoint` and
// `node.unrealized_finalized_checkpoint` to `None`.
let ssz_container_v9: SszContainerV9 = ssz_container_v7.into();
let ssz_container: SszContainer = ssz_container_v9.into();

fork_choice.fork_choice.proto_array_bytes = ssz_container.as_ssz_bytes();
fork_choice.fork_choice.proto_array_bytes = ssz_container.as_ssz_bytes();

Ok(fork_choice)
Ok(fork_choice)
}
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/schema_change/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,4 @@ impl From<SszContainer> for SszContainerV7 {
previous_proposer_boost: container.previous_proposer_boost,
}
}
}
}
4 changes: 2 additions & 2 deletions beacon_node/http_api/src/attestation_performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use eth2::lighthouse::{
AttestationPerformance, AttestationPerformanceQuery, AttestationPerformanceStatistics,
};
use state_processing::{
BlockReplayer, BlockReplayError, per_epoch_processing::EpochProcessingSummary,
per_epoch_processing::EpochProcessingSummary, BlockReplayError, BlockReplayer,
};
use std::sync::Arc;
use types::{BeaconState, BeaconStateError, EthSpec, Hash256, SignedBeaconBlock};
use types::beacon_state::participation_cache::Error as ParticipationCacheError;
use types::{BeaconState, BeaconStateError, EthSpec, Hash256, SignedBeaconBlock};
use warp_utils::reject::{beacon_chain_error, custom_bad_request, custom_server_error};

const MAX_REQUEST_RANGE_EPOCHS: usize = 100;
Expand Down
7 changes: 2 additions & 5 deletions beacon_node/http_api/src/validator_inclusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use eth2::{
lighthouse::{GlobalValidatorInclusionData, ValidatorInclusionData},
types::ValidatorId,
};
use state_processing::per_epoch_processing::{
EpochProcessingSummary,
process_epoch,
};
use types::{BeaconState, ChainSpec, Epoch, EthSpec};
use state_processing::per_epoch_processing::{process_epoch, EpochProcessingSummary};
use types::beacon_state::participation_cache::Error as ParticipationCacheError;
use types::{BeaconState, ChainSpec, Epoch, EthSpec};

/// Returns the state in the last slot of `epoch`.
fn end_of_epoch_state<T: BeaconChainTypes>(
Expand Down
5 changes: 3 additions & 2 deletions consensus/fork_choice/src/fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub enum Error<T> {
MissingFinalizedBlock {
finalized_checkpoint: Checkpoint,
},
UnrealizedVoteProcessing(state_processing::EpochProcessingError)
UnrealizedVoteProcessing(state_processing::EpochProcessingError),
}

impl<T> From<InvalidAttestation> for Error<T> {
Expand Down Expand Up @@ -661,7 +661,8 @@ where
}

// Update unrealized justified/finalized checkpoints.
let (mini_beacon_state, _ ) = state_processing::per_epoch_processing::altair::process_justifiable(state, spec)?;
let (mini_beacon_state, _) =
state_processing::per_epoch_processing::altair::process_justifiable(state, spec)?;

let target_slot = block
.slot()
Expand Down
32 changes: 20 additions & 12 deletions consensus/proto_array/src/proto_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,21 +858,29 @@ impl ProtoArray {
return false;
}


let predicate = |node_justified_checkpoint: Checkpoint, node_finalized_checkpoint: Checkpoint| {
let correct_justified = node_justified_checkpoint == self.justified_checkpoint || self.justified_checkpoint.epoch == Epoch::new(0);
let correct_finalized = node_finalized_checkpoint == self.finalized_checkpoint || self.finalized_checkpoint.epoch == Epoch::new(0);
let checkpoint_match_predicate =
|node_justified_checkpoint: Checkpoint, node_finalized_checkpoint: Checkpoint| {
let correct_justified = node_justified_checkpoint == self.justified_checkpoint
|| self.justified_checkpoint.epoch == Epoch::new(0);
let correct_finalized = node_finalized_checkpoint == self.finalized_checkpoint
|| self.finalized_checkpoint.epoch == Epoch::new(0);
correct_justified && correct_finalized
};
};

if let (Some(unrealized_justified_checkpoint), Some(unrealized_finalized_checkpoint)) = (node.unrealized_justified_checkpoint, node.unrealized_finalized_checkpoint) {
predicate(unrealized_justified_checkpoint, unrealized_finalized_checkpoint)
if let (Some(unrealized_justified_checkpoint), Some(unrealized_finalized_checkpoint)) = (
node.unrealized_justified_checkpoint,
node.unrealized_finalized_checkpoint,
) {
checkpoint_match_predicate(
unrealized_justified_checkpoint,
unrealized_finalized_checkpoint,
)
} else if let (Some(justified_checkpoint), Some(finalized_checkpoint)) =
(node.justified_checkpoint, node.finalized_checkpoint)
{
checkpoint_match_predicate(justified_checkpoint, finalized_checkpoint)
} else {
if let (Some(justified_checkpoint), Some(finalized_checkpoint)) = (node.justified_checkpoint, node.finalized_checkpoint) {
predicate(justified_checkpoint, finalized_checkpoint)
} else {
false
}
false
}
}

Expand Down
8 changes: 6 additions & 2 deletions consensus/proto_array/src/proto_array_fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ pub struct Block {
/// Indicates if an execution node has marked this block as valid. Also contains the execution
/// block hash.
pub execution_status: ExecutionStatus,
pub unrealized_justified_checkpoint : Option<Checkpoint>,
pub unrealized_finalized_checkpoint : Option<Checkpoint>,
pub unrealized_justified_checkpoint: Option<Checkpoint>,
pub unrealized_finalized_checkpoint: Option<Checkpoint>,
}

/// A Vec-wrapper which will grow to match any request.
Expand Down Expand Up @@ -540,6 +540,8 @@ mod test_compute_deltas {
justified_checkpoint: genesis_checkpoint,
finalized_checkpoint: genesis_checkpoint,
execution_status,
unrealized_justified_checkpoint: None,
unrealized_finalized_checkpoint: None,
})
.unwrap();

Expand All @@ -556,6 +558,8 @@ mod test_compute_deltas {
justified_checkpoint: genesis_checkpoint,
finalized_checkpoint: genesis_checkpoint,
execution_status,
unrealized_justified_checkpoint: None,
unrealized_finalized_checkpoint: None,
})
.unwrap();

Expand Down
11 changes: 6 additions & 5 deletions consensus/state_processing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ pub mod state_advance;
pub mod upgrade;
pub mod verify_operation;

pub use block_replayer::{BlockReplayer, BlockReplayError, StateRootStrategy};
pub use block_replayer::{BlockReplayError, BlockReplayer, StateRootStrategy};
pub use genesis::{
eth2_genesis_time, initialize_beacon_state_from_eth1, is_valid_genesis_state,
process_activations,
};
pub use per_block_processing::{
altair::sync_committee, block_signature_verifier, BlockSignatureStrategy, BlockSignatureVerifier,
errors::BlockProcessingError, per_block_processing, signature_sets, VerifyBlockRoot, VerifySignatures
altair::sync_committee, block_signature_verifier, errors::BlockProcessingError,
per_block_processing, signature_sets, BlockSignatureStrategy, BlockSignatureVerifier,
VerifyBlockRoot, VerifySignatures,
};
pub use per_epoch_processing::{
errors::EpochProcessingError, process_epoch as per_epoch_processing
errors::EpochProcessingError, process_epoch as per_epoch_processing,
};
pub use per_slot_processing::{Error as SlotProcessingError, per_slot_processing};
pub use per_slot_processing::{per_slot_processing, Error as SlotProcessingError};
pub use verify_operation::{SigVerifiedOp, VerifyOperation};
16 changes: 9 additions & 7 deletions consensus/state_processing/src/per_epoch_processing/altair.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use itertools::min;
use super::{EpochProcessingSummary, Error, process_registry_updates, process_slashings};
use super::{process_registry_updates, process_slashings, EpochProcessingSummary, Error};
use crate::per_epoch_processing::{
effective_balance_updates::process_effective_balance_updates,
historical_roots_update::process_historical_roots_update,
resets::{process_eth1_data_reset, process_randao_mixes_reset, process_slashings_reset},
};
pub use inactivity_updates::process_inactivity_updates;
pub use justification_and_finalization::process_justification_and_finalization;
pub use types::beacon_state::participation_cache::ParticipationCache;
pub use participation_flag_updates::process_participation_flag_updates;
pub use rewards_and_penalties::process_rewards_and_penalties;
pub use sync_committee_updates::process_sync_committee_updates;
use types::{BeaconState, ChainSpec, EthSpec, MiniBeaconState, RelativeEpoch};
use types::beacon_state::participation_cache::CurrentEpochParticipationCache;
pub use types::beacon_state::participation_cache::ParticipationCache;
use types::{BeaconState, ChainSpec, EthSpec, MiniBeaconState, RelativeEpoch};

pub mod inactivity_updates;
pub mod justification_and_finalization;
Expand All @@ -24,7 +23,6 @@ pub fn process_epoch<T: EthSpec>(
state: &mut BeaconState<T>,
spec: &ChainSpec,
) -> Result<EpochProcessingSummary<T>, Error> {

state.build_committee_cache(RelativeEpoch::Next, spec)?;

let (mini_beacon_state, participation_cache) = process_justifiable(state, spec)?;
Expand Down Expand Up @@ -76,7 +74,10 @@ pub fn process_epoch<T: EthSpec>(
})
}

pub fn process_justifiable<T: EthSpec>(state: &mut BeaconState<T>, spec: &ChainSpec) -> Result<(MiniBeaconState<T>, ParticipationCache), Error> {
pub fn process_justifiable<T: EthSpec>(
state: &mut BeaconState<T>,
spec: &ChainSpec,
) -> Result<(MiniBeaconState<T>, ParticipationCache), Error> {
// Ensure the committee caches are built.
state.build_committee_cache(RelativeEpoch::Previous, spec)?;
state.build_committee_cache(RelativeEpoch::Current, spec)?;
Expand All @@ -86,7 +87,8 @@ pub fn process_justifiable<T: EthSpec>(state: &mut BeaconState<T>, spec: &ChainS

let current_participation_cache = CurrentEpochParticipationCache::new(state, spec)?;

let participation_cache = ParticipationCache::new(prev_participation_cache, current_participation_cache);
let participation_cache =
ParticipationCache::new(prev_participation_cache, current_participation_cache);

// Justification and finalization.
let mini_beacon_state = process_justification_and_finalization(state, &participation_cache)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::per_epoch_processing::Error;
use safe_arith::SafeArith;
use types::consts::altair::TIMELY_TARGET_FLAG_INDEX;
use types::{BeaconState, EthSpec, MiniBeaconState};
use types::beacon_state::participation_cache::CurrentEpochParticipationCache;

/// Update the justified and finalized checkpoints for matching target attestations.
pub fn process_justification_and_finalization<T: EthSpec>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use types::{BeaconState, ChainSpec, EthSpec};

use crate::common::{altair::get_base_reward, decrease_balance, increase_balance};
use crate::per_epoch_processing::{Delta, Error};
use types::beacon_state::participation_cache::CurrentEpochParticipationCache;

/// Apply attester and proposer rewards.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::{
altair::ParticipationCache,
base::{TotalBalances, validator_statuses::InclusionInfo, ValidatorStatus},
base::{validator_statuses::InclusionInfo, TotalBalances, ValidatorStatus},
};
use crate::metrics;
use std::sync::Arc;
use types::{EthSpec, SyncCommittee};
use types::beacon_state::participation_cache::Error as ParticipationCacheError;
use types::{EthSpec, SyncCommittee};

/// Provides a summary of validator participation during the epoch.
#[derive(PartialEq, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub fn weigh_justification_and_finalization<T: EthSpec>(
};

// Process justifications
mini_beacon_state.previous_justified_checkpoint = mini_beacon_state.current_justified_checkpoint;
mini_beacon_state.previous_justified_checkpoint =
mini_beacon_state.current_justified_checkpoint;
mini_beacon_state.justification_bits.shift_up(1)?;

if previous_target_balance.safe_mul(3)? >= total_active_balance.safe_mul(2)? {
Expand Down
22 changes: 11 additions & 11 deletions consensus/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ use compare_fields_derive::CompareFields;
use derivative::Derivative;
use eth2_hashing::hash;
use int_to_bytes::{int_to_bytes4, int_to_bytes8};
use participation_cache::PreviousParticipationCache;
use pubkey_cache::PubkeyCache;
use safe_arith::{ArithError, SafeArith};
use serde_derive::{Deserialize, Serialize};
use ssz::{Decode, DecodeError, Encode, ssz_encode};
use ssz::{ssz_encode, Decode, DecodeError, Encode};
use ssz_derive::{Decode, Encode};
use ssz_types::{BitVector, FixedVector, typenum::Unsigned};
use ssz_types::{typenum::Unsigned, BitVector, FixedVector};
use std::convert::TryInto;
use std::{fmt, mem, sync::Arc};
use superstruct::superstruct;
use swap_or_not_shuffle::compute_shuffled_index;
use test_random_derive::TestRandom;
use tree_hash::TreeHash;
use tree_hash_derive::TreeHash;
use participation_cache::PreviousParticipationCache;

pub use self::committee_cache::{
CommitteeCache, compute_committee_index_in_epoch, compute_committee_range_in_epoch,
epoch_committee_count,
compute_committee_index_in_epoch, compute_committee_range_in_epoch, epoch_committee_count,
CommitteeCache,
};
pub use clone_config::CloneConfig;
pub use eth_spec::*;
Expand All @@ -36,10 +36,10 @@ mod committee_cache;
mod clone_config;
mod exit_cache;
mod iter;
pub mod participation_cache;
mod pubkey_cache;
mod tests;
mod tree_hash_cache;
pub mod participation_cache;

pub const CACHED_EPOCHS: usize = 3;
const MAX_RANDOM_BYTE: u64 = (1 << 8) - 1;
Expand Down Expand Up @@ -1363,7 +1363,10 @@ impl<T: EthSpec> BeaconState<T> {
Ok(())
}

pub fn get_previous_epoch_participation_cache(&mut self, spec: &ChainSpec) -> Result<PreviousParticipationCache, BeaconStateError> {
pub fn get_previous_epoch_participation_cache(
&mut self,
spec: &ChainSpec,
) -> Result<PreviousParticipationCache, BeaconStateError> {
if let Some(cache) = self.previous_epoch_participation_cache() {
if cache.initialized_epoch() == self.current_epoch() {
Ok(cache.clone())
Expand Down Expand Up @@ -1664,10 +1667,7 @@ impl<T: EthSpec> BeaconState<T> {
Ok(sync_committee)
}

pub fn update_justifiable(
&mut self,
mini_beacon_state: MiniBeaconState<T>,
) {
pub fn update_justifiable(&mut self, mini_beacon_state: MiniBeaconState<T>) {
*self.current_justified_checkpoint_mut() = mini_beacon_state.current_justified_checkpoint;
*self.previous_justified_checkpoint_mut() = mini_beacon_state.previous_justified_checkpoint;
*self.finalized_checkpoint_mut() = mini_beacon_state.finalized_checkpoint;
Expand Down
Loading

0 comments on commit 24c6273

Please sign in to comment.