v1.1.6 Fork Choice changes (#2822)

## Issue Addressed

Resolves: https://github.com/sigp/lighthouse/issues/2741
Includes: https://github.com/sigp/lighthouse/pull/2853 so that we can get ssz static tests passing here on v1.1.6. If we want to merge that first, we can make this diff slightly smaller

## Proposed Changes

- Changes the `justified_epoch` and `finalized_epoch` in the `ProtoArrayNode` each to an `Option<Checkpoint>`. The `Option` is necessary only for the migration, so not ideal. But does allow us to add a default logic to `None` on these fields during the database migration.
- Adds a database migration from a legacy fork choice struct to the new one, search for all necessary block roots in fork choice by iterating through blocks in the db.
- updates related to https://github.com/ethereum/consensus-specs/pull/2727
  -  We will have to update the persisted forkchoice to make sure the justified checkpoint stored is correct according to the updated fork choice logic. This boils down to setting the forkchoice store's justified checkpoint to the justified checkpoint of the block that advanced the finalized checkpoint to the current one. 
  - AFAICT there's no migration steps necessary for the update to allow applying attestations from prior blocks, but would appreciate confirmation on that
- I updated the consensus spec tests to v1.1.6 here, but they will fail until we also implement the proposer score boost updates. I confirmed that the previously failing scenario `new_finalized_slot_is_justified_checkpoint_ancestor` will now pass after the boost updates, but haven't confirmed _all_ tests will pass because I just quickly stubbed out the proposer boost test scenario formatting.
- This PR now also includes proposer boosting https://github.com/ethereum/consensus-specs/pull/2730

## Additional Info
I realized checking justified and finalized roots in fork choice makes it more likely that we trigger this bug: https://github.com/ethereum/consensus-specs/pull/2727

It's possible the combination of justified checkpoint and finalized checkpoint in the forkchoice store is different from in any block in fork choice. So when trying to startup our store's justified checkpoint seems invalid to the rest of fork choice (but it should be valid). When this happens we get an `InvalidBestNode` error and fail to start up. So I'm including that bugfix in this branch.

Todo:

- [x] Fix fork choice tests
- [x] Self review
- [x] Add fix for https://github.com/ethereum/consensus-specs/pull/2727
- [x] Rebase onto Kintusgi 
- [x] Fix `num_active_validators` calculation as @michaelsproul pointed out
- [x] Clean up db migrations

Co-authored-by: realbigsean <seananderson33@gmail.com>
This commit is contained in:
realbigsean
2021-12-13 20:43:22 +00:00
parent e391b32858
commit b22ac95d7f
44 changed files with 1802 additions and 658 deletions

View File

@@ -1,14 +1,14 @@
use std::marker::PhantomData;
use crate::ForkChoiceStore;
use proto_array::{Block as ProtoBlock, ExecutionStatus, ProtoArrayForkChoice};
use ssz_derive::{Decode, Encode};
use types::{
AttestationShufflingId, BeaconBlock, BeaconState, BeaconStateError, ChainSpec, Checkpoint,
Epoch, EthSpec, Hash256, IndexedAttestation, RelativeEpoch, SignedBeaconBlock, Slot,
};
use crate::ForkChoiceStore;
use std::cmp::Ordering;
use std::marker::PhantomData;
use std::time::Duration;
use types::{
consts::merge::INTERVALS_PER_SLOT, AttestationShufflingId, BeaconBlock, BeaconState,
BeaconStateError, ChainSpec, Checkpoint, Epoch, EthSpec, Hash256, IndexedAttestation,
RelativeEpoch, SignedBeaconBlock, Slot,
};
#[derive(Debug)]
pub enum Error<T> {
@@ -168,6 +168,13 @@ where
store.set_current_slot(time);
let current_slot = store.get_current_slot();
// Reset proposer boost if this is a new slot.
if current_slot > previous_slot {
store.set_proposer_boost_root(Hash256::zero());
}
// Not a new epoch, return.
if !(current_slot > previous_slot && compute_slots_since_epoch_start::<E>(current_slot) == 0) {
return Ok(());
}
@@ -218,6 +225,15 @@ fn dequeue_attestations(
std::mem::replace(queued_attestations, remaining)
}
/// Denotes whether an attestation we are processing was received from a block or from gossip.
/// Equivalent to the `is_from_block` `bool` in:
///
/// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/fork-choice.md#validate_on_attestation
pub enum AttestationFromBlock {
True,
False,
}
/// Provides an implementation of "Ethereum 2.0 Phase 0 -- Beacon Chain Fork Choice":
///
/// https://github.com/ethereum/eth2.0-specs/blob/v0.12.1/specs/phase0/fork-choice.md#ethereum-20-phase-0----beacon-chain-fork-choice
@@ -292,9 +308,8 @@ where
let proto_array = ProtoArrayForkChoice::new(
finalized_block_slot,
finalized_block_state_root,
fc_store.justified_checkpoint().epoch,
fc_store.finalized_checkpoint().epoch,
fc_store.finalized_checkpoint().root,
*fc_store.justified_checkpoint(),
*fc_store.finalized_checkpoint(),
current_epoch_shuffling_id,
next_epoch_shuffling_id,
execution_status,
@@ -377,17 +392,22 @@ where
/// Is equivalent to:
///
/// https://github.com/ethereum/eth2.0-specs/blob/v0.12.1/specs/phase0/fork-choice.md#get_head
pub fn get_head(&mut self, current_slot: Slot) -> Result<Hash256, Error<T::Error>> {
pub fn get_head(
&mut self,
current_slot: Slot,
spec: &ChainSpec,
) -> Result<Hash256, Error<T::Error>> {
self.update_time(current_slot)?;
let store = &mut self.fc_store;
self.proto_array
.find_head(
store.justified_checkpoint().epoch,
store.justified_checkpoint().root,
store.finalized_checkpoint().epoch,
.find_head::<E>(
*store.justified_checkpoint(),
*store.finalized_checkpoint(),
store.justified_balances(),
store.proposer_boost_root(),
spec,
)
.map_err(Into::into)
}
@@ -462,11 +482,13 @@ where
///
/// The supplied block **must** pass the `state_transition` function as it will not be run
/// here.
#[allow(clippy::too_many_arguments)]
pub fn on_block(
&mut self,
current_slot: Slot,
block: &BeaconBlock<E>,
block_root: Hash256,
block_delay: Duration,
state: &BeaconState<E>,
payload_verification_status: PayloadVerificationStatus,
spec: &ChainSpec,
@@ -520,6 +542,13 @@ where
}));
}
// Add proposer score boost if the block is timely.
let is_before_attesting_interval =
block_delay < Duration::from_secs(spec.seconds_per_slot / INTERVALS_PER_SLOT);
if current_slot == block.slot() && is_before_attesting_interval {
self.fc_store.set_proposer_boost_root(block_root);
}
// Update justified checkpoint.
if state.current_justified_checkpoint().epoch > self.fc_store.justified_checkpoint().epoch {
if state.current_justified_checkpoint().epoch
@@ -539,25 +568,9 @@ where
if state.finalized_checkpoint().epoch > self.fc_store.finalized_checkpoint().epoch {
self.fc_store
.set_finalized_checkpoint(state.finalized_checkpoint());
let finalized_slot =
compute_start_slot_at_epoch::<E>(self.fc_store.finalized_checkpoint().epoch);
// Note: the `if` statement here is not part of the specification, but I claim that it
// is an optimization and equivalent to the specification. See this PR for more
// information:
//
// https://github.com/ethereum/eth2.0-specs/pull/1880
if *self.fc_store.justified_checkpoint() != state.current_justified_checkpoint()
&& (state.current_justified_checkpoint().epoch
> self.fc_store.justified_checkpoint().epoch
|| self
.get_ancestor(self.fc_store.justified_checkpoint().root, finalized_slot)?
!= Some(self.fc_store.finalized_checkpoint().root))
{
self.fc_store
.set_justified_checkpoint(state.current_justified_checkpoint())
.map_err(Error::UnableToSetJustifiedCheckpoint)?;
}
self.fc_store
.set_justified_checkpoint(state.current_justified_checkpoint())
.map_err(Error::UnableToSetJustifiedCheckpoint)?;
}
let target_slot = block
@@ -623,14 +636,43 @@ where
)
.map_err(Error::BeaconStateError)?,
state_root: block.state_root(),
justified_epoch: state.current_justified_checkpoint().epoch,
finalized_epoch: state.finalized_checkpoint().epoch,
justified_checkpoint: state.current_justified_checkpoint(),
finalized_checkpoint: state.finalized_checkpoint(),
execution_status,
})?;
Ok(())
}
/// Validates the `epoch` against the current time according to the fork choice store.
///
/// ## Specification
///
/// Equivalent to:
///
/// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/fork-choice.md#validate_target_epoch_against_current_time
fn validate_target_epoch_against_current_time(
&self,
target_epoch: Epoch,
) -> Result<(), InvalidAttestation> {
let slot_now = self.fc_store.get_current_slot();
let epoch_now = slot_now.epoch(E::slots_per_epoch());
// Attestation must be from the current or previous epoch.
if target_epoch > epoch_now {
return Err(InvalidAttestation::FutureEpoch {
attestation_epoch: target_epoch,
current_epoch: epoch_now,
});
} else if target_epoch + 1 < epoch_now {
return Err(InvalidAttestation::PastEpoch {
attestation_epoch: target_epoch,
current_epoch: epoch_now,
});
}
Ok(())
}
/// Validates the `indexed_attestation` for application to fork choice.
///
/// ## Specification
@@ -641,6 +683,7 @@ where
fn validate_on_attestation(
&self,
indexed_attestation: &IndexedAttestation<E>,
is_from_block: AttestationFromBlock,
) -> Result<(), InvalidAttestation> {
// There is no point in processing an attestation with an empty bitfield. Reject
// it immediately.
@@ -651,21 +694,10 @@ where
return Err(InvalidAttestation::EmptyAggregationBitfield);
}
let slot_now = self.fc_store.get_current_slot();
let epoch_now = slot_now.epoch(E::slots_per_epoch());
let target = indexed_attestation.data.target;
// Attestation must be from the current or previous epoch.
if target.epoch > epoch_now {
return Err(InvalidAttestation::FutureEpoch {
attestation_epoch: target.epoch,
current_epoch: epoch_now,
});
} else if target.epoch + 1 < epoch_now {
return Err(InvalidAttestation::PastEpoch {
attestation_epoch: target.epoch,
current_epoch: epoch_now,
});
if matches!(is_from_block, AttestationFromBlock::False) {
self.validate_target_epoch_against_current_time(target.epoch)?;
}
if target.epoch != indexed_attestation.data.slot.epoch(E::slots_per_epoch()) {
@@ -748,6 +780,7 @@ where
&mut self,
current_slot: Slot,
attestation: &IndexedAttestation<E>,
is_from_block: AttestationFromBlock,
) -> Result<(), Error<T::Error>> {
// Ensure the store is up-to-date.
self.update_time(current_slot)?;
@@ -769,7 +802,7 @@ where
return Ok(());
}
self.validate_on_attestation(attestation)?;
self.validate_on_attestation(attestation, is_from_block)?;
if attestation.data.slot < self.fc_store.get_current_slot() {
for validator_index in attestation.attesting_indices.iter() {
@@ -895,6 +928,11 @@ where
&self.queued_attestations
}
/// Returns the store's `proposer_boost_root`.
pub fn proposer_boost_root(&self) -> Hash256 {
self.fc_store.proposer_boost_root()
}
/// Prunes the underlying fork choice DAG.
pub fn prune(&mut self) -> Result<(), Error<T::Error>> {
let finalized_root = self.fc_store.finalized_checkpoint().root;

View File

@@ -19,7 +19,7 @@ use types::{BeaconBlock, BeaconState, Checkpoint, EthSpec, Hash256, Slot};
pub trait ForkChoiceStore<T: EthSpec>: Sized {
type Error;
/// Returns the last value passed to `Self::update_time`.
/// Returns the last value passed to `Self::set_current_slot`.
fn get_current_slot(&self) -> Slot;
/// Set the value to be returned by `Self::get_current_slot`.
@@ -50,6 +50,9 @@ pub trait ForkChoiceStore<T: EthSpec>: Sized {
/// Returns the `finalized_checkpoint`.
fn finalized_checkpoint(&self) -> &Checkpoint;
/// Returns the `proposer_boost_root`.
fn proposer_boost_root(&self) -> Hash256;
/// Sets `finalized_checkpoint`.
fn set_finalized_checkpoint(&mut self, checkpoint: Checkpoint);
@@ -58,4 +61,7 @@ pub trait ForkChoiceStore<T: EthSpec>: Sized {
/// Sets the `best_justified_checkpoint`.
fn set_best_justified_checkpoint(&mut self, checkpoint: Checkpoint);
/// Sets the proposer boost root.
fn set_proposer_boost_root(&mut self, proposer_boost_root: Hash256);
}

View File

@@ -2,8 +2,8 @@ mod fork_choice;
mod fork_choice_store;
pub use crate::fork_choice::{
Error, ForkChoice, InvalidAttestation, InvalidBlock, PayloadVerificationStatus,
PersistedForkChoice, QueuedAttestation,
AttestationFromBlock, Error, ForkChoice, InvalidAttestation, InvalidBlock,
PayloadVerificationStatus, PersistedForkChoice, QueuedAttestation,
};
pub use fork_choice_store::ForkChoiceStore;
pub use proto_array::Block as ProtoBlock;

View File

@@ -2,6 +2,7 @@
use std::fmt;
use std::sync::Mutex;
use std::time::Duration;
use beacon_chain::test_utils::{
AttestationStrategy, BeaconChainHarness, BlockStrategy, EphemeralHarnessType,
@@ -274,6 +275,7 @@ impl ForkChoiceTest {
current_slot,
&block,
block.canonical_root(),
Duration::from_secs(0),
&state,
PayloadVerificationStatus::Verified,
&self.harness.chain.spec,
@@ -316,6 +318,7 @@ impl ForkChoiceTest {
current_slot,
&block,
block.canonical_root(),
Duration::from_secs(0),
&state,
PayloadVerificationStatus::Verified,
&self.harness.chain.spec,

View File

@@ -1,4 +1,4 @@
use types::{Epoch, Hash256};
use types::{Checkpoint, Epoch, Hash256};
#[derive(Clone, PartialEq, Debug)]
pub enum Error {
@@ -13,6 +13,7 @@ pub enum Error {
InvalidParentDelta(usize),
InvalidNodeDelta(usize),
DeltaOverflow(usize),
ProposerBoostOverflow(usize),
IndexOverflow(&'static str),
InvalidDeltaLen {
deltas: usize,
@@ -22,16 +23,19 @@ pub enum Error {
current_finalized_epoch: Epoch,
new_finalized_epoch: Epoch,
},
InvalidBestNode {
start_root: Hash256,
justified_epoch: Epoch,
finalized_epoch: Epoch,
head_root: Hash256,
head_justified_epoch: Epoch,
head_finalized_epoch: Epoch,
},
InvalidBestNode(Box<InvalidBestNodeInfo>),
InvalidAncestorOfValidPayload {
ancestor_block_root: Hash256,
ancestor_payload_block_hash: Hash256,
},
}
#[derive(Clone, PartialEq, Debug)]
pub struct InvalidBestNodeInfo {
pub start_root: Hash256,
pub justified_checkpoint: Checkpoint,
pub finalized_checkpoint: Checkpoint,
pub head_root: Hash256,
pub head_justified_checkpoint: Option<Checkpoint>,
pub head_finalized_checkpoint: Option<Checkpoint>,
}

View File

@@ -4,7 +4,7 @@ mod votes;
use crate::proto_array_fork_choice::{Block, ExecutionStatus, ProtoArrayForkChoice};
use serde_derive::{Deserialize, Serialize};
use types::{AttestationShufflingId, Epoch, Hash256, Slot};
use types::{AttestationShufflingId, Checkpoint, Epoch, EthSpec, Hash256, MainnetEthSpec, Slot};
pub use ffg_updates::*;
pub use no_votes::*;
@@ -13,24 +13,22 @@ pub use votes::*;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Operation {
FindHead {
justified_epoch: Epoch,
justified_root: Hash256,
finalized_epoch: Epoch,
justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
justified_state_balances: Vec<u64>,
expected_head: Hash256,
},
InvalidFindHead {
justified_epoch: Epoch,
justified_root: Hash256,
finalized_epoch: Epoch,
justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
justified_state_balances: Vec<u64>,
},
ProcessBlock {
slot: Slot,
root: Hash256,
parent_root: Hash256,
justified_epoch: Epoch,
finalized_epoch: Epoch,
justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
},
ProcessAttestation {
validator_index: usize,
@@ -47,9 +45,8 @@ pub enum Operation {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForkChoiceTestDefinition {
pub finalized_block_slot: Slot,
pub justified_epoch: Epoch,
pub finalized_epoch: Epoch,
pub finalized_root: Hash256,
pub justified_checkpoint: Checkpoint,
pub finalized_checkpoint: Checkpoint,
pub operations: Vec<Operation>,
}
@@ -61,9 +58,8 @@ impl ForkChoiceTestDefinition {
let mut fork_choice = ProtoArrayForkChoice::new(
self.finalized_block_slot,
Hash256::zero(),
self.justified_epoch,
self.finalized_epoch,
self.finalized_root,
self.justified_checkpoint,
self.finalized_checkpoint,
junk_shuffling_id.clone(),
junk_shuffling_id,
execution_status,
@@ -73,21 +69,22 @@ impl ForkChoiceTestDefinition {
for (op_index, op) in self.operations.into_iter().enumerate() {
match op.clone() {
Operation::FindHead {
justified_epoch,
justified_root,
finalized_epoch,
justified_checkpoint,
finalized_checkpoint,
justified_state_balances,
expected_head,
} => {
let head = fork_choice
.find_head(
justified_epoch,
justified_root,
finalized_epoch,
.find_head::<MainnetEthSpec>(
justified_checkpoint,
finalized_checkpoint,
&justified_state_balances,
Hash256::zero(),
&MainnetEthSpec::default_spec(),
)
.unwrap_or_else(|_| {
panic!("find_head op at index {} returned error", op_index)
.map_err(|e| e)
.unwrap_or_else(|e| {
panic!("find_head op at index {} returned error {}", op_index, e)
});
assert_eq!(
@@ -98,16 +95,16 @@ impl ForkChoiceTestDefinition {
check_bytes_round_trip(&fork_choice);
}
Operation::InvalidFindHead {
justified_epoch,
justified_root,
finalized_epoch,
justified_checkpoint,
finalized_checkpoint,
justified_state_balances,
} => {
let result = fork_choice.find_head(
justified_epoch,
justified_root,
finalized_epoch,
let result = fork_choice.find_head::<MainnetEthSpec>(
justified_checkpoint,
finalized_checkpoint,
&justified_state_balances,
Hash256::zero(),
&MainnetEthSpec::default_spec(),
);
assert!(
@@ -122,8 +119,8 @@ impl ForkChoiceTestDefinition {
slot,
root,
parent_root,
justified_epoch,
finalized_epoch,
justified_checkpoint,
finalized_checkpoint,
} => {
let block = Block {
slot,
@@ -139,8 +136,8 @@ impl ForkChoiceTestDefinition {
Epoch::new(0),
Hash256::zero(),
),
justified_epoch,
finalized_epoch,
justified_checkpoint,
finalized_checkpoint,
execution_status,
};
fork_choice.process_block(block).unwrap_or_else(|e| {
@@ -193,7 +190,16 @@ impl ForkChoiceTestDefinition {
/// Gives a hash that is not the zero hash (unless i is `usize::max_value)`.
fn get_hash(i: u64) -> Hash256 {
Hash256::from_low_u64_be(i)
Hash256::from_low_u64_be(i + 1)
}
/// Gives a checkpoint with a root that is not the zero hash (unless i is `usize::max_value)`.
/// `Epoch` will always equal `i`.
fn get_checkpoint(i: u64) -> Checkpoint {
Checkpoint {
epoch: Epoch::new(i),
root: get_hash(i),
}
}
fn check_bytes_round_trip(original: &ProtoArrayForkChoice) {

View File

@@ -6,9 +6,8 @@ pub fn get_ffg_case_01_test_definition() -> ForkChoiceTestDefinition {
// Ensure that the head starts at the finalized block.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(0),
});
@@ -26,22 +25,22 @@ pub fn get_ffg_case_01_test_definition() -> ForkChoiceTestDefinition {
slot: Slot::new(1),
root: get_hash(1),
parent_root: get_hash(0),
justified_epoch: Epoch::new(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(2),
root: get_hash(2),
parent_root: get_hash(1),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(1),
finalized_checkpoint: get_checkpoint(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(3),
root: get_hash(3),
parent_root: get_hash(2),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(1),
justified_checkpoint: get_checkpoint(2),
finalized_checkpoint: get_checkpoint(1),
});
// Ensure that with justified epoch 0 we find 3
@@ -54,9 +53,8 @@ pub fn get_ffg_case_01_test_definition() -> ForkChoiceTestDefinition {
// |
// 3 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(3),
});
@@ -71,9 +69,8 @@ pub fn get_ffg_case_01_test_definition() -> ForkChoiceTestDefinition {
// |
// 3 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(2),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(1),
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
@@ -88,9 +85,8 @@ pub fn get_ffg_case_01_test_definition() -> ForkChoiceTestDefinition {
// |
// 3 <- start + head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(3),
finalized_epoch: Epoch::new(1),
justified_checkpoint: get_checkpoint(2),
finalized_checkpoint: get_checkpoint(1),
justified_state_balances: balances,
expected_head: get_hash(3),
});
@@ -98,9 +94,8 @@ pub fn get_ffg_case_01_test_definition() -> ForkChoiceTestDefinition {
// END OF TESTS
ForkChoiceTestDefinition {
finalized_block_slot: Slot::new(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
finalized_root: get_hash(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
operations: ops,
}
}
@@ -111,9 +106,8 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition {
// Ensure that the head starts at the finalized block.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(0),
});
@@ -137,36 +131,48 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition {
slot: Slot::new(1),
root: get_hash(1),
parent_root: get_hash(0),
justified_epoch: Epoch::new(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(2),
root: get_hash(3),
parent_root: get_hash(1),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(1),
},
finalized_checkpoint: get_checkpoint(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(3),
root: get_hash(5),
parent_root: get_hash(3),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(1),
},
finalized_checkpoint: get_checkpoint(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(4),
root: get_hash(7),
parent_root: get_hash(5),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(1),
},
finalized_checkpoint: get_checkpoint(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(4),
slot: Slot::new(5),
root: get_hash(9),
parent_root: get_hash(7),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(3),
},
finalized_checkpoint: get_checkpoint(0),
});
// Right branch
@@ -174,36 +180,42 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition {
slot: Slot::new(1),
root: get_hash(2),
parent_root: get_hash(0),
justified_epoch: Epoch::new(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(2),
root: get_hash(4),
parent_root: get_hash(2),
justified_epoch: Epoch::new(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(3),
root: get_hash(6),
parent_root: get_hash(4),
justified_epoch: Epoch::new(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(4),
root: get_hash(8),
parent_root: get_hash(6),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(2),
},
finalized_checkpoint: get_checkpoint(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(4),
slot: Slot::new(5),
root: get_hash(10),
parent_root: get_hash(8),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(4),
},
finalized_checkpoint: get_checkpoint(0),
});
// Ensure that if we start at 0 we find 10 (just: 0, fin: 0).
@@ -220,25 +232,28 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition {
// | |
// 9 10 <-- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above, but with justified epoch 2.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(4),
},
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above, but with justified epoch 3 (should be invalid).
ops.push(Operation::InvalidFindHead {
justified_epoch: Epoch::new(3),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(3),
root: get_hash(6),
},
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
});
@@ -275,25 +290,28 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition {
// | |
// head -> 9 10
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Save as above but justified epoch 2.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(3),
},
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Save as above but justified epoch 3 (should fail).
ops.push(Operation::InvalidFindHead {
justified_epoch: Epoch::new(3),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(3),
root: get_hash(5),
},
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
});
@@ -330,25 +348,28 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition {
// | |
// 9 10 <-- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above but justified epoch 2.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(4),
},
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above but justified epoch 3 (should fail).
ops.push(Operation::InvalidFindHead {
justified_epoch: Epoch::new(3),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(3),
root: get_hash(6),
},
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
});
@@ -366,25 +387,31 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition {
// | |
// head -> 9 10
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(1),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(0),
root: get_hash(1),
},
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Same as above but justified epoch 2.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(1),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(3),
},
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Same as above but justified epoch 3 (should fail).
ops.push(Operation::InvalidFindHead {
justified_epoch: Epoch::new(3),
justified_root: get_hash(1),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(3),
root: get_hash(5),
},
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
});
@@ -402,34 +429,36 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition {
// | |
// 9 10 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(2),
finalized_epoch: Epoch::new(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above but justified epoch 2.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(2),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(4),
},
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above but justified epoch 3 (should fail).
ops.push(Operation::InvalidFindHead {
justified_epoch: Epoch::new(3),
justified_root: get_hash(2),
finalized_epoch: Epoch::new(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(3),
root: get_hash(6),
},
finalized_checkpoint: get_checkpoint(0),
justified_state_balances: balances,
});
// END OF TESTS
ForkChoiceTestDefinition {
finalized_block_slot: Slot::new(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
finalized_root: get_hash(0),
justified_checkpoint: get_checkpoint(0),
finalized_checkpoint: get_checkpoint(0),
operations: ops,
}
}

View File

@@ -6,9 +6,14 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
let operations = vec![
// Check that the head is the finalized block.
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
justified_state_balances: balances.clone(),
expected_head: Hash256::zero(),
},
@@ -18,11 +23,17 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// /
// 2
Operation::ProcessBlock {
slot: Slot::new(0),
slot: Slot::new(1),
root: get_hash(2),
parent_root: get_hash(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
parent_root: Hash256::zero(),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
},
// Ensure the head is 2
//
@@ -30,9 +41,14 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// /
// 2 <- head
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
},
@@ -42,11 +58,17 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// / \
// 2 1
Operation::ProcessBlock {
slot: Slot::new(0),
slot: Slot::new(1),
root: get_hash(1),
parent_root: get_hash(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
},
// Ensure the head is still 2
//
@@ -54,9 +76,14 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// / \
// head-> 2 1
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
},
@@ -68,11 +95,17 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 3
Operation::ProcessBlock {
slot: Slot::new(0),
slot: Slot::new(2),
root: get_hash(3),
parent_root: get_hash(1),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
},
// Ensure 2 is still the head
//
@@ -82,9 +115,14 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 3
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
},
@@ -96,11 +134,17 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// | |
// 4 3
Operation::ProcessBlock {
slot: Slot::new(0),
slot: Slot::new(2),
root: get_hash(4),
parent_root: get_hash(2),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
},
// Ensure the head is 4.
//
@@ -110,9 +154,14 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// | |
// head-> 4 3
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(4),
},
@@ -126,11 +175,14 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 5 <- justified epoch = 2
Operation::ProcessBlock {
slot: Slot::new(0),
slot: Slot::new(3),
root: get_hash(5),
parent_root: get_hash(4),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(1),
justified_checkpoint: get_checkpoint(2),
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
},
// Ensure the head is still 4 whilst the justified epoch is 0.
//
@@ -142,9 +194,14 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 5
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(4),
},
@@ -158,9 +215,14 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 5 <- starting from 5 with justified epoch 0 should error.
Operation::InvalidFindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
justified_state_balances: balances.clone(),
},
// Set the justified epoch to 2 and the start block to 5 and ensure 5 is the head.
@@ -173,9 +235,11 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 5 <- head
Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(1),
justified_checkpoint: get_checkpoint(2),
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(5),
},
@@ -191,11 +255,14 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 6
Operation::ProcessBlock {
slot: Slot::new(0),
slot: Slot::new(4),
root: get_hash(6),
parent_root: get_hash(5),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(1),
justified_checkpoint: get_checkpoint(2),
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
},
// Ensure 6 is the head
//
@@ -209,9 +276,11 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 6 <- head
Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(1),
justified_checkpoint: get_checkpoint(2),
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
justified_state_balances: balances,
expected_head: get_hash(6),
},
@@ -219,9 +288,14 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
ForkChoiceTestDefinition {
finalized_block_slot: Slot::new(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
finalized_root: get_hash(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: Hash256::zero(),
},
operations,
}
}

View File

@@ -6,9 +6,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// Ensure that the head starts at the finalized block.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(0),
});
@@ -19,11 +24,17 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// /
// 2
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
slot: Slot::new(1),
root: get_hash(2),
parent_root: get_hash(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
});
// Ensure that the head is 2
@@ -32,9 +43,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// /
// head-> 2
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
@@ -46,11 +62,17 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// / \
// 2 1
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
slot: Slot::new(1),
root: get_hash(1),
parent_root: get_hash(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
});
// Ensure that the head is still 2
@@ -59,9 +81,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// / \
// head-> 2 1
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
@@ -77,15 +104,20 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
target_epoch: Epoch::new(2),
});
// Ensure that the head is now 1, beacuse 1 has a vote.
// Ensure that the head is now 1, because 1 has a vote.
//
// 0
// / \
// 2 1 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(1),
});
@@ -107,9 +139,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// / \
// head-> 2 1
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
@@ -122,11 +159,17 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 3
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
slot: Slot::new(2),
root: get_hash(3),
parent_root: get_hash(1),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
});
// Ensure that the head is still 2
@@ -137,9 +180,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 3
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
@@ -165,9 +213,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 3
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
@@ -194,9 +247,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 3 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(3),
});
@@ -211,11 +269,17 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 4
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
slot: Slot::new(3),
root: get_hash(4),
parent_root: get_hash(3),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
});
// Ensure that the head is now 4
@@ -228,9 +292,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// 4 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(4),
});
@@ -247,11 +316,17 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// /
// 5 <- justified epoch = 2
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
slot: Slot::new(4),
root: get_hash(5),
parent_root: get_hash(4),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(1),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(1),
},
});
// Ensure that 5 is filtered out and the head stays at 4.
@@ -266,9 +341,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// /
// 5
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(4),
});
@@ -288,8 +368,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
slot: Slot::new(0),
root: get_hash(6),
parent_root: get_hash(4),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
});
// Move both votes to 5.
@@ -336,22 +422,40 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
slot: Slot::new(0),
root: get_hash(7),
parent_root: get_hash(5),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(8),
parent_root: get_hash(7),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(9),
parent_root: get_hash(8),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
});
// Ensure that 6 is the head, even though 5 has all the votes. This is testing to ensure
@@ -373,9 +477,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// /
// 9
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(6),
});
@@ -401,9 +510,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// /
// head-> 9
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
@@ -460,15 +574,26 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
slot: Slot::new(0),
root: get_hash(10),
parent_root: get_hash(8),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
});
// Double-check the head is still 9 (no diagram this time)
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
@@ -522,9 +647,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// / \
// 9 10 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
@@ -542,9 +672,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// / \
// head-> 9 10
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
@@ -562,9 +697,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// / \
// 9 10 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
@@ -583,9 +723,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// / \
// head-> 9 10
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
@@ -599,9 +744,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// Run find-head, ensure the no-op prune didn't change the head.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
@@ -632,9 +782,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// Run find-head, ensure the prune didn't change the head.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
@@ -654,8 +809,14 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
slot: Slot::new(0),
root: get_hash(11),
parent_root: get_hash(9),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
});
// Ensure the head is now 11
@@ -670,18 +831,28 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
// |
// head-> 11
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(2),
root: get_hash(5),
},
justified_state_balances: balances,
expected_head: get_hash(11),
});
ForkChoiceTestDefinition {
finalized_block_slot: Slot::new(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
finalized_root: get_hash(0),
justified_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
finalized_checkpoint: Checkpoint {
epoch: Epoch::new(1),
root: get_hash(0),
},
operations: ops,
}
}

