nimbus-eth2/tests/test_block_pool.nim

92 lines
2.7 KiB
Nim

# 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],
../beacon_chain/[block_pool, beacon_chain_db, extras, state_transition, ssz]
suite "Block pool processing":
let
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()
toSeq(pool.blockRootsForSlot(GENESIS_SLOT)) == @[state.blck.root]
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..
discard pool.add(state, b1Root, b1)
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
b1 = addBlock(state.data, state.blck.root, BeaconBlockBody(), {})
b1Root = hash_tree_root_final(b1)
b2 = addBlock(state.data, b1Root, BeaconBlockBody(), {})
b2Root = hash_tree_root_final(b2)
discard pool.add(state, b2Root, b2)
check:
pool.get(b2Root).isNone() # Unresolved, shouldn't show up
b1Root in pool.checkUnresolved()
discard pool.add(state, b1Root, b1)
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
toSeq(pool.blockRootsForSlot(b1.slot)) == @[b1Root]
toSeq(pool.blockRootsForSlot(b2.slot)) == @[b2Root]
db.putHeadBlock(b2Root)
# check that init also reloads block graph
var
pool2 = BlockPool.init(db)
check:
pool2.get(b1Root).isSome()
pool2.get(b2Root).isSome()