bls: avoid exception flow on cache miss (#1479)

This commit is contained in:
Jacek Sieka 2020-08-10 16:51:23 +02:00 committed by GitHub
parent fe1a7922c8
commit 2b4526e743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -98,12 +98,7 @@ func toPubKey*(privkey: ValidatorPrivKey): ValidatorPubKey =
## Create a private key from a public key
# Un-specced in either hash-to-curve or Eth2
# TODO: Test suite should use `keyGen` instead
when ValidatorPubKey is BlsValue:
ValidatorPubKey(kind: Real, blsValue: SecretKey(privkey).privToPub())
elif ValidatorPubKey is array:
privkey.getKey.getBytes
else:
privkey.getKey
proc toRealPubKey(pubkey: ValidatorPubKey): Option[ValidatorPubKey] =
var validatorKeyCache {.threadvar.}:
@ -113,17 +108,16 @@ proc toRealPubKey(pubkey: ValidatorPubKey): Option[ValidatorPubKey] =
of Real:
return some(pubkey)
of OpaqueBlob:
try:
validatorKeyCache[pubkey.blob]
except KeyError:
validatorKeyCache.withValue(pubkey.blob, key) do:
return key[]
do:
var val: blscurve.PublicKey
let maybeRealKey =
if fromBytes(val, pubkey.blob):
some ValidatorPubKey(kind: Real, blsValue: val)
else:
none ValidatorPubKey
validatorKeyCache[pubkey.blob] = maybeRealKey
maybeRealKey
return validatorKeyCache.mGetOrPut(pubkey.blob, maybeRealKey)
proc initPubKey*(pubkey: ValidatorPubKey): ValidatorPubKey =
let key = toRealPubKey(pubkey)
@ -375,4 +369,3 @@ func init*(T: typedesc[ValidatorSig], data: array[RawSigSize, byte]): T {.noInit
proc burnMem*(key: var ValidatorPrivKey) =
key = default(ValidatorPrivKey)