nimbus-eth2/beacon_chain/validator_pool.nim

74 lines
2.0 KiB
Nim
Raw Normal View History

import
tables, random,
asyncdispatch2,
2018-12-08 14:17:47 +00:00
spec/[datatypes, crypto, digest], randao
type
ValidatorKind = enum
inProcess
remote
ValidatorConnection = object
AttachedValidator* = ref object
2018-12-08 14:17:47 +00:00
idx*: int # index in the registry
case kind: ValidatorKind
of inProcess:
2018-11-29 01:08:34 +00:00
privKey: ValidatorPrivKey
2018-12-08 14:17:47 +00:00
randaoSecret: Randao
else:
connection: ValidatorConnection
ValidatorPool* = object
2018-11-29 01:08:34 +00:00
validators: Table[ValidatorPubKey, AttachedValidator]
proc init*(T: type ValidatorPool): T =
2018-11-29 01:08:34 +00:00
result.validators = initTable[ValidatorPubKey, AttachedValidator]()
proc addLocalValidator*(pool: var ValidatorPool,
2018-11-29 01:08:34 +00:00
idx: int,
pubKey: ValidatorPubKey,
privKey: ValidatorPrivKey,
2018-12-08 14:17:47 +00:00
randaoSecret: Randao) =
let v = AttachedValidator(idx: idx,
kind: inProcess,
privKey: privKey,
randaoSecret: randaoSecret)
pool.validators[pubKey] = v
2018-11-26 13:33:06 +00:00
proc getValidator*(pool: ValidatorPool,
2018-11-29 01:08:34 +00:00
validatorKey: ValidatorPubKey): AttachedValidator =
pool.validators.getOrDefault(validatorKey)
proc signBlockProposal*(v: AttachedValidator,
2018-11-29 01:08:34 +00:00
proposal: ProposalSignedData): Future[ValidatorSig] {.async.} =
if v.kind == inProcess:
await sleepAsync(1)
# TODO:
# return sign(proposal, v.privKey)
else:
# TODO:
# send RPC
discard
proc signAttestation*(v: AttachedValidator,
attestation: AttestationData): Future[ValidatorSig] {.async.} =
# TODO: implement this
2018-11-29 01:08:34 +00:00
if v.kind == inProcess:
await sleepAsync(1)
# TODO:
# return sign(proposal, v.privKey)
else:
# TODO:
# send RPC
discard
2018-12-08 14:17:47 +00:00
proc randaoReveal*(v: AttachedValidator, commitment: Eth2Digest): Future[Eth2Digest] {.async.} =
if v.kind == inProcess:
result = v.randaoSecret.reveal(commitment)
else:
# TODO:
# send RPC
discard