2022-02-28 22:07:48 +00:00
|
|
|
mod execution_status;
|
2020-01-29 04:05:00 +00:00
|
|
|
mod ffg_updates;
|
|
|
|
mod no_votes;
|
|
|
|
mod votes;
|
|
|
|
|
2021-10-07 11:24:57 +00:00
|
|
|
use crate::proto_array_fork_choice::{Block, ExecutionStatus, ProtoArrayForkChoice};
|
2022-03-03 02:10:57 +00:00
|
|
|
use crate::InvalidationOperation;
|
2020-01-29 04:05:00 +00:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2022-07-28 09:43:41 +00:00
|
|
|
use std::collections::BTreeSet;
|
2022-02-28 22:07:48 +00:00
|
|
|
use types::{
|
|
|
|
AttestationShufflingId, Checkpoint, Epoch, EthSpec, ExecutionBlockHash, Hash256,
|
|
|
|
MainnetEthSpec, Slot,
|
|
|
|
};
|
2020-01-29 04:05:00 +00:00
|
|
|
|
2022-02-28 22:07:48 +00:00
|
|
|
pub use execution_status::*;
|
2020-01-29 04:05:00 +00:00
|
|
|
pub use ffg_updates::*;
|
|
|
|
pub use no_votes::*;
|
|
|
|
pub use votes::*;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub enum Operation {
|
|
|
|
FindHead {
|
2021-12-13 20:43:22 +00:00
|
|
|
justified_checkpoint: Checkpoint,
|
|
|
|
finalized_checkpoint: Checkpoint,
|
2020-01-29 04:05:00 +00:00
|
|
|
justified_state_balances: Vec<u64>,
|
|
|
|
expected_head: Hash256,
|
|
|
|
},
|
2022-02-28 22:07:48 +00:00
|
|
|
ProposerBoostFindHead {
|
|
|
|
justified_checkpoint: Checkpoint,
|
|
|
|
finalized_checkpoint: Checkpoint,
|
|
|
|
justified_state_balances: Vec<u64>,
|
|
|
|
expected_head: Hash256,
|
|
|
|
proposer_boost_root: Hash256,
|
|
|
|
},
|
2020-01-29 04:05:00 +00:00
|
|
|
InvalidFindHead {
|
2021-12-13 20:43:22 +00:00
|
|
|
justified_checkpoint: Checkpoint,
|
|
|
|
finalized_checkpoint: Checkpoint,
|
2020-01-29 04:05:00 +00:00
|
|
|
justified_state_balances: Vec<u64>,
|
|
|
|
},
|
|
|
|
ProcessBlock {
|
|
|
|
slot: Slot,
|
|
|
|
root: Hash256,
|
|
|
|
parent_root: Hash256,
|
2021-12-13 20:43:22 +00:00
|
|
|
justified_checkpoint: Checkpoint,
|
|
|
|
finalized_checkpoint: Checkpoint,
|
2020-01-29 04:05:00 +00:00
|
|
|
},
|
|
|
|
ProcessAttestation {
|
|
|
|
validator_index: usize,
|
|
|
|
block_root: Hash256,
|
|
|
|
target_epoch: Epoch,
|
|
|
|
},
|
|
|
|
Prune {
|
|
|
|
finalized_root: Hash256,
|
|
|
|
prune_threshold: usize,
|
|
|
|
expected_len: usize,
|
|
|
|
},
|
2022-02-28 22:07:48 +00:00
|
|
|
InvalidatePayload {
|
|
|
|
head_block_root: Hash256,
|
|
|
|
latest_valid_ancestor_root: Option<ExecutionBlockHash>,
|
|
|
|
},
|
|
|
|
AssertWeight {
|
|
|
|
block_root: Hash256,
|
|
|
|
weight: u64,
|
|
|
|
},
|
2020-01-29 04:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct ForkChoiceTestDefinition {
|
|
|
|
pub finalized_block_slot: Slot,
|
2021-12-13 20:43:22 +00:00
|
|
|
pub justified_checkpoint: Checkpoint,
|
|
|
|
pub finalized_checkpoint: Checkpoint,
|
2020-01-29 04:05:00 +00:00
|
|
|
pub operations: Vec<Operation>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ForkChoiceTestDefinition {
|
|
|
|
pub fn run(self) {
|
2022-02-28 22:07:48 +00:00
|
|
|
let mut spec = MainnetEthSpec::default_spec();
|
|
|
|
spec.proposer_score_boost = Some(50);
|
|
|
|
|
2021-02-15 07:17:52 +00:00
|
|
|
let junk_shuffling_id =
|
|
|
|
AttestationShufflingId::from_components(Epoch::new(0), Hash256::zero());
|
2022-07-25 23:53:26 +00:00
|
|
|
let mut fork_choice = ProtoArrayForkChoice::new::<MainnetEthSpec>(
|
2020-01-29 04:05:00 +00:00
|
|
|
self.finalized_block_slot,
|
2020-03-05 06:19:35 +00:00
|
|
|
Hash256::zero(),
|
2021-12-13 20:43:22 +00:00
|
|
|
self.justified_checkpoint,
|
|
|
|
self.finalized_checkpoint,
|
2020-09-29 03:46:54 +00:00
|
|
|
junk_shuffling_id.clone(),
|
|
|
|
junk_shuffling_id,
|
2022-04-13 03:54:42 +00:00
|
|
|
ExecutionStatus::Optimistic(ExecutionBlockHash::zero()),
|
2020-01-29 04:05:00 +00:00
|
|
|
)
|
|
|
|
.expect("should create fork choice struct");
|
2022-07-28 09:43:41 +00:00
|
|
|
let equivocating_indices = BTreeSet::new();
|
2020-01-29 04:05:00 +00:00
|
|
|
|
|
|
|
for (op_index, op) in self.operations.into_iter().enumerate() {
|
|
|
|
match op.clone() {
|
|
|
|
Operation::FindHead {
|
2021-12-13 20:43:22 +00:00
|
|
|
justified_checkpoint,
|
|
|
|
finalized_checkpoint,
|
2020-01-29 04:05:00 +00:00
|
|
|
justified_state_balances,
|
|
|
|
expected_head,
|
|
|
|
} => {
|
|
|
|
let head = fork_choice
|
2021-12-13 20:43:22 +00:00
|
|
|
.find_head::<MainnetEthSpec>(
|
|
|
|
justified_checkpoint,
|
|
|
|
finalized_checkpoint,
|
2020-01-29 04:05:00 +00:00
|
|
|
&justified_state_balances,
|
2021-12-13 20:43:22 +00:00
|
|
|
Hash256::zero(),
|
2022-07-28 09:43:41 +00:00
|
|
|
&equivocating_indices,
|
2022-07-25 23:53:26 +00:00
|
|
|
Slot::new(0),
|
2022-02-28 22:07:48 +00:00
|
|
|
&spec,
|
2020-01-29 04:05:00 +00:00
|
|
|
)
|
2021-12-13 20:43:22 +00:00
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
panic!("find_head op at index {} returned error {}", op_index, e)
|
2020-06-25 14:04:08 +00:00
|
|
|
});
|
2020-01-29 04:05:00 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
head, expected_head,
|
2022-02-28 22:07:48 +00:00
|
|
|
"Operation at index {} failed head check. Operation: {:?}",
|
|
|
|
op_index, op
|
|
|
|
);
|
|
|
|
check_bytes_round_trip(&fork_choice);
|
|
|
|
}
|
|
|
|
Operation::ProposerBoostFindHead {
|
|
|
|
justified_checkpoint,
|
|
|
|
finalized_checkpoint,
|
|
|
|
justified_state_balances,
|
|
|
|
expected_head,
|
|
|
|
proposer_boost_root,
|
|
|
|
} => {
|
|
|
|
let head = fork_choice
|
|
|
|
.find_head::<MainnetEthSpec>(
|
|
|
|
justified_checkpoint,
|
|
|
|
finalized_checkpoint,
|
|
|
|
&justified_state_balances,
|
|
|
|
proposer_boost_root,
|
2022-07-28 09:43:41 +00:00
|
|
|
&equivocating_indices,
|
2022-07-25 23:53:26 +00:00
|
|
|
Slot::new(0),
|
2022-02-28 22:07:48 +00:00
|
|
|
&spec,
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
panic!("find_head op at index {} returned error {}", op_index, e)
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
head, expected_head,
|
|
|
|
"Operation at index {} failed head check. Operation: {:?}",
|
2020-01-29 04:05:00 +00:00
|
|
|
op_index, op
|
|
|
|
);
|
|
|
|
check_bytes_round_trip(&fork_choice);
|
|
|
|
}
|
|
|
|
Operation::InvalidFindHead {
|
2021-12-13 20:43:22 +00:00
|
|
|
justified_checkpoint,
|
|
|
|
finalized_checkpoint,
|
2020-01-29 04:05:00 +00:00
|
|
|
justified_state_balances,
|
|
|
|
} => {
|
2021-12-13 20:43:22 +00:00
|
|
|
let result = fork_choice.find_head::<MainnetEthSpec>(
|
|
|
|
justified_checkpoint,
|
|
|
|
finalized_checkpoint,
|
2020-01-29 04:05:00 +00:00
|
|
|
&justified_state_balances,
|
2021-12-13 20:43:22 +00:00
|
|
|
Hash256::zero(),
|
2022-07-28 09:43:41 +00:00
|
|
|
&equivocating_indices,
|
2022-07-25 23:53:26 +00:00
|
|
|
Slot::new(0),
|
2022-02-28 22:07:48 +00:00
|
|
|
&spec,
|
2020-01-29 04:05:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
result.is_err(),
|
|
|
|
"Operation at index {} . Operation: {:?}",
|
|
|
|
op_index,
|
|
|
|
op
|
|
|
|
);
|
|
|
|
check_bytes_round_trip(&fork_choice);
|
|
|
|
}
|
|
|
|
Operation::ProcessBlock {
|
|
|
|
slot,
|
|
|
|
root,
|
|
|
|
parent_root,
|
2021-12-13 20:43:22 +00:00
|
|
|
justified_checkpoint,
|
|
|
|
finalized_checkpoint,
|
2020-01-29 04:05:00 +00:00
|
|
|
} => {
|
v0.12 fork choice update (#1229)
* Incomplete scraps
* Add progress on new fork choice impl
* Further progress
* First complete compiling version
* Remove chain reference
* Add new lmd_ghost crate
* Start integrating into beacon chain
* Update `milagro_bls` to new release (#1183)
* Update milagro_bls to new release
Signed-off-by: Kirk Baird <baird.k@outlook.com>
* Tidy up fake cryptos
Signed-off-by: Kirk Baird <baird.k@outlook.com>
* move SecretHash to bls and put plaintext back
Signed-off-by: Kirk Baird <baird.k@outlook.com>
* Update state processing for v0.12
* Fix EF test runners for v0.12
* Fix some tests
* Fix broken attestation verification test
* More test fixes
* Rough beacon chain impl working
* Remove fork_choice_2
* Remove checkpoint manager
* Half finished ssz impl
* Add missed file
* Add persistence
* Tidy, fix some compile errors
* Remove RwLock from ProtoArrayForkChoice
* Fix store-based compile errors
* Add comments, tidy
* Move function out of ForkChoice struct
* Start testing
* More testing
* Fix compile error
* Tidy beacon_chain::fork_choice
* Queue attestations from the current slot
* Allow fork choice to handle prior-to-genesis start
* Improve error granularity
* Test attestation dequeuing
* Process attestations during block
* Store target root in fork choice
* Move fork choice verification into new crate
* Update tests
* Consensus updates for v0.12 (#1228)
* Update state processing for v0.12
* Fix EF test runners for v0.12
* Fix some tests
* Fix broken attestation verification test
* More test fixes
* Fix typo found in review
* Add `Block` struct to ProtoArray
* Start fixing get_ancestor
* Add rough progress on testing
* Get fork choice tests working
* Progress with testing
* Fix partialeq impl
* Move slot clock from fc_store
* Improve testing
* Add testing for best justified
* Add clone back to SystemTimeSlotClock
* Add balances test
* Start adding balances cache again
* Wire-in balances cache
* Improve tests
* Remove commented-out tests
* Remove beacon_chain::ForkChoice
* Rename crates
* Update wider codebase to new fork_choice layout
* Move advance_slot in test harness
* Tidy ForkChoice::update_time
* Fix verification tests
* Fix compile error with iter::once
* Fix fork choice tests
* Ensure block attestations are processed
* Fix failing beacon_chain tests
* Add first invalid block check
* Add finalized block check
* Progress with testing, new store builder
* Add fixes to get_ancestor
* Fix old genesis justification test
* Fix remaining fork choice tests
* Change root iteration method
* Move on_verified_block
* Remove unused method
* Start adding attestation verification tests
* Add invalid ffg target test
* Add target epoch test
* Add queued attestation test
* Remove old fork choice verification tests
* Tidy, add test
* Move fork choice lock drop
* Rename BeaconForkChoiceStore
* Add comments, tidy BeaconForkChoiceStore
* Update metrics, rename fork_choice_store.rs
* Remove genesis_block_root from ForkChoice
* Tidy
* Update fork_choice comments
* Tidy, add comments
* Tidy, simplify ForkChoice, fix compile issue
* Tidy, removed dead file
* Increase http request timeout
* Fix failing rest_api test
* Set HTTP timeout back to 5s
* Apply fix to get_ancestor
* Address Michael's comments
* Fix typo
* Revert "Fix broken attestation verification test"
This reverts commit 722cdc903b12611de27916a57eeecfa3224f2279.
Co-authored-by: Kirk Baird <baird.k@outlook.com>
Co-authored-by: Michael Sproul <michael@sigmaprime.io>
2020-06-17 01:10:22 +00:00
|
|
|
let block = Block {
|
|
|
|
slot,
|
|
|
|
root,
|
|
|
|
parent_root: Some(parent_root),
|
|
|
|
state_root: Hash256::zero(),
|
|
|
|
target_root: Hash256::zero(),
|
2021-02-15 07:17:52 +00:00
|
|
|
current_epoch_shuffling_id: AttestationShufflingId::from_components(
|
2020-09-29 03:46:54 +00:00
|
|
|
Epoch::new(0),
|
|
|
|
Hash256::zero(),
|
|
|
|
),
|
2021-02-15 07:17:52 +00:00
|
|
|
next_epoch_shuffling_id: AttestationShufflingId::from_components(
|
2020-09-29 03:46:54 +00:00
|
|
|
Epoch::new(0),
|
|
|
|
Hash256::zero(),
|
|
|
|
),
|
2021-12-13 20:43:22 +00:00
|
|
|
justified_checkpoint,
|
|
|
|
finalized_checkpoint,
|
2022-02-28 22:07:48 +00:00
|
|
|
// All blocks are imported optimistically.
|
2022-04-13 03:54:42 +00:00
|
|
|
execution_status: ExecutionStatus::Optimistic(
|
|
|
|
ExecutionBlockHash::from_root(root),
|
|
|
|
),
|
2022-07-25 23:53:26 +00:00
|
|
|
unrealized_justified_checkpoint: None,
|
|
|
|
unrealized_finalized_checkpoint: None,
|
v0.12 fork choice update (#1229)
* Incomplete scraps
* Add progress on new fork choice impl
* Further progress
* First complete compiling version
* Remove chain reference
* Add new lmd_ghost crate
* Start integrating into beacon chain
* Update `milagro_bls` to new release (#1183)
* Update milagro_bls to new release
Signed-off-by: Kirk Baird <baird.k@outlook.com>
* Tidy up fake cryptos
Signed-off-by: Kirk Baird <baird.k@outlook.com>
* move SecretHash to bls and put plaintext back
Signed-off-by: Kirk Baird <baird.k@outlook.com>
* Update state processing for v0.12
* Fix EF test runners for v0.12
* Fix some tests
* Fix broken attestation verification test
* More test fixes
* Rough beacon chain impl working
* Remove fork_choice_2
* Remove checkpoint manager
* Half finished ssz impl
* Add missed file
* Add persistence
* Tidy, fix some compile errors
* Remove RwLock from ProtoArrayForkChoice
* Fix store-based compile errors
* Add comments, tidy
* Move function out of ForkChoice struct
* Start testing
* More testing
* Fix compile error
* Tidy beacon_chain::fork_choice
* Queue attestations from the current slot
* Allow fork choice to handle prior-to-genesis start
* Improve error granularity
* Test attestation dequeuing
* Process attestations during block
* Store target root in fork choice
* Move fork choice verification into new crate
* Update tests
* Consensus updates for v0.12 (#1228)
* Update state processing for v0.12
* Fix EF test runners for v0.12
* Fix some tests
* Fix broken attestation verification test
* More test fixes
* Fix typo found in review
* Add `Block` struct to ProtoArray
* Start fixing get_ancestor
* Add rough progress on testing
* Get fork choice tests working
* Progress with testing
* Fix partialeq impl
* Move slot clock from fc_store
* Improve testing
* Add testing for best justified
* Add clone back to SystemTimeSlotClock
* Add balances test
* Start adding balances cache again
* Wire-in balances cache
* Improve tests
* Remove commented-out tests
* Remove beacon_chain::ForkChoice
* Rename crates
* Update wider codebase to new fork_choice layout
* Move advance_slot in test harness
* Tidy ForkChoice::update_time
* Fix verification tests
* Fix compile error with iter::once
* Fix fork choice tests
* Ensure block attestations are processed
* Fix failing beacon_chain tests
* Add first invalid block check
* Add finalized block check
* Progress with testing, new store builder
* Add fixes to get_ancestor
* Fix old genesis justification test
* Fix remaining fork choice tests
* Change root iteration method
* Move on_verified_block
* Remove unused method
* Start adding attestation verification tests
* Add invalid ffg target test
* Add target epoch test
* Add queued attestation test
* Remove old fork choice verification tests
* Tidy, add test
* Move fork choice lock drop
* Rename BeaconForkChoiceStore
* Add comments, tidy BeaconForkChoiceStore
* Update metrics, rename fork_choice_store.rs
* Remove genesis_block_root from ForkChoice
* Tidy
* Update fork_choice comments
* Tidy, add comments
* Tidy, simplify ForkChoice, fix compile issue
* Tidy, removed dead file
* Increase http request timeout
* Fix failing rest_api test
* Set HTTP timeout back to 5s
* Apply fix to get_ancestor
* Address Michael's comments
* Fix typo
* Revert "Fix broken attestation verification test"
This reverts commit 722cdc903b12611de27916a57eeecfa3224f2279.
Co-authored-by: Kirk Baird <baird.k@outlook.com>
Co-authored-by: Michael Sproul <michael@sigmaprime.io>
2020-06-17 01:10:22 +00:00
|
|
|
};
|
2022-07-25 23:53:26 +00:00
|
|
|
fork_choice
|
|
|
|
.process_block::<MainnetEthSpec>(block, slot)
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
panic!(
|
|
|
|
"process_block op at index {} returned error: {:?}",
|
|
|
|
op_index, e
|
|
|
|
)
|
|
|
|
});
|
2020-01-29 04:05:00 +00:00
|
|
|
check_bytes_round_trip(&fork_choice);
|
|
|
|
}
|
|
|
|
Operation::ProcessAttestation {
|
|
|
|
validator_index,
|
|
|
|
block_root,
|
|
|
|
target_epoch,
|
|
|
|
} => {
|
|
|
|
fork_choice
|
|
|
|
.process_attestation(validator_index, block_root, target_epoch)
|
2020-06-25 14:04:08 +00:00
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
panic!(
|
|
|
|
"process_attestation op at index {} returned error",
|
|
|
|
op_index
|
|
|
|
)
|
|
|
|
});
|
2020-01-29 04:05:00 +00:00
|
|
|
check_bytes_round_trip(&fork_choice);
|
|
|
|
}
|
|
|
|
Operation::Prune {
|
|
|
|
finalized_root,
|
|
|
|
prune_threshold,
|
|
|
|
expected_len,
|
|
|
|
} => {
|
|
|
|
fork_choice.set_prune_threshold(prune_threshold);
|
|
|
|
fork_choice
|
|
|
|
.maybe_prune(finalized_root)
|
|
|
|
.expect("update_finalized_root op at index {} returned error");
|
|
|
|
|
|
|
|
// Ensure that no pruning happened.
|
|
|
|
assert_eq!(
|
|
|
|
fork_choice.len(),
|
|
|
|
expected_len,
|
|
|
|
"Prune op at index {} failed with {} instead of {}",
|
|
|
|
op_index,
|
|
|
|
fork_choice.len(),
|
|
|
|
expected_len
|
|
|
|
);
|
|
|
|
}
|
2022-02-28 22:07:48 +00:00
|
|
|
Operation::InvalidatePayload {
|
|
|
|
head_block_root,
|
|
|
|
latest_valid_ancestor_root,
|
2022-03-03 02:10:57 +00:00
|
|
|
} => {
|
|
|
|
let op = if let Some(latest_valid_ancestor) = latest_valid_ancestor_root {
|
|
|
|
InvalidationOperation::InvalidateMany {
|
|
|
|
head_block_root,
|
|
|
|
always_invalidate_head: true,
|
|
|
|
latest_valid_ancestor,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
InvalidationOperation::InvalidateOne {
|
|
|
|
block_root: head_block_root,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
fork_choice
|
|
|
|
.process_execution_payload_invalidation(&op)
|
|
|
|
.unwrap()
|
|
|
|
}
|
2022-02-28 22:07:48 +00:00
|
|
|
Operation::AssertWeight { block_root, weight } => assert_eq!(
|
|
|
|
fork_choice.get_weight(&block_root).unwrap(),
|
|
|
|
weight,
|
|
|
|
"block weight"
|
|
|
|
),
|
2020-01-29 04:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 22:07:48 +00:00
|
|
|
/// Gives a root that is not the zero hash (unless i is `usize::max_value)`.
|
|
|
|
fn get_root(i: u64) -> Hash256 {
|
2021-12-13 20:43:22 +00:00
|
|
|
Hash256::from_low_u64_be(i + 1)
|
|
|
|
}
|
|
|
|
|
2022-02-28 22:07:48 +00:00
|
|
|
/// Gives a hash that is not the zero hash (unless i is `usize::max_value)`.
|
|
|
|
fn get_hash(i: u64) -> ExecutionBlockHash {
|
|
|
|
ExecutionBlockHash::from_root(get_root(i))
|
|
|
|
}
|
|
|
|
|
2021-12-13 20:43:22 +00:00
|
|
|
/// 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),
|
2022-02-28 22:07:48 +00:00
|
|
|
root: get_root(i),
|
2021-12-13 20:43:22 +00:00
|
|
|
}
|
2020-01-29 04:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_bytes_round_trip(original: &ProtoArrayForkChoice) {
|
|
|
|
let bytes = original.as_bytes();
|
|
|
|
let decoded =
|
|
|
|
ProtoArrayForkChoice::from_bytes(&bytes).expect("fork choice should decode from bytes");
|
|
|
|
assert!(
|
|
|
|
*original == decoded,
|
|
|
|
"fork choice should encode and decode without change"
|
|
|
|
);
|
|
|
|
}
|