2018-11-23 23:58:49 +00:00
|
|
|
import
|
|
|
|
tables, random,
|
2019-02-05 16:13:29 +00:00
|
|
|
asyncdispatch2,
|
2018-12-12 13:57:23 +00:00
|
|
|
spec/[datatypes, crypto, digest], randao, ssz
|
2018-11-23 23:58:49 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
ValidatorKind = enum
|
|
|
|
inProcess
|
|
|
|
remote
|
|
|
|
|
|
|
|
ValidatorConnection = object
|
|
|
|
|
|
|
|
AttachedValidator* = ref object
|
2018-12-08 14:17:47 +00:00
|
|
|
idx*: int # index in the registry
|
2018-11-23 23:58:49 +00:00
|
|
|
case kind: ValidatorKind
|
|
|
|
of inProcess:
|
2018-11-29 01:08:34 +00:00
|
|
|
privKey: ValidatorPrivKey
|
2018-12-08 14:17:47 +00:00
|
|
|
randaoSecret: Randao
|
2018-11-23 23:58:49 +00:00
|
|
|
else:
|
|
|
|
connection: ValidatorConnection
|
|
|
|
|
|
|
|
ValidatorPool* = object
|
2018-11-29 01:08:34 +00:00
|
|
|
validators: Table[ValidatorPubKey, AttachedValidator]
|
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,
|
|
|
|
privKey: ValidatorPrivKey,
|
2018-12-08 14:17:47 +00:00
|
|
|
randaoSecret: Randao) =
|
|
|
|
let v = AttachedValidator(idx: idx,
|
|
|
|
kind: inProcess,
|
|
|
|
privKey: privKey,
|
|
|
|
randaoSecret: randaoSecret)
|
|
|
|
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
|
|
|
|
|
|
|
proc signBlockProposal*(v: AttachedValidator,
|
2018-11-29 01:08:34 +00:00
|
|
|
proposal: ProposalSignedData): Future[ValidatorSig] {.async.} =
|
|
|
|
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
|
|
|
|
|
|
|
# TODO: Should we use proposalRoot as data, or digest in regards to signature?
|
2019-02-05 16:13:29 +00:00
|
|
|
# TODO: Use `domain` here
|
|
|
|
let domain = 0'u64
|
|
|
|
result = bls_sign(v.privKey, proposalRoot.data, domain)
|
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
|
|
|
|
|
2018-12-08 14:17:47 +00:00
|
|
|
proc randaoReveal*(v: AttachedValidator, commitment: Eth2Digest): Future[Eth2Digest] {.async.} =
|
|
|
|
if v.kind == inProcess:
|
|
|
|
result = v.randaoSecret.reveal(commitment)
|
|
|
|
else:
|
|
|
|
# TODO:
|
|
|
|
# send RPC
|
|
|
|
discard
|
|
|
|
|