Skip to content

Commit

Permalink
Add singleton pattern for TestingKeyManager
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusFranco99 committed Feb 1, 2024
1 parent d111858 commit 2545cfe
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions types/testingutils/keymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"crypto/ecdsa"
"crypto/rsa"
"crypto/sha256"
"encoding/hex"
"fmt"
"sync"

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

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

func getHash(data [][]byte) string {
hasher := sha256.New()
for _, d := range data {
hasher.Write(d)
}
return hex.EncodeToString(hasher.Sum(nil))
}

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

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

mu.Lock()
defer mu.Unlock()

hash := getHash(slashableDataRoots)
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 +94,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 2545cfe

Please sign in to comment.