From f12b679880974480082f0bfec3f5bc4582723e29 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Wed, 5 Dec 2018 15:58:41 +0200 Subject: [PATCH] addLocalValidators impl --- beacon_chain/beacon_node.nim | 28 +++++++++++++++++++++------- beacon_chain/conf.nim | 9 +++++++-- beacon_chain/spec/crypto.nim | 2 ++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index 8addf4dff..2e7849c0d 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -1,7 +1,7 @@ import - os, net, + os, net, sequtils, asyncdispatch2, chronicles, confutils, eth_p2p, eth_keys, - spec/[beaconstate, datatypes, helpers], conf, time, fork_choice, + spec/[beaconstate, datatypes, helpers, crypto], conf, time, fork_choice, beacon_chain_db, validator_pool, mainchain_monitor, sync_protocol, gossipsub_protocol, trusted_state_snapshots @@ -67,16 +67,30 @@ proc sync*(node: BeaconNode): Future[bool] {.async.} = return true +template findIt(s: openarray, predicate: untyped): int = + var res = -1 + for i, it {.inject.} in s: + if predicate: + res = i + break + res + proc addLocalValidators*(node: BeaconNode) = for validator in node.config.validatorKeys: - # TODO: # 1. Parse the validator keys - # + let privKey = loadPrivKey(validator) + let pubKey = privKey.pubKey() + # 2. Check whether the validators exist in the beacon state. # (Report a warning otherwise) - # - # 3. Add the validators to node.attachedValidators - discard + let idx = node.beaconState.validator_registry.findIt(it.pubKey == pubKey) + if idx == -1: + warn "Validator not in registry", pubKey + else: + # 3. Add the validators to node.attachedValidators + # TODO: Parse randao secret + node.attachedValidators.addLocalValidator(idx, pubKey, privKey, @[]) + proc getAttachedValidator(node: BeaconNode, idx: int): AttachedValidator = let validatorKey = node.beaconState.validator_registry[idx].pubkey diff --git a/beacon_chain/conf.nim b/beacon_chain/conf.nim index 3d63c563b..0bcdeddd7 100644 --- a/beacon_chain/conf.nim +++ b/beacon_chain/conf.nim @@ -1,5 +1,5 @@ import - confutils/defs + confutils/defs, spec/crypto, milagro_crypto type ValidatorKeyPath* = distinct string @@ -25,9 +25,14 @@ type "Nimbus will automatically add the extensions .privkey and .pubkey.", shorthand: "v".}: seq[ValidatorKeyPath] +proc loadPrivKey*(p: ValidatorKeyPath): ValidatorPrivKey = + initSigKey(cast[seq[byte]](readFile(string(p) & ".privkey"))) + proc parse*(T: type ValidatorKeyPath, input: TaintedString): T = + discard loadPrivKey(ValidatorKeyPath(input)) + # TODO: # Check that the entered string is a valid base file name and - # that it has matching .privkey, .pubkey and .randaosecret files + # that it has matching .privkey, and .randaosecret files T(input) diff --git a/beacon_chain/spec/crypto.nim b/beacon_chain/spec/crypto.nim index c4d3ac2d9..487479f19 100644 --- a/beacon_chain/spec/crypto.nim +++ b/beacon_chain/spec/crypto.nim @@ -20,3 +20,5 @@ type template hash*(k: ValidatorPubKey|ValidatorPrivKey): Hash = hash(k.getRaw) +proc pubKey*(pk: ValidatorPrivKey): ValidatorPubKey = fromSigKey(pk) +