@lodestar/fork-choice 1.43.0-dev.2870b59b6a → 1.43.0-dev.2fba242f5d
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/forkChoice.d.ts +27 -20
- package/lib/forkChoice/forkChoice.d.ts.map +1 -1
- package/lib/forkChoice/forkChoice.js +64 -77
- package/lib/forkChoice/forkChoice.js.map +1 -1
- package/lib/forkChoice/interface.d.ts +27 -8
- package/lib/forkChoice/interface.d.ts.map +1 -1
- package/lib/forkChoice/store.d.ts +16 -40
- package/lib/forkChoice/store.d.ts.map +1 -1
- package/lib/forkChoice/store.js +4 -22
- package/lib/forkChoice/store.js.map +1 -1
- package/lib/index.d.ts +3 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/protoArray/interface.d.ts +7 -8
- package/lib/protoArray/interface.d.ts.map +1 -1
- package/lib/protoArray/interface.js +3 -4
- package/lib/protoArray/interface.js.map +1 -1
- package/lib/protoArray/protoArray.d.ts +10 -2
- package/lib/protoArray/protoArray.d.ts.map +1 -1
- package/lib/protoArray/protoArray.js +59 -40
- package/lib/protoArray/protoArray.js.map +1 -1
- package/package.json +8 -8
- package/src/forkChoice/forkChoice.ts +78 -126
- package/src/forkChoice/interface.ts +28 -9
- package/src/forkChoice/store.ts +20 -52
- package/src/index.ts +3 -9
- package/src/protoArray/interface.ts +8 -7
- package/src/protoArray/protoArray.ts +71 -43
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {ChainForkConfig} from "@lodestar/config";
|
|
2
|
-
import {
|
|
2
|
+
import {SLOTS_PER_EPOCH} from "@lodestar/params";
|
|
3
3
|
import {
|
|
4
4
|
DataAvailabilityStatus,
|
|
5
5
|
EffectiveBalanceIncrements,
|
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
getAttesterSlashableIndices,
|
|
12
12
|
isExecutionBlockBodyType,
|
|
13
13
|
isStatePostBellatrix,
|
|
14
|
-
isStatePostGloas,
|
|
15
14
|
} from "@lodestar/state-transition";
|
|
16
15
|
import {
|
|
17
16
|
AttesterSlashing,
|
|
@@ -53,7 +52,7 @@ import {
|
|
|
53
52
|
NotReorgedReason,
|
|
54
53
|
ShouldOverrideForkChoiceUpdateResult,
|
|
55
54
|
} from "./interface.js";
|
|
56
|
-
import {
|
|
55
|
+
import {CheckpointWithHex, IForkChoiceStore, JustifiedBalances, toCheckpointWithHex} from "./store.js";
|
|
57
56
|
|
|
58
57
|
export type ForkChoiceOpts = {
|
|
59
58
|
proposerBoost?: boolean;
|
|
@@ -311,6 +310,14 @@ export class ForkChoice implements IForkChoice {
|
|
|
311
310
|
return this.proposerBoostRoot ?? HEX_ZERO_HASH;
|
|
312
311
|
}
|
|
313
312
|
|
|
313
|
+
/**
|
|
314
|
+
* Decides whether to extend an available payload from the previous slot,
|
|
315
|
+
* corresponding to the beacon block `blockRoot`.
|
|
316
|
+
*/
|
|
317
|
+
shouldExtendPayload(blockRoot: RootHex): boolean {
|
|
318
|
+
return this.protoArray.shouldExtendPayload(blockRoot, this.proposerBoostRoot);
|
|
319
|
+
}
|
|
320
|
+
|
|
314
321
|
/**
|
|
315
322
|
* To predict the proposer head of the next slot. That is, to predict if proposer-boost-reorg could happen.
|
|
316
323
|
* Reason why we can't be certain is because information of the head block is not fully available yet
|
|
@@ -556,11 +563,11 @@ export class ForkChoice implements IForkChoice {
|
|
|
556
563
|
return this.protoArray.nodes;
|
|
557
564
|
}
|
|
558
565
|
|
|
559
|
-
getFinalizedCheckpoint():
|
|
566
|
+
getFinalizedCheckpoint(): CheckpointWithHex {
|
|
560
567
|
return this.fcStore.finalizedCheckpoint;
|
|
561
568
|
}
|
|
562
569
|
|
|
563
|
-
getJustifiedCheckpoint():
|
|
570
|
+
getJustifiedCheckpoint(): CheckpointWithHex {
|
|
564
571
|
return this.fcStore.justified.checkpoint;
|
|
565
572
|
}
|
|
566
573
|
|
|
@@ -641,10 +648,7 @@ export class ForkChoice implements IForkChoice {
|
|
|
641
648
|
// Check block is a descendant of the finalized block at the checkpoint finalized slot.
|
|
642
649
|
const blockAncestorNode = this.getAncestor(parentRootHex, finalizedSlot);
|
|
643
650
|
const fcStoreFinalized = this.fcStore.finalizedCheckpoint;
|
|
644
|
-
if (
|
|
645
|
-
blockAncestorNode.blockRoot !== fcStoreFinalized.rootHex ||
|
|
646
|
-
blockAncestorNode.payloadStatus !== fcStoreFinalized.payloadStatus
|
|
647
|
-
) {
|
|
651
|
+
if (blockAncestorNode.blockRoot !== fcStoreFinalized.rootHex) {
|
|
648
652
|
throw new ForkChoiceError({
|
|
649
653
|
code: ForkChoiceErrorCode.INVALID_BLOCK,
|
|
650
654
|
err: {
|
|
@@ -670,18 +674,10 @@ export class ForkChoice implements IForkChoice {
|
|
|
670
674
|
this.proposerBoostRoot = blockRootHex;
|
|
671
675
|
}
|
|
672
676
|
|
|
673
|
-
|
|
674
|
-
const justifiedPayloadStatus = getCheckpointPayloadStatus(
|
|
675
|
-
this.config,
|
|
676
|
-
state,
|
|
677
|
-
state.currentJustifiedCheckpoint.epoch
|
|
678
|
-
);
|
|
679
|
-
const justifiedCheckpoint = toCheckpointWithPayload(state.currentJustifiedCheckpoint, justifiedPayloadStatus);
|
|
677
|
+
const justifiedCheckpoint = toCheckpointWithHex(state.currentJustifiedCheckpoint);
|
|
680
678
|
const stateJustifiedEpoch = justifiedCheckpoint.epoch;
|
|
681
679
|
|
|
682
|
-
|
|
683
|
-
const finalizedPayloadStatus = getCheckpointPayloadStatus(this.config, state, state.finalizedCheckpoint.epoch);
|
|
684
|
-
const finalizedCheckpoint = toCheckpointWithPayload(state.finalizedCheckpoint, finalizedPayloadStatus);
|
|
680
|
+
const finalizedCheckpoint = toCheckpointWithHex(state.finalizedCheckpoint);
|
|
685
681
|
|
|
686
682
|
// Justified balances for `justifiedCheckpoint` are new to the fork-choice. Compute them on demand only if
|
|
687
683
|
// the justified checkpoint changes
|
|
@@ -703,61 +699,29 @@ export class ForkChoice implements IForkChoice {
|
|
|
703
699
|
// This is an optimization. It should reduce the amount of times we run
|
|
704
700
|
// `process_justification_and_finalization` by approximately 1/3rd when the chain is
|
|
705
701
|
// performing optimally.
|
|
706
|
-
let unrealizedJustifiedCheckpoint:
|
|
707
|
-
let unrealizedFinalizedCheckpoint:
|
|
702
|
+
let unrealizedJustifiedCheckpoint: CheckpointWithHex;
|
|
703
|
+
let unrealizedFinalizedCheckpoint: CheckpointWithHex;
|
|
708
704
|
if (this.opts?.computeUnrealized) {
|
|
709
705
|
if (
|
|
710
706
|
parentBlock.unrealizedJustifiedEpoch === blockEpoch &&
|
|
711
707
|
parentBlock.unrealizedFinalizedEpoch + 1 >= blockEpoch
|
|
712
708
|
) {
|
|
713
709
|
// reuse from parent, happens at 1/3 last blocks of epoch as monitored in mainnet
|
|
714
|
-
// Get payload status for unrealized justified checkpoint
|
|
715
|
-
const unrealizedJustifiedPayloadStatus = getCheckpointPayloadStatus(
|
|
716
|
-
this.config,
|
|
717
|
-
state,
|
|
718
|
-
parentBlock.unrealizedJustifiedEpoch
|
|
719
|
-
);
|
|
720
710
|
unrealizedJustifiedCheckpoint = {
|
|
721
711
|
epoch: parentBlock.unrealizedJustifiedEpoch,
|
|
722
712
|
root: fromHex(parentBlock.unrealizedJustifiedRoot),
|
|
723
713
|
rootHex: parentBlock.unrealizedJustifiedRoot,
|
|
724
|
-
payloadStatus: unrealizedJustifiedPayloadStatus,
|
|
725
714
|
};
|
|
726
|
-
// Get payload status for unrealized finalized checkpoint
|
|
727
|
-
const unrealizedFinalizedPayloadStatus = getCheckpointPayloadStatus(
|
|
728
|
-
this.config,
|
|
729
|
-
state,
|
|
730
|
-
parentBlock.unrealizedFinalizedEpoch
|
|
731
|
-
);
|
|
732
715
|
unrealizedFinalizedCheckpoint = {
|
|
733
716
|
epoch: parentBlock.unrealizedFinalizedEpoch,
|
|
734
717
|
root: fromHex(parentBlock.unrealizedFinalizedRoot),
|
|
735
718
|
rootHex: parentBlock.unrealizedFinalizedRoot,
|
|
736
|
-
payloadStatus: unrealizedFinalizedPayloadStatus,
|
|
737
719
|
};
|
|
738
720
|
} else {
|
|
739
721
|
// compute new, happens 2/3 first blocks of epoch as monitored in mainnet
|
|
740
722
|
const unrealized = state.computeUnrealizedCheckpoints();
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
this.config,
|
|
744
|
-
state,
|
|
745
|
-
unrealized.justifiedCheckpoint.epoch
|
|
746
|
-
);
|
|
747
|
-
unrealizedJustifiedCheckpoint = toCheckpointWithPayload(
|
|
748
|
-
unrealized.justifiedCheckpoint,
|
|
749
|
-
unrealizedJustifiedPayloadStatus
|
|
750
|
-
);
|
|
751
|
-
// Get payload status for unrealized finalized checkpoint
|
|
752
|
-
const unrealizedFinalizedPayloadStatus = getCheckpointPayloadStatus(
|
|
753
|
-
this.config,
|
|
754
|
-
state,
|
|
755
|
-
unrealized.finalizedCheckpoint.epoch
|
|
756
|
-
);
|
|
757
|
-
unrealizedFinalizedCheckpoint = toCheckpointWithPayload(
|
|
758
|
-
unrealized.finalizedCheckpoint,
|
|
759
|
-
unrealizedFinalizedPayloadStatus
|
|
760
|
-
);
|
|
723
|
+
unrealizedJustifiedCheckpoint = toCheckpointWithHex(unrealized.justifiedCheckpoint);
|
|
724
|
+
unrealizedFinalizedCheckpoint = toCheckpointWithHex(unrealized.finalizedCheckpoint);
|
|
761
725
|
}
|
|
762
726
|
} else {
|
|
763
727
|
unrealizedJustifiedCheckpoint = justifiedCheckpoint;
|
|
@@ -824,7 +788,7 @@ export class ForkChoice implements IForkChoice {
|
|
|
824
788
|
// Fallback to parent block's number (we know it's post-merge from check above)
|
|
825
789
|
return parentBlock.executionPayloadNumber;
|
|
826
790
|
})(),
|
|
827
|
-
executionStatus: this.
|
|
791
|
+
executionStatus: this.getPostMergeExecStatus(executionStatus),
|
|
828
792
|
dataAvailabilityStatus,
|
|
829
793
|
}
|
|
830
794
|
: isExecutionBlockBodyType(block.body) &&
|
|
@@ -834,7 +798,7 @@ export class ForkChoice implements IForkChoice {
|
|
|
834
798
|
? {
|
|
835
799
|
executionPayloadBlockHash: toRootHex(block.body.executionPayload.blockHash),
|
|
836
800
|
executionPayloadNumber: block.body.executionPayload.blockNumber,
|
|
837
|
-
executionStatus: this.
|
|
801
|
+
executionStatus: this.getPostMergeExecStatus(executionStatus),
|
|
838
802
|
dataAvailabilityStatus,
|
|
839
803
|
}
|
|
840
804
|
: {
|
|
@@ -983,17 +947,17 @@ export class ForkChoice implements IForkChoice {
|
|
|
983
947
|
blockRoot: RootHex,
|
|
984
948
|
executionPayloadBlockHash: RootHex,
|
|
985
949
|
executionPayloadNumber: number,
|
|
986
|
-
|
|
987
|
-
|
|
950
|
+
executionStatus: PayloadExecutionStatus,
|
|
951
|
+
dataAvailabilityStatus: DataAvailabilityStatus
|
|
988
952
|
): void {
|
|
989
953
|
this.protoArray.onExecutionPayload(
|
|
990
954
|
blockRoot,
|
|
991
955
|
this.fcStore.currentSlot,
|
|
992
956
|
executionPayloadBlockHash,
|
|
993
957
|
executionPayloadNumber,
|
|
994
|
-
executionPayloadStateRoot,
|
|
995
958
|
this.proposerBoostRoot,
|
|
996
|
-
executionStatus
|
|
959
|
+
executionStatus,
|
|
960
|
+
dataAvailabilityStatus
|
|
997
961
|
);
|
|
998
962
|
}
|
|
999
963
|
|
|
@@ -1079,6 +1043,12 @@ export class ForkChoice implements IForkChoice {
|
|
|
1079
1043
|
return this.protoArray.hasPayload(blockRoot);
|
|
1080
1044
|
}
|
|
1081
1045
|
|
|
1046
|
+
getPTCVotes(blockRootHex: RootHex): (boolean | null)[] | null {
|
|
1047
|
+
const votes = this.protoArray.getPTCVotes(blockRootHex);
|
|
1048
|
+
if (votes === null) return null;
|
|
1049
|
+
return votes.toBoolArray().map((v) => v ?? null);
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1082
1052
|
/**
|
|
1083
1053
|
* Returns a MUTABLE `ProtoBlock` if the block is known **and** a descendant of the finalized root.
|
|
1084
1054
|
*/
|
|
@@ -1122,8 +1092,8 @@ export class ForkChoice implements IForkChoice {
|
|
|
1122
1092
|
}
|
|
1123
1093
|
|
|
1124
1094
|
getJustifiedBlock(): ProtoBlock {
|
|
1125
|
-
const {rootHex
|
|
1126
|
-
const block = this.
|
|
1095
|
+
const {rootHex} = this.fcStore.justified.checkpoint;
|
|
1096
|
+
const block = this.getBlockHexDefaultStatus(rootHex);
|
|
1127
1097
|
if (!block) {
|
|
1128
1098
|
throw new ForkChoiceError({
|
|
1129
1099
|
code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
|
|
@@ -1134,8 +1104,8 @@ export class ForkChoice implements IForkChoice {
|
|
|
1134
1104
|
}
|
|
1135
1105
|
|
|
1136
1106
|
getFinalizedBlock(): ProtoBlock {
|
|
1137
|
-
const {rootHex
|
|
1138
|
-
const block = this.
|
|
1107
|
+
const {rootHex} = this.fcStore.finalizedCheckpoint;
|
|
1108
|
+
const block = this.getBlockHexDefaultStatus(rootHex);
|
|
1139
1109
|
if (!block) {
|
|
1140
1110
|
throw new ForkChoiceError({
|
|
1141
1111
|
code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
|
|
@@ -1210,13 +1180,12 @@ export class ForkChoice implements IForkChoice {
|
|
|
1210
1180
|
}
|
|
1211
1181
|
|
|
1212
1182
|
/**
|
|
1213
|
-
*
|
|
1214
|
-
*
|
|
1183
|
+
* Raw ancestor walk from `blockRoot` back toward the previous finalized block. Includes both
|
|
1184
|
+
* `blockRoot` and the previous-finalized boundary as last element. Mirrors the semantics of
|
|
1185
|
+
* `getAllAncestorAndNonAncestorBlocks.ancestors`
|
|
1215
1186
|
*/
|
|
1216
1187
|
getAllAncestorBlocks(blockRoot: RootHex, payloadStatus: PayloadStatus): ProtoBlock[] {
|
|
1217
|
-
|
|
1218
|
-
// the last node is the previous finalized one, it's there to check onBlock finalized checkpoint only.
|
|
1219
|
-
return blocks.slice(0, blocks.length - 1);
|
|
1188
|
+
return this.protoArray.getAllAncestorNodes(blockRoot, payloadStatus);
|
|
1220
1189
|
}
|
|
1221
1190
|
|
|
1222
1191
|
/**
|
|
@@ -1228,18 +1197,33 @@ export class ForkChoice implements IForkChoice {
|
|
|
1228
1197
|
|
|
1229
1198
|
/**
|
|
1230
1199
|
* Returns both ancestor and non-ancestor blocks in a single traversal.
|
|
1200
|
+
*
|
|
1201
|
+
* `ancestors` is the raw walk and includes the previous finalized block as its last element —
|
|
1202
|
+
* callers that don't want the boundary should slice it off themselves.
|
|
1203
|
+
* Post-gloas for each block root, it returns exactly one variant of it.
|
|
1231
1204
|
*/
|
|
1232
1205
|
getAllAncestorAndNonAncestorBlocks(
|
|
1233
1206
|
blockRoot: RootHex,
|
|
1234
1207
|
payloadStatus: PayloadStatus
|
|
1235
1208
|
): {ancestors: ProtoBlock[]; nonAncestors: ProtoBlock[]} {
|
|
1236
|
-
|
|
1209
|
+
return this.protoArray.getAllAncestorAndNonAncestorNodes(blockRoot, payloadStatus);
|
|
1210
|
+
}
|
|
1237
1211
|
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1212
|
+
/**
|
|
1213
|
+
* Same to getAllAncestorAndNonAncestorBlocks with default variant of ${blockRoot} to start with
|
|
1214
|
+
*/
|
|
1215
|
+
getAllAncestorAndNonAncestorBlocksDefaultStatus(blockRoot: RootHex): {
|
|
1216
|
+
ancestors: ProtoBlock[];
|
|
1217
|
+
nonAncestors: ProtoBlock[];
|
|
1218
|
+
} {
|
|
1219
|
+
const defaultStatus = this.protoArray.getDefaultVariant(blockRoot);
|
|
1220
|
+
if (defaultStatus === undefined) {
|
|
1221
|
+
throw new ForkChoiceError({
|
|
1222
|
+
code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
|
|
1223
|
+
root: blockRoot,
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
return this.getAllAncestorAndNonAncestorBlocks(blockRoot, defaultStatus);
|
|
1243
1227
|
}
|
|
1244
1228
|
|
|
1245
1229
|
getCanonicalBlockByRoot(blockRoot: Root): ProtoBlock | null {
|
|
@@ -1311,6 +1295,17 @@ export class ForkChoice implements IForkChoice {
|
|
|
1311
1295
|
}
|
|
1312
1296
|
}
|
|
1313
1297
|
|
|
1298
|
+
forwardIterateDescendantsDefaultStatus(blockRoot: RootHex): IterableIterator<ProtoBlock> {
|
|
1299
|
+
const defaultStatus = this.protoArray.getDefaultVariant(blockRoot);
|
|
1300
|
+
if (defaultStatus === undefined) {
|
|
1301
|
+
throw new ForkChoiceError({
|
|
1302
|
+
code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
|
|
1303
|
+
root: blockRoot,
|
|
1304
|
+
});
|
|
1305
|
+
}
|
|
1306
|
+
return this.forwardIterateDescendants(blockRoot, defaultStatus);
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1314
1309
|
/** Very expensive function, iterates the entire ProtoArray. TODO: Is this function even necessary? */
|
|
1315
1310
|
getBlockSummariesByParentRoot(parentRoot: RootHex): ProtoBlock[] {
|
|
1316
1311
|
return this.protoArray.nodes.filter((node) => node.parentRoot === parentRoot);
|
|
@@ -1462,24 +1457,16 @@ export class ForkChoice implements IForkChoice {
|
|
|
1462
1457
|
return dataAvailabilityStatus;
|
|
1463
1458
|
}
|
|
1464
1459
|
|
|
1465
|
-
private
|
|
1460
|
+
private getPostMergeExecStatus(
|
|
1466
1461
|
executionStatus: BlockExecutionStatus
|
|
1467
1462
|
): ExecutionStatus.Valid | ExecutionStatus.Syncing {
|
|
1468
|
-
if (executionStatus === ExecutionStatus.PreMerge
|
|
1463
|
+
if (executionStatus === ExecutionStatus.PreMerge)
|
|
1469
1464
|
throw Error(
|
|
1470
1465
|
`Invalid post-merge execution status: expected: ${ExecutionStatus.Syncing} or ${ExecutionStatus.Valid}, got ${executionStatus}`
|
|
1471
1466
|
);
|
|
1472
1467
|
return executionStatus;
|
|
1473
1468
|
}
|
|
1474
1469
|
|
|
1475
|
-
private getPostGloasExecStatus(executionStatus: BlockExecutionStatus): ExecutionStatus.PayloadSeparated {
|
|
1476
|
-
if (executionStatus !== ExecutionStatus.PayloadSeparated)
|
|
1477
|
-
throw Error(
|
|
1478
|
-
`Invalid post-gloas execution status: expected: ${ExecutionStatus.PayloadSeparated}, got ${executionStatus}`
|
|
1479
|
-
);
|
|
1480
|
-
return executionStatus;
|
|
1481
|
-
}
|
|
1482
|
-
|
|
1483
1470
|
/**
|
|
1484
1471
|
* Why `getJustifiedBalances` getter?
|
|
1485
1472
|
* - updateCheckpoints() is called in both on_block and on_tick.
|
|
@@ -1497,12 +1484,12 @@ export class ForkChoice implements IForkChoice {
|
|
|
1497
1484
|
*
|
|
1498
1485
|
* **`on_tick`**
|
|
1499
1486
|
* May need the justified balances of:
|
|
1500
|
-
* - unrealizedJustified: Already available in `
|
|
1487
|
+
* - unrealizedJustified: Already available in `CheckpointWithBalance`
|
|
1501
1488
|
* Since this balances are already available the getter is just `() => balances`, without cache interaction
|
|
1502
1489
|
*/
|
|
1503
1490
|
private updateCheckpoints(
|
|
1504
|
-
justifiedCheckpoint:
|
|
1505
|
-
finalizedCheckpoint:
|
|
1491
|
+
justifiedCheckpoint: CheckpointWithHex,
|
|
1492
|
+
finalizedCheckpoint: CheckpointWithHex,
|
|
1506
1493
|
getJustifiedBalances: () => JustifiedBalances
|
|
1507
1494
|
): void {
|
|
1508
1495
|
// Update justified checkpoint.
|
|
@@ -1522,8 +1509,8 @@ export class ForkChoice implements IForkChoice {
|
|
|
1522
1509
|
* Update unrealized checkpoints in store if necessary
|
|
1523
1510
|
*/
|
|
1524
1511
|
private updateUnrealizedCheckpoints(
|
|
1525
|
-
unrealizedJustifiedCheckpoint:
|
|
1526
|
-
unrealizedFinalizedCheckpoint:
|
|
1512
|
+
unrealizedJustifiedCheckpoint: CheckpointWithHex,
|
|
1513
|
+
unrealizedFinalizedCheckpoint: CheckpointWithHex,
|
|
1527
1514
|
getJustifiedBalances: () => JustifiedBalances
|
|
1528
1515
|
): void {
|
|
1529
1516
|
if (unrealizedJustifiedCheckpoint.epoch > this.fcStore.unrealizedJustified.checkpoint.epoch) {
|
|
@@ -1898,38 +1885,3 @@ export function getCommitteeFraction(
|
|
|
1898
1885
|
const committeeWeight = Math.floor(justifiedTotalActiveBalanceByIncrement / config.slotsPerEpoch);
|
|
1899
1886
|
return Math.floor((committeeWeight * config.committeePercent) / 100);
|
|
1900
1887
|
}
|
|
1901
|
-
|
|
1902
|
-
/**
|
|
1903
|
-
* Get the payload status for a checkpoint.
|
|
1904
|
-
*
|
|
1905
|
-
* Pre-Gloas: always FULL (payload embedded in block)
|
|
1906
|
-
* Gloas: determined by state.execution_payload_availability
|
|
1907
|
-
*
|
|
1908
|
-
* @param config - The chain fork config to determine fork at checkpoint slot
|
|
1909
|
-
* @param state - The state to check execution_payload_availability
|
|
1910
|
-
* @param checkpointEpoch - The epoch of the checkpoint
|
|
1911
|
-
*/
|
|
1912
|
-
export function getCheckpointPayloadStatus(
|
|
1913
|
-
config: ChainForkConfig,
|
|
1914
|
-
state: IBeaconStateView,
|
|
1915
|
-
checkpointEpoch: number
|
|
1916
|
-
): PayloadStatus {
|
|
1917
|
-
// Compute checkpoint slot first to determine the correct fork
|
|
1918
|
-
const checkpointSlot = computeStartSlotAtEpoch(checkpointEpoch);
|
|
1919
|
-
const fork = config.getForkSeq(checkpointSlot);
|
|
1920
|
-
|
|
1921
|
-
// Pre-Gloas: always FULL
|
|
1922
|
-
if (fork < ForkSeq.gloas) {
|
|
1923
|
-
return PayloadStatus.FULL;
|
|
1924
|
-
}
|
|
1925
|
-
if (!isStatePostGloas(state)) {
|
|
1926
|
-
throw new Error(`Expected gloas+ state for checkpoint payload status, got fork=${state.forkName}`);
|
|
1927
|
-
}
|
|
1928
|
-
|
|
1929
|
-
// For Gloas, check state.execution_payload_availability
|
|
1930
|
-
// - For non-skipped slots at checkpoint: returns false (EMPTY) since payload hasn't arrived yet
|
|
1931
|
-
// - For skipped slots at checkpoint: returns the actual availability status from state
|
|
1932
|
-
const payloadAvailable = state.executionPayloadAvailability.get(checkpointSlot % SLOTS_PER_HISTORICAL_ROOT);
|
|
1933
|
-
|
|
1934
|
-
return payloadAvailable ? PayloadStatus.FULL : PayloadStatus.EMPTY;
|
|
1935
|
-
}
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
ProtoNode,
|
|
10
10
|
} from "../protoArray/interface.js";
|
|
11
11
|
import {UpdateAndGetHeadOpt} from "./forkChoice.js";
|
|
12
|
-
import {CheckpointWithHex
|
|
12
|
+
import {CheckpointWithHex} from "./store.js";
|
|
13
13
|
|
|
14
14
|
export type CheckpointHex = {
|
|
15
15
|
epoch: Epoch;
|
|
@@ -21,12 +21,12 @@ export type CheckpointsWithHex = {
|
|
|
21
21
|
finalizedCheckpoint: CheckpointWithHex;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
export type
|
|
25
|
-
checkpoint:
|
|
24
|
+
export type CheckpointWithBalance = {
|
|
25
|
+
checkpoint: CheckpointWithHex;
|
|
26
26
|
balances: EffectiveBalanceIncrements;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
export type
|
|
29
|
+
export type CheckpointWithTotalBalance = CheckpointWithBalance & {
|
|
30
30
|
totalBalance: number;
|
|
31
31
|
};
|
|
32
32
|
|
|
@@ -121,8 +121,8 @@ export interface IForkChoice {
|
|
|
121
121
|
* Retrieve all nodes for the debug API.
|
|
122
122
|
*/
|
|
123
123
|
getAllNodes(): ProtoNode[];
|
|
124
|
-
getFinalizedCheckpoint():
|
|
125
|
-
getJustifiedCheckpoint():
|
|
124
|
+
getFinalizedCheckpoint(): CheckpointWithHex;
|
|
125
|
+
getJustifiedCheckpoint(): CheckpointWithHex;
|
|
126
126
|
/**
|
|
127
127
|
* Add `block` to the fork choice DAG.
|
|
128
128
|
*
|
|
@@ -198,14 +198,13 @@ export interface IForkChoice {
|
|
|
198
198
|
* @param blockRoot - The beacon block root for which the payload arrived
|
|
199
199
|
* @param executionPayloadBlockHash - The block hash of the execution payload
|
|
200
200
|
* @param executionPayloadNumber - The block number of the execution payload
|
|
201
|
-
* @param executionPayloadStateRoot - The execution payload state root ie. the root of post-state after processExecutionPayloadEnvelope()
|
|
202
201
|
*/
|
|
203
202
|
onExecutionPayload(
|
|
204
203
|
blockRoot: RootHex,
|
|
205
204
|
executionPayloadBlockHash: RootHex,
|
|
206
205
|
executionPayloadNumber: number,
|
|
207
|
-
|
|
208
|
-
|
|
206
|
+
executionStatus: PayloadExecutionStatus,
|
|
207
|
+
dataAvailabilityStatus: DataAvailabilityStatus
|
|
209
208
|
): void;
|
|
210
209
|
/**
|
|
211
210
|
* Call `onTick` for all slots between `fcStore.getCurrentSlot()` and the provided `currentSlot`.
|
|
@@ -233,6 +232,7 @@ export interface IForkChoice {
|
|
|
233
232
|
hasPayloadUnsafe(blockRoot: Root): boolean;
|
|
234
233
|
hasPayloadHexUnsafe(blockRoot: RootHex): boolean;
|
|
235
234
|
getSlotsPresent(windowStart: number): number;
|
|
235
|
+
getPTCVotes(blockRootHex: RootHex): (boolean | null)[] | null;
|
|
236
236
|
/**
|
|
237
237
|
* Returns a `ProtoBlock` if the block is known **and** a descendant of the finalized root.
|
|
238
238
|
*/
|
|
@@ -241,6 +241,7 @@ export interface IForkChoice {
|
|
|
241
241
|
getBlockDefaultStatus(blockRoot: Root): ProtoBlock | null;
|
|
242
242
|
getBlockHexDefaultStatus(blockRoot: RootHex): ProtoBlock | null;
|
|
243
243
|
getBlockHexAndBlockHash(blockRoot: RootHex, blockHash: RootHex): ProtoBlock | null;
|
|
244
|
+
shouldExtendPayload(blockRoot: RootHex): boolean;
|
|
244
245
|
getFinalizedBlock(): ProtoBlock;
|
|
245
246
|
getJustifiedBlock(): ProtoBlock;
|
|
246
247
|
getFinalizedCheckpointSlot(): Slot;
|
|
@@ -272,11 +273,23 @@ export interface IForkChoice {
|
|
|
272
273
|
getAllNonAncestorBlocks(blockRoot: RootHex, payloadStatus: PayloadStatus): ProtoBlock[];
|
|
273
274
|
/**
|
|
274
275
|
* Returns both ancestor and non-ancestor blocks in a single traversal.
|
|
276
|
+
*
|
|
277
|
+
* `ancestors` is the raw walk and includes the previous finalized block as its last element —
|
|
278
|
+
* callers that don't want the boundary should slice it off themselves.
|
|
275
279
|
*/
|
|
276
280
|
getAllAncestorAndNonAncestorBlocks(
|
|
277
281
|
blockRoot: RootHex,
|
|
278
282
|
payloadStatus: PayloadStatus
|
|
279
283
|
): {ancestors: ProtoBlock[]; nonAncestors: ProtoBlock[]};
|
|
284
|
+
/**
|
|
285
|
+
* Same as `getAllAncestorAndNonAncestorBlocks` but resolves the default payload-status variant
|
|
286
|
+
* (FULL pre-Gloas, PENDING for Gloas) for the given root. Use when the caller holds a
|
|
287
|
+
* `CheckpointWithHex` / finalized root without a specific payload-status variant in mind.
|
|
288
|
+
*/
|
|
289
|
+
getAllAncestorAndNonAncestorBlocksDefaultStatus(blockRoot: RootHex): {
|
|
290
|
+
ancestors: ProtoBlock[];
|
|
291
|
+
nonAncestors: ProtoBlock[];
|
|
292
|
+
};
|
|
280
293
|
getCanonicalBlockByRoot(blockRoot: Root): ProtoBlock | null;
|
|
281
294
|
getCanonicalBlockAtSlot(slot: Slot): ProtoBlock | null;
|
|
282
295
|
getCanonicalBlockClosestLteSlot(slot: Slot): ProtoBlock | null;
|
|
@@ -288,6 +301,12 @@ export interface IForkChoice {
|
|
|
288
301
|
* Iterates forward descendants of blockRoot. Does not yield blockRoot itself
|
|
289
302
|
*/
|
|
290
303
|
forwardIterateDescendants(blockRoot: RootHex, payloadStatus: PayloadStatus): IterableIterator<ProtoBlock>;
|
|
304
|
+
/**
|
|
305
|
+
* Same as `forwardIterateDescendants` but resolves the default payload-status variant
|
|
306
|
+
* (FULL pre-Gloas, PENDING for Gloas) for the given root. Use when the caller holds a
|
|
307
|
+
* `CheckpointWithHex` / finalized root without a specific payload-status variant in mind.
|
|
308
|
+
*/
|
|
309
|
+
forwardIterateDescendantsDefaultStatus(blockRoot: RootHex): IterableIterator<ProtoBlock>;
|
|
291
310
|
getBlockSummariesByParentRoot(parentRoot: RootHex): ProtoBlock[];
|
|
292
311
|
getBlockSummariesAtSlot(slot: Slot): ProtoBlock[];
|
|
293
312
|
/** Returns the distance of common ancestor of nodes to the max of the newNode and the prevNode. */
|
package/src/forkChoice/store.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import {EffectiveBalanceIncrements, IBeaconStateView} from "@lodestar/state-transition";
|
|
2
2
|
import {RootHex, Slot, ValidatorIndex, phase0} from "@lodestar/types";
|
|
3
3
|
import {toRootHex} from "@lodestar/utils";
|
|
4
|
-
import {
|
|
5
|
-
import {CheckpointWithPayloadAndBalance, CheckpointWithPayloadAndTotalBalance} from "./interface.js";
|
|
4
|
+
import {CheckpointWithBalance, CheckpointWithTotalBalance} from "./interface.js";
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* Stores checkpoints in a hybrid format:
|
|
@@ -11,15 +10,6 @@ import {CheckpointWithPayloadAndBalance, CheckpointWithPayloadAndTotalBalance} f
|
|
|
11
10
|
*/
|
|
12
11
|
export type CheckpointWithHex = phase0.Checkpoint & {rootHex: RootHex};
|
|
13
12
|
|
|
14
|
-
/**
|
|
15
|
-
* Checkpoint with payload status for Gloas fork choice.
|
|
16
|
-
* Used to track which variant (EMPTY or FULL) of the finalized/justified block to use.
|
|
17
|
-
*
|
|
18
|
-
* Pre-Gloas: payloadStatus is always FULL (payload embedded in block)
|
|
19
|
-
* Gloas: determined by state.execution_payload_availability
|
|
20
|
-
*/
|
|
21
|
-
export type CheckpointWithPayloadStatus = CheckpointWithHex & {payloadStatus: PayloadStatus};
|
|
22
|
-
|
|
23
13
|
export type JustifiedBalances = EffectiveBalanceIncrements;
|
|
24
14
|
|
|
25
15
|
/**
|
|
@@ -29,7 +19,7 @@ export type JustifiedBalances = EffectiveBalanceIncrements;
|
|
|
29
19
|
* @param blockState state that declares justified checkpoint `checkpoint`
|
|
30
20
|
*/
|
|
31
21
|
export type JustifiedBalancesGetter = (
|
|
32
|
-
checkpoint:
|
|
22
|
+
checkpoint: CheckpointWithHex,
|
|
33
23
|
blockState: IBeaconStateView
|
|
34
24
|
) => JustifiedBalances;
|
|
35
25
|
|
|
@@ -47,11 +37,11 @@ export type JustifiedBalancesGetter = (
|
|
|
47
37
|
*/
|
|
48
38
|
export interface IForkChoiceStore {
|
|
49
39
|
currentSlot: Slot;
|
|
50
|
-
get justified():
|
|
51
|
-
set justified(justified:
|
|
52
|
-
unrealizedJustified:
|
|
53
|
-
finalizedCheckpoint:
|
|
54
|
-
unrealizedFinalizedCheckpoint:
|
|
40
|
+
get justified(): CheckpointWithTotalBalance;
|
|
41
|
+
set justified(justified: CheckpointWithBalance);
|
|
42
|
+
unrealizedJustified: CheckpointWithBalance;
|
|
43
|
+
finalizedCheckpoint: CheckpointWithHex;
|
|
44
|
+
unrealizedFinalizedCheckpoint: CheckpointWithHex;
|
|
55
45
|
justifiedBalancesGetter: JustifiedBalancesGetter;
|
|
56
46
|
equivocatingIndices: Set<ValidatorIndex>;
|
|
57
47
|
}
|
|
@@ -60,10 +50,10 @@ export interface IForkChoiceStore {
|
|
|
60
50
|
* IForkChoiceStore implementer which emits forkChoice events on updated justified and finalized checkpoints.
|
|
61
51
|
*/
|
|
62
52
|
export class ForkChoiceStore implements IForkChoiceStore {
|
|
63
|
-
private _justified:
|
|
64
|
-
unrealizedJustified:
|
|
65
|
-
private _finalizedCheckpoint:
|
|
66
|
-
unrealizedFinalizedCheckpoint:
|
|
53
|
+
private _justified: CheckpointWithTotalBalance;
|
|
54
|
+
unrealizedJustified: CheckpointWithBalance;
|
|
55
|
+
private _finalizedCheckpoint: CheckpointWithHex;
|
|
56
|
+
unrealizedFinalizedCheckpoint: CheckpointWithHex;
|
|
67
57
|
equivocatingIndices = new Set<ValidatorIndex>();
|
|
68
58
|
justifiedBalancesGetter: JustifiedBalancesGetter;
|
|
69
59
|
currentSlot: Slot;
|
|
@@ -74,49 +64,37 @@ export class ForkChoiceStore implements IForkChoiceStore {
|
|
|
74
64
|
finalizedCheckpoint: phase0.Checkpoint,
|
|
75
65
|
justifiedBalances: EffectiveBalanceIncrements,
|
|
76
66
|
justifiedBalancesGetter: JustifiedBalancesGetter,
|
|
77
|
-
/**
|
|
78
|
-
* Payload status for justified checkpoint.
|
|
79
|
-
* Pre-Gloas: always FULL
|
|
80
|
-
* Gloas: determined by state.execution_payload_availability
|
|
81
|
-
*/
|
|
82
|
-
justifiedPayloadStatus: PayloadStatus,
|
|
83
|
-
/**
|
|
84
|
-
* Payload status for finalized checkpoint.
|
|
85
|
-
* Pre-Gloas: always FULL
|
|
86
|
-
* Gloas: determined by state.execution_payload_availability
|
|
87
|
-
*/
|
|
88
|
-
finalizedPayloadStatus: PayloadStatus,
|
|
89
67
|
private readonly events?: {
|
|
90
|
-
onJustified: (cp:
|
|
91
|
-
onFinalized: (cp:
|
|
68
|
+
onJustified: (cp: CheckpointWithHex) => void;
|
|
69
|
+
onFinalized: (cp: CheckpointWithHex) => void;
|
|
92
70
|
}
|
|
93
71
|
) {
|
|
94
72
|
this.justifiedBalancesGetter = justifiedBalancesGetter;
|
|
95
73
|
this.currentSlot = currentSlot;
|
|
96
74
|
const justified = {
|
|
97
|
-
checkpoint:
|
|
75
|
+
checkpoint: toCheckpointWithHex(justifiedCheckpoint),
|
|
98
76
|
balances: justifiedBalances,
|
|
99
77
|
totalBalance: computeTotalBalance(justifiedBalances),
|
|
100
78
|
};
|
|
101
79
|
this._justified = justified;
|
|
102
80
|
this.unrealizedJustified = justified;
|
|
103
|
-
this._finalizedCheckpoint =
|
|
81
|
+
this._finalizedCheckpoint = toCheckpointWithHex(finalizedCheckpoint);
|
|
104
82
|
this.unrealizedFinalizedCheckpoint = this._finalizedCheckpoint;
|
|
105
83
|
}
|
|
106
84
|
|
|
107
|
-
get justified():
|
|
85
|
+
get justified(): CheckpointWithTotalBalance {
|
|
108
86
|
return this._justified;
|
|
109
87
|
}
|
|
110
|
-
set justified(justified:
|
|
88
|
+
set justified(justified: CheckpointWithBalance) {
|
|
111
89
|
this._justified = {...justified, totalBalance: computeTotalBalance(justified.balances)};
|
|
112
90
|
this.events?.onJustified(justified.checkpoint);
|
|
113
91
|
}
|
|
114
92
|
|
|
115
|
-
get finalizedCheckpoint():
|
|
93
|
+
get finalizedCheckpoint(): CheckpointWithHex {
|
|
116
94
|
return this._finalizedCheckpoint;
|
|
117
95
|
}
|
|
118
|
-
set finalizedCheckpoint(checkpoint:
|
|
119
|
-
const cp =
|
|
96
|
+
set finalizedCheckpoint(checkpoint: CheckpointWithHex) {
|
|
97
|
+
const cp = toCheckpointWithHex(checkpoint);
|
|
120
98
|
this._finalizedCheckpoint = cp;
|
|
121
99
|
this.events?.onFinalized(cp);
|
|
122
100
|
}
|
|
@@ -133,16 +111,6 @@ export function toCheckpointWithHex(checkpoint: phase0.Checkpoint): CheckpointWi
|
|
|
133
111
|
};
|
|
134
112
|
}
|
|
135
113
|
|
|
136
|
-
export function toCheckpointWithPayload(
|
|
137
|
-
checkpoint: phase0.Checkpoint,
|
|
138
|
-
payloadStatus: PayloadStatus
|
|
139
|
-
): CheckpointWithPayloadStatus {
|
|
140
|
-
return {
|
|
141
|
-
...toCheckpointWithHex(checkpoint),
|
|
142
|
-
payloadStatus,
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
114
|
export function equalCheckpointWithHex(a: CheckpointWithHex, b: CheckpointWithHex): boolean {
|
|
147
115
|
return a.epoch === b.epoch && a.rootHex === b.rootHex;
|
|
148
116
|
}
|
package/src/index.ts
CHANGED
|
@@ -6,17 +6,12 @@ export {
|
|
|
6
6
|
type InvalidBlock,
|
|
7
7
|
InvalidBlockCode,
|
|
8
8
|
} from "./forkChoice/errors.js";
|
|
9
|
-
export {
|
|
10
|
-
ForkChoice,
|
|
11
|
-
type ForkChoiceOpts,
|
|
12
|
-
UpdateHeadOpt,
|
|
13
|
-
getCheckpointPayloadStatus,
|
|
14
|
-
} from "./forkChoice/forkChoice.js";
|
|
9
|
+
export {ForkChoice, type ForkChoiceOpts, UpdateHeadOpt} from "./forkChoice/forkChoice.js";
|
|
15
10
|
export {
|
|
16
11
|
type AncestorResult,
|
|
17
12
|
AncestorStatus,
|
|
18
|
-
type
|
|
19
|
-
type
|
|
13
|
+
type CheckpointWithBalance,
|
|
14
|
+
type CheckpointWithTotalBalance,
|
|
20
15
|
EpochDifference,
|
|
21
16
|
type IForkChoice,
|
|
22
17
|
NotReorgedReason,
|
|
@@ -24,7 +19,6 @@ export {
|
|
|
24
19
|
export * from "./forkChoice/safeBlocks.js";
|
|
25
20
|
export {
|
|
26
21
|
type CheckpointWithHex,
|
|
27
|
-
type CheckpointWithPayloadStatus,
|
|
28
22
|
ForkChoiceStore,
|
|
29
23
|
type IForkChoiceStore,
|
|
30
24
|
type JustifiedBalancesGetter,
|