Skip to content

Commit

Permalink
Make KeystoreCache working with KeystoreManagement API. (#5443)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatfate authored Sep 21, 2023
1 parent 2895a9a commit 88565e6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
4 changes: 3 additions & 1 deletion beacon_chain/nimbus_beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ proc init*(T: type BeaconNode,
getStateField(dag.headState, genesis_validators_root)

let
keystoreCache = KeystoreCacheRef.init()
slashingProtectionDB =
SlashingProtectionDB.init(
getStateField(dag.headState, genesis_validators_root),
Expand All @@ -711,6 +712,7 @@ proc init*(T: type BeaconNode,
keymanagerHost = if keymanagerInitResult.server != nil:
newClone KeymanagerHost.init(
validatorPool,
keystoreCache,
rng,
keymanagerInitResult.token,
config.validatorsDir,
Expand Down Expand Up @@ -749,7 +751,7 @@ proc init*(T: type BeaconNode,
restServer: restServer,
keymanagerHost: keymanagerHost,
keymanagerServer: keymanagerInitResult.server,
keystoreCache: KeystoreCacheRef.init(),
keystoreCache: keystoreCache,
eventBus: eventBus,
gossipState: {},
blocksGossipState: {},
Expand Down
7 changes: 4 additions & 3 deletions beacon_chain/nimbus_validator_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,10 @@ proc asyncInit(vc: ValidatorClientRef): Future[ValidatorClientRef] {.async.} =
vc.blockService = await BlockServiceRef.init(vc)
vc.syncCommitteeService = await SyncCommitteeServiceRef.init(vc)
vc.keymanagerServer = keymanagerInitResult.server
if vc.keymanagerServer != nil:
if not(isNil(vc.keymanagerServer)):
vc.keymanagerHost = newClone KeymanagerHost.init(
validatorPool,
vc.keystoreCache,
vc.rng,
keymanagerInitResult.token,
vc.config.validatorsDir,
Expand Down Expand Up @@ -458,8 +459,8 @@ proc asyncRun*(vc: ValidatorClientRef) {.async.} =
vc.blockService.start()
vc.syncCommitteeService.start()

if not isNil(vc.keymanagerServer):
doAssert vc.keymanagerHost != nil
if not(isNil(vc.keymanagerServer)):
doAssert not(isNil(vc.keymanagerHost))
vc.keymanagerServer.router.installKeymanagerHandlers(vc.keymanagerHost[])
vc.keymanagerServer.start()

Expand Down
3 changes: 2 additions & 1 deletion beacon_chain/rpc/rest_key_management_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ proc installKeymanagerHandlers*(router: var RestRouter, host: KeymanagerHost) =
for index, item in request.keystores:
let res = importKeystore(host.validatorPool[], host.rng[],
host.validatorsDir, host.secretsDir,
item, request.passwords[index])
item, request.passwords[index],
host.keystoreCache)
if res.isErr():
let failure = res.error()
case failure.status
Expand Down
18 changes: 8 additions & 10 deletions beacon_chain/validators/keystore_management.nim
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type

KeymanagerHost* = object
validatorPool*: ref ValidatorPool
keystoreCache*: KeystoreCacheRef
rng*: ref HmacDrbgContext
keymanagerToken*: string
validatorsDir*: string
Expand Down Expand Up @@ -111,6 +112,7 @@ func dispose*(decryptor: var MultipleKeystoresDecryptor) =

func init*(T: type KeymanagerHost,
validatorPool: ref ValidatorPool,
keystoreCache: KeystoreCacheRef,
rng: ref HmacDrbgContext,
keymanagerToken: string,
validatorsDir: string,
Expand All @@ -123,6 +125,7 @@ func init*(T: type KeymanagerHost,
getForkFn: GetForkFn,
getGenesisFn: GetGenesisFn): T =
T(validatorPool: validatorPool,
keystoreCache: keystoreCache,
rng: rng,
keymanagerToken: keymanagerToken,
validatorsDir: validatorsDir,
Expand Down Expand Up @@ -1339,18 +1342,13 @@ proc importKeystore*(pool: var ValidatorPool,
rng: var HmacDrbgContext,
validatorsDir, secretsDir: string,
keystore: Keystore,
password: string): ImportResult[KeystoreData] {.
password: string,
cache: KeystoreCacheRef): ImportResult[KeystoreData] {.
raises: [].} =
let keypass = KeystorePass.init(password)
let privateKey =
block:
let res = decryptKeystore(keystore, keypass)
if res.isOk():
res.get()
else:
return err(
AddValidatorFailure.init(AddValidatorStatus.failed, res.error()))
let
keypass = KeystorePass.init(password)
privateKey = decryptKeystore(keystore, keypass, cache).valueOr:
return err(AddValidatorFailure.init(AddValidatorStatus.failed, error))
publicKey = privateKey.toPubKey()
keyName = publicKey.fsName
keystoreDir = validatorsDir / keyName
Expand Down

0 comments on commit 88565e6

Please sign in to comment.