@lodestar/fork-choice 1.41.0-dev.bb33751bfd → 1.41.0-dev.be5acbb8f7
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.
- package/lib/forkChoice/errors.d.ts +9 -1
- package/lib/forkChoice/errors.d.ts.map +1 -1
- package/lib/forkChoice/errors.js +4 -0
- package/lib/forkChoice/errors.js.map +1 -1
- package/lib/forkChoice/forkChoice.d.ts +73 -17
- package/lib/forkChoice/forkChoice.d.ts.map +1 -1
- package/lib/forkChoice/forkChoice.js +299 -116
- package/lib/forkChoice/forkChoice.js.map +1 -1
- package/lib/forkChoice/interface.d.ts +53 -20
- package/lib/forkChoice/interface.d.ts.map +1 -1
- package/lib/forkChoice/interface.js.map +1 -1
- package/lib/forkChoice/store.d.ts +40 -16
- package/lib/forkChoice/store.d.ts.map +1 -1
- package/lib/forkChoice/store.js +22 -4
- package/lib/forkChoice/store.js.map +1 -1
- package/lib/index.d.ts +4 -4
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/lib/protoArray/computeDeltas.d.ts.map +1 -1
- package/lib/protoArray/computeDeltas.js +3 -0
- package/lib/protoArray/computeDeltas.js.map +1 -1
- package/lib/protoArray/errors.d.ts +15 -2
- package/lib/protoArray/errors.d.ts.map +1 -1
- package/lib/protoArray/errors.js +3 -0
- package/lib/protoArray/errors.js.map +1 -1
- package/lib/protoArray/interface.d.ts +20 -2
- package/lib/protoArray/interface.d.ts.map +1 -1
- package/lib/protoArray/interface.js +16 -0
- package/lib/protoArray/interface.js.map +1 -1
- package/lib/protoArray/protoArray.d.ts +217 -22
- package/lib/protoArray/protoArray.d.ts.map +1 -1
- package/lib/protoArray/protoArray.js +748 -133
- package/lib/protoArray/protoArray.js.map +1 -1
- package/package.json +7 -7
- package/src/forkChoice/errors.ts +7 -2
- package/src/forkChoice/forkChoice.ts +384 -126
- package/src/forkChoice/interface.ts +72 -20
- package/src/forkChoice/store.ts +52 -20
- package/src/index.ts +10 -2
- package/src/protoArray/computeDeltas.ts +6 -0
- package/src/protoArray/errors.ts +7 -1
- package/src/protoArray/interface.ts +36 -3
- package/src/protoArray/protoArray.ts +880 -134
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { SLOTS_PER_EPOCH, SLOTS_PER_HISTORICAL_ROOT } from "@lodestar/params";
|
|
1
|
+
import { ForkSeq, SLOTS_PER_EPOCH, SLOTS_PER_HISTORICAL_ROOT } from "@lodestar/params";
|
|
2
2
|
import { DataAvailabilityStatus, ZERO_HASH, computeEpochAtSlot, computeSlotsSinceEpochStart, computeStartSlotAtEpoch, getAttesterSlashableIndices, isExecutionBlockBodyType, isExecutionEnabled, isExecutionStateType, } from "@lodestar/state-transition";
|
|
3
3
|
import { computeUnrealizedCheckpoints } from "@lodestar/state-transition/epoch";
|
|
4
4
|
import { isGloasBeaconBlock, ssz, } from "@lodestar/types";
|
|
5
5
|
import { MapDef, fromHex, toRootHex } from "@lodestar/utils";
|
|
6
6
|
import { computeDeltas } from "../protoArray/computeDeltas.js";
|
|
7
7
|
import { ProtoArrayError, ProtoArrayErrorCode } from "../protoArray/errors.js";
|
|
8
|
-
import { ExecutionStatus, HEX_ZERO_HASH, NULL_VOTE_INDEX, } from "../protoArray/interface.js";
|
|
8
|
+
import { ExecutionStatus, HEX_ZERO_HASH, NULL_VOTE_INDEX, PayloadStatus, isGloasBlock, } from "../protoArray/interface.js";
|
|
9
9
|
import { ForkChoiceError, ForkChoiceErrorCode, InvalidAttestationCode, InvalidBlockCode } from "./errors.js";
|
|
10
10
|
import { AncestorStatus, NotReorgedReason, } from "./interface.js";
|
|
11
|
-
import {
|
|
11
|
+
import { toCheckpointWithPayload } from "./store.js";
|
|
12
12
|
export var UpdateHeadOpt;
|
|
13
13
|
(function (UpdateHeadOpt) {
|
|
14
14
|
UpdateHeadOpt["GetCanonicalHead"] = "getCanonicalHead";
|
|
@@ -16,7 +16,7 @@ export var UpdateHeadOpt;
|
|
|
16
16
|
UpdateHeadOpt["GetPredictedProposerHead"] = "getPredictedProposerHead";
|
|
17
17
|
})(UpdateHeadOpt || (UpdateHeadOpt = {}));
|
|
18
18
|
// the initial vote epoch for all validators
|
|
19
|
-
const
|
|
19
|
+
const INIT_VOTE_SLOT = 0;
|
|
20
20
|
/**
|
|
21
21
|
* Provides an implementation of "Ethereum Consensus -- Beacon Chain Fork Choice":
|
|
22
22
|
*
|
|
@@ -44,16 +44,26 @@ export class ForkChoice {
|
|
|
44
44
|
irrecoverableError;
|
|
45
45
|
/**
|
|
46
46
|
* Votes currently tracked in the protoArray. Instead of tracking a VoteTracker of currentIndex, nextIndex and epoch,
|
|
47
|
-
* we decompose the struct and track them in
|
|
47
|
+
* we decompose the struct and track them in separate arrays for performance reason.
|
|
48
|
+
*
|
|
49
|
+
* For Gloas (ePBS), LatestMessage tracks slot instead of epoch and includes payload_present flag.
|
|
50
|
+
* Spec: gloas/fork-choice.md#modified-latestmessage
|
|
51
|
+
*
|
|
52
|
+
* IMPORTANT: voteCurrentIndices and voteNextIndices point to the EXACT variant node index.
|
|
53
|
+
* The payload status is encoded in the node index itself (different variants have different indices).
|
|
54
|
+
* For example, if a validator votes for the EMPTY variant, voteNextIndices[i] points to that specific EMPTY node.
|
|
48
55
|
*/
|
|
49
56
|
voteCurrentIndices;
|
|
50
57
|
voteNextIndices;
|
|
51
|
-
|
|
58
|
+
voteNextSlots;
|
|
52
59
|
/**
|
|
53
60
|
* Attestations that arrived at the current slot and must be queued for later processing.
|
|
54
61
|
* NOT currently tracked in the protoArray
|
|
62
|
+
*
|
|
63
|
+
* Modified for Gloas to track PayloadStatus per validator.
|
|
64
|
+
* Maps: Slot -> BlockRoot -> ValidatorIndex -> PayloadStatus
|
|
55
65
|
*/
|
|
56
|
-
queuedAttestations = new MapDef(() => new MapDef(() => new
|
|
66
|
+
queuedAttestations = new MapDef(() => new MapDef(() => new Map()));
|
|
57
67
|
/**
|
|
58
68
|
* It's inconsistent to count number of queued attestations at different intervals of slot.
|
|
59
69
|
* Instead of that, we count number of queued attestations at the previous slot.
|
|
@@ -93,11 +103,11 @@ export class ForkChoice {
|
|
|
93
103
|
this.voteCurrentIndices = new Array(validatorCount).fill(NULL_VOTE_INDEX);
|
|
94
104
|
this.voteNextIndices = new Array(validatorCount).fill(NULL_VOTE_INDEX);
|
|
95
105
|
// when compute deltas, we ignore epoch if voteNextIndex is NULL_VOTE_INDEX anyway
|
|
96
|
-
this.
|
|
106
|
+
this.voteNextSlots = new Array(validatorCount).fill(0);
|
|
97
107
|
this.head = this.updateHead();
|
|
98
108
|
this.balances = this.fcStore.justified.balances;
|
|
99
109
|
metrics?.forkChoice.votes.addCollect(() => {
|
|
100
|
-
metrics.forkChoice.votes.set(this.
|
|
110
|
+
metrics.forkChoice.votes.set(this.voteNextSlots.length);
|
|
101
111
|
metrics.forkChoice.queuedAttestations.set(this.queuedAttestationsPreviousSlot);
|
|
102
112
|
metrics.forkChoice.validatedAttestationDatas.set(this.validatedAttestationDatas.size);
|
|
103
113
|
metrics.forkChoice.balancesLength.set(this.balances.length);
|
|
@@ -163,8 +173,7 @@ export class ForkChoice {
|
|
|
163
173
|
// Return true if the given block passes all criteria to be re-orged out
|
|
164
174
|
// Return false otherwise.
|
|
165
175
|
// Note when proposer boost reorg is disabled, it always returns false
|
|
166
|
-
shouldOverrideForkChoiceUpdate(
|
|
167
|
-
const headBlock = this.getBlockHex(blockRoot);
|
|
176
|
+
shouldOverrideForkChoiceUpdate(headBlock, secFromSlot, currentSlot) {
|
|
168
177
|
if (headBlock === null) {
|
|
169
178
|
// should not happen because this block just got imported. Fall back to no-reorg.
|
|
170
179
|
return { shouldOverrideFcu: false, reason: NotReorgedReason.HeadBlockNotAvailable };
|
|
@@ -179,7 +188,7 @@ export class ForkChoice {
|
|
|
179
188
|
});
|
|
180
189
|
return { shouldOverrideFcu: false, reason: NotReorgedReason.ProposerBoostReorgDisabled };
|
|
181
190
|
}
|
|
182
|
-
const parentBlock = this.protoArray.getBlock(headBlock.parentRoot);
|
|
191
|
+
const parentBlock = this.protoArray.getBlock(headBlock.parentRoot, this.protoArray.getParentPayloadStatus(headBlock));
|
|
183
192
|
const proposalSlot = headBlock.slot + 1;
|
|
184
193
|
// No reorg if parentBlock isn't available
|
|
185
194
|
if (parentBlock === undefined) {
|
|
@@ -194,7 +203,10 @@ export class ForkChoice {
|
|
|
194
203
|
if (!currentTimeOk) {
|
|
195
204
|
return { shouldOverrideFcu: false, reason: NotReorgedReason.ReorgMoreThanOneSlot };
|
|
196
205
|
}
|
|
197
|
-
this.logger?.verbose("Block is weak. Should override forkchoice update", {
|
|
206
|
+
this.logger?.verbose("Block is weak. Should override forkchoice update", {
|
|
207
|
+
blockRoot: headBlock.blockRoot,
|
|
208
|
+
slot: currentSlot,
|
|
209
|
+
});
|
|
198
210
|
return { shouldOverrideFcu: true, parentBlock };
|
|
199
211
|
}
|
|
200
212
|
/**
|
|
@@ -225,7 +237,7 @@ export class ForkChoice {
|
|
|
225
237
|
return headBlock;
|
|
226
238
|
}
|
|
227
239
|
const blockRoot = headBlock.blockRoot;
|
|
228
|
-
const result = this.shouldOverrideForkChoiceUpdate(
|
|
240
|
+
const result = this.shouldOverrideForkChoiceUpdate(headBlock, secFromSlot, currentSlot);
|
|
229
241
|
if (result.shouldOverrideFcu) {
|
|
230
242
|
this.logger?.verbose("Current head is weak. Predicting next block to be built on parent of head.", {
|
|
231
243
|
slot: currentSlot,
|
|
@@ -262,7 +274,7 @@ export class ForkChoice {
|
|
|
262
274
|
});
|
|
263
275
|
return { proposerHead, isHeadTimely, notReorgedReason: NotReorgedReason.ProposerBoostReorgDisabled };
|
|
264
276
|
}
|
|
265
|
-
const parentBlock = this.protoArray.getBlock(headBlock.parentRoot);
|
|
277
|
+
const parentBlock = this.protoArray.getBlock(headBlock.parentRoot, this.protoArray.getParentPayloadStatus(headBlock));
|
|
266
278
|
// No reorg if parentBlock isn't available
|
|
267
279
|
if (parentBlock === undefined) {
|
|
268
280
|
return { proposerHead, isHeadTimely, notReorgedReason: NotReorgedReason.ParentBlockNotAvailable };
|
|
@@ -292,7 +304,7 @@ export class ForkChoice {
|
|
|
292
304
|
slotsPerEpoch: SLOTS_PER_EPOCH,
|
|
293
305
|
committeePercent: this.config.REORG_HEAD_WEIGHT_THRESHOLD,
|
|
294
306
|
});
|
|
295
|
-
const headNode = this.protoArray.getNode(headBlock.blockRoot);
|
|
307
|
+
const headNode = this.protoArray.getNode(headBlock.blockRoot, headBlock.payloadStatus);
|
|
296
308
|
// If headNode is unavailable, give up reorg
|
|
297
309
|
if (headNode === undefined || headNode.weight >= reorgThreshold) {
|
|
298
310
|
return { proposerHead, isHeadTimely, notReorgedReason: NotReorgedReason.HeadBlockNotWeak };
|
|
@@ -303,7 +315,7 @@ export class ForkChoice {
|
|
|
303
315
|
slotsPerEpoch: SLOTS_PER_EPOCH,
|
|
304
316
|
committeePercent: this.config.REORG_PARENT_WEIGHT_THRESHOLD,
|
|
305
317
|
});
|
|
306
|
-
const parentNode = this.protoArray.getNode(parentBlock.blockRoot);
|
|
318
|
+
const parentNode = this.protoArray.getNode(parentBlock.blockRoot, parentBlock.payloadStatus);
|
|
307
319
|
// If parentNode is unavailable, give up reorg
|
|
308
320
|
if (parentNode === undefined || parentNode.weight <= parentThreshold) {
|
|
309
321
|
return { proposerHead, isHeadTimely, notReorgedReason: NotReorgedReason.ParentBlockNotStrong };
|
|
@@ -375,22 +387,9 @@ export class ForkChoice {
|
|
|
375
387
|
finalizedRoot: this.fcStore.finalizedCheckpoint.rootHex,
|
|
376
388
|
currentSlot,
|
|
377
389
|
});
|
|
378
|
-
|
|
379
|
-
const
|
|
380
|
-
|
|
381
|
-
throw new ForkChoiceError({
|
|
382
|
-
code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
|
|
383
|
-
root: headRoot,
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
const headNode = this.protoArray.nodes[headIndex];
|
|
387
|
-
if (headNode === undefined) {
|
|
388
|
-
throw new ForkChoiceError({
|
|
389
|
-
code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
|
|
390
|
-
root: headRoot,
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
this.head = headNode;
|
|
390
|
+
// findHead returns the ProtoNode representing the head
|
|
391
|
+
const head = this.protoArray.findHead(this.fcStore.justified.checkpoint.rootHex, currentSlot);
|
|
392
|
+
this.head = head;
|
|
394
393
|
return this.head;
|
|
395
394
|
}
|
|
396
395
|
/**
|
|
@@ -437,14 +436,18 @@ export class ForkChoice {
|
|
|
437
436
|
onBlock(block, state, blockDelaySec, currentSlot, executionStatus, dataAvailabilityStatus) {
|
|
438
437
|
const { parentRoot, slot } = block;
|
|
439
438
|
const parentRootHex = toRootHex(parentRoot);
|
|
440
|
-
// Parent block must be known
|
|
441
|
-
const
|
|
439
|
+
// Parent block must be known because state_transition would have failed otherwise.
|
|
440
|
+
const parentHashHex = isGloasBeaconBlock(block)
|
|
441
|
+
? toRootHex(block.body.signedExecutionPayloadBid.message.parentBlockHash)
|
|
442
|
+
: null;
|
|
443
|
+
const parentBlock = this.protoArray.getParent(parentRootHex, parentHashHex);
|
|
442
444
|
if (!parentBlock) {
|
|
443
445
|
throw new ForkChoiceError({
|
|
444
446
|
code: ForkChoiceErrorCode.INVALID_BLOCK,
|
|
445
447
|
err: {
|
|
446
448
|
code: InvalidBlockCode.UNKNOWN_PARENT,
|
|
447
449
|
root: parentRootHex,
|
|
450
|
+
hash: parentHashHex,
|
|
448
451
|
},
|
|
449
452
|
});
|
|
450
453
|
}
|
|
@@ -476,15 +479,16 @@ export class ForkChoice {
|
|
|
476
479
|
});
|
|
477
480
|
}
|
|
478
481
|
// Check block is a descendant of the finalized block at the checkpoint finalized slot.
|
|
479
|
-
const
|
|
480
|
-
const
|
|
481
|
-
if (
|
|
482
|
+
const blockAncestorNode = this.getAncestor(parentRootHex, finalizedSlot);
|
|
483
|
+
const fcStoreFinalized = this.fcStore.finalizedCheckpoint;
|
|
484
|
+
if (blockAncestorNode.blockRoot !== fcStoreFinalized.rootHex ||
|
|
485
|
+
blockAncestorNode.payloadStatus !== fcStoreFinalized.payloadStatus) {
|
|
482
486
|
throw new ForkChoiceError({
|
|
483
487
|
code: ForkChoiceErrorCode.INVALID_BLOCK,
|
|
484
488
|
err: {
|
|
485
489
|
code: InvalidBlockCode.NOT_FINALIZED_DESCENDANT,
|
|
486
|
-
finalizedRoot,
|
|
487
|
-
blockAncestor:
|
|
490
|
+
finalizedRoot: fcStoreFinalized.rootHex,
|
|
491
|
+
blockAncestor: blockAncestorNode.blockRoot,
|
|
488
492
|
},
|
|
489
493
|
});
|
|
490
494
|
}
|
|
@@ -499,9 +503,13 @@ export class ForkChoice {
|
|
|
499
503
|
this.proposerBoostRoot === null) {
|
|
500
504
|
this.proposerBoostRoot = blockRootHex;
|
|
501
505
|
}
|
|
502
|
-
|
|
503
|
-
const
|
|
506
|
+
// Get justified checkpoint with payload status for Gloas
|
|
507
|
+
const justifiedPayloadStatus = getCheckpointPayloadStatus(state, state.currentJustifiedCheckpoint.epoch);
|
|
508
|
+
const justifiedCheckpoint = toCheckpointWithPayload(state.currentJustifiedCheckpoint, justifiedPayloadStatus);
|
|
504
509
|
const stateJustifiedEpoch = justifiedCheckpoint.epoch;
|
|
510
|
+
// Get finalized checkpoint with payload status for Gloas
|
|
511
|
+
const finalizedPayloadStatus = getCheckpointPayloadStatus(state, state.finalizedCheckpoint.epoch);
|
|
512
|
+
const finalizedCheckpoint = toCheckpointWithPayload(state.finalizedCheckpoint, finalizedPayloadStatus);
|
|
505
513
|
// Justified balances for `justifiedCheckpoint` are new to the fork-choice. Compute them on demand only if
|
|
506
514
|
// the justified checkpoint changes
|
|
507
515
|
this.updateCheckpoints(justifiedCheckpoint, finalizedCheckpoint, () => this.fcStore.justifiedBalancesGetter(justifiedCheckpoint, state));
|
|
@@ -524,22 +532,32 @@ export class ForkChoice {
|
|
|
524
532
|
if (parentBlock.unrealizedJustifiedEpoch === blockEpoch &&
|
|
525
533
|
parentBlock.unrealizedFinalizedEpoch + 1 >= blockEpoch) {
|
|
526
534
|
// reuse from parent, happens at 1/3 last blocks of epoch as monitored in mainnet
|
|
535
|
+
// Get payload status for unrealized justified checkpoint
|
|
536
|
+
const unrealizedJustifiedPayloadStatus = getCheckpointPayloadStatus(state, parentBlock.unrealizedJustifiedEpoch);
|
|
527
537
|
unrealizedJustifiedCheckpoint = {
|
|
528
538
|
epoch: parentBlock.unrealizedJustifiedEpoch,
|
|
529
539
|
root: fromHex(parentBlock.unrealizedJustifiedRoot),
|
|
530
540
|
rootHex: parentBlock.unrealizedJustifiedRoot,
|
|
541
|
+
payloadStatus: unrealizedJustifiedPayloadStatus,
|
|
531
542
|
};
|
|
543
|
+
// Get payload status for unrealized finalized checkpoint
|
|
544
|
+
const unrealizedFinalizedPayloadStatus = getCheckpointPayloadStatus(state, parentBlock.unrealizedFinalizedEpoch);
|
|
532
545
|
unrealizedFinalizedCheckpoint = {
|
|
533
546
|
epoch: parentBlock.unrealizedFinalizedEpoch,
|
|
534
547
|
root: fromHex(parentBlock.unrealizedFinalizedRoot),
|
|
535
548
|
rootHex: parentBlock.unrealizedFinalizedRoot,
|
|
549
|
+
payloadStatus: unrealizedFinalizedPayloadStatus,
|
|
536
550
|
};
|
|
537
551
|
}
|
|
538
552
|
else {
|
|
539
553
|
// compute new, happens 2/3 first blocks of epoch as monitored in mainnet
|
|
540
554
|
const unrealized = computeUnrealizedCheckpoints(state);
|
|
541
|
-
|
|
542
|
-
|
|
555
|
+
// Get payload status for unrealized justified checkpoint
|
|
556
|
+
const unrealizedJustifiedPayloadStatus = getCheckpointPayloadStatus(state, unrealized.justifiedCheckpoint.epoch);
|
|
557
|
+
unrealizedJustifiedCheckpoint = toCheckpointWithPayload(unrealized.justifiedCheckpoint, unrealizedJustifiedPayloadStatus);
|
|
558
|
+
// Get payload status for unrealized finalized checkpoint
|
|
559
|
+
const unrealizedFinalizedPayloadStatus = getCheckpointPayloadStatus(state, unrealized.finalizedCheckpoint.epoch);
|
|
560
|
+
unrealizedFinalizedCheckpoint = toCheckpointWithPayload(unrealized.finalizedCheckpoint, unrealizedFinalizedPayloadStatus);
|
|
543
561
|
}
|
|
544
562
|
}
|
|
545
563
|
else {
|
|
@@ -572,31 +590,51 @@ export class ForkChoice {
|
|
|
572
590
|
unrealizedJustifiedRoot: unrealizedJustifiedCheckpoint.rootHex,
|
|
573
591
|
unrealizedFinalizedEpoch: unrealizedFinalizedCheckpoint.epoch,
|
|
574
592
|
unrealizedFinalizedRoot: unrealizedFinalizedCheckpoint.rootHex,
|
|
575
|
-
// TODO GLOAS: Need to update this when we are merging nc/epbs-fc. Need to define `getPostGloasExecStatus`
|
|
576
|
-
// to make sure execution status of post-gloas blocks is ExecutionStatus.PayloadSeparated
|
|
577
|
-
...(isExecutionBlockBodyType(block.body) && isExecutionStateType(state) && isExecutionEnabled(state, block)
|
|
578
|
-
? {
|
|
579
|
-
executionPayloadBlockHash: toRootHex(block.body.executionPayload.blockHash),
|
|
580
|
-
executionPayloadNumber: block.body.executionPayload.blockNumber,
|
|
581
|
-
executionStatus: this.getPreGloasExecStatus(executionStatus),
|
|
582
|
-
dataAvailabilityStatus,
|
|
583
|
-
}
|
|
584
|
-
: {
|
|
585
|
-
executionPayloadBlockHash: null,
|
|
586
|
-
executionStatus: this.getPreMergeExecStatus(executionStatus),
|
|
587
|
-
dataAvailabilityStatus: this.getPreMergeDataStatus(dataAvailabilityStatus),
|
|
588
|
-
}),
|
|
589
593
|
...(isGloasBeaconBlock(block)
|
|
590
594
|
? {
|
|
591
|
-
|
|
592
|
-
|
|
595
|
+
executionPayloadBlockHash: toRootHex(block.body.signedExecutionPayloadBid.message.parentBlockHash), // post-gloas, we don't know payload hash until we import execution payload. Set to parent payload hash for now
|
|
596
|
+
executionPayloadNumber: (() => {
|
|
597
|
+
// Determine parent's execution payload number based on which variant the block extends
|
|
598
|
+
const parentBlockHashFromBid = toRootHex(block.body.signedExecutionPayloadBid.message.parentBlockHash);
|
|
599
|
+
// If parent is pre-merge, return 0
|
|
600
|
+
if (parentBlock.executionPayloadBlockHash === null) {
|
|
601
|
+
return 0;
|
|
602
|
+
}
|
|
603
|
+
// If parent is pre-Gloas, it only has FULL variant
|
|
604
|
+
if (parentBlock.parentBlockHash === null) {
|
|
605
|
+
return parentBlock.executionPayloadNumber;
|
|
606
|
+
}
|
|
607
|
+
// Parent is Gloas: get the variant that matches the parentBlockHash from bid
|
|
608
|
+
const parentVariant = this.getBlockHexAndBlockHash(parentRootHex, parentBlockHashFromBid);
|
|
609
|
+
if (parentVariant && parentVariant.executionPayloadBlockHash !== null) {
|
|
610
|
+
return parentVariant.executionPayloadNumber;
|
|
611
|
+
}
|
|
612
|
+
// Fallback to parent block's number (we know it's post-merge from check above)
|
|
613
|
+
return parentBlock.executionPayloadNumber;
|
|
614
|
+
})(),
|
|
615
|
+
executionStatus: this.getPostGloasExecStatus(executionStatus),
|
|
616
|
+
dataAvailabilityStatus,
|
|
593
617
|
}
|
|
594
|
-
:
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
618
|
+
: isExecutionBlockBodyType(block.body) && isExecutionStateType(state) && isExecutionEnabled(state, block)
|
|
619
|
+
? {
|
|
620
|
+
executionPayloadBlockHash: toRootHex(block.body.executionPayload.blockHash),
|
|
621
|
+
executionPayloadNumber: block.body.executionPayload.blockNumber,
|
|
622
|
+
executionStatus: this.getPreGloasExecStatus(executionStatus),
|
|
623
|
+
dataAvailabilityStatus,
|
|
624
|
+
}
|
|
625
|
+
: {
|
|
626
|
+
executionPayloadBlockHash: null,
|
|
627
|
+
executionStatus: this.getPreMergeExecStatus(executionStatus),
|
|
628
|
+
dataAvailabilityStatus: this.getPreMergeDataStatus(dataAvailabilityStatus),
|
|
629
|
+
}),
|
|
630
|
+
payloadStatus: isGloasBeaconBlock(block) ? PayloadStatus.PENDING : PayloadStatus.FULL,
|
|
631
|
+
builderIndex: isGloasBeaconBlock(block) ? block.body.signedExecutionPayloadBid.message.builderIndex : null,
|
|
632
|
+
blockHashFromBid: isGloasBeaconBlock(block)
|
|
633
|
+
? toRootHex(block.body.signedExecutionPayloadBid.message.blockHash)
|
|
634
|
+
: null,
|
|
635
|
+
parentBlockHash: parentHashHex,
|
|
598
636
|
};
|
|
599
|
-
this.protoArray.onBlock(protoBlock, currentSlot);
|
|
637
|
+
this.protoArray.onBlock(protoBlock, currentSlot, this.proposerBoostRoot);
|
|
600
638
|
return protoBlock;
|
|
601
639
|
}
|
|
602
640
|
/**
|
|
@@ -639,10 +677,46 @@ export class ForkChoice {
|
|
|
639
677
|
return;
|
|
640
678
|
}
|
|
641
679
|
this.validateOnAttestation(attestation, slot, blockRootHex, targetEpoch, attDataRoot, forceImport);
|
|
680
|
+
// Pre-gloas: payload is always present
|
|
681
|
+
// Post-gloas:
|
|
682
|
+
// - always add weight to PENDING
|
|
683
|
+
// - if message.slot > block.slot, it also add weights to FULL or EMPTY
|
|
684
|
+
let payloadStatus;
|
|
685
|
+
// We need to retrieve block to check if it's Gloas and to compare slot
|
|
686
|
+
// https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.1/specs/gloas/fork-choice.md#new-is_supporting_vote
|
|
687
|
+
const block = this.getBlockHexDefaultStatus(blockRootHex);
|
|
688
|
+
if (block && isGloasBlock(block)) {
|
|
689
|
+
// Post-Gloas block: determine FULL/EMPTY/PENDING based on slot and committee index
|
|
690
|
+
// If slot > block.slot, we can determine FULL or EMPTY. Else always PENDING
|
|
691
|
+
if (slot > block.slot) {
|
|
692
|
+
if (attestationData.index === 1) {
|
|
693
|
+
payloadStatus = PayloadStatus.FULL;
|
|
694
|
+
}
|
|
695
|
+
else if (attestationData.index === 0) {
|
|
696
|
+
payloadStatus = PayloadStatus.EMPTY;
|
|
697
|
+
}
|
|
698
|
+
else {
|
|
699
|
+
throw new ForkChoiceError({
|
|
700
|
+
code: ForkChoiceErrorCode.INVALID_ATTESTATION,
|
|
701
|
+
err: {
|
|
702
|
+
code: InvalidAttestationCode.INVALID_DATA_INDEX,
|
|
703
|
+
index: attestationData.index,
|
|
704
|
+
},
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
else {
|
|
709
|
+
payloadStatus = PayloadStatus.PENDING;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
else {
|
|
713
|
+
// Pre-Gloas block or block not found: always FULL
|
|
714
|
+
payloadStatus = PayloadStatus.FULL;
|
|
715
|
+
}
|
|
642
716
|
if (slot < this.fcStore.currentSlot) {
|
|
643
717
|
for (const validatorIndex of attestation.attestingIndices) {
|
|
644
718
|
if (!this.fcStore.equivocatingIndices.has(validatorIndex)) {
|
|
645
|
-
this.addLatestMessage(validatorIndex,
|
|
719
|
+
this.addLatestMessage(validatorIndex, slot, blockRootHex, payloadStatus);
|
|
646
720
|
}
|
|
647
721
|
}
|
|
648
722
|
}
|
|
@@ -654,10 +728,10 @@ export class ForkChoice {
|
|
|
654
728
|
// Delay consideration in the fork choice until their slot is in the past.
|
|
655
729
|
// ```
|
|
656
730
|
const byRoot = this.queuedAttestations.getOrDefault(slot);
|
|
657
|
-
const
|
|
731
|
+
const validatorVotes = byRoot.getOrDefault(blockRootHex);
|
|
658
732
|
for (const validatorIndex of attestation.attestingIndices) {
|
|
659
733
|
if (!this.fcStore.equivocatingIndices.has(validatorIndex)) {
|
|
660
|
-
|
|
734
|
+
validatorVotes.set(validatorIndex, payloadStatus);
|
|
661
735
|
}
|
|
662
736
|
}
|
|
663
737
|
}
|
|
@@ -674,6 +748,22 @@ export class ForkChoice {
|
|
|
674
748
|
this.fcStore.equivocatingIndices.add(validatorIndex);
|
|
675
749
|
}
|
|
676
750
|
}
|
|
751
|
+
/**
|
|
752
|
+
* Process a PTC (Payload Timeliness Committee) message
|
|
753
|
+
* Updates the PTC votes for multiple validators attesting to a block
|
|
754
|
+
* Spec: gloas/fork-choice.md#new-on_payload_attestation_message
|
|
755
|
+
*/
|
|
756
|
+
notifyPtcMessages(blockRoot, ptcIndices, payloadPresent) {
|
|
757
|
+
this.protoArray.notifyPtcMessages(blockRoot, ptcIndices, payloadPresent);
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Notify fork choice that an execution payload has arrived (Gloas fork)
|
|
761
|
+
* Creates the FULL variant of a Gloas block when the payload becomes available
|
|
762
|
+
* Spec: gloas/fork-choice.md#new-on_execution_payload
|
|
763
|
+
*/
|
|
764
|
+
onExecutionPayload(blockRoot, executionPayloadBlockHash, executionPayloadNumber, executionPayloadStateRoot) {
|
|
765
|
+
this.protoArray.onExecutionPayload(blockRoot, this.fcStore.currentSlot, executionPayloadBlockHash, executionPayloadNumber, executionPayloadStateRoot, this.proposerBoostRoot);
|
|
766
|
+
}
|
|
677
767
|
/**
|
|
678
768
|
* Call `onTick` for all slots between `fcStore.getCurrentSlot()` and the provided `currentSlot`.
|
|
679
769
|
* This should only be called once per slot because:
|
|
@@ -703,14 +793,19 @@ export class ForkChoice {
|
|
|
703
793
|
return this.hasBlockHex(toRootHex(blockRoot));
|
|
704
794
|
}
|
|
705
795
|
/** Returns a `ProtoBlock` if the block is known **and** a descendant of the finalized root. */
|
|
706
|
-
getBlock(blockRoot) {
|
|
707
|
-
return this.getBlockHex(toRootHex(blockRoot));
|
|
796
|
+
getBlock(blockRoot, payloadStatus) {
|
|
797
|
+
return this.getBlockHex(toRootHex(blockRoot), payloadStatus);
|
|
798
|
+
}
|
|
799
|
+
getBlockDefaultStatus(blockRoot) {
|
|
800
|
+
return this.getBlockHexDefaultStatus(toRootHex(blockRoot));
|
|
708
801
|
}
|
|
709
802
|
/**
|
|
710
803
|
* Returns `true` if the block is known **and** a descendant of the finalized root.
|
|
804
|
+
* Uses default variant (PENDING for Gloas, FULL for pre-Gloas).
|
|
711
805
|
*/
|
|
712
806
|
hasBlockHex(blockRoot) {
|
|
713
|
-
const
|
|
807
|
+
const defaultStatus = this.protoArray.getDefaultVariant(blockRoot);
|
|
808
|
+
const node = defaultStatus !== undefined ? this.protoArray.getNode(blockRoot, defaultStatus) : undefined;
|
|
714
809
|
if (node === undefined) {
|
|
715
810
|
return false;
|
|
716
811
|
}
|
|
@@ -731,8 +826,8 @@ export class ForkChoice {
|
|
|
731
826
|
/**
|
|
732
827
|
* Returns a MUTABLE `ProtoBlock` if the block is known **and** a descendant of the finalized root.
|
|
733
828
|
*/
|
|
734
|
-
getBlockHex(blockRoot) {
|
|
735
|
-
const node = this.protoArray.getNode(blockRoot);
|
|
829
|
+
getBlockHex(blockRoot, payloadStatus) {
|
|
830
|
+
const node = this.protoArray.getNode(blockRoot, payloadStatus);
|
|
736
831
|
if (!node) {
|
|
737
832
|
return null;
|
|
738
833
|
}
|
|
@@ -743,22 +838,45 @@ export class ForkChoice {
|
|
|
743
838
|
...node,
|
|
744
839
|
};
|
|
745
840
|
}
|
|
841
|
+
/**
|
|
842
|
+
* Returns a `ProtoBlock` with the default variant for the given block root
|
|
843
|
+
* - Pre-Gloas blocks: returns FULL variant (only variant)
|
|
844
|
+
* - Gloas blocks: returns PENDING variant
|
|
845
|
+
*
|
|
846
|
+
* Use this when you need the canonical block reference regardless of payload status.
|
|
847
|
+
* For searching by execution payload hash and variant-specific info, use `getBlockHexAndBlockHash` instead.
|
|
848
|
+
*/
|
|
849
|
+
getBlockHexDefaultStatus(blockRoot) {
|
|
850
|
+
const defaultStatus = this.protoArray.getDefaultVariant(blockRoot);
|
|
851
|
+
if (defaultStatus === undefined) {
|
|
852
|
+
return null;
|
|
853
|
+
}
|
|
854
|
+
return this.getBlockHex(blockRoot, defaultStatus);
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Returns EMPTY or FULL `ProtoBlock` that has matching block root and block hash
|
|
858
|
+
*/
|
|
859
|
+
getBlockHexAndBlockHash(blockRoot, blockHash) {
|
|
860
|
+
return this.protoArray.getBlockHexAndBlockHash(blockRoot, blockHash);
|
|
861
|
+
}
|
|
746
862
|
getJustifiedBlock() {
|
|
747
|
-
const
|
|
863
|
+
const { rootHex, payloadStatus } = this.fcStore.justified.checkpoint;
|
|
864
|
+
const block = this.getBlockHex(rootHex, payloadStatus);
|
|
748
865
|
if (!block) {
|
|
749
866
|
throw new ForkChoiceError({
|
|
750
867
|
code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
|
|
751
|
-
root:
|
|
868
|
+
root: rootHex,
|
|
752
869
|
});
|
|
753
870
|
}
|
|
754
871
|
return block;
|
|
755
872
|
}
|
|
756
873
|
getFinalizedBlock() {
|
|
757
|
-
const
|
|
874
|
+
const { rootHex, payloadStatus } = this.fcStore.finalizedCheckpoint;
|
|
875
|
+
const block = this.getBlockHex(rootHex, payloadStatus);
|
|
758
876
|
if (!block) {
|
|
759
877
|
throw new ForkChoiceError({
|
|
760
878
|
code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
|
|
761
|
-
root:
|
|
879
|
+
root: rootHex,
|
|
762
880
|
});
|
|
763
881
|
}
|
|
764
882
|
return block;
|
|
@@ -773,8 +891,8 @@ export class ForkChoice {
|
|
|
773
891
|
* Always returns `false` if either input roots are unknown.
|
|
774
892
|
* Still returns `true` if `ancestorRoot===descendantRoot` (and the roots are known)
|
|
775
893
|
*/
|
|
776
|
-
isDescendant(ancestorRoot, descendantRoot) {
|
|
777
|
-
return this.protoArray.isDescendant(ancestorRoot, descendantRoot);
|
|
894
|
+
isDescendant(ancestorRoot, ancestorPayloadStatus, descendantRoot, descendantPayloadStatus) {
|
|
895
|
+
return this.protoArray.isDescendant(ancestorRoot, ancestorPayloadStatus, descendantRoot, descendantPayloadStatus);
|
|
778
896
|
}
|
|
779
897
|
/**
|
|
780
898
|
* All indices in votes are relative to proto array so always keep it up to date
|
|
@@ -782,7 +900,7 @@ export class ForkChoice {
|
|
|
782
900
|
prune(finalizedRoot) {
|
|
783
901
|
const prunedNodes = this.protoArray.maybePrune(finalizedRoot);
|
|
784
902
|
const prunedCount = prunedNodes.length;
|
|
785
|
-
for (let i = 0; i < this.
|
|
903
|
+
for (let i = 0; i < this.voteNextSlots.length; i++) {
|
|
786
904
|
const currentIndex = this.voteCurrentIndices[i];
|
|
787
905
|
if (currentIndex !== NULL_VOTE_INDEX) {
|
|
788
906
|
if (currentIndex >= prunedCount) {
|
|
@@ -813,35 +931,47 @@ export class ForkChoice {
|
|
|
813
931
|
* Iterates backwards through block summaries, starting from a block root.
|
|
814
932
|
* Return only the non-finalized blocks.
|
|
815
933
|
*/
|
|
816
|
-
iterateAncestorBlocks(blockRoot) {
|
|
817
|
-
return this.protoArray.iterateAncestorNodes(blockRoot);
|
|
934
|
+
iterateAncestorBlocks(blockRoot, payloadStatus) {
|
|
935
|
+
return this.protoArray.iterateAncestorNodes(blockRoot, payloadStatus);
|
|
818
936
|
}
|
|
819
937
|
/**
|
|
820
938
|
* Returns all blocks backwards starting from a block root.
|
|
821
939
|
* Return only the non-finalized blocks.
|
|
822
940
|
*/
|
|
823
|
-
getAllAncestorBlocks(blockRoot) {
|
|
824
|
-
const blocks = this.protoArray.getAllAncestorNodes(blockRoot);
|
|
941
|
+
getAllAncestorBlocks(blockRoot, payloadStatus) {
|
|
942
|
+
const blocks = this.protoArray.getAllAncestorNodes(blockRoot, payloadStatus);
|
|
825
943
|
// the last node is the previous finalized one, it's there to check onBlock finalized checkpoint only.
|
|
826
944
|
return blocks.slice(0, blocks.length - 1);
|
|
827
945
|
}
|
|
828
946
|
/**
|
|
829
947
|
* The same to iterateAncestorBlocks but this gets non-ancestor nodes instead of ancestor nodes.
|
|
830
948
|
*/
|
|
831
|
-
getAllNonAncestorBlocks(blockRoot) {
|
|
832
|
-
return this.protoArray.getAllNonAncestorNodes(blockRoot);
|
|
949
|
+
getAllNonAncestorBlocks(blockRoot, payloadStatus) {
|
|
950
|
+
return this.protoArray.getAllNonAncestorNodes(blockRoot, payloadStatus);
|
|
833
951
|
}
|
|
834
952
|
/**
|
|
835
953
|
* Returns both ancestor and non-ancestor blocks in a single traversal.
|
|
836
954
|
*/
|
|
837
|
-
getAllAncestorAndNonAncestorBlocks(blockRoot) {
|
|
838
|
-
const { ancestors, nonAncestors } = this.protoArray.getAllAncestorAndNonAncestorNodes(blockRoot);
|
|
955
|
+
getAllAncestorAndNonAncestorBlocks(blockRoot, payloadStatus) {
|
|
956
|
+
const { ancestors, nonAncestors } = this.protoArray.getAllAncestorAndNonAncestorNodes(blockRoot, payloadStatus);
|
|
839
957
|
return {
|
|
840
958
|
// the last node is the previous finalized one, it's there to check onBlock finalized checkpoint only.
|
|
841
959
|
ancestors: ancestors.slice(0, ancestors.length - 1),
|
|
842
960
|
nonAncestors,
|
|
843
961
|
};
|
|
844
962
|
}
|
|
963
|
+
getCanonicalBlockByRoot(blockRoot) {
|
|
964
|
+
const blockRootHex = toRootHex(blockRoot);
|
|
965
|
+
if (blockRootHex === this.head.blockRoot) {
|
|
966
|
+
return this.head;
|
|
967
|
+
}
|
|
968
|
+
for (const block of this.protoArray.iterateAncestorNodes(this.head.blockRoot, this.head.payloadStatus)) {
|
|
969
|
+
if (block.blockRoot === blockRootHex) {
|
|
970
|
+
return block;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
return null;
|
|
974
|
+
}
|
|
845
975
|
getCanonicalBlockAtSlot(slot) {
|
|
846
976
|
if (slot > this.head.slot) {
|
|
847
977
|
return null;
|
|
@@ -849,7 +979,7 @@ export class ForkChoice {
|
|
|
849
979
|
if (slot === this.head.slot) {
|
|
850
980
|
return this.head;
|
|
851
981
|
}
|
|
852
|
-
for (const block of this.protoArray.iterateAncestorNodes(this.head.blockRoot)) {
|
|
982
|
+
for (const block of this.protoArray.iterateAncestorNodes(this.head.blockRoot, this.head.payloadStatus)) {
|
|
853
983
|
if (block.slot === slot) {
|
|
854
984
|
return block;
|
|
855
985
|
}
|
|
@@ -860,7 +990,7 @@ export class ForkChoice {
|
|
|
860
990
|
if (slot >= this.head.slot) {
|
|
861
991
|
return this.head;
|
|
862
992
|
}
|
|
863
|
-
for (const block of this.protoArray.iterateAncestorNodes(this.head.blockRoot)) {
|
|
993
|
+
for (const block of this.protoArray.iterateAncestorNodes(this.head.blockRoot, this.head.payloadStatus)) {
|
|
864
994
|
if (slot >= block.slot) {
|
|
865
995
|
return block;
|
|
866
996
|
}
|
|
@@ -871,9 +1001,9 @@ export class ForkChoice {
|
|
|
871
1001
|
forwarditerateAncestorBlocks() {
|
|
872
1002
|
return this.protoArray.nodes;
|
|
873
1003
|
}
|
|
874
|
-
*forwardIterateDescendants(blockRoot) {
|
|
1004
|
+
*forwardIterateDescendants(blockRoot, payloadStatus) {
|
|
875
1005
|
const rootsInChain = new Set([blockRoot]);
|
|
876
|
-
const blockIndex = this.protoArray.
|
|
1006
|
+
const blockIndex = this.protoArray.getNodeIndexByRootAndStatus(blockRoot, payloadStatus);
|
|
877
1007
|
if (blockIndex === undefined) {
|
|
878
1008
|
throw new ForkChoiceError({
|
|
879
1009
|
code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
|
|
@@ -906,8 +1036,8 @@ export class ForkChoice {
|
|
|
906
1036
|
}
|
|
907
1037
|
/** Returns the distance of common ancestor of nodes to the max of the newNode and the prevNode. */
|
|
908
1038
|
getCommonAncestorDepth(prevBlock, newBlock) {
|
|
909
|
-
const prevNode = this.protoArray.getNode(prevBlock.blockRoot);
|
|
910
|
-
const newNode = this.protoArray.getNode(newBlock.blockRoot);
|
|
1039
|
+
const prevNode = this.protoArray.getNode(prevBlock.blockRoot, prevBlock.payloadStatus);
|
|
1040
|
+
const newNode = this.protoArray.getNode(newBlock.blockRoot, newBlock.payloadStatus);
|
|
911
1041
|
if (!prevNode || !newNode) {
|
|
912
1042
|
return { code: AncestorStatus.BlockUnknown };
|
|
913
1043
|
}
|
|
@@ -979,12 +1109,17 @@ export class ForkChoice {
|
|
|
979
1109
|
if (block.slot === beforeSlot) {
|
|
980
1110
|
return block.parentRoot;
|
|
981
1111
|
}
|
|
982
|
-
block
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1112
|
+
// For the first slot of the epoch, a block is it's own target
|
|
1113
|
+
const nextRoot = block.blockRoot === block.targetRoot ? block.parentRoot : block.targetRoot;
|
|
1114
|
+
// Use default variant (PENDING for Gloas, FULL for pre-Gloas)
|
|
1115
|
+
// For Gloas: we search for PENDING blocks because dependent root is determined by the block itself,
|
|
1116
|
+
// not the payload. In state-transition, block parentage is independent of payload status,
|
|
1117
|
+
// so linking by PENDING block in fork-choice is correct.
|
|
1118
|
+
const defaultStatus = this.protoArray.getDefaultVariant(nextRoot);
|
|
1119
|
+
if (defaultStatus === undefined) {
|
|
1120
|
+
throw Error(`No block for root ${nextRoot}`);
|
|
1121
|
+
}
|
|
1122
|
+
block = this.protoArray.getBlockReadonly(nextRoot, defaultStatus);
|
|
988
1123
|
}
|
|
989
1124
|
throw Error(`Not found dependent root for block slot ${block.slot}, epoch difference ${epochDifference}`);
|
|
990
1125
|
}
|
|
@@ -1020,6 +1155,11 @@ export class ForkChoice {
|
|
|
1020
1155
|
throw Error(`Invalid post-merge execution status: expected: ${ExecutionStatus.Syncing} or ${ExecutionStatus.Valid}, got ${executionStatus}`);
|
|
1021
1156
|
return executionStatus;
|
|
1022
1157
|
}
|
|
1158
|
+
getPostGloasExecStatus(executionStatus) {
|
|
1159
|
+
if (executionStatus !== ExecutionStatus.PayloadSeparated)
|
|
1160
|
+
throw Error(`Invalid post-gloas execution status: expected: ${ExecutionStatus.PayloadSeparated}, got ${executionStatus}`);
|
|
1161
|
+
return executionStatus;
|
|
1162
|
+
}
|
|
1023
1163
|
/**
|
|
1024
1164
|
* Why `getJustifiedBalances` getter?
|
|
1025
1165
|
* - updateCheckpoints() is called in both on_block and on_tick.
|
|
@@ -1037,7 +1177,7 @@ export class ForkChoice {
|
|
|
1037
1177
|
*
|
|
1038
1178
|
* **`on_tick`**
|
|
1039
1179
|
* May need the justified balances of:
|
|
1040
|
-
* - unrealizedJustified: Already available in `
|
|
1180
|
+
* - unrealizedJustified: Already available in `CheckpointWithPayloadAndBalance`
|
|
1041
1181
|
* Since this balances are already available the getter is just `() => balances`, without cache interaction
|
|
1042
1182
|
*/
|
|
1043
1183
|
updateCheckpoints(justifiedCheckpoint, finalizedCheckpoint, getJustifiedBalances) {
|
|
@@ -1152,7 +1292,9 @@ export class ForkChoice {
|
|
|
1152
1292
|
//
|
|
1153
1293
|
// Attestations must be for a known block. If the block is unknown, we simply drop the
|
|
1154
1294
|
// attestation and do not delay consideration for later.
|
|
1155
|
-
|
|
1295
|
+
// We don't care which variant it is, just need to find the block
|
|
1296
|
+
const defaultStatus = this.protoArray.getDefaultVariant(beaconBlockRootHex);
|
|
1297
|
+
const block = defaultStatus !== undefined ? this.protoArray.getBlock(beaconBlockRootHex, defaultStatus) : undefined;
|
|
1156
1298
|
if (!block) {
|
|
1157
1299
|
throw new ForkChoiceError({
|
|
1158
1300
|
code: ForkChoiceErrorCode.INVALID_ATTESTATION,
|
|
@@ -1189,30 +1331,48 @@ export class ForkChoice {
|
|
|
1189
1331
|
},
|
|
1190
1332
|
});
|
|
1191
1333
|
}
|
|
1334
|
+
// For Gloas blocks, attestation index must be 0 or 1
|
|
1335
|
+
if (isGloasBlock(block) && attestationData.index !== 0 && attestationData.index !== 1) {
|
|
1336
|
+
throw new ForkChoiceError({
|
|
1337
|
+
code: ForkChoiceErrorCode.INVALID_ATTESTATION,
|
|
1338
|
+
err: {
|
|
1339
|
+
code: InvalidAttestationCode.INVALID_DATA_INDEX,
|
|
1340
|
+
index: attestationData.index,
|
|
1341
|
+
},
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1192
1344
|
this.validatedAttestationDatas.add(attDataRoot);
|
|
1193
1345
|
}
|
|
1194
1346
|
/**
|
|
1195
1347
|
* Add a validator's latest message to the tracked votes.
|
|
1196
1348
|
* Always sync voteCurrentIndices and voteNextIndices so that it'll not throw in computeDeltas()
|
|
1349
|
+
*
|
|
1350
|
+
* Modified for Gloas to accept slot and payloadPresent.
|
|
1351
|
+
* Spec: gloas/fork-choice.md#modified-update_latest_messages
|
|
1352
|
+
*
|
|
1353
|
+
* For backward compatibility with Fulu (pre-Gloas):
|
|
1354
|
+
* - Accepts both epoch-derived and slot parameters
|
|
1355
|
+
* - payloadPresent defaults to true for Fulu (payloads embedded in blocks)
|
|
1197
1356
|
*/
|
|
1198
|
-
addLatestMessage(validatorIndex,
|
|
1357
|
+
addLatestMessage(validatorIndex, nextSlot, nextRoot, nextPayloadStatus) {
|
|
1199
1358
|
// should not happen, attestation is validated before this step
|
|
1200
|
-
|
|
1359
|
+
// Get the node index for the voted block
|
|
1360
|
+
const nextIndex = this.protoArray.getNodeIndexByRootAndStatus(nextRoot, nextPayloadStatus);
|
|
1201
1361
|
if (nextIndex === undefined) {
|
|
1202
|
-
throw new Error(`Could not find proto index for nextRoot ${nextRoot}`);
|
|
1362
|
+
throw new Error(`Could not find proto index for nextRoot ${nextRoot} with payloadStatus ${nextPayloadStatus}`);
|
|
1203
1363
|
}
|
|
1204
1364
|
// ensure there is no undefined entries in Votes arrays
|
|
1205
|
-
if (this.
|
|
1206
|
-
for (let i = this.
|
|
1207
|
-
this.
|
|
1365
|
+
if (this.voteNextSlots.length < validatorIndex + 1) {
|
|
1366
|
+
for (let i = this.voteNextSlots.length; i < validatorIndex + 1; i++) {
|
|
1367
|
+
this.voteNextSlots[i] = INIT_VOTE_SLOT;
|
|
1208
1368
|
this.voteCurrentIndices[i] = this.voteNextIndices[i] = NULL_VOTE_INDEX;
|
|
1209
1369
|
}
|
|
1210
1370
|
}
|
|
1211
|
-
const
|
|
1212
|
-
if (
|
|
1371
|
+
const existingNextSlot = this.voteNextSlots[validatorIndex];
|
|
1372
|
+
if (existingNextSlot === INIT_VOTE_SLOT || computeEpochAtSlot(nextSlot) > computeEpochAtSlot(existingNextSlot)) {
|
|
1213
1373
|
// nextIndex is transfered to currentIndex in computeDeltas()
|
|
1214
1374
|
this.voteNextIndices[validatorIndex] = nextIndex;
|
|
1215
|
-
this.
|
|
1375
|
+
this.voteNextSlots[validatorIndex] = nextSlot;
|
|
1216
1376
|
}
|
|
1217
1377
|
// else its an old vote, don't count it
|
|
1218
1378
|
}
|
|
@@ -1223,17 +1383,16 @@ export class ForkChoice {
|
|
|
1223
1383
|
processAttestationQueue() {
|
|
1224
1384
|
const currentSlot = this.fcStore.currentSlot;
|
|
1225
1385
|
for (const [slot, byRoot] of this.queuedAttestations.entries()) {
|
|
1226
|
-
const targetEpoch = computeEpochAtSlot(slot);
|
|
1227
1386
|
if (slot < currentSlot) {
|
|
1228
1387
|
this.queuedAttestations.delete(slot);
|
|
1229
|
-
for (const [blockRoot,
|
|
1388
|
+
for (const [blockRoot, validatorVotes] of byRoot.entries()) {
|
|
1230
1389
|
const blockRootHex = blockRoot;
|
|
1231
|
-
for (const validatorIndex of
|
|
1390
|
+
for (const [validatorIndex, payloadStatus] of validatorVotes.entries()) {
|
|
1232
1391
|
// equivocatingIndices was checked in onAttestation
|
|
1233
|
-
this.addLatestMessage(validatorIndex,
|
|
1392
|
+
this.addLatestMessage(validatorIndex, slot, blockRootHex, payloadStatus);
|
|
1234
1393
|
}
|
|
1235
1394
|
if (slot === currentSlot - 1) {
|
|
1236
|
-
this.queuedAttestationsPreviousSlot +=
|
|
1395
|
+
this.queuedAttestationsPreviousSlot += validatorVotes.size;
|
|
1237
1396
|
}
|
|
1238
1397
|
}
|
|
1239
1398
|
}
|
|
@@ -1324,4 +1483,28 @@ export function getCommitteeFraction(justifiedTotalActiveBalanceByIncrement, con
|
|
|
1324
1483
|
const committeeWeight = Math.floor(justifiedTotalActiveBalanceByIncrement / config.slotsPerEpoch);
|
|
1325
1484
|
return Math.floor((committeeWeight * config.committeePercent) / 100);
|
|
1326
1485
|
}
|
|
1486
|
+
/**
|
|
1487
|
+
* Get the payload status for a checkpoint.
|
|
1488
|
+
*
|
|
1489
|
+
* Pre-Gloas: always FULL (payload embedded in block)
|
|
1490
|
+
* Gloas: determined by state.execution_payload_availability
|
|
1491
|
+
*
|
|
1492
|
+
* @param state - The state to check execution_payload_availability
|
|
1493
|
+
* @param checkpointEpoch - The epoch of the checkpoint
|
|
1494
|
+
*/
|
|
1495
|
+
export function getCheckpointPayloadStatus(state, checkpointEpoch) {
|
|
1496
|
+
// Compute checkpoint slot first to determine the correct fork
|
|
1497
|
+
const checkpointSlot = computeStartSlotAtEpoch(checkpointEpoch);
|
|
1498
|
+
const fork = state.config.getForkSeq(checkpointSlot);
|
|
1499
|
+
// Pre-Gloas: always FULL
|
|
1500
|
+
if (fork < ForkSeq.gloas) {
|
|
1501
|
+
return PayloadStatus.FULL;
|
|
1502
|
+
}
|
|
1503
|
+
// For Gloas, check state.execution_payload_availability
|
|
1504
|
+
// - For non-skipped slots at checkpoint: returns false (EMPTY) since payload hasn't arrived yet
|
|
1505
|
+
// - For skipped slots at checkpoint: returns the actual availability status from state
|
|
1506
|
+
const gloasState = state;
|
|
1507
|
+
const payloadAvailable = gloasState.executionPayloadAvailability.get(checkpointSlot % SLOTS_PER_HISTORICAL_ROOT);
|
|
1508
|
+
return payloadAvailable ? PayloadStatus.FULL : PayloadStatus.EMPTY;
|
|
1509
|
+
}
|
|
1327
1510
|
//# sourceMappingURL=forkChoice.js.map
|