Skip to content
Open
Changes from all 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
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