2018-11-24 01:58:49 +02:00
|
|
|
import
|
2019-03-12 16:03:14 +01:00
|
|
|
tables,
|
2019-04-06 01:46:07 -06:00
|
|
|
chronos, chronicles,
|
|
|
|
spec/[datatypes, crypto, digest, helpers], ssz,
|
2019-03-12 16:03:14 +01:00
|
|
|
beacon_node_types
|
2018-11-24 01:58:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
proc init*(T: type ValidatorPool): T =
|
2018-11-29 03:08:34 +02:00
|
|
|
result.validators = initTable[ValidatorPubKey, AttachedValidator]()
|
2018-11-24 01:58:49 +02:00
|
|
|
|
2018-12-19 14:58:53 +02:00
|
|
|
template count*(pool: ValidatorPool): int =
|
|
|
|
pool.validators.len
|
|
|
|
|
2018-11-24 01:58:49 +02:00
|
|
|
proc addLocalValidator*(pool: var ValidatorPool,
|
2018-11-29 03:08:34 +02:00
|
|
|
pubKey: ValidatorPubKey,
|
2019-02-14 13:41:04 -08:00
|
|
|
privKey: ValidatorPrivKey) =
|
2019-04-06 01:46:07 -06:00
|
|
|
let v = AttachedValidator(pubKey: pubKey,
|
2018-12-08 16:17:47 +02:00
|
|
|
kind: inProcess,
|
2019-02-14 13:41:04 -08:00
|
|
|
privKey: privKey)
|
2018-12-08 16:17:47 +02:00
|
|
|
pool.validators[pubKey] = v
|
2018-11-24 01:58:49 +02:00
|
|
|
|
2019-04-06 01:46:07 -06:00
|
|
|
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)
|
2018-11-24 01:58:49 +02:00
|
|
|
|
2019-04-29 16:48:30 +00:00
|
|
|
proc signBlockProposal*(v: AttachedValidator, state: BeaconState, slot: Slot,
|
2019-04-06 01:46:07 -06:00
|
|
|
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))
|
2019-04-06 01:46:07 -06:00
|
|
|
result = bls_sign(v.privKey, blockRoot.data,
|
2019-07-01 07:53:42 +00:00
|
|
|
get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_of_slot(slot)))
|
2018-11-24 01:58:49 +02:00
|
|
|
else:
|
|
|
|
# TODO:
|
|
|
|
# send RPC
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc signAttestation*(v: AttachedValidator,
|
2019-09-08 14:09:01 -04:00
|
|
|
attestation: AttestationData,
|
|
|
|
state: BeaconState): Future[ValidatorSig] {.async.} =
|
2018-11-24 01:58:49 +02:00
|
|
|
# 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
|
|
|
|
2019-03-25 10:46:31 -06:00
|
|
|
let attestationRoot = hash_tree_root(attestation)
|
2018-12-28 18:51:40 +02:00
|
|
|
# TODO: Avoid the allocations belows
|
2019-01-09 03:01:07 +02:00
|
|
|
var dataToSign = @(attestationRoot.data) & @[0'u8]
|
2019-09-08 14:09:01 -04:00
|
|
|
let domain = get_domain(
|
|
|
|
state,
|
|
|
|
DOMAIN_ATTESTATION,
|
|
|
|
attestation.target.epoch
|
|
|
|
)
|
2019-02-05 17:13:29 +01:00
|
|
|
result = bls_sign(v.privKey, dataToSign, domain)
|
2018-11-24 01:58:49 +02: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-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)
|
2019-07-01 07:53:42 +00:00
|
|
|
bls_sign(k, hash_tree_root(compute_epoch_of_slot(slot).uint64).data,
|
|
|
|
get_domain(state, DOMAIN_RANDAO, compute_epoch_of_slot(slot)))
|
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 13:41:04 -08:00
|
|
|
genRandaoReveal(v.privKey, state, slot)
|