2018-09-20 15:45:02 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
2019-02-05 16:13:29 +00:00
|
|
|
unittest, nimcrypto, eth_common, sequtils, options, blscurve,
|
2018-11-28 19:49:03 +00:00
|
|
|
../beacon_chain/ssz, ../beacon_chain/spec/datatypes
|
2018-09-20 15:45:02 +00:00
|
|
|
|
|
|
|
func filled[N: static[int], T](typ: type array[N, T], value: T): array[N, T] =
|
|
|
|
for val in result.mitems:
|
|
|
|
val = value
|
|
|
|
|
|
|
|
func filled(T: type MDigest, value: byte): T =
|
|
|
|
for val in result.data.mitems:
|
|
|
|
val = value
|
|
|
|
|
|
|
|
suite "Simple serialization":
|
|
|
|
# pending spec updates in
|
|
|
|
# - https://github.com/ethereum/eth2.0-specs
|
|
|
|
type
|
|
|
|
Foo = object
|
|
|
|
f0: uint8
|
|
|
|
f1: uint32
|
|
|
|
f2: EthAddress
|
|
|
|
f3: MDigest[256]
|
|
|
|
f4: seq[byte]
|
2019-01-29 04:15:00 +00:00
|
|
|
f5: ValidatorIndex
|
2018-09-20 15:45:02 +00:00
|
|
|
|
|
|
|
let expected_deser = Foo(
|
|
|
|
f0: 5,
|
|
|
|
f1: 0'u32 - 3,
|
|
|
|
f2: EthAddress.filled(byte 35),
|
|
|
|
f3: MDigest[256].filled(byte 35),
|
2018-12-17 18:03:53 +00:00
|
|
|
f4: @[byte 'c'.ord, 'o'.ord, 'w'.ord],
|
2019-01-29 04:15:00 +00:00
|
|
|
f5: ValidatorIndex(79)
|
2018-09-20 15:45:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var expected_ser = @[
|
2019-01-22 17:56:01 +00:00
|
|
|
byte 67, 0, 0, 0, # length
|
2018-12-17 18:03:53 +00:00
|
|
|
5,
|
2019-01-22 17:56:01 +00:00
|
|
|
0xFD, 0xFF, 0xFF, 0xFF,
|
2018-09-20 15:45:02 +00:00
|
|
|
]
|
|
|
|
expected_ser &= EthAddress.filled(byte 35)
|
|
|
|
expected_ser &= MDigest[256].filled(byte 35).data
|
2019-01-22 17:56:01 +00:00
|
|
|
expected_ser &= [byte 3, 0, 0, 0, 'c'.ord, 'o'.ord, 'w'.ord]
|
|
|
|
expected_ser &= [byte 79, 0, 0]
|
2018-09-20 15:45:02 +00:00
|
|
|
|
2018-12-17 18:03:53 +00:00
|
|
|
test "Object deserialization":
|
2018-10-08 10:56:34 +00:00
|
|
|
let deser = expected_ser.deserialize(Foo).get()
|
2018-09-20 15:45:02 +00:00
|
|
|
check: expected_deser == deser
|
|
|
|
|
2018-12-17 18:03:53 +00:00
|
|
|
test "Object serialization":
|
2018-09-20 15:45:02 +00:00
|
|
|
let ser = expected_deser.serialize()
|
|
|
|
check: expected_ser == ser
|
2018-10-08 10:56:34 +00:00
|
|
|
|
2018-12-17 18:03:53 +00:00
|
|
|
test "Not enough data":
|
2018-10-08 10:56:34 +00:00
|
|
|
check:
|
|
|
|
expected_ser[0..^2].deserialize(Foo).isNone()
|
|
|
|
expected_ser[1..^1].deserialize(Foo).isNone()
|
2018-11-14 20:06:04 +00:00
|
|
|
|
2019-01-29 04:15:00 +00:00
|
|
|
test "ValidatorIndex roundtrip":
|
2018-12-17 18:03:53 +00:00
|
|
|
# https://github.com/nim-lang/Nim/issues/10027
|
2019-01-29 04:15:00 +00:00
|
|
|
let v = 79.ValidatorIndex
|
2018-12-17 18:03:53 +00:00
|
|
|
let ser = v.serialize()
|
|
|
|
check:
|
|
|
|
ser.len() == 3
|
|
|
|
deserialize(ser, type(v)).get() == v
|
|
|
|
|
|
|
|
test "Array roundtrip":
|
|
|
|
let v = [1, 2, 3]
|
|
|
|
let ser = v.serialize()
|
|
|
|
check:
|
|
|
|
deserialize(ser, type(v)).get() == v
|
|
|
|
|
|
|
|
test "Seq roundtrip":
|
|
|
|
let v = @[1, 2, 3]
|
|
|
|
let ser = v.serialize()
|
|
|
|
|
|
|
|
check:
|
|
|
|
deserialize(ser, type(v)).get() == v
|
|
|
|
|
|
|
|
test "Key roundtrip":
|
2019-02-05 16:13:29 +00:00
|
|
|
let v = SigKey.random().getKey()
|
2018-12-17 18:03:53 +00:00
|
|
|
let ser = v.serialize()
|
|
|
|
|
|
|
|
check:
|
|
|
|
deserialize(ser, type(v)).get() == v
|
|
|
|
|
|
|
|
# Just to see that we can serialize stuff at all
|
|
|
|
test "Roundtrip main eth2 types":
|
|
|
|
let
|
|
|
|
bb = BeaconBlock(
|
|
|
|
slot: 42,
|
2019-02-05 16:13:29 +00:00
|
|
|
signature: sign(SigKey.random(), 0'u64, "")
|
2018-12-17 18:03:53 +00:00
|
|
|
)
|
|
|
|
bs = BeaconState(slot: 42)
|
|
|
|
|
|
|
|
check:
|
|
|
|
bb.serialize().deserialize(BeaconBlock).get() == bb
|
|
|
|
bs.serialize().deserialize(BeaconState).get() == bs
|
|
|
|
|
2018-11-14 20:06:04 +00:00
|
|
|
suite "Tree hashing":
|
2018-11-29 22:11:05 +00:00
|
|
|
# TODO Nothing but smoke tests for now..
|
2018-11-14 20:06:04 +00:00
|
|
|
|
2019-01-17 18:27:11 +00:00
|
|
|
test "Hash Validator":
|
|
|
|
let vr = Validator()
|
2018-12-11 21:53:18 +00:00
|
|
|
check: hash_tree_root(vr).len > 0
|
2018-11-14 20:06:04 +00:00
|
|
|
|
2018-12-06 02:07:04 +00:00
|
|
|
test "Hash ShardCommittee":
|
|
|
|
let sc = ShardCommittee()
|
2018-12-11 21:53:18 +00:00
|
|
|
check: hash_tree_root(sc).len > 0
|
2018-11-14 20:06:04 +00:00
|
|
|
|
2018-11-22 10:17:05 +00:00
|
|
|
test "Hash BeaconBlock":
|
|
|
|
## TODO: Test genesis hash when spec is updated
|
|
|
|
let bb = BeaconBlock()
|
2018-12-11 21:53:18 +00:00
|
|
|
check: hash_tree_root(bb).len > 0
|
2018-11-22 10:17:05 +00:00
|
|
|
|
2018-11-14 20:06:04 +00:00
|
|
|
test "Hash integer":
|
2019-01-22 17:56:01 +00:00
|
|
|
check: hash_tree_root(0x01'u32) == [1'u8, 0, 0, 0] # little endian!
|
2019-01-29 04:15:00 +00:00
|
|
|
check: hash_tree_root(ValidatorIndex(0x01)) == [1'u8, 0, 0] # little endian!
|