nimbus-eth2/beacon_chain/validator_pool.nim

76 lines
2.6 KiB
Nim

import
tables,
chronos, chronicles,
spec/[datatypes, crypto, digest, helpers], ssz,
beacon_node_types
proc init*(T: type ValidatorPool): T =
result.validators = initTable[ValidatorPubKey, AttachedValidator]()
template count*(pool: ValidatorPool): int =
pool.validators.len
proc addLocalValidator*(pool: var ValidatorPool,
pubKey: ValidatorPubKey,
privKey: ValidatorPrivKey) =
let v = AttachedValidator(pubKey: pubKey,
kind: inProcess,
privKey: privKey)
pool.validators[pubKey] = v
info "Local validator attached", pubKey, validator = shortLog(v)
proc getValidator*(pool: ValidatorPool,
validatorKey: ValidatorPubKey): AttachedValidator =
pool.validators.getOrDefault(validatorKey)
proc signBlockProposal*(v: AttachedValidator, state: BeaconState, slot: Slot,
blockRoot: Eth2Digest): Future[ValidatorSig] {.async.} =
if v.kind == inProcess:
# TODO this is an ugly hack to fake a delay and subsequent async reordering
# for the purpose of testing the external validator delay - to be
# replaced by something more sensible
await sleepAsync(chronos.milliseconds(1))
let
domain =
get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_of_slot(slot))
result = bls_sign(v.privKey, blockRoot.data, domain)
else:
# TODO:
# send RPC
discard
proc signAttestation*(v: AttachedValidator,
attestation: AttestationData,
state: BeaconState): Future[ValidatorSig] {.async.} =
if v.kind == inProcess:
# TODO this is an ugly hack to fake a delay and subsequent async reordering
# for the purpose of testing the external validator delay - to be
# replaced by something more sensible
await sleepAsync(chronos.milliseconds(1))
let
attestationRoot = hash_tree_root(
AttestationDataAndCustodyBit(data: attestation, custody_bit: false))
domain = get_domain(state, DOMAIN_ATTESTATION, attestation.target.epoch)
result = bls_sign(v.privKey, attestationRoot.data, domain)
else:
# TODO:
# send RPC
discard
func genRandaoReveal*(k: ValidatorPrivKey, state: BeaconState, slot: Slot):
ValidatorSig =
let
domain = get_domain(state, DOMAIN_RANDAO, compute_epoch_of_slot(slot))
root = hash_tree_root(compute_epoch_of_slot(slot).uint64).data
bls_sign(k, root, domain)
func genRandaoReveal*(v: AttachedValidator, state: BeaconState, slot: Slot):
ValidatorSig =
genRandaoReveal(v.privKey, state, slot)