@lodestar/state-transition 1.39.0-dev.075956b855 → 1.39.0-dev.0bce7eb9f1

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 (91) hide show
  1. package/lib/block/isValidIndexedAttestation.d.ts +4 -5
  2. package/lib/block/isValidIndexedAttestation.d.ts.map +1 -1
  3. package/lib/block/isValidIndexedAttestation.js +9 -10
  4. package/lib/block/isValidIndexedAttestation.js.map +1 -1
  5. package/lib/block/processAttestationPhase0.d.ts.map +1 -1
  6. package/lib/block/processAttestationPhase0.js +1 -1
  7. package/lib/block/processAttestationPhase0.js.map +1 -1
  8. package/lib/block/processAttesterSlashing.d.ts +3 -2
  9. package/lib/block/processAttesterSlashing.d.ts.map +1 -1
  10. package/lib/block/processAttesterSlashing.js +3 -3
  11. package/lib/block/processAttesterSlashing.js.map +1 -1
  12. package/lib/block/processBlsToExecutionChange.d.ts +3 -1
  13. package/lib/block/processBlsToExecutionChange.d.ts.map +1 -1
  14. package/lib/block/processBlsToExecutionChange.js +7 -11
  15. package/lib/block/processBlsToExecutionChange.js.map +1 -1
  16. package/lib/block/processProposerSlashing.d.ts +5 -2
  17. package/lib/block/processProposerSlashing.d.ts.map +1 -1
  18. package/lib/block/processProposerSlashing.js +7 -5
  19. package/lib/block/processProposerSlashing.js.map +1 -1
  20. package/lib/block/processWithdrawals.d.ts +3 -3
  21. package/lib/block/processWithdrawals.d.ts.map +1 -1
  22. package/lib/block/processWithdrawals.js +152 -105
  23. package/lib/block/processWithdrawals.js.map +1 -1
  24. package/lib/cache/epochCache.d.ts +6 -12
  25. package/lib/cache/epochCache.d.ts.map +1 -1
  26. package/lib/cache/epochCache.js +33 -105
  27. package/lib/cache/epochCache.js.map +1 -1
  28. package/lib/cache/epochTransitionCache.d.ts +5 -12
  29. package/lib/cache/epochTransitionCache.d.ts.map +1 -1
  30. package/lib/cache/epochTransitionCache.js +4 -14
  31. package/lib/cache/epochTransitionCache.js.map +1 -1
  32. package/lib/cache/stateCache.d.ts.map +1 -1
  33. package/lib/cache/stateCache.js +1 -2
  34. package/lib/cache/stateCache.js.map +1 -1
  35. package/lib/epoch/processProposerLookahead.d.ts.map +1 -1
  36. package/lib/epoch/processProposerLookahead.js +3 -6
  37. package/lib/epoch/processProposerLookahead.js.map +1 -1
  38. package/lib/index.d.ts +1 -0
  39. package/lib/index.d.ts.map +1 -1
  40. package/lib/index.js +1 -0
  41. package/lib/index.js.map +1 -1
  42. package/lib/rewards/attestationsRewards.d.ts +6 -0
  43. package/lib/rewards/attestationsRewards.d.ts.map +1 -0
  44. package/lib/rewards/attestationsRewards.js +113 -0
  45. package/lib/rewards/attestationsRewards.js.map +1 -0
  46. package/lib/rewards/blockRewards.d.ts +14 -0
  47. package/lib/rewards/blockRewards.d.ts.map +1 -0
  48. package/lib/rewards/blockRewards.js +96 -0
  49. package/lib/rewards/blockRewards.js.map +1 -0
  50. package/lib/rewards/index.d.ts +4 -0
  51. package/lib/rewards/index.d.ts.map +1 -0
  52. package/lib/rewards/index.js +4 -0
  53. package/lib/rewards/index.js.map +1 -0
  54. package/lib/rewards/syncCommitteeRewards.d.ts +6 -0
  55. package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -0
  56. package/lib/rewards/syncCommitteeRewards.js +35 -0
  57. package/lib/rewards/syncCommitteeRewards.js.map +1 -0
  58. package/lib/signatureSets/blsToExecutionChange.d.ts +1 -2
  59. package/lib/signatureSets/blsToExecutionChange.d.ts.map +1 -1
  60. package/lib/signatureSets/blsToExecutionChange.js +2 -2
  61. package/lib/signatureSets/blsToExecutionChange.js.map +1 -1
  62. package/lib/signatureSets/proposer.d.ts +3 -4
  63. package/lib/signatureSets/proposer.d.ts.map +1 -1
  64. package/lib/signatureSets/proposer.js +5 -6
  65. package/lib/signatureSets/proposer.js.map +1 -1
  66. package/lib/types.d.ts +1 -1
  67. package/lib/types.d.ts.map +1 -1
  68. package/lib/util/epochShuffling.d.ts +1 -34
  69. package/lib/util/epochShuffling.d.ts.map +1 -1
  70. package/lib/util/epochShuffling.js +1 -1
  71. package/lib/util/epochShuffling.js.map +1 -1
  72. package/package.json +14 -11
  73. package/src/block/isValidIndexedAttestation.ts +17 -12
  74. package/src/block/processAttestationPhase0.ts +2 -1
  75. package/src/block/processAttesterSlashing.ts +16 -4
  76. package/src/block/processBlsToExecutionChange.ts +13 -14
  77. package/src/block/processProposerSlashing.ts +21 -11
  78. package/src/block/processWithdrawals.ts +230 -135
  79. package/src/cache/epochCache.ts +40 -118
  80. package/src/cache/epochTransitionCache.ts +9 -34
  81. package/src/cache/stateCache.ts +1 -2
  82. package/src/epoch/processProposerLookahead.ts +3 -7
  83. package/src/index.ts +1 -0
  84. package/src/rewards/attestationsRewards.ts +200 -0
  85. package/src/rewards/blockRewards.ts +150 -0
  86. package/src/rewards/index.ts +3 -0
  87. package/src/rewards/syncCommitteeRewards.ts +56 -0
  88. package/src/signatureSets/blsToExecutionChange.ts +2 -3
  89. package/src/signatureSets/proposer.ts +6 -7
  90. package/src/types.ts +1 -0
  91. package/src/util/epochShuffling.ts +2 -43
