Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve AggregateAndProof gossip validation #2329

Merged
merged 3 commits into from
Apr 12, 2021

Conversation

twoeths
Copy link
Contributor

@twoeths twoeths commented Apr 6, 2021

Motivation

  • AggregateAndProof as TreeBacked to avoid multiple struct_hashTreeRoot
  • Increase JobQueue for AggregateAndProof
  • Filter out known aggregated attestation

Description

part of #2306

@github-actions github-actions bot added the scope-networking All issues related to networking, gossip, and libp2p. label Apr 6, 2021
@codeclimate
Copy link

codeclimate bot commented Apr 6, 2021

Code Climate has analyzed commit f45ffee and detected 1 issue on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 1

View more on Code Climate.

@twoeths twoeths marked this pull request as ready for review April 6, 2021 04:24
@twoeths twoeths force-pushed the tuyen/gossip-aggregate-and-proof-validation branch from d65e58f to bdb80e5 Compare April 7, 2021 02:08
@twoeths
Copy link
Contributor Author

twoeths commented Apr 7, 2021

some statistic (found cached AggregateAndProof / total)

slot 104922: 32/596
slot 104956: 99/555
slot 104954: 38/437
slot 104948: 50/525
slot 105057: 0/14
slot 105056: 2/49

@twoeths
Copy link
Contributor Author

twoeths commented Apr 7, 2021

based on prater block explorer, I think there are at most 64 attestation data based on 64 different committee index of the same slot. There are ~600 different AggregateAndProof messages of the same slot because each aggregator see different aggregationBits. So we can improve the cache strategy by:

  • cache by AttestationData root + number of attesters (based on aggregationBits)
  • if same AttestationData root and not greater number of attesters, we can ignore that AggregationAndProof

what do you think @dapplion ?

@dapplion
Copy link
Contributor

dapplion commented Apr 7, 2021

based on prater block explorer, I think there are at most 64 attestation data based on 64 different committee index of the same slot. There are ~600 different AggregateAndProof messages of the same slot because each aggregator see different aggregationBits. So we can improve the cache strategy by:

  • cache by AttestationData root + number of attesters (based on aggregationBits)
  • if same AttestationData root and not greater number of attesters, we can ignore that AggregationAndProof

what do you think @dapplion ?

@wemeetagain Can confirm but we should take any aggregate that includes new bits so the fork choice is aware of as many attestations as possible

@twoeths
Copy link
Contributor Author

twoeths commented Apr 7, 2021

in that case we should cache AttestationData root + XOR of (aggregationBits):

  • {rootA, [0, 1, 1, 0]} comes => cache {rootA => [0, 1, 1, 0]}
  • {rootA, [1, 0, 1, 0]} comes => cache {rootA => [1, 1, 1, 0]}
  • {rootA, [1, 1, 0, 0]} comes => IGNORE since its aggregationBits are subset of the cached one
  • {rootA, [1, 1, 1, 0]} comes => IGNORE since its aggregationBits are same to the cached one
  • {rootA, [0, 0, 0, 1} comes => cache {rootA => [1, 1, 1, 1]}
  • from now on whenever rootA comes => IGNORE

@twoeths twoeths marked this pull request as draft April 7, 2021 11:29
@twoeths twoeths force-pushed the tuyen/gossip-aggregate-and-proof-validation branch 2 times, most recently from 0426a73 to 6ca8401 Compare April 8, 2021 08:56
@twoeths
Copy link
Contributor Author

twoeths commented Apr 8, 2021

with the cache by AttestationData root hex + XOR (aggregationBits) we have (found seen gossip AggregationAndProof/total):

slot 113716: 397/546
slot 113720: 384/553
slot 113736: 394/596

so we don't have to validate every SignedAggregateAndProof like before, just around 30% of that

@twoeths twoeths marked this pull request as ready for review April 8, 2021 09:15

/**
* USed to verify gossip attestation. When there are multiple
* attestation from same validator
*/
export class SeenAttestationCache {
private cache: Map<string, boolean>;

// key as AttestationDelta root hex, value as aggregationBits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// key as AttestationDelta root hex, value as aggregationBits
// key as AttestationData root hex, value as aggregationBits

* We're only interested in the AttestationData + aggregationBits inside AggregateAndProof.
*/
private aggregateAndProofKey(value: phase0.AggregateAndProof): string {
return toHexString(this.config.types.phase0.AttestationData.hashTreeRoot(value.aggregate.data));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return toHexString(this.config.types.phase0.AttestationData.hashTreeRoot(value.aggregate.data));
return toHexString(this.config.getTypes(value.aggregate.slot).AttestationData.hashTreeRoot(value.aggregate.data));

Comment on lines 45 to 48
this.attDataCache.set(
key,
aggBit.map((_, i) => aggBit[i] || cachedAggBit[i])
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might consider something like:

for (const [i, bit] of aggBit.entries()) {
  if (bit) {
    cachedAggBit[i] = true;
  }
}

tbh i'm not sure which is better.

@@ -48,7 +48,7 @@ export class BeaconDb extends DatabaseService implements IBeaconDb {
this.badBlock = new BadBlockRepository(this.config, this.db);
this.block = new BlockRepository(this.config, this.db);
this.pendingBlock = new PendingBlockRepository(this.config, this.db);
this.seenAttestationCache = new SeenAttestationCache(5000);
this.seenAttestationCache = new SeenAttestationCache(this.config, 2048);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious why this is decreased?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently it's used for 2 caches: (committee) Attestation and AttestationAndProof but this PR changes to cache them separately so mathematically it should be 5000 / 2 = 2500, but I changed it to 2048

@twoeths twoeths force-pushed the tuyen/gossip-aggregate-and-proof-validation branch from 6ca8401 to 995860f Compare April 10, 2021 03:43
@twoeths twoeths force-pushed the tuyen/gossip-aggregate-and-proof-validation branch from 995860f to f45ffee Compare April 10, 2021 03:45
@@ -20,7 +20,8 @@ import {
// Numbers from https://github.com/sigp/lighthouse/blob/b34a79dc0b02e04441ba01fd0f304d1e203d877d/beacon_node/network/src/beacon_processor/mod.rs#L69
const gossipQueueOpts: {[K in GossipType]: {maxLength: number; type: QueueType}} = {
[GossipType.beacon_block]: {maxLength: 1024, type: QueueType.FIFO},
[GossipType.beacon_aggregate_and_proof]: {maxLength: 1024, type: QueueType.LIFO},
// this is different from lighthouse's, there are more gossip aggregate_and_proof than gossip block
[GossipType.beacon_aggregate_and_proof]: {maxLength: 4096, type: QueueType.LIFO},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the rationale for changing maxLength here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue itself is the queue length is not enough to handle SignedAggregateAndProof gossip topic, it's ~600 gossip messages per slot

@wemeetagain wemeetagain merged commit e478ce6 into master Apr 12, 2021
@wemeetagain wemeetagain deleted the tuyen/gossip-aggregate-and-proof-validation branch April 12, 2021 20:18
@twoeths twoeths mentioned this pull request May 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope-networking All issues related to networking, gossip, and libp2p.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants