@lodestar/state-transition 1.41.0-dev.afd446235e → 1.41.0-dev.b90dff673d
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.d.ts +2 -1
- package/lib/block/externalData.d.ts.map +1 -1
- package/lib/block/externalData.js +6 -2
- package/lib/block/externalData.js.map +1 -1
- package/lib/block/index.d.ts +3 -3
- 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.d.ts +1 -1
- package/lib/block/processExecutionPayloadBid.js.map +1 -1
- package/lib/block/processExecutionPayloadEnvelope.d.ts +5 -2
- package/lib/block/processExecutionPayloadEnvelope.d.ts.map +1 -1
- package/lib/block/processExecutionPayloadEnvelope.js +20 -14
- package/lib/block/processExecutionPayloadEnvelope.js.map +1 -1
- package/lib/block/processOperations.js.map +1 -1
- package/lib/block/processPayloadAttestation.d.ts +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 +2 -0
- package/lib/cache/epochCache.d.ts.map +1 -1
- package/lib/cache/epochCache.js +21 -6
- 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.d.ts +1 -1
- package/lib/epoch/index.js +2 -1
- package/lib/epoch/index.js.map +1 -1
- package/lib/epoch/processBuilderPendingPayments.d.ts +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.d.ts +2 -0
- package/lib/testUtils/cache.d.ts.map +1 -0
- package/lib/testUtils/cache.js +7 -0
- package/lib/testUtils/cache.js.map +1 -0
- package/lib/testUtils/index.d.ts +6 -0
- package/lib/testUtils/index.d.ts.map +1 -0
- package/lib/testUtils/index.js +6 -0
- package/lib/testUtils/index.js.map +1 -0
- package/lib/testUtils/infura.d.ts +3 -0
- package/lib/testUtils/infura.d.ts.map +1 -0
- package/lib/testUtils/infura.js +8 -0
- package/lib/testUtils/infura.js.map +1 -0
- package/lib/testUtils/interop.d.ts +2 -0
- package/lib/testUtils/interop.d.ts.map +1 -0
- package/lib/testUtils/interop.js +24 -0
- package/lib/testUtils/interop.js.map +1 -0
- package/lib/testUtils/params.d.ts +18 -0
- package/lib/testUtils/params.d.ts.map +1 -0
- package/lib/testUtils/params.js +20 -0
- package/lib/testUtils/params.js.map +1 -0
- package/lib/testUtils/state.d.ts +20 -0
- package/lib/testUtils/state.d.ts.map +1 -0
- package/lib/testUtils/state.js +78 -0
- package/lib/testUtils/state.js.map +1 -0
- package/lib/testUtils/testFileCache.d.ts +17 -0
- package/lib/testUtils/testFileCache.d.ts.map +1 -0
- package/lib/testUtils/testFileCache.js +96 -0
- package/lib/testUtils/testFileCache.js.map +1 -0
- package/lib/testUtils/util.d.ts +50 -0
- package/lib/testUtils/util.d.ts.map +1 -0
- package/lib/testUtils/util.js +329 -0
- package/lib/testUtils/util.js.map +1 -0
- 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 +14 -9
- package/src/block/externalData.ts +2 -0
- package/src/block/index.ts +3 -3
- package/src/block/isValidIndexedPayloadAttestation.ts +2 -2
- package/src/block/processAttestationsAltair.ts +1 -1
- package/src/block/processExecutionPayloadBid.ts +4 -4
- package/src/block/processExecutionPayloadEnvelope.ts +35 -21
- package/src/block/processOperations.ts +1 -1
- package/src/block/processPayloadAttestation.ts +2 -2
- package/src/block/processWithdrawals.ts +1 -1
- package/src/cache/epochCache.ts +27 -5
- package/src/epoch/index.ts +1 -1
- package/src/epoch/processBuilderPendingPayments.ts +2 -2
- package/src/stateView/beaconStateView.ts +72 -7
- package/src/stateView/interface.ts +19 -2
- package/src/testUtils/cache.ts +8 -0
- package/src/testUtils/index.ts +5 -0
- package/src/testUtils/infura.ts +10 -0
- package/src/testUtils/interop.ts +29 -0
- package/src/testUtils/params.ts +23 -0
- package/src/testUtils/state.ts +110 -0
- package/src/testUtils/testFileCache.ts +127 -0
- package/src/testUtils/util.ts +429 -0
- package/src/util/seed.ts +0 -71
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import {PublicKey, SecretKey} from "@chainsafe/blst";
|
|
2
|
+
import {BitArray, fromHexString} from "@chainsafe/ssz";
|
|
3
|
+
import {createBeaconConfig, createChainForkConfig} from "@lodestar/config";
|
|
4
|
+
import {config} from "@lodestar/config/default";
|
|
5
|
+
import {
|
|
6
|
+
EPOCHS_PER_ETH1_VOTING_PERIOD,
|
|
7
|
+
EPOCHS_PER_HISTORICAL_VECTOR,
|
|
8
|
+
ForkName,
|
|
9
|
+
ForkSeq,
|
|
10
|
+
MAX_ATTESTATIONS,
|
|
11
|
+
MAX_EFFECTIVE_BALANCE,
|
|
12
|
+
SLOTS_PER_EPOCH,
|
|
13
|
+
SLOTS_PER_HISTORICAL_ROOT,
|
|
14
|
+
} from "@lodestar/params";
|
|
15
|
+
import {BeaconState, Slot, phase0, ssz} from "@lodestar/types";
|
|
16
|
+
import {getEffectiveBalanceIncrements} from "../cache/effectiveBalanceIncrements.js";
|
|
17
|
+
import {
|
|
18
|
+
computeCommitteeCount,
|
|
19
|
+
computeEpochAtSlot,
|
|
20
|
+
createCachedBeaconState,
|
|
21
|
+
createPubkeyCache,
|
|
22
|
+
interopSecretKey,
|
|
23
|
+
newFilledArray,
|
|
24
|
+
processSlots,
|
|
25
|
+
} from "../index.js";
|
|
26
|
+
import {
|
|
27
|
+
BeaconStateAltair,
|
|
28
|
+
BeaconStatePhase0,
|
|
29
|
+
CachedBeaconStateAllForks,
|
|
30
|
+
CachedBeaconStateAltair,
|
|
31
|
+
CachedBeaconStatePhase0,
|
|
32
|
+
} from "../types.js";
|
|
33
|
+
import {getNextSyncCommittee} from "../util/syncCommittee.js";
|
|
34
|
+
import {getActiveValidatorIndices} from "../util/validator.js";
|
|
35
|
+
import {interopPubkeysCached} from "./interop.js";
|
|
36
|
+
|
|
37
|
+
let phase0State: BeaconStatePhase0 | null = null;
|
|
38
|
+
let phase0CachedState23637: CachedBeaconStatePhase0 | null = null;
|
|
39
|
+
let phase0CachedState23638: CachedBeaconStatePhase0 | null = null;
|
|
40
|
+
let phase0SignedBlock: phase0.SignedBeaconBlock | null = null;
|
|
41
|
+
let altairState: BeaconStateAltair | null = null;
|
|
42
|
+
let altairCachedState23637: CachedBeaconStateAltair | null = null;
|
|
43
|
+
let altairCachedState23638: CachedBeaconStateAltair | null = null;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Number of validators in prater is 210000 as of May 2021
|
|
47
|
+
*/
|
|
48
|
+
export const numValidators = 250000;
|
|
49
|
+
export const keypairsMod = 100;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* As of Jul 07 2021, the performance state has
|
|
53
|
+
* out.prevEpochUnslashedStake.targetStake 7750000000000000n
|
|
54
|
+
* out.currEpochUnslashedTargetStake 7750000000000000n
|
|
55
|
+
* This prefix represent the total stake in Peta Wei
|
|
56
|
+
*/
|
|
57
|
+
export const perfStateId = `${numValidators} vs - 7PWei`;
|
|
58
|
+
|
|
59
|
+
/** Cache interop secret keys */
|
|
60
|
+
const secretKeyByModIndex = new Map<number, SecretKey>();
|
|
61
|
+
const epoch = 23638;
|
|
62
|
+
export const perfStateEpoch = epoch;
|
|
63
|
+
|
|
64
|
+
export function getPubkeys(vc = numValidators) {
|
|
65
|
+
const pubkeysMod = interopPubkeysCached(keypairsMod);
|
|
66
|
+
const pubkeysModObj = pubkeysMod.map((pk) => PublicKey.fromBytes(pk));
|
|
67
|
+
const pubkeys = Array.from({length: vc}, (_, i) => pubkeysMod[i % keypairsMod]);
|
|
68
|
+
return {pubkeysMod, pubkeysModObj, pubkeys};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Get secret key of a validatorIndex, if the pubkeys are generated with `getPubkeys()` */
|
|
72
|
+
export function getSecretKeyFromIndex(validatorIndex: number): SecretKey {
|
|
73
|
+
return interopSecretKey(validatorIndex % keypairsMod);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Get secret key of a validatorIndex, if the pubkeys are generated with `getPubkeys()` */
|
|
77
|
+
export function getSecretKeyFromIndexCached(validatorIndex: number): SecretKey {
|
|
78
|
+
const keyIndex = validatorIndex % keypairsMod;
|
|
79
|
+
let sk = secretKeyByModIndex.get(keyIndex);
|
|
80
|
+
if (!sk) {
|
|
81
|
+
sk = interopSecretKey(keyIndex);
|
|
82
|
+
secretKeyByModIndex.set(keyIndex, sk);
|
|
83
|
+
}
|
|
84
|
+
return sk;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getPubkeyCaches({pubkeysMod}: ReturnType<typeof getPubkeys>) {
|
|
88
|
+
// Manually sync pubkeys to prevent doing BLS opts 110_000 times
|
|
89
|
+
const pubkeyCache = createPubkeyCache();
|
|
90
|
+
for (let i = 0; i < numValidators; i++) {
|
|
91
|
+
const pubkey = pubkeysMod[i % keypairsMod];
|
|
92
|
+
pubkeyCache.set(i, pubkey);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {pubkeyCache};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function generatePerfTestCachedStatePhase0(opts?: {goBackOneSlot: boolean}): CachedBeaconStatePhase0 {
|
|
99
|
+
// Generate only some publicKeys
|
|
100
|
+
const {pubkeys, pubkeysMod, pubkeysModObj} = getPubkeys();
|
|
101
|
+
const {pubkeyCache} = getPubkeyCaches({pubkeys, pubkeysMod, pubkeysModObj});
|
|
102
|
+
|
|
103
|
+
if (!phase0State) {
|
|
104
|
+
const state = buildPerformanceStatePhase0();
|
|
105
|
+
|
|
106
|
+
// no justificationBits
|
|
107
|
+
phase0State = ssz.phase0.BeaconState.toViewDU(state);
|
|
108
|
+
|
|
109
|
+
// cache roots
|
|
110
|
+
phase0State.hashTreeRoot();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (!phase0CachedState23637) {
|
|
114
|
+
const state = phase0State.clone();
|
|
115
|
+
state.slot -= 1;
|
|
116
|
+
phase0CachedState23637 = createCachedBeaconState(state, {
|
|
117
|
+
config: createBeaconConfig(config, state.genesisValidatorsRoot),
|
|
118
|
+
pubkeyCache,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const currentEpoch = computeEpochAtSlot(state.slot - 1);
|
|
122
|
+
const previousEpoch = currentEpoch - 1;
|
|
123
|
+
|
|
124
|
+
// previous epoch attestations
|
|
125
|
+
const numPrevAttestations = SLOTS_PER_EPOCH * MAX_ATTESTATIONS;
|
|
126
|
+
const activeValidatorCount = pubkeys.length;
|
|
127
|
+
const committeesPerSlot = computeCommitteeCount(activeValidatorCount);
|
|
128
|
+
for (let i = 0; i < numPrevAttestations; i++) {
|
|
129
|
+
const slotInEpoch = i % SLOTS_PER_EPOCH;
|
|
130
|
+
const slot = previousEpoch * SLOTS_PER_EPOCH + slotInEpoch;
|
|
131
|
+
const index = i % committeesPerSlot;
|
|
132
|
+
const shuffling = phase0CachedState23637.epochCtx.getShufflingAtEpoch(previousEpoch);
|
|
133
|
+
const committee = shuffling.committees[slotInEpoch][index];
|
|
134
|
+
phase0CachedState23637.previousEpochAttestations.push(
|
|
135
|
+
ssz.phase0.PendingAttestation.toViewDU({
|
|
136
|
+
aggregationBits: BitArray.fromBoolArray(Array.from({length: committee.length}, () => true)),
|
|
137
|
+
data: {
|
|
138
|
+
beaconBlockRoot: phase0CachedState23637.blockRoots.get(slotInEpoch % SLOTS_PER_HISTORICAL_ROOT),
|
|
139
|
+
index,
|
|
140
|
+
slot,
|
|
141
|
+
source: state.previousJustifiedCheckpoint,
|
|
142
|
+
target: state.currentJustifiedCheckpoint,
|
|
143
|
+
},
|
|
144
|
+
inclusionDelay: 1,
|
|
145
|
+
proposerIndex: i,
|
|
146
|
+
})
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// current epoch attestations
|
|
151
|
+
const numCurAttestations = (SLOTS_PER_EPOCH - 1) * MAX_ATTESTATIONS;
|
|
152
|
+
for (let i = 0; i < numCurAttestations; i++) {
|
|
153
|
+
const slotInEpoch = i % SLOTS_PER_EPOCH;
|
|
154
|
+
const slot = currentEpoch * SLOTS_PER_EPOCH + slotInEpoch;
|
|
155
|
+
const index = i % committeesPerSlot;
|
|
156
|
+
const shuffling = phase0CachedState23637.epochCtx.getShufflingAtEpoch(previousEpoch);
|
|
157
|
+
const committee = shuffling.committees[slotInEpoch][index];
|
|
158
|
+
|
|
159
|
+
phase0CachedState23637.currentEpochAttestations.push(
|
|
160
|
+
ssz.phase0.PendingAttestation.toViewDU({
|
|
161
|
+
aggregationBits: BitArray.fromBoolArray(Array.from({length: committee.length}, () => true)),
|
|
162
|
+
data: {
|
|
163
|
+
beaconBlockRoot: phase0CachedState23637.blockRoots.get(slotInEpoch % SLOTS_PER_HISTORICAL_ROOT),
|
|
164
|
+
index,
|
|
165
|
+
slot,
|
|
166
|
+
source: state.currentJustifiedCheckpoint,
|
|
167
|
+
target: {
|
|
168
|
+
epoch: currentEpoch,
|
|
169
|
+
root: phase0CachedState23637.blockRoots.get((currentEpoch * SLOTS_PER_EPOCH) % SLOTS_PER_HISTORICAL_ROOT),
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
inclusionDelay: 1,
|
|
173
|
+
proposerIndex: i,
|
|
174
|
+
})
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (!phase0CachedState23638) {
|
|
179
|
+
phase0CachedState23638 = processSlots(
|
|
180
|
+
phase0CachedState23637,
|
|
181
|
+
phase0CachedState23637.slot + 1
|
|
182
|
+
) as CachedBeaconStatePhase0;
|
|
183
|
+
phase0CachedState23638.slot += 1;
|
|
184
|
+
}
|
|
185
|
+
const resultingState = opts?.goBackOneSlot ? phase0CachedState23637 : phase0CachedState23638;
|
|
186
|
+
|
|
187
|
+
return resultingState.clone();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function cachedStateAltairPopulateCaches(state: CachedBeaconStateAltair): void {
|
|
191
|
+
// Populate caches
|
|
192
|
+
state.blockRoots.getAllReadonly();
|
|
193
|
+
state.eth1DataVotes.getAllReadonly();
|
|
194
|
+
state.validators.getAllReadonly();
|
|
195
|
+
state.balances.getAll();
|
|
196
|
+
state.previousEpochParticipation.getAll();
|
|
197
|
+
state.currentEpochParticipation.getAll();
|
|
198
|
+
state.inactivityScores.getAll();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Warning: This function has side effects on the cached state
|
|
203
|
+
* The order in which the caches are populated is important and can cause stable tests to fail.
|
|
204
|
+
*/
|
|
205
|
+
export function generatePerfTestCachedStateAltair(opts?: {
|
|
206
|
+
goBackOneSlot: boolean;
|
|
207
|
+
vc?: number;
|
|
208
|
+
}): CachedBeaconStateAltair {
|
|
209
|
+
const {pubkeys, pubkeysMod, pubkeysModObj} = getPubkeys(opts?.vc);
|
|
210
|
+
const {pubkeyCache} = getPubkeyCaches({pubkeys, pubkeysMod, pubkeysModObj});
|
|
211
|
+
|
|
212
|
+
const altairConfig = createChainForkConfig({ALTAIR_FORK_EPOCH: 0});
|
|
213
|
+
|
|
214
|
+
const origState = generatePerformanceStateAltair(pubkeys);
|
|
215
|
+
|
|
216
|
+
if (!altairCachedState23637) {
|
|
217
|
+
const state = origState.clone();
|
|
218
|
+
state.slot -= 1;
|
|
219
|
+
altairCachedState23637 = createCachedBeaconState(state, {
|
|
220
|
+
config: createBeaconConfig(altairConfig, state.genesisValidatorsRoot),
|
|
221
|
+
pubkeyCache,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
if (!altairCachedState23638) {
|
|
225
|
+
altairCachedState23638 = processSlots(
|
|
226
|
+
altairCachedState23637,
|
|
227
|
+
altairCachedState23637.slot + 1
|
|
228
|
+
) as CachedBeaconStateAltair;
|
|
229
|
+
altairCachedState23638.slot += 1;
|
|
230
|
+
}
|
|
231
|
+
const resultingState = opts?.goBackOneSlot ? altairCachedState23637 : altairCachedState23638;
|
|
232
|
+
|
|
233
|
+
return resultingState.clone();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* This is generated from Medalla state 756416
|
|
238
|
+
*/
|
|
239
|
+
export function generatePerformanceStateAltair(pubkeysArg?: Uint8Array[]): BeaconStateAltair {
|
|
240
|
+
if (!altairState) {
|
|
241
|
+
const pubkeys = pubkeysArg || getPubkeys().pubkeys;
|
|
242
|
+
const statePhase0 = buildPerformanceStatePhase0(pubkeys);
|
|
243
|
+
const state = statePhase0 as BeaconState as BeaconState<ForkName.altair>;
|
|
244
|
+
|
|
245
|
+
state.previousEpochParticipation = newFilledArray(pubkeys.length, 0b111);
|
|
246
|
+
state.currentEpochParticipation = state.previousEpochParticipation;
|
|
247
|
+
state.inactivityScores = Array.from({length: pubkeys.length}, (_, i) => i % 2);
|
|
248
|
+
|
|
249
|
+
// Placeholder syncCommittees
|
|
250
|
+
state.currentSyncCommittee = ssz.altair.SyncCommittee.defaultValue();
|
|
251
|
+
state.nextSyncCommittee = state.currentSyncCommittee;
|
|
252
|
+
|
|
253
|
+
// Now the state is fully populated to convert to ViewDU
|
|
254
|
+
altairState = ssz.altair.BeaconState.toViewDU(state);
|
|
255
|
+
|
|
256
|
+
// Now set correct syncCommittees
|
|
257
|
+
const epoch = computeEpochAtSlot(state.slot);
|
|
258
|
+
const activeValidatorIndices = getActiveValidatorIndices(altairState, epoch);
|
|
259
|
+
|
|
260
|
+
const effectiveBalanceIncrements = getEffectiveBalanceIncrements(altairState);
|
|
261
|
+
const {syncCommittee} = getNextSyncCommittee(
|
|
262
|
+
ForkSeq.altair,
|
|
263
|
+
altairState,
|
|
264
|
+
activeValidatorIndices,
|
|
265
|
+
effectiveBalanceIncrements
|
|
266
|
+
);
|
|
267
|
+
state.currentSyncCommittee = syncCommittee;
|
|
268
|
+
state.nextSyncCommittee = syncCommittee;
|
|
269
|
+
|
|
270
|
+
altairState = ssz.altair.BeaconState.toViewDU(state);
|
|
271
|
+
// cache roots
|
|
272
|
+
altairState.hashTreeRoot();
|
|
273
|
+
}
|
|
274
|
+
return altairState.clone();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* This is generated from Medalla block 756417
|
|
279
|
+
*/
|
|
280
|
+
export function generatePerformanceBlockPhase0(): phase0.SignedBeaconBlock {
|
|
281
|
+
if (!phase0SignedBlock) {
|
|
282
|
+
const block = ssz.phase0.SignedBeaconBlock.defaultValue();
|
|
283
|
+
const parentState = generatePerfTestCachedStatePhase0();
|
|
284
|
+
block.message.slot = parentState.slot;
|
|
285
|
+
block.message.proposerIndex = parentState.epochCtx.getBeaconProposer(parentState.slot);
|
|
286
|
+
block.message.parentRoot = ssz.phase0.BeaconBlockHeader.hashTreeRoot(parentState.latestBlockHeader);
|
|
287
|
+
block.message.stateRoot = fromHexString("0x6c86ca3c4c6688cf189421b8a68bf2dbc91521609965e6f4e207d44347061fee");
|
|
288
|
+
block.message.body.randaoReveal = fromHexString(
|
|
289
|
+
"0x8a5d2673c48f22f6ed19462efec35645db490df29eed2f56321dbe4a89b2463b0c902095a7ab74a2dc5b7f67edb1a19507ea3d4361d5af9cb0a524945c91638dfd6568841486813a2c45142659d6d9403f5081febb123a7931edbc248b9d0025"
|
|
290
|
+
);
|
|
291
|
+
// eth1Data, graffiti, attestations
|
|
292
|
+
phase0SignedBlock = block;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return phase0SignedBlock;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function buildPerformanceStatePhase0(pubkeysArg?: Uint8Array[]): phase0.BeaconState {
|
|
299
|
+
const slot = epoch * SLOTS_PER_EPOCH;
|
|
300
|
+
const pubkeys = pubkeysArg || getPubkeys().pubkeys;
|
|
301
|
+
const currentEpoch = computeEpochAtSlot(slot - 1);
|
|
302
|
+
|
|
303
|
+
return {
|
|
304
|
+
// Misc
|
|
305
|
+
genesisTime: 1596546008,
|
|
306
|
+
genesisValidatorsRoot: fromHexString("0x04700007fabc8282644aed6d1c7c9e21d38a03a0c4ba193f3afe428824b3a673"),
|
|
307
|
+
slot: epoch * SLOTS_PER_EPOCH,
|
|
308
|
+
fork: {
|
|
309
|
+
currentVersion: fromHexString("0x00000001"),
|
|
310
|
+
previousVersion: fromHexString("0x00000001"),
|
|
311
|
+
epoch: 0,
|
|
312
|
+
},
|
|
313
|
+
// History
|
|
314
|
+
latestBlockHeader: {
|
|
315
|
+
slot: slot - 1,
|
|
316
|
+
proposerIndex: 80882,
|
|
317
|
+
parentRoot: fromHexString("0x5b83c3078e474b86af60043eda82a34c3c2e5ebf83146b14d9d909aea4163ef2"),
|
|
318
|
+
stateRoot: fromHexString("0x2761ae355e8a53c11e0e37d5e417f8984db0c53fa83f1bc65f89c6af35a196a7"),
|
|
319
|
+
bodyRoot: fromHexString("0x249a1962eef90e122fa2447040bfac102798b1dba9c73e5593bc5aa32eb92bfd"),
|
|
320
|
+
},
|
|
321
|
+
blockRoots: Array.from({length: SLOTS_PER_HISTORICAL_ROOT}, (_, i) => Buffer.alloc(32, i)),
|
|
322
|
+
stateRoots: Array.from({length: SLOTS_PER_HISTORICAL_ROOT}, (_, i) => Buffer.alloc(32, i)),
|
|
323
|
+
historicalRoots: [],
|
|
324
|
+
// Eth1
|
|
325
|
+
eth1Data: {
|
|
326
|
+
depositCount: pubkeys.length,
|
|
327
|
+
depositRoot: fromHexString("0xcb1f89a924cfd31224823db5a41b1643f10faa7aedf231f1e28887f6ee98c047"),
|
|
328
|
+
blockHash: fromHexString("0x701fb2869ce16d0f1d14f6705725adb0dec6799da29006dfc6fff83960298f21"),
|
|
329
|
+
},
|
|
330
|
+
// minus one so that inserting 1 from block works
|
|
331
|
+
eth1DataVotes: newFilledArray(EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH - 1, {
|
|
332
|
+
depositCount: 1,
|
|
333
|
+
depositRoot: Buffer.alloc(32, 1),
|
|
334
|
+
blockHash: Buffer.alloc(32, 1),
|
|
335
|
+
}),
|
|
336
|
+
eth1DepositIndex: pubkeys.length,
|
|
337
|
+
// Registry
|
|
338
|
+
validators: pubkeys.map((_, i) => ({
|
|
339
|
+
pubkey: pubkeys[i],
|
|
340
|
+
withdrawalCredentials: Buffer.alloc(32, i),
|
|
341
|
+
effectiveBalance: 31000000000,
|
|
342
|
+
slashed: false,
|
|
343
|
+
activationEligibilityEpoch: 0,
|
|
344
|
+
activationEpoch: 0,
|
|
345
|
+
exitEpoch: Infinity,
|
|
346
|
+
withdrawableEpoch: Infinity,
|
|
347
|
+
})),
|
|
348
|
+
balances: Array.from({length: pubkeys.length}, () => 31217089836),
|
|
349
|
+
randaoMixes: Array.from({length: EPOCHS_PER_HISTORICAL_VECTOR}, (_, i) => Buffer.alloc(32, i)),
|
|
350
|
+
// Slashings
|
|
351
|
+
slashings: ssz.phase0.Slashings.defaultValue(),
|
|
352
|
+
previousEpochAttestations: [],
|
|
353
|
+
currentEpochAttestations: [],
|
|
354
|
+
// Finality
|
|
355
|
+
justificationBits: BitArray.fromBitLen(4),
|
|
356
|
+
previousJustifiedCheckpoint: {
|
|
357
|
+
epoch: currentEpoch - 2,
|
|
358
|
+
root: fromHexString("0x3fe60bf06a57b0956cd1f8181d26649cf8bf79e48bf82f55562e04b33d4785d4"),
|
|
359
|
+
},
|
|
360
|
+
currentJustifiedCheckpoint: {
|
|
361
|
+
epoch: currentEpoch - 1,
|
|
362
|
+
root: fromHexString("0x3ba0913d2fb5e4cbcfb0d39eb15803157c1e769d63b8619285d8fdabbd8181c7"),
|
|
363
|
+
},
|
|
364
|
+
finalizedCheckpoint: {
|
|
365
|
+
epoch: currentEpoch - 3,
|
|
366
|
+
root: fromHexString("0x122b8ff579d0c8f8a8b66326bdfec3f685007d2842f01615a0768870961ccc17"),
|
|
367
|
+
},
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export function generateTestCachedBeaconStateOnlyValidators({
|
|
372
|
+
vc,
|
|
373
|
+
slot,
|
|
374
|
+
}: {
|
|
375
|
+
vc: number;
|
|
376
|
+
slot: Slot;
|
|
377
|
+
}): CachedBeaconStateAllForks {
|
|
378
|
+
// Generate only some publicKeys
|
|
379
|
+
const {pubkeys, pubkeysMod} = getPubkeys(vc);
|
|
380
|
+
|
|
381
|
+
// Manually sync pubkeys to prevent doing BLS opts 110_000 times
|
|
382
|
+
const pubkeyCache = createPubkeyCache();
|
|
383
|
+
for (let i = 0; i < vc; i++) {
|
|
384
|
+
const pubkey = pubkeysMod[i % keypairsMod];
|
|
385
|
+
pubkeyCache.set(i, pubkey);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const state = ssz.phase0.BeaconState.defaultViewDU();
|
|
389
|
+
state.slot = slot;
|
|
390
|
+
|
|
391
|
+
const activeValidator = ssz.phase0.Validator.toViewDU({
|
|
392
|
+
pubkey: Buffer.alloc(48, 0),
|
|
393
|
+
withdrawalCredentials: Buffer.alloc(32, 0),
|
|
394
|
+
effectiveBalance: MAX_EFFECTIVE_BALANCE,
|
|
395
|
+
slashed: false,
|
|
396
|
+
activationEligibilityEpoch: 0,
|
|
397
|
+
activationEpoch: 0,
|
|
398
|
+
exitEpoch: Infinity,
|
|
399
|
+
withdrawableEpoch: Infinity,
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
for (let i = 0; i < vc; i++) {
|
|
403
|
+
const validator = activeValidator.clone();
|
|
404
|
+
validator.pubkey = pubkeys[i];
|
|
405
|
+
state.validators.push(validator);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
state.balances = ssz.phase0.Balances.toViewDU(newFilledArray(pubkeys.length, MAX_EFFECTIVE_BALANCE));
|
|
409
|
+
state.randaoMixes = ssz.phase0.RandaoMixes.toViewDU(
|
|
410
|
+
newFilledArray(EPOCHS_PER_HISTORICAL_VECTOR, Buffer.alloc(32, 0xdd))
|
|
411
|
+
);
|
|
412
|
+
|
|
413
|
+
// Commit ViewDU changes
|
|
414
|
+
state.commit();
|
|
415
|
+
|
|
416
|
+
// Sanity check for .commit() above
|
|
417
|
+
if (state.validators.length !== vc) {
|
|
418
|
+
throw Error(`Wrong number of validators in the state: ${state.validators.length} !== ${vc}`);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return createCachedBeaconState(
|
|
422
|
+
state,
|
|
423
|
+
{
|
|
424
|
+
config: createBeaconConfig(config, state.genesisValidatorsRoot),
|
|
425
|
+
pubkeyCache,
|
|
426
|
+
},
|
|
427
|
+
{skipSyncPubkeys: true}
|
|
428
|
+
);
|
|
429
|
+
}
|
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
|
*/
|