@lodestar/state-transition 1.42.0-dev.1d50253953 → 1.42.0-dev.2f5ab270cc
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/isValidIndexedAttestation.d.ts.map +1 -1
- package/lib/block/isValidIndexedAttestation.js +2 -3
- package/lib/block/isValidIndexedAttestation.js.map +1 -1
- package/lib/block/processAttestationsAltair.d.ts +2 -1
- package/lib/block/processAttestationsAltair.d.ts.map +1 -1
- package/lib/block/processAttestationsAltair.js +5 -3
- package/lib/block/processAttestationsAltair.js.map +1 -1
- package/lib/signatureSets/index.d.ts +2 -2
- package/lib/signatureSets/index.d.ts.map +1 -1
- package/lib/signatureSets/index.js +1 -2
- package/lib/signatureSets/index.js.map +1 -1
- package/lib/slot/upgradeStateToAltair.d.ts.map +1 -1
- package/lib/slot/upgradeStateToAltair.js +2 -1
- package/lib/slot/upgradeStateToAltair.js.map +1 -1
- package/lib/stateView/beaconStateView.d.ts +6 -9
- package/lib/stateView/beaconStateView.d.ts.map +1 -1
- package/lib/stateView/beaconStateView.js +8 -32
- package/lib/stateView/beaconStateView.js.map +1 -1
- package/lib/stateView/index.d.ts +1 -0
- package/lib/stateView/index.d.ts.map +1 -1
- package/lib/stateView/index.js +1 -0
- package/lib/stateView/index.js.map +1 -1
- package/lib/stateView/interface.d.ts +12 -8
- package/lib/stateView/interface.d.ts.map +1 -1
- package/lib/stateView/stateViewFactory.d.ts +40 -0
- package/lib/stateView/stateViewFactory.d.ts.map +1 -0
- package/lib/stateView/stateViewFactory.js +46 -0
- package/lib/stateView/stateViewFactory.js.map +1 -0
- package/lib/testUtils/util.d.ts +13 -1
- package/lib/testUtils/util.d.ts.map +1 -1
- package/lib/testUtils/util.js +103 -12
- package/lib/testUtils/util.js.map +1 -1
- package/lib/util/rootCache.d.ts +2 -2
- package/lib/util/rootCache.d.ts.map +1 -1
- package/lib/util/rootCache.js +2 -3
- package/lib/util/rootCache.js.map +1 -1
- package/lib/util/shuffling.d.ts +2 -1
- package/lib/util/shuffling.d.ts.map +1 -1
- package/lib/util/shuffling.js +2 -2
- package/lib/util/shuffling.js.map +1 -1
- package/package.json +7 -7
- package/src/block/isValidIndexedAttestation.ts +2 -3
- package/src/block/processAttestationsAltair.ts +7 -4
- package/src/signatureSets/index.ts +3 -4
- package/src/slot/upgradeStateToAltair.ts +2 -1
- package/src/stateView/beaconStateView.ts +16 -48
- package/src/stateView/index.ts +1 -0
- package/src/stateView/interface.ts +10 -5
- package/src/stateView/stateViewFactory.ts +78 -0
- package/src/testUtils/util.ts +129 -12
- package/src/util/rootCache.ts +4 -5
- package/src/util/shuffling.ts +5 -4
|
@@ -3,8 +3,7 @@ import {ForkSeq} from "@lodestar/params";
|
|
|
3
3
|
import {IndexedAttestation, SignedBeaconBlock, altair, capella} from "@lodestar/types";
|
|
4
4
|
import {getSyncCommitteeSignatureSet} from "../block/processSyncCommittee.js";
|
|
5
5
|
import {SyncCommitteeCache} from "../cache/syncCommitteeCache.js";
|
|
6
|
-
import {
|
|
7
|
-
import {CachedBeaconStateAllForks} from "../types.js";
|
|
6
|
+
import {IBeaconStateView} from "../stateView/interface.js";
|
|
8
7
|
import {ISignatureSet} from "../util/index.js";
|
|
9
8
|
import {getAttesterSlashingsSignatureSets} from "./attesterSlashings.js";
|
|
10
9
|
import {getBlsToExecutionChangeSignatureSets} from "./blsToExecutionChange.js";
|
|
@@ -32,7 +31,7 @@ export * from "./voluntaryExits.js";
|
|
|
32
31
|
export function getBlockSignatureSets(
|
|
33
32
|
config: BeaconConfig,
|
|
34
33
|
currentSyncCommitteeIndexed: SyncCommitteeCache,
|
|
35
|
-
state:
|
|
34
|
+
state: IBeaconStateView,
|
|
36
35
|
signedBlock: SignedBeaconBlock,
|
|
37
36
|
indexedAttestations: IndexedAttestation[],
|
|
38
37
|
opts?: {
|
|
@@ -48,7 +47,7 @@ export function getBlockSignatureSets(
|
|
|
48
47
|
...getProposerSlashingsSignatureSets(config, signedBlock),
|
|
49
48
|
...getAttesterSlashingsSignatureSets(config, signedBlock),
|
|
50
49
|
...getAttestationsSignatureSets(config, signedBlock, indexedAttestations),
|
|
51
|
-
...getVoluntaryExitsSignatureSets(config,
|
|
50
|
+
...getVoluntaryExitsSignatureSets(config, state, signedBlock),
|
|
52
51
|
];
|
|
53
52
|
|
|
54
53
|
if (!opts?.skipProposerSignature) {
|
|
@@ -3,6 +3,7 @@ import {ForkSeq} from "@lodestar/params";
|
|
|
3
3
|
import {ssz} from "@lodestar/types";
|
|
4
4
|
import {getAttestationParticipationStatus} from "../block/processAttestationsAltair.js";
|
|
5
5
|
import {getCachedBeaconState} from "../cache/stateCache.js";
|
|
6
|
+
import {BeaconStateView} from "../stateView/beaconStateView.js";
|
|
6
7
|
import {CachedBeaconStateAltair, CachedBeaconStatePhase0} from "../types.js";
|
|
7
8
|
import {RootCache, newZeroedArray} from "../util/index.js";
|
|
8
9
|
import {getNextSyncCommittee} from "../util/syncCommittee.js";
|
|
@@ -125,7 +126,7 @@ function translateParticipation(
|
|
|
125
126
|
pendingAttesations: CompositeViewDU<typeof ssz.phase0.EpochAttestations>
|
|
126
127
|
): void {
|
|
127
128
|
const {epochCtx} = state;
|
|
128
|
-
const rootCache = new RootCache(state);
|
|
129
|
+
const rootCache = new RootCache(new BeaconStateView(state));
|
|
129
130
|
const epochParticipation = state.previousEpochParticipation;
|
|
130
131
|
|
|
131
132
|
for (const attestation of pendingAttesations.getAllReadonly()) {
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {CompactMultiProof, ProofType, Tree, createProof} from "@chainsafe/persistent-merkle-tree";
|
|
2
|
-
import {ByteViews} from "@chainsafe/ssz";
|
|
2
|
+
import {BitArray, ByteViews} from "@chainsafe/ssz";
|
|
3
3
|
import {BeaconConfig} from "@lodestar/config";
|
|
4
4
|
import {ForkSeq, SLOTS_PER_HISTORICAL_ROOT, isForkPostGloas} from "@lodestar/params";
|
|
5
5
|
import {
|
|
6
6
|
BeaconBlock,
|
|
7
|
+
BeaconState,
|
|
7
8
|
BlindedBeaconBlock,
|
|
8
9
|
BuilderIndex,
|
|
9
10
|
Bytes32,
|
|
@@ -33,7 +34,6 @@ import {VoluntaryExitValidity, getVoluntaryExitValidity} from "../block/processV
|
|
|
33
34
|
import {getExpectedWithdrawals} from "../block/processWithdrawals.js";
|
|
34
35
|
import {EffectiveBalanceIncrements} from "../cache/effectiveBalanceIncrements.js";
|
|
35
36
|
import {EpochTransitionCacheOpts} from "../cache/epochTransitionCache.js";
|
|
36
|
-
import {PubkeyCache, createPubkeyCache} from "../cache/pubkeyCache.js";
|
|
37
37
|
import {RewardCache} from "../cache/rewardCache.js";
|
|
38
38
|
import {
|
|
39
39
|
CachedBeaconStateAllForks,
|
|
@@ -47,7 +47,6 @@ import {
|
|
|
47
47
|
isStateValidatorsNodesPopulated,
|
|
48
48
|
} from "../cache/stateCache.js";
|
|
49
49
|
import {SyncCommitteeCache} from "../cache/syncCommitteeCache.js";
|
|
50
|
-
import {BeaconStateAllForks} from "../cache/types.js";
|
|
51
50
|
import {computeUnrealizedCheckpoints} from "../epoch/computeUnrealizedCheckpoints.js";
|
|
52
51
|
import {getFinalizedRootProof, getSyncCommitteesWitness} from "../lightClient/proofs.js";
|
|
53
52
|
import {SyncCommitteeWitness} from "../lightClient/types.js";
|
|
@@ -64,7 +63,6 @@ import {isExecutionEnabled, isExecutionStateType, isMergeTransitionComplete} fro
|
|
|
64
63
|
import {canBuilderCoverBid} from "../util/gloas.js";
|
|
65
64
|
import {loadState} from "../util/loadState/loadState.js";
|
|
66
65
|
import {getRandaoMix} from "../util/seed.js";
|
|
67
|
-
import {getStateTypeFromBytes} from "../util/sszBytes.js";
|
|
68
66
|
import {getLatestWeakSubjectivityCheckpointEpoch} from "../util/weakSubjectivity.js";
|
|
69
67
|
import {IBeaconStateView} from "./interface.js";
|
|
70
68
|
|
|
@@ -92,7 +90,7 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
92
90
|
// fulu
|
|
93
91
|
private _proposerLookahead: fulu.ProposerLookahead | null = null;
|
|
94
92
|
// gloas
|
|
95
|
-
private _executionPayloadAvailability:
|
|
93
|
+
private _executionPayloadAvailability: BitArray | null = null;
|
|
96
94
|
private _latestExecutionPayloadBid: ExecutionPayloadBid | null = null;
|
|
97
95
|
|
|
98
96
|
constructor(readonly cachedState: CachedBeaconStateAllForks) {
|
|
@@ -357,15 +355,15 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
357
355
|
|
|
358
356
|
// gloas
|
|
359
357
|
|
|
360
|
-
get executionPayloadAvailability():
|
|
358
|
+
get executionPayloadAvailability(): BitArray {
|
|
361
359
|
if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.gloas) {
|
|
362
360
|
throw new Error("executionPayloadAvailability is not available before GLOAS");
|
|
363
361
|
}
|
|
364
362
|
|
|
365
363
|
if (this._executionPayloadAvailability === null) {
|
|
366
|
-
this._executionPayloadAvailability = (
|
|
367
|
-
.
|
|
368
|
-
|
|
364
|
+
this._executionPayloadAvailability = (
|
|
365
|
+
this.cachedState as CachedBeaconStateGloas
|
|
366
|
+
).executionPayloadAvailability.toValue();
|
|
369
367
|
}
|
|
370
368
|
|
|
371
369
|
return this._executionPayloadAvailability;
|
|
@@ -404,7 +402,7 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
404
402
|
* Return the index of the validator in the PTC committee for the given slot.
|
|
405
403
|
* return -1 if validator is not in the PTC committee for the given slot.
|
|
406
404
|
*/
|
|
407
|
-
|
|
405
|
+
getIndexInPayloadTimelinessCommittee(validatorIndex: ValidatorIndex, slot: Slot): number {
|
|
408
406
|
if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.gloas) {
|
|
409
407
|
throw new Error("PTC committees are not supported before GLOAS");
|
|
410
408
|
}
|
|
@@ -503,6 +501,10 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
503
501
|
return this.cachedState.epochCtx.syncProposerReward;
|
|
504
502
|
}
|
|
505
503
|
|
|
504
|
+
getIndexedSyncCommittee(slot: Slot): SyncCommitteeCache {
|
|
505
|
+
return this.cachedState.epochCtx.getIndexedSyncCommittee(slot);
|
|
506
|
+
}
|
|
507
|
+
|
|
506
508
|
getIndexedSyncCommitteeAtEpoch(epoch: Epoch): SyncCommitteeCache {
|
|
507
509
|
return this.cachedState.epochCtx.getIndexedSyncCommitteeAtEpoch(epoch);
|
|
508
510
|
}
|
|
@@ -713,6 +715,10 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
713
715
|
return new BeaconStateView(cachedState);
|
|
714
716
|
}
|
|
715
717
|
|
|
718
|
+
toValue(): BeaconState {
|
|
719
|
+
return this.cachedState.toValue();
|
|
720
|
+
}
|
|
721
|
+
|
|
716
722
|
serialize(): Uint8Array {
|
|
717
723
|
return this.cachedState.serialize();
|
|
718
724
|
}
|
|
@@ -778,41 +784,3 @@ export class BeaconStateView implements IBeaconStateView {
|
|
|
778
784
|
return new BeaconStateView(postPayloadState);
|
|
779
785
|
}
|
|
780
786
|
}
|
|
781
|
-
|
|
782
|
-
/**
|
|
783
|
-
* Create BeaconStateView for historical state regen, no need to sync pubkey cache there.
|
|
784
|
-
*/
|
|
785
|
-
export function createBeaconStateViewForHistoricalRegen(
|
|
786
|
-
config: BeaconConfig,
|
|
787
|
-
stateBytes: Uint8Array
|
|
788
|
-
): IBeaconStateView {
|
|
789
|
-
const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
|
|
790
|
-
|
|
791
|
-
const pubkeyCache = createPubkeyCache();
|
|
792
|
-
syncPubkeyCache(state, pubkeyCache);
|
|
793
|
-
const cachedState = createCachedBeaconState(
|
|
794
|
-
state,
|
|
795
|
-
{
|
|
796
|
-
config,
|
|
797
|
-
pubkeyCache,
|
|
798
|
-
},
|
|
799
|
-
{
|
|
800
|
-
skipSyncPubkeys: true,
|
|
801
|
-
}
|
|
802
|
-
);
|
|
803
|
-
|
|
804
|
-
return new BeaconStateView(cachedState);
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
/**
|
|
808
|
-
* Populate a PubkeyIndexMap with any new entries based on a BeaconState
|
|
809
|
-
*/
|
|
810
|
-
function syncPubkeyCache(state: BeaconStateAllForks, pubkeyCache: PubkeyCache): void {
|
|
811
|
-
// Get the validators sub tree once for all the loop
|
|
812
|
-
|
|
813
|
-
const newCount = state.validators.length;
|
|
814
|
-
for (let i = pubkeyCache.size; i < newCount; i++) {
|
|
815
|
-
const pubkey = state.validators.getReadonly(i).pubkey;
|
|
816
|
-
pubkeyCache.set(i, pubkey);
|
|
817
|
-
}
|
|
818
|
-
}
|
package/src/stateView/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {CompactMultiProof} from "@chainsafe/persistent-merkle-tree";
|
|
2
|
-
import {ByteViews} from "@chainsafe/ssz";
|
|
2
|
+
import {BitArray, ByteViews} from "@chainsafe/ssz";
|
|
3
3
|
import {
|
|
4
4
|
BeaconBlock,
|
|
5
|
+
BeaconState,
|
|
5
6
|
BlindedBeaconBlock,
|
|
6
7
|
BuilderIndex,
|
|
7
8
|
Bytes32,
|
|
@@ -93,11 +94,11 @@ export interface IBeaconStateView {
|
|
|
93
94
|
proposerLookahead: fulu.ProposerLookahead;
|
|
94
95
|
|
|
95
96
|
// gloas
|
|
96
|
-
executionPayloadAvailability:
|
|
97
|
+
executionPayloadAvailability: BitArray;
|
|
97
98
|
latestExecutionPayloadBid: ExecutionPayloadBid;
|
|
98
99
|
getBuilder(index: BuilderIndex): gloas.Builder;
|
|
99
100
|
canBuilderCoverBid(builderIndex: BuilderIndex, bidAmount: number): boolean;
|
|
100
|
-
|
|
101
|
+
getIndexInPayloadTimelinessCommittee(validatorIndex: ValidatorIndex, slot: Slot): number;
|
|
101
102
|
|
|
102
103
|
// Shuffling and committees
|
|
103
104
|
getShufflingAtEpoch(epoch: Epoch): EpochShuffling;
|
|
@@ -110,12 +111,11 @@ export interface IBeaconStateView {
|
|
|
110
111
|
getCurrentShuffling(): EpochShuffling;
|
|
111
112
|
getNextShuffling(): EpochShuffling;
|
|
112
113
|
|
|
113
|
-
//
|
|
114
|
+
// Proposer shuffling
|
|
114
115
|
previousProposers: ValidatorIndex[] | null;
|
|
115
116
|
currentProposers: ValidatorIndex[];
|
|
116
117
|
nextProposers: ValidatorIndex[];
|
|
117
118
|
getBeaconProposer(slot: Slot): ValidatorIndex;
|
|
118
|
-
computeAnchorCheckpoint(): {checkpoint: phase0.Checkpoint; blockHeader: phase0.BeaconBlockHeader};
|
|
119
119
|
|
|
120
120
|
// Sync committees
|
|
121
121
|
currentSyncCommittee: altair.SyncCommittee;
|
|
@@ -123,6 +123,8 @@ export interface IBeaconStateView {
|
|
|
123
123
|
currentSyncCommitteeIndexed: SyncCommitteeCache;
|
|
124
124
|
syncProposerReward: number;
|
|
125
125
|
getIndexedSyncCommitteeAtEpoch(epoch: Epoch): SyncCommitteeCache;
|
|
126
|
+
/** Get indexed sync committee with slot+1 offset for duty lookups */
|
|
127
|
+
getIndexedSyncCommittee(slot: Slot): SyncCommitteeCache;
|
|
126
128
|
|
|
127
129
|
// Validators and balances
|
|
128
130
|
effectiveBalanceIncrements: EffectiveBalanceIncrements;
|
|
@@ -149,6 +151,7 @@ export interface IBeaconStateView {
|
|
|
149
151
|
expectedWithdrawals: capella.Withdrawal[];
|
|
150
152
|
processedBuilderWithdrawalsCount: number;
|
|
151
153
|
processedPartialWithdrawalsCount: number;
|
|
154
|
+
processedBuildersSweepCount: number;
|
|
152
155
|
processedValidatorSweepCount: number;
|
|
153
156
|
};
|
|
154
157
|
|
|
@@ -180,6 +183,7 @@ export interface IBeaconStateView {
|
|
|
180
183
|
justifiedCheckpoint: phase0.Checkpoint;
|
|
181
184
|
finalizedCheckpoint: phase0.Checkpoint;
|
|
182
185
|
};
|
|
186
|
+
computeAnchorCheckpoint(): {checkpoint: phase0.Checkpoint; blockHeader: phase0.BeaconBlockHeader};
|
|
183
187
|
|
|
184
188
|
// this is for backward compatible
|
|
185
189
|
clonedCount: number;
|
|
@@ -190,6 +194,7 @@ export interface IBeaconStateView {
|
|
|
190
194
|
|
|
191
195
|
// Serialization
|
|
192
196
|
loadOtherState(stateBytes: Uint8Array, seedValidatorsBytes?: Uint8Array): IBeaconStateView;
|
|
197
|
+
toValue(): BeaconState;
|
|
193
198
|
serialize(): Uint8Array;
|
|
194
199
|
serializedSize(): number;
|
|
195
200
|
serializeToBytes(output: ByteViews, offset: number): number;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import {BeaconConfig} from "@lodestar/config";
|
|
2
|
+
import {PubkeyCache, createPubkeyCache} from "../cache/pubkeyCache.js";
|
|
3
|
+
import {createCachedBeaconState} from "../cache/stateCache.js";
|
|
4
|
+
import {BeaconStateAllForks} from "../cache/types.js";
|
|
5
|
+
import {getStateTypeFromBytes} from "../util/sszBytes.js";
|
|
6
|
+
import {BeaconStateView} from "./beaconStateView.js";
|
|
7
|
+
import {IBeaconStateView} from "./interface.js";
|
|
8
|
+
|
|
9
|
+
// ---- createBeaconStateView (startup path) ----
|
|
10
|
+
|
|
11
|
+
type NodeJSOpts = {
|
|
12
|
+
useNative: false;
|
|
13
|
+
anchorState: BeaconStateAllForks;
|
|
14
|
+
config: BeaconConfig;
|
|
15
|
+
pubkeyCache: PubkeyCache;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type NativeOpts = {
|
|
19
|
+
useNative: true;
|
|
20
|
+
stateBytes: Uint8Array;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Create a BeaconStateView from a pre-deserialized state. Used at node startup.
|
|
25
|
+
*
|
|
26
|
+
* The caller is responsible for creating and populating `pubkeyCache` (it is also
|
|
27
|
+
* passed separately to BeaconNode.init, so it must live outside this factory).
|
|
28
|
+
*
|
|
29
|
+
* Set `useNative: true` to use the native (Zig) implementation once available.
|
|
30
|
+
*/
|
|
31
|
+
export function createBeaconStateView(opts: NodeJSOpts | NativeOpts): IBeaconStateView {
|
|
32
|
+
if (opts.useNative) {
|
|
33
|
+
throw new Error("Native (Zig) BeaconStateView not yet implemented");
|
|
34
|
+
}
|
|
35
|
+
const {anchorState, config, pubkeyCache} = opts;
|
|
36
|
+
const cachedState = createCachedBeaconState(anchorState, {config, pubkeyCache}, {skipSyncPubkeys: true});
|
|
37
|
+
return new BeaconStateView(cachedState);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ---- createBeaconStateViewForHistoricalRegen (regen path) ----
|
|
41
|
+
|
|
42
|
+
// Reused across all historical state regen calls in the worker thread
|
|
43
|
+
const pubkeyCacheRegen = createPubkeyCache();
|
|
44
|
+
|
|
45
|
+
function syncPubkeyCache(state: BeaconStateAllForks, pubkeyCache: PubkeyCache): void {
|
|
46
|
+
const newCount = state.validators.length;
|
|
47
|
+
for (let i = pubkeyCache.size; i < newCount; i++) {
|
|
48
|
+
const pubkey = state.validators.getReadonly(i).pubkey;
|
|
49
|
+
pubkeyCache.set(i, pubkey);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type RegenNodeJSOpts = {
|
|
54
|
+
useNative: false;
|
|
55
|
+
config: BeaconConfig;
|
|
56
|
+
stateBytes: Uint8Array;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
type RegenNativeOpts = {
|
|
60
|
+
useNative: true;
|
|
61
|
+
stateBytes: Uint8Array;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Create a BeaconStateView from raw SSZ bytes. Used in the historical state regen worker thread.
|
|
66
|
+
*
|
|
67
|
+
* Set `useNative: true` to use the native (Zig) implementation once available.
|
|
68
|
+
*/
|
|
69
|
+
export function createBeaconStateViewForHistoricalRegen(opts: RegenNodeJSOpts | RegenNativeOpts): IBeaconStateView {
|
|
70
|
+
if (opts.useNative) {
|
|
71
|
+
throw new Error("Native (Zig) BeaconStateView not yet implemented");
|
|
72
|
+
}
|
|
73
|
+
const {config, stateBytes} = opts;
|
|
74
|
+
const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
|
|
75
|
+
syncPubkeyCache(state, pubkeyCacheRegen);
|
|
76
|
+
const cachedState = createCachedBeaconState(state, {config, pubkeyCache: pubkeyCacheRegen}, {skipSyncPubkeys: true});
|
|
77
|
+
return new BeaconStateView(cachedState);
|
|
78
|
+
}
|
package/src/testUtils/util.ts
CHANGED
|
@@ -25,9 +25,11 @@ import {
|
|
|
25
25
|
} from "../index.js";
|
|
26
26
|
import {
|
|
27
27
|
BeaconStateAltair,
|
|
28
|
+
BeaconStateElectra,
|
|
28
29
|
BeaconStatePhase0,
|
|
29
30
|
CachedBeaconStateAllForks,
|
|
30
31
|
CachedBeaconStateAltair,
|
|
32
|
+
CachedBeaconStateElectra,
|
|
31
33
|
CachedBeaconStatePhase0,
|
|
32
34
|
} from "../types.js";
|
|
33
35
|
import {getNextSyncCommittee} from "../util/syncCommittee.js";
|
|
@@ -38,9 +40,12 @@ let phase0State: BeaconStatePhase0 | null = null;
|
|
|
38
40
|
let phase0CachedState23637: CachedBeaconStatePhase0 | null = null;
|
|
39
41
|
let phase0CachedState23638: CachedBeaconStatePhase0 | null = null;
|
|
40
42
|
let phase0SignedBlock: phase0.SignedBeaconBlock | null = null;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
const altairStateByVc = new Map<number, BeaconStateAltair>();
|
|
44
|
+
const altairCachedState23637ByVc = new Map<number, CachedBeaconStateAltair>();
|
|
45
|
+
const altairCachedState23638ByVc = new Map<number, CachedBeaconStateAltair>();
|
|
46
|
+
const electraStateByVc = new Map<number, BeaconStateElectra>();
|
|
47
|
+
const electraCachedState23637ByVc = new Map<number, CachedBeaconStateElectra>();
|
|
48
|
+
const electraCachedState23638ByVc = new Map<number, CachedBeaconStateElectra>();
|
|
44
49
|
|
|
45
50
|
/**
|
|
46
51
|
* Number of validators in prater is 210000 as of May 2021
|
|
@@ -84,10 +89,10 @@ export function getSecretKeyFromIndexCached(validatorIndex: number): SecretKey {
|
|
|
84
89
|
return sk;
|
|
85
90
|
}
|
|
86
91
|
|
|
87
|
-
function getPubkeyCaches({pubkeysMod}: ReturnType<typeof getPubkeys
|
|
92
|
+
function getPubkeyCaches({pubkeysMod}: ReturnType<typeof getPubkeys>, vc = numValidators) {
|
|
88
93
|
// Manually sync pubkeys to prevent doing BLS opts 110_000 times
|
|
89
94
|
const pubkeyCache = createPubkeyCache();
|
|
90
|
-
for (let i = 0; i <
|
|
95
|
+
for (let i = 0; i < vc; i++) {
|
|
91
96
|
const pubkey = pubkeysMod[i % keypairsMod];
|
|
92
97
|
pubkeyCache.set(i, pubkey);
|
|
93
98
|
}
|
|
@@ -184,7 +189,8 @@ export function generatePerfTestCachedStatePhase0(opts?: {goBackOneSlot: boolean
|
|
|
184
189
|
}
|
|
185
190
|
const resultingState = opts?.goBackOneSlot ? phase0CachedState23637 : phase0CachedState23638;
|
|
186
191
|
|
|
187
|
-
|
|
192
|
+
// Use dontTransferCache to preserve the global cache for subsequent callers
|
|
193
|
+
return resultingState.clone(true);
|
|
188
194
|
}
|
|
189
195
|
|
|
190
196
|
export function cachedStateAltairPopulateCaches(state: CachedBeaconStateAltair): void {
|
|
@@ -206,39 +212,96 @@ export function generatePerfTestCachedStateAltair(opts?: {
|
|
|
206
212
|
goBackOneSlot: boolean;
|
|
207
213
|
vc?: number;
|
|
208
214
|
}): CachedBeaconStateAltair {
|
|
209
|
-
const
|
|
210
|
-
const {
|
|
215
|
+
const vc = opts?.vc ?? numValidators;
|
|
216
|
+
const {pubkeys, pubkeysMod, pubkeysModObj} = getPubkeys(vc);
|
|
217
|
+
const {pubkeyCache} = getPubkeyCaches({pubkeys, pubkeysMod, pubkeysModObj}, vc);
|
|
211
218
|
|
|
212
219
|
const altairConfig = createChainForkConfig({ALTAIR_FORK_EPOCH: 0});
|
|
213
220
|
|
|
214
221
|
const origState = generatePerformanceStateAltair(pubkeys);
|
|
215
222
|
|
|
223
|
+
let altairCachedState23637 = altairCachedState23637ByVc.get(vc);
|
|
216
224
|
if (!altairCachedState23637) {
|
|
217
|
-
const state = origState.clone();
|
|
225
|
+
const state = origState.clone(true);
|
|
218
226
|
state.slot -= 1;
|
|
219
227
|
altairCachedState23637 = createCachedBeaconState(state, {
|
|
220
228
|
config: createBeaconConfig(altairConfig, state.genesisValidatorsRoot),
|
|
221
229
|
pubkeyCache,
|
|
222
230
|
});
|
|
231
|
+
altairCachedState23637ByVc.set(vc, altairCachedState23637);
|
|
223
232
|
}
|
|
233
|
+
|
|
234
|
+
let altairCachedState23638 = altairCachedState23638ByVc.get(vc);
|
|
224
235
|
if (!altairCachedState23638) {
|
|
225
236
|
altairCachedState23638 = processSlots(
|
|
226
237
|
altairCachedState23637,
|
|
227
238
|
altairCachedState23637.slot + 1
|
|
228
239
|
) as CachedBeaconStateAltair;
|
|
229
240
|
altairCachedState23638.slot += 1;
|
|
241
|
+
altairCachedState23638ByVc.set(vc, altairCachedState23638);
|
|
230
242
|
}
|
|
243
|
+
|
|
231
244
|
const resultingState = opts?.goBackOneSlot ? altairCachedState23637 : altairCachedState23638;
|
|
232
245
|
|
|
233
|
-
|
|
246
|
+
// Use dontTransferCache to preserve the global cache for subsequent callers
|
|
247
|
+
return resultingState.clone(true);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Warning: This function has side effects on the cached state
|
|
252
|
+
* The order in which the caches are populated is important and can cause stable tests to fail.
|
|
253
|
+
*/
|
|
254
|
+
export function generatePerfTestCachedStateElectra(opts?: {
|
|
255
|
+
goBackOneSlot: boolean;
|
|
256
|
+
vc?: number;
|
|
257
|
+
}): CachedBeaconStateElectra {
|
|
258
|
+
const vc = opts?.vc ?? numValidators;
|
|
259
|
+
const {pubkeys, pubkeysMod, pubkeysModObj} = getPubkeys(vc);
|
|
260
|
+
const {pubkeyCache} = getPubkeyCaches({pubkeys, pubkeysMod, pubkeysModObj}, vc);
|
|
261
|
+
|
|
262
|
+
const electraConfig = createChainForkConfig({
|
|
263
|
+
ALTAIR_FORK_EPOCH: 0,
|
|
264
|
+
BELLATRIX_FORK_EPOCH: 0,
|
|
265
|
+
CAPELLA_FORK_EPOCH: 0,
|
|
266
|
+
DENEB_FORK_EPOCH: 0,
|
|
267
|
+
ELECTRA_FORK_EPOCH: 0,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
const origState = generatePerformanceStateElectra(pubkeys);
|
|
271
|
+
|
|
272
|
+
let electraCachedState23637 = electraCachedState23637ByVc.get(vc);
|
|
273
|
+
if (!electraCachedState23637) {
|
|
274
|
+
const state = origState.clone(true);
|
|
275
|
+
state.slot -= 1;
|
|
276
|
+
electraCachedState23637 = createCachedBeaconState(state, {
|
|
277
|
+
config: createBeaconConfig(electraConfig, state.genesisValidatorsRoot),
|
|
278
|
+
pubkeyCache,
|
|
279
|
+
});
|
|
280
|
+
electraCachedState23637ByVc.set(vc, electraCachedState23637);
|
|
281
|
+
}
|
|
282
|
+
let electraCachedState23638 = electraCachedState23638ByVc.get(vc);
|
|
283
|
+
if (!electraCachedState23638) {
|
|
284
|
+
electraCachedState23638 = processSlots(
|
|
285
|
+
electraCachedState23637,
|
|
286
|
+
electraCachedState23637.slot + 1
|
|
287
|
+
) as CachedBeaconStateElectra;
|
|
288
|
+
electraCachedState23638.slot += 1;
|
|
289
|
+
electraCachedState23638ByVc.set(vc, electraCachedState23638);
|
|
290
|
+
}
|
|
291
|
+
const resultingState = opts?.goBackOneSlot ? electraCachedState23637 : electraCachedState23638;
|
|
292
|
+
|
|
293
|
+
// Use dontTransferCache to preserve the global cache for subsequent callers
|
|
294
|
+
return resultingState.clone(true);
|
|
234
295
|
}
|
|
235
296
|
|
|
236
297
|
/**
|
|
237
298
|
* This is generated from Medalla state 756416
|
|
238
299
|
*/
|
|
239
300
|
export function generatePerformanceStateAltair(pubkeysArg?: Uint8Array[]): BeaconStateAltair {
|
|
301
|
+
const pubkeys = pubkeysArg || getPubkeys().pubkeys;
|
|
302
|
+
const vc = pubkeys.length;
|
|
303
|
+
let altairState = altairStateByVc.get(vc);
|
|
240
304
|
if (!altairState) {
|
|
241
|
-
const pubkeys = pubkeysArg || getPubkeys().pubkeys;
|
|
242
305
|
const statePhase0 = buildPerformanceStatePhase0(pubkeys);
|
|
243
306
|
const state = statePhase0 as BeaconState as BeaconState<ForkName.altair>;
|
|
244
307
|
|
|
@@ -270,8 +333,62 @@ export function generatePerformanceStateAltair(pubkeysArg?: Uint8Array[]): Beaco
|
|
|
270
333
|
altairState = ssz.altair.BeaconState.toViewDU(state);
|
|
271
334
|
// cache roots
|
|
272
335
|
altairState.hashTreeRoot();
|
|
336
|
+
altairStateByVc.set(vc, altairState);
|
|
337
|
+
}
|
|
338
|
+
// Use dontTransferCache to preserve the global cache for subsequent callers
|
|
339
|
+
return altairState.clone(true);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* This is generated from the same performance state as Altair, upgraded to Electra fields.
|
|
344
|
+
*/
|
|
345
|
+
export function generatePerformanceStateElectra(pubkeysArg?: Uint8Array[]): BeaconStateElectra {
|
|
346
|
+
const pubkeys = pubkeysArg || getPubkeys().pubkeys;
|
|
347
|
+
const vc = pubkeys.length;
|
|
348
|
+
let electraState = electraStateByVc.get(vc);
|
|
349
|
+
if (!electraState) {
|
|
350
|
+
const electraConfig = createChainForkConfig({
|
|
351
|
+
ALTAIR_FORK_EPOCH: 0,
|
|
352
|
+
BELLATRIX_FORK_EPOCH: 0,
|
|
353
|
+
CAPELLA_FORK_EPOCH: 0,
|
|
354
|
+
DENEB_FORK_EPOCH: 0,
|
|
355
|
+
ELECTRA_FORK_EPOCH: 0,
|
|
356
|
+
});
|
|
357
|
+
const state = ssz.electra.BeaconState.defaultValue();
|
|
358
|
+
|
|
359
|
+
Object.assign(state, buildPerformanceStatePhase0(pubkeys));
|
|
360
|
+
|
|
361
|
+
state.fork.previousVersion = electraConfig.DENEB_FORK_VERSION;
|
|
362
|
+
state.fork.currentVersion = electraConfig.ELECTRA_FORK_VERSION;
|
|
363
|
+
state.fork.epoch = electraConfig.ELECTRA_FORK_EPOCH;
|
|
364
|
+
state.previousEpochParticipation = newFilledArray(pubkeys.length, 0b111);
|
|
365
|
+
state.currentEpochParticipation = state.previousEpochParticipation;
|
|
366
|
+
state.inactivityScores = Array.from({length: pubkeys.length}, (_, i) => i % 2);
|
|
367
|
+
state.currentSyncCommittee = ssz.altair.SyncCommittee.defaultValue();
|
|
368
|
+
state.nextSyncCommittee = state.currentSyncCommittee;
|
|
369
|
+
state.latestExecutionPayloadHeader = ssz.electra.ExecutionPayloadHeader.defaultValue();
|
|
370
|
+
state.depositRequestsStartIndex = 2023n;
|
|
371
|
+
|
|
372
|
+
electraState = ssz.electra.BeaconState.toViewDU(state);
|
|
373
|
+
|
|
374
|
+
const epoch = computeEpochAtSlot(state.slot);
|
|
375
|
+
const activeValidatorIndices = getActiveValidatorIndices(electraState, epoch);
|
|
376
|
+
const effectiveBalanceIncrements = getEffectiveBalanceIncrements(electraState);
|
|
377
|
+
const {syncCommittee} = getNextSyncCommittee(
|
|
378
|
+
ForkSeq.electra,
|
|
379
|
+
electraState,
|
|
380
|
+
activeValidatorIndices,
|
|
381
|
+
effectiveBalanceIncrements
|
|
382
|
+
);
|
|
383
|
+
state.currentSyncCommittee = syncCommittee;
|
|
384
|
+
state.nextSyncCommittee = syncCommittee;
|
|
385
|
+
|
|
386
|
+
electraState = ssz.electra.BeaconState.toViewDU(state);
|
|
387
|
+
electraState.hashTreeRoot();
|
|
388
|
+
electraStateByVc.set(vc, electraState);
|
|
273
389
|
}
|
|
274
|
-
|
|
390
|
+
// Use dontTransferCache to preserve the global cache for subsequent callers
|
|
391
|
+
return electraState.clone(true);
|
|
275
392
|
}
|
|
276
393
|
|
|
277
394
|
/**
|
package/src/util/rootCache.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {Epoch, Root, Slot, phase0} from "@lodestar/types";
|
|
2
|
-
import {
|
|
3
|
-
import {getBlockRoot, getBlockRootAtSlot} from "./blockRoot.js";
|
|
2
|
+
import {IBeaconStateView} from "../stateView/interface.js";
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* Cache to prevent accessing the state tree to fetch block roots repeteadly.
|
|
@@ -12,7 +11,7 @@ export class RootCache {
|
|
|
12
11
|
private readonly blockRootEpochCache = new Map<Epoch, Root>();
|
|
13
12
|
private readonly blockRootSlotCache = new Map<Slot, Root>();
|
|
14
13
|
|
|
15
|
-
constructor(private readonly state:
|
|
14
|
+
constructor(private readonly state: IBeaconStateView) {
|
|
16
15
|
this.currentJustifiedCheckpoint = state.currentJustifiedCheckpoint;
|
|
17
16
|
this.previousJustifiedCheckpoint = state.previousJustifiedCheckpoint;
|
|
18
17
|
}
|
|
@@ -20,7 +19,7 @@ export class RootCache {
|
|
|
20
19
|
getBlockRoot(epoch: Epoch): Root {
|
|
21
20
|
let root = this.blockRootEpochCache.get(epoch);
|
|
22
21
|
if (!root) {
|
|
23
|
-
root =
|
|
22
|
+
root = this.state.getBlockRootAtEpoch(epoch);
|
|
24
23
|
this.blockRootEpochCache.set(epoch, root);
|
|
25
24
|
}
|
|
26
25
|
return root;
|
|
@@ -29,7 +28,7 @@ export class RootCache {
|
|
|
29
28
|
getBlockRootAtSlot(slot: Slot): Root {
|
|
30
29
|
let root = this.blockRootSlotCache.get(slot);
|
|
31
30
|
if (!root) {
|
|
32
|
-
root =
|
|
31
|
+
root = this.state.getBlockRootAtSlot(slot);
|
|
33
32
|
this.blockRootSlotCache.set(slot, root);
|
|
34
33
|
}
|
|
35
34
|
return root;
|
package/src/util/shuffling.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from "@lodestar/types";
|
|
12
12
|
import {LodestarError} from "@lodestar/utils";
|
|
13
13
|
import {CachedBeaconStateAllForks} from "../cache/stateCache.js";
|
|
14
|
+
import {IBeaconStateView} from "../stateView/interface.js";
|
|
14
15
|
import {getBlockRootAtSlot} from "./blockRoot.js";
|
|
15
16
|
import {computeStartSlotAtEpoch} from "./epoch.js";
|
|
16
17
|
import {EpochShuffling} from "./epochShuffling.js";
|
|
@@ -22,21 +23,21 @@ import {EpochShuffling} from "./epochShuffling.js";
|
|
|
22
23
|
* Returns `null` on the one-off scenario where the genesis block decides its own shuffling.
|
|
23
24
|
* It should be set to the latest block applied to this `state` or the genesis block root.
|
|
24
25
|
*/
|
|
25
|
-
export function proposerShufflingDecisionRoot(fork: ForkName, state:
|
|
26
|
+
export function proposerShufflingDecisionRoot(fork: ForkName, state: IBeaconStateView): Root | null {
|
|
26
27
|
const decisionSlot = proposerShufflingDecisionSlot(fork, state);
|
|
27
28
|
if (state.slot === decisionSlot) {
|
|
28
29
|
return null;
|
|
29
30
|
}
|
|
30
|
-
return getBlockRootAtSlot(
|
|
31
|
+
return state.getBlockRootAtSlot(decisionSlot);
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
/**
|
|
34
35
|
* Returns the slot at which the proposer shuffling was decided. The block root at this slot
|
|
35
36
|
* can be used to key the proposer shuffling for the current epoch.
|
|
36
37
|
*/
|
|
37
|
-
function proposerShufflingDecisionSlot(fork: ForkName, state:
|
|
38
|
+
function proposerShufflingDecisionSlot(fork: ForkName, state: IBeaconStateView): Slot {
|
|
38
39
|
// After fulu, the decision slot is in previous epoch due to deterministic proposer lookahead
|
|
39
|
-
const epoch = isForkPostFulu(fork) ? state.
|
|
40
|
+
const epoch = isForkPostFulu(fork) ? state.epoch - 1 : state.epoch;
|
|
40
41
|
const startSlot = computeStartSlotAtEpoch(epoch);
|
|
41
42
|
return Math.max(startSlot - 1, 0);
|
|
42
43
|
}
|