2022-10-27 06:29:24 +00:00
|
|
|
# beacon_chain
|
2023-01-14 04:20:51 +00:00
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-10-27 06:29:24 +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
|
2023-05-25 07:55:00 +00:00
|
|
|
../../../beacon_chain/spec/state_transition_block,
|
2022-10-27 06:29:24 +00:00
|
|
|
../../../beacon_chain/spec/datatypes/capella,
|
|
|
|
# Test utilities
|
|
|
|
../../testutil,
|
2023-02-10 20:59:38 +00:00
|
|
|
../fixtures_utils, ../os_ops,
|
2022-10-27 06:29:24 +00:00
|
|
|
../../helpers/debug_state
|
|
|
|
|
2023-05-25 07:55:00 +00:00
|
|
|
from std/sequtils import mapIt, toSeq
|
|
|
|
from std/strutils import contains
|
|
|
|
from ../../../beacon_chain/spec/beaconstate import
|
|
|
|
get_base_reward_per_increment, get_total_active_balance, process_attestation
|
|
|
|
|
2022-10-27 06:29:24 +00:00
|
|
|
const
|
|
|
|
OpDir = SszTestsDir/const_preset/"capella"/"operations"
|
|
|
|
OpAttestationsDir = OpDir/"attestation"
|
|
|
|
OpAttSlashingDir = OpDir/"attester_slashing"
|
|
|
|
OpBlockHeaderDir = OpDir/"block_header"
|
|
|
|
OpBlsToExecutionChangeDir = OpDir/"bls_to_execution_change"
|
|
|
|
OpDepositsDir = OpDir/"deposit"
|
|
|
|
OpExecutionPayloadDir = OpDir/"execution_payload"
|
|
|
|
OpProposerSlashingDir = OpDir/"proposer_slashing"
|
|
|
|
OpSyncAggregateDir = OpDir/"sync_aggregate"
|
|
|
|
OpVoluntaryExitDir = OpDir/"voluntary_exit"
|
|
|
|
OpWithdrawalsDir = OpDir/"withdrawals"
|
|
|
|
|
|
|
|
baseDescription = "EF - Capella - Operations - "
|
|
|
|
|
|
|
|
doAssert toHashSet(mapIt(toSeq(walkDir(OpDir, relative = false)), it.path)) ==
|
|
|
|
toHashSet([
|
|
|
|
OpAttestationsDir, OpAttSlashingDir, OpBlockHeaderDir,
|
|
|
|
OpBlsToExecutionChangeDir, OpDepositsDir, OpExecutionPayloadDir,
|
|
|
|
OpProposerSlashingDir, OpSyncAggregateDir, OpVoluntaryExitDir,
|
|
|
|
OpWithdrawalsDir])
|
|
|
|
|
|
|
|
proc runTest[T, U](
|
|
|
|
testSuiteDir: string, testSuiteName: string, applyFile: string,
|
|
|
|
applyProc: U, identifier: string) =
|
|
|
|
let testDir = testSuiteDir / "pyspec_tests" / identifier
|
|
|
|
|
|
|
|
proc testImpl() =
|
|
|
|
let prefix =
|
|
|
|
if fileExists(testDir/"post.ssz_snappy"):
|
|
|
|
"[Valid] "
|
|
|
|
else:
|
|
|
|
"[Invalid] "
|
|
|
|
|
|
|
|
test prefix & baseDescription & testSuiteName & " - " & identifier:
|
|
|
|
let preState = newClone(
|
|
|
|
parseTest(testDir/"pre.ssz_snappy", SSZ, capella.BeaconState))
|
|
|
|
let done = applyProc(
|
|
|
|
preState[], parseTest(testDir/(applyFile & ".ssz_snappy"), SSZ, T))
|
|
|
|
|
|
|
|
if fileExists(testDir/"post.ssz_snappy"):
|
|
|
|
let postState =
|
|
|
|
newClone(parseTest(
|
|
|
|
testDir/"post.ssz_snappy", SSZ, capella.BeaconState))
|
|
|
|
|
|
|
|
reportDiff(preState, postState)
|
|
|
|
check:
|
|
|
|
done.isOk()
|
|
|
|
preState[].hash_tree_root() == postState[].hash_tree_root()
|
|
|
|
else:
|
|
|
|
check: done.isErr() # No post state = processing should fail
|
|
|
|
|
|
|
|
testImpl()
|
|
|
|
|
|
|
|
suite baseDescription & "Attestation " & preset():
|
|
|
|
proc applyAttestation(
|
|
|
|
preState: var capella.BeaconState, attestation: Attestation):
|
|
|
|
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(
|
|
|
|
preState: var capella.BeaconState, attesterSlashing: AttesterSlashing):
|
|
|
|
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(
|
|
|
|
preState: var capella.BeaconState, blck: capella.BeaconBlock):
|
|
|
|
Result[void, cstring] =
|
|
|
|
var cache = StateCache()
|
|
|
|
process_block_header(preState, blck, {}, cache)
|
|
|
|
|
|
|
|
for path in walkTests(OpBlockHeaderDir):
|
|
|
|
runTest[capella.BeaconBlock, typeof applyBlockHeader](
|
|
|
|
OpBlockHeaderDir, "Block Header", "block", applyBlockHeader, path)
|
|
|
|
|
|
|
|
suite baseDescription & "BLS to execution change " & preset():
|
|
|
|
proc applyBlsToExecutionChange(
|
|
|
|
preState: var capella.BeaconState,
|
|
|
|
signed_address_change: SignedBLSToExecutionChange):
|
|
|
|
Result[void, cstring] =
|
2023-01-14 04:20:51 +00:00
|
|
|
process_bls_to_execution_change(
|
|
|
|
defaultRuntimeConfig, preState, signed_address_change)
|
2022-10-27 06:29:24 +00:00
|
|
|
|
|
|
|
for path in walkTests(OpBlsToExecutionChangeDir):
|
|
|
|
runTest[SignedBLSToExecutionChange, typeof applyBlsToExecutionChange](
|
|
|
|
OpBlsToExecutionChangeDir, "BLS to execution change", "address_change",
|
|
|
|
applyBlsToExecutionChange, path)
|
|
|
|
|
|
|
|
suite baseDescription & "Deposit " & preset():
|
|
|
|
proc applyDeposit(
|
|
|
|
preState: var capella.BeaconState, deposit: Deposit):
|
|
|
|
Result[void, cstring] =
|
|
|
|
process_deposit(defaultRuntimeConfig, preState, deposit, {})
|
|
|
|
|
|
|
|
for path in walkTests(OpDepositsDir):
|
|
|
|
runTest[Deposit, typeof applyDeposit](
|
|
|
|
OpDepositsDir, "Deposit", "deposit", applyDeposit, path)
|
|
|
|
|
|
|
|
suite baseDescription & "Execution Payload " & preset():
|
|
|
|
for path in walkTests(OpExecutionPayloadDir):
|
|
|
|
proc applyExecutionPayload(
|
2023-05-25 07:55:00 +00:00
|
|
|
preState: var capella.BeaconState, body: capella.BeaconBlockBody):
|
2022-10-27 06:29:24 +00:00
|
|
|
Result[void, cstring] =
|
|
|
|
let payloadValid =
|
2023-02-10 20:59:38 +00:00
|
|
|
os_ops.readFile(OpExecutionPayloadDir/"pyspec_tests"/path/"execution.yaml").
|
2022-10-27 06:29:24 +00:00
|
|
|
contains("execution_valid: true")
|
|
|
|
func executePayload(_: capella.ExecutionPayload): bool = payloadValid
|
|
|
|
process_execution_payload(
|
2023-05-25 07:55:00 +00:00
|
|
|
preState, body.execution_payload, executePayload)
|
2022-10-27 06:29:24 +00:00
|
|
|
|
2023-05-25 07:55:00 +00:00
|
|
|
runTest[capella.BeaconBlockBody, typeof applyExecutionPayload](
|
|
|
|
OpExecutionPayloadDir, "Execution Payload", "body",
|
2022-10-27 06:29:24 +00:00
|
|
|
applyExecutionPayload, path)
|
|
|
|
|
|
|
|
suite baseDescription & "Proposer Slashing " & preset():
|
|
|
|
proc applyProposerSlashing(
|
|
|
|
preState: var capella.BeaconState, proposerSlashing: ProposerSlashing):
|
|
|
|
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(
|
|
|
|
preState: var capella.BeaconState, syncAggregate: SyncAggregate):
|
|
|
|
Result[void, cstring] =
|
|
|
|
var cache = StateCache()
|
|
|
|
process_sync_aggregate(
|
2023-06-17 06:00:01 +00:00
|
|
|
preState, syncAggregate, get_total_active_balance(preState, cache),
|
|
|
|
{}, cache)
|
2022-10-27 06:29:24 +00:00
|
|
|
|
|
|
|
for path in walkTests(OpSyncAggregateDir):
|
|
|
|
runTest[SyncAggregate, typeof applySyncAggregate](
|
|
|
|
OpSyncAggregateDir, "Sync Aggregate", "sync_aggregate",
|
|
|
|
applySyncAggregate, path)
|
|
|
|
|
|
|
|
suite baseDescription & "Voluntary Exit " & preset():
|
|
|
|
proc applyVoluntaryExit(
|
|
|
|
preState: var capella.BeaconState, voluntaryExit: SignedVoluntaryExit):
|
|
|
|
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)
|
|
|
|
|
|
|
|
suite baseDescription & "Withdrawals " & preset():
|
|
|
|
proc applyWithdrawals(
|
|
|
|
preState: var capella.BeaconState,
|
|
|
|
executionPayload: capella.ExecutionPayload): Result[void, cstring] =
|
|
|
|
process_withdrawals(preState, executionPayload)
|
|
|
|
|
|
|
|
for path in walkTests(OpWithdrawalsDir):
|
|
|
|
runTest[capella.ExecutionPayload, typeof applyWithdrawals](
|
|
|
|
OpWithdrawalsDir, "Withdrawals", "execution_payload",
|
|
|
|
applyWithdrawals, path)
|