2019-02-28 21:21:29 +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
|
|
|
|
options, sequtils, unittest,
|
|
|
|
./testutil,
|
|
|
|
../beacon_chain/spec/[beaconstate, crypto, datatypes, digest, helpers, validator],
|
2019-03-12 15:03:14 +00:00
|
|
|
../beacon_chain/[beacon_node_types, block_pool, beacon_chain_db, extras, state_transition, ssz]
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
suite "Block pool processing" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
let
|
2019-07-10 12:23:02 +00:00
|
|
|
genState = initialize_beacon_state_from_eth1(
|
2019-02-28 21:21:29 +00:00
|
|
|
makeInitialDeposits(flags = {skipValidation}), 0, Eth1Data(),
|
|
|
|
{skipValidation})
|
|
|
|
genBlock = get_initial_beacon_block(genState)
|
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
test "loadTailState gets genesis block on first load" & preset():
|
2019-02-28 21:21:29 +00:00
|
|
|
var
|
|
|
|
pool = BlockPool.init(makeTestDB(genState, genBlock))
|
|
|
|
state = pool.loadTailState()
|
|
|
|
b0 = pool.get(state.blck.root)
|
|
|
|
|
|
|
|
check:
|
2019-05-04 14:10:45 +00:00
|
|
|
state.data.data.slot == GENESIS_SLOT
|
2019-02-28 21:21:29 +00:00
|
|
|
b0.isSome()
|
2019-03-11 15:38:36 +00:00
|
|
|
toSeq(pool.blockRootsForSlot(GENESIS_SLOT)) == @[state.blck.root]
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
test "Simple block add&get" & preset():
|
2019-02-28 21:21:29 +00:00
|
|
|
var
|
|
|
|
pool = BlockPool.init(makeTestDB(genState, genBlock))
|
|
|
|
state = pool.loadTailState()
|
|
|
|
|
|
|
|
let
|
2019-05-04 14:10:45 +00:00
|
|
|
b1 = makeBlock(state.data.data, state.blck.root, BeaconBlockBody())
|
2019-05-09 12:27:37 +00:00
|
|
|
b1Root = signing_root(b1)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
# TODO the return value is ugly here, need to fix and test..
|
2019-03-08 16:40:17 +00:00
|
|
|
discard pool.add(state, b1Root, b1)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
let b1Ref = pool.get(b1Root)
|
|
|
|
|
|
|
|
check:
|
|
|
|
b1Ref.isSome()
|
|
|
|
b1Ref.get().refs.root == b1Root
|
2019-05-04 14:10:45 +00:00
|
|
|
hash_tree_root(state.data.data) == state.data.root
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
test "Reverse order block add & get" & preset():
|
2019-02-28 21:21:29 +00:00
|
|
|
var
|
|
|
|
db = makeTestDB(genState, genBlock)
|
|
|
|
pool = BlockPool.init(db)
|
|
|
|
state = pool.loadTailState()
|
|
|
|
|
|
|
|
let
|
2019-05-04 14:10:45 +00:00
|
|
|
b1 = addBlock(state.data.data, state.blck.root, BeaconBlockBody(), {})
|
2019-05-09 12:27:37 +00:00
|
|
|
b1Root = signing_root(b1)
|
2019-05-04 14:10:45 +00:00
|
|
|
b2 = addBlock(state.data.data, b1Root, BeaconBlockBody(), {})
|
2019-05-09 12:27:37 +00:00
|
|
|
b2Root = signing_root(b2)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-08 16:40:17 +00:00
|
|
|
discard pool.add(state, b2Root, b2)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
pool.get(b2Root).isNone() # Unresolved, shouldn't show up
|
2019-04-26 16:38:56 +00:00
|
|
|
FetchRecord(root: b1Root, historySlots: 1) in pool.checkMissing()
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-08 16:40:17 +00:00
|
|
|
discard pool.add(state, b1Root, b1)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-05-04 14:10:45 +00:00
|
|
|
check: hash_tree_root(state.data.data) == state.data.root
|
2019-04-29 08:14:22 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
let
|
|
|
|
b1r = pool.get(b1Root)
|
|
|
|
b2r = pool.get(b2Root)
|
|
|
|
|
|
|
|
check:
|
|
|
|
b1r.isSome()
|
|
|
|
b2r.isSome()
|
|
|
|
|
|
|
|
b1r.get().refs.children[0] == b2r.get().refs
|
|
|
|
b2r.get().refs.parent == b1r.get().refs
|
2019-03-11 15:38:36 +00:00
|
|
|
toSeq(pool.blockRootsForSlot(b1.slot)) == @[b1Root]
|
|
|
|
toSeq(pool.blockRootsForSlot(b2.slot)) == @[b2Root]
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
db.putHeadBlock(b2Root)
|
|
|
|
|
|
|
|
# check that init also reloads block graph
|
|
|
|
var
|
|
|
|
pool2 = BlockPool.init(db)
|
|
|
|
|
|
|
|
check:
|
2019-05-04 14:10:45 +00:00
|
|
|
hash_tree_root(state.data.data) == state.data.root
|
2019-02-28 21:21:29 +00:00
|
|
|
pool2.get(b1Root).isSome()
|
|
|
|
pool2.get(b2Root).isSome()
|