@lodestar/state-transition 1.50.0 → 1.51.0-dev.7d2ef8139c

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 (120) hide show
  1. package/lib/block/index.d.ts.map +1 -1
  2. package/lib/block/index.js +1 -1
  3. package/lib/block/index.js.map +1 -1
  4. package/lib/block/processAttestationsAltair.d.ts.map +1 -1
  5. package/lib/block/processAttestationsAltair.js +3 -1
  6. package/lib/block/processAttestationsAltair.js.map +1 -1
  7. package/lib/block/processDeposit.d.ts +2 -2
  8. package/lib/block/processDeposit.d.ts.map +1 -1
  9. package/lib/block/processDeposit.js.map +1 -1
  10. package/lib/block/processEth1Data.d.ts +3 -3
  11. package/lib/block/processEth1Data.d.ts.map +1 -1
  12. package/lib/block/processEth1Data.js.map +1 -1
  13. package/lib/block/processOperations.d.ts.map +1 -1
  14. package/lib/block/processOperations.js +8 -6
  15. package/lib/block/processOperations.js.map +1 -1
  16. package/lib/cache/epochCache.d.ts +3 -0
  17. package/lib/cache/epochCache.d.ts.map +1 -1
  18. package/lib/cache/epochCache.js +11 -4
  19. package/lib/cache/epochCache.js.map +1 -1
  20. package/lib/cache/epochTransitionCache.d.ts.map +1 -1
  21. package/lib/cache/epochTransitionCache.js +2 -1
  22. package/lib/cache/epochTransitionCache.js.map +1 -1
  23. package/lib/cache/stateCache.d.ts +2 -1
  24. package/lib/cache/stateCache.d.ts.map +1 -1
  25. package/lib/cache/stateCache.js.map +1 -1
  26. package/lib/cache/types.d.ts +2 -1
  27. package/lib/cache/types.d.ts.map +1 -1
  28. package/lib/epoch/getRewardsAndPenalties.d.ts.map +1 -1
  29. package/lib/epoch/getRewardsAndPenalties.js +9 -2
  30. package/lib/epoch/getRewardsAndPenalties.js.map +1 -1
  31. package/lib/epoch/index.d.ts.map +1 -1
  32. package/lib/epoch/index.js +3 -1
  33. package/lib/epoch/index.js.map +1 -1
  34. package/lib/epoch/processEth1DataReset.d.ts +2 -2
  35. package/lib/epoch/processEth1DataReset.d.ts.map +1 -1
  36. package/lib/epoch/processEth1DataReset.js.map +1 -1
  37. package/lib/index.d.ts +1 -1
  38. package/lib/index.d.ts.map +1 -1
  39. package/lib/index.js.map +1 -1
  40. package/lib/rewards/attestationsRewards.d.ts.map +1 -1
  41. package/lib/rewards/attestationsRewards.js +10 -3
  42. package/lib/rewards/attestationsRewards.js.map +1 -1
  43. package/lib/slot/upgradeStateToHeze.d.ts.map +1 -1
  44. package/lib/slot/upgradeStateToHeze.js +4 -4
  45. package/lib/slot/upgradeStateToHeze.js.map +1 -1
  46. package/lib/stateView/beaconStateView.d.ts +1 -0
  47. package/lib/stateView/beaconStateView.d.ts.map +1 -1
  48. package/lib/stateView/beaconStateView.js +6 -0
  49. package/lib/stateView/beaconStateView.js.map +1 -1
  50. package/lib/stateView/interface.d.ts +4 -1
  51. package/lib/stateView/interface.d.ts.map +1 -1
  52. package/lib/stateView/interface.js.map +1 -1
  53. package/lib/stateView/nativeBeaconStateView.d.ts +2 -0
  54. package/lib/stateView/nativeBeaconStateView.d.ts.map +1 -1
  55. package/lib/stateView/nativeBeaconStateView.js +14 -0
  56. package/lib/stateView/nativeBeaconStateView.js.map +1 -1
  57. package/lib/types.d.ts +2 -2
  58. package/lib/types.d.ts.map +1 -1
  59. package/lib/util/altair.d.ts +3 -1
  60. package/lib/util/altair.d.ts.map +1 -1
  61. package/lib/util/altair.js +7 -4
  62. package/lib/util/altair.js.map +1 -1
  63. package/lib/util/blindedBlock.d.ts +1 -1
  64. package/lib/util/blindedBlock.d.ts.map +1 -1
  65. package/lib/util/blindedBlock.js +5 -2
  66. package/lib/util/blindedBlock.js.map +1 -1
  67. package/lib/util/clock.d.ts +5 -0
  68. package/lib/util/clock.d.ts.map +1 -1
  69. package/lib/util/clock.js +20 -17
  70. package/lib/util/clock.js.map +1 -1
  71. package/lib/util/deposit.d.ts +2 -2
  72. package/lib/util/deposit.d.ts.map +1 -1
  73. package/lib/util/deposit.js.map +1 -1
  74. package/lib/util/genesis.d.ts.map +1 -1
  75. package/lib/util/genesis.js +46 -23
  76. package/lib/util/genesis.js.map +1 -1
  77. package/lib/util/slot.d.ts +18 -2
  78. package/lib/util/slot.d.ts.map +1 -1
  79. package/lib/util/slot.js +70 -6
  80. package/lib/util/slot.js.map +1 -1
  81. package/lib/util/syncCommittee.d.ts +4 -3
  82. package/lib/util/syncCommittee.d.ts.map +1 -1
  83. package/lib/util/syncCommittee.js +9 -7
  84. package/lib/util/syncCommittee.js.map +1 -1
  85. package/lib/util/validator.d.ts +1 -1
  86. package/lib/util/validator.d.ts.map +1 -1
  87. package/lib/util/validator.js +32 -11
  88. package/lib/util/validator.js.map +1 -1
  89. package/lib/util/weakSubjectivity.d.ts.map +1 -1
  90. package/lib/util/weakSubjectivity.js +4 -3
  91. package/lib/util/weakSubjectivity.js.map +1 -1
  92. package/package.json +7 -7
  93. package/src/block/index.ts +4 -3
  94. package/src/block/processAttestationsAltair.ts +4 -1
  95. package/src/block/processDeposit.ts +7 -2
  96. package/src/block/processEth1Data.ts +3 -3
  97. package/src/block/processOperations.ts +14 -11
  98. package/src/cache/epochCache.ts +28 -4
  99. package/src/cache/epochTransitionCache.ts +2 -1
  100. package/src/cache/stateCache.ts +2 -0
  101. package/src/cache/types.ts +2 -1
  102. package/src/epoch/getRewardsAndPenalties.ts +11 -1
  103. package/src/epoch/index.ts +4 -1
  104. package/src/epoch/processEth1DataReset.ts +2 -2
  105. package/src/index.ts +2 -0
  106. package/src/rewards/attestationsRewards.ts +12 -1
  107. package/src/slot/upgradeStateToHeze.ts +6 -4
  108. package/src/stateView/beaconStateView.ts +9 -1
  109. package/src/stateView/interface.ts +4 -0
  110. package/src/stateView/nativeBeaconStateView.ts +14 -0
  111. package/src/types.ts +2 -0
  112. package/src/util/altair.ts +15 -4
  113. package/src/util/blindedBlock.ts +8 -3
  114. package/src/util/clock.ts +26 -17
  115. package/src/util/deposit.ts +2 -2
  116. package/src/util/genesis.ts +56 -30
  117. package/src/util/slot.ts +78 -8
  118. package/src/util/syncCommittee.ts +21 -9
  119. package/src/util/validator.ts +47 -20
  120. package/src/util/weakSubjectivity.ts +3 -3
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "bugs": {
12
12
  "url": "https://github.com/ChainSafe/lodestar/issues"
13
13
  },
