Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use rust shuffle #7120

Merged
merged 17 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
21 changes: 13 additions & 8 deletions packages/beacon-node/src/chain/shufflingCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
IShufflingCache,
ShufflingBuildProps,
computeEpochShuffling,
computeEpochShufflingAsync,
} from "@lodestar/state-transition";
import {Epoch, RootHex} from "@lodestar/types";
import {LodestarError, Logger, MapDef, pruneSetToMax} from "@lodestar/utils";
import {Metrics} from "../metrics/metrics.js";
import {callInNextEventLoop} from "../util/eventLoop.js";

/**
* Same value to CheckpointBalancesCache, with the assumption that we don't have to use it for old epochs. In the worse case:
Expand Down Expand Up @@ -178,14 +178,19 @@ export class ShufflingCache implements IShufflingCache {
this.insertPromise(epoch, decisionRoot);
/**
* TODO: (@matthewkeil) This will get replaced by a proper build queue and a worker to do calculations
* on a NICE thread with a rust implementation
* on a NICE thread
*/
callInNextEventLoop(() => {
const timer = this.metrics?.shufflingCache.shufflingCalculationTime.startTimer({source: "build"});
const shuffling = computeEpochShuffling(state, activeIndices, epoch);
timer?.();
this.set(shuffling, decisionRoot);
});
const timer = this.metrics?.shufflingCache.shufflingCalculationTime.startTimer({source: "build"});
computeEpochShufflingAsync(state, activeIndices, epoch)
.then((shuffling) => {
this.set(shuffling, decisionRoot);
})
.catch((err) =>
this.logger?.error(`error building shuffling for epoch ${epoch} at decisionRoot ${decisionRoot}`, err)
matthewkeil marked this conversation as resolved.
Show resolved Hide resolved
)
.finally(() => {
timer?.();
});
}

