From c142de4b7f409aa9ce22fec4c7a8ddbb31d5b671 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Thu, 23 Jul 2020 20:24:38 +0200 Subject: [PATCH] be more consistent about pubkeys fed to verify_foo_signature() not being separately initialized, while pubkeys, generally, used for matching purposes, elsewhere explicitly initialized --- beacon_chain/block_pools/chain_dag.nim | 4 +++- beacon_chain/spec/beaconstate.nim | 3 ++- beacon_chain/spec/crypto.nim | 11 ++++++++--- beacon_chain/validator_duties.nim | 4 ++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/beacon_chain/block_pools/chain_dag.nim b/beacon_chain/block_pools/chain_dag.nim index 12432ec79..37213fa58 100644 --- a/beacon_chain/block_pools/chain_dag.nim +++ b/beacon_chain/block_pools/chain_dag.nim @@ -879,4 +879,6 @@ proc getProposer*( balances=state.balances return - return some((proposerIdx.get(), state.validators[proposerIdx.get()].pubkey)) + return some(( + proposerIdx.get(), + state.validators[proposerIdx.get()].pubkey.initPubKey)) diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 6874002eb..04ff454a2 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -88,11 +88,12 @@ proc process_deposit*(preset: RuntimePreset, let pubkey = deposit.data.pubkey + pubkey_inited = pubkey.initPubKey # TODO replicate previous PR semantics, check later amount = deposit.data.amount var index = -1 for i, validator in state.validators: - if pubkey == validator.pubkey: + if pubkey == validator.pubkey.initPubKey: index = i break diff --git a/beacon_chain/spec/crypto.nim b/beacon_chain/spec/crypto.nim index 83a194d71..5cdf1a2f7 100644 --- a/beacon_chain/spec/crypto.nim +++ b/beacon_chain/spec/crypto.nim @@ -122,10 +122,16 @@ proc toRealPubKey(pubkey: ValidatorPubKey): Option[ValidatorPubKey] = if fromBytes(val, pubkey.blob): some ValidatorPubKey(kind: Real, blsValue: val) else: - none(ValidatorPubKey) + none ValidatorPubKey validatorKeyCache[key] = maybeRealKey maybeRealKey +proc initPubKey*(pubkey: ValidatorPubKey): ValidatorPubKey = + let key = toRealPubKey(pubkey) + if key.isNone: + return ValidatorPubKey() + key.get + func aggregate*(x: var ValidatorSig, other: ValidatorSig) = ## Aggregate 2 Validator Signatures ## This assumes that they are real signatures @@ -245,8 +251,7 @@ func fromRaw*(T: type ValidatorPrivKey, bytes: openArray[byte]): BlsResult[T] = func fromRaw*[N, T](BT: type BlsValue[N, T], bytes: openArray[byte]): BlsResult[BT] = # This is a workaround, so that we can deserialize the serialization of a # default-initialized BlsValue without raising an exception - when defined(ssz_testing) or T is blscurve.PublicKey: - # Only for SSZ parsing tests, everything is an opaque blob + when defined(ssz_testing) or BT is ValidatorPubKey: ok BT(kind: OpaqueBlob, blob: toArray(N, bytes)) else: # Try if valid BLS value diff --git a/beacon_chain/validator_duties.nim b/beacon_chain/validator_duties.nim index ec0d6149a..d1de50a0d 100644 --- a/beacon_chain/validator_duties.nim +++ b/beacon_chain/validator_duties.nim @@ -60,10 +60,10 @@ proc addLocalValidators*(node: BeaconNode) = info "Local validators attached ", count = node.attachedValidators.count -func getAttachedValidator*(node: BeaconNode, +proc getAttachedValidator*(node: BeaconNode, state: BeaconState, idx: ValidatorIndex): AttachedValidator = - let validatorKey = state.validators[idx].pubkey + let validatorKey = state.validators[idx].pubkey.initPubKey node.attachedValidators.getValidator(validatorKey) proc isSynced*(node: BeaconNode, head: BlockRef): bool =