14
- "version": "1.50.0",
14
+ "version": "1.51.0-dev.7d2ef8139c",
15
15
  "type": "module",
16
16
  "exports": {
17
17
  ".": {
@@ -69,14 +69,14 @@
69
69
  "@chainsafe/lodestar-z": "catalog:",
70
70
  "@chainsafe/persistent-merkle-tree": "^1.3.1",
71
71
  "@chainsafe/ssz": "^1.8.1",
72
- "@lodestar/config": "^1.50.0",
73
- "@lodestar/params": "^1.50.0",
74
- "@lodestar/types": "^1.50.0",
75
- "@lodestar/utils": "^1.50.0",
72
+ "@lodestar/config": "^1.51.0-dev.7d2ef8139c",
73
+ "@lodestar/params": "^1.51.0-dev.7d2ef8139c",
74
+ "@lodestar/types": "^1.51.0-dev.7d2ef8139c",
75
+ "@lodestar/utils": "^1.51.0-dev.7d2ef8139c",
76
76
  "@vekexasia/bigint-buffer2": "^1.1.1"
77
77
  },
78
78
  "devDependencies": {
79
- "@lodestar/test-utils": "^1.50.0"
79
+ "@lodestar/test-utils": "^1.51.0-dev.7d2ef8139c"
80
80
  },
81
81
  "keywords": [
82
82
  "ethereum",
@@ -84,5 +84,5 @@
84
84
  "beacon",
85
85
  "blockchain"
86
86
  ],
87
- "gitHead": "003bd66543fa65b5c1e954eec9e8077054c226dc"
87
+ "gitHead": "247d6fb7a7be86a876b60f2d48aa962ddd36ead0"
88
88
  }
@@ -1,4 +1,4 @@
1
- import {ForkPostGloas, ForkSeq} from "@lodestar/params";
1
+ import {ForkPostGloas, ForkPreHeze, ForkSeq} from "@lodestar/params";
2
2
  import {BeaconBlock, BlindedBeaconBlock, Slot, altair, capella} from "@lodestar/types";
3
3
  import {BeaconStateTransitionMetrics} from "../metrics.js";
