nimbus-eth2/beacon_chain/validator_pool.nim

85 lines
2.7 KiB
Nim
Raw Normal View History

import
tables, random,
chronos,
spec/[datatypes, crypto, digest, helpers], ssz
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
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]()
template count*(pool: ValidatorPool): int =
pool.validators.len
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
let v = AttachedValidator(idx: idx,
kind: inProcess,
privKey: privKey)
2018-12-08 14:17:47 +00:00
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)
2019-02-07 20:36:03 +00:00
proc signBlockProposal*(v: AttachedValidator, fork: Fork,
proposal: Proposal): Future[ValidatorSig] {.async.} =
2018-11-29 01:08:34 +00:00
if v.kind == inProcess:
await sleepAsync(1)
let proposalRoot = hash_tree_root_final(proposal)
2018-12-12 13:57:23 +00:00
result = bls_sign(v.privKey, signed_root(proposal, "signature"),
get_domain(fork, slot_to_epoch(proposal.slot), DOMAIN_PROPOSAL))
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)
2018-12-12 13:57:23 +00:00
let attestationRoot = hash_tree_root_final(attestation)
# TODO: Avoid the allocations belows
2019-01-09 01:01:07 +00:00
var dataToSign = @(attestationRoot.data) & @[0'u8]
# TODO: Use `domain` here
let domain = 0'u64
result = bls_sign(v.privKey, dataToSign, domain)
else:
# TODO:
# send RPC
discard
func genRandaoReveal*(k: ValidatorPrivKey, state: BeaconState, slot: Slot):
ValidatorSig =
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#randao
# Off-by-one? I often get slot == state.slot but the check was "assert slot > state.slot" (Mamy)
assert slot >= state.slot, "input slot: " & $humaneSlotNum(slot) & " - beacon state slot: " & $humaneSlotNum(state.slot)
bls_sign(k, int_to_bytes32(slot_to_epoch(slot)),
get_domain(state.fork, slot_to_epoch(slot), DOMAIN_RANDAO))
func genRandaoReveal*(v: AttachedValidator, state: BeaconState, slot: Slot):
ValidatorSig =
genRandaoReveal(v.privKey, state, slot)