Compare commits

...

13 Commits

Author SHA1 Message Date
nisdas
1f1c4e2b08 Merge branch 'develop' of https://github.com/prysmaticlabs/geth-sharding into useNewCacheKey 2024-12-17 18:13:26 +08:00
Nishant Das
6387040613 Merge branch 'develop' into useNewCacheKey 2024-12-10 14:41:38 +08:00
Nishant Das
1c7077e254 Merge branch 'develop' into useNewCacheKey 2024-11-29 11:58:31 +08:00
Nishant Das
9ab7e7d37c Update CHANGELOG.md 2024-11-29 11:57:31 +08:00
Nishant Das
bf80a5430f Merge branch 'develop' into useNewCacheKey 2024-11-27 13:06:03 +08:00
Nishant Das
9aef2c4ee2 Merge branch 'develop' into useNewCacheKey 2024-11-25 17:25:43 +08:00
nisdas
d094118082 Add benchmarks 2024-11-08 17:02:04 +08:00
nisdas
c69599f343 Addressing Review Comments 2024-11-08 16:41:28 +08:00
nisdas
1e0e5e110a Add Changelog 2024-11-08 16:38:26 +08:00
nisdas
8254cb30b4 Merge branch 'develop' of https://github.com/prysmaticlabs/geth-sharding into useNewCacheKey 2024-11-08 16:37:35 +08:00
nisdas
74961eb51f Gosec 2024-10-30 16:50:28 +08:00
nisdas
514f96ec0e Changelog 2024-10-30 13:58:27 +08:00
nisdas
cabe7d34b6 Use New Cache Key 2024-10-30 13:46:38 +08:00
5 changed files with 67 additions and 6 deletions

View File

@@ -16,6 +16,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
### Changed
- Process light client finality updates only for new finalized epochs instead of doing it for every block.
- Add more efficient method of computing the cache key for unaggregated attestations.
### Deprecated

View File

@@ -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",
@@ -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",
],
)

View File

@@ -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"))
}
})
}

View File

@@ -2,6 +2,7 @@ package sync
import (
"context"
"encoding/binary"
"fmt"
"reflect"
"strings"
@@ -305,9 +306,7 @@ 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
}
@@ -315,9 +314,7 @@ func (s *Service) hasSeenCommitteeIndicesSlot(slot primitives.Slot, committeeID
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
@@ -327,3 +324,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 {
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)
}

View File

@@ -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)
}