@lodestar/state-transition 1.39.1 → 1.40.0-dev.1020f27ca9

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 (218) hide show
  1. package/lib/block/index.d.ts +1 -0
  2. package/lib/block/index.d.ts.map +1 -1
  3. package/lib/block/index.js +1 -0
  4. package/lib/block/index.js.map +1 -1
  5. package/lib/block/isValidIndexedAttestation.d.ts +4 -5
  6. package/lib/block/isValidIndexedAttestation.d.ts.map +1 -1
  7. package/lib/block/isValidIndexedAttestation.js +9 -10
  8. package/lib/block/isValidIndexedAttestation.js.map +1 -1
  9. package/lib/block/isValidIndexedPayloadAttestation.d.ts.map +1 -1
  10. package/lib/block/isValidIndexedPayloadAttestation.js +1 -1
  11. package/lib/block/isValidIndexedPayloadAttestation.js.map +1 -1
  12. package/lib/block/processAttestationPhase0.d.ts.map +1 -1
  13. package/lib/block/processAttestationPhase0.js +1 -1
  14. package/lib/block/processAttestationPhase0.js.map +1 -1
  15. package/lib/block/processAttestationsAltair.d.ts.map +1 -1
  16. package/lib/block/processAttestationsAltair.js +2 -2
  17. package/lib/block/processAttestationsAltair.js.map +1 -1
  18. package/lib/block/processAttesterSlashing.d.ts +3 -2
  19. package/lib/block/processAttesterSlashing.d.ts.map +1 -1
  20. package/lib/block/processAttesterSlashing.js +3 -3
  21. package/lib/block/processAttesterSlashing.js.map +1 -1
  22. package/lib/block/processBlsToExecutionChange.d.ts +3 -1
  23. package/lib/block/processBlsToExecutionChange.d.ts.map +1 -1
  24. package/lib/block/processBlsToExecutionChange.js +7 -11
  25. package/lib/block/processBlsToExecutionChange.js.map +1 -1
  26. package/lib/block/processConsolidationRequest.d.ts +1 -2
  27. package/lib/block/processConsolidationRequest.d.ts.map +1 -1
  28. package/lib/block/processConsolidationRequest.js +2 -2
  29. package/lib/block/processConsolidationRequest.js.map +1 -1
  30. package/lib/block/processDepositRequest.d.ts +8 -2
  31. package/lib/block/processDepositRequest.d.ts.map +1 -1
  32. package/lib/block/processDepositRequest.js +81 -8
  33. package/lib/block/processDepositRequest.js.map +1 -1
  34. package/lib/block/processExecutionPayloadBid.d.ts.map +1 -1
  35. package/lib/block/processExecutionPayloadBid.js +16 -29
  36. package/lib/block/processExecutionPayloadBid.js.map +1 -1
  37. package/lib/block/processExecutionPayloadEnvelope.d.ts.map +1 -1
  38. package/lib/block/processExecutionPayloadEnvelope.js +26 -25
  39. package/lib/block/processExecutionPayloadEnvelope.js.map +1 -1
  40. package/lib/block/processOperations.js +2 -2
  41. package/lib/block/processOperations.js.map +1 -1
  42. package/lib/block/processProposerSlashing.d.ts +5 -2
  43. package/lib/block/processProposerSlashing.d.ts.map +1 -1
  44. package/lib/block/processProposerSlashing.js +8 -6
  45. package/lib/block/processProposerSlashing.js.map +1 -1
  46. package/lib/block/processSyncCommittee.d.ts +1 -2
  47. package/lib/block/processSyncCommittee.d.ts.map +1 -1
  48. package/lib/block/processSyncCommittee.js +5 -5
  49. package/lib/block/processSyncCommittee.js.map +1 -1
  50. package/lib/block/processVoluntaryExit.d.ts +1 -1
  51. package/lib/block/processVoluntaryExit.d.ts.map +1 -1
  52. package/lib/block/processVoluntaryExit.js +45 -3
  53. package/lib/block/processVoluntaryExit.js.map +1 -1
  54. package/lib/block/processWithdrawalRequest.js +1 -1
  55. package/lib/block/processWithdrawalRequest.js.map +1 -1
  56. package/lib/block/processWithdrawals.d.ts +1 -0
  57. package/lib/block/processWithdrawals.d.ts.map +1 -1
  58. package/lib/block/processWithdrawals.js +121 -66
  59. package/lib/block/processWithdrawals.js.map +1 -1
  60. package/lib/cache/epochCache.d.ts +8 -28
  61. package/lib/cache/epochCache.d.ts.map +1 -1
  62. package/lib/cache/epochCache.js +40 -180
  63. package/lib/cache/epochCache.js.map +1 -1
  64. package/lib/cache/epochTransitionCache.d.ts +5 -12
  65. package/lib/cache/epochTransitionCache.d.ts.map +1 -1
  66. package/lib/cache/epochTransitionCache.js +4 -14
  67. package/lib/cache/epochTransitionCache.js.map +1 -1
  68. package/lib/cache/stateCache.d.ts.map +1 -1
  69. package/lib/cache/stateCache.js +1 -2
  70. package/lib/cache/stateCache.js.map +1 -1
  71. package/lib/epoch/processBuilderPendingPayments.d.ts.map +1 -1
  72. package/lib/epoch/processBuilderPendingPayments.js +1 -4
  73. package/lib/epoch/processBuilderPendingPayments.js.map +1 -1
  74. package/lib/epoch/processProposerLookahead.d.ts.map +1 -1
  75. package/lib/epoch/processProposerLookahead.js +3 -6
  76. package/lib/epoch/processProposerLookahead.js.map +1 -1
  77. package/lib/index.d.ts +1 -1
  78. package/lib/index.d.ts.map +1 -1
  79. package/lib/index.js +1 -1
  80. package/lib/index.js.map +1 -1
  81. package/lib/rewards/blockRewards.d.ts +2 -1
  82. package/lib/rewards/blockRewards.d.ts.map +1 -1
  83. package/lib/rewards/blockRewards.js +3 -2
  84. package/lib/rewards/blockRewards.js.map +1 -1
  85. package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -1
  86. package/lib/rewards/syncCommitteeRewards.js +10 -11
  87. package/lib/rewards/syncCommitteeRewards.js.map +1 -1
  88. package/lib/signatureSets/attesterSlashings.d.ts +3 -4
  89. package/lib/signatureSets/attesterSlashings.d.ts.map +1 -1
  90. package/lib/signatureSets/attesterSlashings.js +6 -6
  91. package/lib/signatureSets/attesterSlashings.js.map +1 -1
  92. package/lib/signatureSets/blsToExecutionChange.d.ts +4 -5
  93. package/lib/signatureSets/blsToExecutionChange.d.ts.map +1 -1
  94. package/lib/signatureSets/blsToExecutionChange.js +2 -2
  95. package/lib/signatureSets/blsToExecutionChange.js.map +1 -1
  96. package/lib/signatureSets/executionPayloadBid.d.ts +4 -0
  97. package/lib/signatureSets/executionPayloadBid.d.ts.map +1 -0
  98. package/lib/signatureSets/executionPayloadBid.js +8 -0
  99. package/lib/signatureSets/executionPayloadBid.js.map +1 -0
  100. package/lib/signatureSets/executionPayloadEnvelope.d.ts +4 -0
  101. package/lib/signatureSets/executionPayloadEnvelope.d.ts.map +1 -0
  102. package/lib/signatureSets/executionPayloadEnvelope.js +8 -0
  103. package/lib/signatureSets/executionPayloadEnvelope.js.map +1 -0
  104. package/lib/signatureSets/index.d.ts +3 -2
  105. package/lib/signatureSets/index.d.ts.map +1 -1
  106. package/lib/signatureSets/index.js +10 -8
  107. package/lib/signatureSets/index.js.map +1 -1
  108. package/lib/signatureSets/indexedAttestation.d.ts +3 -4
  109. package/lib/signatureSets/indexedAttestation.d.ts.map +1 -1
  110. package/lib/signatureSets/indexedAttestation.js +6 -6
  111. package/lib/signatureSets/indexedAttestation.js.map +1 -1
  112. package/lib/signatureSets/indexedPayloadAttestation.d.ts +5 -4
  113. package/lib/signatureSets/indexedPayloadAttestation.d.ts.map +1 -1
  114. package/lib/signatureSets/indexedPayloadAttestation.js +3 -3
  115. package/lib/signatureSets/indexedPayloadAttestation.js.map +1 -1
  116. package/lib/signatureSets/proposer.d.ts +3 -3
  117. package/lib/signatureSets/proposer.d.ts.map +1 -1
  118. package/lib/signatureSets/proposer.js +12 -12
  119. package/lib/signatureSets/proposer.js.map +1 -1
  120. package/lib/signatureSets/proposerSlashings.d.ts +2 -3
  121. package/lib/signatureSets/proposerSlashings.d.ts.map +1 -1
  122. package/lib/signatureSets/proposerSlashings.js +6 -6
  123. package/lib/signatureSets/proposerSlashings.js.map +1 -1
  124. package/lib/signatureSets/randao.d.ts +1 -1
  125. package/lib/signatureSets/randao.d.ts.map +1 -1
  126. package/lib/signatureSets/randao.js +4 -4
  127. package/lib/signatureSets/randao.js.map +1 -1
  128. package/lib/signatureSets/voluntaryExits.d.ts +2 -2
  129. package/lib/signatureSets/voluntaryExits.d.ts.map +1 -1
  130. package/lib/signatureSets/voluntaryExits.js +6 -6
  131. package/lib/signatureSets/voluntaryExits.js.map +1 -1
  132. package/lib/types.d.ts +1 -1
  133. package/lib/types.d.ts.map +1 -1
  134. package/lib/util/electra.d.ts.map +1 -1
  135. package/lib/util/electra.js +1 -2
  136. package/lib/util/electra.js.map +1 -1
  137. package/lib/util/epochShuffling.d.ts +1 -34
  138. package/lib/util/epochShuffling.d.ts.map +1 -1
  139. package/lib/util/epochShuffling.js +1 -1
  140. package/lib/util/epochShuffling.js.map +1 -1
  141. package/lib/util/gloas.d.ts +46 -5
  142. package/lib/util/gloas.d.ts.map +1 -1
  143. package/lib/util/gloas.js +91 -5
  144. package/lib/util/gloas.js.map +1 -1
  145. package/lib/util/index.d.ts +2 -2
  146. package/lib/util/index.d.ts.map +1 -1
  147. package/lib/util/index.js +2 -2
  148. package/lib/util/index.js.map +1 -1
  149. package/lib/util/interop.js +1 -1
  150. package/lib/util/interop.js.map +1 -1
  151. package/lib/util/shuffling.d.ts +58 -0
  152. package/lib/util/shuffling.d.ts.map +1 -0
  153. package/lib/util/shuffling.js +175 -0
  154. package/lib/util/shuffling.js.map +1 -0
  155. package/lib/util/signatureSets.d.ts +38 -5
  156. package/lib/util/signatureSets.d.ts.map +1 -1
  157. package/lib/util/signatureSets.js +48 -6
  158. package/lib/util/signatureSets.js.map +1 -1
  159. package/lib/util/validator.d.ts +6 -1
  160. package/lib/util/validator.d.ts.map +1 -1
  161. package/lib/util/validator.js +26 -16
  162. package/lib/util/validator.js.map +1 -1
  163. package/package.json +8 -8
  164. package/src/block/index.ts +1 -0
  165. package/src/block/isValidIndexedAttestation.ts +18 -12
  166. package/src/block/isValidIndexedPayloadAttestation.ts +4 -1
  167. package/src/block/processAttestationPhase0.ts +2 -1
  168. package/src/block/processAttestationsAltair.ts +2 -8
  169. package/src/block/processAttesterSlashing.ts +16 -4
  170. package/src/block/processBlsToExecutionChange.ts +13 -14
  171. package/src/block/processConsolidationRequest.ts +2 -3
  172. package/src/block/processDepositRequest.ts +101 -8
  173. package/src/block/processExecutionPayloadBid.ts +20 -42
  174. package/src/block/processExecutionPayloadEnvelope.ts +34 -30
  175. package/src/block/processOperations.ts +2 -2
  176. package/src/block/processProposerSlashing.ts +22 -12
  177. package/src/block/processSyncCommittee.ts +3 -6
  178. package/src/block/processVoluntaryExit.ts +60 -5
  179. package/src/block/processWithdrawalRequest.ts +1 -1
  180. package/src/block/processWithdrawals.ts +168 -70
  181. package/src/cache/epochCache.ts +52 -214
  182. package/src/cache/epochTransitionCache.ts +9 -34
  183. package/src/cache/stateCache.ts +1 -2
  184. package/src/epoch/processBuilderPendingPayments.ts +1 -5
  185. package/src/epoch/processProposerLookahead.ts +3 -7
  186. package/src/index.ts +0 -2
  187. package/src/rewards/blockRewards.ts +6 -3
  188. package/src/rewards/syncCommitteeRewards.ts +10 -13
  189. package/src/signatureSets/attesterSlashings.ts +3 -7
  190. package/src/signatureSets/blsToExecutionChange.ts +5 -6
  191. package/src/signatureSets/executionPayloadBid.ts +14 -0
  192. package/src/signatureSets/executionPayloadEnvelope.ts +13 -0
  193. package/src/signatureSets/index.ts +8 -9
  194. package/src/signatureSets/indexedAttestation.ts +2 -7
  195. package/src/signatureSets/indexedPayloadAttestation.ts +9 -7
  196. package/src/signatureSets/proposer.ts +8 -12
  197. package/src/signatureSets/proposerSlashings.ts +4 -7
  198. package/src/signatureSets/randao.ts +4 -8
  199. package/src/signatureSets/voluntaryExits.ts +5 -10
  200. package/src/types.ts +1 -0
  201. package/src/util/electra.ts +1 -4
  202. package/src/util/epochShuffling.ts +2 -43
  203. package/src/util/gloas.ts +116 -10
  204. package/src/util/index.ts +2 -2
  205. package/src/util/interop.ts +1 -1
  206. package/src/util/shuffling.ts +234 -0
  207. package/src/util/signatureSets.ts +84 -8
  208. package/src/util/validator.ts +31 -16
  209. package/lib/util/calculateCommitteeAssignments.d.ts +0 -12
  210. package/lib/util/calculateCommitteeAssignments.d.ts.map +0 -1
  211. package/lib/util/calculateCommitteeAssignments.js +0 -26
  212. package/lib/util/calculateCommitteeAssignments.js.map +0 -1
  213. package/lib/util/shufflingDecisionRoot.d.ts +0 -20
  214. package/lib/util/shufflingDecisionRoot.d.ts.map +0 -1
  215. package/lib/util/shufflingDecisionRoot.js +0 -76
  216. package/lib/util/shufflingDecisionRoot.js.map +0 -1
  217. package/src/util/calculateCommitteeAssignments.ts +0 -43
  218. package/src/util/shufflingDecisionRoot.ts +0 -81
