@lodestar/state-transition 1.41.0 → 1.42.0-dev.2fd27cd2f6

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 (59) hide show
  1. package/lib/block/isValidIndexedAttestation.d.ts.map +1 -1
  2. package/lib/block/isValidIndexedAttestation.js +2 -3
  3. package/lib/block/isValidIndexedAttestation.js.map +1 -1
  4. package/lib/block/processAttestationsAltair.d.ts +2 -1
  5. package/lib/block/processAttestationsAltair.d.ts.map +1 -1
  6. package/lib/block/processAttestationsAltair.js +5 -3
  7. package/lib/block/processAttestationsAltair.js.map +1 -1
  8. package/lib/block/processExecutionPayloadEnvelope.d.ts +3 -1
  9. package/lib/block/processExecutionPayloadEnvelope.d.ts.map +1 -1
  10. package/lib/block/processExecutionPayloadEnvelope.js +11 -28
  11. package/lib/block/processExecutionPayloadEnvelope.js.map +1 -1
  12. package/lib/block/processVoluntaryExit.d.ts.map +1 -1
  13. package/lib/block/processVoluntaryExit.js +49 -47
  14. package/lib/block/processVoluntaryExit.js.map +1 -1
  15. package/lib/signatureSets/executionPayloadEnvelope.d.ts +5 -1
  16. package/lib/signatureSets/executionPayloadEnvelope.d.ts.map +1 -1
  17. package/lib/signatureSets/executionPayloadEnvelope.js +10 -1
  18. package/lib/signatureSets/executionPayloadEnvelope.js.map +1 -1
  19. package/lib/signatureSets/index.d.ts +2 -1
  20. package/lib/signatureSets/index.d.ts.map +1 -1
  21. package/lib/signatureSets/index.js +2 -2
  22. package/lib/signatureSets/index.js.map +1 -1
  23. package/lib/signatureSets/voluntaryExits.d.ts +7 -3
  24. package/lib/signatureSets/voluntaryExits.d.ts.map +1 -1
  25. package/lib/signatureSets/voluntaryExits.js +30 -9
  26. package/lib/signatureSets/voluntaryExits.js.map +1 -1
  27. package/lib/slot/upgradeStateToAltair.d.ts.map +1 -1
  28. package/lib/slot/upgradeStateToAltair.js +2 -1
  29. package/lib/slot/upgradeStateToAltair.js.map +1 -1
  30. package/lib/stateView/beaconStateView.d.ts +11 -7
  31. package/lib/stateView/beaconStateView.d.ts.map +1 -1
  32. package/lib/stateView/beaconStateView.js +13 -10
  33. package/lib/stateView/beaconStateView.js.map +1 -1
  34. package/lib/stateView/interface.d.ts +14 -9
  35. package/lib/stateView/interface.d.ts.map +1 -1
  36. package/lib/util/execution.js +1 -1
  37. package/lib/util/execution.js.map +1 -1
  38. package/lib/util/rootCache.d.ts +2 -2
  39. package/lib/util/rootCache.d.ts.map +1 -1
  40. package/lib/util/rootCache.js +2 -3
  41. package/lib/util/rootCache.js.map +1 -1
  42. package/lib/util/shuffling.d.ts +2 -1
  43. package/lib/util/shuffling.d.ts.map +1 -1
  44. package/lib/util/shuffling.js +2 -2
  45. package/lib/util/shuffling.js.map +1 -1
  46. package/package.json +7 -7
  47. package/src/block/isValidIndexedAttestation.ts +2 -3
  48. package/src/block/processAttestationsAltair.ts +7 -4
  49. package/src/block/processExecutionPayloadEnvelope.ts +18 -35
  50. package/src/block/processVoluntaryExit.ts +77 -54
  51. package/src/signatureSets/executionPayloadEnvelope.ts +26 -2
  52. package/src/signatureSets/index.ts +3 -1
  53. package/src/signatureSets/voluntaryExits.ts +51 -9
  54. package/src/slot/upgradeStateToAltair.ts +2 -1
  55. package/src/stateView/beaconStateView.ts +31 -13
  56. package/src/stateView/interface.ts +15 -6
  57. package/src/util/execution.ts +1 -1
  58. package/src/util/rootCache.ts +4 -5
  59. package/src/util/shuffling.ts +5 -4
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "bugs": {
12
12
  "url": "https://github.com/ChainSafe/lodestar/issues"
13
13
  },
14
- "version": "1.41.0",
14
+ "version": "1.42.0-dev.2fd27cd2f6",
15
15
  "type": "module",
