nimbus-eth2/beacon_chain/validator_pool.nim

70 lines
2.4 KiB
Nim
Raw Normal View History

import
tables,
chronos, chronicles,
spec/[datatypes, crypto, digest, helpers], ssz,
beacon_node_types
proc init*(T: type ValidatorPool): T =
2018-11-29 03:08:34 +02:00
result.validators = initTable[ValidatorPubKey, AttachedValidator]()
template count*(pool: ValidatorPool): int =
pool.validators.len
proc addLocalValidator*(pool: var ValidatorPool,
2018-11-29 03:08:34 +02:00
pubKey: ValidatorPubKey,
privKey: ValidatorPrivKey) =
let v = AttachedValidator(pubKey: pubKey,
2018-12-08 16:17:47 +02:00
kind: inProcess,
privKey: privKey)
2018-12-08 16:17:47 +02:00
pool.validators[pubKey] = v
info "Local validator attached", pubKey, validator = shortLog(v)
2018-11-26 15:33:06 +02:00
proc getValidator*(pool: ValidatorPool,
2018-11-29 03:08:34 +02:00
validatorKey: ValidatorPubKey): AttachedValidator =
pool.validators.getOrDefault(validatorKey)
proc signBlockProposal*(v: AttachedValidator, state: BeaconState, slot: Slot,
blockRoot: Eth2Digest): Future[ValidatorSig] {.async.} =
2018-11-29 03:08:34 +02:00
if v.kind == inProcess:
2019-03-29 16:45:38 +02:00
await sleepAsync(chronos.milliseconds(1))
result = bls_sign(v.privKey, blockRoot.data,
get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_of_slot(slot)))
else:
# TODO:
# send RPC
discard
proc signAttestation*(v: AttachedValidator,
attestation: AttestationData,
state: BeaconState): Future[ValidatorSig] {.async.} =
# TODO: implement this
2018-11-29 03:08:34 +02:00
if v.kind == inProcess:
2019-03-29 16:45:38 +02:00
await sleepAsync(chronos.milliseconds(1))
2018-12-12 15:57:23 +02:00
let attestationRoot = hash_tree_root(attestation)
# TODO: Avoid the allocations belows
2019-01-09 03:01:07 +02:00
var dataToSign = @(attestationRoot.data) & @[0'u8]
let domain = get_domain(
state,
DOMAIN_ATTESTATION,
attestation.target.epoch
)
result = bls_sign(v.privKey, dataToSign, domain)
else:
# TODO:
# send RPC
discard
func genRandaoReveal*(k: ValidatorPrivKey, state: BeaconState, slot: Slot):
ValidatorSig =
2019-03-14 00:04:43 +01:00
# Off-by-one? I often get slot == state.slot but the check was "doAssert slot > state.slot" (Mamy)
2019-08-15 18:01:55 +02:00
doAssert slot >= state.slot, "input slot: " & $shortLog(slot) & " - beacon state slot: " & $shortLog(state.slot)
bls_sign(k, hash_tree_root(compute_epoch_of_slot(slot).uint64).data,
get_domain(state, DOMAIN_RANDAO, compute_epoch_of_slot(slot)))
func genRandaoReveal*(v: AttachedValidator, state: BeaconState, slot: Slot):
ValidatorSig =
genRandaoReveal(v.privKey, state, slot)