@lodestar/state-transition 1.41.0-dev.bb33751bfd → 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.
Files changed (204) hide show
  1. package/lib/block/externalData.js +4 -2
  2. package/lib/block/externalData.js.map +1 -1
  3. package/lib/block/index.d.ts +3 -3
  4. package/lib/block/index.js.map +1 -1
  5. package/lib/block/initiateValidatorExit.js.map +1 -1
  6. package/lib/block/isValidIndexedAttestation.js.map +1 -1
  7. package/lib/block/isValidIndexedPayloadAttestation.js.map +1 -1
  8. package/lib/block/processAttestationPhase0.js.map +1 -1
  9. package/lib/block/processAttestations.js.map +1 -1
  10. package/lib/block/processAttestationsAltair.js.map +1 -1
  11. package/lib/block/processAttesterSlashing.js.map +1 -1
  12. package/lib/block/processBlobKzgCommitments.js.map +1 -1
  13. package/lib/block/processBlockHeader.js.map +1 -1
  14. package/lib/block/processBlsToExecutionChange.js.map +1 -1
  15. package/lib/block/processConsolidationRequest.js.map +1 -1
  16. package/lib/block/processDeposit.js.map +1 -1
  17. package/lib/block/processDepositRequest.js.map +1 -1
  18. package/lib/block/processEth1Data.js.map +1 -1
  19. package/lib/block/processExecutionPayload.js.map +1 -1
  20. package/lib/block/processExecutionPayloadBid.d.ts +1 -1
  21. package/lib/block/processExecutionPayloadBid.js.map +1 -1
  22. package/lib/block/processExecutionPayloadEnvelope.d.ts +5 -2
  23. package/lib/block/processExecutionPayloadEnvelope.d.ts.map +1 -1
  24. package/lib/block/processExecutionPayloadEnvelope.js +20 -14
  25. package/lib/block/processExecutionPayloadEnvelope.js.map +1 -1
  26. package/lib/block/processOperations.js.map +1 -1
  27. package/lib/block/processPayloadAttestation.d.ts +1 -1
  28. package/lib/block/processPayloadAttestation.js.map +1 -1
  29. package/lib/block/processProposerSlashing.js.map +1 -1
  30. package/lib/block/processRandao.js.map +1 -1
  31. package/lib/block/processSyncCommittee.js.map +1 -1
  32. package/lib/block/processVoluntaryExit.js +2 -1
  33. package/lib/block/processVoluntaryExit.js.map +1 -1
  34. package/lib/block/processWithdrawalRequest.js.map +1 -1
  35. package/lib/block/processWithdrawals.js.map +1 -1
  36. package/lib/block/slashValidator.js.map +1 -1
  37. package/lib/block/types.js +2 -1
  38. package/lib/block/types.js.map +1 -1
  39. package/lib/cache/effectiveBalanceIncrements.js.map +1 -1
  40. package/lib/cache/epochCache.d.ts +2 -0
  41. package/lib/cache/epochCache.d.ts.map +1 -1
  42. package/lib/cache/epochCache.js +21 -6
  43. package/lib/cache/epochCache.js.map +1 -1
  44. package/lib/cache/epochTransitionCache.js.map +1 -1
  45. package/lib/cache/pubkeyCache.js.map +1 -1
  46. package/lib/cache/rewardCache.js.map +1 -1
  47. package/lib/cache/stateCache.js.map +1 -1
  48. package/lib/cache/syncCommitteeCache.js.map +1 -1
  49. package/lib/epoch/computeUnrealizedCheckpoints.js.map +1 -1
  50. package/lib/epoch/getAttestationDeltas.js.map +1 -1
  51. package/lib/epoch/getRewardsAndPenalties.js.map +1 -1
  52. package/lib/epoch/index.d.ts +1 -1
  53. package/lib/epoch/index.js +2 -1
  54. package/lib/epoch/index.js.map +1 -1
  55. package/lib/epoch/processBuilderPendingPayments.d.ts +1 -1
  56. package/lib/epoch/processBuilderPendingPayments.js.map +1 -1
  57. package/lib/epoch/processEffectiveBalanceUpdates.js.map +1 -1
  58. package/lib/epoch/processEth1DataReset.js.map +1 -1
  59. package/lib/epoch/processHistoricalRootsUpdate.js.map +1 -1
  60. package/lib/epoch/processHistoricalSummariesUpdate.js.map +1 -1
  61. package/lib/epoch/processInactivityUpdates.js.map +1 -1
  62. package/lib/epoch/processJustificationAndFinalization.js.map +1 -1
  63. package/lib/epoch/processParticipationFlagUpdates.js.map +1 -1
  64. package/lib/epoch/processParticipationRecordUpdates.js.map +1 -1
  65. package/lib/epoch/processPendingAttestations.js.map +1 -1
  66. package/lib/epoch/processPendingConsolidations.js.map +1 -1
  67. package/lib/epoch/processPendingDeposits.js.map +1 -1
  68. package/lib/epoch/processProposerLookahead.js.map +1 -1
  69. package/lib/epoch/processRandaoMixesReset.js.map +1 -1
  70. package/lib/epoch/processRegistryUpdates.js.map +1 -1
  71. package/lib/epoch/processRewardsAndPenalties.js.map +1 -1
  72. package/lib/epoch/processSlashings.js.map +1 -1
  73. package/lib/epoch/processSlashingsReset.js.map +1 -1
  74. package/lib/epoch/processSyncCommitteeUpdates.js.map +1 -1
  75. package/lib/lightClient/proofs.js.map +1 -1
  76. package/lib/metrics.d.ts.map +1 -1
  77. package/lib/metrics.js.map +1 -1
  78. package/lib/rewards/attestationsRewards.js.map +1 -1
  79. package/lib/rewards/blockRewards.js.map +1 -1
  80. package/lib/rewards/syncCommitteeRewards.js.map +1 -1
  81. package/lib/signatureSets/attesterSlashings.js.map +1 -1
  82. package/lib/signatureSets/blsToExecutionChange.js.map +1 -1
  83. package/lib/signatureSets/executionPayloadBid.js.map +1 -1
  84. package/lib/signatureSets/executionPayloadEnvelope.js.map +1 -1
  85. package/lib/signatureSets/index.js.map +1 -1
  86. package/lib/signatureSets/indexedAttestation.js.map +1 -1
  87. package/lib/signatureSets/indexedPayloadAttestation.js.map +1 -1
  88. package/lib/signatureSets/proposer.js.map +1 -1
  89. package/lib/signatureSets/proposerSlashings.js.map +1 -1
  90. package/lib/signatureSets/randao.js.map +1 -1
  91. package/lib/signatureSets/voluntaryExits.js.map +1 -1
  92. package/lib/slot/index.js.map +1 -1
  93. package/lib/slot/upgradeStateToAltair.js.map +1 -1
  94. package/lib/slot/upgradeStateToBellatrix.js.map +1 -1
  95. package/lib/slot/upgradeStateToCapella.js.map +1 -1
  96. package/lib/slot/upgradeStateToDeneb.js.map +1 -1
  97. package/lib/slot/upgradeStateToElectra.js.map +1 -1
  98. package/lib/slot/upgradeStateToFulu.js.map +1 -1
  99. package/lib/slot/upgradeStateToGloas.js.map +1 -1
  100. package/lib/stateTransition.js +4 -2
  101. package/lib/stateTransition.js.map +1 -1
  102. package/lib/stateView/beaconStateView.d.ts +18 -2
  103. package/lib/stateView/beaconStateView.d.ts.map +1 -1
  104. package/lib/stateView/beaconStateView.js +60 -3
  105. package/lib/stateView/beaconStateView.js.map +1 -1
  106. package/lib/stateView/interface.d.ts +19 -2
  107. package/lib/stateView/interface.d.ts.map +1 -1
  108. package/lib/testUtils/cache.d.ts +2 -0
  109. package/lib/testUtils/cache.d.ts.map +1 -0
  110. package/lib/testUtils/cache.js +7 -0
  111. package/lib/testUtils/cache.js.map +1 -0
  112. package/lib/testUtils/index.d.ts +6 -0
  113. package/lib/testUtils/index.d.ts.map +1 -0
  114. package/lib/testUtils/index.js +6 -0
  115. package/lib/testUtils/index.js.map +1 -0
  116. package/lib/testUtils/infura.d.ts +3 -0
  117. package/lib/testUtils/infura.d.ts.map +1 -0
  118. package/lib/testUtils/infura.js +8 -0
  119. package/lib/testUtils/infura.js.map +1 -0
  120. package/lib/testUtils/interop.d.ts +2 -0
  121. package/lib/testUtils/interop.d.ts.map +1 -0
  122. package/lib/testUtils/interop.js +24 -0
  123. package/lib/testUtils/interop.js.map +1 -0
  124. package/lib/testUtils/params.d.ts +18 -0
  125. package/lib/testUtils/params.d.ts.map +1 -0
  126. package/lib/testUtils/params.js +20 -0
  127. package/lib/testUtils/params.js.map +1 -0
  128. package/lib/testUtils/state.d.ts +20 -0
  129. package/lib/testUtils/state.d.ts.map +1 -0
  130. package/lib/testUtils/state.js +78 -0
  131. package/lib/testUtils/state.js.map +1 -0
  132. package/lib/testUtils/testFileCache.d.ts +17 -0
  133. package/lib/testUtils/testFileCache.d.ts.map +1 -0
  134. package/lib/testUtils/testFileCache.js +96 -0
  135. package/lib/testUtils/testFileCache.js.map +1 -0
  136. package/lib/testUtils/util.d.ts +50 -0
  137. package/lib/testUtils/util.d.ts.map +1 -0
  138. package/lib/testUtils/util.js +329 -0
  139. package/lib/testUtils/util.js.map +1 -0
  140. package/lib/util/aggregator.js.map +1 -1
  141. package/lib/util/altair.js.map +1 -1
  142. package/lib/util/array.js.map +1 -1
  143. package/lib/util/attestation.js.map +1 -1
  144. package/lib/util/attesterStatus.js.map +1 -1
  145. package/lib/util/balance.js.map +1 -1
  146. package/lib/util/blindedBlock.js.map +1 -1
  147. package/lib/util/blockRoot.js.map +1 -1
  148. package/lib/util/capella.js.map +1 -1
  149. package/lib/util/computeAnchorCheckpoint.js.map +1 -1
  150. package/lib/util/deposit.js.map +1 -1
  151. package/lib/util/domain.js.map +1 -1
  152. package/lib/util/electra.js.map +1 -1
  153. package/lib/util/epoch.js.map +1 -1
  154. package/lib/util/epochShuffling.js.map +1 -1
  155. package/lib/util/execution.js.map +1 -1
  156. package/lib/util/finality.js.map +1 -1
  157. package/lib/util/fulu.js.map +1 -1
  158. package/lib/util/genesis.js.map +1 -1
  159. package/lib/util/gloas.js.map +1 -1
  160. package/lib/util/interop.js.map +1 -1
  161. package/lib/util/loadState/findModifiedInactivityScores.js.map +1 -1
  162. package/lib/util/loadState/findModifiedValidators.js.map +1 -1
  163. package/lib/util/loadState/loadState.js.map +1 -1
  164. package/lib/util/loadState/loadValidator.js.map +1 -1
  165. package/lib/util/rootCache.d.ts.map +1 -1
  166. package/lib/util/rootCache.js.map +1 -1
  167. package/lib/util/seed.d.ts +0 -8
  168. package/lib/util/seed.d.ts.map +1 -1
  169. package/lib/util/seed.js +0 -64
  170. package/lib/util/seed.js.map +1 -1
  171. package/lib/util/shuffling.js +2 -1
  172. package/lib/util/shuffling.js.map +1 -1
  173. package/lib/util/signatureSets.js +2 -1
  174. package/lib/util/signatureSets.js.map +1 -1
  175. package/lib/util/signingRoot.js.map +1 -1
  176. package/lib/util/slot.js.map +1 -1
  177. package/lib/util/sszBytes.js.map +1 -1
  178. package/lib/util/syncCommittee.js.map +1 -1
  179. package/lib/util/targetUnslashedBalance.js.map +1 -1
  180. package/lib/util/validator.js.map +1 -1
  181. package/lib/util/weakSubjectivity.js.map +1 -1
  182. package/package.json +14 -9
  183. package/src/block/index.ts +3 -3
  184. package/src/block/isValidIndexedPayloadAttestation.ts +2 -2
  185. package/src/block/processAttestationsAltair.ts +1 -1
  186. package/src/block/processExecutionPayloadBid.ts +4 -4
  187. package/src/block/processExecutionPayloadEnvelope.ts +35 -21
  188. package/src/block/processOperations.ts +1 -1
  189. package/src/block/processPayloadAttestation.ts +2 -2
  190. package/src/block/processWithdrawals.ts +1 -1
  191. package/src/cache/epochCache.ts +27 -5
  192. package/src/epoch/index.ts +1 -1
  193. package/src/epoch/processBuilderPendingPayments.ts +2 -2
  194. package/src/stateView/beaconStateView.ts +72 -7
  195. package/src/stateView/interface.ts +19 -2
  196. package/src/testUtils/cache.ts +8 -0
  197. package/src/testUtils/index.ts +5 -0
  198. package/src/testUtils/infura.ts +10 -0
  199. package/src/testUtils/interop.ts +29 -0
  200. package/src/testUtils/params.ts +23 -0
  201. package/src/testUtils/state.ts +110 -0
  202. package/src/testUtils/testFileCache.ts +127 -0
  203. package/src/testUtils/util.ts +429 -0
  204. package/src/util/seed.ts +0 -71