View File

@@ -8,5 +8,7 @@ pub use crate::proto_array_fork_choice::{Block, ExecutionStatus, ProtoArrayForkC
pub use error::Error;
pub mod core {
pub use super::proto_array::ProtoArray;
pub use super::proto_array::{ProposerBoost, ProtoArray, ProtoNode};
pub use super::proto_array_fork_choice::VoteTracker;
pub use super::ssz_container::SszContainer;
}

View File

@@ -1,13 +1,16 @@
use crate::error::InvalidBestNodeInfo;
use crate::{error::Error, Block, ExecutionStatus};
use serde_derive::{Deserialize, Serialize};
use ssz::four_byte_option_impl;
use ssz::Encode;
use ssz_derive::{Decode, Encode};
use std::collections::HashMap;
use types::{AttestationShufflingId, Epoch, Hash256, Slot};
use types::{AttestationShufflingId, ChainSpec, Checkpoint, Epoch, EthSpec, Hash256, Slot};
// Define a "legacy" implementation of `Option<usize>` which uses four bytes for encoding the union
// selector.
four_byte_option_impl!(four_byte_option_usize, usize);
four_byte_option_impl!(four_byte_option_checkpoint, Checkpoint);
#[derive(Clone, PartialEq, Debug, Encode, Decode, Serialize, Deserialize)]
pub struct ProtoNode {
@@ -28,59 +31,31 @@ pub struct ProtoNode {
pub root: Hash256,
#[ssz(with = "four_byte_option_usize")]
pub parent: Option<usize>,
pub justified_epoch: Epoch,
pub finalized_epoch: Epoch,
weight: u64,
#[ssz(with = "four_byte_option_checkpoint")]
pub justified_checkpoint: Option<Checkpoint>,
#[ssz(with = "four_byte_option_checkpoint")]
pub finalized_checkpoint: Option<Checkpoint>,
pub weight: u64,
#[ssz(with = "four_byte_option_usize")]
best_child: Option<usize>,
pub best_child: Option<usize>,
#[ssz(with = "four_byte_option_usize")]
best_descendant: Option<usize>,
pub best_descendant: Option<usize>,
/// Indicates if an execution node has marked this block as valid. Also contains the execution
/// block hash.
pub execution_status: ExecutionStatus,
}
/// Only used for SSZ deserialization of the persisted fork choice during the database migration
/// from schema 4 to schema 5.
#[derive(Encode, Decode)]
pub struct LegacyProtoNode {
pub slot: Slot,
pub state_root: Hash256,
pub target_root: Hash256,
pub current_epoch_shuffling_id: AttestationShufflingId,
pub next_epoch_shuffling_id: AttestationShufflingId,
#[derive(PartialEq, Debug, Encode, Decode, Serialize, Deserialize, Copy, Clone)]
pub struct ProposerBoost {
pub root: Hash256,
#[ssz(with = "four_byte_option_usize")]
pub parent: Option<usize>,
pub justified_epoch: Epoch,
pub finalized_epoch: Epoch,
weight: u64,
#[ssz(with = "four_byte_option_usize")]
best_child: Option<usize>,
#[ssz(with = "four_byte_option_usize")]
best_descendant: Option<usize>,
pub score: u64,
}
impl Into<ProtoNode> for LegacyProtoNode {
fn into(self) -> ProtoNode {
ProtoNode {
slot: self.slot,
state_root: self.state_root,
target_root: self.target_root,
current_epoch_shuffling_id: self.current_epoch_shuffling_id,
next_epoch_shuffling_id: self.next_epoch_shuffling_id,
root: self.root,
parent: self.parent,
justified_epoch: self.justified_epoch,
finalized_epoch: self.finalized_epoch,
weight: self.weight,
best_child: self.best_child,
best_descendant: self.best_descendant,
// We set the following execution value as if the block is a pre-merge-fork block. This
// is safe as long as we never import a merge block with the old version of proto-array.
// This will be safe since we can't actually process merge blocks until we've made this
// change to fork choice.
execution_status: ExecutionStatus::irrelevant(),
impl Default for ProposerBoost {
fn default() -> Self {
Self {
root: Hash256::zero(),
score: 0,
}
}
}
@@ -90,10 +65,11 @@ pub struct ProtoArray {
/// Do not attempt to prune the tree unless it has at least this many nodes. Small prunes
/// simply waste time.
pub prune_threshold: usize,
pub justified_epoch: Epoch,
pub finalized_epoch: Epoch,
pub justified_checkpoint: Checkpoint,
pub finalized_checkpoint: Checkpoint,
pub nodes: Vec<ProtoNode>,
pub indices: HashMap<Hash256, usize>,
pub previous_proposer_boost: ProposerBoost,
}
impl ProtoArray {
@@ -110,11 +86,14 @@ impl ProtoArray {
/// - Compare the current node with the parents best-child, updating it if the current node
/// should become the best child.
/// - If required, update the parents best-descendant with the current node or its best-descendant.
pub fn apply_score_changes(
pub fn apply_score_changes<E: EthSpec>(
&mut self,
mut deltas: Vec<i64>,
justified_epoch: Epoch,
finalized_epoch: Epoch,
justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
new_balances: &[u64],
proposer_boost_root: Hash256,
spec: &ChainSpec,
) -> Result<(), Error> {
if deltas.len() != self.indices.len() {
return Err(Error::InvalidDeltaLen {
@@ -123,11 +102,16 @@ impl ProtoArray {
});
}
if justified_epoch != self.justified_epoch || finalized_epoch != self.finalized_epoch {
self.justified_epoch = justified_epoch;
self.finalized_epoch = finalized_epoch;
if justified_checkpoint != self.justified_checkpoint
|| finalized_checkpoint != self.finalized_checkpoint
{
self.justified_checkpoint = justified_checkpoint;
self.finalized_checkpoint = finalized_checkpoint;
}
// Default the proposer boost score to zero.
let mut proposer_score = 0;
// Iterate backwards through all indices in `self.nodes`.
for node_index in (0..self.nodes.len()).rev() {
let node = self
@@ -142,11 +126,35 @@ impl ProtoArray {
continue;
}
let node_delta = deltas
let mut node_delta = deltas
.get(node_index)
.copied()
.ok_or(Error::InvalidNodeDelta(node_index))?;
// If we find the node for which the proposer boost was previously applied, decrease
// the delta by the previous score amount.
if self.previous_proposer_boost.root != Hash256::zero()
&& self.previous_proposer_boost.root == node.root
{
node_delta = node_delta
.checked_sub(self.previous_proposer_boost.score as i64)
.ok_or(Error::DeltaOverflow(node_index))?;
}
// If we find the node matching the current proposer boost root, increase
// the delta by the new score amount.
//
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/fork-choice.md#get_latest_attesting_balance
if let Some(proposer_score_boost) = spec.proposer_score_boost {
if proposer_boost_root != Hash256::zero() && proposer_boost_root == node.root {
proposer_score =
calculate_proposer_boost::<E>(new_balances, proposer_score_boost)
.ok_or(Error::ProposerBoostOverflow(node_index))?;
node_delta = node_delta
.checked_add(proposer_score as i64)
.ok_or(Error::DeltaOverflow(node_index))?;
}
}
// Apply the delta to the node.
if node_delta < 0 {
// Note: I am conflicted about whether to use `saturating_sub` or `checked_sub`
@@ -180,6 +188,12 @@ impl ProtoArray {
}
}
// After applying all deltas, update the `previous_proposer_boost`.
self.previous_proposer_boost = ProposerBoost {
root: proposer_boost_root,
score: proposer_score,
};
// A second time, iterate backwards through all indices in `self.nodes`.
//
// We _must_ perform these functions separate from the weight-updating loop above to ensure
@@ -221,8 +235,8 @@ impl ProtoArray {
parent: block
.parent_root
.and_then(|parent| self.indices.get(&parent).copied()),
justified_epoch: block.justified_epoch,
finalized_epoch: block.finalized_epoch,
justified_checkpoint: Some(block.justified_checkpoint),
finalized_checkpoint: Some(block.finalized_checkpoint),
weight: 0,
best_child: None,
best_descendant: None,
@@ -315,14 +329,14 @@ impl ProtoArray {
// Perform a sanity check that the node is indeed valid to be the head.
if !self.node_is_viable_for_head(best_node) {
return Err(Error::InvalidBestNode {
return Err(Error::InvalidBestNode(Box::new(InvalidBestNodeInfo {
start_root: *justified_root,
justified_epoch: self.justified_epoch,
finalized_epoch: self.finalized_epoch,
justified_checkpoint: self.justified_checkpoint,
finalized_checkpoint: self.finalized_checkpoint,
head_root: justified_node.root,
head_justified_epoch: justified_node.justified_epoch,
head_finalized_epoch: justified_node.finalized_epoch,
});
head_justified_checkpoint: justified_node.justified_checkpoint,
head_finalized_checkpoint: justified_node.finalized_checkpoint,
})));
}
Ok(best_node.root)
@@ -523,9 +537,16 @@ impl ProtoArray {
/// Any node that has a different finalized or justified epoch should not be viable for the
/// head.
fn node_is_viable_for_head(&self, node: &ProtoNode) -> bool {
(node.justified_epoch == self.justified_epoch || self.justified_epoch == Epoch::new(0))
&& (node.finalized_epoch == self.finalized_epoch
|| self.finalized_epoch == Epoch::new(0))
if let (Some(node_justified_checkpoint), Some(node_finalized_checkpoint)) =
(node.justified_checkpoint, node.finalized_checkpoint)
{
(node_justified_checkpoint == self.justified_checkpoint
|| self.justified_checkpoint.epoch == Epoch::new(0))
&& (node_finalized_checkpoint == self.finalized_checkpoint
|| self.finalized_checkpoint.epoch == Epoch::new(0))
} else {
false
}
}
/// Return a reverse iterator over the nodes which comprise the chain ending at `block_root`.
@@ -549,6 +570,38 @@ impl ProtoArray {
}
}
/// A helper method to calculate the proposer boost based on the given `validator_balances`.
/// This does *not* do any verification about whether a boost should or should not be applied.
/// The `validator_balances` array used here is assumed to be structured like the one stored in
/// the `BalancesCache`, where *effective* balances are stored and inactive balances are defaulted
/// to zero.
///
/// Returns `None` if there is an overflow or underflow when calculating the score.
///
/// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/fork-choice.md#get_latest_attesting_balance
fn calculate_proposer_boost<E: EthSpec>(
validator_balances: &[u64],
proposer_score_boost: u64,
) -> Option<u64> {
let mut total_balance: u64 = 0;
let mut num_validators: u64 = 0;
for &balance in validator_balances {
// We need to filter zero balances here to get an accurate active validator count.
// This is because we default inactive validator balances to zero when creating
// this balances array.
if balance != 0 {
total_balance = total_balance.checked_add(balance)?;
num_validators = num_validators.checked_add(1)?;
}
}
let average_balance = total_balance.checked_div(num_validators)?;
let committee_size = num_validators.checked_div(E::slots_per_epoch())?;
let committee_weight = committee_size.checked_mul(average_balance)?;
committee_weight
.checked_mul(proposer_score_boost)?
.checked_div(100)
}
/// Reverse iterator over one path through a `ProtoArray`.
pub struct Iter<'a> {
next_node_index: Option<usize>,

View File

@@ -1,11 +1,11 @@
use crate::error::Error;
use crate::proto_array::ProtoArray;
use crate::ssz_container::{LegacySszContainer, SszContainer};
use crate::proto_array::{ProposerBoost, ProtoArray};
use crate::ssz_container::SszContainer;
use serde_derive::{Deserialize, Serialize};
use ssz::{Decode, Encode};
use ssz_derive::{Decode, Encode};
use std::collections::HashMap;
use types::{AttestationShufflingId, Epoch, Hash256, Slot};
use types::{AttestationShufflingId, ChainSpec, Checkpoint, Epoch, EthSpec, Hash256, Slot};
pub const DEFAULT_PRUNE_THRESHOLD: usize = 256;
@@ -63,8 +63,8 @@ pub struct Block {
pub target_root: Hash256,
pub current_epoch_shuffling_id: AttestationShufflingId,
pub next_epoch_shuffling_id: AttestationShufflingId,
pub justified_epoch: Epoch,
pub finalized_epoch: Epoch,
pub justified_checkpoint: Checkpoint,
pub finalized_checkpoint: Checkpoint,
/// Indicates if an execution node has marked this block as valid. Also contains the execution
/// block hash.
pub execution_status: ExecutionStatus,
@@ -109,33 +109,33 @@ impl ProtoArrayForkChoice {
pub fn new(
finalized_block_slot: Slot,
finalized_block_state_root: Hash256,
justified_epoch: Epoch,
finalized_epoch: Epoch,
finalized_root: Hash256,
justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
current_epoch_shuffling_id: AttestationShufflingId,
next_epoch_shuffling_id: AttestationShufflingId,
execution_status: ExecutionStatus,
) -> Result<Self, String> {
let mut proto_array = ProtoArray {
prune_threshold: DEFAULT_PRUNE_THRESHOLD,
justified_epoch,
finalized_epoch,
justified_checkpoint,
finalized_checkpoint,
nodes: Vec::with_capacity(1),
indices: HashMap::with_capacity(1),
previous_proposer_boost: ProposerBoost::default(),
};
let block = Block {
slot: finalized_block_slot,
root: finalized_root,
root: finalized_checkpoint.root,
parent_root: None,
state_root: finalized_block_state_root,
// We are using the finalized_root as the target_root, since it always lies on an
// epoch boundary.
target_root: finalized_root,
target_root: finalized_checkpoint.root,
current_epoch_shuffling_id,
next_epoch_shuffling_id,
justified_epoch,
finalized_epoch,
justified_checkpoint,
finalized_checkpoint,
execution_status,
};
@@ -176,12 +176,13 @@ impl ProtoArrayForkChoice {
.map_err(|e| format!("process_block_error: {:?}", e))
}
pub fn find_head(
pub fn find_head<E: EthSpec>(
&mut self,
justified_epoch: Epoch,
justified_root: Hash256,
finalized_epoch: Epoch,
justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
justified_state_balances: &[u64],
proposer_boost_root: Hash256,
spec: &ChainSpec,
) -> Result<Hash256, String> {
let old_balances = &mut self.balances;
@@ -196,13 +197,20 @@ impl ProtoArrayForkChoice {
.map_err(|e| format!("find_head compute_deltas failed: {:?}", e))?;
self.proto_array
.apply_score_changes(deltas, justified_epoch, finalized_epoch)
.apply_score_changes::<E>(
deltas,
justified_checkpoint,
finalized_checkpoint,
new_balances,
proposer_boost_root,
spec,
)
.map_err(|e| format!("find_head apply_score_changes failed: {:?}", e))?;
*old_balances = new_balances.to_vec();
self.proto_array
.find_head(&justified_root)
.find_head(&justified_checkpoint.root)
.map_err(|e| format!("find_head failed: {:?}", e))
}
@@ -236,18 +244,27 @@ impl ProtoArrayForkChoice {
.and_then(|i| self.proto_array.nodes.get(i))
.map(|parent| parent.root);
Some(Block {
slot: block.slot,
root: block.root,
parent_root,
state_root: block.state_root,
target_root: block.target_root,
current_epoch_shuffling_id: block.current_epoch_shuffling_id.clone(),
next_epoch_shuffling_id: block.next_epoch_shuffling_id.clone(),
justified_epoch: block.justified_epoch,
finalized_epoch: block.finalized_epoch,
execution_status: block.execution_status,
})
// If a node does not have a `finalized_checkpoint` or `justified_checkpoint` populated,
// it means it is not a descendant of the finalized checkpoint, so it is valid to return
// `None` here.
if let (Some(justified_checkpoint), Some(finalized_checkpoint)) =
(block.justified_checkpoint, block.finalized_checkpoint)
{
Some(Block {
slot: block.slot,
root: block.root,
parent_root,
state_root: block.state_root,
target_root: block.target_root,
current_epoch_shuffling_id: block.current_epoch_shuffling_id.clone(),
next_epoch_shuffling_id: block.next_epoch_shuffling_id.clone(),
justified_checkpoint,
finalized_checkpoint,
execution_status: block.execution_status,
})
} else {
None
}
}
/// Returns `true` if the `descendant_root` has an ancestor with `ancestor_root`. Always
@@ -295,28 +312,19 @@ impl ProtoArrayForkChoice {
.map_err(|e| format!("Failed to decode ProtoArrayForkChoice: {:?}", e))
}
/// Only used for SSZ deserialization of the persisted fork choice during the database migration
/// from schema 5 to schema 6.
pub fn from_bytes_legacy(bytes: &[u8]) -> Result<Self, String> {
LegacySszContainer::from_ssz_bytes(bytes)
.map(|legacy_container| {
let container: SszContainer = legacy_container.into();
container.into()
})
.map_err(|e| {
format!(
"Failed to decode ProtoArrayForkChoice during schema migration: {:?}",
e
)
})
}
/// Returns a read-lock to core `ProtoArray` struct.
///
/// Should only be used when encoding/decoding during troubleshooting.
pub fn core_proto_array(&self) -> &ProtoArray {
&self.proto_array
}
/// Returns a mutable reference to the core `ProtoArray` struct.
///
/// Should only be used during database schema migrations.
pub fn core_proto_array_mut(&mut self) -> &mut ProtoArray {
&mut self.proto_array
}
}
/// Returns a list of `deltas`, where there is one delta for each of the indices in
@@ -412,12 +420,16 @@ mod test_compute_deltas {
AttestationShufflingId::from_components(Epoch::new(0), Hash256::zero());
let execution_status = ExecutionStatus::irrelevant();
let genesis_checkpoint = Checkpoint {
epoch: genesis_epoch,
root: finalized_root,
};
let mut fc = ProtoArrayForkChoice::new(
genesis_slot,
state_root,
genesis_epoch,
genesis_epoch,
finalized_root,
genesis_checkpoint,
genesis_checkpoint,
junk_shuffling_id.clone(),
junk_shuffling_id.clone(),
execution_status,
@@ -434,8 +446,8 @@ mod test_compute_deltas {
target_root: finalized_root,
current_epoch_shuffling_id: junk_shuffling_id.clone(),
next_epoch_shuffling_id: junk_shuffling_id.clone(),
justified_epoch: genesis_epoch,
finalized_epoch: genesis_epoch,
justified_checkpoint: genesis_checkpoint,
finalized_checkpoint: genesis_checkpoint,
execution_status,
})
.unwrap();
@@ -450,8 +462,8 @@ mod test_compute_deltas {
target_root: finalized_root,
current_epoch_shuffling_id: junk_shuffling_id.clone(),
next_epoch_shuffling_id: junk_shuffling_id,
justified_epoch: genesis_epoch,
finalized_epoch: genesis_epoch,
justified_checkpoint: genesis_checkpoint,
finalized_checkpoint: genesis_checkpoint,
execution_status,
})
.unwrap();

View File

@@ -1,50 +1,27 @@
use crate::proto_array::LegacyProtoNode;
use crate::proto_array::ProposerBoost;
use crate::{
proto_array::{ProtoArray, ProtoNode},
proto_array_fork_choice::{ElasticList, ProtoArrayForkChoice, VoteTracker},
};
use ssz::{four_byte_option_impl, Encode};
use ssz_derive::{Decode, Encode};
use std::collections::HashMap;
use types::{Epoch, Hash256};
use types::{Checkpoint, Hash256};
// Define a "legacy" implementation of `Option<usize>` which uses four bytes for encoding the union
// selector.
four_byte_option_impl!(four_byte_option_checkpoint, Checkpoint);
#[derive(Encode, Decode)]
pub struct SszContainer {
votes: Vec<VoteTracker>,
balances: Vec<u64>,
prune_threshold: usize,
justified_epoch: Epoch,
finalized_epoch: Epoch,
nodes: Vec<ProtoNode>,
indices: Vec<(Hash256, usize)>,
}
/// Only used for SSZ deserialization of the persisted fork choice during the database migration
/// from schema 5 to schema 6.
#[derive(Encode, Decode)]
pub struct LegacySszContainer {
votes: Vec<VoteTracker>,
balances: Vec<u64>,
prune_threshold: usize,
justified_epoch: Epoch,
finalized_epoch: Epoch,
nodes: Vec<LegacyProtoNode>,
indices: Vec<(Hash256, usize)>,
}
impl Into<SszContainer> for LegacySszContainer {
fn into(self) -> SszContainer {
let nodes = self.nodes.into_iter().map(Into::into).collect();
SszContainer {
votes: self.votes,
balances: self.balances,
prune_threshold: self.prune_threshold,
justified_epoch: self.justified_epoch,
finalized_epoch: self.finalized_epoch,
nodes,
indices: self.indices,
}
}
pub votes: Vec<VoteTracker>,
pub balances: Vec<u64>,
pub prune_threshold: usize,
pub justified_checkpoint: Checkpoint,
pub finalized_checkpoint: Checkpoint,
pub nodes: Vec<ProtoNode>,
pub indices: Vec<(Hash256, usize)>,
pub previous_proposer_boost: ProposerBoost,
}
impl From<&ProtoArrayForkChoice> for SszContainer {
@@ -55,10 +32,11 @@ impl From<&ProtoArrayForkChoice> for SszContainer {
votes: from.votes.0.clone(),
balances: from.balances.clone(),
prune_threshold: proto_array.prune_threshold,
justified_epoch: proto_array.justified_epoch,
finalized_epoch: proto_array.finalized_epoch,
justified_checkpoint: proto_array.justified_checkpoint,
finalized_checkpoint: proto_array.finalized_checkpoint,
nodes: proto_array.nodes.clone(),
indices: proto_array.indices.iter().map(|(k, v)| (*k, *v)).collect(),
previous_proposer_boost: proto_array.previous_proposer_boost,
}
}
}
@@ -67,10 +45,11 @@ impl From<SszContainer> for ProtoArrayForkChoice {
fn from(from: SszContainer) -> Self {
let proto_array = ProtoArray {
prune_threshold: from.prune_threshold,
justified_epoch: from.justified_epoch,
finalized_epoch: from.finalized_epoch,
justified_checkpoint: from.justified_checkpoint,
finalized_checkpoint: from.finalized_checkpoint,
nodes: from.nodes,
indices: from.indices.into_iter().collect::<HashMap<_, _>>(),
previous_proposer_boost: from.previous_proposer_boost,
};
Self {

View File

@@ -43,7 +43,7 @@ regex = "1.3.9"
lazy_static = "1.4.0"
parking_lot = "0.11.1"
itertools = "0.10.0"
superstruct = "0.2.0"
superstruct = "0.3.0"
[dev-dependencies]
criterion = "0.3.3"

View File

@@ -101,6 +101,7 @@ pub struct ChainSpec {
* Fork choice
*/
pub safe_slots_to_update_justified: u64,
pub proposer_score_boost: Option<u64>,
/*
* Eth1
@@ -489,6 +490,7 @@ impl ChainSpec {
* Fork choice
*/
safe_slots_to_update_justified: 8,
proposer_score_boost: None,
/*
* Eth1
@@ -657,6 +659,8 @@ pub struct Config {
#[serde(with = "eth2_serde_utils::quoted_u64")]
churn_limit_quotient: u64,
proposer_score_boost: Option<MaybeQuoted<u64>>,
#[serde(with = "eth2_serde_utils::quoted_u64")]
deposit_chain_id: u64,
#[serde(with = "eth2_serde_utils::quoted_u64")]
@@ -746,6 +750,8 @@ impl Config {
churn_limit_quotient: spec.churn_limit_quotient,
min_per_epoch_churn_limit: spec.min_per_epoch_churn_limit,
proposer_score_boost: spec.proposer_score_boost.map(|value| MaybeQuoted { value }),
deposit_chain_id: spec.deposit_chain_id,
deposit_network_id: spec.deposit_network_id,
deposit_contract_address: spec.deposit_contract_address,
@@ -784,6 +790,7 @@ impl Config {
ejection_balance,
min_per_epoch_churn_limit,
churn_limit_quotient,
proposer_score_boost,
deposit_chain_id,
deposit_network_id,
deposit_contract_address,
@@ -812,6 +819,7 @@ impl Config {
ejection_balance,
min_per_epoch_churn_limit,
churn_limit_quotient,
proposer_score_boost: proposer_score_boost.map(|q| q.value),
deposit_chain_id,
deposit_network_id,
deposit_contract_address,

View File

@@ -19,3 +19,6 @@ pub mod altair {
pub const NUM_FLAG_INDICES: usize = 3;
}
pub mod merge {
pub const INTERVALS_PER_SLOT: u64 = 3;
}