/**
Expand Down
17 changes: 10 additions & 7 deletions packages/beacon-node/test/spec/presets/shuffling.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import path from "node:path";
import {unshuffleList} from "@lodestar/state-transition";
import {unshuffleList} from "@chainsafe/swap-or-not-shuffle";
import {InputType} from "@lodestar/spec-test-util";
import {bnToNum, fromHex} from "@lodestar/utils";
import {ACTIVE_PRESET} from "@lodestar/params";
import {ACTIVE_PRESET, SHUFFLE_ROUND_COUNT} from "@lodestar/params";
import {RunnerType, TestRunnerFn} from "../utils/types.js";
import {ethereumConsensusSpecsTests} from "../specTestVersioning.js";
import {specTestIterator} from "../utils/specTestIterator.js";

const shuffling: TestRunnerFn<ShufflingTestCase, number[]> = () => {
const shuffling: TestRunnerFn<ShufflingTestCase, string> = () => {
return {
testFunction: (testcase) => {
const seed = fromHex(testcase.mapping.seed);
const output = Array.from({length: bnToNum(testcase.mapping.count)}, (_, i) => i);
unshuffleList(output, seed);
return output;
const output = unshuffleList(
Uint32Array.from(Array.from({length: bnToNum(testcase.mapping.count)}, (_, i) => i)),
seed,
SHUFFLE_ROUND_COUNT
);
return Buffer.from(output).toString("hex");
},
options: {
inputTypes: {mapping: InputType.YAML},
timeout: 10000,
getExpected: (testCase) => testCase.mapping.mapping.map((value) => bnToNum(value)),
getExpected: (testCase) => Buffer.from(testCase.mapping.mapping.map((value) => bnToNum(value))).toString("hex"),
matthewkeil marked this conversation as resolved.
Show resolved Hide resolved
// Do not manually skip tests here, do it in packages/beacon-node/test/spec/presets/index.test.ts
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/state-transition/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@chainsafe/persistent-merkle-tree": "^0.8.0",
"@chainsafe/persistent-ts": "^0.19.1",
"@chainsafe/ssz": "^0.17.1",
"@chainsafe/swap-or-not-shuffle": "^0.0.1",
"@lodestar/config": "^1.22.0",
"@lodestar/params": "^1.22.0",
"@lodestar/types": "^1.22.0",
Expand Down
44 changes: 41 additions & 3 deletions packages/state-transition/src/util/epochShuffling.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {asyncUnshuffleList, unshuffleList} from "@chainsafe/swap-or-not-shuffle";
import {Epoch, RootHex, ssz, ValidatorIndex} from "@lodestar/types";
import {GaugeExtra, intDiv, Logger, NoLabels, toRootHex} from "@lodestar/utils";
import {
Expand All @@ -6,11 +7,11 @@ import {
MAX_COMMITTEES_PER_SLOT,
SLOTS_PER_EPOCH,
TARGET_COMMITTEE_SIZE,
SHUFFLE_ROUND_COUNT,
} from "@lodestar/params";
import {BeaconConfig} from "@lodestar/config";
import {BeaconStateAllForks} from "../types.js";
import {getSeed} from "./seed.js";
import {unshuffleList} from "./shuffle.js";
import {computeStartSlotAtEpoch} from "./epoch.js";
import {getBlockRootAtSlot} from "./blockRoot.js";
import {computeAnchorCheckpoint} from "./computeAnchorCheckpoint.js";
Expand Down Expand Up @@ -109,9 +110,46 @@ export function computeEpochShuffling(
): EpochShuffling {
const activeValidatorCount = activeIndices.length;

const shuffling = activeIndices.slice();
const seed = getSeed(state, epoch, DOMAIN_BEACON_ATTESTER);
unshuffleList(shuffling, seed);
const shuffling = unshuffleList(activeIndices, seed, SHUFFLE_ROUND_COUNT);

const committeesPerSlot = computeCommitteeCount(activeValidatorCount);
matthewkeil marked this conversation as resolved.
Show resolved Hide resolved

const committeeCount = committeesPerSlot * SLOTS_PER_EPOCH;

const committees: Uint32Array[][] = [];
matthewkeil marked this conversation as resolved.
Show resolved Hide resolved
for (let slot = 0; slot < SLOTS_PER_EPOCH; slot++) {
const slotCommittees: Uint32Array[] = [];
matthewkeil marked this conversation as resolved.
Show resolved Hide resolved
for (let committeeIndex = 0; committeeIndex < committeesPerSlot; committeeIndex++) {
const index = slot * committeesPerSlot + committeeIndex;
const startOffset = Math.floor((activeValidatorCount * index) / committeeCount);
const endOffset = Math.floor((activeValidatorCount * (index + 1)) / committeeCount);
if (!(startOffset <= endOffset)) {
throw new Error(`Invalid offsets: start ${startOffset} must be less than or equal end ${endOffset}`);
}
slotCommittees.push(shuffling.subarray(startOffset, endOffset));
}
committees.push(slotCommittees);
}

return {
epoch,
activeIndices,
shuffling,
committees,
committeesPerSlot,
};
}

export async function computeEpochShufflingAsync(
matthewkeil marked this conversation as resolved.
Show resolved Hide resolved
state: BeaconStateAllForks,
activeIndices: Uint32Array,
epoch: Epoch
): Promise<EpochShuffling> {
const activeValidatorCount = activeIndices.length;

const seed = getSeed(state, epoch, DOMAIN_BEACON_ATTESTER);
const shuffling = await asyncUnshuffleList(activeIndices, seed, SHUFFLE_ROUND_COUNT);
twoeths marked this conversation as resolved.
Show resolved Hide resolved

const committeesPerSlot = computeCommitteeCount(activeValidatorCount);

Expand Down
1 change: 0 additions & 1 deletion packages/state-transition/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export * from "./genesis.js";
export * from "./interop.js";
export * from "./rootCache.js";
export * from "./seed.js";
export * from "./shuffle.js";
export * from "./shufflingDecisionRoot.js";
export * from "./signatureSets.js";
export * from "./signingRoot.js";
Expand Down
222 changes: 0 additions & 222 deletions packages/state-transition/src/util/shuffle.ts

This file was deleted.

Loading
Loading