Skip to content

Commit

Permalink
Improve performance for test generation and execution (#359)
Browse files Browse the repository at this point in the history
* Add singleton pattern for TestingKeyManager

* Invert lock place

* Use fnv
  • Loading branch information
MatheusFranco99 authored Feb 12, 2024
1 parent 7e4ff9c commit 7635881
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions types/testingutils/keymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"crypto/rsa"
"encoding/hex"
"fmt"
"hash/fnv"
"sync"

spec "github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/bloxapp/ssv-spec/types"
Expand All @@ -25,11 +27,34 @@ type testingKeyManager struct {
slashableDataRoots [][]byte
}

var (
instancesMap = make(map[uint64]*testingKeyManager)
mu sync.Mutex
)

func getHash(data [][]byte) uint64 {
h := fnv.New64a()
for _, d := range data {
h.Write(d)
}
return h.Sum64()
}

func NewTestingKeyManager() *testingKeyManager {
return NewTestingKeyManagerWithSlashableRoots([][]byte{})
}

func NewTestingKeyManagerWithSlashableRoots(slashableDataRoots [][]byte) *testingKeyManager {

hash := getHash(slashableDataRoots)

mu.Lock()
defer mu.Unlock()

if instance, ok := instancesMap[hash]; ok {
return instance
}

ret := &testingKeyManager{
keys: map[string]*bls.SecretKey{},
ecdsaKeys: map[string]*ecdsa.PrivateKey{},
Expand Down Expand Up @@ -70,6 +95,9 @@ func NewTestingKeyManagerWithSlashableRoots(slashableDataRoots [][]byte) *testin
for _, o := range Testing13SharesSet().DKGOperators {
ret.ecdsaKeys[o.ETHAddress.String()] = o.SK
}

instancesMap[hash] = ret

return ret
}

Expand Down

0 comments on commit 7635881

Please sign in to comment.