@@ -0,0 +1,150 @@
1
+ import {BeaconConfig} from "@lodestar/config";
2
+ import {
3
+ ForkName,
4
+ WHISTLEBLOWER_REWARD_QUOTIENT,
5
+ WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA,
6
+ isForkPostElectra,
7
+ } from "@lodestar/params";
8
+ import {BeaconBlock, altair, phase0, rewards} from "@lodestar/types";
9
+ import {processAttestationsAltair} from "../block/processAttestationsAltair.js";
10
+ import {RewardCache} from "../cache/rewardCache.js";
11
+ import {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStatePhase0} from "../cache/stateCache.js";
12
+ import {getAttesterSlashableIndices} from "../util/attestation.js";
13
+
14
+ type SubRewardValue = number; // All reward values should be integer
15
+
16
+ /**
17
+ * Calculate total proposer block rewards given block and the beacon state of the same slot before the block is applied (preState)
18
+ * postState can be passed in to read reward cache if available
19
+ * Standard (Non MEV) rewards for proposing a block consists of:
20
+ * 1) Including attestations from (beacon) committee
21
+ * 2) Including attestations from sync committee
22
+ * 3) Reporting slashable behaviours from proposer and attester
23
+ */
24
+ export async function computeBlockRewards(
25
+ config: BeaconConfig,
26
+ block: BeaconBlock,
27
+ preStateIn: CachedBeaconStateAllForks,
28
+ proposerRewards?: RewardCache
29
+ ): Promise<rewards.BlockRewards> {
30
+ const preState = preStateIn.clone();
31
+
32
+ const fork = config.getForkName(block.slot);
33
+ const {attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0} =
34
+ proposerRewards ?? {};
35
+ let blockAttestationReward = cachedAttestationsReward;
36
+ let syncAggregateReward = cachedSyncAggregateReward;
37
+
38
+ if (blockAttestationReward === 0) {
39
+ blockAttestationReward =
40
+ fork === ForkName.phase0
41
+ ? computeBlockAttestationRewardPhase0(block as phase0.BeaconBlock, preState as CachedBeaconStatePhase0)
42
+ : computeBlockAttestationRewardAltair(config, block as altair.BeaconBlock, preState as CachedBeaconStateAltair);
43
+ }
44
+
45
+ if (syncAggregateReward === 0) {
46
+ syncAggregateReward = computeSyncAggregateReward(block as altair.BeaconBlock, preState as CachedBeaconStateAltair);
47
+ }
48
+
49
+ const blockProposerSlashingReward = computeBlockProposerSlashingReward(fork, block, preState);
50
+ const blockAttesterSlashingReward = computeBlockAttesterSlashingReward(fork, block, preState);
51
+
52
+ const total =
53
+ blockAttestationReward + syncAggregateReward + blockProposerSlashingReward + blockAttesterSlashingReward;
54
+
55
+ return {
56
+ proposerIndex: block.proposerIndex,
57
+ total,
58
+ attestations: blockAttestationReward,
59
+ syncAggregate: syncAggregateReward,
60
+ proposerSlashings: blockProposerSlashingReward,
61
+ attesterSlashings: blockAttesterSlashingReward,
62
+ };
63
+ }
64
+
65
+ /**
66
+ * TODO: Calculate rewards received by block proposer for including attestations.
67
+ */
68
+ function computeBlockAttestationRewardPhase0(
69
+ _block: phase0.BeaconBlock,
70
+ _preState: CachedBeaconStatePhase0
71
+ ): SubRewardValue {
72
+ throw new Error("Unsupported fork! Block attestation reward calculation is not available in phase0");
73
+ }
74
+
75
+ /**
76
+ * Calculate rewards received by block proposer for including attestations since Altair.
77
+ * Reuses `processAttestationsAltair()`. Has dependency on RewardCache
78
+ */
79
+ function computeBlockAttestationRewardAltair(
80
+ config: BeaconConfig,
81
+ block: altair.BeaconBlock,
82
+ preState: CachedBeaconStateAltair
83
+ ): SubRewardValue {
84
+ const fork = config.getForkSeq(block.slot);
85
+ const {attestations} = block.body;
86
+
87
+ processAttestationsAltair(fork, preState, attestations, false);
88
+
89
+ return preState.proposerRewards.attestations;
90
+ }
91
+
92
+ function computeSyncAggregateReward(block: altair.BeaconBlock, preState: CachedBeaconStateAltair): SubRewardValue {
93
+ if (block.body.syncAggregate !== undefined) {
94
+ const {syncCommitteeBits} = block.body.syncAggregate;
95
+ const {syncProposerReward} = preState.epochCtx;
96
+
97
+ return syncCommitteeBits.getTrueBitIndexes().length * Math.floor(syncProposerReward); // syncProposerReward should already be integer
98
+ }
99
+
100
+ return 0; // phase0 block does not have syncAggregate
101
+ }
102
+
103
+ /**
104
+ * Calculate rewards received by block proposer for including proposer slashings.
105
+ * All proposer slashing rewards go to block proposer and none to whistleblower as of Deneb
106
+ */
107
+ function computeBlockProposerSlashingReward(
108
+ fork: ForkName,
109
+ block: BeaconBlock,
110
+ state: CachedBeaconStateAllForks
111
+ ): SubRewardValue {
112
+ let proposerSlashingReward = 0;
113
+
114
+ for (const proposerSlashing of block.body.proposerSlashings) {
115
+ const offendingProposerIndex = proposerSlashing.signedHeader1.message.proposerIndex;
116
+ const offendingProposerBalance = state.validators.getReadonly(offendingProposerIndex).effectiveBalance;
117
+ const whistleblowerRewardQuotient = isForkPostElectra(fork)
118
+ ? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
119
+ : WHISTLEBLOWER_REWARD_QUOTIENT;
120
+
121
+ proposerSlashingReward += Math.floor(offendingProposerBalance / whistleblowerRewardQuotient);
122
+ }
123
+
124
+ return proposerSlashingReward;
125
+ }
126
+
127
+ /**
128
+ * Calculate rewards received by block proposer for including attester slashings.
129
+ * All attester slashing rewards go to block proposer and none to whistleblower as of Deneb
130
+ */
131
+ function computeBlockAttesterSlashingReward(
132
+ fork: ForkName,
133
+ block: BeaconBlock,
134
+ preState: CachedBeaconStateAllForks
135
+ ): SubRewardValue {
136
+ let attesterSlashingReward = 0;
137
+
138
+ for (const attesterSlashing of block.body.attesterSlashings) {
139
+ for (const offendingAttesterIndex of getAttesterSlashableIndices(attesterSlashing)) {
140
+ const offendingAttesterBalance = preState.validators.getReadonly(offendingAttesterIndex).effectiveBalance;
141
+ const whistleblowerRewardQuotient = isForkPostElectra(fork)
142
+ ? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
143
+ : WHISTLEBLOWER_REWARD_QUOTIENT;
144
+
145
+ attesterSlashingReward += Math.floor(offendingAttesterBalance / whistleblowerRewardQuotient);
146
+ }
147
+ }
148
+
149
+ return attesterSlashingReward;
150
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./attestationsRewards.js";
2
+ export * from "./blockRewards.js";
3
+ export * from "./syncCommitteeRewards.js";
@@ -0,0 +1,56 @@
1
+ import {BeaconConfig} from "@lodestar/config";
2
+ import {ForkName, SYNC_COMMITTEE_SIZE} from "@lodestar/params";
3
+ import {BeaconBlock, ValidatorIndex, altair, rewards} from "@lodestar/types";
4
+ import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
5
+ import {CachedBeaconStateAllForks, CachedBeaconStateAltair} from "../cache/stateCache.js";
6
+
7
+ export async function computeSyncCommitteeRewards(
8
+ config: BeaconConfig,
9
+ index2pubkey: Index2PubkeyCache,
10
+ block: BeaconBlock,
11
+ preState: CachedBeaconStateAllForks,
12
+ validatorIds: (ValidatorIndex | string)[] = []
13
+ ): Promise<rewards.SyncCommitteeRewards> {
14
+ const fork = config.getForkName(block.slot);
15
+ if (fork === ForkName.phase0) {
16
+ throw Error("Cannot get sync rewards as phase0 block does not have sync committee");
17
+ }
18
+
19
+ const altairBlock = block as altair.BeaconBlock;
20
+ const preStateAltair = preState.clone() as CachedBeaconStateAltair;
21
+
22
+ // Bound syncCommitteeValidatorIndices in case it goes beyond SYNC_COMMITTEE_SIZE just to be safe
23
+ const syncCommitteeValidatorIndices = preStateAltair.epochCtx.currentSyncCommitteeIndexed.validatorIndices.slice(
24
+ 0,
25
+ SYNC_COMMITTEE_SIZE
26
+ );
27
+ const {syncParticipantReward} = preStateAltair.epochCtx;
28
+ const {syncCommitteeBits} = altairBlock.body.syncAggregate;
29
+
30
+ // Track reward deltas per validator (can appear multiple times in sync committee)
31
+ const rewardDeltas: Map<ValidatorIndex, number> = new Map();
32
+
33
+ // Iterate by position index to correctly access syncCommitteeBits
34
+ for (let i = 0; i < syncCommitteeValidatorIndices.length; i++) {
35
+ const validatorIndex = syncCommitteeValidatorIndices[i];
36
+ const currentDelta = rewardDeltas.get(validatorIndex) ?? 0;
37
+ if (syncCommitteeBits.get(i)) {
38
+ // Positive rewards for participants
39
+ rewardDeltas.set(validatorIndex, currentDelta + syncParticipantReward);
40
+ } else {
41
+ // Negative rewards for non participants
42
+ rewardDeltas.set(validatorIndex, currentDelta - syncParticipantReward);
43
+ }
44
+ }
45
+
46
+ const rewards = Array.from(rewardDeltas, ([validatorIndex, reward]) => ({validatorIndex, reward}));
47
+
48
+ if (validatorIds.length) {
49
+ const filtersSet = new Set(validatorIds);
50
+ return rewards.filter(
51
+ (reward) => filtersSet.has(reward.validatorIndex) || filtersSet.has(index2pubkey[reward.validatorIndex].toHex())
52
+ );
53
+ }
54
+
55
+ return rewards;
56
+ }
@@ -2,14 +2,13 @@ import {PublicKey} from "@chainsafe/blst";
2
2
  import {BeaconConfig} from "@lodestar/config";
3
3
  import {DOMAIN_BLS_TO_EXECUTION_CHANGE, ForkName} from "@lodestar/params";
4
4
  import {capella, ssz} from "@lodestar/types";
5
- import {CachedBeaconStateAllForks} from "../types.js";
6
5
  import {ISignatureSet, SignatureSetType, computeSigningRoot, verifySignatureSet} from "../util/index.js";
7
6
 
8
7
  export function verifyBlsToExecutionChangeSignature(
9
- state: CachedBeaconStateAllForks,
8
+ config: BeaconConfig,
10
9
  signedBLSToExecutionChange: capella.SignedBLSToExecutionChange
11
10
  ): boolean {
12
- return verifySignatureSet(getBlsToExecutionChangeSignatureSet(state.config, signedBLSToExecutionChange));
11
+ return verifySignatureSet(getBlsToExecutionChangeSignatureSet(config, signedBLSToExecutionChange));
13
12
  }
14
13
 
15
14
  /**
@@ -2,7 +2,6 @@ import {BeaconConfig} from "@lodestar/config";
2
2
  import {DOMAIN_BEACON_PROPOSER} from "@lodestar/params";
3
3
  import {SignedBeaconBlock, SignedBlindedBeaconBlock, Slot, isBlindedBeaconBlock, phase0, ssz} from "@lodestar/types";
4
4
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
5
- import {CachedBeaconStateAllForks} from "../types.js";
6
5
  import {computeSigningRoot} from "../util/index.js";
7
6
  import {ISignatureSet, SignatureSetType, verifySignatureSet} from "../util/signatureSets.js";
8
7
 
@@ -38,28 +37,28 @@ export function getBlockProposerSignatureSet(
38
37
  }
39
38
 
40
39
  export function getBlockHeaderProposerSignatureSetByParentStateSlot(
40
+ config: BeaconConfig,
41
41
  index2pubkey: Index2PubkeyCache,
42
- parentState: CachedBeaconStateAllForks,
42
+ parentStateSlot: Slot,
43
43
  signedBlockHeader: phase0.SignedBeaconBlockHeader
44
44
  ) {
45
- return getBlockHeaderProposerSignatureSet(index2pubkey, parentState, signedBlockHeader, parentState.slot);
45
+ return getBlockHeaderProposerSignatureSet(config, index2pubkey, signedBlockHeader, parentStateSlot);
46
46
  }
47
47
 
48
48
  export function getBlockHeaderProposerSignatureSetByHeaderSlot(
49
+ config: BeaconConfig,
49
50
  index2pubkey: Index2PubkeyCache,
50
- headState: CachedBeaconStateAllForks,
51
51
  signedBlockHeader: phase0.SignedBeaconBlockHeader
52
52
  ) {
53
- return getBlockHeaderProposerSignatureSet(index2pubkey, headState, signedBlockHeader, signedBlockHeader.message.slot);
53
+ return getBlockHeaderProposerSignatureSet(config, index2pubkey, signedBlockHeader, signedBlockHeader.message.slot);
54
54
  }
55
55
 
56
56
  function getBlockHeaderProposerSignatureSet(
57
+ config: BeaconConfig,
57
58
  index2pubkey: Index2PubkeyCache,
58
- state: CachedBeaconStateAllForks,
59
59
  signedBlockHeader: phase0.SignedBeaconBlockHeader,
60
60
  domainSlot: Slot
61
61
  ): ISignatureSet {
62
- const {config} = state;
63
62
  const domain = config.getDomain(domainSlot, DOMAIN_BEACON_PROPOSER, signedBlockHeader.message.slot);
64
63
 
65
64
  return {
package/src/types.ts CHANGED
@@ -23,4 +23,5 @@ export type {
23
23
  BeaconStateFulu,
24
24
  BeaconStateGloas,
25
25
  BeaconStatePhase0,
26
+ ShufflingGetter,
26
27
  } from "./cache/types.js";
@@ -9,54 +9,13 @@ import {
9
9
  TARGET_COMMITTEE_SIZE,
10
10
  } from "@lodestar/params";
11
11
  import {Epoch, RootHex, ValidatorIndex, ssz} from "@lodestar/types";
12
- import {GaugeExtra, Logger, NoLabels, intDiv, toRootHex} from "@lodestar/utils";
12
+ import {intDiv, toRootHex} from "@lodestar/utils";
13
13
  import {BeaconStateAllForks} from "../types.js";
14
14
  import {getBlockRootAtSlot} from "./blockRoot.js";
15
15
  import {computeAnchorCheckpoint} from "./computeAnchorCheckpoint.js";
16
16
  import {computeStartSlotAtEpoch} from "./epoch.js";
17
17
  import {getSeed} from "./seed.js";
18
18
 
19
- export interface ShufflingBuildProps {
20
- state: BeaconStateAllForks;
21
- activeIndices: Uint32Array;
22
- }
23
-
24
- export interface PublicShufflingCacheMetrics {
25
- shufflingCache: {
26
- nextShufflingNotOnEpochCache: GaugeExtra<NoLabels>;
27
- };
28
- }
29
- export interface IShufflingCache {
30
- metrics: PublicShufflingCacheMetrics | null;
31
- logger: Logger | null;
32
- /**
33
- * Gets a cached shuffling via the epoch and decision root. If the state and
34
- * activeIndices are passed and a shuffling is not available it will be built
35
- * synchronously. If the state is not passed and the shuffling is not available
36
- * nothing will be returned.
37
- *
38
- * NOTE: If a shuffling is already queued and not calculated it will build and resolve
39
- * the promise but the already queued build will happen at some later time
40
- */
41
- getSync<T extends ShufflingBuildProps | undefined>(
42
- epoch: Epoch,
43
- decisionRoot: RootHex,
44
- buildProps?: T
45
- ): T extends ShufflingBuildProps ? EpochShuffling : EpochShuffling | null;
46
-
47
- /**
48
- * Gets a cached shuffling via the epoch and decision root. Returns a promise
49
- * for the shuffling if it hs not calculated yet. Returns null if a build has
50
- * not been queued nor a shuffling was calculated.
51
- */
52
- get(epoch: Epoch, decisionRoot: RootHex): Promise<EpochShuffling | null>;
53
-
54
- /**
55
- * Queue asynchronous build for an EpochShuffling
56
- */
57
- build(epoch: Epoch, decisionRoot: RootHex, state: BeaconStateAllForks, activeIndices: Uint32Array): void;
58
- }
59
-
60
19
  /**
61
20
  * Readonly interface for EpochShuffling.
62
21
  */
@@ -164,7 +123,7 @@ export async function computeEpochShufflingAsync(
164
123
  };
165
124
  }
166
125
 
167
- function calculateDecisionRoot(state: BeaconStateAllForks, epoch: Epoch): RootHex {
126
+ export function calculateDecisionRoot(state: BeaconStateAllForks, epoch: Epoch): RootHex {
168
127
  const pivotSlot = computeStartSlotAtEpoch(epoch - 1) - 1;
169
128
  return toRootHex(getBlockRootAtSlot(state, pivotSlot));
170
129
  }