nimbus-eth2/beacon_chain/validator_pool.nim

78 lines
2.8 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 01:08:34 +00:00
result.validators = initTable[ValidatorPubKey, AttachedValidator]()
template count*(pool: ValidatorPool): int =
pool.validators.len
proc addLocalValidator*(pool: var ValidatorPool,
2019-10-03 01:51:44 +00:00
idx: ValidatorIndex,
2018-11-29 01:08:34 +00:00
pubKey: ValidatorPubKey,
privKey: ValidatorPrivKey) =
2019-10-03 01:51:44 +00:00
let v = AttachedValidator(idx: idx,
pubKey: pubKey,
2018-12-08 14:17:47 +00:00
kind: inProcess,
privKey: privKey)
2018-12-08 14:17:47 +00:00
pool.validators[pubKey] = v
info "Local validator attached", pubKey, validator = shortLog(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, state: BeaconState, slot: Slot,
blockRoot: Eth2Digest): Future[ValidatorSig] {.async.} =
2018-11-29 01:08:34 +00:00
if v.kind == inProcess:
# TODO state might become invalid after any async calls - it's fragile to
# care about this in here
let
domain =
get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_of_slot(slot))
2019-09-08 22:35:13 +00:00
# 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
2019-03-29 14:45:38 +00:00
await sleepAsync(chronos.milliseconds(1))
2019-09-08 22:35:13 +00:00
result = bls_sign(v.privKey, blockRoot.data, domain)
else:
2019-09-08 18:27:09 +00:00
error "Unimplemented"
quit 1
proc signAttestation*(v: AttachedValidator,
attestation: AttestationData,
state: BeaconState): Future[ValidatorSig] {.async.} =
2018-11-29 01:08:34 +00:00
if v.kind == inProcess:
2019-09-08 22:35:13 +00:00
let
attestationRoot = hash_tree_root(
AttestationDataAndCustodyBit(data: attestation, custody_bit: false))
domain = get_domain(state, DOMAIN_ATTESTATION, attestation.target.epoch)
# 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))
2019-09-08 22:35:13 +00:00
result = bls_sign(v.privKey, attestationRoot.data, domain)
else:
2019-09-08 18:27:09 +00:00
error "Unimplemented"
quit 1
func genRandaoReveal*(k: ValidatorPrivKey, state: BeaconState, slot: Slot):
ValidatorSig =
2019-09-08 22:35:13 +00:00
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)