@lodestar/state-transition 1.41.0-dev.be5acbb8f7 → 1.41.0-dev.c11b797bd6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/block/externalData.js +4 -2
- package/lib/block/externalData.js.map +1 -1
- package/lib/block/index.js.map +1 -1
- package/lib/block/initiateValidatorExit.js.map +1 -1
- package/lib/block/isValidIndexedAttestation.js.map +1 -1
- package/lib/block/isValidIndexedPayloadAttestation.js.map +1 -1
- package/lib/block/processAttestationPhase0.js.map +1 -1
- package/lib/block/processAttestations.js.map +1 -1
- package/lib/block/processAttestationsAltair.js.map +1 -1
- package/lib/block/processAttesterSlashing.js.map +1 -1
- package/lib/block/processBlobKzgCommitments.js.map +1 -1
- package/lib/block/processBlockHeader.js.map +1 -1
- package/lib/block/processBlsToExecutionChange.js.map +1 -1
- package/lib/block/processConsolidationRequest.js.map +1 -1
- package/lib/block/processDeposit.js.map +1 -1
- package/lib/block/processDepositRequest.js.map +1 -1
- package/lib/block/processEth1Data.js.map +1 -1
- package/lib/block/processExecutionPayload.js.map +1 -1
- package/lib/block/processExecutionPayloadBid.js.map +1 -1
- package/lib/block/processExecutionPayloadEnvelope.js.map +1 -1
- package/lib/block/processOperations.js.map +1 -1
- package/lib/block/processPayloadAttestation.js.map +1 -1
- package/lib/block/processProposerSlashing.js.map +1 -1
- package/lib/block/processRandao.js.map +1 -1
- package/lib/block/processSyncCommittee.js.map +1 -1
- package/lib/block/processVoluntaryExit.js +2 -1
- package/lib/block/processVoluntaryExit.js.map +1 -1
- package/lib/block/processWithdrawalRequest.js.map +1 -1
- package/lib/block/processWithdrawals.js.map +1 -1
- package/lib/block/slashValidator.js.map +1 -1
- package/lib/block/types.js +2 -1
- package/lib/block/types.js.map +1 -1
- package/lib/cache/effectiveBalanceIncrements.js.map +1 -1
- package/lib/cache/epochCache.d.ts.map +1 -1
- package/lib/cache/epochCache.js +2 -1
- package/lib/cache/epochCache.js.map +1 -1
- package/lib/cache/epochTransitionCache.js.map +1 -1
- package/lib/cache/pubkeyCache.js.map +1 -1
- package/lib/cache/rewardCache.js.map +1 -1
- package/lib/cache/stateCache.js.map +1 -1
- package/lib/cache/syncCommitteeCache.js.map +1 -1
- package/lib/epoch/computeUnrealizedCheckpoints.js.map +1 -1
- package/lib/epoch/getAttestationDeltas.js.map +1 -1
- package/lib/epoch/getRewardsAndPenalties.js.map +1 -1
- package/lib/epoch/index.js +2 -1
- package/lib/epoch/index.js.map +1 -1
- package/lib/epoch/processBuilderPendingPayments.js.map +1 -1
- package/lib/epoch/processEffectiveBalanceUpdates.js.map +1 -1
- package/lib/epoch/processEth1DataReset.js.map +1 -1
- package/lib/epoch/processHistoricalRootsUpdate.js.map +1 -1
- package/lib/epoch/processHistoricalSummariesUpdate.js.map +1 -1
- package/lib/epoch/processInactivityUpdates.js.map +1 -1
- package/lib/epoch/processJustificationAndFinalization.js.map +1 -1
- package/lib/epoch/processParticipationFlagUpdates.js.map +1 -1
- package/lib/epoch/processParticipationRecordUpdates.js.map +1 -1
- package/lib/epoch/processPendingAttestations.js.map +1 -1
- package/lib/epoch/processPendingConsolidations.js.map +1 -1
- package/lib/epoch/processPendingDeposits.js.map +1 -1
- package/lib/epoch/processProposerLookahead.js.map +1 -1
- package/lib/epoch/processRandaoMixesReset.js.map +1 -1
- package/lib/epoch/processRegistryUpdates.js.map +1 -1
- package/lib/epoch/processRewardsAndPenalties.js.map +1 -1
- package/lib/epoch/processSlashings.js.map +1 -1
- package/lib/epoch/processSlashingsReset.js.map +1 -1
- package/lib/epoch/processSyncCommitteeUpdates.js.map +1 -1
- package/lib/lightClient/proofs.js.map +1 -1
- package/lib/metrics.d.ts.map +1 -1
- package/lib/metrics.js.map +1 -1
- package/lib/rewards/attestationsRewards.js.map +1 -1
- package/lib/rewards/blockRewards.js.map +1 -1
- package/lib/rewards/syncCommitteeRewards.js.map +1 -1
- package/lib/signatureSets/attesterSlashings.js.map +1 -1
- package/lib/signatureSets/blsToExecutionChange.js.map +1 -1
- package/lib/signatureSets/executionPayloadBid.js.map +1 -1
- package/lib/signatureSets/executionPayloadEnvelope.js.map +1 -1
- package/lib/signatureSets/index.js.map +1 -1
- package/lib/signatureSets/indexedAttestation.js.map +1 -1
- package/lib/signatureSets/indexedPayloadAttestation.js.map +1 -1
- package/lib/signatureSets/proposer.js.map +1 -1
- package/lib/signatureSets/proposerSlashings.js.map +1 -1
- package/lib/signatureSets/randao.js.map +1 -1
- package/lib/signatureSets/voluntaryExits.js.map +1 -1
- package/lib/slot/index.js.map +1 -1
- package/lib/slot/upgradeStateToAltair.js.map +1 -1
- package/lib/slot/upgradeStateToBellatrix.js.map +1 -1
- package/lib/slot/upgradeStateToCapella.js.map +1 -1
- package/lib/slot/upgradeStateToDeneb.js.map +1 -1
- package/lib/slot/upgradeStateToElectra.js.map +1 -1
- package/lib/slot/upgradeStateToFulu.js.map +1 -1
- package/lib/slot/upgradeStateToGloas.js.map +1 -1
- package/lib/stateTransition.js +4 -2
- package/lib/stateTransition.js.map +1 -1
- package/lib/stateView/beaconStateView.d.ts +18 -2
- package/lib/stateView/beaconStateView.d.ts.map +1 -1
- package/lib/stateView/beaconStateView.js +60 -3
- package/lib/stateView/beaconStateView.js.map +1 -1
- package/lib/stateView/interface.d.ts +19 -2
- package/lib/stateView/interface.d.ts.map +1 -1
- package/lib/testUtils/cache.js.map +1 -1
- package/lib/testUtils/infura.js.map +1 -1
- package/lib/testUtils/interop.js.map +1 -1
- package/lib/testUtils/state.js.map +1 -1
- package/lib/testUtils/testFileCache.js.map +1 -1
- package/lib/testUtils/util.d.ts +1 -1
- package/lib/testUtils/util.d.ts.map +1 -1
- package/lib/testUtils/util.js.map +1 -1
- package/lib/util/aggregator.js.map +1 -1
- package/lib/util/altair.js.map +1 -1
- package/lib/util/array.js.map +1 -1
- package/lib/util/attestation.js.map +1 -1
- package/lib/util/attesterStatus.js.map +1 -1
- package/lib/util/balance.js.map +1 -1
- package/lib/util/blindedBlock.js.map +1 -1
- package/lib/util/blockRoot.js.map +1 -1
- package/lib/util/capella.js.map +1 -1
- package/lib/util/computeAnchorCheckpoint.js.map +1 -1
- package/lib/util/deposit.js.map +1 -1
- package/lib/util/domain.js.map +1 -1
- package/lib/util/electra.js.map +1 -1
- package/lib/util/epoch.js.map +1 -1
- package/lib/util/epochShuffling.js.map +1 -1
- package/lib/util/execution.js.map +1 -1
- package/lib/util/finality.js.map +1 -1
- package/lib/util/fulu.js.map +1 -1
- package/lib/util/genesis.js.map +1 -1
- package/lib/util/gloas.js.map +1 -1
- package/lib/util/interop.js.map +1 -1
- package/lib/util/loadState/findModifiedInactivityScores.js.map +1 -1
- package/lib/util/loadState/findModifiedValidators.js.map +1 -1
- package/lib/util/loadState/loadState.js.map +1 -1
- package/lib/util/loadState/loadValidator.js.map +1 -1
- package/lib/util/rootCache.d.ts.map +1 -1
- package/lib/util/rootCache.js.map +1 -1
- package/lib/util/seed.d.ts +0 -8
- package/lib/util/seed.d.ts.map +1 -1
- package/lib/util/seed.js +0 -64
- package/lib/util/seed.js.map +1 -1
- package/lib/util/shuffling.js +2 -1
- package/lib/util/shuffling.js.map +1 -1
- package/lib/util/signatureSets.js +2 -1
- package/lib/util/signatureSets.js.map +1 -1
- package/lib/util/signingRoot.js.map +1 -1
- package/lib/util/slot.js.map +1 -1
- package/lib/util/sszBytes.js.map +1 -1
- package/lib/util/syncCommittee.js.map +1 -1
- package/lib/util/targetUnslashedBalance.js.map +1 -1
- package/lib/util/validator.js.map +1 -1
- package/lib/util/weakSubjectivity.js.map +1 -1
- package/package.json +9 -9
- package/src/stateView/beaconStateView.ts +72 -7
- package/src/stateView/interface.ts +19 -2
- package/src/util/seed.ts +0 -71
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {CompactMultiProof, ProofType, Tree, createProof} from "@chainsafe/persistent-merkle-tree";
|
|
2
2
|
import {ByteViews} from "@chainsafe/ssz";
|
|
3
3
|
import {BeaconConfig} from "@lodestar/config";
|
|
4
|
-
import {ForkSeq, SLOTS_PER_HISTORICAL_ROOT} from "@lodestar/params";
|
|
4
|
+
import {ForkSeq, SLOTS_PER_HISTORICAL_ROOT, isForkPostGloas} from "@lodestar/params";
|
|
5
5
|
import {
|
|
6
6
|
BeaconBlock,
|
|
7
7
|
BlindedBeaconBlock,
|
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
rewards,
|
|
28
28
|
} from "@lodestar/types";
|
|
29
29
|
import {Checkpoint, Fork} from "@lodestar/types/phase0";
|
|
30
|
+
import {processExecutionPayloadEnvelope} from "../block/index.js";
|
|
30
31
|
import {VoluntaryExitValidity, getVoluntaryExitValidity} from "../block/processVoluntaryExit.js";
|
|
31
32
|
import {getExpectedWithdrawals} from "../block/processWithdrawals.js";
|
|
32
33
|
import {EffectiveBalanceIncrements} from "../cache/effectiveBalanceIncrements.js";
|
|
@@ -75,10 +76,12 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
75
76
|
// altair
|
|
76
77
|
private _currentSyncCommittee: SyncCommittee | null = null;
|
|
77
78
|
private _nextSyncCommittee: SyncCommittee | null = null;
|
|
78
|
-
private _previousEpochParticipation:
|
|
79
|
-
private _currentEpochParticipation:
|
|
79
|
+
private _previousEpochParticipation: Uint8Array | null = null;
|
|
80
|
+
private _currentEpochParticipation: Uint8Array | null = null;
|
|
80
81
|
// bellatrix
|
|
81
82
|
private _latestExecutionPayloadHeader: ExecutionPayloadHeader | null = null;
|
|
83
|
+
// Caches the cross-fork latestBlockHash value
|
|
84
|
+
private _latestBlockHash: Bytes32 | null = null;
|
|
82
85
|
// capella
|
|
83
86
|
private _historicalSummaries: capella.HistoricalSummaries | null = null;
|
|
84
87
|
// electra
|
|
@@ -159,7 +162,7 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
159
162
|
return getRandaoMix(this.cachedState, epoch);
|
|
160
163
|
}
|
|
161
164
|
|
|
162
|
-
get previousEpochParticipation():
|
|
165
|
+
get previousEpochParticipation(): Uint8Array {
|
|
163
166
|
if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.altair) {
|
|
164
167
|
throw new Error("previousEpochParticipation is not available before Altair");
|
|
165
168
|
}
|
|
@@ -167,7 +170,7 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
167
170
|
if (this._previousEpochParticipation === null) {
|
|
168
171
|
this._previousEpochParticipation = (
|
|
169
172
|
this.cachedState as CachedBeaconStateAltair
|
|
170
|
-
).previousEpochParticipation.
|
|
173
|
+
).previousEpochParticipation.serialize();
|
|
171
174
|
}
|
|
172
175
|
|
|
173
176
|
return this._previousEpochParticipation;
|
|
@@ -175,7 +178,7 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
175
178
|
|
|
176
179
|
// altair
|
|
177
180
|
|
|
178
|
-
get currentEpochParticipation():
|
|
181
|
+
get currentEpochParticipation(): Uint8Array {
|
|
179
182
|
if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.altair) {
|
|
180
183
|
throw new Error("currentEpochParticipation is not available before Altair");
|
|
181
184
|
}
|
|
@@ -183,12 +186,26 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
183
186
|
if (this._currentEpochParticipation === null) {
|
|
184
187
|
this._currentEpochParticipation = (
|
|
185
188
|
this.cachedState as CachedBeaconStateAltair
|
|
186
|
-
).currentEpochParticipation.
|
|
189
|
+
).currentEpochParticipation.serialize();
|
|
187
190
|
}
|
|
188
191
|
|
|
189
192
|
return this._currentEpochParticipation;
|
|
190
193
|
}
|
|
191
194
|
|
|
195
|
+
getPreviousEpochParticipation(validatorIndex: ValidatorIndex): number {
|
|
196
|
+
if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.altair) {
|
|
197
|
+
throw new Error("previousEpochParticipation is not available before Altair");
|
|
198
|
+
}
|
|
199
|
+
return (this.cachedState as CachedBeaconStateAltair).previousEpochParticipation.get(validatorIndex);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
getCurrentEpochParticipation(validatorIndex: ValidatorIndex): number {
|
|
203
|
+
if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.altair) {
|
|
204
|
+
throw new Error("currentEpochParticipation is not available before Altair");
|
|
205
|
+
}
|
|
206
|
+
return (this.cachedState as CachedBeaconStateAltair).currentEpochParticipation.get(validatorIndex);
|
|
207
|
+
}
|
|
208
|
+
|
|
192
209
|
// bellatrix
|
|
193
210
|
|
|
194
211
|
get latestExecutionPayloadHeader(): ExecutionPayloadHeader {
|
|
@@ -205,6 +222,46 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
205
222
|
return this._latestExecutionPayloadHeader;
|
|
206
223
|
}
|
|
207
224
|
|
|
225
|
+
/**
|
|
226
|
+
* Cross-fork accessor for the execution block hash of the most recently included payload.
|
|
227
|
+
* Pre-gloas: reads from latestExecutionPayloadHeader.blockHash.
|
|
228
|
+
* Gloas+: reads the dedicated latestBlockHash field (EIP-7732).
|
|
229
|
+
*/
|
|
230
|
+
get latestBlockHash(): Bytes32 {
|
|
231
|
+
const forkSeq = this.config.getForkSeq(this.cachedState.slot);
|
|
232
|
+
if (forkSeq < ForkSeq.bellatrix) {
|
|
233
|
+
throw new Error("latestBlockHash is not available before Bellatrix");
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (this._latestBlockHash === null) {
|
|
237
|
+
if (forkSeq >= ForkSeq.gloas) {
|
|
238
|
+
this._latestBlockHash = (this.cachedState as CachedBeaconStateGloas).latestBlockHash;
|
|
239
|
+
} else {
|
|
240
|
+
this._latestBlockHash = (
|
|
241
|
+
this.cachedState as CachedBeaconStateExecutions
|
|
242
|
+
).latestExecutionPayloadHeader.blockHash;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return this._latestBlockHash;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* The execution block number of the most recently included payload.
|
|
251
|
+
* Named payloadBlockNumber (not latestBlockNumber) to mirror ExecutionPayloadHeader.blockNumber pre-gloas.
|
|
252
|
+
* Only available from bellatrix through fulu — not tracked on BeaconState in gloas+ (EIP-7732).
|
|
253
|
+
*/
|
|
254
|
+
get payloadBlockNumber(): number {
|
|
255
|
+
const forkSeq = this.config.getForkSeq(this.cachedState.slot);
|
|
256
|
+
if (forkSeq < ForkSeq.bellatrix) {
|
|
257
|
+
throw new Error("payloadBlockNumber is not available before Bellatrix");
|
|
258
|
+
}
|
|
259
|
+
if (forkSeq >= ForkSeq.gloas) {
|
|
260
|
+
throw new Error("payloadBlockNumber is not available post-gloas");
|
|
261
|
+
}
|
|
262
|
+
return (this.cachedState as CachedBeaconStateExecutions).latestExecutionPayloadHeader.blockNumber;
|
|
263
|
+
}
|
|
264
|
+
|
|
208
265
|
// capella
|
|
209
266
|
|
|
210
267
|
get historicalSummaries(): capella.HistoricalSummaries {
|
|
@@ -703,6 +760,14 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
703
760
|
const newState = processSlots(this.cachedState, slot, epochTransitionCacheOpts, modules);
|
|
704
761
|
return new BeaconStateView(newState);
|
|
705
762
|
}
|
|
763
|
+
|
|
764
|
+
processExecutionPayloadEnvelope(signedEnvelope: gloas.SignedExecutionPayloadEnvelope, verify: boolean): void {
|
|
765
|
+
const fork = this.config.getForkName(this.cachedState.slot);
|
|
766
|
+
if (!isForkPostGloas(fork)) {
|
|
767
|
+
throw Error(`processExecutionPayloadEnvelope is only available for gloas+ forks, got fork=${fork}`);
|
|
768
|
+
}
|
|
769
|
+
processExecutionPayloadEnvelope(this.cachedState as CachedBeaconStateGloas, signedEnvelope, verify);
|
|
770
|
+
}
|
|
706
771
|
}
|
|
707
772
|
|
|
708
773
|
/**
|
|
@@ -55,11 +55,27 @@ export interface IBeaconStateView {
|
|
|
55
55
|
getRandaoMix(epoch: Epoch): Bytes32;
|
|
56
56
|
|
|
57
57
|
// altair
|
|
58
|
-
previousEpochParticipation:
|
|
59
|
-
currentEpochParticipation:
|
|
58
|
+
previousEpochParticipation: Uint8Array;
|
|
59
|
+
currentEpochParticipation: Uint8Array;
|
|
60
|
+
getPreviousEpochParticipation(validatorIndex: ValidatorIndex): number;
|
|
61
|
+
getCurrentEpochParticipation(validatorIndex: ValidatorIndex): number;
|
|
60
62
|
|
|
61
63
|
// bellatrix
|
|
62
64
|
latestExecutionPayloadHeader: ExecutionPayloadHeader;
|
|
65
|
+
/**
|
|
66
|
+
* Cross-fork accessor for the execution block hash of the most recently included payload.
|
|
67
|
+
* Pre-gloas: returns latestExecutionPayloadHeader.blockHash (bellatrix–fulu).
|
|
68
|
+
* Gloas+: returns the dedicated latestBlockHash state field (EIP-7732).
|
|
69
|
+
* Throws before bellatrix.
|
|
70
|
+
*/
|
|
71
|
+
latestBlockHash: Bytes32;
|
|
72
|
+
/**
|
|
73
|
+
* The execution block number of the most recently included payload.
|
|
74
|
+
* Named payloadBlockNumber (not latestBlockNumber) to mirror ExecutionPayloadHeader.blockNumber pre-gloas.
|
|
75
|
+
* Only available from bellatrix through fulu — not tracked on BeaconState in gloas+ (EIP-7732).
|
|
76
|
+
* Throws before bellatrix and from gloas onwards.
|
|
77
|
+
*/
|
|
78
|
+
payloadBlockNumber: number;
|
|
63
79
|
|
|
64
80
|
// capella
|
|
65
81
|
historicalSummaries: capella.HistoricalSummaries;
|
|
@@ -193,4 +209,5 @@ export interface IBeaconStateView {
|
|
|
193
209
|
epochTransitionCacheOpts?: EpochTransitionCacheOpts & {dontTransferCache?: boolean},
|
|
194
210
|
modules?: StateTransitionModules
|
|
195
211
|
): IBeaconStateView;
|
|
212
|
+
processExecutionPayloadEnvelope(signedEnvelope: gloas.SignedExecutionPayloadEnvelope, verify: boolean): void;
|
|
196
213
|
}
|
package/src/util/seed.ts
CHANGED
|
@@ -417,8 +417,6 @@ export function naiveComputePayloadTimelinessCommitteeIndices(
|
|
|
417
417
|
* https://link.springer.com/content/pdf/10.1007%2F978-3-642-32009-5_1.pdf
|
|
418
418
|
*
|
|
419
419
|
* See the 'generalized domain' algorithm on page 3.
|
|
420
|
-
* This is the naive implementation just to make sure lodestar follows the spec, this is not for production.
|
|
421
|
-
* The optimized version is in `getComputeShuffledIndexFn`.
|
|
422
420
|
*/
|
|
423
421
|
export function computeShuffledIndex(index: number, indexCount: number, seed: Bytes32): number {
|
|
424
422
|
let permuted = index;
|
|
@@ -439,75 +437,6 @@ export function computeShuffledIndex(index: number, indexCount: number, seed: By
|
|
|
439
437
|
return permuted;
|
|
440
438
|
}
|
|
441
439
|
|
|
442
|
-
type ComputeShuffledIndexFn = (index: number) => number;
|
|
443
|
-
|
|
444
|
-
/**
|
|
445
|
-
* An optimized version of `computeShuffledIndex`, this is for production.
|
|
446
|
-
*/
|
|
447
|
-
export function getComputeShuffledIndexFn(indexCount: number, seed: Bytes32): ComputeShuffledIndexFn {
|
|
448
|
-
// there are possibly SHUFFLE_ROUND_COUNT (90 for mainnet) values for this cache
|
|
449
|
-
// this cache will always hit after the 1st call
|
|
450
|
-
const pivotByIndex: Map<number, number> = new Map();
|
|
451
|
-
// given 2M active validators, there are 2 M / 256 = 8k possible positionDiv
|
|
452
|
-
// it means there are at most 8k different sources for each round
|
|
453
|
-
const sourceByPositionDivByIndex: Map<number, Map<number, Uint8Array>> = new Map();
|
|
454
|
-
// 32 bytes seed + 1 byte i
|
|
455
|
-
const pivotBuffer = Buffer.alloc(32 + 1);
|
|
456
|
-
pivotBuffer.set(seed, 0);
|
|
457
|
-
// 32 bytes seed + 1 byte i + 4 bytes positionDiv
|
|
458
|
-
const sourceBuffer = Buffer.alloc(32 + 1 + 4);
|
|
459
|
-
sourceBuffer.set(seed, 0);
|
|
460
|
-
|
|
461
|
-
return (index): number => {
|
|
462
|
-
assert.lt(index, indexCount, "indexCount must be less than index");
|
|
463
|
-
assert.lte(indexCount, 2 ** 40, "indexCount too big");
|
|
464
|
-
let permuted = index;
|
|
465
|
-
// const _seed = seed;
|
|
466
|
-
for (let i = 0; i < SHUFFLE_ROUND_COUNT; i++) {
|
|
467
|
-
// optimized version of the below naive code
|
|
468
|
-
// const pivot = Number(
|
|
469
|
-
// bytesToBigInt(digest(Buffer.concat([_seed, intToBytes(i, 1)])).slice(0, 8)) % BigInt(indexCount)
|
|
470
|
-
// );
|
|
471
|
-
|
|
472
|
-
let pivot = pivotByIndex.get(i);
|
|
473
|
-
if (pivot == null) {
|
|
474
|
-
// naive version always creates a new buffer, we can reuse the buffer
|
|
475
|
-
// pivot = Number(
|
|
476
|
-
// bytesToBigInt(digest(Buffer.concat([_seed, intToBytes(i, 1)])).slice(0, 8)) % BigInt(indexCount)
|
|
477
|
-
// );
|
|
478
|
-
pivotBuffer[32] = i % 256;
|
|
479
|
-
pivot = Number(bytesToBigInt(digest(pivotBuffer).subarray(0, 8)) % BigInt(indexCount));
|
|
480
|
-
pivotByIndex.set(i, pivot);
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
const flip = (pivot + indexCount - permuted) % indexCount;
|
|
484
|
-
const position = Math.max(permuted, flip);
|
|
485
|
-
|
|
486
|
-
// optimized version of the below naive code
|
|
487
|
-
// const source = digest(Buffer.concat([_seed, intToBytes(i, 1), intToBytes(Math.floor(position / 256), 4)]));
|
|
488
|
-
let sourceByPositionDiv = sourceByPositionDivByIndex.get(i);
|
|
489
|
-
if (sourceByPositionDiv == null) {
|
|
490
|
-
sourceByPositionDiv = new Map<number, Uint8Array>();
|
|
491
|
-
sourceByPositionDivByIndex.set(i, sourceByPositionDiv);
|
|
492
|
-
}
|
|
493
|
-
const positionDiv256 = Math.floor(position / 256);
|
|
494
|
-
let source = sourceByPositionDiv.get(positionDiv256);
|
|
495
|
-
if (source == null) {
|
|
496
|
-
// naive version always creates a new buffer, we can reuse the buffer
|
|
497
|
-
// don't want to go through intToBytes() to avoid BigInt
|
|
498
|
-
sourceBuffer[32] = i % 256;
|
|
499
|
-
sourceBuffer.writeUint32LE(positionDiv256, 33);
|
|
500
|
-
source = digest(sourceBuffer);
|
|
501
|
-
sourceByPositionDiv.set(positionDiv256, source);
|
|
502
|
-
}
|
|
503
|
-
const byte = source[Math.floor((position % 256) / 8)];
|
|
504
|
-
const bit = (byte >> (position % 8)) % 2;
|
|
505
|
-
permuted = bit ? flip : permuted;
|
|
506
|
-
}
|
|
507
|
-
return permuted;
|
|
508
|
-
};
|
|
509
|
-
}
|
|
510
|
-
|
|
511
440
|
/**
|
|
512
441
|
* Return the randao mix at a recent [[epoch]].
|
|
513
442
|
*/
|