Skip to content

Commit

Permalink
Fix getParticipantIndices() for struct
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jun 9, 2021
1 parent 647acb0 commit 12cf112
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import {DOMAIN_SYNC_COMMITTEE} from "@chainsafe/lodestar-params";
import {
computeEpochAtSlot,
computeSigningRoot,
extractParticipantIndices,
getBlockRootAtSlot,
getDomain,
increaseBalance,
ISignatureSet,
SignatureSetType,
verifySignatureSet,
zipIndexesInBitList,
} from "../../util";
import {CachedBeaconState} from "../../allForks/util";
import {BitList, isTreeBacked, TreeBacked} from "@chainsafe/ssz";

export function processSyncCommittee(
state: CachedBeaconState<altair.BeaconState>,
Expand Down Expand Up @@ -89,13 +88,5 @@ function getParticipantIndices(
syncAggregate: altair.SyncAggregate
): number[] {
const committeeIndices = state.currSyncCommitteeIndexes;

// the only time aggregate is not a TreeBacked is when producing a new block
return isTreeBacked(syncAggregate)
? zipIndexesInBitList(
committeeIndices,
syncAggregate.syncCommitteeBits as TreeBacked<BitList>,
ssz.altair.SyncCommitteeBits
)
: committeeIndices.filter((index) => !!syncAggregate.syncCommitteeBits[index]);
return extractParticipantIndices(committeeIndices, syncAggregate);
}
26 changes: 25 additions & 1 deletion packages/beacon-state-transition/src/util/syncCommittee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import {
SYNC_COMMITTEE_SUBNET_COUNT,
TARGET_AGGREGATORS_PER_SYNC_SUBCOMMITTEE,
} from "@chainsafe/lodestar-params";
import {altair} from "@chainsafe/lodestar-types";
import {ssz} from "@chainsafe/lodestar-types";
import {BLSSignature, phase0} from "@chainsafe/lodestar-types";
import {bytesToInt, intDiv} from "@chainsafe/lodestar-utils";
import {hash} from "@chainsafe/ssz";
import {BitList, hash, isTreeBacked, TreeBacked} from "@chainsafe/ssz";
import {zipIndexesInBitList} from "./aggregationBits";

/**
* TODO
Expand All @@ -25,3 +28,24 @@ export function isSyncCommitteeAggregator(selectionProof: BLSSignature): boolean
);
return bytesToInt(hash(selectionProof.valueOf() as Uint8Array).slice(0, 8)) % modulo === 0;
}

export function extractParticipantIndices(
committeeIndices: phase0.ValidatorIndex[],
syncAggregate: altair.SyncAggregate
): phase0.ValidatorIndex[] {
if (isTreeBacked(syncAggregate)) {
return zipIndexesInBitList(
committeeIndices,
syncAggregate.syncCommitteeBits as TreeBacked<BitList>,
ssz.altair.SyncCommitteeBits
);
} else {
const participantIndices = [];
for (const [i, index] of committeeIndices.entries()) {
if (syncAggregate.syncCommitteeBits[i]) {
participantIndices.push(index);
}
}
return participantIndices;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {SYNC_COMMITTEE_SIZE} from "@chainsafe/lodestar-params";
import {ssz} from "@chainsafe/lodestar-types";
import {expect} from "chai";
import {extractParticipantIndices} from "../../../src";

describe("extractParticipantIndices", function () {
const committeeIndices = Array.from({length: SYNC_COMMITTEE_SIZE}, (_, i) => i * 2);
// 3 first bits are true
const syncCommitteeBits = Array.from({length: SYNC_COMMITTEE_SIZE}, (_, i) => {
return i < 3 ? true : false;
});

it("should extract from TreeBacked SyncAggregate", function () {
const syncAggregate = ssz.altair.SyncAggregate.defaultTreeBacked();
syncAggregate.syncCommitteeBits = syncCommitteeBits;
expect(extractParticipantIndices(committeeIndices, syncAggregate)).to.be.deep.equal(
[0, 2, 4],
"Incorrect participant indices from TreeBacked SyncAggregate"
);
});

it("should extract from struct SyncAggregate", function () {
const syncAggregate = ssz.altair.SyncAggregate.defaultValue();
syncAggregate.syncCommitteeBits = syncCommitteeBits;
expect(extractParticipantIndices(committeeIndices, syncAggregate)).to.be.deep.equal(
[0, 2, 4],
"Incorrect participant indices from TreeBacked SyncAggregate"
);
});
});

0 comments on commit 12cf112

Please sign in to comment.