2021-08-18 18:57:58 +00:00
|
|
|
# beacon_chain
|
2024-02-29 13:24:08 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-08-18 18:57:58 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2024-02-29 13:24:08 +00:00
|
|
|
{.push raises: [].}
|
2021-08-18 18:57:58 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
unittest2,
|
|
|
|
../beacon_chain/spec/datatypes/[phase0, altair],
|
|
|
|
../beacon_chain/spec/eth2_ssz_serialization
|
|
|
|
|
|
|
|
static:
|
|
|
|
doAssert isFixedSize(Slot) == true
|
|
|
|
|
|
|
|
type
|
|
|
|
Specific = object
|
|
|
|
f1: Slot
|
|
|
|
f2: Epoch
|
|
|
|
|
|
|
|
Primitive = object # Same as above, but using primitive fields
|
|
|
|
f1: uint64
|
|
|
|
f2: uint64
|
|
|
|
|
|
|
|
suite "Specific field types":
|
|
|
|
test "roundtrip":
|
|
|
|
let encoded = SSZ.encode(Specific(f1: Slot(1), f2: Epoch(2)))
|
|
|
|
check SSZ.decode(encoded, Primitive) == Primitive(f1: 1, f2: 2)
|
|
|
|
|
|
|
|
test "root update":
|
|
|
|
template testit(T: type) =
|
|
|
|
var t: T
|
|
|
|
t.root = hash_tree_root(t.message)
|
|
|
|
let encoded = SSZ.encode(t)
|
|
|
|
let decoded = SSZ.decode(encoded, T)
|
|
|
|
check:
|
|
|
|
t.message == decoded.message
|
|
|
|
t.root == decoded.root
|
|
|
|
|
2021-08-18 21:03:20 +00:00
|
|
|
t = default(type t)
|
|
|
|
readSszBytes(encoded, t, false)
|
|
|
|
check:
|
2022-02-14 05:26:19 +00:00
|
|
|
t.root.isZero
|
2021-08-18 21:03:20 +00:00
|
|
|
|
2021-08-18 18:57:58 +00:00
|
|
|
testit(phase0.SignedBeaconBlock)
|
|
|
|
testit(phase0.TrustedSignedBeaconBlock)
|
|
|
|
testit(altair.SignedBeaconBlock)
|
|
|
|
testit(altair.TrustedSignedBeaconBlock)
|