2021-10-19 14:38:41 +00:00
|
|
|
# beacon_chain
|
2022-01-05 08:42:56 +00:00
|
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
2021-10-19 14:38:41 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
# Utilities
|
|
|
|
chronicles,
|
|
|
|
unittest2,
|
|
|
|
stew/results,
|
|
|
|
# Beacon chain internals
|
|
|
|
../../../beacon_chain/spec/[beaconstate, state_transition_block],
|
2022-01-07 17:10:40 +00:00
|
|
|
../../../beacon_chain/spec/datatypes/bellatrix,
|
2021-10-19 14:38:41 +00:00
|
|
|
# Test utilities
|
|
|
|
../../testutil,
|
|
|
|
../fixtures_utils,
|
|
|
|
../../helpers/debug_state
|
|
|
|
|
2022-12-14 17:30:56 +00:00
|
|
|
from std/os import fileExists, walkDir, `/`
|
|
|
|
from std/sequtils import mapIt, toSeq
|
|
|
|
from std/strutils import contains
|
|
|
|
|
2021-10-19 14:38:41 +00:00
|
|
|
const
|
2022-01-03 17:43:00 +00:00
|
|
|
OpDir = SszTestsDir/const_preset/"bellatrix"/"operations"
|
2021-10-19 14:38:41 +00:00
|
|
|
OpAttestationsDir = OpDir/"attestation"
|
|
|
|
OpAttSlashingDir = OpDir/"attester_slashing"
|
|
|
|
OpBlockHeaderDir = OpDir/"block_header"
|
|
|
|
OpDepositsDir = OpDir/"deposit"
|
2021-10-20 11:36:38 +00:00
|
|
|
OpExecutionPayloadDir = OpDir/"execution_payload"
|
2021-10-19 14:38:41 +00:00
|
|
|
OpProposerSlashingDir = OpDir/"proposer_slashing"
|
|
|
|
OpSyncAggregateDir = OpDir/"sync_aggregate"
|
|
|
|
OpVoluntaryExitDir = OpDir/"voluntary_exit"
|
|
|
|
|
2022-01-05 08:42:56 +00:00
|
|
|
baseDescription = "EF - Bellatrix - Operations - "
|
2021-10-19 14:38:41 +00:00
|
|
|
|
2022-01-28 22:47:48 +00:00
|
|
|
doAssert toHashSet(mapIt(toSeq(walkDir(OpDir, relative = false)), it.path)) ==
|
2021-10-20 11:36:38 +00:00
|
|
|
toHashSet([OpAttestationsDir, OpAttSlashingDir, OpBlockHeaderDir,
|
|
|
|
OpDepositsDir, OpExecutionPayloadDir, OpProposerSlashingDir,
|
|
|
|
OpSyncAggregateDir, OpVoluntaryExitDir])
|
|
|
|
|
2021-10-19 14:38:41 +00:00
|
|
|
proc runTest[T, U](
|
|
|
|
testSuiteDir: string, testSuiteName: string, applyFile: string,
|
|
|
|
applyProc: U, identifier: string) =
|
|
|
|
let testDir = testSuiteDir / "pyspec_tests" / identifier
|
|
|
|
|
|
|
|
proc testImpl() =
|
|
|
|
let prefix =
|
2022-03-24 00:38:48 +00:00
|
|
|
if fileExists(testDir/"post.ssz_snappy"):
|
2021-10-19 14:38:41 +00:00
|
|
|
"[Valid] "
|
|
|
|
else:
|
|
|
|
"[Invalid] "
|
|
|
|
|
|
|
|
test prefix & baseDescription & testSuiteName & " - " & identifier:
|
2022-02-20 20:13:06 +00:00
|
|
|
let preState = newClone(
|
2022-01-07 17:10:40 +00:00
|
|
|
parseTest(testDir/"pre.ssz_snappy", SSZ, bellatrix.BeaconState))
|
2021-10-19 14:38:41 +00:00
|
|
|
let done = applyProc(
|
|
|
|
preState[], parseTest(testDir/(applyFile & ".ssz_snappy"), SSZ, T))
|
|
|
|
|
2022-03-24 00:38:48 +00:00
|
|
|
if fileExists(testDir/"post.ssz_snappy"):
|
2021-10-19 14:38:41 +00:00
|
|
|
let postState =
|
2022-01-07 17:10:40 +00:00
|
|
|
newClone(parseTest(
|
|
|
|
testDir/"post.ssz_snappy", SSZ, bellatrix.BeaconState))
|
2021-10-19 14:38:41 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
done.isOk()
|
|
|
|
preState[].hash_tree_root() == postState[].hash_tree_root()
|
|
|
|
reportDiff(preState, postState)
|
|
|
|
else:
|
|
|
|
check: done.isErr() # No post state = processing should fail
|
|
|
|
|
|
|
|
testImpl()
|
|
|
|
|
|
|
|
suite baseDescription & "Attestation " & preset():
|
|
|
|
proc applyAttestation(
|
2022-01-07 17:10:40 +00:00
|
|
|
preState: var bellatrix.BeaconState, attestation: Attestation):
|
2021-10-19 14:38:41 +00:00
|
|
|
Result[void, cstring] =
|
|
|
|
var cache = StateCache()
|
|
|
|
let
|
|
|
|
total_active_balance = get_total_active_balance(preState, cache)
|
|
|
|
base_reward_per_increment =
|
|
|
|
get_base_reward_per_increment(total_active_balance)
|
|
|
|
|
|
|
|
process_attestation(
|
|
|
|
preState, attestation, {}, base_reward_per_increment, cache)
|
|
|
|
|
|
|
|
for path in walkTests(OpAttestationsDir):
|
|
|
|
runTest[Attestation, typeof applyAttestation](
|
|
|
|
OpAttestationsDir, "Attestation", "attestation", applyAttestation, path)
|
|
|
|
|
|
|
|
suite baseDescription & "Attester Slashing " & preset():
|
|
|
|
proc applyAttesterSlashing(
|
2022-01-07 17:10:40 +00:00
|
|
|
preState: var bellatrix.BeaconState, attesterSlashing: AttesterSlashing):
|
2021-10-19 14:38:41 +00:00
|
|
|
Result[void, cstring] =
|
|
|
|
var cache = StateCache()
|
|
|
|
process_attester_slashing(
|
|
|
|
defaultRuntimeConfig, preState, attesterSlashing, {}, cache)
|
|
|
|
|
|
|
|
for path in walkTests(OpAttSlashingDir):
|
|
|
|
runTest[AttesterSlashing, typeof applyAttesterSlashing](
|
|
|
|
OpAttSlashingDir, "Attester Slashing", "attester_slashing",
|
|
|
|
applyAttesterSlashing, path)
|
|
|
|
|
|
|
|
suite baseDescription & "Block Header " & preset():
|
|
|
|
func applyBlockHeader(
|
2022-01-07 17:10:40 +00:00
|
|
|
preState: var bellatrix.BeaconState, blck: bellatrix.BeaconBlock):
|
2021-10-19 14:38:41 +00:00
|
|
|
Result[void, cstring] =
|
|
|
|
var cache = StateCache()
|
|
|
|
process_block_header(preState, blck, {}, cache)
|
|
|
|
|
|
|
|
for path in walkTests(OpBlockHeaderDir):
|
2022-01-07 17:10:40 +00:00
|
|
|
runTest[bellatrix.BeaconBlock, typeof applyBlockHeader](
|
2021-10-19 14:38:41 +00:00
|
|
|
OpBlockHeaderDir, "Block Header", "block", applyBlockHeader, path)
|
|
|
|
|
|
|
|
suite baseDescription & "Deposit " & preset():
|
|
|
|
proc applyDeposit(
|
2022-01-07 17:10:40 +00:00
|
|
|
preState: var bellatrix.BeaconState, deposit: Deposit):
|
2021-10-19 14:38:41 +00:00
|
|
|
Result[void, cstring] =
|
|
|
|
process_deposit(defaultRuntimeConfig, preState, deposit, {})
|
|
|
|
|
|
|
|
for path in walkTests(OpDepositsDir):
|
|
|
|
runTest[Deposit, typeof applyDeposit](
|
|
|
|
OpDepositsDir, "Deposit", "deposit", applyDeposit, path)
|
|
|
|
|
2021-10-20 11:36:38 +00:00
|
|
|
suite baseDescription & "Execution Payload " & preset():
|
|
|
|
for path in walkTests(OpExecutionPayloadDir):
|
|
|
|
proc applyExecutionPayload(
|
2022-01-07 17:10:40 +00:00
|
|
|
preState: var bellatrix.BeaconState,
|
|
|
|
executionPayload: ExecutionPayload):
|
2021-10-20 11:36:38 +00:00
|
|
|
Result[void, cstring] =
|
|
|
|
let payloadValid =
|
|
|
|
readFile(OpExecutionPayloadDir/"pyspec_tests"/path/"execution.yaml").
|
|
|
|
contains("execution_valid: true")
|
|
|
|
func executePayload(_: ExecutionPayload): bool = payloadValid
|
|
|
|
process_execution_payload(
|
|
|
|
preState, executionPayload, executePayload)
|
|
|
|
|
|
|
|
runTest[ExecutionPayload, typeof applyExecutionPayload](
|
|
|
|
OpExecutionPayloadDir, "Execution Payload", "execution_payload",
|
|
|
|
applyExecutionPayload, path)
|
|
|
|
|
2021-10-19 14:38:41 +00:00
|
|
|
suite baseDescription & "Proposer Slashing " & preset():
|
|
|
|
proc applyProposerSlashing(
|
2022-01-07 17:10:40 +00:00
|
|
|
preState: var bellatrix.BeaconState, proposerSlashing: ProposerSlashing):
|
2021-10-19 14:38:41 +00:00
|
|
|
Result[void, cstring] =
|
|
|
|
var cache = StateCache()
|
|
|
|
process_proposer_slashing(
|
|
|
|
defaultRuntimeConfig, preState, proposerSlashing, {}, cache)
|
|
|
|
|
|
|
|
for path in walkTests(OpProposerSlashingDir):
|
|
|
|
runTest[ProposerSlashing, typeof applyProposerSlashing](
|
|
|
|
OpProposerSlashingDir, "Proposer Slashing", "proposer_slashing",
|
|
|
|
applyProposerSlashing, path)
|
|
|
|
|
|
|
|
suite baseDescription & "Sync Aggregate " & preset():
|
|
|
|
proc applySyncAggregate(
|
2022-01-07 17:10:40 +00:00
|
|
|
preState: var bellatrix.BeaconState, syncAggregate: SyncAggregate):
|
2021-10-19 14:38:41 +00:00
|
|
|
Result[void, cstring] =
|
|
|
|
var cache = StateCache()
|
|
|
|
process_sync_aggregate(
|
|
|
|
preState, syncAggregate, get_total_active_balance(preState, cache), cache)
|
|
|
|
|
|
|
|
for path in walkTests(OpSyncAggregateDir):
|
|
|
|
runTest[SyncAggregate, typeof applySyncAggregate](
|
|
|
|
OpSyncAggregateDir, "Sync Aggregate", "sync_aggregate",
|
|
|
|
applySyncAggregate, path)
|
|
|
|
|
|
|
|
suite baseDescription & "Voluntary Exit " & preset():
|
|
|
|
proc applyVoluntaryExit(
|
2022-01-07 17:10:40 +00:00
|
|
|
preState: var bellatrix.BeaconState, voluntaryExit: SignedVoluntaryExit):
|
2021-10-19 14:38:41 +00:00
|
|
|
Result[void, cstring] =
|
|
|
|
var cache = StateCache()
|
|
|
|
process_voluntary_exit(
|
|
|
|
defaultRuntimeConfig, preState, voluntaryExit, {}, cache)
|
|
|
|
|
|
|
|
for path in walkTests(OpVoluntaryExitDir):
|
|
|
|
runTest[SignedVoluntaryExit, typeof applyVoluntaryExit](
|
|
|
|
OpVoluntaryExitDir, "Voluntary Exit", "voluntary_exit",
|
|
|
|
applyVoluntaryExit, path)
|