@@ -1,10 +1,15 @@
1
1
  import {PublicKey, Signature, verify} from "@chainsafe/blst";
2
2
  import {byteArrayEquals} from "@chainsafe/ssz";
3
- import {DOMAIN_BEACON_BUILDER, SLOTS_PER_EPOCH, SLOTS_PER_HISTORICAL_ROOT} from "@lodestar/params";
3
+ import {
4
+ BUILDER_INDEX_SELF_BUILD,
5
+ DOMAIN_BEACON_BUILDER,
6
+ SLOTS_PER_EPOCH,
7
+ SLOTS_PER_HISTORICAL_ROOT,
8
+ } from "@lodestar/params";
4
9
  import {gloas, ssz} from "@lodestar/types";
5
10
  import {toHex, toRootHex} from "@lodestar/utils";
6
11
  import {CachedBeaconStateGloas} from "../types.ts";
7
- import {computeExitEpochAndUpdateChurn, computeSigningRoot, computeTimeAtSlot} from "../util/index.ts";
12
+ import {computeSigningRoot, computeTimeAtSlot} from "../util/index.ts";
8
13
  import {processConsolidationRequest} from "./processConsolidationRequest.ts";
9
14
  import {processDepositRequest} from "./processDepositRequest.ts";
10
15
  import {processWithdrawalRequest} from "./processWithdrawalRequest.ts";
