2019-02-21 17:20:50 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
# 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.}
|
|
|
|
|
|
|
|
import options, unittest, sequtils, eth/trie/[db],
|
2019-09-05 14:27:28 +00:00
|
|
|
../beacon_chain/[beacon_chain_db, extras, interop, ssz],
|
|
|
|
../beacon_chain/spec/[beaconstate, datatypes, digest, crypto],
|
2019-05-27 12:48:13 +00:00
|
|
|
# test utilies
|
|
|
|
./testutil
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
suite "Beacon chain DB" & preset():
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
test "empty database" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
var
|
|
|
|
db = init(BeaconChainDB, newMemoryDB())
|
|
|
|
|
2019-02-21 17:20:50 +00:00
|
|
|
check:
|
2019-02-21 21:38:26 +00:00
|
|
|
db.getState(Eth2Digest()).isNone
|
|
|
|
db.getBlock(Eth2Digest()).isNone
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
test "sanity check blocks" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
var
|
|
|
|
db = init(BeaconChainDB, newMemoryDB())
|
|
|
|
|
|
|
|
let
|
|
|
|
blck = BeaconBlock()
|
2019-05-09 12:27:37 +00:00
|
|
|
root = signing_root(blck)
|
2019-03-08 16:40:17 +00:00
|
|
|
|
|
|
|
db.putBlock(blck)
|
|
|
|
|
|
|
|
check:
|
|
|
|
db.containsBlock(root)
|
|
|
|
db.getBlock(root).get() == blck
|
|
|
|
|
2019-03-28 06:10:48 +00:00
|
|
|
db.putStateRoot(root, blck.slot, root)
|
|
|
|
check:
|
|
|
|
db.getStateRoot(root, blck.slot).get() == root
|
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
test "sanity check states" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
var
|
|
|
|
db = init(BeaconChainDB, newMemoryDB())
|
|
|
|
|
|
|
|
let
|
|
|
|
state = BeaconState()
|
2019-03-25 16:46:31 +00:00
|
|
|
root = hash_tree_root(state)
|
2019-03-08 16:40:17 +00:00
|
|
|
|
|
|
|
db.putState(state)
|
|
|
|
|
|
|
|
check:
|
|
|
|
db.containsState(root)
|
|
|
|
db.getState(root).get() == state
|
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
test "find ancestors" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
var
|
|
|
|
db = init(BeaconChainDB, newMemoryDB())
|
|
|
|
x: ValidatorSig
|
|
|
|
y = init(ValidatorSig, x.getBytes())
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2019-03-08 16:40:17 +00:00
|
|
|
# Silly serialization check that fails without the right import
|
2019-02-21 17:20:50 +00:00
|
|
|
check: x == y
|
|
|
|
|
|
|
|
let
|
2019-03-16 19:52:37 +00:00
|
|
|
a0 = BeaconBlock(slot: GENESIS_SLOT + 0)
|
2019-05-09 12:27:37 +00:00
|
|
|
a0r = signing_root(a0)
|
2019-06-14 13:50:47 +00:00
|
|
|
a1 = BeaconBlock(slot: GENESIS_SLOT + 1, parent_root: a0r)
|
2019-05-09 12:27:37 +00:00
|
|
|
a1r = signing_root(a1)
|
2019-06-14 13:50:47 +00:00
|
|
|
a2 = BeaconBlock(slot: GENESIS_SLOT + 2, parent_root: a1r)
|
2019-05-09 12:27:37 +00:00
|
|
|
a2r = signing_root(a2)
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
doAssert toSeq(db.getAncestors(a0r)) == []
|
|
|
|
doAssert toSeq(db.getAncestors(a2r)) == []
|
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
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
doAssert toSeq(db.getAncestors(a0r)) == []
|
|
|
|
doAssert toSeq(db.getAncestors(a2r)) == [(a2r, 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
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
doAssert toSeq(db.getAncestors(a0r)) == []
|
|
|
|
doAssert toSeq(db.getAncestors(a2r)) == [(a2r, a2), (a1r, 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
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
doAssert toSeq(db.getAncestors(a0r)) == [(a0r, a0)]
|
|
|
|
doAssert toSeq(db.getAncestors(a2r)) == [(a2r, a2), (a1r, a1), (a0r, a0)]
|
2019-09-05 14:27:28 +00:00
|
|
|
|
|
|
|
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
|