2019-02-21 17:20:50 +00:00
|
|
|
# Nimbus
|
2021-03-15 14:11:51 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2019-02-21 17:20:50 +00:00
|
|
|
# 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.}
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
import
|
|
|
|
std/[algorithm, options, sequtils],
|
|
|
|
unittest2,
|
2021-03-15 14:11:51 +00:00
|
|
|
../beacon_chain/[beacon_chain_db, extras, interop, ssz],
|
2020-06-23 13:54:24 +00:00
|
|
|
../beacon_chain/spec/[
|
2021-06-24 07:11:47 +00:00
|
|
|
beaconstate, crypto, digest, forkedbeaconstate_helpers, presets,
|
2021-06-11 17:51:46 +00:00
|
|
|
state_transition],
|
2021-06-24 07:11:47 +00:00
|
|
|
../beacon_chain/spec/datatypes/[phase0, altair],
|
2021-03-15 14:11:51 +00:00
|
|
|
../beacon_chain/consensus_object_pools/blockchain_dag,
|
2020-04-27 16:36:28 +00:00
|
|
|
eth/db/kvstore,
|
2019-05-27 12:48:13 +00:00
|
|
|
# test utilies
|
2021-04-28 16:41:02 +00:00
|
|
|
./testutil, ./testdbutil, ./testblockutil, ./teststateutil
|
2021-03-15 14:11:51 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
import chronicles # or some random compile error happens...
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2021-06-24 07:11:47 +00:00
|
|
|
proc getPhase0StateRef(db: BeaconChainDB, root: Eth2Digest):
|
|
|
|
phase0.NilableBeaconStateRef =
|
2020-07-30 19:18:17 +00:00
|
|
|
# load beaconstate the way the block pool does it - into an existing instance
|
2021-06-24 07:11:47 +00:00
|
|
|
let res = (phase0.BeaconStateRef)()
|
2020-04-28 08:08:32 +00:00
|
|
|
if db.getState(root, res[], noRollback):
|
|
|
|
return res
|
|
|
|
|
2021-06-24 07:11:47 +00:00
|
|
|
proc getAltairStateRef(db: BeaconChainDB, root: Eth2Digest):
|
|
|
|
altair.NilableBeaconStateRef =
|
|
|
|
# load beaconstate the way the block pool does it - into an existing instance
|
|
|
|
let res = (altair.BeaconStateRef)()
|
|
|
|
if db.getAltairState(root, res[], noRollback):
|
|
|
|
return res
|
|
|
|
|
|
|
|
func withDigest(blck: phase0.TrustedBeaconBlock):
|
|
|
|
phase0.TrustedSignedBeaconBlock =
|
|
|
|
phase0.TrustedSignedBeaconBlock(
|
|
|
|
message: blck,
|
|
|
|
root: hash_tree_root(blck)
|
|
|
|
)
|
|
|
|
|
|
|
|
func withDigest(blck: altair.TrustedBeaconBlock):
|
|
|
|
altair.TrustedSignedBeaconBlock =
|
|
|
|
altair.TrustedSignedBeaconBlock(
|
2020-07-16 13:16:51 +00:00
|
|
|
message: blck,
|
|
|
|
root: hash_tree_root(blck)
|
|
|
|
)
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
suite "Beacon chain DB" & preset():
|
|
|
|
test "empty database" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
var
|
2021-04-06 18:56:45 +00:00
|
|
|
db = BeaconChainDB.new(defaultRuntimePreset, "", inMemory = true)
|
2019-02-21 17:20:50 +00:00
|
|
|
check:
|
2021-06-24 07:11:47 +00:00
|
|
|
db.getPhase0StateRef(Eth2Digest()).isNil
|
2020-04-28 08:08:32 +00:00
|
|
|
db.getBlock(Eth2Digest()).isNone
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2021-06-24 07:11:47 +00:00
|
|
|
test "sanity check phase 0 blocks" & preset():
|
2021-06-29 15:09:29 +00:00
|
|
|
var db = BeaconChainDB.new(defaultRuntimePreset, "", inMemory = true)
|
2019-03-08 16:40:17 +00:00
|
|
|
|
|
|
|
let
|
2021-06-24 07:11:47 +00:00
|
|
|
signedBlock = withDigest((phase0.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
|
|
|
|
2021-06-24 07:11:47 +00:00
|
|
|
db.delBlock(root)
|
|
|
|
check:
|
|
|
|
not db.containsBlock(root)
|
|
|
|
db.getBlock(root).isErr()
|
|
|
|
|
|
|
|
db.putStateRoot(root, signedBlock.message.slot, root)
|
|
|
|
var root2 = root
|
|
|
|
root2.data[0] = root.data[0] + 1
|
|
|
|
db.putStateRoot(root, signedBlock.message.slot + 1, root2)
|
|
|
|
|
|
|
|
check:
|
|
|
|
db.getStateRoot(root, signedBlock.message.slot).get() == root
|
|
|
|
db.getStateRoot(root, signedBlock.message.slot + 1).get() == root2
|
|
|
|
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
test "sanity check Altair blocks" & preset():
|
2021-06-29 15:09:29 +00:00
|
|
|
var db = BeaconChainDB.new(defaultRuntimePreset, "", inMemory = true)
|
2021-06-24 07:11:47 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
signedBlock = withDigest((altair.TrustedBeaconBlock)())
|
|
|
|
root = hash_tree_root(signedBlock.message)
|
|
|
|
|
|
|
|
db.putBlock(signedBlock)
|
|
|
|
|
|
|
|
check:
|
|
|
|
db.containsBlock(root)
|
|
|
|
db.getAltairBlock(root).get() == signedBlock
|
|
|
|
|
|
|
|
db.delBlock(root)
|
|
|
|
check:
|
|
|
|
not db.containsBlock(root)
|
|
|
|
db.getAltairBlock(root).isErr()
|
|
|
|
|
2020-02-29 15:15:44 +00:00
|
|
|
db.putStateRoot(root, signedBlock.message.slot, root)
|
2020-08-07 20:17:24 +00:00
|
|
|
var root2 = root
|
|
|
|
root2.data[0] = root.data[0] + 1
|
|
|
|
db.putStateRoot(root, signedBlock.message.slot + 1, root2)
|
|
|
|
|
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
|
2020-08-07 20:17:24 +00:00
|
|
|
db.getStateRoot(root, signedBlock.message.slot + 1).get() == root2
|
2019-03-28 06:10:48 +00:00
|
|
|
|
2020-09-12 05:35:58 +00:00
|
|
|
db.close()
|
|
|
|
|
2021-06-24 07:11:47 +00:00
|
|
|
test "sanity check phase 0 states" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
var
|
2021-03-15 14:11:51 +00:00
|
|
|
db = makeTestDB(SLOTS_PER_EPOCH)
|
|
|
|
dag = init(ChainDAGRef, defaultRuntimePreset, db)
|
|
|
|
testStates = getTestStates(dag.headState.data)
|
2019-03-08 16:40:17 +00:00
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
# Ensure transitions beyond just adding validators and increasing slots
|
2021-06-11 17:51:46 +00:00
|
|
|
sort(testStates) do (x, y: ref ForkedHashedBeaconState) -> int:
|
|
|
|
cmp($getStateRoot(x[]), $getStateRoot(y[]))
|
2019-03-08 16:40:17 +00:00
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
for state in testStates:
|
2021-06-11 17:51:46 +00:00
|
|
|
db.putState(state[].hbsPhase0.data)
|
|
|
|
let root = hash_tree_root(state[])
|
2019-03-08 16:40:17 +00:00
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
check:
|
|
|
|
db.containsState(root)
|
2021-06-24 07:11:47 +00:00
|
|
|
hash_tree_root(db.getPhase0StateRef(root)[]) == root
|
2021-03-15 14:11:51 +00:00
|
|
|
|
|
|
|
db.delState(root)
|
2021-06-24 07:11:47 +00:00
|
|
|
check:
|
|
|
|
not db.containsState(root)
|
|
|
|
db.getPhase0StateRef(root).isNil
|
2021-03-15 14:11:51 +00:00
|
|
|
|
|
|
|
db.close()
|
|
|
|
|
2021-06-24 07:11:47 +00:00
|
|
|
test "sanity check Altair states" & preset():
|
2021-03-15 14:11:51 +00:00
|
|
|
var
|
|
|
|
db = makeTestDB(SLOTS_PER_EPOCH)
|
|
|
|
dag = init(ChainDAGRef, defaultRuntimePreset, db)
|
2021-06-24 07:11:47 +00:00
|
|
|
testStates = getTestStates(dag.headState.data, true)
|
|
|
|
|
|
|
|
# Ensure transitions beyond just adding validators and increasing slots
|
|
|
|
sort(testStates) do (x, y: ref ForkedHashedBeaconState) -> int:
|
|
|
|
cmp($getStateRoot(x[]), $getStateRoot(y[]))
|
2021-03-15 14:11:51 +00:00
|
|
|
|
2021-06-24 07:11:47 +00:00
|
|
|
for state in testStates:
|
|
|
|
db.putState(state[].hbsAltair.data)
|
|
|
|
let root = hash_tree_root(state[])
|
|
|
|
|
|
|
|
check:
|
|
|
|
db.containsState(root)
|
|
|
|
hash_tree_root(db.getAltairStateRef(root)[]) == root
|
|
|
|
|
|
|
|
db.delState(root)
|
|
|
|
check:
|
|
|
|
not db.containsState(root)
|
|
|
|
db.getAltairStateRef(root).isNil
|
|
|
|
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
test "sanity check phase 0 states, reusing buffers" & preset():
|
|
|
|
var
|
|
|
|
db = makeTestDB(SLOTS_PER_EPOCH)
|
|
|
|
dag = init(ChainDAGRef, defaultRuntimePreset, db)
|
|
|
|
|
|
|
|
let stateBuffer = (phase0.BeaconStateRef)()
|
2021-03-15 14:11:51 +00:00
|
|
|
var testStates = getTestStates(dag.headState.data)
|
|
|
|
|
|
|
|
# Ensure transitions beyond just adding validators and increasing slots
|
2021-06-11 17:51:46 +00:00
|
|
|
sort(testStates) do (x, y: ref ForkedHashedBeaconState) -> int:
|
|
|
|
cmp($getStateRoot(x[]), $getStateRoot(y[]))
|
2021-03-15 14:11:51 +00:00
|
|
|
|
|
|
|
for state in testStates:
|
2021-06-11 17:51:46 +00:00
|
|
|
db.putState(state[].hbsPhase0.data)
|
|
|
|
let root = hash_tree_root(state[])
|
2021-03-15 14:11:51 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
db.getState(root, stateBuffer[], noRollback)
|
|
|
|
db.containsState(root)
|
|
|
|
hash_tree_root(stateBuffer[]) == root
|
|
|
|
|
|
|
|
db.delState(root)
|
2021-06-24 07:11:47 +00:00
|
|
|
check:
|
|
|
|
not db.containsState(root)
|
|
|
|
not db.getState(root, stateBuffer[], noRollback)
|
|
|
|
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
test "sanity check Altair states, reusing buffers" & preset():
|
|
|
|
var
|
|
|
|
db = makeTestDB(SLOTS_PER_EPOCH)
|
|
|
|
dag = init(ChainDAGRef, defaultRuntimePreset, db)
|
|
|
|
|
|
|
|
let stateBuffer = (altair.BeaconStateRef)()
|
|
|
|
var testStates = getTestStates(dag.headState.data, true)
|
|
|
|
|
|
|
|
# Ensure transitions beyond just adding validators and increasing slots
|
|
|
|
sort(testStates) do (x, y: ref ForkedHashedBeaconState) -> int:
|
|
|
|
cmp($getStateRoot(x[]), $getStateRoot(y[]))
|
|
|
|
|
|
|
|
for state in testStates:
|
|
|
|
db.putState(state[].hbsAltair.data)
|
|
|
|
let root = hash_tree_root(state[])
|
|
|
|
|
|
|
|
check:
|
|
|
|
db.getAltairState(root, stateBuffer[], noRollback)
|
|
|
|
db.containsState(root)
|
|
|
|
hash_tree_root(stateBuffer[]) == root
|
|
|
|
|
|
|
|
db.delState(root)
|
|
|
|
check:
|
|
|
|
not db.containsState(root)
|
|
|
|
not db.getAltairState(root, stateBuffer[], noRollback)
|
2021-03-15 14:11:51 +00:00
|
|
|
|
|
|
|
db.close()
|
|
|
|
|
2021-06-29 15:09:29 +00:00
|
|
|
test "sanity check phase 0 getState rollback" & preset():
|
|
|
|
var
|
|
|
|
db = makeTestDB(SLOTS_PER_EPOCH)
|
|
|
|
dag = init(ChainDAGRef, defaultRuntimePreset, db)
|
|
|
|
state = (ref ForkedHashedBeaconState)(
|
|
|
|
beaconStateFork: forkPhase0,
|
|
|
|
hbsPhase0: phase0.HashedBeaconState(data: phase0.BeaconState(
|
|
|
|
slot: 10.Slot)))
|
|
|
|
root = Eth2Digest()
|
|
|
|
|
|
|
|
db.putCorruptPhase0State(root)
|
|
|
|
|
|
|
|
let restoreAddr = addr dag.headState
|
|
|
|
|
|
|
|
func restore() =
|
|
|
|
assign(state[], restoreAddr[].data)
|
|
|
|
|
|
|
|
check:
|
|
|
|
state[].hbsPhase0.data.slot == 10.Slot
|
|
|
|
not db.getState(root, state[].hbsPhase0.data, restore)
|
|
|
|
state[].hbsPhase0.data.slot != 10.Slot
|
|
|
|
|
|
|
|
test "sanity check Altair and cross-fork getState rollback" & preset():
|
|
|
|
var
|
|
|
|
db = makeTestDB(SLOTS_PER_EPOCH)
|
|
|
|
dag = init(ChainDAGRef, defaultRuntimePreset, db)
|
|
|
|
state = (ref ForkedHashedBeaconState)(
|
|
|
|
beaconStateFork: forkAltair,
|
|
|
|
hbsAltair: altair.HashedBeaconState(data: altair.BeaconState(
|
|
|
|
slot: 10.Slot)))
|
|
|
|
root = Eth2Digest()
|
|
|
|
|
|
|
|
db.putCorruptAltairState(root)
|
|
|
|
|
|
|
|
let restoreAddr = addr dag.headState
|
|
|
|
|
|
|
|
func restore() =
|
|
|
|
assign(state[], restoreAddr[].data)
|
|
|
|
|
|
|
|
check:
|
|
|
|
state[].hbsAltair.data.slot == 10.Slot
|
|
|
|
not db.getAltairState(root, state[].hbsAltair.data, restore)
|
|
|
|
|
|
|
|
# assign() has switched the case object fork
|
|
|
|
state[].beaconStateFork == forkPhase0
|
|
|
|
state[].hbsPhase0.data.slot != 10.Slot
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test "find ancestors" & preset():
|
2019-03-08 16:40:17 +00:00
|
|
|
var
|
2021-04-06 18:56:45 +00:00
|
|
|
db = BeaconChainDB.new(defaultRuntimePreset, "", inMemory = true)
|
2019-02-21 17:20:50 +00:00
|
|
|
|
|
|
|
let
|
2020-07-16 13:16:51 +00:00
|
|
|
a0 = withDigest(
|
2021-06-24 07:11:47 +00:00
|
|
|
(phase0.TrustedBeaconBlock)(slot: GENESIS_SLOT + 0))
|
2020-07-16 13:16:51 +00:00
|
|
|
a1 = withDigest(
|
2021-06-24 07:11:47 +00:00
|
|
|
(phase0.TrustedBeaconBlock)(slot: GENESIS_SLOT + 1, parent_root: a0.root))
|
2020-07-16 13:16:51 +00:00
|
|
|
a2 = withDigest(
|
2021-06-24 07:11:47 +00:00
|
|
|
(phase0.TrustedBeaconBlock)(slot: GENESIS_SLOT + 2, parent_root: a1.root))
|
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
|
|
|
|
2020-11-03 22:30:43 +00:00
|
|
|
doAssert toSeq(db.getAncestorSummaries(a0.root)).len == 0
|
|
|
|
doAssert toSeq(db.getAncestorSummaries(a2.root)).len == 0
|
|
|
|
|
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
|
|
|
|
2020-11-03 22:30:43 +00:00
|
|
|
doAssert toSeq(db.getAncestorSummaries(a0.root)).len == 0
|
|
|
|
doAssert toSeq(db.getAncestorSummaries(a2.root)).len == 1
|
|
|
|
|
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
|
|
|
|
2020-11-03 22:30:43 +00:00
|
|
|
doAssert toSeq(db.getAncestorSummaries(a0.root)).len == 0
|
|
|
|
doAssert toSeq(db.getAncestorSummaries(a2.root)).len == 2
|
|
|
|
|
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-11-03 22:30:43 +00:00
|
|
|
doAssert toSeq(db.getAncestorSummaries(a0.root)).len == 1
|
|
|
|
doAssert toSeq(db.getAncestorSummaries(a2.root)).len == 3
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test "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
|
2021-04-06 18:56:45 +00:00
|
|
|
db = BeaconChainDB.new(defaultRuntimePreset, "", inMemory = true)
|
2019-09-05 14:27:28 +00:00
|
|
|
|
|
|
|
let
|
2021-05-04 10:19:11 +00:00
|
|
|
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)
|
2021-06-24 07:11:47 +00:00
|
|
|
let state2 = db.getPhase0StateRef(root)
|
2021-01-18 20:34:41 +00:00
|
|
|
db.delState(root)
|
|
|
|
check not db.containsState(root)
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
check:
|
|
|
|
hash_tree_root(state2[]) == root
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test "sanity check state diff roundtrip" & preset():
|
2021-01-18 20:34:41 +00:00
|
|
|
var
|
2021-04-06 18:56:45 +00:00
|
|
|
db = BeaconChainDB.new(defaultRuntimePreset, "", inMemory = true)
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
# TODO htr(diff) probably not interesting/useful, but stand-in
|
|
|
|
let
|
|
|
|
stateDiff = BeaconStateDiff()
|
|
|
|
root = hash_tree_root(stateDiff)
|
|
|
|
|
|
|
|
db.putStateDiff(root, stateDiff)
|
|
|
|
|
|
|
|
let state2 = db.getStateDiff(root)
|
|
|
|
db.delStateDiff(root)
|
2021-05-17 16:37:26 +00:00
|
|
|
check db.getStateDiff(root).isNone()
|
2020-09-12 05:35:58 +00:00
|
|
|
db.close()
|
2020-05-28 08:28:14 +00:00
|
|
|
|
2019-09-05 14:27:28 +00:00
|
|
|
check:
|
2020-05-28 08:28:14 +00:00
|
|
|
hash_tree_root(state2[]) == root
|