Feature lru cache wrapper 2 (#9511)

* Add Wrapper to LRU Cache to handle Invalid Parameters #9461

* Regenerate BUILD.bazel and simplify tests using lru.Cache

* Fix: fuzz_exports.go build error

* Fix: block_fuzz.go

* Revert lru.Cache interface

* Remove redundant err check in pending_attestations_queue_test.go

* Add tests for lru wrapper

* Use lru package in prysm/shared instead of lruwrpr

* Fix: goimports

* Fix: BUILD.bazel

Co-authored-by: Nishant Das <nishdas93@gmail.com>
This commit is contained in:
Marcin Górzyński
2021-09-02 12:36:54 +02:00
committed by GitHub
parent 43a24e3d8b
commit 02966e64d8
38 changed files with 220 additions and 314 deletions

View File

@@ -44,6 +44,7 @@ go_library(
"//shared/copyutil:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/lru:go_default_library",
"//shared/mathutil:go_default_library",
"//shared/params:go_default_library",
"//shared/rand:go_default_library",

View File

@@ -13,6 +13,7 @@ import (
ethTypes "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
)
@@ -40,13 +41,8 @@ type BalanceCache struct {
// NewEffectiveBalanceCache creates a new effective balance cache for storing/accessing total balance by epoch.
func NewEffectiveBalanceCache() *BalanceCache {
c, err := lru.New(int(maxBalanceCacheSize))
// An error is only returned if the size of the cache is <= 0.
if err != nil {
panic(err)
}
return &BalanceCache{
cache: c,
cache: lruwrpr.New(int(maxBalanceCacheSize)),
}
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/state"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
)
var (
@@ -36,12 +37,8 @@ type CheckpointStateCache struct {
// NewCheckpointStateCache creates a new checkpoint state cache for storing/accessing processed state.
func NewCheckpointStateCache() *CheckpointStateCache {
cache, err := lru.New(maxCheckpointStateSize)
if err != nil {
panic(err)
}
return &CheckpointStateCache{
cache: cache,
cache: lruwrpr.New(maxCheckpointStateSize),
}
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
types "github.com/prysmaticlabs/eth2-types"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/mathutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/sliceutil"
@@ -49,14 +50,8 @@ func committeeKeyFn(obj interface{}) (string, error) {
// NewCommitteesCache creates a new committee cache for storing/accessing shuffled indices of a committee.
func NewCommitteesCache() *CommitteeCache {
cCache, err := lru.New(int(maxCommitteesCacheSize))
// An error is only returned if the size of the cache is
// <= 0.
if err != nil {
panic(err)
}
return &CommitteeCache{
CommitteeCache: cCache,
CommitteeCache: lruwrpr.New(int(maxCommitteesCacheSize)),
}
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"go.opencensus.io/trace"
)
@@ -35,12 +36,8 @@ type SkipSlotCache struct {
// NewSkipSlotCache initializes the map and underlying cache.
func NewSkipSlotCache() *SkipSlotCache {
cache, err := lru.New(8)
if err != nil {
panic(err)
}
return &SkipSlotCache{
cache: cache,
cache: lruwrpr.New(8),
inProgress: make(map[[32]byte]bool),
}
}

View File

@@ -7,6 +7,7 @@ import (
lru "github.com/hashicorp/golang-lru"
"github.com/patrickmn/go-cache"
types "github.com/prysmaticlabs/eth2-types"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/sliceutil"
)
@@ -27,14 +28,8 @@ func newSubnetIDs() *subnetIDs {
// Given a node can calculate committee assignments of current epoch and next epoch.
// Max size is set to 2 epoch length.
cacheSize := int(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().MaxCommitteesPerSlot * 2))
attesterCache, err := lru.New(cacheSize)
if err != nil {
panic(err)
}
aggregatorCache, err := lru.New(cacheSize)
if err != nil {
panic(err)
}
attesterCache := lruwrpr.New(cacheSize)
aggregatorCache := lruwrpr.New(cacheSize)
epochDuration := time.Duration(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().SecondsPerSlot))
subLength := epochDuration * time.Duration(params.BeaconConfig().EpochsPerRandomSubnetSubscription)
persistentCache := cache.New(subLength*time.Second, epochDuration*time.Second)

View File

@@ -7,6 +7,7 @@ import (
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
stateAltair "github.com/prysmaticlabs/prysm/beacon-chain/state/v2"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
)
// SyncCommitteeHeadStateCache for the latest head state requested by a sync committee participant.
@@ -17,10 +18,7 @@ type SyncCommitteeHeadStateCache struct {
// NewSyncCommitteeHeadState initializes a LRU cache for `SyncCommitteeHeadState` with size of 1.
func NewSyncCommitteeHeadState() *SyncCommitteeHeadStateCache {
c, err := lru.New(1) // only need size of 1 to avoid redundant state copies, hashing, and slot processing.
if err != nil {
panic(err)
}
c := lruwrpr.New(1) // only need size of 1 to avoid redundant state copies, hashing, and slot processing.
return &SyncCommitteeHeadStateCache{cache: c}
}

View File

@@ -22,6 +22,7 @@ go_library(
"//shared/aggregation/attestations:go_default_library",
"//shared/copyutil:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/lru:go_default_library",
"//shared/params:go_default_library",
"//shared/slotutil:go_default_library",
"//shared/timeutils:go_default_library",

View File

@@ -8,6 +8,7 @@ import (
"time"
lru "github.com/hashicorp/golang-lru"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
)
@@ -32,10 +33,7 @@ type Config struct {
// NewService instantiates a new attestation pool service instance that will
// be registered into a running beacon node.
func NewService(ctx context.Context, cfg *Config) (*Service, error) {
cache, err := lru.New(forkChoiceProcessedRootsSize)
if err != nil {
return nil, err
}
cache := lruwrpr.New(forkChoiceProcessedRootsSize)
if cfg.pruneInterval == 0 {
// Prune expired attestations from the pool every slot interval.

View File

@@ -30,6 +30,7 @@ go_library(
"//proto/prysm/v1alpha1:go_default_library",
"//proto/prysm/v1alpha1/block:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/lru:go_default_library",
"//shared/params:go_default_library",
"//shared/version:go_default_library",
"@com_github_hashicorp_golang_lru//:go_default_library",

View File

@@ -7,6 +7,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
)
var (
@@ -31,12 +32,8 @@ type hotStateCache struct {
// newHotStateCache initializes the map and underlying cache.
func newHotStateCache() *hotStateCache {
cache, err := lru.New(hotStateCacheSize)
if err != nil {
panic(err)
}
return &hotStateCache{
cache: cache,
cache: lruwrpr.New(hotStateCacheSize),
}
}

View File

@@ -78,6 +78,7 @@ go_library(
"//shared/bls:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/lru:go_default_library",
"//shared/messagehandler:go_default_library",
"//shared/mputil:go_default_library",
"//shared/p2putils:go_default_library",
@@ -175,6 +176,7 @@ go_test(
"//shared/bls:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/copyutil:go_default_library",
"//shared/lru:go_default_library",
"//shared/p2putils:go_default_library",
"//shared/params:go_default_library",
"//shared/rand:go_default_library",
@@ -186,7 +188,6 @@ go_test(
"@com_github_d4l3k_messagediff//:go_default_library",
"@com_github_ethereum_go_ethereum//p2p/enr:go_default_library",
"@com_github_golang_snappy//:go_default_library",
"@com_github_hashicorp_golang_lru//:go_default_library",
"@com_github_kevinms_leakybucket_go//:go_default_library",
"@com_github_libp2p_go_libp2p_core//:go_default_library",
"@com_github_libp2p_go_libp2p_core//mux:go_default_library",

View File

@@ -40,6 +40,6 @@ func (s *Service) FuzzBeaconBlockSubscriber(ctx context.Context, msg proto.Messa
return s.beaconBlockSubscriber(ctx, msg)
}
func (s *Service) InitCaches() error {
return s.initCaches()
func (s *Service) InitCaches() {
s.initCaches()
}

View File

@@ -6,7 +6,6 @@ import (
"time"
"github.com/ethereum/go-ethereum/p2p/enr"
lru "github.com/hashicorp/golang-lru"
"github.com/libp2p/go-libp2p-core/network"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/go-bitfield"
@@ -23,6 +22,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/attestationutil"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
@@ -106,8 +106,6 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) {
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
P2P: p1,
@@ -121,7 +119,7 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) {
AttPool: attestations.NewPool(),
},
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
seenUnAggregatedAttestationCache: c,
seenUnAggregatedAttestationCache: lruwrpr.New(10),
}
sb = testutil.NewBeaconBlock()
@@ -223,8 +221,6 @@ func TestProcessPendingAtts_NoBroadcastWithBadSignature(t *testing.T) {
require.NoError(t, err)
require.NoError(t, s.SetGenesisTime(uint64(time.Now().Unix())))
c, err := lru.New(10)
require.NoError(t, err)
r = &Service{
cfg: &Config{
P2P: p1,
@@ -238,7 +234,7 @@ func TestProcessPendingAtts_NoBroadcastWithBadSignature(t *testing.T) {
AttPool: attestations.NewPool(),
},
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
seenUnAggregatedAttestationCache: c,
seenUnAggregatedAttestationCache: lruwrpr.New(10),
}
r.blkRootToPendingAtts[r32] = []*ethpb.SignedAggregateAttestationAndProof{{Message: aggregateAndProof, Signature: aggreSig}}
@@ -303,8 +299,6 @@ func TestProcessPendingAtts_HasBlockSaveAggregatedAtt(t *testing.T) {
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
P2P: p1,
@@ -318,7 +312,7 @@ func TestProcessPendingAtts_HasBlockSaveAggregatedAtt(t *testing.T) {
AttPool: attestations.NewPool(),
},
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
seenAggregatedAttestationCache: c,
seenAggregatedAttestationCache: lruwrpr.New(10),
}
sb = testutil.NewBeaconBlock()

View File

@@ -51,8 +51,7 @@ func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks1(t *testing.T) {
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
err := r.initCaches()
require.NoError(t, err)
r.initCaches()
b0 := testutil.NewBeaconBlock()
require.NoError(t, r.cfg.DB.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(b0)))
@@ -113,8 +112,7 @@ func TestRegularSync_InsertDuplicateBlocks(t *testing.T) {
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
err := r.initCaches()
require.NoError(t, err)
r.initCaches()
b0 := testutil.NewBeaconBlock()
b0r := [32]byte{'a'}
@@ -183,8 +181,8 @@ func TestRegularSyncBeaconBlockSubscriber_ProcessPendingBlocks_2Chains(t *testin
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
err := r.initCaches()
require.NoError(t, err)
r.initCaches()
p1.Peers().Add(new(enr.Record), p2.PeerID(), nil, network.DirOutbound)
p1.Peers().SetConnectionState(p2.PeerID(), peers.PeerConnected)
p1.Peers().SetChainState(p2.PeerID(), &ethpb.Status{})
@@ -274,8 +272,8 @@ func TestRegularSyncBeaconBlockSubscriber_PruneOldPendingBlocks(t *testing.T) {
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
err := r.initCaches()
require.NoError(t, err)
r.initCaches()
p1.Peers().Add(new(enr.Record), p1.PeerID(), nil, network.DirOutbound)
p1.Peers().SetConnectionState(p1.PeerID(), peers.PeerConnected)
p1.Peers().SetChainState(p1.PeerID(), &ethpb.Status{})
@@ -360,9 +358,8 @@ func TestService_BatchRootRequest(t *testing.T) {
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
r.initCaches()
err := r.initCaches()
require.NoError(t, err)
p1.Peers().Add(new(enr.Record), p2.PeerID(), nil, network.DirOutbound)
p1.Peers().SetConnectionState(p2.PeerID(), peers.PeerConnected)
p1.Peers().SetChainState(p2.PeerID(), &ethpb.Status{FinalizedEpoch: 2})
@@ -470,8 +467,7 @@ func TestService_ProcessPendingBlockOnCorrectSlot(t *testing.T) {
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
err := r.initCaches()
require.NoError(t, err)
r.initCaches()
beaconState, privKeys := testutil.DeterministicGenesisState(t, 100)
parentBlock := testutil.NewBeaconBlock()
@@ -543,8 +539,7 @@ func TestService_ProcessBadPendingBlocks(t *testing.T) {
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
err := r.initCaches()
require.NoError(t, err)
r.initCaches()
beaconState, privKeys := testutil.DeterministicGenesisState(t, 100)
parentBlock := testutil.NewBeaconBlock()

View File

@@ -32,6 +32,7 @@ import (
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/shared"
"github.com/prysmaticlabs/prysm/shared/abool"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/runutil"
"github.com/prysmaticlabs/prysm/shared/slotutil"
@@ -150,9 +151,7 @@ func NewService(ctx context.Context, cfg *Config) *Service {
// Start the regular sync service.
func (s *Service) Start() {
if err := s.initCaches(); err != nil {
panic(err)
}
s.initCaches()
s.cfg.P2P.AddConnectionHandler(s.reValidatePeer, s.sendGoodbye)
s.cfg.P2P.AddDisconnectionHandler(func(_ context.Context, _ peer.ID) error {
@@ -203,45 +202,15 @@ func (s *Service) Status() error {
// This initializes the caches to update seen beacon objects coming in from the wire
// and prevent DoS.
func (s *Service) initCaches() error {
blkCache, err := lru.New(seenBlockSize)
if err != nil {
return err
}
aggregatedAttCache, err := lru.New(seenAggregatedAttSize)
if err != nil {
return err
}
unAggregatedAttCache, err := lru.New(seenUnaggregatedAttSize)
if err != nil {
return err
}
syncMsgCache, err := lru.New(seenSyncMsgSize)
if err != nil {
return err
}
exitCache, err := lru.New(seenExitSize)
if err != nil {
return err
}
proposerSlashingCache, err := lru.New(seenProposerSlashingSize)
if err != nil {
return err
}
badBlockCache, err := lru.New(badBlockSize)
if err != nil {
return err
}
s.seenBlockCache = blkCache
s.seenAggregatedAttestationCache = aggregatedAttCache
s.seenUnAggregatedAttestationCache = unAggregatedAttCache
s.seenSyncMessageCache = syncMsgCache
s.seenExitCache = exitCache
func (s *Service) initCaches() {
s.seenBlockCache = lruwrpr.New(seenBlockSize)
s.seenAggregatedAttestationCache = lruwrpr.New(seenAggregatedAttSize)
s.seenUnAggregatedAttestationCache = lruwrpr.New(seenUnaggregatedAttSize)
s.seenSyncMessageCache = lruwrpr.New(seenSyncMsgSize)
s.seenExitCache = lruwrpr.New(seenExitSize)
s.seenAttesterSlashingCache = make(map[uint64]bool)
s.seenProposerSlashingCache = proposerSlashingCache
s.badBlockCache = badBlockCache
return nil
s.seenProposerSlashingCache = lruwrpr.New(seenProposerSlashingSize)
s.badBlockCache = lruwrpr.New(badBlockSize)
}
func (s *Service) registerHandlers() {

View File

@@ -4,25 +4,23 @@ import (
"context"
"testing"
lru "github.com/hashicorp/golang-lru"
"github.com/prysmaticlabs/go-bitfield"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestBeaconAggregateProofSubscriber_CanSaveAggregatedAttestation(t *testing.T) {
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
AttPool: attestations.NewPool(),
OperationNotifier: (&mock.ChainService{}).OperationNotifier(),
},
seenUnAggregatedAttestationCache: c,
seenUnAggregatedAttestationCache: lruwrpr.New(10),
}
a := &ethpb.SignedAggregateAttestationAndProof{
@@ -39,14 +37,12 @@ func TestBeaconAggregateProofSubscriber_CanSaveAggregatedAttestation(t *testing.
}
func TestBeaconAggregateProofSubscriber_CanSaveUnaggregatedAttestation(t *testing.T) {
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
AttPool: attestations.NewPool(),
OperationNotifier: (&mock.ChainService{}).OperationNotifier(),
},
seenUnAggregatedAttestationCache: c,
seenUnAggregatedAttestationCache: lruwrpr.New(10),
}
a := &ethpb.SignedAggregateAttestationAndProof{

View File

@@ -86,7 +86,7 @@ func TestService_beaconBlockSubscriber(t *testing.T) {
AttPool: attestations.NewPool(),
},
}
assert.NoError(t, s.initCaches())
s.initCaches()
// Set up attestation pool.
for _, att := range pooledAttestations {
if helpers.IsAggregated(att) {

View File

@@ -8,7 +8,6 @@ import (
"testing"
"time"
lru "github.com/hashicorp/golang-lru"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
@@ -23,6 +22,7 @@ import (
pb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/abool"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
@@ -169,8 +169,6 @@ func TestSubscribe_ReceivesProposerSlashing(t *testing.T) {
Genesis: time.Now(),
}
d := db.SetupDB(t)
c, err := lru.New(10)
require.NoError(t, err)
r := Service{
ctx: ctx,
cfg: &Config{
@@ -180,7 +178,7 @@ func TestSubscribe_ReceivesProposerSlashing(t *testing.T) {
Chain: chainService,
DB: d,
},
seenProposerSlashingCache: c,
seenProposerSlashingCache: lruwrpr.New(10),
chainStarted: abool.New(),
subHandler: newSubTopicHandler(),
}

View File

@@ -7,7 +7,6 @@ import (
"testing"
"time"
lru "github.com/hashicorp/golang-lru"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
types "github.com/prysmaticlabs/eth2-types"
@@ -24,6 +23,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/attestationutil"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
@@ -111,8 +111,7 @@ func TestValidateAggregateAndProof_NoBlock(t *testing.T) {
}
signedAggregateAndProof := &ethpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof, Signature: make([]byte, 96)}
c, err := lru.New(10)
require.NoError(t, err)
c := lruwrpr.New(10)
r := &Service{
cfg: &Config{
P2P: p,
@@ -124,11 +123,10 @@ func TestValidateAggregateAndProof_NoBlock(t *testing.T) {
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
seenAggregatedAttestationCache: c,
}
err = r.initCaches()
require.NoError(t, err)
r.initCaches()
buf := new(bytes.Buffer)
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
_, err := p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
require.NoError(t, err)
topic := p2p.GossipTypeMapping[reflect.TypeOf(signedAggregateAndProof)]
@@ -180,8 +178,6 @@ func TestValidateAggregateAndProof_NotWithinSlotRange(t *testing.T) {
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
P2P: p,
@@ -194,10 +190,9 @@ func TestValidateAggregateAndProof_NotWithinSlotRange(t *testing.T) {
AttPool: attestations.NewPool(),
OperationNotifier: (&mock.ChainService{}).OperationNotifier(),
},
seenAggregatedAttestationCache: c,
seenAggregatedAttestationCache: lruwrpr.New(10),
}
err = r.initCaches()
require.NoError(t, err)
r.initCaches()
buf := new(bytes.Buffer)
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
@@ -264,8 +259,6 @@ func TestValidateAggregateAndProof_ExistedInPool(t *testing.T) {
signedAggregateAndProof := &ethpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof, Signature: make([]byte, 96)}
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
AttPool: attestations.NewPool(),
@@ -276,11 +269,10 @@ func TestValidateAggregateAndProof_ExistedInPool(t *testing.T) {
State: beaconState},
OperationNotifier: (&mock.ChainService{}).OperationNotifier(),
},
seenAggregatedAttestationCache: c,
seenAggregatedAttestationCache: lruwrpr.New(10),
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
}
err = r.initCaches()
require.NoError(t, err)
r.initCaches()
buf := new(bytes.Buffer)
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
@@ -355,8 +347,6 @@ func TestValidateAggregateAndProof_CanValidate(t *testing.T) {
require.NoError(t, err)
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
P2P: p,
@@ -372,10 +362,9 @@ func TestValidateAggregateAndProof_CanValidate(t *testing.T) {
AttPool: attestations.NewPool(),
OperationNotifier: (&mock.ChainService{}).OperationNotifier(),
},
seenAggregatedAttestationCache: c,
seenAggregatedAttestationCache: lruwrpr.New(10),
}
err = r.initCaches()
require.NoError(t, err)
r.initCaches()
buf := new(bytes.Buffer)
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
@@ -447,8 +436,6 @@ func TestVerifyIndexInCommittee_SeenAggregatorEpoch(t *testing.T) {
require.NoError(t, err)
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
P2P: p,
@@ -466,10 +453,9 @@ func TestVerifyIndexInCommittee_SeenAggregatorEpoch(t *testing.T) {
AttPool: attestations.NewPool(),
OperationNotifier: (&mock.ChainService{}).OperationNotifier(),
},
seenAggregatedAttestationCache: c,
seenAggregatedAttestationCache: lruwrpr.New(10),
}
err = r.initCaches()
require.NoError(t, err)
r.initCaches()
buf := new(bytes.Buffer)
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)
@@ -559,8 +545,6 @@ func TestValidateAggregateAndProof_BadBlock(t *testing.T) {
require.NoError(t, err)
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
P2P: p,
@@ -575,10 +559,9 @@ func TestValidateAggregateAndProof_BadBlock(t *testing.T) {
AttPool: attestations.NewPool(),
OperationNotifier: (&mock.ChainService{}).OperationNotifier(),
},
seenAggregatedAttestationCache: c,
seenAggregatedAttestationCache: lruwrpr.New(10),
}
err = r.initCaches()
require.NoError(t, err)
r.initCaches()
// Set beacon block as bad.
r.setBadBlock(context.Background(), root)
buf := new(bytes.Buffer)
@@ -651,8 +634,6 @@ func TestValidateAggregateAndProof_RejectWhenAttEpochDoesntEqualTargetEpoch(t *t
require.NoError(t, err)
require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
P2P: p,
@@ -668,10 +649,9 @@ func TestValidateAggregateAndProof_RejectWhenAttEpochDoesntEqualTargetEpoch(t *t
AttPool: attestations.NewPool(),
OperationNotifier: (&mock.ChainService{}).OperationNotifier(),
},
seenAggregatedAttestationCache: c,
seenAggregatedAttestationCache: lruwrpr.New(10),
}
err = r.initCaches()
require.NoError(t, err)
r.initCaches()
buf := new(bytes.Buffer)
_, err = p.Encoding().EncodeGossip(buf, signedAggregateAndProof)

View File

@@ -7,7 +7,6 @@ import (
"testing"
"time"
lru "github.com/hashicorp/golang-lru"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
"github.com/prysmaticlabs/go-bitfield"
@@ -19,6 +18,7 @@ import (
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
@@ -35,8 +35,6 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) {
ValidAttestation: true,
}
c, err := lru.New(10)
require.NoError(t, err)
s := &Service{
cfg: &Config{
InitialSync: &mockSync.Sync{IsSyncing: false},
@@ -46,10 +44,9 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) {
OperationNotifier: (&mockChain.ChainService{}).OperationNotifier(),
},
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
seenUnAggregatedAttestationCache: c,
seenUnAggregatedAttestationCache: lruwrpr.New(10),
}
err = s.initCaches()
require.NoError(t, err)
s.initCaches()
invalidRoot := [32]byte{'A', 'B', 'C', 'D'}
s.setBadBlock(ctx, invalidRoot)

View File

@@ -8,7 +8,6 @@ import (
"testing"
"time"
lru "github.com/hashicorp/golang-lru"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
gcache "github.com/patrickmn/go-cache"
@@ -28,6 +27,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/abool"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
@@ -60,10 +60,6 @@ func TestValidateBeaconBlockPubSub_InvalidSignature(t *testing.T) {
msg.Block.ProposerIndex = proposerIdx
msg.Signature = bytesutil.PadTo([]byte("fake"), 96)
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
stateGen := stategen.New(db)
chainService := &mock.ChainService{Genesis: time.Unix(time.Now().Unix()-int64(params.BeaconConfig().SecondsPerSlot), 0),
FinalizedCheckPoint: &ethpb.Checkpoint{
@@ -79,8 +75,8 @@ func TestValidateBeaconBlockPubSub_InvalidSignature(t *testing.T) {
BlockNotifier: chainService.BlockNotifier(),
StateGen: stateGen,
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
}
buf := new(bytes.Buffer)
@@ -107,10 +103,6 @@ func TestValidateBeaconBlockPubSub_BlockAlreadyPresentInDB(t *testing.T) {
msg.Block.ParentRoot = testutil.Random32Bytes(t)
require.NoError(t, db.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(msg)))
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
chainService := &mock.ChainService{Genesis: time.Now()}
r := &Service{
cfg: &Config{
@@ -120,12 +112,12 @@ func TestValidateBeaconBlockPubSub_BlockAlreadyPresentInDB(t *testing.T) {
Chain: chainService,
BlockNotifier: chainService.BlockNotifier(),
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
}
buf := new(bytes.Buffer)
_, err = p.Encoding().EncodeGossip(buf, msg)
_, err := p.Encoding().EncodeGossip(buf, msg)
require.NoError(t, err)
topic := p2p.GossipTypeMapping[reflect.TypeOf(msg)]
@@ -160,10 +152,6 @@ func TestValidateBeaconBlockPubSub_CanRecoverStateSummary(t *testing.T) {
msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx])
require.NoError(t, err)
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
stateGen := stategen.New(db)
chainService := &mock.ChainService{Genesis: time.Unix(time.Now().Unix()-int64(params.BeaconConfig().SecondsPerSlot), 0),
State: beaconState,
@@ -181,8 +169,8 @@ func TestValidateBeaconBlockPubSub_CanRecoverStateSummary(t *testing.T) {
BlockNotifier: chainService.BlockNotifier(),
StateGen: stateGen,
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
@@ -223,10 +211,6 @@ func TestValidateBeaconBlockPubSub_ValidProposerSignature(t *testing.T) {
msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx])
require.NoError(t, err)
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
stateGen := stategen.New(db)
chainService := &mock.ChainService{Genesis: time.Unix(time.Now().Unix()-int64(params.BeaconConfig().SecondsPerSlot), 0),
State: beaconState,
@@ -244,8 +228,8 @@ func TestValidateBeaconBlockPubSub_ValidProposerSignature(t *testing.T) {
BlockNotifier: chainService.BlockNotifier(),
StateGen: stateGen,
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
@@ -289,10 +273,6 @@ func TestValidateBeaconBlockPubSub_WithLookahead(t *testing.T) {
msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx])
require.NoError(t, err)
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
stateGen := stategen.New(db)
offset := int64(blkSlot.Mul(params.BeaconConfig().SecondsPerSlot))
chainService := &mock.ChainService{Genesis: time.Unix(time.Now().Unix()-offset, 0),
@@ -309,8 +289,8 @@ func TestValidateBeaconBlockPubSub_WithLookahead(t *testing.T) {
BlockNotifier: chainService.BlockNotifier(),
StateGen: stateGen,
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
@@ -354,10 +334,6 @@ func TestValidateBeaconBlockPubSub_AdvanceEpochsForState(t *testing.T) {
msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx])
require.NoError(t, err)
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
stateGen := stategen.New(db)
offset := int64(blkSlot.Mul(params.BeaconConfig().SecondsPerSlot))
chainService := &mock.ChainService{Genesis: time.Unix(time.Now().Unix()-offset, 0),
@@ -374,8 +350,8 @@ func TestValidateBeaconBlockPubSub_AdvanceEpochsForState(t *testing.T) {
BlockNotifier: chainService.BlockNotifier(),
StateGen: stateGen,
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
@@ -458,10 +434,6 @@ func TestValidateBeaconBlockPubSub_AcceptBlocksFromNearFuture(t *testing.T) {
msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx])
require.NoError(t, err)
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
stateGen := stategen.New(db)
chainService := &mock.ChainService{Genesis: time.Now(),
FinalizedCheckPoint: &ethpb.Checkpoint{
@@ -478,8 +450,8 @@ func TestValidateBeaconBlockPubSub_AcceptBlocksFromNearFuture(t *testing.T) {
StateGen: stateGen,
},
chainStarted: abool.New(),
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
@@ -514,10 +486,6 @@ func TestValidateBeaconBlockPubSub_RejectBlocksFromFuture(t *testing.T) {
msg.Block.ParentRoot = testutil.Random32Bytes(t)
msg.Signature = sk.Sign([]byte("data")).Marshal()
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
chainService := &mock.ChainService{Genesis: time.Now()}
r := &Service{
cfg: &Config{
@@ -528,8 +496,8 @@ func TestValidateBeaconBlockPubSub_RejectBlocksFromFuture(t *testing.T) {
BlockNotifier: chainService.BlockNotifier(),
},
chainStarted: abool.New(),
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
@@ -562,10 +530,6 @@ func TestValidateBeaconBlockPubSub_RejectBlocksFromThePast(t *testing.T) {
msg.Signature = sk.Sign([]byte("data")).Marshal()
genesisTime := time.Now()
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
chainService := &mock.ChainService{
Genesis: time.Unix(genesisTime.Unix()-1000, 0),
FinalizedCheckPoint: &ethpb.Checkpoint{
@@ -580,8 +544,8 @@ func TestValidateBeaconBlockPubSub_RejectBlocksFromThePast(t *testing.T) {
Chain: chainService,
BlockNotifier: chainService.BlockNotifier(),
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
}
buf := new(bytes.Buffer)
@@ -618,10 +582,6 @@ func TestValidateBeaconBlockPubSub_SeenProposerSlot(t *testing.T) {
msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx])
require.NoError(t, err)
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
chainService := &mock.ChainService{Genesis: time.Unix(time.Now().Unix()-int64(params.BeaconConfig().SecondsPerSlot), 0),
State: beaconState,
FinalizedCheckPoint: &ethpb.Checkpoint{
@@ -637,8 +597,8 @@ func TestValidateBeaconBlockPubSub_SeenProposerSlot(t *testing.T) {
Chain: chainService,
BlockNotifier: chainService.BlockNotifier(),
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
@@ -672,10 +632,7 @@ func TestValidateBeaconBlockPubSub_FilterByFinalizedEpoch(t *testing.T) {
FinalizedCheckPoint: &ethpb.Checkpoint{
Epoch: 1,
}}
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
DB: db,
@@ -685,8 +642,8 @@ func TestValidateBeaconBlockPubSub_FilterByFinalizedEpoch(t *testing.T) {
AttPool: attestations.NewPool(),
InitialSync: &mockSync.Sync{IsSyncing: false},
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
}
b := testutil.NewBeaconBlock()
@@ -743,10 +700,6 @@ func TestValidateBeaconBlockPubSub_ParentNotFinalizedDescendant(t *testing.T) {
msg.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, msg.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx])
require.NoError(t, err)
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
stateGen := stategen.New(db)
chainService := &mock.ChainService{Genesis: time.Unix(time.Now().Unix()-int64(params.BeaconConfig().SecondsPerSlot), 0),
State: beaconState,
@@ -765,8 +718,8 @@ func TestValidateBeaconBlockPubSub_ParentNotFinalizedDescendant(t *testing.T) {
BlockNotifier: chainService.BlockNotifier(),
StateGen: stateGen,
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
@@ -811,10 +764,6 @@ func TestValidateBeaconBlockPubSub_InvalidParentBlock(t *testing.T) {
currBlockRoot, err := msg.Block.HashTreeRoot()
require.NoError(t, err)
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
stateGen := stategen.New(db)
chainService := &mock.ChainService{Genesis: time.Unix(time.Now().Unix()-int64(params.BeaconConfig().SecondsPerSlot), 0),
State: beaconState,
@@ -830,8 +779,8 @@ func TestValidateBeaconBlockPubSub_InvalidParentBlock(t *testing.T) {
BlockNotifier: chainService.BlockNotifier(),
StateGen: stateGen,
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
@@ -910,10 +859,6 @@ func TestValidateBeaconBlockPubSub_RejectEvilBlocksFromFuture(t *testing.T) {
require.NoError(t, err)
genesisTime := time.Now()
c, err := lru.New(10)
require.NoError(t, err)
c2, err := lru.New(10)
require.NoError(t, err)
stateGen := stategen.New(db)
chainService := &mock.ChainService{
@@ -931,8 +876,8 @@ func TestValidateBeaconBlockPubSub_RejectEvilBlocksFromFuture(t *testing.T) {
BlockNotifier: chainService.BlockNotifier(),
StateGen: stateGen,
},
seenBlockCache: c,
badBlockCache: c2,
seenBlockCache: lruwrpr.New(10),
badBlockCache: lruwrpr.New(10),
slotToPendingBlocks: gcache.New(time.Second, 2*time.Second),
seenPendingBlocks: make(map[[32]byte]bool),
}
@@ -953,7 +898,7 @@ func TestValidateBeaconBlockPubSub_RejectEvilBlocksFromFuture(t *testing.T) {
func TestService_setBadBlock_DoesntSetWithContextErr(t *testing.T) {
s := Service{}
require.NoError(t, s.initCaches())
s.initCaches()
root := [32]byte{'b', 'a', 'd'}
ctx, cancel := context.WithCancel(context.Background())

View File

@@ -8,7 +8,6 @@ import (
"testing"
"time"
lru "github.com/hashicorp/golang-lru"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
types "github.com/prysmaticlabs/eth2-types"
@@ -23,6 +22,7 @@ import (
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
statepb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bls"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
@@ -113,19 +113,17 @@ func TestValidateProposerSlashing_ValidSlashing(t *testing.T) {
slashing, s := setupValidProposerSlashing(t)
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
P2P: p,
Chain: &mock.ChainService{State: s},
InitialSync: &mockSync.Sync{IsSyncing: false},
},
seenProposerSlashingCache: c,
seenProposerSlashingCache: lruwrpr.New(10),
}
buf := new(bytes.Buffer)
_, err = p.Encoding().EncodeGossip(buf, slashing)
_, err := p.Encoding().EncodeGossip(buf, slashing)
require.NoError(t, err)
topic := p2p.GossipTypeMapping[reflect.TypeOf(slashing)]
m := &pubsub.Message{
@@ -152,15 +150,13 @@ func TestValidateProposerSlashing_ContextTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
P2P: p,
Chain: &mock.ChainService{State: state},
InitialSync: &mockSync.Sync{IsSyncing: false},
},
seenProposerSlashingCache: c,
seenProposerSlashingCache: lruwrpr.New(10),
}
buf := new(bytes.Buffer)

View File

@@ -71,7 +71,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
s.cfg.StateGen = stategen.New(beaconDB)
msg.BlockRoot = headRoot[:]
s.cfg.DB = beaconDB
assert.NoError(t, s.initCaches())
s.initCaches()
return s, topic
},
args: args{
@@ -99,7 +99,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
s.cfg.StateGen = stategen.New(beaconDB)
msg.BlockRoot = headRoot[:]
s.cfg.DB = beaconDB
assert.NoError(t, s.initCaches())
s.initCaches()
return s, topic
},
args: args{
@@ -126,7 +126,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
setupSvc: func(s *Service, msg *ethpb.SyncCommitteeMessage, topic string) (*Service, string) {
s.cfg.StateGen = stategen.New(beaconDB)
s.cfg.DB = beaconDB
assert.NoError(t, s.initCaches())
s.initCaches()
return s, topic
},
args: args{
@@ -153,7 +153,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
setupSvc: func(s *Service, msg *ethpb.SyncCommitteeMessage, topic string) (*Service, string) {
s.cfg.StateGen = stategen.New(beaconDB)
s.cfg.DB = beaconDB
assert.NoError(t, s.initCaches())
s.initCaches()
s.setSeenSyncMessageIndexSlot(1, 1, 0)
return s, topic
@@ -182,7 +182,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
setupSvc: func(s *Service, msg *ethpb.SyncCommitteeMessage, topic string) (*Service, string) {
s.cfg.StateGen = stategen.New(beaconDB)
s.cfg.DB = beaconDB
assert.NoError(t, s.initCaches())
s.initCaches()
s.cfg.Chain = &mockChain.ChainService{
ValidatorsRoot: [32]byte{'A'},
Genesis: time.Now().Add(-time.Second * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Duration(10)),
@@ -216,7 +216,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
setupSvc: func(s *Service, msg *ethpb.SyncCommitteeMessage, topic string) (*Service, string) {
s.cfg.StateGen = stategen.New(beaconDB)
s.cfg.DB = beaconDB
assert.NoError(t, s.initCaches())
s.initCaches()
msg.BlockRoot = headRoot[:]
hState, err := beaconDB.State(context.Background(), headRoot)
assert.NoError(t, err)
@@ -264,7 +264,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
setupSvc: func(s *Service, msg *ethpb.SyncCommitteeMessage, topic string) (*Service, string) {
s.cfg.StateGen = stategen.New(beaconDB)
s.cfg.DB = beaconDB
assert.NoError(t, s.initCaches())
s.initCaches()
msg.BlockRoot = headRoot[:]
hState, err := beaconDB.State(context.Background(), headRoot)
assert.NoError(t, err)
@@ -307,7 +307,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
setupSvc: func(s *Service, msg *ethpb.SyncCommitteeMessage, topic string) (*Service, string) {
s.cfg.StateGen = stategen.New(beaconDB)
s.cfg.DB = beaconDB
assert.NoError(t, s.initCaches())
s.initCaches()
msg.BlockRoot = headRoot[:]
hState, err := beaconDB.State(context.Background(), headRoot)
assert.NoError(t, err)
@@ -362,7 +362,7 @@ func TestService_ValidateSyncCommitteeMessage(t *testing.T) {
setupSvc: func(s *Service, msg *ethpb.SyncCommitteeMessage, topic string) (*Service, string) {
s.cfg.StateGen = stategen.New(beaconDB)
s.cfg.DB = beaconDB
assert.NoError(t, s.initCaches())
s.initCaches()
msg.BlockRoot = headRoot[:]
hState, err := beaconDB.State(context.Background(), headRoot)
assert.NoError(t, err)
@@ -442,7 +442,7 @@ func TestService_ignoreHasSeenSyncMsg(t *testing.T) {
{
name: "has seen",
setupSvc: func(s *Service, msg *ethpb.SyncCommitteeMessage, topic string) (*Service, string) {
assert.NoError(t, s.initCaches())
s.initCaches()
s.setSeenSyncMessageIndexSlot(1, 0, 0)
return s, ""
},
@@ -453,7 +453,7 @@ func TestService_ignoreHasSeenSyncMsg(t *testing.T) {
{
name: "has not seen",
setupSvc: func(s *Service, msg *ethpb.SyncCommitteeMessage, topic string) (*Service, string) {
assert.NoError(t, s.initCaches())
s.initCaches()
s.setSeenSyncMessageIndexSlot(1, 0, 0)
return s, ""
},

View File

@@ -7,7 +7,6 @@ import (
"reflect"
"testing"
lru "github.com/hashicorp/golang-lru"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
@@ -20,6 +19,7 @@ import (
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
statepb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bls"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
@@ -73,8 +73,6 @@ func TestValidateVoluntaryExit_ValidExit(t *testing.T) {
exit, s := setupValidExit(t)
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
P2P: p,
@@ -83,11 +81,11 @@ func TestValidateVoluntaryExit_ValidExit(t *testing.T) {
},
InitialSync: &mockSync.Sync{IsSyncing: false},
},
seenExitCache: c,
seenExitCache: lruwrpr.New(10),
}
buf := new(bytes.Buffer)
_, err = p.Encoding().EncodeGossip(buf, exit)
_, err := p.Encoding().EncodeGossip(buf, exit)
require.NoError(t, err)
topic := p2p.GossipTypeMapping[reflect.TypeOf(exit)]
m := &pubsub.Message{
@@ -108,8 +106,6 @@ func TestValidateVoluntaryExit_InvalidExitSlot(t *testing.T) {
exit, s := setupValidExit(t)
// Set state slot to 1 to cause exit object fail to verify.
require.NoError(t, s.SetSlot(1))
c, err := lru.New(10)
require.NoError(t, err)
r := &Service{
cfg: &Config{
P2P: p,
@@ -118,11 +114,11 @@ func TestValidateVoluntaryExit_InvalidExitSlot(t *testing.T) {
},
InitialSync: &mockSync.Sync{IsSyncing: false},
},
seenExitCache: c,
seenExitCache: lruwrpr.New(10),
}
buf := new(bytes.Buffer)
_, err = p.Encoding().EncodeGossip(buf, exit)
_, err := p.Encoding().EncodeGossip(buf, exit)
require.NoError(t, err)
topic := p2p.GossipTypeMapping[reflect.TypeOf(exit)]
m := &pubsub.Message{

View File

@@ -169,9 +169,7 @@ func BeaconFuzzBlock(b []byte) {
StateGen: sgen,
})
if err := s.InitCaches(); err != nil {
panic(err)
}
s.InitCaches()
buf := new(bytes.Buffer)
_, err = p2p.Encoding().EncodeGossip(buf, input.Block)

View File

@@ -10,6 +10,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//shared/event:go_default_library",
"//shared/lru:go_default_library",
"//shared/params:go_default_library",
"//shared/rand:go_default_library",
"@com_github_hashicorp_golang_lru//:go_default_library",

View File

@@ -5,6 +5,7 @@ import (
lru "github.com/hashicorp/golang-lru"
"github.com/prysmaticlabs/prysm/shared/event"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/rand"
)
@@ -35,13 +36,9 @@ type StreamServer struct {
// NewStreamServer initializes a new stream server capable of
// streaming log events.
func NewStreamServer() *StreamServer {
c, err := lru.New(logCacheSize)
if err != nil {
panic(err) // This can only occur when the LogCacheSize is negative.
}
ss := &StreamServer{
feed: new(event.Feed),
cache: c,
cache: lruwrpr.New(logCacheSize),
}
addLogWriter(ss)
return ss

16
shared/lru/BUILD.bazel Normal file
View File

@@ -0,0 +1,16 @@
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["lru_wrpr.go"],
importpath = "github.com/prysmaticlabs/prysm/shared/lru",
visibility = ["//visibility:public"],
deps = ["@com_github_hashicorp_golang_lru//:go_default_library"],
)
go_test(
name = "go_default_test",
srcs = ["lru_wrpr_test.go"],
embed = [":go_default_library"],
deps = ["@com_github_stretchr_testify//assert:go_default_library"],
)

26
shared/lru/lru_wrpr.go Normal file
View File

@@ -0,0 +1,26 @@
package lru
import (
"fmt"
lru "github.com/hashicorp/golang-lru"
)
// New creates an LRU of the given size.
func New(size int) *lru.Cache {
cache, err := lru.New(size)
if err != nil {
panic(fmt.Errorf("lru new failed: %w", err))
}
return cache
}
// NewWithEvict constructs a fixed size cache with the given eviction
// callback.
func NewWithEvict(size int, onEvicted func(key interface{}, value interface{})) *lru.Cache {
cache, err := lru.NewWithEvict(size, onEvicted)
if err != nil {
panic(fmt.Errorf("lru new with evict failed: %w", err))
}
return cache
}

View File

@@ -0,0 +1,37 @@
package lru
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
assert.NotPanics(t, func() {
New(10)
})
}
func TestNew_ZeroOrNegativeSize(t *testing.T) {
assert.Panics(t, func() {
New(0)
})
assert.Panics(t, func() {
New(-1)
})
}
func TestNewWithEvict(t *testing.T) {
assert.NotPanics(t, func() {
NewWithEvict(10, func(key interface{}, value interface{}) {})
})
}
func TestNewWithEvict_ZeroOrNegativeSize(t *testing.T) {
assert.Panics(t, func() {
NewWithEvict(0, func(key interface{}, value interface{}) {})
})
assert.Panics(t, func() {
NewWithEvict(-1, func(key interface{}, value interface{}) {})
})
}

View File

@@ -14,6 +14,7 @@ go_library(
visibility = ["//slasher:__subpackages__"],
deps = [
"//proto/prysm/v1alpha1:go_default_library",
"//shared/lru:go_default_library",
"//slasher/detection/attestations/types:go_default_library",
"@com_github_hashicorp_golang_lru//:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",

View File

@@ -3,6 +3,7 @@ package cache
import (
lru "github.com/hashicorp/golang-lru"
types "github.com/prysmaticlabs/eth2-types"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
slashertypes "github.com/prysmaticlabs/prysm/slasher/detection/attestations/types"
)
@@ -16,11 +17,7 @@ func NewEpochFlatSpansCache(size int, onEvicted func(key interface{}, value inte
if size != 0 {
epochSpansCacheSize = size
}
cache, err := lru.NewWithEvict(epochSpansCacheSize, onEvicted)
if err != nil {
return nil, err
}
return &EpochFlatSpansCache{cache: cache}, nil
return &EpochFlatSpansCache{cache: lruwrpr.NewWithEvict(epochSpansCacheSize, onEvicted)}, nil
}
// Get returns an ok bool and the cached value for the requested epoch key, if any.

View File

@@ -3,6 +3,7 @@ package cache
import (
lru "github.com/hashicorp/golang-lru"
slashpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
)
var (
@@ -20,11 +21,7 @@ func NewHighestAttestationCache(size int, onEvicted func(key interface{}, value
if size != 0 {
highestAttCacheSize = size
}
cache, err := lru.NewWithEvict(highestAttCacheSize, onEvicted)
if err != nil {
return nil, err
}
return &HighestAttestationCache{cache: cache}, nil
return &HighestAttestationCache{cache: lruwrpr.NewWithEvict(highestAttCacheSize, onEvicted)}, nil
}
// Get returns an ok bool and the cached value for the requested validator id key, if any.

View File

@@ -5,6 +5,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
types "github.com/prysmaticlabs/eth2-types"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
)
var (
@@ -31,11 +32,7 @@ func NewPublicKeyCache(size int, onEvicted func(key interface{}, value interface
if size != 0 {
validatorsCacheSize = size
}
cache, err := lru.NewWithEvict(validatorsCacheSize, onEvicted)
if err != nil {
return nil, err
}
return &PublicKeyCache{cache: cache}, nil
return &PublicKeyCache{cache: lruwrpr.NewWithEvict(validatorsCacheSize, onEvicted)}, nil
}
// Get returns an ok bool and the cached value for the requested validator id key, if any.

View File

@@ -34,6 +34,7 @@ go_library(
"//shared/featureconfig:go_default_library",
"//shared/grpcutils:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/lru:go_default_library",
"//shared/mathutil:go_default_library",
"//shared/mputil:go_default_library",
"//shared/params:go_default_library",
@@ -112,6 +113,7 @@ go_test(
"//shared/event:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/fileutil:go_default_library",
"//shared/lru:go_default_library",
"//shared/mock:go_default_library",
"//shared/params:go_default_library",
"//shared/slotutil:go_default_library",
@@ -130,7 +132,6 @@ go_test(
"//validator/slashing-protection/local/standard-protection-format:go_default_library",
"//validator/testing:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_hashicorp_golang_lru//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",

View File

@@ -9,12 +9,12 @@ import (
"time"
"github.com/golang/mock/gomock"
lru "github.com/hashicorp/golang-lru"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/mock"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
@@ -73,8 +73,7 @@ func setupWithKey(t *testing.T, validatorKey bls.SecretKey) (*validator, *mocks,
},
}
aggregatedSlotCommitteeIDCache, err := lru.New(int(params.BeaconConfig().MaxCommitteesPerSlot))
require.NoError(t, err)
aggregatedSlotCommitteeIDCache := lruwrpr.New(int(params.BeaconConfig().MaxCommitteesPerSlot))
copy(pubKey[:], validatorKey.PublicKey().Marshal())
km := &mockKeymanager{
keysMap: map[[48]byte]bls.SecretKey{

View File

@@ -11,13 +11,13 @@ import (
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
lru "github.com/hashicorp/golang-lru"
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/grpcutils"
lruwrpr "github.com/prysmaticlabs/prysm/shared/lru"
"github.com/prysmaticlabs/prysm/shared/params"
accountsiface "github.com/prysmaticlabs/prysm/validator/accounts/iface"
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
@@ -155,11 +155,7 @@ func (v *ValidatorService) Start() {
panic(err)
}
aggregatedSlotCommitteeIDCache, err := lru.New(int(params.BeaconConfig().MaxCommitteesPerSlot))
if err != nil {
log.Errorf("Could not initialize cache: %v", err)
return
}
aggregatedSlotCommitteeIDCache := lruwrpr.New(int(params.BeaconConfig().MaxCommitteesPerSlot))
sPubKeys, err := v.db.EIPImportBlacklistedPublicKeys(v.ctx)
if err != nil {