diff --git a/crypto/bls/blst/BUILD.bazel b/crypto/bls/blst/BUILD.bazel index 4d3248cb9..beb101ba1 100644 --- a/crypto/bls/blst/BUILD.bazel +++ b/crypto/bls/blst/BUILD.bazel @@ -125,6 +125,7 @@ go_library( "//config/fieldparams:go_default_library", "//config/params:go_default_library", "//crypto/rand:go_default_library", + "//cache/lru:go_default_library", "@com_github_dgraph_io_ristretto//:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_supranational_blst//:go_default_library", @@ -227,7 +228,7 @@ go_test( deps = [ ":go_default_library", "//crypto/bls/common:go_default_library", - "//encoding/bytesutil:go_default_library", "//crypto/hash:go_default_library", + "//encoding/bytesutil:go_default_library", ], ) diff --git a/crypto/bls/blst/public_key.go b/crypto/bls/blst/public_key.go index b594aefab..df41b96d3 100644 --- a/crypto/bls/blst/public_key.go +++ b/crypto/bls/blst/public_key.go @@ -7,19 +7,16 @@ package blst import ( "fmt" - "github.com/dgraph-io/ristretto" "github.com/pkg/errors" + lruwrpr "github.com/prysmaticlabs/prysm/cache/lru" "github.com/prysmaticlabs/prysm/config/features" + fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" "github.com/prysmaticlabs/prysm/config/params" "github.com/prysmaticlabs/prysm/crypto/bls/common" ) -var maxKeys = int64(1000000) -var pubkeyCache, _ = ristretto.NewCache(&ristretto.Config{ - NumCounters: maxKeys, - MaxCost: 1 << 26, // ~64mb is cache max size - BufferItems: 64, -}) +var maxKeys = 1000000 +var pubkeyCache = lruwrpr.New(maxKeys) // PublicKey used in the BLS signature scheme. type PublicKey struct { @@ -34,7 +31,8 @@ func PublicKeyFromBytes(pubKey []byte) (common.PublicKey, error) { if len(pubKey) != params.BeaconConfig().BLSPubkeyLength { return nil, fmt.Errorf("public key must be %d bytes", params.BeaconConfig().BLSPubkeyLength) } - if cv, ok := pubkeyCache.Get(string(pubKey)); ok { + newKey := (*[fieldparams.BLSPubkeyLength]byte)(pubKey) + if cv, ok := pubkeyCache.Get(*newKey); ok { return cv.(*PublicKey).Copy(), nil } // Subgroup check NOT done when decompressing pubkey. @@ -49,7 +47,8 @@ func PublicKeyFromBytes(pubKey []byte) (common.PublicKey, error) { } pubKeyObj := &PublicKey{p: p} copiedKey := pubKeyObj.Copy() - pubkeyCache.Set(string(pubKey), copiedKey, 48) + cacheKey := *newKey + pubkeyCache.Add(cacheKey, copiedKey) return pubKeyObj, nil }