Integrate Read-Only-Lock-on-Get LRU Cache for Public Keys (#12646)

* use new lru cache

* update build

---------

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Raul Jordan 2023-07-22 00:37:53 -04:00 committed by GitHub
parent 9c250dd4c2
commit e9c8e84618
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 4 deletions

View File

@ -17,7 +17,7 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/v4/crypto/bls/blst",
visibility = ["//visibility:public"],
deps = [
"//cache/lru:go_default_library",
"//cache/nonblocking:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//crypto/bls/common:go_default_library",

View File

@ -3,8 +3,11 @@
package blst
import (
"fmt"
"runtime"
"github.com/prysmaticlabs/prysm/v4/cache/nonblocking"
"github.com/prysmaticlabs/prysm/v4/crypto/bls/common"
blst "github.com/supranational/blst/bindings/go"
)
@ -15,4 +18,10 @@ func init() {
maxProcs = 1
}
blst.SetMaxProcs(maxProcs)
onEvict := func(_ [48]byte, _ common.PublicKey) {}
keysCache, err := nonblocking.NewLRU(maxKeys, onEvict)
if err != nil {
panic(fmt.Sprintf("Could not initiate public keys cache: %v", err))
}
pubkeyCache = keysCache
}

View File

@ -6,14 +6,14 @@ import (
"fmt"
"github.com/pkg/errors"
lruwrpr "github.com/prysmaticlabs/prysm/v4/cache/lru"
"github.com/prysmaticlabs/prysm/v4/cache/nonblocking"
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
"github.com/prysmaticlabs/prysm/v4/config/params"
"github.com/prysmaticlabs/prysm/v4/crypto/bls/common"
)
var maxKeys = 1_000_000
var pubkeyCache = lruwrpr.New(maxKeys)
var pubkeyCache *nonblocking.LRU[[48]byte, common.PublicKey]
// PublicKey used in the BLS signature scheme.
type PublicKey struct {
@ -27,7 +27,7 @@ func PublicKeyFromBytes(pubKey []byte) (common.PublicKey, error) {
}
newKey := (*[fieldparams.BLSPubkeyLength]byte)(pubKey)
if cv, ok := pubkeyCache.Get(*newKey); ok {
return cv.(*PublicKey).Copy(), nil
return cv.Copy(), nil
}
// Subgroup check NOT done when decompressing pubkey.
p := new(blstPublicKey).Uncompress(pubKey)