From 9c65c50249a3d89e50b28174af286a8cbad4b2e0 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Tue, 4 Aug 2020 19:44:16 +0200 Subject: [PATCH] use case statement to avoid runtime doAssert --- beacon_chain/spec/crypto.nim | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/beacon_chain/spec/crypto.nim b/beacon_chain/spec/crypto.nim index 5cdf1a2f7..68c058012 100644 --- a/beacon_chain/spec/crypto.nim +++ b/beacon_chain/spec/crypto.nim @@ -108,23 +108,22 @@ func toPubKey*(privkey: ValidatorPrivKey): ValidatorPubKey = proc toRealPubKey(pubkey: ValidatorPubKey): Option[ValidatorPubKey] = var validatorKeyCache {.threadvar.}: Table[Hash, Option[ValidatorPubKey]] - if pubkey.kind == Real: + case pubkey.kind: + of Real: return some(pubkey) - - doAssert pubkey.kind == OpaqueBlob - - let key = hash(pubkey.blob) - try: - validatorKeyCache[key] - except KeyError: - var val: blscurve.PublicKey - let maybeRealKey = - if fromBytes(val, pubkey.blob): - some ValidatorPubKey(kind: Real, blsValue: val) - else: - none ValidatorPubKey - validatorKeyCache[key] = maybeRealKey - maybeRealKey + of OpaqueBlob: + let key = hash(pubkey.blob) + try: + validatorKeyCache[key] + except KeyError: + var val: blscurve.PublicKey + let maybeRealKey = + if fromBytes(val, pubkey.blob): + some ValidatorPubKey(kind: Real, blsValue: val) + else: + none ValidatorPubKey + validatorKeyCache[key] = maybeRealKey + maybeRealKey proc initPubKey*(pubkey: ValidatorPubKey): ValidatorPubKey = let key = toRealPubKey(pubkey)