@lodestar/state-transition 1.50.0-dev.9c3f7bebe9 → 1.50.0-dev.9c7f3c203f

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 (72) hide show
  1. package/lib/block/indexedBuilderState.d.ts +19 -0
  2. package/lib/block/indexedBuilderState.d.ts.map +1 -0
  3. package/lib/block/indexedBuilderState.js +60 -0
  4. package/lib/block/indexedBuilderState.js.map +1 -0
  5. package/lib/block/processBuilderDepositRequest.d.ts +2 -2
  6. package/lib/block/processBuilderDepositRequest.d.ts.map +1 -1
  7. package/lib/block/processBuilderDepositRequest.js +5 -4
  8. package/lib/block/processBuilderDepositRequest.js.map +1 -1
  9. package/lib/block/processBuilderExitRequest.d.ts +2 -2
  10. package/lib/block/processBuilderExitRequest.d.ts.map +1 -1
  11. package/lib/block/processBuilderExitRequest.js +4 -3
  12. package/lib/block/processBuilderExitRequest.js.map +1 -1
  13. package/lib/block/processParentExecutionPayload.d.ts.map +1 -1
  14. package/lib/block/processParentExecutionPayload.js +23 -18
  15. package/lib/block/processParentExecutionPayload.js.map +1 -1
  16. package/lib/index.d.ts +3 -1
  17. package/lib/index.d.ts.map +1 -1
  18. package/lib/index.js +2 -0
  19. package/lib/index.js.map +1 -1
  20. package/lib/rewards/attestationsRewards.d.ts.map +1 -1
  21. package/lib/rewards/attestationsRewards.js +8 -1
  22. package/lib/rewards/attestationsRewards.js.map +1 -1
  23. package/lib/signatureSets/index.d.ts.map +1 -1
  24. package/lib/signatureSets/index.js +2 -2
  25. package/lib/signatureSets/index.js.map +1 -1
  26. package/lib/signatureSets/voluntaryExits.d.ts +2 -6
  27. package/lib/signatureSets/voluntaryExits.d.ts.map +1 -1
  28. package/lib/signatureSets/voluntaryExits.js +2 -32
  29. package/lib/signatureSets/voluntaryExits.js.map +1 -1
  30. package/lib/stateView/beaconStateView.d.ts +22 -21
  31. package/lib/stateView/beaconStateView.d.ts.map +1 -1
  32. package/lib/stateView/beaconStateView.js +71 -71
  33. package/lib/stateView/beaconStateView.js.map +1 -1
  34. package/lib/stateView/interface.d.ts +34 -21
  35. package/lib/stateView/interface.d.ts.map +1 -1
  36. package/lib/stateView/interface.js.map +1 -1
  37. package/lib/stateView/nativeBeaconStateView.d.ts +92 -100
  38. package/lib/stateView/nativeBeaconStateView.d.ts.map +1 -1
  39. package/lib/stateView/nativeBeaconStateView.js +339 -368
  40. package/lib/stateView/nativeBeaconStateView.js.map +1 -1
  41. package/lib/stateView/stateViewFactory.d.ts +10 -17
  42. package/lib/stateView/stateViewFactory.d.ts.map +1 -1
  43. package/lib/stateView/stateViewFactory.js +17 -13
  44. package/lib/stateView/stateViewFactory.js.map +1 -1
  45. package/lib/util/gloas.d.ts +11 -14
  46. package/lib/util/gloas.d.ts.map +1 -1
  47. package/lib/util/gloas.js +2 -42
  48. package/lib/util/gloas.js.map +1 -1
  49. package/lib/util/index.d.ts +1 -1
  50. package/lib/util/index.d.ts.map +1 -1
  51. package/lib/util/index.js +1 -1
  52. package/lib/util/index.js.map +1 -1
  53. package/lib/util/sszBytes.d.ts +3 -0
  54. package/lib/util/sszBytes.d.ts.map +1 -1
  55. package/lib/util/sszBytes.js +40 -14
  56. package/lib/util/sszBytes.js.map +1 -1
  57. package/package.json +8 -8
  58. package/src/block/indexedBuilderState.ts +65 -0
  59. package/src/block/processBuilderDepositRequest.ts +6 -17
  60. package/src/block/processBuilderExitRequest.ts +5 -9
  61. package/src/block/processParentExecutionPayload.ts +27 -23
  62. package/src/index.ts +4 -1
  63. package/src/rewards/attestationsRewards.ts +8 -1
  64. package/src/signatureSets/index.ts +4 -2
  65. package/src/signatureSets/voluntaryExits.ts +3 -55
  66. package/src/stateView/beaconStateView.ts +75 -76
  67. package/src/stateView/interface.ts +61 -21
  68. package/src/stateView/nativeBeaconStateView.ts +354 -375
  69. package/src/stateView/stateViewFactory.ts +29 -27
  70. package/src/util/gloas.ts +2 -53
  71. package/src/util/index.ts +6 -1
  72. package/src/util/sszBytes.ts +52 -16