@@ -0,0 +1,23 @@
1
+ // DON'T MODIFY OR FORMAT THIS FILE
2
+ // USE FOR GA CACHE
3
+
4
+ export const phase0State = {
5
+ network: "mainnet" as const,
6
+ epoch: 58758, // Pre-altair fork
7
+ };
8
+
9
+ export const altairState = {
10
+ network: "mainnet" as const,
11
+ epoch: 81889, // Post altair fork
12
+ };
13
+
14
+ export const capellaState = {
15
+ network: "mainnet" as const,
16
+ epoch: 217614, // Post capella fork
17
+ };
18
+
19
+ export const rangeSyncTest = {
20
+ network: "mainnet" as const,
21
+ startSlot: 3766816, // Post altair, first slot in epoch 117713
22
+ endSlot: 3766847, // 3766816 + 31, all blocks in same epoch
23
+ };
@@ -0,0 +1,110 @@
1
+ import {ChainForkConfig, createBeaconConfig} from "@lodestar/config";
2
+ import {config, config as minimalConfig} from "@lodestar/config/default";
3
+ import {
4
+ EPOCHS_PER_HISTORICAL_VECTOR,
5
+ EPOCHS_PER_SLASHINGS_VECTOR,
6
+ GENESIS_EPOCH,
7
+ GENESIS_SLOT,
8
+ SLOTS_PER_HISTORICAL_ROOT,
9
+ } from "@lodestar/params";
10
+ import {phase0, ssz} from "@lodestar/types";
11
+ import {EpochCacheOpts} from "../cache/epochCache.js";
12
+ import {BeaconStateCache} from "../cache/stateCache.js";
13
+ import {ZERO_HASH} from "../constants/index.js";
14
+ import {
15
+ BeaconStateAllForks,
16
+ BeaconStatePhase0,
17
+ CachedBeaconStateAllForks,
18
+ createCachedBeaconState,
19
+ createPubkeyCache,
20
+ } from "../index.js";
21
+ import {newZeroedArray} from "../util/index.js";
22
+
23
+ /**
24
+ * Copy of BeaconState, but all fields are marked optional to allow for swapping out variables as needed.
25
+ */
26
+ type TestBeaconState = Partial<phase0.BeaconState>;
27
+
28
+ /**
29
+ * Generate beaconState, by default it will use the initial state defined when the `ChainStart` log is emitted.
30
+ * NOTE: All fields can be overridden through `opts`.
31
+ * @param {TestBeaconState} opts
32
+ * @returns {BeaconState}
33
+ */
34
+ export function generateState(opts?: TestBeaconState): BeaconStatePhase0 {
35
+ return ssz.phase0.BeaconState.toViewDU({
36
+ genesisTime: Math.floor(Date.now() / 1000),
37
+ genesisValidatorsRoot: ZERO_HASH,
38
+ slot: GENESIS_SLOT,
39
+ fork: {
40
+ previousVersion: config.GENESIS_FORK_VERSION,
41
+ currentVersion: config.GENESIS_FORK_VERSION,
42
+ epoch: GENESIS_EPOCH,
43
+ },
44
+ latestBlockHeader: {
45
+ slot: 0,
46
+ proposerIndex: 0,
47
+ parentRoot: Buffer.alloc(32),
48
+ stateRoot: Buffer.alloc(32),
49
+ bodyRoot: ssz.phase0.BeaconBlockBody.hashTreeRoot(ssz.phase0.BeaconBlockBody.defaultValue()),
50
+ },
51
+ blockRoots: Array.from({length: SLOTS_PER_HISTORICAL_ROOT}, () => ZERO_HASH),
52
+ stateRoots: Array.from({length: SLOTS_PER_HISTORICAL_ROOT}, () => ZERO_HASH),
53
+ historicalRoots: [],
54
+ eth1Data: {
55
+ depositRoot: Buffer.alloc(32),
56
+ blockHash: Buffer.alloc(32),
57
+ depositCount: 0,
58
+ },
59
+ eth1DataVotes: [],
60
+ eth1DepositIndex: 0,
61
+ validators: [],
62
+ balances: [],
63
+ randaoMixes: Array.from({length: EPOCHS_PER_HISTORICAL_VECTOR}, () => ZERO_HASH),
64
+ slashings: newZeroedArray(EPOCHS_PER_SLASHINGS_VECTOR),
65
+ previousEpochAttestations: [],
66
+ currentEpochAttestations: [],
67
+ justificationBits: ssz.phase0.JustificationBits.defaultValue(),
68
+ previousJustifiedCheckpoint: {
69
+ epoch: GENESIS_EPOCH,
70
+ root: ZERO_HASH,
71
+ },
72
+ currentJustifiedCheckpoint: {
73
+ epoch: GENESIS_EPOCH,
74
+ root: ZERO_HASH,
75
+ },
76
+ finalizedCheckpoint: {
77
+ epoch: GENESIS_EPOCH,
78
+ root: ZERO_HASH,
79
+ },
80
+ ...opts,
81
+ });
82
+ }
83
+
84
+ export function generateCachedState(
85
+ config: ChainForkConfig = minimalConfig,
86
+ opts: TestBeaconState = {}
87
+ ): CachedBeaconStateAllForks {
88
+ const state = generateState(opts);
89
+ return createCachedBeaconState(state, {
90
+ config: createBeaconConfig(config, state.genesisValidatorsRoot),
91
+ // This is a test state, there's no need to have a global shared cache of keys
92
+ pubkeyCache: createPubkeyCache(),
93
+ });
94
+ }
95
+
96
+ export function createCachedBeaconStateTest<T extends BeaconStateAllForks>(
97
+ state: T,
98
+ configCustom: ChainForkConfig = config,
99
+ opts?: EpochCacheOpts
100
+ ): T & BeaconStateCache {
101
+ return createCachedBeaconState<T>(
102
+ state,
103
+ {
104
+ config: createBeaconConfig(configCustom, state.genesisValidatorsRoot),
105
+ // This is a test state, there's no need to have a global shared cache of keys
106
+ pubkeyCache: createPubkeyCache(),
107
+ },
108
+ opts
109
+ );
110
+ }
@@ -0,0 +1,127 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import {getClient} from "@lodestar/api";
4
+ import {ChainForkConfig, createChainForkConfig} from "@lodestar/config";
5
+ import {NetworkName, networksChainConfig} from "@lodestar/config/networks";
6
+ import {SignedBeaconBlock} from "@lodestar/types";
7
+ import {fetch} from "@lodestar/utils";
8
+ import {CachedBeaconStateAllForks} from "../index.js";
9
+ import {testCachePath} from "./cache.js";
10
+ import {getInfuraBeaconUrl} from "./infura.js";
11
+ import {createCachedBeaconStateTest} from "./state.js";
12
+
13
+ /**
14
+ * Full link example:
15
+ * ```
16
+ * https://github.com/dapplion/ethereum-consensus-test-data/releases/download/v0.1.0/block_mainnet_3766821.ssz
17
+ * ``` */
18
+ const TEST_FILES_BASE_URL = "https://github.com/dapplion/ethereum-consensus-test-data/releases/download/v0.1.0";
19
+
20
+ /**
21
+ * Create a network config from known network params
22
+ */
23
+ export function getNetworkConfig(network: NetworkName): ChainForkConfig {
24
+ const configNetwork = networksChainConfig[network];
25
+ return createChainForkConfig(configNetwork);
26
+ }
27
+
28
+ /**
29
+ * Download a state from Infura. Caches states in local fs by network and slot to only download once.
30
+ */
31
+ export async function getNetworkCachedState(
32
+ network: NetworkName,
33
+ slot: number,
34
+ timeout?: number
35
+ ): Promise<CachedBeaconStateAllForks> {
36
+ const config = getNetworkConfig(network);
37
+ const fileId = `state_${network}_${slot}.ssz`;
38
+
39
+ const filepath = path.join(testCachePath, fileId);
40
+
41
+ if (fs.existsSync(filepath)) {
42
+ const stateSsz = fs.readFileSync(filepath);
43
+ return createCachedBeaconStateTest(config.getForkTypes(slot).BeaconState.deserializeToViewDU(stateSsz), config);
44
+ }
45
+
46
+ const stateSsz = await tryEach([
47
+ () => downloadTestFile(fileId),
48
+ () => {
49
+ const client = getClient(
50
+ {baseUrl: getInfuraBeaconUrl(network), globalInit: {timeoutMs: timeout ?? 300_000}},
51
+ {config}
52
+ );
53
+ return client.debug.getStateV2({stateId: slot}).then((r) => {
54
+ return r.ssz();
55
+ });
56
+ },
57
+ ]);
58
+
59
+ fs.writeFileSync(filepath, stateSsz);
60
+ return createCachedBeaconStateTest(config.getForkTypes(slot).BeaconState.deserializeToViewDU(stateSsz), config);
61
+ }
62
+
63
+ /**
64
+ * Download a state from Infura. Caches states in local fs by network and slot to only download once.
65
+ */
66
+ export async function getNetworkCachedBlock(
67
+ network: NetworkName,
68
+ slot: number,
69
+ timeout?: number
70
+ ): Promise<SignedBeaconBlock> {
71
+ const config = getNetworkConfig(network);
72
+ const fileId = `block_${network}_${slot}.ssz`;
73
+
74
+ const filepath = path.join(testCachePath, fileId);
75
+
76
+ if (fs.existsSync(filepath)) {
77
+ const blockSsz = fs.readFileSync(filepath);
78
+ return config.getForkTypes(slot).SignedBeaconBlock.deserialize(blockSsz);
79
+ }
80
+
81
+ const blockSsz = await tryEach([
82
+ () => downloadTestFile(fileId),
83
+ async () => {
84
+ const client = getClient(
85
+ {baseUrl: getInfuraBeaconUrl(network), globalInit: {timeoutMs: timeout ?? 300_000}},
86
+ {config}
87
+ );
88
+
89
+ return (await client.beacon.getBlockV2({blockId: slot})).ssz();
90
+ },
91
+ ]);
92
+
93
+ fs.writeFileSync(filepath, blockSsz);
94
+ return config.getForkTypes(slot).SignedBeaconBlock.deserialize(blockSsz);
95
+ }
96
+
97
+ async function downloadTestFile(fileId: string): Promise<Uint8Array> {
98
+ const fileUrl = `${TEST_FILES_BASE_URL}/${fileId}`;
99
+ // biome-ignore lint/suspicious/noConsole: We explicity need to log to console
100
+ console.log(`Downloading file ${fileUrl}`);
101
+
102
+ try {
103
+ const res = await fetch(fileUrl);
104
+ if (!res.ok) {
105
+ throw new Error(`Error downloading ${fileUrl}: ${res.status} ${res.statusText}`);
106
+ }
107
+ return new Uint8Array(await res.arrayBuffer());
108
+ } catch (e) {
109
+ const error = e as Error;
110
+ error.message = `Error downloading ${fileUrl}: ${error.message}`;
111
+ throw error;
112
+ }
113
+ }
114
+
115
+ async function tryEach<T>(promises: (() => Promise<T>)[]): Promise<T> {
116
+ const errors: Error[] = [];
117
+
118
+ for (let i = 0; i < promises.length; i++) {
119
+ try {
120
+ return await promises[i]();
121
+ } catch (e) {
122
+ errors.push(e as Error);
123
+ }
124
+ }
125
+
126
+ throw Error(errors.map((e, i) => `Error[${i}] ${e.message}`).join("\n"));
127
+ }
@@ -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
+ }