Deserialize serialized default bls values (#388)

Ugly workaround to quick-fix broken network/interop sim
This commit is contained in:
Jacek Sieka 2019-09-05 16:27:28 +02:00 committed by Dustin Brody
parent ea8ef2d6ed
commit 31db5d3a62
2 changed files with 32 additions and 5 deletions

View File

@ -212,10 +212,18 @@ else:
ValidatorSig(kind: Real, blsValue: key.sign(domain, msg))
proc fromBytes*[T](R: type BlsValue[T], bytes: openarray[byte]): R =
when defined(ssz_testing):
result = R(kind: OpaqueBlob, blob: toArray(result.blob.len, bytes))
# This is a workaround, so that we can deserialize the serialization of a
# default-initialized BlsValue without raising an exception
# TODO don't use exceptions for parsing, ever. Handle deserialization issues
# sanely, always.
const allZeroes = T()
if bytes == allZeroes.getBytes():
R(kind: Real)
else:
result = R(kind: Real, blsValue: init(T, bytes))
when defined(ssz_testing):
R(kind: OpaqueBlob, blob: toArray(result.blob.len, bytes))
else:
R(kind: Real, blsValue: init(T, bytes))
proc initFromBytes*[T](val: var BlsValue[T], bytes: openarray[byte]) =
val = fromBytes(BlsValue[T], bytes)

View File

@ -6,8 +6,8 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import options, unittest, sequtils, strutils, eth/trie/[db],
../beacon_chain/[beacon_chain_db, ssz],
../beacon_chain/spec/[datatypes, digest, crypto],
../beacon_chain/[beacon_chain_db, extras, interop, ssz],
../beacon_chain/spec/[beaconstate, datatypes, digest, crypto],
# test utilies
./testutil
@ -87,3 +87,22 @@ suite "Beacon chain DB" & preset():
doAssert toSeq(db.getAncestors(a0r)) == [(a0r, a0)]
doAssert toSeq(db.getAncestors(a2r)) == [(a2r, a2), (a1r, a1), (a0r, a0)]
test "sanity check genesis roundtrip" & preset():
# This is a really dumb way of checking that we can roundtrip a genesis
# state. We've been bit by this because we've had a bug in the BLS
# serialization where an all-zero default-initialized bls signature could
# not be deserialized because the deserialization was too strict.
var
db = init(BeaconChainDB, newMemoryDB())
let
state = initialize_beacon_state_from_eth1(
eth1BlockHash, 0, makeInitialDeposits(SLOTS_PER_EPOCH), {skipValidation})
root = hash_tree_root(state)
db.putState(state)
check:
db.containsState(root)
db.getState(root).get() == state