-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
keystoretest.go
81 lines (70 loc) · 2.12 KB
/
keystoretest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package keystore
import (
"errors"
"sync"
"github.com/smartcontractkit/sqlx"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
// memoryORM is an in-memory version of the keystore. This is
// only intended to be used in tests to avoid DB lock contention on
// the single DB row that stores the key material.
//
// Note: we store `q` on the struct since `saveEncryptedKeyRing` needs
// to support DB callbacks.
type memoryORM struct {
keyRing *encryptedKeyRing
q pg.Queryer
mu sync.RWMutex
}
func (o *memoryORM) isEmpty() (bool, error) {
return false, nil
}
func (o *memoryORM) saveEncryptedKeyRing(kr *encryptedKeyRing, callbacks ...func(pg.Queryer) error) (err error) {
o.mu.Lock()
defer o.mu.Unlock()
o.keyRing = kr
for _, c := range callbacks {
err = errors.Join(err, c(o.q))
}
return
}
func (o *memoryORM) getEncryptedKeyRing() (encryptedKeyRing, error) {
o.mu.RLock()
defer o.mu.RUnlock()
if o.keyRing == nil {
return encryptedKeyRing{}, nil
}
return *o.keyRing, nil
}
func newInMemoryORM(q pg.Queryer) *memoryORM {
return &memoryORM{q: q}
}
// NewInMemory sets up a keystore which NOOPs attempts to access the `encrypted_key_rings` table. Accessing `evm.key_states`
// will still hit the DB.
func NewInMemory(db *sqlx.DB, scryptParams utils.ScryptParams, lggr logger.Logger, cfg pg.QConfig) *master {
dbORM := NewORM(db, lggr, cfg)
memoryORM := newInMemoryORM(dbORM.q)
km := &keyManager{
orm: memoryORM,
keystateORM: dbORM,
scryptParams: scryptParams,
lock: &sync.RWMutex{},
logger: lggr.Named("KeyStore"),
}
return &master{
keyManager: km,
cosmos: newCosmosKeyStore(km),
csa: newCSAKeyStore(km),
eth: newEthKeyStore(km, dbORM, dbORM.q),
ocr: newOCRKeyStore(km),
ocr2: newOCR2KeyStore(km),
p2p: newP2PKeyStore(km),
solana: newSolanaKeyStore(km),
starknet: newStarkNetKeyStore(km),
vrf: newVRFKeyStore(km),
dkgSign: newDKGSignKeyStore(km),
dkgEncrypt: newDKGEncryptKeyStore(km),
}
}