lighthouse/beacon_node
Paul Hauner be11437c27 Batch BLS verification for attestations (#2399)
## Issue Addressed

NA

## Proposed Changes

Adds the ability to verify batches of aggregated/unaggregated attestations from the network.

When the `BeaconProcessor` finds there are messages in the aggregated or unaggregated attestation queues, it will first check the length of the queue:

- `== 1` verify the attestation individually.
- `>= 2` take up to 64 of those attestations and verify them in a batch.

Notably, we only perform batch verification if the queue has a backlog. We don't apply any artificial delays to attestations to try and force them into batches. 

### Batching Details

To assist with implementing batches we modify `beacon_chain::attestation_verification` to have two distinct categories for attestations:

- *Indexed* attestations: those which have passed initial validation and were valid enough for us to derive an `IndexedAttestation`.
- *Verified* attestations: those attestations which were indexed *and also* passed signature verification. These are well-formed, interesting messages which were signed by validators.

The batching functions accept `n` attestations and then return `n` attestation verification `Result`s, where those `Result`s can be any combination of `Ok` or `Err`. In other words, we attempt to verify as many attestations as possible and return specific per-attestation results so peer scores can be updated, if required.

When we batch verify attestations, we first try to map all those attestations to *indexed* attestations. If any of those attestations were able to be indexed, we then perform batch BLS verification on those indexed attestations. If the batch verification succeeds, we convert them into *verified* attestations, disabling individual signature checking. If the batch fails, we convert to verified attestations with individual signature checking enabled.

Ultimately, we optimistically try to do a batch verification of attestation signatures and fall-back to individual verification if it fails. This opens an attach vector for "poisoning" the attestations and causing us to waste a batch verification. I argue that peer scoring should do a good-enough job of defending against this and the typical-case gains massively outweigh the worst-case losses.

## Additional Info

Before this PR, attestation verification took the attestations by value (instead of by reference). It turns out that this was unnecessary and, in my opinion, resulted in some undesirable ergonomics (e.g., we had to pass the attestation back in the `Err` variant to avoid clones). In this PR I've modified attestation verification so that it now takes a reference.

I refactored the `beacon_chain/tests/attestation_verification.rs` tests so they use a builder-esque "tester" struct instead of a weird macro. It made it easier for me to test individual/batch with the same set of tests and I think it was a nice tidy-up. Notably, I did this last to try and make sure my new refactors to *actual* production code would pass under the existing test suite.
2021-09-22 08:49:41 +00:00
..
beacon_chain Batch BLS verification for attestations (#2399) 2021-09-22 08:49:41 +00:00
client Implement checkpoint sync (#2244) 2021-09-22 00:37:28 +00:00
eth1 Update sloggers to v2.0.2 (#2588) 2021-09-14 06:48:26 +00:00
eth2_libp2p Implement checkpoint sync (#2244) 2021-09-22 00:37:28 +00:00
genesis Updates to make crates publishable (#2472) 2021-09-03 01:10:25 +00:00
http_api Batch BLS verification for attestations (#2399) 2021-09-22 08:49:41 +00:00
http_metrics Upgrade dependencies (#2513) 2021-08-17 01:00:24 +00:00
network Batch BLS verification for attestations (#2399) 2021-09-22 08:49:41 +00:00
operation_pool Optimize process_attestation with active balance cache (#2560) 2021-09-03 07:50:43 +00:00
src Implement checkpoint sync (#2244) 2021-09-22 00:37:28 +00:00
store Implement checkpoint sync (#2244) 2021-09-22 00:37:28 +00:00
tests Altair consensus changes and refactors (#2279) 2021-07-09 06:15:32 +00:00
timer Upgrade dependencies (#2513) 2021-08-17 01:00:24 +00:00
websocket_server Server sent events (#1920) 2020-12-04 00:18:58 +00:00
Cargo.toml v1.5.2 (#2595) 2021-09-13 23:01:19 +00:00