Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add committees cache * committees cache usage * fix test * fix log * goimports * Merge branch 'master' of github.com:prysmaticlabs/prysm into slasher_committees_cache # Conflicts: # slasher/service/data_update.go * fix imports * fix comment * fix comment * Merge refs/heads/master into slasher_committees_cache * Merge refs/heads/master into slasher_committees_cache * Update slasher/cache/BUILD.bazel Co-Authored-By: Ivan Martinez <ivanthegreatdev@gmail.com> * Merge refs/heads/master into slasher_committees_cache * Merge refs/heads/master into slasher_committees_cache * Merge refs/heads/master into slasher_committees_cache * added in the service context * baz * Merge refs/heads/master into slasher_committees_cache * Merge refs/heads/master into slasher_committees_cache
- Loading branch information
Showing
8 changed files
with
210 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["committees_cache.go"], | ||
importpath = "github.com/prysmaticlabs/prysm/slasher/cache", | ||
visibility = ["//slasher:__subpackages__"], | ||
deps = [ | ||
"@com_github_hashicorp_golang_lru//:go_default_library", | ||
"@com_github_prometheus_client_golang//prometheus:go_default_library", | ||
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library", | ||
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = ["committees_cache_test.go"], | ||
embed = [":go_default_library"], | ||
deps = ["@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
lru "github.com/hashicorp/golang-lru" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
) | ||
|
||
var ( | ||
// Metrics | ||
committeesCacheHit = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: "committees_cache_hit", | ||
Help: "The total number of cache hits on the committees cache.", | ||
}) | ||
committeesCacheMiss = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: "committees_cache_miss", | ||
Help: "The total number of cache misses on the committees cache.", | ||
}) | ||
) | ||
|
||
// CommitteesCache is used to store the cached results of committees for epoch. | ||
type CommitteesCache struct { | ||
cache *lru.Cache | ||
lock sync.RWMutex | ||
} | ||
|
||
// NewCommitteesCache initializes the map and underlying cache. | ||
func NewCommitteesCache() *CommitteesCache { | ||
cache, err := lru.New(50) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return &CommitteesCache{ | ||
cache: cache, | ||
} | ||
} | ||
|
||
// Get returns the cached response. | ||
func (c *CommitteesCache) Get(ctx context.Context, epoch uint64) (*ethpb.BeaconCommittees, error) { | ||
if ctx.Err() != nil { | ||
return nil, ctx.Err() | ||
} | ||
c.lock.RLock() | ||
|
||
item, exists := c.cache.Get(epoch) | ||
|
||
if exists && item != nil { | ||
committeesCacheHit.Inc() | ||
return item.(*ethpb.BeaconCommittees), nil | ||
} | ||
committeesCacheMiss.Inc() | ||
return nil, nil | ||
} | ||
|
||
// Put the response in the cache. | ||
func (c *CommitteesCache) Put(ctx context.Context, epoch uint64, committees *ethpb.BeaconCommittees) error { | ||
c.cache.Add(epoch, committees) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cache_test | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/slasher/cache" | ||
) | ||
|
||
func TestCommitteesCache_RoundTrip(t *testing.T) { | ||
ctx := context.Background() | ||
c := cache.NewCommitteesCache() | ||
numValidators := 64 | ||
wanted := make(map[uint64]*ethpb.BeaconCommittees_CommitteesList) | ||
committeeItems := make([]*ethpb.BeaconCommittees_CommitteeItem, 1) | ||
committeeItems[0] = ðpb.BeaconCommittees_CommitteeItem{ValidatorIndices: []uint64{1, 2, 3}} | ||
wanted[0] = ðpb.BeaconCommittees_CommitteesList{Committees: committeeItems} | ||
wantedRes := ðpb.BeaconCommittees{ | ||
Epoch: 5, | ||
Committees: wanted, | ||
ActiveValidatorCount: uint64(numValidators), | ||
} | ||
|
||
committees, err := c.Get(ctx, 5) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if committees != nil { | ||
t.Errorf("Empty cache returned an object: %v", committees) | ||
} | ||
|
||
if err = c.Put(ctx, 5, wantedRes); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
res, err := c.Get(ctx, 5) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if !reflect.DeepEqual(wantedRes, res) { | ||
t.Error("Expected equal protos to return from cache") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.