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

Use New Cache Key For Unaggregated Attestations #14590

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
- Capella blocks are execution.
- Fixed panic when http request to subscribe to event stream fails.
- Return early for blob reconstructor during capella fork
- Add more efficient method of computing the cache key for unaggregated attestations.

### Deprecated

Expand Down
18 changes: 18 additions & 0 deletions beacon-chain/sync/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ go_test(
size = "small",
srcs = [
"batch_verifier_test.go",
"benchmark_test.go",
"blobs_test.go",
"block_batcher_test.go",
"broadcast_bls_changes_test.go",
Expand Down Expand Up @@ -268,3 +269,20 @@ go_test(
"@org_golang_google_protobuf//proto:go_default_library",
],
)

go_test(
name = "go_benchmark_test",
size = "medium",
srcs = ["benchmark_test.go"],
args = [
"-test.bench=.",
"-test.benchmem",
"-test.v",
],
embed = [":go_default_library"],
local = True,
tags = [
"benchmark",
"no-cache",
],
)
25 changes: 25 additions & 0 deletions beacon-chain/sync/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sync

import (
"testing"

"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
)

func BenchmarkCacheKeyImplementations(b *testing.B) {

b.Run("Old Cache Key Implementation", func(b *testing.B) {
for i := 0; i < b.N; i++ {
b := append(bytesutil.Bytes32(uint64(i)), bytesutil.Bytes32(uint64(i+10))...)
b = append(b, bytesutil.SafeCopyBytes([]byte("random"))...)
_ = string(b)
}
})

b.Run("New Cache Key Implementation", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = seenAttCacheKey(primitives.Slot(i), primitives.CommitteeIndex(i+10), []byte("random"))
}
})
}
21 changes: 15 additions & 6 deletions beacon-chain/sync/validate_beacon_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sync

import (
"context"
"encoding/binary"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -306,19 +307,15 @@ func (s *Service) validateBitLength(
func (s *Service) hasSeenCommitteeIndicesSlot(slot primitives.Slot, committeeID primitives.CommitteeIndex, aggregateBits []byte) bool {
s.seenUnAggregatedAttestationLock.RLock()
defer s.seenUnAggregatedAttestationLock.RUnlock()
b := append(bytesutil.Bytes32(uint64(slot)), bytesutil.Bytes32(uint64(committeeID))...)
b = append(b, aggregateBits...)
_, seen := s.seenUnAggregatedAttestationCache.Get(string(b))
_, seen := s.seenUnAggregatedAttestationCache.Get(seenAttCacheKey(slot, committeeID, aggregateBits))
return seen
}

// Set committee's indices and slot as seen for incoming attestations.
func (s *Service) setSeenCommitteeIndicesSlot(slot primitives.Slot, committeeID primitives.CommitteeIndex, aggregateBits []byte) {
s.seenUnAggregatedAttestationLock.Lock()
defer s.seenUnAggregatedAttestationLock.Unlock()
b := append(bytesutil.Bytes32(uint64(slot)), bytesutil.Bytes32(uint64(committeeID))...)
b = append(b, bytesutil.SafeCopyBytes(aggregateBits)...)
s.seenUnAggregatedAttestationCache.Add(string(b), true)
s.seenUnAggregatedAttestationCache.Add(seenAttCacheKey(slot, committeeID, aggregateBits), true)
}

// hasBlockAndState returns true if the beacon node knows about a block and associated state in the
Expand All @@ -328,3 +325,15 @@ func (s *Service) hasBlockAndState(ctx context.Context, blockRoot [32]byte) bool
hasState := hasStateSummary || s.cfg.beaconDB.HasState(ctx, blockRoot)
return hasState && s.cfg.chain.HasBlock(ctx, blockRoot)
}

// Amount of bytes required to store a uint64 value.
const uint64ByteLength = 8

func seenAttCacheKey(slot primitives.Slot, committeeID primitives.CommitteeIndex, aggregationBits []byte) string {
Copy link
Member

Choose a reason for hiding this comment

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

A benchmark test would be nice so that we can see the before/after improvements.

Copy link
Member Author

Choose a reason for hiding this comment

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

Benchmark between old and new implementations:

goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
BenchmarkCacheKeyImplementations
BenchmarkCacheKeyImplementations/Old_Cache_Key_Implementation
BenchmarkCacheKeyImplementations/Old_Cache_Key_Implementation-8                  7620234               169.0 ns/op           280 B/op          4 allocs/op
BenchmarkCacheKeyImplementations/New_Cache_Key_Implementation
BenchmarkCacheKeyImplementations/New_Cache_Key_Implementation-8                 35468606                31.01 ns/op           24 B/op          1 allocs/op

totalLen := uint64ByteLength + uint64ByteLength + len(aggregationBits)
key := make([]byte, totalLen)
binary.LittleEndian.PutUint64(key[:8], uint64(slot))
binary.LittleEndian.PutUint64(key[8:16], uint64(committeeID))
copy(key[16:], aggregationBits)
return bytesutil.UnsafeCastToString(key)
}
8 changes: 8 additions & 0 deletions beacon-chain/sync/validate_beacon_attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,11 @@ func TestService_setSeenCommitteeIndicesSlot(t *testing.T) {
require.Equal(t, false, s.hasSeenCommitteeIndicesSlot(0, 2, b1))
require.Equal(t, true, s.hasSeenCommitteeIndicesSlot(1, 2, b1))
}

func TestAttestationCacheKey(t *testing.T) {
stringKey := seenAttCacheKey(1, 1024, []byte("aggregation"))
wantedKey := append(bytesutil.Bytes8(1), bytesutil.Bytes8(1024)...)
wantedKey = append(wantedKey, []byte("aggregation")...)

require.Equal(t, string(wantedKey), stringKey)
}
Loading