Skip to content

Commit

Permalink
Make indexedSyncCommittee fields mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jun 15, 2021
1 parent 1ba9c2a commit b83c681
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import {
IParticipationStatus,
} from "./cachedEpochParticipation";
import {ForkName} from "@chainsafe/lodestar-params";
import {convertToIndexedSyncCommittee, createIndexedSyncCommittee, IndexedSyncCommittee} from "./indexedSyncCommittee";
import {
convertToIndexedSyncCommittee,
createIndexedSyncCommittee,
emptyIndexedSyncCommittee,
IndexedSyncCommittee,
} from "./indexedSyncCommittee";
import {getNextSyncCommittee} from "../../altair/epoch/sync_committee";
import {ssz} from "@chainsafe/lodestar-types";

Expand Down Expand Up @@ -60,16 +65,17 @@ export function createCachedBeaconState<T extends allForks.BeaconState>(
const cachedBalances = MutableVector.from(readonlyValues(state.balances));
let cachedPreviousParticipation, cachedCurrentParticipation;
const forkName = config.getForkName(state.slot);
const isOnPhase0 = forkName === ForkName.phase0;
let currIndexedSyncCommittee: IndexedSyncCommittee | undefined;
let nextIndexedSyncCommittee: IndexedSyncCommittee | undefined;
let currIndexedSyncCommittee: IndexedSyncCommittee;
let nextIndexedSyncCommittee: IndexedSyncCommittee;
const epochCtx = createEpochContext(config, state, cachedValidators, opts);
if (isOnPhase0) {
if (forkName === ForkName.phase0) {
const emptyParticipationStatus = {
timelyHead: false,
timelySource: false,
timelyTarget: false,
};
currIndexedSyncCommittee = emptyIndexedSyncCommittee;
nextIndexedSyncCommittee = emptyIndexedSyncCommittee;
cachedPreviousParticipation = MutableVector.from(
Array.from({length: cachedValidators.length}, () => emptyParticipationStatus)
);
Expand Down Expand Up @@ -119,8 +125,8 @@ export class BeaconStateContext<T extends allForks.BeaconState> {
previousEpochParticipation: CachedEpochParticipation & List<ParticipationFlags>;
currentEpochParticipation: CachedEpochParticipation & List<ParticipationFlags>;
// phase0 has no sync committee
currentSyncCommittee: IndexedSyncCommittee | undefined;
nextSyncCommittee: IndexedSyncCommittee | undefined;
currentSyncCommittee: IndexedSyncCommittee;
nextSyncCommittee: IndexedSyncCommittee;

constructor(
type: ContainerType<T>,
Expand All @@ -129,8 +135,8 @@ export class BeaconStateContext<T extends allForks.BeaconState> {
balanceCache: MutableVector<T["balances"][number]>,
previousEpochParticipationCache: MutableVector<IParticipationStatus>,
currentEpochParticipationCache: MutableVector<IParticipationStatus>,
currentSyncCommittee: IndexedSyncCommittee | undefined,
nextSyncCommittee: IndexedSyncCommittee | undefined,
currentSyncCommittee: IndexedSyncCommittee,
nextSyncCommittee: IndexedSyncCommittee,
epochCtx: EpochContext
) {
this.config = epochCtx.config;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {altair, phase0} from "@chainsafe/lodestar-types";
import {altair, phase0, ssz} from "@chainsafe/lodestar-types";
import {TreeBacked, Vector} from "@chainsafe/ssz";
import {PubkeyIndexMap} from "./epochContext";

Expand Down Expand Up @@ -42,6 +42,12 @@ export class IndexedSyncCommittee implements altair.SyncCommittee {
}
}

export const emptyIndexedSyncCommittee = new IndexedSyncCommittee(
ssz.altair.SyncCommittee.defaultTreeBacked(),
[],
new Map()
);

export function createIndexedSyncCommittee(
pubkey2index: PubkeyIndexMap,
state: TreeBacked<altair.BeaconState>,
Expand Down
2 changes: 1 addition & 1 deletion packages/lodestar/src/api/impl/beacon/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function getBeaconPoolApi({
await Promise.all(
signatures.map(async (signature, i) => {
try {
const indexesInCommittee = state.currentSyncCommittee?.validatorIndexMap.get(signature.validatorIndex);
const indexesInCommittee = state.currentSyncCommittee.validatorIndexMap.get(signature.validatorIndex);
if (indexesInCommittee === undefined || indexesInCommittee.length === 0) {
return; // Not a sync committee member
}
Expand Down
4 changes: 2 additions & 2 deletions packages/lodestar/src/chain/validation/syncCommittee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ function getIndexInSubCommittee(

const syncComitteeValidatorIndexMap =
dataPeriod === statePeriod + 1
? headState.nextSyncCommittee?.validatorIndexMap
: headState.currentSyncCommittee?.validatorIndexMap;
? headState.nextSyncCommittee.validatorIndexMap
: headState.currentSyncCommittee.validatorIndexMap;

const indexesInCommittee = syncComitteeValidatorIndexMap?.get(data.validatorIndex);
if (indexesInCommittee === undefined) {
Expand Down

0 comments on commit b83c681

Please sign in to comment.