Cache target attester balances for unrealized FFG progression calculation (#4362)

## Issue Addressed

#4118 

## Proposed Changes

This PR introduces a "progressive balances" cache on the `BeaconState`, which keeps track of the accumulated target attestation balance for the current & previous epochs. The cached values are utilised by fork choice to calculate unrealized justification and finalization (instead of converting epoch participation arrays to balances for each block we receive).

This optimization will be rolled out gradually to allow for more testing. A new `--progressive-balances disabled|checked|strict|fast` flag is introduced to support this:
- `checked`: enabled with checks against participation cache, and falls back to the existing epoch processing calculation if there is a total target attester balance mismatch. There is no performance gain from this as the participation cache still needs to be computed. **This is the default mode for now.**
- `strict`: enabled with checks against participation cache, returns error if there is a mismatch. **Used for testing only**.
- `fast`: enabled with no comparative checks and without computing the participation cache. This mode gives us the performance gains from the optimization. This is still experimental and not currently recommended for production usage, but will become the default mode in a future release.
- `disabled`: disable the usage of progressive cache, and use the existing method for FFG progression calculation. This mode may be useful if we find a bug and want to stop the frequent error logs.

### Tasks

- [x] Initial cache implementation in `BeaconState`
- [x] Perform checks in fork choice to compare the progressive balances cache against results from `ParticipationCache`
- [x] Add CLI flag, and disable the optimization by default
- [x] Testing on Goerli & Benchmarking
- [x]  Move caching logic from state processing to the `ProgressiveBalancesCache` (see [this comment](https://github.com/sigp/lighthouse/pull/4362#discussion_r1230877001))
- [x] Add attesting balance metrics



Co-authored-by: Jimmy Chen <jimmy@sigmaprime.io>
This commit is contained in:
Jimmy Chen
2023-06-30 01:13:06 +00:00
parent 826e090f50
commit 46be05f728
48 changed files with 953 additions and 121 deletions

View File

@@ -1,5 +1,6 @@
use clap::{App, Arg};
use strum::VariantNames;
use types::ProgressiveBalancesMode;
pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
App::new("beacon_node")
@@ -1117,4 +1118,17 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
developers. This directory is not pruned, users should be careful to avoid \
filling up their disks.")
)
.arg(
Arg::with_name("progressive-balances")
.long("progressive-balances")
.value_name("MODE")
.help("Options to enable or disable the progressive balances cache for \
unrealized FFG progression calculation. The default `checked` mode compares \
the progressive balances from the cache against results from the existing \
method. If there is a mismatch, it falls back to the existing method. The \
optimized mode (`fast`) is faster but is still experimental, and is \
not recommended for mainnet usage at this time.")
.takes_value(true)
.possible_values(ProgressiveBalancesMode::VARIANTS)
)
}

View File

@@ -800,6 +800,12 @@ pub fn get_config<E: EthSpec>(
client_config.network.invalid_block_storage = Some(path);
}
if let Some(progressive_balances_mode) =
clap_utils::parse_optional(cli_args, "progressive-balances")?
{
client_config.chain.progressive_balances_mode = progressive_balances_mode;
}
Ok(client_config)
}