@@ -19,13 +24,8 @@ export function processExecutionPayloadEnvelope(
19
24
  const payload = envelope.payload;
20
25
  const fork = state.config.getForkSeq(envelope.slot);
21
26
 
22
- if (verify) {
23
- const builderIndex = envelope.builderIndex;
24
- const pubkey = state.validators.getReadonly(builderIndex).pubkey;
25
-
26
- if (!verifyExecutionPayloadEnvelopeSignature(state, pubkey, signedEnvelope)) {
27
- throw new Error("Payload Envelope has invalid signature");
28
- }
27
+ if (verify && !verifyExecutionPayloadEnvelopeSignature(state, signedEnvelope)) {
28
+ throw Error(`Execution payload envelope has invalid signature builderIndex=${envelope.builderIndex}`);
29
29
  }
30
30
 
31
31
  validateExecutionPayloadEnvelope(state, envelope);
@@ -33,7 +33,7 @@ export function processExecutionPayloadEnvelope(
33
33
  const requests = envelope.executionRequests;
34
34
 
35
35
  for (const deposit of requests.deposits) {
36
- processDepositRequest(state, deposit);
36
+ processDepositRequest(fork, state, deposit);
37
37
  }
38
38
 
39
39
  for (const withdrawal of requests.withdrawals) {
@@ -41,7 +41,7 @@ export function processExecutionPayloadEnvelope(
41
41
  }
42
42
 
43
43
  for (const consolidation of requests.consolidations) {
44
- processConsolidationRequest(fork, state, consolidation);
44
+ processConsolidationRequest(state, consolidation);
45
45
  }
46
46
 
47
47
  // Queue the builder payment
@@ -50,9 +50,6 @@ export function processExecutionPayloadEnvelope(
50
50
  const amount = payment.withdrawal.amount;
51
51
 
52
52
  if (amount > 0) {
53
- const exitQueueEpoch = computeExitEpochAndUpdateChurn(state, BigInt(amount));
54
-
55
- payment.withdrawal.withdrawableEpoch = exitQueueEpoch + state.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY;
56
53
  state.builderPendingWithdrawals.push(payment.withdrawal);
57
54
  }
58
55
 
@@ -75,6 +72,7 @@ function validateExecutionPayloadEnvelope(
75
72
  ): void {
76
73
  const payload = envelope.payload;
77
74
 
75
+ // Cache latest block header state root
78
76
  if (byteArrayEquals(state.latestBlockHeader.stateRoot, ssz.Root.defaultValue())) {
79
77
  const previousStateRoot = state.hashTreeRoot();
80
78
  state.latestBlockHeader.stateRoot = previousStateRoot;
@@ -87,20 +85,18 @@ function validateExecutionPayloadEnvelope(
87
85
  );
88
86
  }
89
87
 
90
- // Verify consistency with the beacon block
91
88
  if (envelope.slot !== state.slot) {
92
89
  throw new Error(`Slot mismatch between envelope and state envelope=${envelope.slot} state=${state.slot}`);
93
90
  }
94
91
 
95
- const committedBid = state.latestExecutionPayloadBid;
96
92
  // Verify consistency with the committed bid
93
+ const committedBid = state.latestExecutionPayloadBid;
97
94
  if (envelope.builderIndex !== committedBid.builderIndex) {
98
95
  throw new Error(
99
96
  `Builder index mismatch between envelope and committed bid envelope=${envelope.builderIndex} committedBid=${committedBid.builderIndex}`
100
97
  );
101
98
  }
102
99
 
103
- // Verify consistency with the committed bid
104
100
  const envelopeKzgRoot = ssz.deneb.BlobKzgCommitments.hashTreeRoot(envelope.blobKzgCommitments);
105
101
  if (!byteArrayEquals(committedBid.blobKzgCommitmentsRoot, envelopeKzgRoot)) {
106
102
  throw new Error(
@@ -108,11 +104,18 @@ function validateExecutionPayloadEnvelope(
108
104
  );
109
105
  }
110
106
 
111
- // Verify the withdrawals root
112
- const envelopeWithdrawalsRoot = ssz.capella.Withdrawals.hashTreeRoot(envelope.payload.withdrawals);
113
- if (!byteArrayEquals(state.latestWithdrawalsRoot, envelopeWithdrawalsRoot)) {
107
+ if (!byteArrayEquals(committedBid.prevRandao, payload.prevRandao)) {
108
+ throw new Error(
109
+ `Prev randao mismatch between committed bid and payload committedBid=${toHex(committedBid.prevRandao)} payload=${toHex(payload.prevRandao)}`
110
+ );
111
+ }
112
+
113
+ // Verify consistency with expected withdrawals
114
+ const payloadWithdrawalsRoot = ssz.capella.Withdrawals.hashTreeRoot(payload.withdrawals);
115
+ const expectedWithdrawalsRoot = state.payloadExpectedWithdrawals.hashTreeRoot();
116
+ if (!byteArrayEquals(payloadWithdrawalsRoot, expectedWithdrawalsRoot)) {
114
117
  throw new Error(
115
- `Withdrawals root mismatch between envelope and latest withdrawals root envelope=${toRootHex(envelopeWithdrawalsRoot)} latestWithdrawalRoot=${toRootHex(state.latestWithdrawalsRoot)}`
118
+ `Withdrawals mismatch between payload and expected withdrawals payload=${toRootHex(payloadWithdrawalsRoot)} expected=${toRootHex(expectedWithdrawalsRoot)}`
116
119
  );
117
120
  }
118
121
 
@@ -137,13 +140,6 @@ function validateExecutionPayloadEnvelope(
137
140
  );
138
141
  }
139
142
 
140
- // Verify prev_randao matches committed bid
141
- if (!byteArrayEquals(committedBid.prevRandao, payload.prevRandao)) {
142
- throw new Error(
143
- `Prev randao mismatch between committed bid and payload committedBid=${toHex(committedBid.prevRandao)} payload=${toHex(payload.prevRandao)}`
144
- );
145
- }
146
-
147
143
  // Verify timestamp
148
144
  if (payload.timestamp !== computeTimeAtSlot(state.config, state.slot, state.genesisTime)) {
149
145
  throw new Error(
@@ -164,14 +160,22 @@ function validateExecutionPayloadEnvelope(
164
160
 
165
161
  function verifyExecutionPayloadEnvelopeSignature(
166
162
  state: CachedBeaconStateGloas,
167
- pubkey: Uint8Array,
168
163
  signedEnvelope: gloas.SignedExecutionPayloadEnvelope
169
164
  ): boolean {
165
+ const builderIndex = signedEnvelope.message.builderIndex;
166
+
170
167
  const domain = state.config.getDomain(state.slot, DOMAIN_BEACON_BUILDER);
171
168
  const signingRoot = computeSigningRoot(ssz.gloas.ExecutionPayloadEnvelope, signedEnvelope.message, domain);
172
169
 
173
170
  try {
174
- const publicKey = PublicKey.fromBytes(pubkey);
171
+ let publicKey: PublicKey;
172
+
173
+ if (builderIndex === BUILDER_INDEX_SELF_BUILD) {
174
+ const validatorIndex = state.latestBlockHeader.proposerIndex;
175
+ publicKey = state.epochCtx.index2pubkey[validatorIndex];
176
+ } else {
177
+ publicKey = PublicKey.fromBytes(state.builders.getReadonly(builderIndex).pubkey);
178
+ }
175
179
  const signature = Signature.fromBytes(signedEnvelope.signature, true);
176
180
 
177
181
  return verify(signingRoot, publicKey, signature);
@@ -75,7 +75,7 @@ export function processOperations(
75
75
  const bodyElectra = body as electra.BeaconBlockBody;
76
76
 
77
77
  for (const depositRequest of bodyElectra.executionRequests.deposits) {
78
- processDepositRequest(stateElectra, depositRequest);
78
+ processDepositRequest(fork, stateElectra, depositRequest);
79
79
  }
80
80
 
81
81
  for (const elWithdrawalRequest of bodyElectra.executionRequests.withdrawals) {
@@ -83,7 +83,7 @@ export function processOperations(
83
83
  }
84
84
 
85
85
  for (const elConsolidationRequest of bodyElectra.executionRequests.consolidations) {
86
- processConsolidationRequest(fork, stateElectra, elConsolidationRequest);
86
+ processConsolidationRequest(stateElectra, elConsolidationRequest);
87
87
  }
88
88
  }
89
89
 
@@ -1,5 +1,8 @@
1
+ import {BeaconConfig} from "@lodestar/config";
1
2
  import {ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";
2
- import {phase0, ssz} from "@lodestar/types";
3
+ import {Slot, phase0, ssz} from "@lodestar/types";
4
+ import {Validator} from "@lodestar/types/phase0";
5
+ import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
3
6
  import {getProposerSlashingSignatureSets} from "../signatureSets/index.js";
4
7
  import {CachedBeaconStateAllForks, CachedBeaconStateGloas} from "../types.js";
5
8
  import {computeEpochAtSlot, isSlashableValidator} from "../util/index.js";
@@ -18,7 +21,15 @@ export function processProposerSlashing(
18
21
  proposerSlashing: phase0.ProposerSlashing,
19
22
  verifySignatures = true
20
23
  ): void {
21
- assertValidProposerSlashing(state, proposerSlashing, verifySignatures);
24
+ const proposer = state.validators.getReadonly(proposerSlashing.signedHeader1.message.proposerIndex);
25
+ assertValidProposerSlashing(
26
+ state.config,
27
+ state.epochCtx.index2pubkey,
28
+ state.slot,
29
+ proposerSlashing,
30
+ proposer,
31
+ verifySignatures
32
+ );
22
33
 
23
34
  if (fork >= ForkSeq.gloas) {
24
35
  const slot = Number(proposerSlashing.signedHeader1.message.slot);
@@ -45,8 +56,11 @@ export function processProposerSlashing(
45
56
  }
46
57
 
47
58
  export function assertValidProposerSlashing(
48
- state: CachedBeaconStateAllForks,
59
+ config: BeaconConfig,
60
+ index2pubkey: Index2PubkeyCache,
61
+ stateSlot: Slot,
49
62
  proposerSlashing: phase0.ProposerSlashing,
63
+ proposer: Validator,
50
64
  verifySignatures = true
51
65
  ): void {
52
66
  const header1 = proposerSlashing.signedHeader1.message;
@@ -70,21 +84,17 @@ export function assertValidProposerSlashing(
70
84
  }
71
85
 
72
86
  // verify the proposer is slashable
73
- const proposer = state.validators.getReadonly(header1.proposerIndex);
74
- if (!isSlashableValidator(proposer, state.epochCtx.epoch)) {
87
+ // ideally we would get the proposer from state.validators using proposerIndex but that requires access to state
88
+ // instead of that we pass in the proposer directly from the consumer side
89
+ if (!isSlashableValidator(proposer, computeEpochAtSlot(stateSlot))) {
75
90
  throw new Error("ProposerSlashing proposer is not slashable");
76
91
  }
77
92
 
78
93
  // verify signatures
79
94
  if (verifySignatures) {
80
- const signatureSets = getProposerSlashingSignatureSets(
81
- state.config,
82
- state.epochCtx.index2pubkey,
83
- state.slot,
84
- proposerSlashing
85
- );
95
+ const signatureSets = getProposerSlashingSignatureSets(config, stateSlot, proposerSlashing);
86
96
  for (let i = 0; i < signatureSets.length; i++) {
87
- if (!verifySignatureSet(signatureSets[i])) {
97
+ if (!verifySignatureSet(signatureSets[i], index2pubkey)) {
88
98
  throw new Error(`ProposerSlashing header${i + 1} signature invalid`);
89
99
  }
90
100
  }
@@ -2,7 +2,6 @@ import {byteArrayEquals} from "@chainsafe/ssz";
2
2
  import {BeaconConfig} from "@lodestar/config";
3
3
  import {DOMAIN_SYNC_COMMITTEE, SYNC_COMMITTEE_SIZE} from "@lodestar/params";
4
4
  import {altair, ssz} from "@lodestar/types";
5
- import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
6
5
  import {SyncCommitteeCache} from "../cache/syncCommitteeCache.js";
7
6
  import {G2_POINT_AT_INFINITY} from "../constants/index.js";
8
7
  import {CachedBeaconStateAllForks} from "../types.js";
@@ -28,13 +27,12 @@ export function processSyncAggregate(
28
27
  const participantIndices = block.body.syncAggregate.syncCommitteeBits.intersectValues(committeeIndices);
29
28
  const signatureSet = getSyncCommitteeSignatureSet(
30
29
  state.config,
31
- state.epochCtx.index2pubkey,
32
30
  state.epochCtx.currentSyncCommitteeIndexed,
33
31
  block,
34
32
  participantIndices
35
33
  );
36
- // When there's no participation we consider the signature valid and just ignore i
37
- if (signatureSet !== null && !verifySignatureSet(signatureSet)) {
34
+ // When there's no participation we consider the signature valid and just ignore it
35
+ if (signatureSet !== null && !verifySignatureSet(signatureSet, state.epochCtx.index2pubkey)) {
38
36
  throw Error("Sync committee signature invalid");
39
37
  }
40
38
  }
@@ -73,7 +71,6 @@ export function processSyncAggregate(
73
71
 
74
72
  export function getSyncCommitteeSignatureSet(
75
73
  config: BeaconConfig,
76
- index2pubkey: Index2PubkeyCache,
77
74
  currentSyncCommitteeIndexed: SyncCommitteeCache,
78
75
  block: altair.BeaconBlock,
79
76
  /** Optional parameter to prevent computing it twice */
@@ -122,7 +119,7 @@ export function getSyncCommitteeSignatureSet(
122
119
 
123
120
  return {
124
121
  type: SignatureSetType.aggregate,
125
- pubkeys: participantIndices.map((i) => index2pubkey[i]),
122
+ indices: participantIndices,
126
123
  signingRoot: computeSigningRoot(ssz.Root, rootSigned, domain),
127
124
  signature,
128
125
  };
@@ -1,8 +1,16 @@
1
+ import {PublicKey, Signature, verify} from "@chainsafe/blst";
1
2
  import {FAR_FUTURE_EPOCH, ForkSeq} from "@lodestar/params";
2
- import {phase0} from "@lodestar/types";
3
+ import {phase0, ssz} from "@lodestar/types";
3
4
  import {verifyVoluntaryExitSignature} from "../signatureSets/index.js";
4
- import {CachedBeaconStateAllForks, CachedBeaconStateElectra} from "../types.js";
5
- import {getPendingBalanceToWithdraw, isActiveValidator} from "../util/index.js";
5
+ import {CachedBeaconStateAllForks, CachedBeaconStateElectra, CachedBeaconStateGloas} from "../types.js";
6
+ import {
7
+ convertValidatorIndexToBuilderIndex,
8
+ getPendingBalanceToWithdrawForBuilder,
9
+ initiateBuilderExit,
10
+ isActiveBuilder,
11
+ isBuilderIndex,
12
+ } from "../util/gloas.js";
13
+ import {computeSigningRoot, getCurrentEpoch, getPendingBalanceToWithdraw, isActiveValidator} from "../util/index.js";
6
14
  import {initiateValidatorExit} from "./index.js";
7
15
 
8
16
  export enum VoluntaryExitValidity {
@@ -16,7 +24,7 @@ export enum VoluntaryExitValidity {
16
24
  }
17
25
 
18
26
  /**
19
- * Process a VoluntaryExit operation. Initiates the exit of a validator.
27
+ * Process a VoluntaryExit operation. Initiates the exit of a validator or builder.
20
28
  *
21
29
  * PERF: Work depends on number of VoluntaryExit per block. On regular networks the average is 0 / block.
22
30
  */
@@ -26,6 +34,53 @@ export function processVoluntaryExit(
26
34
  signedVoluntaryExit: phase0.SignedVoluntaryExit,
27
35
  verifySignature = true
28
36
  ): void {
37
+ const voluntaryExit = signedVoluntaryExit.message;
38
+ const currentEpoch = getCurrentEpoch(state);
39
+
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}`);
43
+ }
44
+
45
+ // Check if this is a builder exit
46
+ 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);
80
+ return;
81
+ }
82
+
83
+ // Handle validator exit
29
84
  const validity = getVoluntaryExitValidity(fork, state, signedVoluntaryExit, verifySignature);
30
85
  if (validity !== VoluntaryExitValidity.valid) {
31
86
  throw Error(`Invalid voluntary exit at forkSeq=${fork} reason=${validity}`);
@@ -69,7 +124,7 @@ export function getVoluntaryExitValidity(
69
124
  // only exit validator if it has no pending withdrawals in the queue
70
125
  if (
71
126
  fork >= ForkSeq.electra &&
72
- getPendingBalanceToWithdraw(fork, state as CachedBeaconStateElectra, voluntaryExit.validatorIndex) !== 0
127
+ getPendingBalanceToWithdraw(state as CachedBeaconStateElectra, voluntaryExit.validatorIndex) !== 0
73
128
  ) {
74
129
  return VoluntaryExitValidity.pendingWithdrawals;
75
130
  }
@@ -42,7 +42,7 @@ export function processWithdrawalRequest(
42
42
  }
43
43
 
44
44
  // TODO Electra: Consider caching pendingPartialWithdrawals
45
- const pendingBalanceToWithdraw = getPendingBalanceToWithdraw(fork, state, validatorIndex);
45
+ const pendingBalanceToWithdraw = getPendingBalanceToWithdraw(state, validatorIndex);
46
46
  const validatorBalance = state.balances.get(validatorIndex);
47
47
 
48
48
  if (isFullExitRequest) {