Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Dec 7, 2022
1 parent a18856d commit e1c8fe3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import {IBeaconChain} from "../../../../../../src/chain/index.js";
import {PERSIST_STATE_EVERY_EPOCHS} from "../../../../../../src/chain/archiver/archiveStates.js";
import {generateProtoBlock} from "../../../../../utils/block.js";
import {generateCachedState, generateCachedStateWithPubkeys, generateState} from "../../../../../utils/state.js";
import {generateCachedAltairState, generateCachedState, generateState} from "../../../../../utils/state.js";
import {StubbedBeaconDb} from "../../../../../utils/stub/index.js";

use(chaiAsPromised);
Expand Down Expand Up @@ -222,7 +222,7 @@ describe("beacon state api utils", function () {
});

describe("getStateValidatorIndex", async function () {
const state = await generateCachedStateWithPubkeys({}, config, true);
const state = generateCachedAltairState();
const pubkey2index = state.epochCtx.pubkey2index;

it("should return valid: false on invalid input", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import sinon from "sinon";
import type {SecretKey} from "@chainsafe/bls/types";
import bls from "@chainsafe/bls";
import {BitArray, fromHexString} from "@chainsafe/ssz";
import {createIChainForkConfig, defaultChainConfig} from "@lodestar/config";
import {CachedBeaconStateAllForks} from "@lodestar/state-transition";
import {SLOTS_PER_EPOCH} from "@lodestar/params";
import {ssz, phase0} from "@lodestar/types";
Expand All @@ -18,7 +17,7 @@ import {
import {InsertOutcome} from "../../../../src/chain/opPools/types.js";
import {linspace} from "../../../../src/util/numpy.js";
import {generateAttestation, generateEmptyAttestation} from "../../../utils/attestation.js";
import {generateCachedState} from "../../../utils/state.js";
import {generateCachedAltairState} from "../../../utils/state.js";
import {renderBitArray} from "../../../utils/render.js";
import {ZERO_HASH_HEX} from "../../../../src/constants/constants.js";
import {generateEmptyProtoBlock} from "../../../utils/block.js";
Expand All @@ -33,9 +32,7 @@ describe("AggregatedAttestationPool", function () {
const altairForkEpoch = 2020;
const currentEpoch = altairForkEpoch + 10;
const currentSlot = SLOTS_PER_EPOCH * currentEpoch;
// eslint-disable-next-line @typescript-eslint/naming-convention
const config = createIChainForkConfig(Object.assign({}, defaultChainConfig, {ALTAIR_FORK_EPOCH: altairForkEpoch}));
const originalState = generateCachedState({slot: currentSlot + 1}, config, true);
const originalState = generateCachedAltairState({slot: currentSlot + 1}, altairForkEpoch);
let altairState: CachedBeaconStateAllForks;
const attestation = generateAttestation({data: {slot: currentSlot, target: {epoch: currentEpoch}}});
const committee = [0, 1, 2, 3];
Expand Down
3 changes: 1 addition & 2 deletions packages/beacon-node/test/unit/chain/prepareNextSlot.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expect} from "chai";
import sinon, {SinonStubbedInstance} from "sinon";
import {config} from "@lodestar/config/default";
import {ForkChoice, ProtoBlock} from "@lodestar/fork-choice";
import {WinstonLogger} from "@lodestar/utils";
import {ForkName, SLOTS_PER_EPOCH} from "@lodestar/params";
Expand All @@ -16,14 +17,12 @@ import {PayloadIdCache} from "../../../src/execution/engine/payloadIdCache.js";
import {ExecutionEngineHttp} from "../../../src/execution/engine/http.js";
import {IExecutionEngine} from "../../../src/execution/engine/interface.js";
import {StubbedChainMutable} from "../../utils/stub/index.js";
import {getConfig} from "../../utils/config.js";

type StubbedChain = StubbedChainMutable<"clock" | "forkChoice" | "emitter" | "regen">;

describe("PrepareNextSlot scheduler", () => {
const sandbox = sinon.createSandbox();
const abortController = new AbortController();
const config = getConfig(ForkName.bellatrix, 0);

let chainStub: StubbedChain;
let scheduler: PrepareNextSlotScheduler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {LocalClock} from "../../../../src/chain/clock/index.js";
import {SyncCommitteeErrorCode} from "../../../../src/chain/errors/syncCommitteeError.js";
import {validateGossipSyncCommittee} from "../../../../src/chain/validation/syncCommittee.js";
import {expectRejectedWithLodestarError} from "../../../utils/errors.js";
import {generateCachedState} from "../../../utils/state.js";
import {generateCachedAltairState} from "../../../utils/state.js";
import {generateSyncCommitteeSignature} from "../../../utils/syncCommittee.js";
import {SeenSyncCommitteeMessages} from "../../../../src/chain/seenCache/index.js";
import {BlsVerifierMock} from "../../../utils/mocks/bls.js";
Expand Down Expand Up @@ -69,7 +69,7 @@ describe("Sync Committee Signature validation", function () {
slot: currentSlot,
validatorIndex: validatorIndexInSyncCommittee,
});
const headState = generateCachedState({slot: currentSlot}, config, true);
const headState = generateCachedAltairState({slot: currentSlot}, altairForkEpoch);
chain.getHeadState.returns(headState);
chain.seenSyncCommitteeMessages.isKnown = () => true;
await expectRejectedWithLodestarError(
Expand All @@ -80,7 +80,7 @@ describe("Sync Committee Signature validation", function () {

it("should throw error - the validator is not part of the current sync committee", async function () {
const syncCommittee = generateSyncCommitteeSignature({slot: currentSlot, validatorIndex: 100});
const headState = generateCachedState({slot: currentSlot}, config, true);
const headState = generateCachedAltairState({slot: currentSlot}, altairForkEpoch);
chain.getHeadState.returns(headState);

await expectRejectedWithLodestarError(
Expand All @@ -95,7 +95,7 @@ describe("Sync Committee Signature validation", function () {
*/
it.skip("should throw error - incorrect subnet", async function () {
const syncCommittee = generateSyncCommitteeSignature({slot: currentSlot, validatorIndex: 1});
const headState = generateCachedState({slot: currentSlot}, config, true);
const headState = generateCachedAltairState({slot: currentSlot}, altairForkEpoch);
chain.getHeadState.returns(headState);
await expectRejectedWithLodestarError(
validateGossipSyncCommittee(chain, syncCommittee, 0),
Expand All @@ -108,7 +108,7 @@ describe("Sync Committee Signature validation", function () {
slot: currentSlot,
validatorIndex: validatorIndexInSyncCommittee,
});
const headState = generateCachedState({slot: currentSlot}, config, true);
const headState = generateCachedAltairState({slot: currentSlot}, altairForkEpoch);

chain.getHeadState.returns(headState);
chain.bls = new BlsVerifierMock(false);
Expand Down
14 changes: 11 additions & 3 deletions packages/beacon-node/test/utils/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
CachedBeaconStateBellatrix,
BeaconStateBellatrix,
} from "@lodestar/state-transition";
import {allForks, altair, ssz} from "@lodestar/types";
import {allForks, altair, bellatrix, ssz} from "@lodestar/types";
import {createIBeaconConfig, IChainForkConfig} from "@lodestar/config";
import {FAR_FUTURE_EPOCH, ForkName, ForkSeq, MAX_EFFECTIVE_BALANCE, SYNC_COMMITTEE_SIZE} from "@lodestar/params";

Expand Down Expand Up @@ -81,6 +81,14 @@ export function generateState(
};
}

if (forkSeq >= ForkSeq.bellatrix) {
const stateBellatrix = state as bellatrix.BeaconState;
stateBellatrix.latestExecutionPayloadHeader = {
...ssz.bellatrix.ExecutionPayloadHeader.defaultValue(),
blockNumber: 2022,
};
}

return config.getForkTypes(stateSlot).BeaconState.toViewDU(state);
}

Expand All @@ -101,8 +109,8 @@ export function generateCachedState(opts?: TestBeaconState): CachedBeaconStateAl
/**
* This generates state with default pubkey
*/
export function generateCachedAltairState(opts?: TestBeaconState): CachedBeaconStateAllForks {
const config = getConfig(ForkName.altair);
export function generateCachedAltairState(opts?: TestBeaconState, altairForkEpoch = 0): CachedBeaconStateAllForks {
const config = getConfig(ForkName.altair, altairForkEpoch);
const state = generateState(opts, config);
return createCachedBeaconState(state, {
config: createIBeaconConfig(config, state.genesisValidatorsRoot),
Expand Down

0 comments on commit e1c8fe3

Please sign in to comment.