2019-02-21 17:20:50 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT)
|
2019-02-21 17:20:50 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2019-11-14 10:47:55 +00:00
|
|
|
{.used.}
|
|
|
|
|
2020-01-15 15:06:50 +00:00
|
|
|
import options, unittest, sequtils,
|
2020-06-23 13:54:24 +00:00
|
|
|
../beacon_chain/[beacon_chain_db, extras, interop, ssz],
|
|
|
|
../beacon_chain/spec/[
|
2020-07-07 23:02:14 +00:00
|
|
|
beaconstate, datatypes, digest, crypto, state_transition, presets],
|
2020-04-27 16:36:28 +00:00
|
|
|
eth/db/kvstore,
|
2019-05-27 12:48:13 +00:00
|
|
|
# test utilies
|
2019-12-03 16:45:12 +00:00
|
|
|
./testutil, ./testblockutil
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2020-04-28 08:08:32 +00:00
|
|
|
proc getStateRef(db: BeaconChainDB, root: Eth2Digest): NilableBeaconStateRef =
|
2020-07-30 19:18:17 +00:00
|
|
|
# load beaconstate the way the block pool does it - into an existing instance
|
2020-04-28 08:08:32 +00:00
|
|
|
let res = BeaconStateRef()
|
|
|
|
if db.getState(root, res[], noRollback):
|
|
|
|
return res
|
|
|
|
|
2020-05-14 10:38:45 +00:00
|
|
|
template wrappedTimedTest(name: string, body: untyped) =
|
2020-05-28 08:28:14 +00:00
|
|
|
# `check` macro takes a copy of whatever it's checking, on the stack!
|
2020-05-14 10:38:45 +00:00
|
|
|
block: # Symbol namespacing
|
|
|
|
proc wrappedTest() =
|
|
|
|
timedTest name:
|
|
|
|
body
|
|
|
|
wrappedTest()
|
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
func withDigest(blck: TrustedBeaconBlock): TrustedSignedBeaconBlock =
|
|
|
|
TrustedSignedBeaconBlock(
|
|
|
|
message: blck,
|
|
|
|
root: hash_tree_root(blck)
|
|
|
|
)
|
|
|
|
|
2020-03-10 04:00:19 +00:00
|
|
|
suiteReport "Beacon chain DB" & preset():
|
2020-05-14 10:38:45 +00:00
|
|
|
wrappedTimedTest "empty database" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
var
|
2020-04-23 06:27:35 +00:00
|
|
|
db = init(BeaconChainDB, kvStore MemStoreRef.init())
|
2019-02-21 17:20:50 +00:00
|
|
|
check:
|
2020-04-28 08:08:32 +00:00
|
|
|
db.getStateRef(Eth2Digest()).isNil
|
|
|
|
db.getBlock(Eth2Digest()).isNone
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2020-05-14 10:38:45 +00:00
|
|
|
wrappedTimedTest "sanity check blocks" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
var
|
2020-04-23 06:27:35 +00:00
|
|
|
db = init(BeaconChainDB, kvStore MemStoreRef.init())
|
2019-03-08 16:40:17 +00:00
|
|
|
|
|
|
|
let
|
2020-07-16 13:16:51 +00:00
|
|
|
signedBlock = withDigest(TrustedBeaconBlock())
|
2020-02-29 15:15:44 +00:00
|
|
|
root = hash_tree_root(signedBlock.message)
|
2019-03-08 16:40:17 +00:00
|
|
|
|
2020-02-29 15:15:44 +00:00
|
|
|
db.putBlock(signedBlock)
|
2019-03-08 16:40:17 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
db.containsBlock(root)
|
2020-02-29 15:15:44 +00:00
|
|
|
db.getBlock(root).get() == signedBlock
|
2019-03-08 16:40:17 +00:00
|
|
|
|
2020-02-29 15:15:44 +00:00
|
|
|
db.putStateRoot(root, signedBlock.message.slot, root)
|
2019-03-28 06:10:48 +00:00
|
|
|
check:
|
2020-02-29 15:15:44 +00:00
|
|
|
db.getStateRoot(root, signedBlock.message.slot).get() == root
|
2019-03-28 06:10:48 +00:00
|
|
|
|
2020-05-14 10:38:45 +00:00
|
|
|
wrappedTimedTest "sanity check states" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
var
|
2020-04-23 06:27:35 +00:00
|
|
|
db = init(BeaconChainDB, kvStore MemStoreRef.init())
|
2019-03-08 16:40:17 +00:00
|
|
|
|
|
|
|
let
|
2020-04-23 18:58:54 +00:00
|
|
|
state = BeaconStateRef()
|
2020-04-29 20:12:07 +00:00
|
|
|
root = hash_tree_root(state[])
|
2019-03-08 16:40:17 +00:00
|
|
|
|
2020-04-28 08:08:32 +00:00
|
|
|
db.putState(state[])
|
2019-03-08 16:40:17 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
db.containsState(root)
|
2020-05-28 08:28:14 +00:00
|
|
|
hash_tree_root(db.getStateRef(root)[]) == root
|
2019-03-08 16:40:17 +00:00
|
|
|
|
2020-05-14 10:38:45 +00:00
|
|
|
wrappedTimedTest "find ancestors" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
var
|
2020-04-23 06:27:35 +00:00
|
|
|
db = init(BeaconChainDB, kvStore MemStoreRef.init())
|
2019-02-21 17:20:50 +00:00
|
|
|
|
|
|
|
let
|
2020-07-16 13:16:51 +00:00
|
|
|
a0 = withDigest(
|
2020-06-25 10:23:10 +00:00
|
|
|
TrustedBeaconBlock(slot: GENESIS_SLOT + 0))
|
2020-07-16 13:16:51 +00:00
|
|
|
a1 = withDigest(
|
|
|
|
TrustedBeaconBlock(slot: GENESIS_SLOT + 1, parent_root: a0.root))
|
|
|
|
a2 = withDigest(
|
|
|
|
TrustedBeaconBlock(slot: GENESIS_SLOT + 2, parent_root: a1.root))
|
2019-12-16 18:08:50 +00:00
|
|
|
a2r = hash_tree_root(a2.message)
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
doAssert toSeq(db.getAncestors(a0.root)) == []
|
|
|
|
doAssert toSeq(db.getAncestors(a2.root)) == []
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2019-02-21 21:38:26 +00:00
|
|
|
db.putBlock(a2)
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
doAssert toSeq(db.getAncestors(a0.root)) == []
|
|
|
|
doAssert toSeq(db.getAncestors(a2.root)) == [a2]
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2019-02-21 21:38:26 +00:00
|
|
|
db.putBlock(a1)
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
doAssert toSeq(db.getAncestors(a0.root)) == []
|
|
|
|
doAssert toSeq(db.getAncestors(a2.root)) == [a2, a1]
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2019-02-21 21:38:26 +00:00
|
|
|
db.putBlock(a0)
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
doAssert toSeq(db.getAncestors(a0.root)) == [a0]
|
|
|
|
doAssert toSeq(db.getAncestors(a2.root)) == [a2, a1, a0]
|
2019-09-05 14:27:28 +00:00
|
|
|
|
2020-05-14 10:38:45 +00:00
|
|
|
wrappedTimedTest "sanity check genesis roundtrip" & preset():
|
2019-09-05 14:27:28 +00:00
|
|
|
# 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
|
2020-04-23 06:27:35 +00:00
|
|
|
db = init(BeaconChainDB, kvStore MemStoreRef.init())
|
2019-09-05 14:27:28 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
state = initialize_beacon_state_from_eth1(
|
2020-07-07 23:02:14 +00:00
|
|
|
defaultRuntimePreset, eth1BlockHash, 0,
|
|
|
|
makeInitialDeposits(SLOTS_PER_EPOCH), {skipBlsValidation})
|
2020-04-29 20:12:07 +00:00
|
|
|
root = hash_tree_root(state[])
|
2019-09-05 14:27:28 +00:00
|
|
|
|
2020-04-28 08:08:32 +00:00
|
|
|
db.putState(state[])
|
2019-09-05 14:27:28 +00:00
|
|
|
|
2020-05-28 08:28:14 +00:00
|
|
|
check db.containsState(root)
|
|
|
|
let state2 = db.getStateRef(root)
|
|
|
|
|
2019-09-05 14:27:28 +00:00
|
|
|
check:
|
2020-05-28 08:28:14 +00:00
|
|
|
hash_tree_root(state2[]) == root
|