-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BCF-2689] Use an in-memory keystore for tests (#10946)
- Remove unused migration methods from the keystore - Split the keystore ORM in two, between methods which access the `encrypted_key_rings` table, and methods which access `evm.key_states` - Create an in-memory version of the base ORM, which doesn't actually write to the DB. Methods which access `evm.key_states` are unaffected by this. This will prevent tests from competing to get a row-level lock on the singleton keystore record. - Start using this in-memory keystore in `cltest` and in tests generally. Note: tests in the keystore package themselves are unaffected by this, since we still want to have test coverage over the behaviour that accesses the DB.
- Loading branch information
1 parent
9cc576f
commit 30c6b4b
Showing
27 changed files
with
137 additions
and
465 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package keystore | ||
|
||
import ( | ||
"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) error { | ||
o.mu.Lock() | ||
defer o.mu.Unlock() | ||
o.keyRing = kr | ||
for _, c := range callbacks { | ||
c(o.q) | ||
} | ||
return nil | ||
} | ||
|
||
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), | ||
} | ||
} |
Oops, something went wrong.