2018-11-23 23:58:49 +00:00
|
|
|
import
|
2019-03-12 15:03:14 +00:00
|
|
|
tables,
|
2019-04-06 07:46:07 +00:00
|
|
|
chronos, chronicles,
|
2020-06-03 13:52:02 +00:00
|
|
|
spec/[datatypes, crypto, digest, state_transition_block],
|
2019-03-12 15:03:14 +00:00
|
|
|
beacon_node_types
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func 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
|
|
|
pubKey: ValidatorPubKey,
|
2019-02-14 21:41:04 +00:00
|
|
|
privKey: ValidatorPrivKey) =
|
2019-11-25 12:47:29 +00:00
|
|
|
let v = AttachedValidator(pubKey: pubKey,
|
2018-12-08 14:17:47 +00:00
|
|
|
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
|
|
|
|
2019-04-06 07:46:07 +00:00
|
|
|
info "Local validator attached", pubKey, validator = shortLog(v)
|
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func 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
|
|
|
|
2020-04-10 14:06:24 +00:00
|
|
|
# TODO: Honest validator - https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/validator.md
|
2020-03-30 11:31:44 +00:00
|
|
|
proc signBlockProposal*(v: AttachedValidator, fork: Fork,
|
|
|
|
genesis_validators_root: Eth2Digest, slot: Slot,
|
2019-04-06 07:46:07 +00:00
|
|
|
blockRoot: Eth2Digest): Future[ValidatorSig] {.async.} =
|
2019-10-07 02:34:45 +00:00
|
|
|
|
2018-11-29 01:08:34 +00:00
|
|
|
if v.kind == inProcess:
|
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
|
|
|
|
2020-03-30 11:31:44 +00:00
|
|
|
result = get_block_signature(
|
|
|
|
fork, genesis_validators_root, slot, blockRoot, v.privKey)
|
2018-11-23 23:58:49 +00:00
|
|
|
else:
|
2019-09-08 18:27:09 +00:00
|
|
|
error "Unimplemented"
|
|
|
|
quit 1
|
2018-11-23 23:58:49 +00:00
|
|
|
|
|
|
|
proc signAttestation*(v: AttachedValidator,
|
2019-09-08 18:09:01 +00:00
|
|
|
attestation: AttestationData,
|
2020-03-30 11:31:44 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest):
|
|
|
|
Future[ValidatorSig] {.async.} =
|
2018-11-29 01:08:34 +00:00
|
|
|
if v.kind == inProcess:
|
2019-10-07 02:34:45 +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
|
|
|
|
await sleepAsync(chronos.milliseconds(1))
|
|
|
|
|
2020-03-30 11:31:44 +00:00
|
|
|
result = get_attestation_signature(
|
|
|
|
fork, genesis_validators_root, attestation, v.privKey)
|
2018-11-23 23:58:49 +00:00
|
|
|
else:
|
2019-09-08 18:27:09 +00:00
|
|
|
error "Unimplemented"
|
|
|
|
quit 1
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2020-04-15 02:41:22 +00:00
|
|
|
proc signAggregateAndProof*(v: AttachedValidator,
|
|
|
|
aggregate_and_proof: AggregateAndProof,
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest): ValidatorSig =
|
|
|
|
if v.kind == inProcess:
|
|
|
|
result = get_aggregate_and_proof_signature(
|
|
|
|
fork, genesis_validators_root, aggregate_and_proof, v.privKey)
|
|
|
|
else:
|
|
|
|
error "Out of process signAggregateAndProof not implemented"
|
|
|
|
quit 1
|
|
|
|
|
2020-03-30 11:31:44 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/validator.md#randao-reveal
|
|
|
|
func genRandaoReveal*(k: ValidatorPrivKey, fork: Fork,
|
|
|
|
genesis_validators_root: Eth2Digest, slot: Slot): ValidatorSig =
|
|
|
|
get_epoch_signature(fork, genesis_validators_root, slot, k)
|
2019-02-07 20:13:10 +00:00
|
|
|
|
2020-03-30 11:31:44 +00:00
|
|
|
func genRandaoReveal*(v: AttachedValidator, fork: Fork,
|
|
|
|
genesis_validators_root: Eth2Digest, slot: Slot): ValidatorSig =
|
|
|
|
genRandaoReveal(v.privKey, fork, genesis_validators_root, slot)
|