@@ -0,0 +1,65 @@
1
+ import {BuilderIndex, Epoch, PubkeyHex} from "@lodestar/types";
2
+ import {Builder} from "@lodestar/types/gloas";
3
+ import {toPubkeyHex} from "@lodestar/utils";
4
+ import {CachedBeaconStateGloas} from "../types.js";
5
+ import {computeEpochAtSlot} from "../util/epoch.js";
6
+ import {createBuilderView} from "../util/gloas.js";
7
+
8
+ /**
9
+ * Block-scoped pubkey -> index map and ascending reuse candidates for `state.builders`, built in one pass.
10
+ * Only valid while builder pubkeys change exclusively via `addBuilderToRegistry()` and no builder becomes
11
+ * newly reusable (candidates are re-checked lazily, never added). Do not use across blocks/slots.
12
+ */
13
+ export class IndexedBuilderState {
14
+ private readonly currentEpoch: Epoch;
15
+ private readonly indexByPubkey = new Map<PubkeyHex, BuilderIndex>();
16
+ private readonly reusableIndexCandidates: BuilderIndex[] = [];
17
+ private candidateCursor = 0;
18
+
19
+ constructor(readonly state: CachedBeaconStateGloas) {
20
+ state.builders.commit();
21
+ const builders = state.builders.getAllReadonlyValues();
22
+ this.currentEpoch = computeEpochAtSlot(state.slot);
23
+ for (const [index, builder] of builders.entries()) {
24
+ const pubkeyHex = toPubkeyHex(builder.pubkey);
25
+ this.indexByPubkey.set(pubkeyHex, index);
26
+ if (canReuseBuilder(builder, this.currentEpoch)) {
27
+ this.reusableIndexCandidates.push(index);
28
+ }
29
+ }
30
+ }
31
+
32
+ findBuilderIndexByPubkey(pubkey: Uint8Array): BuilderIndex | null {
33
+ return this.indexByPubkey.get(toPubkeyHex(pubkey)) ?? null;
34
+ }
35
+
36
+ addBuilderToRegistry(pubkey: Uint8Array, version: number, executionAddress: Uint8Array, amount: number): void {
37
+ const builder = createBuilderView(pubkey, version, executionAddress, amount, this.currentEpoch);
38
+ const reusableIndex = this.findReusableBuilderIndex();
39
+ const index = reusableIndex ?? this.state.builders.length;
40
+ if (reusableIndex !== null) {
41
+ const oldBuilder = this.state.builders.getReadonly(index);
42
+ this.indexByPubkey.delete(toPubkeyHex(oldBuilder.pubkey));
43
+ this.state.builders.set(index, builder);
44
+ } else {
45
+ this.state.builders.push(builder);
46
+ }
47
+ this.indexByPubkey.set(toPubkeyHex(pubkey), index);
48
+ }
49
+
50
+ private findReusableBuilderIndex(): BuilderIndex | null {
51
+ while (this.candidateCursor < this.reusableIndexCandidates.length) {
52
+ const index = this.reusableIndexCandidates[this.candidateCursor++];
53
+ const builder = this.state.builders.getReadonly(index);
54
+ // Top-ups can invalidate initial candidates
55
+ if (canReuseBuilder(builder, this.currentEpoch)) {
56
+ return index;
57
+ }
58
+ }
59
+ return null;
60
+ }
61
+ }
62
+
63
+ function canReuseBuilder(builder: Builder, currentEpoch: Epoch): boolean {
64
+ return builder.withdrawableEpoch <= currentEpoch && builder.balance === 0;
65
+ }
@@ -1,13 +1,8 @@
1
1
  import {FAR_FUTURE_EPOCH, PAYLOAD_BUILDER_VERSION} from "@lodestar/params";
