2018-11-23 23:58:49 +00:00
|
|
|
import
|
2019-03-12 15:03:14 +00:00
|
|
|
tables,
|
2019-02-06 17:56:04 +00:00
|
|
|
chronos,
|
2019-03-12 15:03:14 +00:00
|
|
|
spec/[datatypes, crypto, helpers], ssz,
|
|
|
|
beacon_node_types
|
2018-11-23 23:58:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc init*(T: type ValidatorPool): T =
|
2018-11-29 01:08:34 +00:00
|
|
|
result.validators = initTable[ValidatorPubKey, AttachedValidator]()
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2018-12-19 12:58:53 +00:00
|
|
|
template count*(pool: ValidatorPool): int =
|
|
|
|
pool.validators.len
|
|
|
|
|
2018-11-23 23:58:49 +00:00
|
|
|
proc addLocalValidator*(pool: var ValidatorPool,
|
2018-11-29 01:08:34 +00:00
|
|
|
idx: int,
|
|
|
|
pubKey: ValidatorPubKey,
|
2019-02-14 21:41:04 +00:00
|
|
|
privKey: ValidatorPrivKey) =
|
2018-12-08 14:17:47 +00:00
|
|
|
let v = AttachedValidator(idx: idx,
|
|
|
|
kind: inProcess,
|
2019-02-14 21:41:04 +00:00
|
|
|
privKey: privKey)
|
2018-12-08 14:17:47 +00:00
|
|
|
pool.validators[pubKey] = v
|
2018-11-23 23:58:49 +00:00
|
|
|
|
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)
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-02-07 20:36:03 +00:00
|
|
|
proc signBlockProposal*(v: AttachedValidator, fork: Fork,
|
2019-03-11 15:33:24 +00:00
|
|
|
proposal: Proposal): Future[ValidatorSig] {.async.} =
|
2018-11-29 01:08:34 +00:00
|
|
|
if v.kind == inProcess:
|
2018-11-23 23:58:49 +00:00
|
|
|
await sleepAsync(1)
|
2018-12-21 23:47:55 +00:00
|
|
|
let proposalRoot = hash_tree_root_final(proposal)
|
2018-12-12 13:57:23 +00:00
|
|
|
|
2019-03-11 15:33:24 +00:00
|
|
|
result = bls_sign(v.privKey, signed_root(proposal, "signature"),
|
2019-02-12 15:56:58 +00:00
|
|
|
get_domain(fork, slot_to_epoch(proposal.slot), DOMAIN_PROPOSAL))
|
2018-11-23 23:58:49 +00:00
|
|
|
else:
|
|
|
|
# TODO:
|
|
|
|
# send RPC
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc signAttestation*(v: AttachedValidator,
|
2018-12-03 21:41:24 +00:00
|
|
|
attestation: AttestationData): Future[ValidatorSig] {.async.} =
|
2018-11-23 23:58:49 +00:00
|
|
|
# TODO: implement this
|
2018-11-29 01:08:34 +00:00
|
|
|
if v.kind == inProcess:
|
2018-11-23 23:58:49 +00:00
|
|
|
await sleepAsync(1)
|
2018-12-12 13:57:23 +00:00
|
|
|
|
2018-12-21 23:47:55 +00:00
|
|
|
let attestationRoot = hash_tree_root_final(attestation)
|
2018-12-28 16:51:40 +00:00
|
|
|
# TODO: Avoid the allocations belows
|
2019-01-09 01:01:07 +00:00
|
|
|
var dataToSign = @(attestationRoot.data) & @[0'u8]
|
2019-02-05 16:13:29 +00:00
|
|
|
# TODO: Use `domain` here
|
|
|
|
let domain = 0'u64
|
|
|
|
result = bls_sign(v.privKey, dataToSign, domain)
|
2018-11-23 23:58:49 +00:00
|
|
|
else:
|
|
|
|
# TODO:
|
|
|
|
# send RPC
|
|
|
|
discard
|
|
|
|
|
2019-02-20 01:33:58 +00:00
|
|
|
func genRandaoReveal*(k: ValidatorPrivKey, state: BeaconState, slot: Slot):
|
2019-02-07 20:13:10 +00:00
|
|
|
ValidatorSig =
|
2019-02-14 21:41:04 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#randao
|
2019-02-18 15:58:34 +00:00
|
|
|
|
|
|
|
# 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)
|
2019-03-13 01:46:44 +00:00
|
|
|
bls_sign(k, hash_tree_root(slot_to_epoch(slot).uint64),
|
2019-02-14 21:41:04 +00:00
|
|
|
get_domain(state.fork, slot_to_epoch(slot), DOMAIN_RANDAO))
|
2019-02-07 20:13:10 +00:00
|
|
|
|
2019-02-20 01:33:58 +00:00
|
|
|
func genRandaoReveal*(v: AttachedValidator, state: BeaconState, slot: Slot):
|
2019-02-07 20:13:10 +00:00
|
|
|
ValidatorSig =
|
2019-02-14 21:41:04 +00:00
|
|
|
genRandaoReveal(v.privKey, state, slot)
|