16
16
  "exports": {
17
17
  ".": {
@@ -67,14 +67,14 @@
67
67
  "@chainsafe/pubkey-index-map": "^3.0.0",
68
68
  "@chainsafe/ssz": "^1.2.2",
69
69
  "@chainsafe/swap-or-not-shuffle": "^1.2.1",
70
- "@lodestar/config": "^1.41.0",
71
- "@lodestar/params": "^1.41.0",
72
- "@lodestar/types": "^1.41.0",
73
- "@lodestar/utils": "^1.41.0",
70
+ "@lodestar/config": "^1.42.0-dev.2fd27cd2f6",
71
+ "@lodestar/params": "^1.42.0-dev.2fd27cd2f6",
72
+ "@lodestar/types": "^1.42.0-dev.2fd27cd2f6",
73
+ "@lodestar/utils": "^1.42.0-dev.2fd27cd2f6",
74
74
  "@vekexasia/bigint-buffer2": "^1.1.1"
75
75
  },
76
76
  "devDependencies": {
77
- "@lodestar/api": "^1.41.0"
77
+ "@lodestar/api": "^1.42.0-dev.2fd27cd2f6"
78
78
  },
79
79
  "keywords": [
80
80
  "ethereum",
@@ -82,5 +82,5 @@
82
82
  "beacon",
83
83
  "blockchain"
84
84
  ],
85
- "gitHead": "13e882a3a430d6df25a408b61b24954277e1f8e8"
85
+ "gitHead": "b6290748008d38486e41d582d1f43b647f57e515"
86
86
  }
@@ -74,9 +74,8 @@ export function isValidIndexedAttestationIndices(
74
74
  prev = index;
75
75
  }
76
76
 
