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 performance during CheckAttesterDoubleVotes #8927

Merged
merged 11 commits into from
May 25, 2021
Merged

Improve performance during CheckAttesterDoubleVotes #8927

merged 11 commits into from
May 25, 2021

Conversation

jmozah
Copy link
Contributor

@jmozah jmozah commented May 23, 2021

What type of PR is this?
Performance Improvement

What does this PR do? Why is it needed?
This PR tries to improves the performance when checking for double votes during attestation.
The code was doing multiple in-directions inside a double loop.
The fix was to store the attestations in multiple places (de-normalize) and process
every attestation in a seperate go routine.

Which issues(s) does this PR fix?
Fixes #8913

Other notes for review

  • Bucket removed (attestationRecordsBucket)
  • Bucket 'attestationDataRootsBucket' value changed from
    'att.SigningRoot + FastSum64([]ValidatorIndex)' to 'att.SigningRoot + AttestationRecord'
  • DB migration after merging 'feature/slasher' ??

@codecov
Copy link

codecov bot commented May 24, 2021

Codecov Report

Merging #8927 (55afc29) into develop (aa0fb05) will decrease coverage by 0.22%.
The diff coverage is 79.06%.

@@             Coverage Diff             @@
##           develop    #8927      +/-   ##
===========================================
- Coverage    60.90%   60.67%   -0.23%     
===========================================
  Files          529      529              
  Lines        37372    37367       -5     
===========================================
- Hits         22762    22674      -88     
- Misses       11344    11434      +90     
+ Partials      3266     3259       -7     

@jmozah
Copy link
Contributor Author

jmozah commented May 24, 2021

Benchmarks: Old schema vs New shema

BenchmarkStore_CheckDoubleBlockProposals-16 2 504269674 ns/op 208081248 B/op 5010016 allocs/op
BenchmarkStore_CheckDoubleBlockProposals-16 7 157626795 ns/op 189470513 B/op 4086973 allocs/op

@rauljordan rauljordan marked this pull request as ready for review May 24, 2021 14:43
@rauljordan rauljordan requested a review from a team as a code owner May 24, 2021 14:43
rauljordan
rauljordan previously approved these changes May 24, 2021
Copy link
Contributor

@rauljordan rauljordan left a comment

Choose a reason for hiding this comment

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

Wow! Really great solution and thank you for the benchmark and detailed comments. I think adding errgroup is also a great way to speed up given just how many attestations this method is called with. Great work

Copy link
Member

@prestonvanloon prestonvanloon left a comment

Choose a reason for hiding this comment

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

Very nice PR! I had just a few comments or questions.

@@ -9,7 +9,6 @@ package slasherkv
var (
// Slasher buckets.
attestedEpochsByValidator = []byte("attested-epochs-by-validator")
attestationRecordsBucket = []byte("attestation-records")
Copy link
Member

Choose a reason for hiding this comment

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

We should add a database "migration" to delete this bucket to reclaim the space, if it is no longer used.

@rauljordan, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

This db is unused at the moment, so we are safe, but we should add a migration in the future for slasher changes

doubleVotesMu := sync.Mutex{}
eg, egctx := errgroup.WithContext(ctx)
for _, att := range attestations {
attToProcess := att // https://golang.org/doc/faq#closures_and_goroutines
Copy link
Member

Choose a reason for hiding this comment

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

It is not clear how this comment adds additional context to the reader. Could you please use a full sentence followed by "See: $URL".

eg, egctx := errgroup.WithContext(ctx)
for _, att := range attestations {
attToProcess := att // https://golang.org/doc/faq#closures_and_goroutines
eg.Go(func() error { // process every attestation parallely
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
eg.Go(func() error { // process every attestation parallely
eg.Go(func() error { // process every attestation parallelly

Comment on lines 373 to 374
attWrapper, err := decodeAttestationRecord(encodedAttRecord)
if err != nil {
return err
attWrapper, err1 := decodeAttestationRecord(encodedAttRecord)
if err1 != nil {
return err1
Copy link
Member

Choose a reason for hiding this comment

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

I'm curious about this change. Why rename err to err1? Was it to satisfy some err / shadowing linter, personal preference, or some other standard we should adopt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. err is returned by View. By naming like this we can avoid shadowing. We should actually use more legible errName. I have changed this to 'decodeErr'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize CheckAttesterDoubleVotes Slasher DB Method
3 participants