2021-10-19 14:38:41 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 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.
|
|
|
|
|
2024-02-28 11:54:29 +00:00
|
|
|
{.push raises: [].}
|
2021-10-19 14:38:41 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
# Utilities
|
|
|
|
chronicles,
|
|
|
|
unittest2,
|
|
|
|
# Beacon chain internals
|
2023-05-25 07:55:00 +00:00
|
|
|
../../../beacon_chain/spec/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,
|
2023-02-10 20:59:38 +00:00
|
|
|
../fixtures_utils, ../os_ops,
|
2021-10-19 14:38:41 +00:00
|
|
|
../../helpers/debug_state
|
|
|
|
|
2022-12-14 17:30:56 +00:00
|
|
|
from std/sequtils import mapIt, toSeq
|
|
|
|
from std/strutils import contains
|
2023-05-25 07:55:00 +00:00
|
|
|
from ../../../beacon_chain/spec/beaconstate import
|
2024-04-02 12:18:40 +00:00
|
|
|
get_base_reward_per_increment, get_state_exit_queue_info,
|
|
|
|
get_total_active_balance, process_attestation
|
2022-12-14 17:30:56 +00:00
|
|
|
|
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](
|
2023-09-04 13:05:11 +00:00
|
|
|
testSuiteDir, suiteName, opName, applyFile: string,
|
2021-10-19 14:38:41 +00:00
|
|
|
applyProc: U, identifier: string) =
|
|
|
|
let testDir = testSuiteDir / "pyspec_tests" / identifier
|
|
|
|
|
2023-09-04 13:05:11 +00:00
|
|
|
let prefix =
|
|
|
|
if fileExists(testDir/"post.ssz_snappy"):
|
|
|
|
"[Valid] "
|
|
|
|
else:
|
|
|
|
"[Invalid] "
|
|
|
|
|
|
|
|
test prefix & baseDescription & opName & " - " & identifier:
|
|
|
|
let preState = newClone(
|
|
|
|
parseTest(testDir/"pre.ssz_snappy", SSZ, bellatrix.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, bellatrix.BeaconState))
|
|
|
|
|
|
|
|
check:
|
|
|
|
done.isOk()
|
|
|
|
preState[].hash_tree_root() == postState[].hash_tree_root()
|
|
|
|
reportDiff(preState, postState)
|
|
|
|
else:
|
|
|
|
check: done.isErr() # No post state = processing should fail
|
2021-10-19 14:38:41 +00:00
|
|
|
|
|
|
|
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] =
|
2024-03-04 17:00:46 +00:00
|
|
|
var cache: StateCache
|
2021-10-19 14:38:41 +00:00
|
|
|
let
|
|
|
|
total_active_balance = get_total_active_balance(preState, cache)
|
|
|
|
base_reward_per_increment =
|
|
|
|
get_base_reward_per_increment(total_active_balance)
|
|
|
|
|
2024-03-04 17:00:46 +00:00
|
|
|
# This returns the proposer reward for including the attestation, which
|
|
|
|
# isn't tested here.
|
|
|
|
discard ? process_attestation(
|
2021-10-19 14:38:41 +00:00
|
|
|
preState, attestation, {}, base_reward_per_increment, cache)
|
2024-03-04 17:00:46 +00:00
|
|
|
ok()
|
2021-10-19 14:38:41 +00:00
|
|
|
|
|
|
|
for path in walkTests(OpAttestationsDir):
|
|
|
|
runTest[Attestation, typeof applyAttestation](
|
2023-09-04 13:05:11 +00:00
|
|
|
OpAttestationsDir, suiteName, "Attestation", "attestation",
|
|
|
|
applyAttestation, path)
|
2021-10-19 14:38:41 +00:00
|
|
|
|
|
|
|
suite baseDescription & "Attester Slashing " & preset():
|
|
|
|
proc applyAttesterSlashing(
|
2024-04-21 05:49:11 +00:00
|
|
|
preState: var bellatrix.BeaconState,
|
|
|
|
attesterSlashing: phase0.AttesterSlashing): Result[void, cstring] =
|
2024-03-04 17:00:46 +00:00
|
|
|
var cache: StateCache
|
|
|
|
doAssert (? process_attester_slashing(
|
2024-04-02 12:18:40 +00:00
|
|
|
defaultRuntimeConfig, preState, attesterSlashing, {strictVerification},
|
2024-04-03 01:45:57 +00:00
|
|
|
get_state_exit_queue_info(preState), cache))[0] > 0.Gwei
|
2024-03-04 17:00:46 +00:00
|
|
|
ok()
|
2021-10-19 14:38:41 +00:00
|
|
|
|
|
|
|
for path in walkTests(OpAttSlashingDir):
|
2024-04-21 05:49:11 +00:00
|
|
|
runTest[phase0.AttesterSlashing, typeof applyAttesterSlashing](
|
2023-09-04 13:05:11 +00:00
|
|
|
OpAttSlashingDir, suiteName, "Attester Slashing", "attester_slashing",
|
2021-10-19 14:38:41 +00:00
|
|
|
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] =
|
2024-03-04 17:00:46 +00:00
|
|
|
var cache: StateCache
|
2021-10-19 14:38:41 +00:00
|
|
|
process_block_header(preState, blck, {}, cache)
|
|
|
|
|
|
|
|
for path in walkTests(OpBlockHeaderDir):
|
2022-01-07 17:10:40 +00:00
|
|
|
runTest[bellatrix.BeaconBlock, typeof applyBlockHeader](
|
2023-09-04 13:05:11 +00:00
|
|
|
OpBlockHeaderDir, suiteName, "Block Header", "block",
|
|
|
|
applyBlockHeader, path)
|
2021-10-19 14:38:41 +00:00
|
|
|
|
2024-03-07 23:38:24 +00:00
|
|
|
from ".."/".."/".."/beacon_chain/bloomfilter import constructBloomFilter
|
|
|
|
|
2021-10-19 14:38:41 +00:00
|
|
|
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] =
|
2024-03-07 23:38:24 +00:00
|
|
|
process_deposit(
|
|
|
|
defaultRuntimeConfig, preState,
|
|
|
|
constructBloomFilter(preState.validators.asSeq)[], deposit, {})
|
2021-10-19 14:38:41 +00:00
|
|
|
|
|
|
|
for path in walkTests(OpDepositsDir):
|
|
|
|
runTest[Deposit, typeof applyDeposit](
|
2023-09-04 13:05:11 +00:00
|
|
|
OpDepositsDir, suiteName, "Deposit", "deposit", applyDeposit, path)
|
2021-10-19 14:38:41 +00:00
|
|
|
|
2021-10-20 11:36:38 +00:00
|
|
|
suite baseDescription & "Execution Payload " & preset():
|
2023-09-06 08:35:37 +00:00
|
|
|
proc makeApplyExecutionPayloadCb(path: string): auto =
|
|
|
|
return proc(
|
2023-05-25 07:55:00 +00:00
|
|
|
preState: var bellatrix.BeaconState, body: bellatrix.BeaconBlockBody):
|
2024-02-28 11:54:29 +00:00
|
|
|
Result[void, cstring] {.raises: [IOError].} =
|
2023-09-06 08:35:37 +00:00
|
|
|
let payloadValid = os_ops.readFile(
|
|
|
|
OpExecutionPayloadDir/"pyspec_tests"/path/"execution.yaml"
|
|
|
|
).contains("execution_valid: true")
|
2023-01-14 21:19:50 +00:00
|
|
|
func executePayload(_: bellatrix.ExecutionPayload): bool = payloadValid
|
2021-10-20 11:36:38 +00:00
|
|
|
process_execution_payload(
|
2023-05-25 07:55:00 +00:00
|
|
|
preState, body.execution_payload, executePayload)
|
2021-10-20 11:36:38 +00:00
|
|
|
|
2023-09-06 08:35:37 +00:00
|
|
|
for path in walkTests(OpExecutionPayloadDir):
|
|
|
|
let applyExecutionPayload = makeApplyExecutionPayloadCb(path)
|
2023-05-25 07:55:00 +00:00
|
|
|
runTest[bellatrix.BeaconBlockBody, typeof applyExecutionPayload](
|
2023-09-04 13:05:11 +00:00
|
|
|
OpExecutionPayloadDir, suiteName, "Execution Payload", "body",
|
2021-10-20 11:36:38 +00:00
|
|
|
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] =
|
2024-03-04 17:00:46 +00:00
|
|
|
var cache: StateCache
|
|
|
|
doAssert (? process_proposer_slashing(
|
2024-04-02 12:18:40 +00:00
|
|
|
defaultRuntimeConfig, preState, proposerSlashing, {},
|
2024-04-03 01:45:57 +00:00
|
|
|
get_state_exit_queue_info(preState), cache))[0] > 0.Gwei
|
2024-03-04 17:00:46 +00:00
|
|
|
ok()
|
2021-10-19 14:38:41 +00:00
|
|
|
|
|
|
|
for path in walkTests(OpProposerSlashingDir):
|
|
|
|
runTest[ProposerSlashing, typeof applyProposerSlashing](
|
2023-09-04 13:05:11 +00:00
|
|
|
OpProposerSlashingDir, suiteName, "Proposer Slashing", "proposer_slashing",
|
2021-10-19 14:38:41 +00:00
|
|
|
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] =
|
2024-03-04 17:00:46 +00:00
|
|
|
var cache: StateCache
|
|
|
|
doAssert (? process_sync_aggregate(
|
2023-06-17 06:00:01 +00:00
|
|
|
preState, syncAggregate, get_total_active_balance(preState, cache),
|
2024-03-19 13:22:07 +00:00
|
|
|
{}, cache)) > 0.Gwei
|
2024-03-04 17:00:46 +00:00
|
|
|
ok()
|
2021-10-19 14:38:41 +00:00
|
|
|
|
|
|
|
for path in walkTests(OpSyncAggregateDir):
|
|
|
|
runTest[SyncAggregate, typeof applySyncAggregate](
|
2023-09-04 13:05:11 +00:00
|
|
|
OpSyncAggregateDir, suiteName, "Sync Aggregate", "sync_aggregate",
|
2021-10-19 14:38:41 +00:00
|
|
|
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] =
|
2024-03-04 17:00:46 +00:00
|
|
|
var cache: StateCache
|
2024-04-02 12:18:40 +00:00
|
|
|
if process_voluntary_exit(
|
|
|
|
defaultRuntimeConfig, preState, voluntaryExit, {},
|
2024-04-03 01:45:57 +00:00
|
|
|
get_state_exit_queue_info(preState), cache).isOk:
|
2024-04-02 12:18:40 +00:00
|
|
|
ok()
|
|
|
|
else:
|
|
|
|
err("")
|
2021-10-19 14:38:41 +00:00
|
|
|
|
|
|
|
for path in walkTests(OpVoluntaryExitDir):
|
|
|
|
runTest[SignedVoluntaryExit, typeof applyVoluntaryExit](
|
2023-09-04 13:05:11 +00:00
|
|
|
OpVoluntaryExitDir, suiteName, "Voluntary Exit", "voluntary_exit",
|
2024-04-02 12:18:40 +00:00
|
|
|
applyVoluntaryExit, path)
|