77
- // check if indices are out of bounds, by checking the highest index (since it is sorted)
78
- const lastIndex = indices.at(-1);
79
- if (lastIndex && lastIndex >= validatorsLen) {
77
+ // check if indices are out of bounds, `prev` is the highest index since indices are sorted
78
+ if (prev >= validatorsLen) {
80
79
  return false;
81
80
  }
82
81
 
@@ -1,3 +1,4 @@
1
+ import {BitArray} from "@chainsafe/ssz";
1
2
  import {
2
3
  EFFECTIVE_BALANCE_INCREMENT,
3
4
  ForkSeq,
@@ -17,6 +18,7 @@ import {Attestation, Epoch, phase0} from "@lodestar/types";
17
18
  import {byteArrayEquals, intSqrt} from "@lodestar/utils";
18
19
  import {BeaconStateTransitionMetrics} from "../metrics.js";
19
20
  import {getAttestationWithIndicesSignatureSet} from "../signatureSets/indexedAttestation.js";
21
+ import {BeaconStateView} from "../stateView/beaconStateView.js";
20
22
  import {CachedBeaconStateAltair, CachedBeaconStateGloas} from "../types.js";
21
23
  import {isAttestationSameSlot, isAttestationSameSlotRootCache} from "../util/gloas.js";
22
24
  import {increaseBalance, verifySignatureSet} from "../util/index.js";
@@ -41,7 +43,7 @@ export function processAttestationsAltair(
41
43
  const {epochCtx} = state;
42
44
  const {effectiveBalanceIncrements} = epochCtx;
43
45
  const stateSlot = state.slot;
44
- const rootCache = new RootCache(state);
46
+ const rootCache = new RootCache(new BeaconStateView(state));
45
47
  const currentEpoch = epochCtx.epoch;
46
48
 
47
49
  // Process all attestations first and then increase the balance of the proposer once
@@ -80,7 +82,7 @@ export function processAttestationsAltair(
80
82
  stateSlot - data.slot,
81
83
  epochCtx.epoch,
82
84
  rootCache,
83
- fork >= ForkSeq.gloas ? (state as CachedBeaconStateGloas).executionPayloadAvailability.toBoolArray() : null
85
+ fork >= ForkSeq.gloas ? (state as CachedBeaconStateGloas).executionPayloadAvailability : null
84
86
  );
85
87
 
86
88
  // For each participant, update their participation
@@ -177,7 +179,7 @@ export function getAttestationParticipationStatus(
177
179
  inclusionDelay: number,
178
180
  currentEpoch: Epoch,
179
181
  rootCache: RootCache,
180
- executionPayloadAvailability: boolean[] | null
182
+ executionPayloadAvailability: BitArray | null
181
183
  ): number {
182
184
  const justifiedCheckpoint =
183
185
  data.target.epoch === currentEpoch ? rootCache.currentJustifiedCheckpoint : rootCache.previousJustifiedCheckpoint;
@@ -221,7 +223,8 @@ export function getAttestationParticipationStatus(
221
223
  throw new Error(`data index must be 0 or 1 index=${data.index}`);
222
224
  }
223
225
 
224
- isMatchingPayload = Boolean(data.index) === executionPayloadAvailability[data.slot % SLOTS_PER_HISTORICAL_ROOT];
226
+ isMatchingPayload =
227
+ Boolean(data.index) === executionPayloadAvailability.get(data.slot % SLOTS_PER_HISTORICAL_ROOT);
225
228
  }
226
229
 
227
230
  isMatchingHead = isMatchingHead && isMatchingPayload;
@@ -1,19 +1,18 @@
1
- import {PublicKey, Signature, verify} from "@chainsafe/blst";
2
- import {
3
- BUILDER_INDEX_SELF_BUILD,
4
- DOMAIN_BEACON_BUILDER,
5
- SLOTS_PER_EPOCH,
6
- SLOTS_PER_HISTORICAL_ROOT,
7
- } from "@lodestar/params";
1
+ import {SLOTS_PER_EPOCH, SLOTS_PER_HISTORICAL_ROOT} from "@lodestar/params";
8
2
  import {gloas, ssz} from "@lodestar/types";
9
3
  import {byteArrayEquals, toHex, toRootHex} from "@lodestar/utils";
4
+ import {getExecutionPayloadEnvelopeSignatureSet} from "../signatureSets/executionPayloadEnvelope.js";
5
+ import {BeaconStateView} from "../stateView/beaconStateView.js";
10
6
  import {CachedBeaconStateGloas} from "../types.js";
11
- import {computeSigningRoot, computeTimeAtSlot} from "../util/index.js";
7
+ import {computeTimeAtSlot} from "../util/index.js";
8
+ import {verifySignatureSet} from "../util/signatureSets.js";
12
9
  import {processConsolidationRequest} from "./processConsolidationRequest.js";
13
10
  import {processDepositRequest} from "./processDepositRequest.js";
14
11
  import {processWithdrawalRequest} from "./processWithdrawalRequest.js";
15
12
 
16
13
  export type ProcessExecutionPayloadEnvelopeOpts = {
14
+ verifySignature?: boolean;
15
+ verifyStateRoot?: boolean;
17
16
  dontTransferCache?: boolean;
18
17
  };
19
18
 
@@ -23,14 +22,14 @@ export type ProcessExecutionPayloadEnvelopeOpts = {
23
22
  export function processExecutionPayloadEnvelope(
24
23
  state: CachedBeaconStateGloas,
25
24
  signedEnvelope: gloas.SignedExecutionPayloadEnvelope,
26
- verify: boolean,
27
25
  opts?: ProcessExecutionPayloadEnvelopeOpts
28
26
  ): CachedBeaconStateGloas {
27
+ const {verifySignature = true, verifyStateRoot = true} = opts ?? {};
29
28
  const envelope = signedEnvelope.message;
30
29
  const payload = envelope.payload;
31
30
  const fork = state.config.getForkSeq(envelope.slot);
32
31
 
33
- if (verify && !verifyExecutionPayloadEnvelopeSignature(state, signedEnvelope)) {
32
+ if (verifySignature && !verifyExecutionPayloadEnvelopeSignature(state, signedEnvelope)) {
34
33
  throw Error(`Execution payload envelope has invalid signature builderIndex=${envelope.builderIndex}`);
35
34
  }
36
35
 
@@ -70,7 +69,7 @@ export function processExecutionPayloadEnvelope(
70
69
 
71
70
  postState.commit();
72
71
 
73
- if (verify && !byteArrayEquals(envelope.stateRoot, postState.hashTreeRoot())) {
72
+ if (verifyStateRoot && !byteArrayEquals(envelope.stateRoot, postState.hashTreeRoot())) {
74
73
  throw new Error(
75
74
  `Envelope's state root does not match state envelope=${toRootHex(envelope.stateRoot)} state=${toRootHex(postState.hashTreeRoot())}`
76
75
  );
@@ -160,28 +159,12 @@ function verifyExecutionPayloadEnvelopeSignature(
160
159
  state: CachedBeaconStateGloas,
161
160
  signedEnvelope: gloas.SignedExecutionPayloadEnvelope
162
161
  ): boolean {
163
- const builderIndex = signedEnvelope.message.builderIndex;
164
-
165
- const domain = state.config.getDomain(state.slot, DOMAIN_BEACON_BUILDER);
166
- const signingRoot = computeSigningRoot(ssz.gloas.ExecutionPayloadEnvelope, signedEnvelope.message, domain);
167
-
168
- try {
169
- let publicKey: PublicKey;
170
-
171
- if (builderIndex === BUILDER_INDEX_SELF_BUILD) {
172
- const validatorIndex = state.latestBlockHeader.proposerIndex;
173
- const proposerPubkey = state.epochCtx.pubkeyCache.get(validatorIndex);
174
- if (!proposerPubkey) {
175
- return false;
176
- }
177
- publicKey = proposerPubkey;
178
- } else {
179
- publicKey = PublicKey.fromBytes(state.builders.getReadonly(builderIndex).pubkey);
180
- }
181
- const signature = Signature.fromBytes(signedEnvelope.signature, true);
182
-
183
- return verify(signingRoot, publicKey, signature);
184
- } catch (_e) {
185
- return false; // Catch all BLS errors: failed key validation, failed signature validation, invalid signature
186
- }
162
+ const signatureSet = getExecutionPayloadEnvelopeSignatureSet(
163
+ state.config,
164
+ state.epochCtx.pubkeyCache,
165
+ new BeaconStateView(state),
166
+ signedEnvelope,
167
+ state.latestBlockHeader.proposerIndex
168
+ );
169
+ return verifySignatureSet(signatureSet);
187
170
  }
@@ -1,7 +1,7 @@
1
- import {PublicKey, Signature, verify} from "@chainsafe/blst";
2
1
  import {FAR_FUTURE_EPOCH, ForkSeq} from "@lodestar/params";
3
- import {phase0, ssz} from "@lodestar/types";
2
+ import {phase0} from "@lodestar/types";
4
3
  import {verifyVoluntaryExitSignature} from "../signatureSets/index.js";
4
+ import {BeaconStateView} from "../stateView/beaconStateView.js";
5
5
  import {CachedBeaconStateAllForks, CachedBeaconStateElectra, CachedBeaconStateGloas} from "../types.js";
6
6
  import {
7
7
  convertValidatorIndexToBuilderIndex,
@@ -10,7 +10,7 @@ import {
10
10
  isActiveBuilder,
11
11
  isBuilderIndex,
12
12
  } from "../util/gloas.js";
13
- import {computeSigningRoot, getCurrentEpoch, getPendingBalanceToWithdraw, isActiveValidator} from "../util/index.js";
13
+ import {getPendingBalanceToWithdraw, isActiveValidator} from "../util/index.js";
14
14
  import {initiateValidatorExit} from "./index.js";
15
15
 
16
16
  export enum VoluntaryExitValidity {
@@ -35,57 +35,20 @@ export function processVoluntaryExit(
35
35
  verifySignature = true
36
36
  ): void {
37
37
  const voluntaryExit = signedVoluntaryExit.message;
38
- const currentEpoch = getCurrentEpoch(state);
39
38
 
40
- // Exits must specify an epoch when they become valid; they are not valid before then
41
- if (currentEpoch < voluntaryExit.epoch) {
42
- throw Error(`Voluntary exit epoch ${voluntaryExit.epoch} is after current epoch ${currentEpoch}`);
39
+ const validity = getVoluntaryExitValidity(fork, state, signedVoluntaryExit, verifySignature);
40
+ if (validity !== VoluntaryExitValidity.valid) {
41
+ throw Error(`Invalid voluntary exit at forkSeq=${fork} reason=${validity}`);
43
42
  }
44
43
 
45
- // Check if this is a builder exit
46
44
  if (fork >= ForkSeq.gloas && isBuilderIndex(voluntaryExit.validatorIndex)) {
47
- const stateGloas = state as CachedBeaconStateGloas;
48
- const builderIndex = convertValidatorIndexToBuilderIndex(voluntaryExit.validatorIndex);
49
- const builder = stateGloas.builders.getReadonly(builderIndex);
50
-
51
- // Verify the builder is active
52
- if (!isActiveBuilder(builder, state.finalizedCheckpoint.epoch)) {
53
- throw Error(`Builder ${builderIndex} is not active`);
54
- }
55
-
56
- // Only exit builder if it has no pending withdrawals in the queue
57
- if (getPendingBalanceToWithdrawForBuilder(stateGloas, builderIndex) !== 0) {
58
- throw Error(`Builder ${builderIndex} has pending withdrawals`);
59
- }
60
-
61
- // Verify signature
62
- if (verifySignature) {
63
- const domain = state.config.getDomainForVoluntaryExit(state.slot);
64
- const signingRoot = computeSigningRoot(ssz.phase0.VoluntaryExit, voluntaryExit, domain);
65
-
66
- try {
67
- const publicKey = PublicKey.fromBytes(builder.pubkey);
68
- const signature = Signature.fromBytes(signedVoluntaryExit.signature, true);
69
-
70
- if (!verify(signingRoot, publicKey, signature)) {
71
- throw Error("BLS verify failed");
72
- }
73
- } catch (e) {
74
- throw Error(`Builder ${builderIndex} invalid exit signature reason=${(e as Error).message}`);
75
- }
76
- }
77
-
78
- // Initiate builder exit
79
- initiateBuilderExit(stateGloas, builderIndex);
45
+ initiateBuilderExit(
46
+ state as CachedBeaconStateGloas,
47
+ convertValidatorIndexToBuilderIndex(voluntaryExit.validatorIndex)
48
+ );
80
49
  return;
81
50
  }
82
51
 
83
- // Handle validator exit
84
- const validity = getVoluntaryExitValidity(fork, state, signedVoluntaryExit, verifySignature);
85
- if (validity !== VoluntaryExitValidity.valid) {
86
- throw Error(`Invalid voluntary exit at forkSeq=${fork} reason=${validity}`);
87
- }
88
-
89
52
  const validator = state.validators.get(signedVoluntaryExit.message.validatorIndex);
90
53
  initiateValidatorExit(fork, state, validator);
91
54
  }
@@ -95,10 +58,74 @@ export function getVoluntaryExitValidity(
95
58
  state: CachedBeaconStateAllForks,
96
59
  signedVoluntaryExit: phase0.SignedVoluntaryExit,
97
60
  verifySignature = true
61
+ ): VoluntaryExitValidity {
62
+ const currentEpoch = state.epochCtx.epoch;
63
+ const voluntaryExit = signedVoluntaryExit.message;
64
+
65
+ // Exits must specify an epoch when they become valid; they are not valid before then
66
+ if (currentEpoch < voluntaryExit.epoch) {
67
+ return VoluntaryExitValidity.earlyEpoch;
68
+ }
69
+
70
+ // Check if this is a builder exit
71
+ if (fork >= ForkSeq.gloas && isBuilderIndex(voluntaryExit.validatorIndex)) {
72
+ return getBuilderVoluntaryExitValidity(state as CachedBeaconStateGloas, signedVoluntaryExit, verifySignature);
73
+ }
74
+
75
+ return getValidatorVoluntaryExitValidity(fork, state, signedVoluntaryExit, verifySignature);
76
+ }
77
+
78
+ function getBuilderVoluntaryExitValidity(
79
+ state: CachedBeaconStateGloas,
80
+ signedVoluntaryExit: phase0.SignedVoluntaryExit,
81
+ verifySignature: boolean
82
+ ): VoluntaryExitValidity {
83
+ const {config, epochCtx} = state;
84
+ const builderIndex = convertValidatorIndexToBuilderIndex(signedVoluntaryExit.message.validatorIndex);
85
+
86
+ if (builderIndex >= state.builders.length) {
87
+ return VoluntaryExitValidity.inactive;
88
+ }
89
+
90
+ const builder = state.builders.getReadonly(builderIndex);
91
+
92
+ // Verify the builder is active
93
+ if (!isActiveBuilder(builder, state.finalizedCheckpoint.epoch)) {
94
+ return builder.withdrawableEpoch !== FAR_FUTURE_EPOCH
95
+ ? VoluntaryExitValidity.alreadyExited
96
+ : VoluntaryExitValidity.inactive;
97
+ }
98
+
99
+ // Only exit builder if it has no pending withdrawals in the queue
100
+ if (getPendingBalanceToWithdrawForBuilder(state, builderIndex) !== 0) {
101
+ return VoluntaryExitValidity.pendingWithdrawals;
102
+ }
103
+
104
+ // Verify signature
105
+ if (
106
+ verifySignature &&
107
+ !verifyVoluntaryExitSignature(config, epochCtx.pubkeyCache, new BeaconStateView(state), signedVoluntaryExit)
108
+ ) {
109
+ return VoluntaryExitValidity.invalidSignature;
110
+ }
111
+
112
+ return VoluntaryExitValidity.valid;
113
+ }
114
+
115
+ function getValidatorVoluntaryExitValidity(
116
+ fork: ForkSeq,
117
+ state: CachedBeaconStateAllForks,
118
+ signedVoluntaryExit: phase0.SignedVoluntaryExit,
119
+ verifySignature: boolean
98
120
  ): VoluntaryExitValidity {
99
121
  const {config, epochCtx} = state;
100
122
  const voluntaryExit = signedVoluntaryExit.message;
101
- const validator = state.validators.get(voluntaryExit.validatorIndex);
123
+
124
+ if (voluntaryExit.validatorIndex >= state.validators.length) {
125
+ return VoluntaryExitValidity.inactive;
126
+ }
127
+
128
+ const validator = state.validators.getReadonly(voluntaryExit.validatorIndex);
102
129
  const currentEpoch = epochCtx.epoch;
103
130
 
104
131
  // verify the validator is active
@@ -111,11 +138,6 @@ export function getVoluntaryExitValidity(
111
138
  return VoluntaryExitValidity.alreadyExited;
112
139
  }
113
140
 
114
- // exits must specify an epoch when they become valid; they are not valid before then
115
- if (currentEpoch < voluntaryExit.epoch) {
116
- return VoluntaryExitValidity.earlyEpoch;
117
- }
118
-
119
141
  // verify the validator had been active long enough
120
142
  if (currentEpoch < validator.activationEpoch + config.SHARD_COMMITTEE_PERIOD) {
121
143
  return VoluntaryExitValidity.shortTimeActive;
@@ -129,9 +151,10 @@ export function getVoluntaryExitValidity(
129
151
  return VoluntaryExitValidity.pendingWithdrawals;
130
152
  }
131
153
 
154
+ // Verify signature
132
155
  if (
133
156
  verifySignature &&
134
- !verifyVoluntaryExitSignature(state.config, epochCtx.pubkeyCache, state.slot, signedVoluntaryExit)
157
+ !verifyVoluntaryExitSignature(config, epochCtx.pubkeyCache, new BeaconStateView(state), signedVoluntaryExit)
135
158
  ) {
136
159
  return VoluntaryExitValidity.invalidSignature;
137
160
  }
@@ -1,7 +1,11 @@
1
+ import {PublicKey} from "@chainsafe/blst";
1
2
  import {BeaconConfig} from "@lodestar/config";
2
- import {DOMAIN_BEACON_BUILDER} from "@lodestar/params";
3
- import {gloas, ssz} from "@lodestar/types";
3
+ import {BUILDER_INDEX_SELF_BUILD, DOMAIN_BEACON_BUILDER} from "@lodestar/params";
4
+ import {ValidatorIndex, gloas, ssz} from "@lodestar/types";
5
+ import {PubkeyCache} from "../cache/pubkeyCache.js";
6
+ import {IBeaconStateView} from "../stateView/interface.js";
4
7
  import {computeSigningRoot} from "../util/index.js";
8
+ import {type SingleSignatureSet, createSingleSignatureSetFromComponents} from "../util/signatureSets.js";
5
9
 
6
10
  export function getExecutionPayloadEnvelopeSigningRoot(
7
11
  config: BeaconConfig,
@@ -11,3 +15,23 @@ export function getExecutionPayloadEnvelopeSigningRoot(
11
15
 
12
16
  return computeSigningRoot(ssz.gloas.ExecutionPayloadEnvelope, envelope, domain);
13
17
  }
18
+
19
+ export function getExecutionPayloadEnvelopeSignatureSet(
20
+ config: BeaconConfig,
21
+ pubkeyCache: PubkeyCache,
22
+ state: IBeaconStateView,
23
+ signedEnvelope: gloas.SignedExecutionPayloadEnvelope,
24
+ proposerIndex: ValidatorIndex
25
+ ): SingleSignatureSet {
26
+ const envelope = signedEnvelope.message;
27
+ const pubkey =
28
+ envelope.builderIndex === BUILDER_INDEX_SELF_BUILD
29
+ ? pubkeyCache.getOrThrow(proposerIndex)
30
+ : PublicKey.fromBytes(state.getBuilder(envelope.builderIndex).pubkey);
31
+
32
+ return createSingleSignatureSetFromComponents(
33
+ pubkey,
34
+ getExecutionPayloadEnvelopeSigningRoot(config, envelope),
35
+ signedEnvelope.signature
36
+ );
37
+ }
@@ -3,6 +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 {IBeaconStateView} from "../stateView/interface.js";
6
7
  import {ISignatureSet} from "../util/index.js";
7
8
  import {getAttesterSlashingsSignatureSets} from "./attesterSlashings.js";
8
9
  import {getBlsToExecutionChangeSignatureSets} from "./blsToExecutionChange.js";
@@ -30,6 +31,7 @@ export * from "./voluntaryExits.js";
30
31
  export function getBlockSignatureSets(
31
32
  config: BeaconConfig,
32
33
  currentSyncCommitteeIndexed: SyncCommitteeCache,
34
+ state: IBeaconStateView,
33
35
  signedBlock: SignedBeaconBlock,
34
36
  indexedAttestations: IndexedAttestation[],
35
37
  opts?: {
@@ -45,7 +47,7 @@ export function getBlockSignatureSets(
45
47
  ...getProposerSlashingsSignatureSets(config, signedBlock),
46
48
  ...getAttesterSlashingsSignatureSets(config, signedBlock),
47
49
  ...getAttestationsSignatureSets(config, signedBlock, indexedAttestations),
48
- ...getVoluntaryExitsSignatureSets(config, signedBlock),
50
+ ...getVoluntaryExitsSignatureSets(config, state, signedBlock),
49
51
  ];
50
52
 
51
53
  if (!opts?.skipProposerSignature) {
@@ -1,27 +1,56 @@
1
+ import {PublicKey} from "@chainsafe/blst";
1
2
  import {BeaconConfig} from "@lodestar/config";
3
+ import {ForkSeq} from "@lodestar/params";
2
4
  import {SignedBeaconBlock, Slot, phase0, ssz} from "@lodestar/types";
3
5
  import {PubkeyCache} from "../cache/pubkeyCache.js";
6
+ import {IBeaconStateView} from "../stateView/interface.js";
4
7
  import {
5
8
  ISignatureSet,
6
9
  SignatureSetType,
7
10
  computeSigningRoot,
8
11
  computeStartSlotAtEpoch,
12
+ convertValidatorIndexToBuilderIndex,
13
+ isBuilderIndex,
9
14
  verifySignatureSet,
10
15
  } from "../util/index.js";
11
16
 
12
17
  export function verifyVoluntaryExitSignature(
13
18
  config: BeaconConfig,
14
19
  pubkeyCache: PubkeyCache,
15
- stateSlot: Slot,
20
+ state: IBeaconStateView,
16
21
  signedVoluntaryExit: phase0.SignedVoluntaryExit
17
22
  ): boolean {
18
- return verifySignatureSet(getVoluntaryExitSignatureSet(config, stateSlot, signedVoluntaryExit), pubkeyCache);
23
+ return verifySignatureSet(getVoluntaryExitSignatureSet(config, state, signedVoluntaryExit), pubkeyCache);
19
24
  }
20
25
 
21
26
  /**
22
27
  * Extract signatures to allow validating all block signatures at once
23
28
  */
24
29
  export function getVoluntaryExitSignatureSet(
30
+ config: BeaconConfig,
31
+ state: IBeaconStateView,
32
+ signedVoluntaryExit: phase0.SignedVoluntaryExit
33
+ ): ISignatureSet {
34
+ const fork = config.getForkSeq(state.slot);
35
+
36
+ if (fork >= ForkSeq.gloas && isBuilderVoluntaryExit(signedVoluntaryExit)) {
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(
25
54
  config: BeaconConfig,
26
55
  stateSlot: Slot,
27
56
  signedVoluntaryExit: phase0.SignedVoluntaryExit
@@ -37,11 +66,24 @@ export function getVoluntaryExitSignatureSet(
37
66
  };
38
67
  }
39
68
 
40
- export function getVoluntaryExitsSignatureSets(config: BeaconConfig, signedBlock: SignedBeaconBlock): ISignatureSet[] {
41
- // the getDomain() api requires the state slot as 1st param, however it's the same to block.slot in state-transition
42
- // and the same epoch when we verify blocks in batch in beacon-node. So we can safely use block.slot here.
43
- const blockSlot = signedBlock.message.slot;
44
- return signedBlock.message.body.voluntaryExits.map((voluntaryExit) =>
45
- getVoluntaryExitSignatureSet(config, blockSlot, voluntaryExit)
46
- );
69
+ export function getBuilderVoluntaryExitSignatureSet(
70
+ config: BeaconConfig,
71
+ state: IBeaconStateView,
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: PublicKey.fromBytes(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);
47
89
  }
@@ -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,
@@ -28,11 +29,12 @@ import {
28
29
  } from "@lodestar/types";
29
30
  import {Checkpoint, Fork} from "@lodestar/types/phase0";
30
31
  import {processExecutionPayloadEnvelope} from "../block/index.js";
32
+ import {ProcessExecutionPayloadEnvelopeOpts} from "../block/processExecutionPayloadEnvelope.js";
31
33
  import {VoluntaryExitValidity, getVoluntaryExitValidity} from "../block/processVoluntaryExit.js";
32
34
  import {getExpectedWithdrawals} from "../block/processWithdrawals.js";
33
35
  import {EffectiveBalanceIncrements} from "../cache/effectiveBalanceIncrements.js";
34
36
  import {EpochTransitionCacheOpts} from "../cache/epochTransitionCache.js";
35
- import {PubkeyCache, createPubkeyCache} from "../cache/pubkeyCache.js";
37
+ import {PubkeyCache} from "../cache/pubkeyCache.js";
36
38
  import {RewardCache} from "../cache/rewardCache.js";
37
39
  import {
38
40
  CachedBeaconStateAllForks,
@@ -91,7 +93,7 @@ export class BeaconStateView implements IBeaconStateView {
91
93
  // fulu
92
94
  private _proposerLookahead: fulu.ProposerLookahead | null = null;
93
95
  // gloas
94
- private _executionPayloadAvailability: boolean[] | null = null;
96
+ private _executionPayloadAvailability: BitArray | null = null;
95
97
  private _latestExecutionPayloadBid: ExecutionPayloadBid | null = null;
96
98
 
97
99
  constructor(readonly cachedState: CachedBeaconStateAllForks) {
@@ -356,15 +358,15 @@ export class BeaconStateView implements IBeaconStateView {
356
358
 
357
359
  // gloas
358
360
 
359
- get executionPayloadAvailability(): boolean[] {
361
+ get executionPayloadAvailability(): BitArray {
360
362
  if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.gloas) {
361
363
  throw new Error("executionPayloadAvailability is not available before GLOAS");
362
364
  }
363
365
 
364
366
  if (this._executionPayloadAvailability === null) {
365
- this._executionPayloadAvailability = (this.cachedState as CachedBeaconStateGloas).executionPayloadAvailability
366
- .toValue()
367
- .toBoolArray();
367
+ this._executionPayloadAvailability = (
368
+ this.cachedState as CachedBeaconStateGloas
369
+ ).executionPayloadAvailability.toValue();
368
370
  }
369
371
 
370
372
  return this._executionPayloadAvailability;
@@ -403,7 +405,7 @@ export class BeaconStateView implements IBeaconStateView {
403
405
  * Return the index of the validator in the PTC committee for the given slot.
404
406
  * return -1 if validator is not in the PTC committee for the given slot.
405
407
  */
406
- validatorPTCCommitteeIndex(validatorIndex: ValidatorIndex, slot: Slot): number {
408
+ getIndexInPayloadTimelinessCommittee(validatorIndex: ValidatorIndex, slot: Slot): number {
407
409
  if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.gloas) {
408
410
  throw new Error("PTC committees are not supported before GLOAS");
409
411
  }
@@ -502,6 +504,10 @@ export class BeaconStateView implements IBeaconStateView {
502
504
  return this.cachedState.epochCtx.syncProposerReward;
503
505
  }
504
506
 
507
+ getIndexedSyncCommittee(slot: Slot): SyncCommitteeCache {
508
+ return this.cachedState.epochCtx.getIndexedSyncCommittee(slot);
509
+ }
510
+
505
511
  getIndexedSyncCommitteeAtEpoch(epoch: Epoch): SyncCommitteeCache {
506
512
  return this.cachedState.epochCtx.getIndexedSyncCommitteeAtEpoch(epoch);
507
513
  }
@@ -712,6 +718,10 @@ export class BeaconStateView implements IBeaconStateView {
712
718
  return new BeaconStateView(cachedState);
713
719
  }
714
720
 
721
+ toValue(): BeaconState {
722
+ return this.cachedState.toValue();
723
+ }
724
+
715
725
  serialize(): Uint8Array {
716
726
  return this.cachedState.serialize();
717
727
  }
@@ -761,25 +771,33 @@ export class BeaconStateView implements IBeaconStateView {
761
771
  return new BeaconStateView(newState);
762
772
  }
763
773
 
764
- processExecutionPayloadEnvelope(signedEnvelope: gloas.SignedExecutionPayloadEnvelope, verify: boolean): void {
774
+ processExecutionPayloadEnvelope(
775
+ signedEnvelope: gloas.SignedExecutionPayloadEnvelope,
776
+ opts?: ProcessExecutionPayloadEnvelopeOpts
777
+ ): BeaconStateView {
765
778
  const fork = this.config.getForkName(this.cachedState.slot);
766
779
  if (!isForkPostGloas(fork)) {
767
780
  throw Error(`processExecutionPayloadEnvelope is only available for gloas+ forks, got fork=${fork}`);
768
781
  }
769
- processExecutionPayloadEnvelope(this.cachedState as CachedBeaconStateGloas, signedEnvelope, verify);
782
+ const postPayloadState = processExecutionPayloadEnvelope(
783
+ this.cachedState as CachedBeaconStateGloas,
784
+ signedEnvelope,
785
+ opts
786
+ );
787
+ return new BeaconStateView(postPayloadState);
770
788
  }
771
789
  }
772
790
 
773
791
  /**
774
- * Create BeaconStateView for historical state regen, no need to sync pubkey cache there.
792
+ * Create BeaconStateView for historical state regen, this is called from a worker thread.
775
793
  */
776
794
  export function createBeaconStateViewForHistoricalRegen(
777
795
  config: BeaconConfig,
778
- stateBytes: Uint8Array
796
+ stateBytes: Uint8Array,
797
+ pubkeyCache: PubkeyCache
779
798
  ): IBeaconStateView {
780
799
  const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
781
800
 
782
- const pubkeyCache = createPubkeyCache();
783
801
  syncPubkeyCache(state, pubkeyCache);
784
802
  const cachedState = createCachedBeaconState(
785
803
  state,