@lodestar/state-transition 1.43.0-dev.9fa9f08ef6 → 1.43.0-dev.abc719ddc1

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.
Files changed (42) hide show
  1. package/lib/cache/epochCache.d.ts +3 -1
  2. package/lib/cache/epochCache.d.ts.map +1 -1
  3. package/lib/cache/epochCache.js +31 -13
  4. package/lib/cache/epochCache.js.map +1 -1
  5. package/lib/cache/epochTransitionCache.d.ts +5 -0
  6. package/lib/cache/epochTransitionCache.d.ts.map +1 -1
  7. package/lib/cache/epochTransitionCache.js +1 -0
  8. package/lib/cache/epochTransitionCache.js.map +1 -1
  9. package/lib/epoch/index.d.ts +3 -1
  10. package/lib/epoch/index.d.ts.map +1 -1
  11. package/lib/epoch/index.js +8 -1
  12. package/lib/epoch/index.js.map +1 -1
  13. package/lib/epoch/processPtcWindow.d.ts +11 -0
  14. package/lib/epoch/processPtcWindow.d.ts.map +1 -0
  15. package/lib/epoch/processPtcWindow.js +28 -0
  16. package/lib/epoch/processPtcWindow.js.map +1 -0
  17. package/lib/slot/upgradeStateToGloas.d.ts.map +1 -1
  18. package/lib/slot/upgradeStateToGloas.js +2 -1
  19. package/lib/slot/upgradeStateToGloas.js.map +1 -1
  20. package/lib/stateTransition.js +1 -1
  21. package/lib/stateTransition.js.map +1 -1
  22. package/lib/stateView/beaconStateView.d.ts +3 -1
  23. package/lib/stateView/beaconStateView.d.ts.map +1 -1
  24. package/lib/stateView/beaconStateView.js +5 -4
  25. package/lib/stateView/beaconStateView.js.map +1 -1
  26. package/lib/stateView/interface.d.ts +5 -1
  27. package/lib/stateView/interface.d.ts.map +1 -1
  28. package/lib/stateView/interface.js.map +1 -1
  29. package/lib/util/gloas.d.ts +7 -1
  30. package/lib/util/gloas.d.ts.map +1 -1
  31. package/lib/util/gloas.js +26 -1
  32. package/lib/util/gloas.js.map +1 -1
  33. package/package.json +8 -8
  34. package/src/cache/epochCache.ts +32 -30
  35. package/src/cache/epochTransitionCache.ts +7 -0
  36. package/src/epoch/index.ts +9 -0
  37. package/src/epoch/processPtcWindow.ts +38 -0
  38. package/src/slot/upgradeStateToGloas.ts +2 -1
  39. package/src/stateTransition.ts +1 -1
  40. package/src/stateView/beaconStateView.ts +9 -4
  41. package/src/stateView/interface.ts +7 -1
  42. package/src/util/gloas.ts +48 -1
