Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/beacon-node/test/spec/presets/sanity.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "node:path";
import {getConfig} from "@lodestar/config/test-utils";
import {ACTIVE_PRESET, ForkName} from "@lodestar/params";
import {ACTIVE_PRESET, ForkName, PresetName} from "@lodestar/params";
Comment thread
nflaig marked this conversation as resolved.
Resolved
import {InputType} from "@lodestar/spec-test-util";
import {
BeaconStateAllForks,
Expand Down Expand Up @@ -30,6 +30,8 @@ const sanity: TestRunnerFn<any, BeaconStateAllForks> = (fork, testName, testSuit
};

const sanitySlots: TestRunnerFn<SanitySlotsTestCase, BeaconStateAllForks> = (fork) => {
const timeout = fork === ForkName.gloas && ACTIVE_PRESET === PresetName.mainnet ? 60000 : 30000;
Comment thread
nflaig marked this conversation as resolved.
Resolved

Comment thread
nflaig marked this conversation as resolved.
Resolved
return {
testFunction: (testcase) => {
const stateTB = testcase.pre.clone();
Expand All @@ -46,7 +48,7 @@ const sanitySlots: TestRunnerFn<SanitySlotsTestCase, BeaconStateAllForks> = (for
post: ssz[fork].BeaconState,
},
shouldError: (testCase) => !testCase.post,
timeout: 30000,
timeout,
Comment thread
nflaig marked this conversation as resolved.
Resolved
getExpected: (testCase) => testCase.post,
expectFunc: (_testCase, expected, actual) => {
expectEqualBeaconState(fork, expected, actual);
Expand Down
30 changes: 26 additions & 4 deletions packages/beacon-node/test/spec/utils/expectEqualBeaconState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,42 @@ import {ForkAll, ForkName} from "@lodestar/params";
import {InputType} from "@lodestar/spec-test-util";
import {BeaconStateAllForks} from "@lodestar/state-transition";
import {SSZTypesFor, ssz} from "@lodestar/types";
import {byteArrayEquals, toRootHex} from "@lodestar/utils";

/** Compare each field in BeaconState to help debug failed test easier. */
export function expectEqualBeaconState(
fork: ForkName,
expectedView: BeaconStateAllForks,
actualView: BeaconStateAllForks
): void {
// TODO: Is it cheaper to compare roots? Or maybe the serialized bytes?
const expected = expectedView.toValue();
const actual = actualView.toValue();
const expectedRoot = expectedView.hashTreeRoot();
const actualRoot = actualView.hashTreeRoot();
if (byteArrayEquals(actualRoot, expectedRoot)) {
return;
}

const stateType = ssz[fork].BeaconState as SSZTypesFor<ForkAll, "BeaconState">;
let expected: ReturnType<typeof expectedView.toValue>;
let actual: ReturnType<typeof actualView.toValue>;
try {
expected = expectedView.toValue();
actual = actualView.toValue();
} catch {
expect(toRootHex(actualRoot)).toEqualWithMessage(toRootHex(expectedRoot), "Wrong state root");
return;
}

if (!stateType.equals(actual, expected)) {
expect(stateType.toJson(actual)).toEqualWithMessage(stateType.toJson(expected), "Wrong state");
let expectedJson: unknown;
let actualJson: unknown;
try {
expectedJson = stateType.toJson(expected);
actualJson = stateType.toJson(actual);
} catch {
expect(toRootHex(actualRoot)).toEqualWithMessage(toRootHex(expectedRoot), "Wrong state root");
return;
}
expect(actualJson).toEqualWithMessage(expectedJson, "Wrong state");
}
}

Expand Down
Loading