lighthouse/beacon_node
Michael Sproul 8fa032c8ae Run fork choice before block proposal (#3168)
## Issue Addressed

Upcoming spec change https://github.com/ethereum/consensus-specs/pull/2878

## Proposed Changes

1. Run fork choice at the start of every slot, and wait for this run to complete before proposing a block.
2. As an optimisation, also run fork choice 3/4 of the way through the slot (at 9s), _dequeueing attestations for the next slot_.
3. Remove the fork choice run from the state advance timer that occurred before advancing the state.

## Additional Info

### Block Proposal Accuracy

This change makes us more likely to propose on top of the correct head in the presence of re-orgs with proposer boost in play. The main scenario that this change is designed to address is described in the linked spec issue.

### Attestation Accuracy

This change _also_ makes us more likely to attest to the correct head. Currently in the case of a skipped slot at `slot` we only run fork choice 9s into `slot - 1`. This means the attestations from `slot - 1` aren't taken into consideration, and any boost applied to the block from `slot - 1` is not removed (it should be). In the language of the linked spec issue, this means we are liable to attest to C, even when the majority voting weight has already caused a re-org to B.

### Why remove the call before the state advance?

If we've run fork choice at the start of the slot then it has already dequeued all the attestations from the previous slot, which are the only ones eligible to influence the head in the current slot. Running fork choice again is unnecessary (unless we run it for the next slot and try to pre-empt a re-org, but I don't currently think this is a great idea).

### Performance

Based on Prater testing this adds about 5-25ms of runtime to block proposal times, which are 500-1000ms on average (and spike to 5s+ sometimes due to state handling issues 😢 ). I believe this is a small enough penalty to enable it by default, with the option to disable it via the new flag `--fork-choice-before-proposal-timeout 0`. Upcoming work on block packing and state representation will also reduce block production times in general, while removing the spikes.

### Implementation

Fork choice gets invoked at the start of the slot via the `per_slot_task` function called from the slot timer. It then uses a condition variable to signal to block production that fork choice has been updated. This is a bit funky, but it seems to work. One downside of the timer-based approach is that it doesn't happen automatically in most of the tests. The test added by this PR has to trigger the run manually.
2022-05-20 05:02:11 +00:00
..
beacon_chain Run fork choice before block proposal (#3168) 2022-05-20 05:02:11 +00:00
client Allow TaskExecutor to be used in async tests (#3178) 2022-05-16 08:35:59 +00:00
eth1 v2.2.0 (#3139) 2022-04-05 02:53:09 +00:00
execution_layer Log out response status when we hit PayloadIdUnavailable (#3190) 2022-05-19 06:00:48 +00:00
genesis Update and consolidate dependencies (#3136) 2022-04-04 00:26:16 +00:00
http_api Run fork choice before block proposal (#3168) 2022-05-20 05:02:11 +00:00
http_metrics Support IPv6 in BN and VC HTTP APIs (#3104) 2022-03-24 00:04:49 +00:00
lighthouse_network Tiny improvement: PeerManager and maximum discovery query (#3182) 2022-05-19 06:00:46 +00:00
network Allow TaskExecutor to be used in async tests (#3178) 2022-05-16 08:35:59 +00:00
operation_pool Remove DB migrations for legacy database schemas (#3181) 2022-05-17 04:54:39 +00:00
src Run fork choice before block proposal (#3168) 2022-05-20 05:02:11 +00:00
store Separate execution payloads in the DB (#3157) 2022-05-12 00:42:17 +00:00
tests Altair consensus changes and refactors (#2279) 2021-07-09 06:15:32 +00:00
timer Update to Rust 1.59 and 2021 edition (#3038) 2022-02-25 00:10:17 +00:00
Cargo.toml v2.2.1 (#3149) 2022-04-12 02:52:12 +00:00