-
-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathoperations.test.ts
176 lines (151 loc) Β· 6.97 KB
/
operations.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import path from "node:path";
import {
BeaconStateAllForks,
CachedBeaconStateAllForks,
CachedBeaconStateBellatrix,
CachedBeaconStateCapella,
CachedBeaconStateElectra,
ExecutionPayloadStatus,
getBlockRootAtSlot,
} from "@lodestar/state-transition";
import * as blockFns from "@lodestar/state-transition/block";
import {ssz, phase0, altair, bellatrix, capella, electra} from "@lodestar/types";
import {InputType} from "@lodestar/spec-test-util";
import {ACTIVE_PRESET, ForkName, ForkSeq} from "@lodestar/params";
import {createCachedBeaconStateTest} from "../../utils/cachedBeaconState.js";
import {expectEqualBeaconState, inputTypeSszTreeViewDU} from "../utils/expectEqualBeaconState.js";
import {getConfig} from "../../utils/config.js";
import {BaseSpecTest, RunnerType, shouldVerify, TestRunnerFn} from "../utils/types.js";
import {ethereumConsensusSpecsTests} from "../specTestVersioning.js";
import {specTestIterator} from "../utils/specTestIterator.js";
/* eslint-disable @typescript-eslint/naming-convention */
// Define above to re-use in sync_aggregate and sync_aggregate_random
const sync_aggregate: BlockProcessFn<CachedBeaconStateAllForks> = (
state,
testCase: {sync_aggregate: altair.SyncAggregate}
) => {
const block = ssz.altair.BeaconBlock.defaultValue();
// processSyncAggregate() needs the full block to get the slot
block.slot = state.slot;
block.body.syncAggregate = ssz.altair.SyncAggregate.toViewDU(testCase["sync_aggregate"]);
block.parentRoot = getBlockRootAtSlot(state, Math.max(block.slot, 1) - 1);
blockFns.processSyncAggregate(state, block);
};
const operationFns: Record<string, BlockProcessFn<CachedBeaconStateAllForks>> = {
attestation: (state, testCase: {attestation: phase0.Attestation}) => {
const fork = state.config.getForkSeq(state.slot);
blockFns.processAttestations(fork, state, [testCase.attestation]);
},
attester_slashing: (state, testCase: BaseSpecTest & {attester_slashing: phase0.AttesterSlashing}) => {
const fork = state.config.getForkSeq(state.slot);
blockFns.processAttesterSlashing(fork, state, testCase.attester_slashing, shouldVerify(testCase));
},
block_header: (state, testCase: {block: phase0.BeaconBlock}) => {
blockFns.processBlockHeader(state, testCase.block);
},
deposit: (state, testCase: {deposit: phase0.Deposit}) => {
const fork = state.config.getForkSeq(state.slot);
blockFns.processDeposit(fork, state, testCase.deposit);
},
deposit_receipt: (state, testCase: {deposit_receipt: electra.DepositRequest}) => {
const fork = state.config.getForkSeq(state.slot);
blockFns.processDepositRequest(fork, state as CachedBeaconStateElectra, testCase.deposit_receipt);
},
proposer_slashing: (state, testCase: {proposer_slashing: phase0.ProposerSlashing}) => {
const fork = state.config.getForkSeq(state.slot);
blockFns.processProposerSlashing(fork, state, testCase.proposer_slashing);
},
sync_aggregate,
sync_aggregate_random: sync_aggregate,
voluntary_exit: (state, testCase: {voluntary_exit: phase0.SignedVoluntaryExit}) => {
const fork = state.config.getForkSeq(state.slot);
blockFns.processVoluntaryExit(fork, state, testCase.voluntary_exit);
},
execution_payload: (state, testCase: {body: bellatrix.BeaconBlockBody; execution: {execution_valid: boolean}}) => {
const fork = state.config.getForkSeq(state.slot);
blockFns.processExecutionPayload(fork, state as CachedBeaconStateBellatrix, testCase.body, {
executionPayloadStatus: testCase.execution.execution_valid
? ExecutionPayloadStatus.valid
: ExecutionPayloadStatus.invalid,
});
},
bls_to_execution_change: (state, testCase: {address_change: capella.SignedBLSToExecutionChange}) => {
blockFns.processBlsToExecutionChange(state as CachedBeaconStateCapella, testCase.address_change);
},
withdrawals: (state, testCase: {execution_payload: capella.ExecutionPayload}) => {
blockFns.processWithdrawals(ForkSeq.capella, state as CachedBeaconStateCapella, testCase.execution_payload);
},
consolidation: (state, testCase: {consolidation: electra.SignedConsolidation}) => {
blockFns.processConsolidation(state as CachedBeaconStateElectra, testCase.consolidation);
},
execution_layer_withdrawal_request: (
state,
testCase: {execution_layer_withdrawal_request: electra.ExecutionLayerWithdrawalRequest}
) => {
blockFns.processExecutionLayerWithdrawalRequest(
ForkSeq.electra,
state as CachedBeaconStateElectra,
testCase.execution_layer_withdrawal_request
);
},
};
export type BlockProcessFn<T extends CachedBeaconStateAllForks> = (state: T, testCase: any) => void;
export type OperationsTestCase = {
meta?: {bls_setting?: bigint};
pre: BeaconStateAllForks;
post: BeaconStateAllForks;
execution: {execution_valid: boolean};
};
const operations: TestRunnerFn<OperationsTestCase, BeaconStateAllForks> = (fork, testName) => {
const operationFn = operationFns[testName];
if (operationFn === undefined) {
throw Error(`No operationFn for ${testName}`);
}
return {
testFunction: (testcase) => {
const state = testcase.pre.clone();
const epoch = (state.fork as phase0.Fork).epoch;
const cachedState = createCachedBeaconStateTest(state, getConfig(fork, epoch));
operationFn(cachedState, testcase);
state.commit();
return state;
},
options: {
inputTypes: {...inputTypeSszTreeViewDU, execution: InputType.YAML},
sszTypes: {
pre: ssz[fork].BeaconState,
post: ssz[fork].BeaconState,
attestation: ssz.allForks[fork].Attestation,
attester_slashing: ssz.allForks[fork].AttesterSlashing,
block: ssz[fork].BeaconBlock,
body: ssz[fork].BeaconBlockBody,
deposit: ssz.phase0.Deposit,
deposit_receipt: ssz.electra.DepositRequest,
proposer_slashing: ssz.phase0.ProposerSlashing,
voluntary_exit: ssz.phase0.SignedVoluntaryExit,
// Altair
sync_aggregate: ssz.altair.SyncAggregate,
// Bellatrix
execution_payload:
fork !== ForkName.phase0 && fork !== ForkName.altair
? ssz.allForksExecution[fork as ExecutionFork].ExecutionPayload
: ssz.bellatrix.ExecutionPayload,
// Capella
address_change: ssz.capella.SignedBLSToExecutionChange,
// Electra
consolidation: ssz.electra.SignedConsolidation,
execution_layer_withdrawal_request: ssz.electra.ExecutionLayerWithdrawalRequest,
},
shouldError: (testCase) => testCase.post === undefined,
getExpected: (testCase) => testCase.post,
expectFunc: (testCase, expected, actual) => {
expectEqualBeaconState(fork, expected, actual);
},
// Do not manually skip tests here, do it in packages/beacon-node/test/spec/presets/index.test.ts
},
};
};
type ExecutionFork = Exclude<ForkName, ForkName.phase0 | ForkName.altair>;
specTestIterator(path.join(ethereumConsensusSpecsTests.outputDir, "tests", ACTIVE_PRESET), {
operations: {type: RunnerType.default, fn: operations},
});