2
2
  import {gloas} from "@lodestar/types";
3
- import {CachedBeaconStateGloas} from "../types.js";
4
3
  import {computeEpochAtSlot} from "../util/epoch.js";
5
- import {
6
- addBuilderToRegistry,
7
- findBuilderIndexByPubkey,
8
- isBuilderWithdrawalCredential,
9
- isValidBuilderDepositSignature,
10
- } from "../util/gloas.js";
4
+ import {isBuilderWithdrawalCredential, isValidBuilderDepositSignature} from "../util/gloas.js";
5
+ import {IndexedBuilderState} from "./indexedBuilderState.js";
11
6
 
12
7
  /**
13
8
  * Process a builder deposit request from the execution layer: register a new builder
@@ -16,9 +11,10 @@ import {
16
11
  * Spec: https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.11/specs/gloas/beacon-chain.md#new-process_builder_deposit_request
17
12
  */
18
13
  export function processBuilderDepositRequest(
19
- state: CachedBeaconStateGloas,
14
+ indexedState: IndexedBuilderState,
20
15
  request: gloas.BuilderDepositRequest
21
16
  ): void {
17
+ const {state} = indexedState;
22
18
  const {pubkey, withdrawalCredentials, amount, signature} = request;
23
19
 
24
20
  // Ignore deposits with unexpected withdrawal credential prefixes.
@@ -26,18 +22,11 @@ export function processBuilderDepositRequest(
26
22
  return;
27
23
  }
28
24
 
29
- const builderIndex = findBuilderIndexByPubkey(state, pubkey);
25
+ const builderIndex = indexedState.findBuilderIndexByPubkey(pubkey);
30
26
 
31
27
  if (builderIndex === null) {
32
28
  if (isValidBuilderDepositSignature(state.config, pubkey, withdrawalCredentials, amount, signature)) {
33
- addBuilderToRegistry(
34
- state,
35
- pubkey,
36
- PAYLOAD_BUILDER_VERSION,
37
- withdrawalCredentials.subarray(12),
38
- amount,
39
- state.slot
40
- );
29
+ indexedState.addBuilderToRegistry(pubkey, PAYLOAD_BUILDER_VERSION, withdrawalCredentials.subarray(12), amount);
41
30
  }
42
31
  return;
43
32
  }
@@ -1,12 +1,7 @@
1
1
  import {gloas} from "@lodestar/types";
2
2
  import {byteArrayEquals} from "@lodestar/utils";
3
- import {CachedBeaconStateGloas} from "../types.js";
4
- import {
5
- findBuilderIndexByPubkey,
6
- getPendingBalanceToWithdrawForBuilder,
7
- initiateBuilderExit,
8
- isActiveBuilder,
9
- } from "../util/gloas.js";
3
+ import {getPendingBalanceToWithdrawForBuilder, initiateBuilderExit, isActiveBuilder} from "../util/gloas.js";
4
+ import {IndexedBuilderState} from "./indexedBuilderState.js";
10
5
 
11
6
  /**
12
7
  * Apply a builder exit request. Authorizes the exit via `source_address` (the builder's
@@ -17,8 +12,9 @@ import {
17
12
  *
18
13
  * Spec: https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.11/specs/gloas/beacon-chain.md#new-process_builder_exit_request
19
14
  */
20
- export function processBuilderExitRequest(state: CachedBeaconStateGloas, request: gloas.BuilderExitRequest): void {
21
- const builderIndex = findBuilderIndexByPubkey(state, request.pubkey);
15
+ export function processBuilderExitRequest(indexedState: IndexedBuilderState, request: gloas.BuilderExitRequest): void {
16
+ const {state} = indexedState;
17
+ const builderIndex = indexedState.findBuilderIndexByPubkey(request.pubkey);
22
18
  if (builderIndex === null) {
23
19
  return;
24
20
  }
@@ -3,6 +3,7 @@ import {BeaconBlock, gloas, ssz} from "@lodestar/types";
3
3
  import {byteArrayEquals, toRootHex} from "@lodestar/utils";
4
4
  import {CachedBeaconStateGloas} from "../types.js";
5
5
  import {computeEpochAtSlot} from "../util/epoch.js";
6
+ import {IndexedBuilderState} from "./indexedBuilderState.js";
6
7
  import {processBuilderDepositRequest} from "./processBuilderDepositRequest.js";
7
8
  import {processBuilderExitRequest} from "./processBuilderExitRequest.js";
8
9
  import {processConsolidationRequest} from "./processConsolidationRequest.js";
@@ -53,29 +54,8 @@ export function applyParentExecutionPayload(state: CachedBeaconStateGloas, reque
53
54
  const parentEpoch = computeEpochAtSlot(parentSlot);
54
55
  const currentEpoch = computeEpochAtSlot(state.slot);
55
56
 
56
- // Process execution requests from parent's payload. The execution
57
- // requests are processed at state.slot (child's slot), not the parent's slot.
58
- for (const deposit of requests.deposits) {
59
- processDepositRequest(fork, state, deposit);
60
- }
61
-
62
- for (const withdrawal of requests.withdrawals) {
63
- processWithdrawalRequest(fork, state, withdrawal);
64
- }
65
-
66
- for (const consolidation of requests.consolidations) {
67
- processConsolidationRequest(state, consolidation);
68
- }
69
-
70
- for (const builderDeposit of requests.builderDeposits) {
71
- processBuilderDepositRequest(state, builderDeposit);
72
- }
73
-
74
- for (const builderExit of requests.builderExits) {
75
- processBuilderExitRequest(state, builderExit);
76
- }
77
-
78
- // Settle the builder payment
57
+ // Settle the builder payment before the requests so that a builder exit request
58
+ // is rejected while the payment is pending
79
59
  if (parentEpoch === currentEpoch) {
80
60
  settleBuilderPayment(state, SLOTS_PER_EPOCH + (parentSlot % SLOTS_PER_EPOCH));
81
61
  } else if (parentEpoch === currentEpoch - 1) {
@@ -92,6 +72,30 @@ export function applyParentExecutionPayload(state: CachedBeaconStateGloas, reque
92
72
  );
93
73
  }
94
74
 
75
+ // Process execution requests from parent's payload. The execution
76
+ // requests are processed at state.slot (child's slot), not the parent's slot.
77
+ for (const deposit of requests.deposits) {
78
+ processDepositRequest(fork, state, deposit);
79
+ }
80
+
81
+ for (const withdrawal of requests.withdrawals) {
82
+ processWithdrawalRequest(fork, state, withdrawal);
83
+ }
84
+
85
+ for (const consolidation of requests.consolidations) {
86
+ processConsolidationRequest(state, consolidation);
87
+ }
88
+
89
+ if (requests.builderDeposits.length > 0 || requests.builderExits.length > 0) {
90
+ const indexedState = new IndexedBuilderState(state);
91
+ for (const builderDeposit of requests.builderDeposits) {
92
+ processBuilderDepositRequest(indexedState, builderDeposit);
93
+ }
94
+ for (const builderExit of requests.builderExits) {
95
+ processBuilderExitRequest(indexedState, builderExit);
96
+ }
97
+ }
98
+
95
99
  // Update parent payload availability and latest block hash
96
100
  state.executionPayloadAvailability.set(parentSlot % SLOTS_PER_HISTORICAL_ROOT, true);
97
101
  state.latestBlockHash = parentBid.blockHash;
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export {type BlockExternalData, DataAvailabilityStatus, ExecutionPayloadStatus} from "./block/externalData.js";
2
+ export {IndexedBuilderState} from "./block/indexedBuilderState.js";
2
3
  export {getAttestationParticipationStatus, processAttestationsAltair} from "./block/processAttestationsAltair.js";
3
4
  export {assertValidAttesterSlashing} from "./block/processAttesterSlashing.js";
4
5
  export {isValidBlsToExecutionChange} from "./block/processBlsToExecutionChange.js";
@@ -43,7 +44,7 @@ export * from "./signatureSets/index.js";
43
44
  export * from "./stateTransition.js";
44
45
  export {BeaconStateView} from "./stateView/beaconStateView.js";
45
46
  export {
46
- type ComputeNewStateRootInput,
47
+ type BlockSTFInput,
47
48
  type ComputeNewStateRootResult,
48
49
  type IBeaconStateView,
49
50
  type IBeaconStateViewAltair,
@@ -54,6 +55,7 @@ export {
54
55
  type IBeaconStateViewFulu,
55
56
  type IBeaconStateViewGloas,
56
57
  type IBeaconStateViewHeze,
58
+ type IBeaconStateViewNative,
57
59
  isStatePostAltair,
58
60
  isStatePostBellatrix,
59
61
  isStatePostCapella,
@@ -63,6 +65,7 @@ export {
63
65
  isStatePostGloas,
64
66
  isStatePostHeze,
65
67
  } from "./stateView/interface.js";
68
+ export {NativeBeaconStateView} from "./stateView/nativeBeaconStateView.js";
66
69
  export {createBeaconStateView, createBeaconStateViewForHistoricalRegen} from "./stateView/stateViewFactory.js";
67
70
  export type {
68
71
  BeaconStateAllForks,
@@ -18,6 +18,8 @@ import {
18
18
  import {ValidatorIndex, rewards} from "@lodestar/types";
19
19
  import {fromHex} from "@lodestar/utils";
20
20
  import {EpochTransitionCache, beforeProcessEpoch} from "../cache/epochTransitionCache.js";
21
+ import {processInactivityUpdates} from "../epoch/processInactivityUpdates.js";
22
+ import {processJustificationAndFinalization} from "../epoch/processJustificationAndFinalization.js";
21
23
  import {CachedBeaconStateAllForks, CachedBeaconStateAltair} from "../types.js";
22
24
  import {
23
25
  FLAG_ELIGIBLE_ATTESTER,
@@ -45,8 +47,13 @@ export async function computeAttestationsRewards(
45
47
  throw Error("Unsupported fork. Attestations rewards calculation is not available in phase0");
46
48
  }
47
49
 
48
- const stateAltair = state as CachedBeaconStateAltair;
50
+ // Rewards are computed after justification and inactivity updates in epoch processing, so apply both to a clone
51
+ // before reading finality and inactivity scores, see
52
+ // https://github.com/ethereum/consensus-specs/blob/v1.7.0-beta.2/specs/altair/beacon-chain.md#epoch-processing
53
+ const stateAltair = state.clone() as CachedBeaconStateAltair;
49
54
  const transitionCache = beforeProcessEpoch(stateAltair);
55
+ processJustificationAndFinalization(stateAltair, transitionCache);
56
+ processInactivityUpdates(stateAltair, transitionCache);
50
57
 
51
58
  const [idealRewards, penalties] = computeIdealAttestationsRewardsAndPenaltiesAltair(
52
59
  config,
@@ -12,7 +12,7 @@ import {getPayloadAttestationDataSigningRoot} from "./indexedPayloadAttestation.
12
12
  import {getBlockProposerSignatureSet} from "./proposer.js";
13
13
  import {getProposerSlashingsSignatureSets} from "./proposerSlashings.js";
14
14
  import {getRandaoRevealSignatureSet} from "./randao.js";
15
- import {getVoluntaryExitsSignatureSets} from "./voluntaryExits.js";
15
+ import {getVoluntaryExitSignatureSet} from "./voluntaryExits.js";
16
16
 
17
17
  export * from "./attesterSlashings.js";
18
18
  export * from "./blsToExecutionChange.js";
@@ -49,7 +49,9 @@ export function getBlockSignatureSets(
49
49
  ...getProposerSlashingsSignatureSets(config, signedBlock),
50
50
  ...getAttesterSlashingsSignatureSets(config, signedBlock),
51
51
  ...getAttestationsSignatureSets(config, signedBlock, indexedAttestations),
52
- ...getVoluntaryExitsSignatureSets(config, state, signedBlock),
52
+ ...signedBlock.message.body.voluntaryExits.map((voluntaryExit) =>
53
+ getVoluntaryExitSignatureSet(config, state, voluntaryExit)
54
+ ),
53
55
  ];
54
56
 
55
57
  if (!opts?.skipProposerSignature) {
@@ -1,14 +1,11 @@
1
1
  import {BeaconConfig} from "@lodestar/config";
2
- import {ForkSeq} from "@lodestar/params";
3
- import {SignedBeaconBlock, Slot, phase0, ssz} from "@lodestar/types";
4
- import {IBeaconStateView, IBeaconStateViewGloas, isStatePostGloas} from "../stateView/interface.js";
2
+ import {phase0, ssz} from "@lodestar/types";
3
+ import {IBeaconStateView} from "../stateView/interface.js";
5
4
  import {
6
5
  ISignatureSet,
7
6
  SignatureSetType,
8
7
  computeSigningRoot,
9
8
  computeStartSlotAtEpoch,
10
- convertValidatorIndexToBuilderIndex,
11
- isBuilderIndex,
12
9
  verifySignatureSet,
13
10
  } from "../util/index.js";
14
11
 
@@ -27,36 +24,9 @@ export function getVoluntaryExitSignatureSet(
27
24
  config: BeaconConfig,
28
25
  state: IBeaconStateView,
29
26
  signedVoluntaryExit: phase0.SignedVoluntaryExit
30
- ): ISignatureSet {
31
- const fork = config.getForkSeq(state.slot);
32
-
33
- if (fork >= ForkSeq.gloas && isBuilderVoluntaryExit(signedVoluntaryExit)) {
34
- if (!isStatePostGloas(state)) {
35
- throw new Error(`Expected gloas+ state for builder voluntary exit signature, got fork=${state.forkName}`);
36
- }
37
- return getBuilderVoluntaryExitSignatureSet(config, state, signedVoluntaryExit);
38
- }
39
-
40
- return getValidatorVoluntaryExitSignatureSet(config, state.slot, signedVoluntaryExit);
41
- }
42
-
43
- export function getVoluntaryExitsSignatureSets(
44
- config: BeaconConfig,
45
- state: IBeaconStateView,
46
- signedBlock: SignedBeaconBlock
47
- ): ISignatureSet[] {
48
- return signedBlock.message.body.voluntaryExits.map((voluntaryExit) =>
49
- getVoluntaryExitSignatureSet(config, state, voluntaryExit)
50
- );
51
- }
52
-
53
- export function getValidatorVoluntaryExitSignatureSet(
54
- config: BeaconConfig,
55
- stateSlot: Slot,
56
- signedVoluntaryExit: phase0.SignedVoluntaryExit
57
27
  ): ISignatureSet {
58
28
  const messageSlot = computeStartSlotAtEpoch(signedVoluntaryExit.message.epoch);
59
- const domain = config.getDomainForVoluntaryExit(stateSlot, messageSlot);
29
+ const domain = config.getDomainForVoluntaryExit(state.slot, messageSlot);
60
30
 
61
31
  return {
62
32
  type: SignatureSetType.indexed,
@@ -65,25 +35,3 @@ export function getValidatorVoluntaryExitSignatureSet(
65
35
  signature: signedVoluntaryExit.signature,
66
36
  };
67
37
  }
68
-
69
- export function getBuilderVoluntaryExitSignatureSet(
70
- config: BeaconConfig,
71
- state: IBeaconStateViewGloas,
72
- signedVoluntaryExit: phase0.SignedVoluntaryExit
73
- ): ISignatureSet {
74
- const messageSlot = computeStartSlotAtEpoch(signedVoluntaryExit.message.epoch);
75
- const domain = config.getDomainForVoluntaryExit(state.slot, messageSlot);
76
- const builderIndex = convertValidatorIndexToBuilderIndex(signedVoluntaryExit.message.validatorIndex);
77
- const builder = state.getBuilder(builderIndex);
78
-
79
- return {
80
- type: SignatureSetType.single,
81
- pubkey: builder.pubkey,
82
- signingRoot: computeSigningRoot(ssz.phase0.VoluntaryExit, signedVoluntaryExit.message, domain),
83
- signature: signedVoluntaryExit.signature,
84
- };
85
- }
86
-
87
- export function isBuilderVoluntaryExit(signedVoluntaryExit: phase0.SignedVoluntaryExit): boolean {
88
- return isBuilderIndex(signedVoluntaryExit.message.validatorIndex);
89
- }