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
|
|
|
|
|
|
|
suite "Block pool processing":
|
2019-03-08 16:40:17 +00:00
|
|
|
let
|
2019-02-28 21:21:29 +00:00
|
|
|
genState = get_genesis_beacon_state(
|
|
|
|
makeInitialDeposits(flags = {skipValidation}), 0, Eth1Data(),
|
|
|
|
{skipValidation})
|
|
|
|
genBlock = get_initial_beacon_block(genState)
|
|
|
|
|
|
|
|
test "loadTailState gets genesis block on first load":
|
|
|
|
var
|
|
|
|
pool = BlockPool.init(makeTestDB(genState, genBlock))
|
|
|
|
state = pool.loadTailState()
|
|
|
|
b0 = pool.get(state.blck.root)
|
|
|
|
|
|
|
|
check:
|
|
|
|
state.data.slot == GENESIS_SLOT
|
|
|
|
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
|
|
|
|
|
|
|
test "Simple block add&get":
|
|
|
|
var
|
|
|
|
pool = BlockPool.init(makeTestDB(genState, genBlock))
|
|
|
|
state = pool.loadTailState()
|
|
|
|
|
|
|
|
let
|
|
|
|
b1 = makeBlock(state.data, state.blck.root, BeaconBlockBody())
|
|
|
|
b1Root = hash_tree_root_final(b1)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
test "Reverse order block add & get":
|
|
|
|
var
|
|
|
|
db = makeTestDB(genState, genBlock)
|
|
|
|
pool = BlockPool.init(db)
|
|
|
|
state = pool.loadTailState()
|
|
|
|
|
|
|
|
let
|
2019-03-08 16:40:17 +00:00
|
|
|
b1 = addBlock(state.data, state.blck.root, BeaconBlockBody(), {})
|
2019-02-28 21:21:29 +00:00
|
|
|
b1Root = hash_tree_root_final(b1)
|
2019-03-08 16:40:17 +00:00
|
|
|
b2 = addBlock(state.data, b1Root, BeaconBlockBody(), {})
|
2019-02-28 21:21:29 +00:00
|
|
|
b2Root = hash_tree_root_final(b2)
|
|
|
|
|
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
|
|
|
|
b1Root in pool.checkUnresolved()
|
|
|
|
|
2019-03-08 16:40:17 +00:00
|
|
|
discard pool.add(state, b1Root, b1)
|
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:
|
|
|
|
pool2.get(b1Root).isSome()
|
|
|
|
pool2.get(b2Root).isSome()
|