4
4
  import {
@@ -6,6 +6,7 @@ import {
6
6
  CachedBeaconStateBellatrix,
7
7
  CachedBeaconStateCapella,
8
8
  CachedBeaconStateGloas,
9
+ CachedBeaconStatePreHeze,
9
10
  } from "../types.js";
10
11
  import {getFullOrBlindedPayload, isExecutionEnabled} from "../util/execution.js";
11
12
  import {BlockExternalData, DataAvailabilityStatus} from "./externalData.js";
@@ -117,9 +118,9 @@ export function processBlock(
117
118
  timer?.();
118
119
  }
119
120
 
120
- {
121
+ if (fork < ForkSeq.heze) {
121
122
  const timer = metrics?.processBlockStepTime.startTimer({step: BlockProcessStep.processEth1Data});
122
- processEth1Data(state, block.body.eth1Data);
123
+ processEth1Data(state as CachedBeaconStatePreHeze, (block as BeaconBlock<ForkPreHeze>).body.eth1Data);
123
124
  timer?.();
124
125
  }
125
126
 
@@ -141,7 +141,10 @@ export function processAttestationsAltair(
141
141
 
142
142
  // Do the discrete math inside the loop to ensure a deterministic result
143
143
  const totalIncrements = totalBalanceIncrementsWithWeight;
144
- const proposerRewardNumerator = totalIncrements * state.epochCtx.baseRewardPerIncrement;
144
+ // The proposer reward is priced at the attestation's target epoch
145
+ const proposerRewardNumerator =
146
+ totalIncrements *
147
+ (inCurrentEpoch ? state.epochCtx.baseRewardPerIncrement : state.epochCtx.baseRewardPerIncrementPrevEpoch);
145
148
  proposerReward += Math.floor(proposerRewardNumerator / PROPOSER_REWARD_DOMINATOR);
146
149
 
147
150
  if (fork >= ForkSeq.gloas) {
@@ -17,7 +17,12 @@ import {
17
17
  import {BLSPubkey, Bytes32, UintNum64, electra, phase0, ssz} from "@lodestar/types";
18
18
  import {verifyMerkleBranch} from "@lodestar/utils";
19
19
  import {ZERO_HASH} from "../constants/index.js";
20
- import {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStateElectra} from "../types.js";
20
+ import {
21
+ CachedBeaconStateAllForks,
22
+ CachedBeaconStateAltair,
23
+ CachedBeaconStateElectra,
24
+ CachedBeaconStatePreHeze,
25
+ } from "../types.js";
21
26
  import {
22
27
  computeDomain,
23
28
  computeSigningRoot,
@@ -33,7 +38,7 @@ import {
33
38
  *
34
39
  * PERF: Work depends on number of Deposit per block. On regular networks the average is 0 / block.
35
40
  */
36
- export function processDeposit(fork: ForkSeq, state: CachedBeaconStateAllForks, deposit: phase0.Deposit): void {
41
+ export function processDeposit(fork: ForkSeq, state: CachedBeaconStatePreHeze, deposit: phase0.Deposit): void {
37
42
  // verify the merkle branch
38
43
  if (
39
44
  !verifyMerkleBranch(
@@ -2,7 +2,7 @@ import {Node} from "@chainsafe/persistent-merkle-tree";
2
2
  import {CompositeViewDU} from "@chainsafe/ssz";
3
3
  import {EPOCHS_PER_ETH1_VOTING_PERIOD, SLOTS_PER_EPOCH} from "@lodestar/params";
4
4
  import {phase0, ssz} from "@lodestar/types";
5
- import {BeaconStateAllForks, CachedBeaconStateAllForks} from "../types.js";
5
+ import {BeaconStatePreHeze, CachedBeaconStatePreHeze} from "../types.js";
6
6
 
7
7
  /**
8
8
  * Store vote counts for every eth-execution block that has votes; if any eth-execution block wins majority support within a 1024-slot
@@ -12,7 +12,7 @@ import {BeaconStateAllForks, CachedBeaconStateAllForks} from "../types.js";
12
12
  * - Best case: Vote is already decided, zero work. See becomesNewEth1Data conditions
13
13
  * - Worst case: 1023 votes and no majority vote yet.
14
14
  */
15
- export function processEth1Data(state: CachedBeaconStateAllForks, eth1Data: phase0.Eth1Data): void {
15
+ export function processEth1Data(state: CachedBeaconStatePreHeze, eth1Data: phase0.Eth1Data): void {
16
16
  // Convert to view first to hash once and compare hashes
17
17
  const eth1DataView = ssz.phase0.Eth1Data.toViewDU(eth1Data);
18
18
 
@@ -28,7 +28,7 @@ export function processEth1Data(state: CachedBeaconStateAllForks, eth1Data: phas
28
28
  * result in a change to `state.eth1Data`.
29
29
  */
30
30
  export function becomesNewEth1Data(
31
- state: BeaconStateAllForks,
31
+ state: BeaconStatePreHeze,
32
32
  newEth1Data: CompositeViewDU<typeof ssz.phase0.Eth1Data>
33
33
  ): boolean {
34
34
  const SLOTS_PER_ETH1_VOTING_PERIOD = EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH;
@@ -1,4 +1,4 @@
1
- import {ForkSeq} from "@lodestar/params";
1
+ import {ForkPreHeze, ForkSeq} from "@lodestar/params";
2
2
  import {BeaconBlockBody, Slot, capella, electra, gloas} from "@lodestar/types";
3
3
  import {BeaconStateTransitionMetrics} from "../metrics.js";
4
4
  import {
@@ -6,6 +6,7 @@ import {
6
6
  CachedBeaconStateCapella,
7
7
  CachedBeaconStateElectra,
8
8
  CachedBeaconStateGloas,
9
+ CachedBeaconStatePreHeze,
9
10
  } from "../types.js";
10
11
  import {getEth1DepositCount} from "../util/deposit.js";
11
12
  import {processAttestations} from "./processAttestations.js";
@@ -40,13 +41,15 @@ export function processOperations(
40
41
  opts: ProcessBlockOpts = {verifySignatures: true},
41
42
  metrics?: BeaconStateTransitionMetrics | null
42
43
  ): void {
43
- // verify that outstanding deposits are processed up to the maximum number of deposits.
44
- // From Fulu the eth1 bridge deposit mechanism was removed, so blocks must not contain any deposits.
45
- const maxDeposits = fork >= ForkSeq.fulu ? 0 : getEth1DepositCount(state);
46
- if (body.deposits.length !== maxDeposits) {
47
- throw new Error(
48
- `Block contains incorrect number of deposits: depositCount=${body.deposits.length} expected=${maxDeposits}`
49
- );
44
+ if (fork < ForkSeq.heze) {
45
+ // From Fulu the eth1 bridge deposit mechanism was removed, so blocks must not contain any deposits.
46
+ const deposits = (body as BeaconBlockBody<ForkPreHeze>).deposits;
47
+ const maxDeposits = fork >= ForkSeq.fulu ? 0 : getEth1DepositCount(state as CachedBeaconStatePreHeze);
48
+ if (deposits.length !== maxDeposits) {
49
+ throw new Error(
50
+ `Block contains incorrect number of deposits: depositCount=${deposits.length} expected=${maxDeposits}`
51
+ );
52
+ }
50
53
  }
51
54
 
52
55
  {
@@ -71,10 +74,10 @@ export function processOperations(
71
74
  timer?.();
72
75
  }
73
76
 
74
- {
77
+ if (fork < ForkSeq.heze) {
75
78
  const timer = metrics?.processOperationsStepTime.startTimer({step: ProcessOperationsStep.processDeposit});
76
- for (const deposit of body.deposits) {
77
- processDeposit(fork, state, deposit);
79
+ for (const deposit of (body as BeaconBlockBody<ForkPreHeze>).deposits) {
80
+ processDeposit(fork, state as CachedBeaconStatePreHeze, deposit);
78
81
  }
79
82
  timer?.();
80
83
  }
@@ -178,6 +178,8 @@ export class EpochCache {
178
178
  * Update freq: once per epoch after `process_effective_balance_updates()`
179
179
  */
180
180
  baseRewardPerIncrement: number;
181
+ /** As baseRewardPerIncrement but priced at the previous epoch's slot duration */
182
+ baseRewardPerIncrementPrevEpoch: number;
181
183
  /**
182
184
  * Total active balance for current epoch, to be used instead of getTotalBalance()
183
185
  */
@@ -269,6 +271,7 @@ export class EpochCache {
269
271
  syncParticipantReward: number;
270
272
  syncProposerReward: number;
271
273
  baseRewardPerIncrement: number;
274
+ baseRewardPerIncrementPrevEpoch: number;
272
275
  totalActiveBalanceIncrements: number;
273
276
  churnLimit: number;
274
277
  activationChurnLimit: number;
@@ -302,6 +305,7 @@ export class EpochCache {
302
305
  this.syncParticipantReward = data.syncParticipantReward;
303
306
  this.syncProposerReward = data.syncProposerReward;
304
307
  this.baseRewardPerIncrement = data.baseRewardPerIncrement;
308
+ this.baseRewardPerIncrementPrevEpoch = data.baseRewardPerIncrementPrevEpoch;
305
309
  this.totalActiveBalanceIncrements = data.totalActiveBalanceIncrements;
306
310
  this.churnLimit = data.churnLimit;
307
311
  this.activationChurnLimit = data.activationChurnLimit;
@@ -452,9 +456,14 @@ export class EpochCache {
452
456
 
453
457
  // Values syncParticipantReward, syncProposerReward, baseRewardPerIncrement are only used after altair.
454
458
  // However, since they are very cheap to compute they are computed always to simplify upgradeState function.
455
- const syncParticipantReward = computeSyncParticipantReward(totalActiveBalanceIncrements);
459
+ const syncParticipantReward = computeSyncParticipantReward(config, currentEpoch, totalActiveBalanceIncrements);
456
460
  const syncProposerReward = Math.floor(syncParticipantReward * PROPOSER_WEIGHT_FACTOR);
457
- const baseRewardPerIncrement = computeBaseRewardPerIncrement(totalActiveBalanceIncrements);
461
+ const baseRewardPerIncrement = computeBaseRewardPerIncrement(config, currentEpoch, totalActiveBalanceIncrements);
462
+ const baseRewardPerIncrementPrevEpoch = computeBaseRewardPerIncrement(
463
+ config,
464
+ Math.max(currentEpoch - 1, GENESIS_EPOCH),
465
+ totalActiveBalanceIncrements
466
+ );
458
467
 
459
468
  let currentSyncCommitteeIndexed: SyncCommitteeCache;
460
469
  let nextSyncCommitteeIndexed: SyncCommitteeCache;
@@ -542,6 +551,7 @@ export class EpochCache {
542
551
  syncParticipantReward,
543
552
  syncProposerReward,
544
553
  baseRewardPerIncrement,
554
+ baseRewardPerIncrementPrevEpoch,
545
555
  totalActiveBalanceIncrements,
546
556
  churnLimit,
547
557
  activationChurnLimit,
@@ -591,6 +601,7 @@ export class EpochCache {
591
601
  syncParticipantReward: this.syncParticipantReward,
592
602
  syncProposerReward: this.syncProposerReward,
593
603
  baseRewardPerIncrement: this.baseRewardPerIncrement,
604
+ baseRewardPerIncrementPrevEpoch: this.baseRewardPerIncrementPrevEpoch,
594
605
  totalActiveBalanceIncrements: this.totalActiveBalanceIncrements,
595
606
  churnLimit: this.churnLimit,
596
607
  activationChurnLimit: this.activationChurnLimit,
@@ -685,9 +696,22 @@ export class EpochCache {
685
696
 
686
697
  this.totalActiveBalanceIncrements = epochTransitionCache.nextEpochTotalActiveBalanceByIncrement;
687
698
  if (upcomingEpoch >= this.config.ALTAIR_FORK_EPOCH) {
688
- this.syncParticipantReward = computeSyncParticipantReward(this.totalActiveBalanceIncrements);
699
+ this.syncParticipantReward = computeSyncParticipantReward(
700
+ this.config,
701
+ upcomingEpoch,
702
+ this.totalActiveBalanceIncrements
703
+ );
689
704
  this.syncProposerReward = Math.floor(this.syncParticipantReward * PROPOSER_WEIGHT_FACTOR);
690
- this.baseRewardPerIncrement = computeBaseRewardPerIncrement(this.totalActiveBalanceIncrements);
705
+ this.baseRewardPerIncrement = computeBaseRewardPerIncrement(
706
+ this.config,
707
+ upcomingEpoch,
708
+ this.totalActiveBalanceIncrements
709
+ );
710
+ this.baseRewardPerIncrementPrevEpoch = computeBaseRewardPerIncrement(
711
+ this.config,
712
+ Math.max(upcomingEpoch - 1, GENESIS_EPOCH),
713
+ this.totalActiveBalanceIncrements
714
+ );
691
715
  }
692
716
 
693
717
  this.previousTargetUnslashedBalanceIncrements = this.currentTargetUnslashedBalanceIncrements;
@@ -384,7 +384,8 @@ export function beforeProcessEpoch(
384
384
  }
385
385
 
386
386
  // SPEC: function getBaseRewardPerIncrement()
387
- const baseRewardPerIncrement = computeBaseRewardPerIncrement(totalActiveStakeByIncrement);
387
+ // Flag deltas pay for the previous epoch, so they are priced at its slot duration
388
+ const baseRewardPerIncrement = computeBaseRewardPerIncrement(config, prevEpoch, totalActiveStakeByIncrement);
388
389
 
389
390
  // To optimize process_registry_updates():
390
391
  // order by sequence of activationEligibilityEpoch setting and then index
@@ -15,6 +15,7 @@ import {
15
15
  BeaconStateGloas,
16
16
  BeaconStateHeze,
17
17
  BeaconStatePhase0,
18
+ BeaconStatePreHeze,
18
19
  } from "./types.js";
19
20
 
20
21
  export type BeaconStateCache = {
@@ -142,6 +143,7 @@ export type CachedBeaconStateHeze = CachedBeaconState<BeaconStateHeze>;
142
143
 
143
144
  export type CachedBeaconStateAllForks = CachedBeaconState<BeaconStateAllForks>;
144
145
  export type CachedBeaconStateExecutions = CachedBeaconState<BeaconStateExecutions>;
146
+ export type CachedBeaconStatePreHeze = CachedBeaconState<BeaconStatePreHeze>;
145
147
  /**
146
148
  * Create CachedBeaconState computing a new EpochCache instance
147
149
  * TODO ELECTRA: rename this to createFinalizedCachedBeaconState() as it's intended for finalized state only
@@ -1,5 +1,5 @@
1
1
  import {CompositeViewDU} from "@chainsafe/ssz";
2
- import {ForkAll, ForkName, ForkPostBellatrix, ForkPreGloas} from "@lodestar/params";
2
+ import {ForkAll, ForkName, ForkPostBellatrix, ForkPreGloas, ForkPreHeze} from "@lodestar/params";
3
3
  import {Epoch, RootHex, SSZTypesFor} from "@lodestar/types";
4
4
  import {EpochShuffling} from "../util/epochShuffling.js";
5
5
 
@@ -15,5 +15,6 @@ export type BeaconStateHeze = CompositeViewDU<SSZTypesFor<ForkName.heze, "Beacon
15
15
 
16
16
  export type BeaconStateAllForks = CompositeViewDU<SSZTypesFor<ForkAll, "BeaconState">>;
17
17
  export type BeaconStateExecutions = CompositeViewDU<SSZTypesFor<ForkPostBellatrix & ForkPreGloas, "BeaconState">>;
18
+ export type BeaconStatePreHeze = CompositeViewDU<SSZTypesFor<ForkPreHeze, "BeaconState">>;
18
19
 
19
20
  export type ShufflingGetter = (shufflingEpoch: Epoch, dependentRoot: RootHex) => EpochShuffling | null;
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  EFFECTIVE_BALANCE_INCREMENT,
3
3
  ForkSeq,
4
+ GENESIS_EPOCH,
4
5
  INACTIVITY_PENALTY_QUOTIENT_ALTAIR,
5
6
  INACTIVITY_PENALTY_QUOTIENT_BELLATRIX,
6
7
  PARTICIPATION_FLAG_WEIGHTS,
@@ -18,6 +19,7 @@ import {
18
19
  hasMarkers,
19
20
  } from "../util/attesterStatus.js";
20
21
  import {isInInactivityLeak} from "../util/index.js";
22
+ import {getSlotDurationMsAtEpoch} from "../util/slot.js";
21
23
 
22
24
  type RewardPenaltyItem = {
23
25
  baseReward: number;
@@ -67,7 +69,15 @@ export function getRewardsAndPenaltiesAltair(
67
69
 
68
70
  const inactivityPenalityMultiplier =
69
71
  fork === ForkSeq.altair ? INACTIVITY_PENALTY_QUOTIENT_ALTAIR : INACTIVITY_PENALTY_QUOTIENT_BELLATRIX;
70
- const penaltyDenominator = config.INACTIVITY_SCORE_BIAS * inactivityPenalityMultiplier;
72
+ let penaltyDenominator = config.INACTIVITY_SCORE_BIAS * inactivityPenalityMultiplier;
73
+ // The penalty pays for the previous epoch and scales with the square of its slot duration
74
+ const genesisSlotDurationMs = getSlotDurationMsAtEpoch(config, GENESIS_EPOCH);
75
+ const prevEpochSlotDurationMs = getSlotDurationMsAtEpoch(config, Math.max(epochCtx.epoch - 1, GENESIS_EPOCH));
76
+ if (prevEpochSlotDurationMs !== genesisSlotDurationMs) {
77
+ penaltyDenominator = Number(
78
+ (BigInt(penaltyDenominator) * BigInt(genesisSlotDurationMs) ** 2n) / BigInt(prevEpochSlotDurationMs) ** 2n
79
+ );
80
+ }
71
81
 
72
82
  const {flags} = cache;
73
83
  for (let i = 0; i < flags.length; i++) {
@@ -14,6 +14,7 @@ import {
14
14
  CachedBeaconStateFulu,
15
15
  CachedBeaconStateGloas,
16
16
  CachedBeaconStatePhase0,
17
+ CachedBeaconStatePreHeze,
17
18
  EpochTransitionCache,
18
19
  } from "../types.js";
19
20
  import {processBuilderPendingPayments} from "./processBuilderPendingPayments.js";
@@ -140,7 +141,9 @@ export function processEpoch(
140
141
  timer?.();
141
142
  }
142
143
 
143
- processEth1DataReset(state, cache);
144
+ if (fork < ForkSeq.heze) {
145
+ processEth1DataReset(state as CachedBeaconStatePreHeze, cache);
146
+ }
144
147
 
145
148
  if (fork >= ForkSeq.electra) {
146
149
  const stateElectra = state as CachedBeaconStateElectra;
@@ -1,13 +1,13 @@
1
1
  import {EPOCHS_PER_ETH1_VOTING_PERIOD} from "@lodestar/params";
2
2
  import {ssz} from "@lodestar/types";
3
- import {CachedBeaconStateAllForks, EpochTransitionCache} from "../types.js";
3
+ import {CachedBeaconStatePreHeze, EpochTransitionCache} from "../types.js";
4
4
 
5
5
  /**
6
6
  * Reset eth1DataVotes tree every `EPOCHS_PER_ETH1_VOTING_PERIOD`.
7
7
  *
8
8
  * PERF: Almost no (constant) cost
9
9
  */
10
- export function processEth1DataReset(state: CachedBeaconStateAllForks, cache: EpochTransitionCache): void {
10
+ export function processEth1DataReset(state: CachedBeaconStatePreHeze, cache: EpochTransitionCache): void {
11
11
  const nextEpoch = cache.currentEpoch + 1;
12
12
 
13
13
  // reset eth1 data votes
package/src/index.ts CHANGED
@@ -80,6 +80,7 @@ export type {
80
80
  BeaconStateHeze,
81
81
  // Non-cached states
82
82
  BeaconStatePhase0,
83
+ BeaconStatePreHeze,
83
84
  CachedBeaconStateAllForks,
84
85
  CachedBeaconStateAltair,
85
86
  CachedBeaconStateBellatrix,
@@ -91,5 +92,6 @@ export type {
91
92
  CachedBeaconStateGloas,
92
93
  CachedBeaconStateHeze,
93
94
  CachedBeaconStatePhase0,
95
+ CachedBeaconStatePreHeze,
94
96
  } from "./types.js";
95
97
  export * from "./util/index.js";
@@ -4,6 +4,7 @@ import {
4
4
  EFFECTIVE_BALANCE_INCREMENT,
5
5
  ForkName,
6
6
  ForkSeq,
7
+ GENESIS_EPOCH,
7
8
  INACTIVITY_PENALTY_QUOTIENT_ALTAIR,
8
9
  INACTIVITY_PENALTY_QUOTIENT_BELLATRIX,
9
10
  MAX_EFFECTIVE_BALANCE,
@@ -26,6 +27,7 @@ import {
26
27
  FLAG_PREV_HEAD_ATTESTER_UNSLASHED,
27
28
  FLAG_PREV_SOURCE_ATTESTER_UNSLASHED,
28
29
  FLAG_PREV_TARGET_ATTESTER_UNSLASHED,
30
+ getSlotDurationMsAtEpoch,
29
31
  hasMarkers,
30
32
  isInInactivityLeak,
31
33
  } from "../util/index.js";
@@ -168,7 +170,16 @@ function computeTotalAttestationsRewardsAltair(
168
170
  config.getForkSeq(state.slot) === ForkSeq.altair
169
171
  ? INACTIVITY_PENALTY_QUOTIENT_ALTAIR
170
172
  : INACTIVITY_PENALTY_QUOTIENT_BELLATRIX;
171
- const inactivityPenaltyDenominator = config.INACTIVITY_SCORE_BIAS * inactivityPenaltyQuotient;
173
+ let inactivityPenaltyDenominator = config.INACTIVITY_SCORE_BIAS * inactivityPenaltyQuotient;
174
+ // The penalty pays for the previous epoch and scales with the square of its slot duration
175
+ const genesisSlotDurationMs = getSlotDurationMsAtEpoch(config, GENESIS_EPOCH);
176
+ const prevEpochSlotDurationMs = getSlotDurationMsAtEpoch(config, Math.max(epochCtx.epoch - 1, GENESIS_EPOCH));
177
+ if (prevEpochSlotDurationMs !== genesisSlotDurationMs) {
178
+ inactivityPenaltyDenominator = Number(
179
+ (BigInt(inactivityPenaltyDenominator) * BigInt(genesisSlotDurationMs) ** 2n) /
180
+ BigInt(prevEpochSlotDurationMs) ** 2n
181
+ );
182
+ }
172
183
 
173
184
  for (let i = 0; i < flags.length; i++) {
174
185
  if (validatorIndices.length && !validatorIndices.includes(i)) {
@@ -7,6 +7,12 @@ import {CachedBeaconStateGloas, CachedBeaconStateHeze} from "../types.js";
7
7
  * Spec: heze/fork.md `upgrade_to_heze`.
8
8
  */
9
9
  export function upgradeStateToHeze(stateGloas: CachedBeaconStateGloas): CachedBeaconStateHeze {
10
+ // In Heze, the old deposit mechanism must be fully drained before the fork
11
+ if (BigInt(stateGloas.eth1DepositIndex) !== stateGloas.depositRequestsStartIndex) {
12
+ throw new Error(
13
+ `Cannot upgrade to Heze: legacy deposit mechanism still active eth1DepositIndex=${stateGloas.eth1DepositIndex} depositRequestsStartIndex=${stateGloas.depositRequestsStartIndex}`
14
+ );
15
+ }
10
16
  const {config} = stateGloas;
11
17
 
12
18
  ssz.gloas.BeaconState.commitViewDU(stateGloas);
@@ -26,9 +32,6 @@ export function upgradeStateToHeze(stateGloas: CachedBeaconStateGloas): CachedBe
26
32
  stateHezeView.blockRoots = stateHezeCloned.blockRoots;
27
33
  stateHezeView.stateRoots = stateHezeCloned.stateRoots;
28
34
  stateHezeView.historicalRoots = stateHezeCloned.historicalRoots;
29
- stateHezeView.eth1Data = stateHezeCloned.eth1Data;
30
- stateHezeView.eth1DataVotes = stateHezeCloned.eth1DataVotes;
31
- stateHezeView.eth1DepositIndex = stateHezeCloned.eth1DepositIndex;
32
35
  stateHezeView.validators = stateHezeCloned.validators;
33
36
  stateHezeView.balances = stateHezeCloned.balances;
34
37
  stateHezeView.randaoMixes = stateHezeCloned.randaoMixes;
@@ -46,7 +49,6 @@ export function upgradeStateToHeze(stateGloas: CachedBeaconStateGloas): CachedBe
46
49
  stateHezeView.nextWithdrawalIndex = stateHezeCloned.nextWithdrawalIndex;
47
50
  stateHezeView.nextWithdrawalValidatorIndex = stateHezeCloned.nextWithdrawalValidatorIndex;
48
51
  stateHezeView.historicalSummaries = stateHezeCloned.historicalSummaries;
49
- stateHezeView.depositRequestsStartIndex = stateHezeCloned.depositRequestsStartIndex;
50
52
  stateHezeView.depositBalanceToConsume = stateHezeCloned.depositBalanceToConsume;
51
53
  stateHezeView.exitBalanceToConsume = stateHezeCloned.exitBalanceToConsume;
52
54
  stateHezeView.earliestExitEpoch = stateHezeCloned.earliestExitEpoch;
@@ -40,6 +40,7 @@ import {
40
40
  CachedBeaconStateExecutions,
41
41
  CachedBeaconStateFulu,
42
42
  CachedBeaconStateGloas,
43
+ CachedBeaconStatePreHeze,
43
44
  createCachedBeaconState,
44
45
  isStateValidatorsNodesPopulated,
45
46
  } from "../cache/stateCache.js";
@@ -145,7 +146,10 @@ export class BeaconStateView implements IBeaconStateViewLatestFork {
145
146
  }
146
147
 
147
148
  get eth1Data(): phase0.Eth1Data {
148
- return this.cachedState.eth1Data;
149
+ if (this.forkSeq >= ForkSeq.heze) {
150
+ throw new Error("eth1Data was removed from the beacon state in Heze");
151
+ }
152
+ return (this.cachedState as CachedBeaconStatePreHeze).eth1Data;
149
153
  }
150
154
 
151
155
  get latestBlockHeader(): phase0.BeaconBlockHeader {
@@ -611,6 +615,10 @@ export class BeaconStateView implements IBeaconStateViewLatestFork {
611
615
  return this.cachedState.epochCtx.currentSyncCommitteeIndexed;
612
616
  }
613
617
 
618
+ get currentSyncCommitteeValidatorIndices(): Uint32Array {
619
+ return this.cachedState.epochCtx.currentSyncCommitteeIndexed.validatorIndices;
620
+ }
621
+
614
622
  get syncProposerReward(): number {
615
623
  return this.cachedState.epochCtx.syncProposerReward;
616
624
  }
@@ -207,6 +207,7 @@ export interface IBeaconStateViewAltair extends IBeaconStateView {
207
207
  currentSyncCommittee: altair.SyncCommittee;
208
208
  nextSyncCommittee: altair.SyncCommittee;
209
209
  currentSyncCommitteeIndexed: SyncCommitteeCache;
210
+ readonly currentSyncCommitteeValidatorIndices: Uint32Array;
210
211
  syncProposerReward: number;
211
212
  getIndexedSyncCommitteeAtEpoch(epoch: Epoch): SyncCommitteeCache;
212
213
  /** Get indexed sync committee with slot+1 offset for duty lookups */
@@ -341,6 +342,7 @@ export type IBeaconStateViewNative = Omit<
341
342
  | "computeBlockRewards"
342
343
  | "computeNewStateRoot"
343
344
  | "computeSyncCommitteeRewards"
345
+ | "currentSyncCommitteeValidatorIndices"
344
346
  | "eth1Data"
345
347
  | "executionPayloadAvailability"
346
348
  | "getBeaconCommittee"
@@ -358,6 +360,8 @@ export type IBeaconStateViewNative = Omit<
358
360
  | "withParentPayloadApplied"
359
361
  > & {
360
362
  pendingDeposits: Uint8Array;
363
+ /** Older native addons expose indices only through currentSyncCommitteeIndexed. */
364
+ readonly currentSyncCommitteeValidatorIndices?: Uint32Array;
361
365
  pendingPartialWithdrawals: Uint8Array;
362
366
  pendingConsolidations: Uint8Array;
363
367
  proposerLookahead: Uint32Array;
@@ -97,6 +97,7 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
97
97
  private cachedPreviousEpochParticipation: Uint8Array | null = null;
98
98
  private cachedCurrentEpochParticipation: Uint8Array | null = null;
99
99
  private cachedCurrentSyncCommitteeIndexed: SyncCommitteeCache | null = null;
100
+ private cachedCurrentSyncCommitteeValidatorIndices: Uint32Array | null = null;
100
101
  private cachedSyncProposerReward: number | null = null;
101
102
  // bellatrix
102
103
  private cachedLatestExecutionPayloadHeader: ExecutionPayloadHeader | null = null;
@@ -228,6 +229,9 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
228
229
  }
229
230
 
230
231
  get eth1Data(): phase0.Eth1Data {
232
+ if (this.forkSeq >= ForkSeq.heze) {
233
+ throw new Error("eth1Data was removed from the beacon state in Heze");
234
+ }
231
235
  if (this.cachedEth1Data === null) {
232
236
  this.cachedEth1Data = this.binding.eth1Data;
233
237
  }
@@ -719,6 +723,16 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
719
723
  return this.cachedCurrentSyncCommitteeIndexed;
720
724
  }
721
725
 
726
+ get currentSyncCommitteeValidatorIndices(): Uint32Array {
727
+ if (this.cachedCurrentSyncCommitteeValidatorIndices === null) {
728
+ this.cachedCurrentSyncCommitteeValidatorIndices =
729
+ this.cachedCurrentSyncCommitteeIndexed?.validatorIndices ??
730
+ this.binding.currentSyncCommitteeValidatorIndices ??
731
+ this.currentSyncCommitteeIndexed.validatorIndices;
732
+ }
733
+ return this.cachedCurrentSyncCommitteeValidatorIndices;
734
+ }
735
+
722
736
  get syncProposerReward(): number {
723
737
  if (this.cachedSyncProposerReward === null) {
724
738
  this.cachedSyncProposerReward = this.binding.syncProposerReward;
package/src/types.ts CHANGED
@@ -12,6 +12,7 @@ export type {
12
12
  CachedBeaconStateGloas,
13
13
  CachedBeaconStateHeze,
14
14
  CachedBeaconStatePhase0,
15
+ CachedBeaconStatePreHeze,
15
16
  } from "./cache/stateCache.js";
16
17
  export type {
17
18
  BeaconStateAllForks,
@@ -25,5 +26,6 @@ export type {
25
26
  BeaconStateGloas,
26
27
  BeaconStateHeze,
27
28
  BeaconStatePhase0,
29
+ BeaconStatePreHeze,
28
30
  ShufflingGetter,
29
31
  } from "./cache/types.js";
@@ -1,13 +1,24 @@
1
- import {BASE_REWARD_FACTOR, EFFECTIVE_BALANCE_INCREMENT} from "@lodestar/params";
1
+ import {ChainConfig} from "@lodestar/config";
2
+ import {BASE_REWARD_FACTOR, EFFECTIVE_BALANCE_INCREMENT, GENESIS_EPOCH} from "@lodestar/params";
3
+ import {Epoch} from "@lodestar/types";
2
4
  import {bigIntSqrt, bnToNum} from "@lodestar/utils";
5
+ import {getSlotDurationMsAtEpoch} from "./slot.js";
3
6
 
4
7
  /**
5
8
  * Before we manage bigIntSqrt(totalActiveStake) as BigInt and return BigInt.
6
9
  * bigIntSqrt(totalActiveStake) should fit a number (2 ** 53 -1 max)
7
10
  **/
8
- export function computeBaseRewardPerIncrement(totalActiveStakeByIncrement: number): number {
11
+ export function computeBaseRewardPerIncrement(
12
+ config: ChainConfig,
13
+ epoch: Epoch,
14
+ totalActiveStakeByIncrement: number
15
+ ): number {
16
+ // Divide last so the exact slot duration ratio applies
17
+ const numerator = Math.floor(
18
+ (EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR * getSlotDurationMsAtEpoch(config, epoch)) /
19
+ getSlotDurationMsAtEpoch(config, GENESIS_EPOCH)
20
+ );
9
21
  return Math.floor(
10
- (EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR) /
11
- bnToNum(bigIntSqrt(BigInt(totalActiveStakeByIncrement) * BigInt(EFFECTIVE_BALANCE_INCREMENT)))
22
+ numerator / bnToNum(bigIntSqrt(BigInt(totalActiveStakeByIncrement) * BigInt(EFFECTIVE_BALANCE_INCREMENT)))
12
23
  );
13
24
  }
@@ -3,10 +3,12 @@ import {
3
3
  ForkName,
4
4
  ForkPostBellatrix,
5
5
  ForkPostDeneb,
6
+ ForkPreDeneb,
6
7
  ForkPreGloas,
7
8
  ForkSeq,
8
9
  isForkPostBellatrix,
9
10
  isForkPostDeneb,
11
+ isForkPostGloas,
10
12
  } from "@lodestar/params";
11
13
  import {
12
14
  BeaconBlock,
@@ -128,7 +130,10 @@ export function reconstructSignedBlockContents(
128
130
  signedBlindedBlock: SignedBlindedBeaconBlock,
129
131
  executionPayload: ExecutionPayload | null,
130
132
  blobsBundle: BlobsBundle | null
131
- ): SignedBlockContents {
133
+ ): SignedBlockContents<ForkPreGloas> {
134
+ if (isForkPostGloas(fork)) {
135
+ throw Error("Blinded block reconstruction is not supported after Gloas");
136
+ }
132
137
  const signedBlock = signedBlindedBlockToFull(fork, signedBlindedBlock, executionPayload);
133
138
 
134
139
  if (isForkPostDeneb(fork)) {
@@ -136,10 +141,10 @@ export function reconstructSignedBlockContents(
136
141
  throw Error("Missing blobs bundle to reconstruct post-deneb block contents");
137
142
  }
138
143
  return {
139
- signedBlock: signedBlock as SignedBeaconBlock<ForkPostDeneb>,
144
+ signedBlock: signedBlock as SignedBeaconBlock<ForkPostDeneb & ForkPreGloas>,
140
145
  kzgProofs: blobsBundle.proofs,
141
146
  blobs: blobsBundle.blobs,
142
147
  };
143
148
  }
144
- return {signedBlock};
149
+ return {signedBlock: signedBlock as SignedBeaconBlock<ForkPreDeneb>};
145
150
  }