lighthouse/consensus
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
..
cached_tree_hash Updates to make crates publishable (#2472) 2021-09-03 01:10:25 +00:00
fork_choice Batch BLS verification for attestations (#2399) 2021-09-22 08:49:41 +00:00
int_to_bytes Update to tokio 1.1 (#2172) 2021-02-10 23:29:49 +00:00
merkle_proof Updates to make crates publishable (#2472) 2021-09-03 01:10:25 +00:00
proto_array Updates to make crates publishable (#2472) 2021-09-03 01:10:25 +00:00
safe_arith Remove saturating arith from state_processing (#1644) 2020-09-25 05:18:21 +00:00
serde_utils Updates to make crates publishable (#2472) 2021-09-03 01:10:25 +00:00
ssz Updates to make crates publishable (#2472) 2021-09-03 01:10:25 +00:00
ssz_derive Updates to make crates publishable (#2472) 2021-09-03 01:10:25 +00:00
ssz_types Updates to make crates publishable (#2472) 2021-09-03 01:10:25 +00:00
state_processing Implement checkpoint sync (#2244) 2021-09-22 00:37:28 +00:00
swap_or_not_shuffle Updates to make crates publishable (#2472) 2021-09-03 01:10:25 +00:00
tree_hash Updates to make crates publishable (#2472) 2021-09-03 01:10:25 +00:00
tree_hash_derive Updates to make crates publishable (#2472) 2021-09-03 01:10:25 +00:00
types Implement checkpoint sync (#2244) 2021-09-22 00:37:28 +00:00