@@ -704,7 +704,11 @@ export class BeaconStateView implements IBeaconStateViewLatestFork {
704
704
 
705
705
  // Serialization
706
706
 
707
- loadOtherState(stateBytes: Uint8Array, seedValidatorsBytes?: Uint8Array): IBeaconStateView {
707
+ loadOtherState(
708
+ stateBytes: Uint8Array,
709
+ seedValidatorsBytes?: Uint8Array,
710
+ opts?: {preloadValidatorsAndBalances?: boolean}
711
+ ): IBeaconStateView {
708
712
  const {state} = loadState(this.config, this.cachedState, stateBytes, seedValidatorsBytes);
709
713
 
710
714
  const cachedState = createCachedBeaconState(
@@ -719,9 +723,10 @@ export class BeaconStateView implements IBeaconStateViewLatestFork {
719
723
  }
720
724
  );
721
725
 
722
- // load all cache in order for consumers (usually regen.getState()) to process blocks faster
723
- cachedState.validators.getAllReadonlyValues();
724
- cachedState.balances.getAll();
726
+ if (opts?.preloadValidatorsAndBalances) {
727
+ cachedState.validators.getAllReadonlyValues();
728
+ cachedState.balances.getAll();
729
+ }
725
730
 
726
731
  return new BeaconStateView(cachedState);
727
732
  }
@@ -138,7 +138,13 @@ export interface IBeaconStateView {
138
138
  isStateValidatorsNodesPopulated(): boolean;
139
139
 
140
140
  // Serialization
141
- loadOtherState(stateBytes: Uint8Array, seedValidatorsBytes?: Uint8Array): IBeaconStateView;
141
+ /** Set `preloadValidatorsAndBalances` only when the whole state will be consumed
142
+ * immediately (e.g. CP reload before block replay). */
143
+ loadOtherState(
144
+ stateBytes: Uint8Array,
145
+ seedValidatorsBytes?: Uint8Array,
146
+ opts?: {preloadValidatorsAndBalances?: boolean}
147
+ ): IBeaconStateView;
142
148
  toValue(): BeaconState;
143
149
  serialize(): Uint8Array;
144
150
  serializedSize(): number;
package/src/util/gloas.ts CHANGED
@@ -6,15 +6,20 @@ import {
6
6
  EFFECTIVE_BALANCE_INCREMENT,
7
7
  FAR_FUTURE_EPOCH,
8
8
  MIN_DEPOSIT_AMOUNT,
9
+ MIN_SEED_LOOKAHEAD,
10
+ PTC_SIZE,
9
11
  SLOTS_PER_EPOCH,
10
12
  } from "@lodestar/params";
11
13
  import {BuilderIndex, Epoch, ValidatorIndex, gloas} from "@lodestar/types";
12
14
  import {AttestationData} from "@lodestar/types/phase0";
13
15
  import {byteArrayEquals} from "@lodestar/utils";
14
- import {CachedBeaconStateGloas} from "../types.js";
16
+ import {CachedBeaconStateFulu, CachedBeaconStateGloas} from "../types.js";
15
17
  import {getBlockRootAtSlot} from "./blockRoot.js";
16
18
  import {computeEpochAtSlot} from "./epoch.js";
19
+ import {computeEpochShuffling} from "./epochShuffling.js";
17
20
  import {RootCache} from "./rootCache.js";
21
+ import {computePayloadTimelinessCommitteesForEpoch} from "./seed.js";
22
+ import {getActiveValidatorIndices} from "./validator.js";
18
23
 
19
24
  export function isBuilderWithdrawalCredential(withdrawalCredentials: Uint8Array): boolean {
20
25
  return withdrawalCredentials[0] === BUILDER_WITHDRAWAL_PREFIX;
@@ -170,3 +175,45 @@ export function isAttestationSameSlotRootCache(rootCache: RootCache, data: Attes
170
175
  export function isParentBlockFull(state: CachedBeaconStateGloas): boolean {
171
176
  return byteArrayEquals(state.latestExecutionPayloadBid.blockHash, state.latestBlockHash);
172
177
  }
178
+
179
+ export function initializePtcWindow(state: CachedBeaconStateFulu): Uint32Array[] {
180
+ const ptcWindow: Uint32Array[] = Array.from({length: SLOTS_PER_EPOCH}, () => new Uint32Array(PTC_SIZE));
181
+ const currentEpoch = state.epochCtx.epoch;
182
+
183
+ for (let epochOffset = 0; epochOffset <= MIN_SEED_LOOKAHEAD; epochOffset++) {
184
+ const epoch = currentEpoch + epochOffset;
185
+ const shuffling =
186
+ state.epochCtx.getShufflingAtEpochOrNull(epoch) ??
187
+ computeEpochShuffling(state, getActiveValidatorIndices(state, epoch), epoch);
188
+
189
+ ptcWindow.push(
190
+ ...computePayloadTimelinessCommitteesForEpoch(
191
+ state,
192
+ epoch,
193
+ shuffling.committees,
194
+ state.epochCtx.effectiveBalanceIncrements
195
+ )
196
+ );
197
+ }
198
+
199
+ return ptcWindow;
200
+ }
201
+
202
+ export function getPtcWindowEpochCacheData(state: CachedBeaconStateGloas): {
203
+ previousPayloadTimelinessCommittees: Uint32Array[];
204
+ payloadTimelinessCommittees: Uint32Array[];
205
+ nextPayloadTimelinessCommittees: Uint32Array[];
206
+ } {
207
+ const toUint32Arrays = (views: ReturnType<typeof state.ptcWindow.getReadonlyByRange>) =>
208
+ views.map((v) => Uint32Array.from(v.getAll()));
209
+
210
+ const previousPtcWindow = state.ptcWindow.getReadonlyByRange(0, SLOTS_PER_EPOCH);
211
+ const currentPtcWindow = state.ptcWindow.getReadonlyByRange(SLOTS_PER_EPOCH, SLOTS_PER_EPOCH);
212
+ const nextPtcWindow = state.ptcWindow.getReadonlyByRange(2 * SLOTS_PER_EPOCH, SLOTS_PER_EPOCH);
213
+
214
+ return {
215
+ previousPayloadTimelinessCommittees: toUint32Arrays(previousPtcWindow),
216
+ payloadTimelinessCommittees: toUint32Arrays(currentPtcWindow),
217
+ nextPayloadTimelinessCommittees: toUint32Arrays(nextPtcWindow),
218
+ };
219
+ }