@lodestar/state-transition 1.39.0-dev.aceb5b7416 → 1.39.0-dev.b2437a6348

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 (154) hide show
  1. package/lib/block/index.d.ts.map +1 -1
  2. package/lib/block/index.js +4 -2
  3. package/lib/block/index.js.map +1 -1
  4. package/lib/block/isValidIndexedAttestation.d.ts +5 -5
  5. package/lib/block/isValidIndexedAttestation.d.ts.map +1 -1
  6. package/lib/block/isValidIndexedAttestation.js +9 -10
  7. package/lib/block/isValidIndexedAttestation.js.map +1 -1
  8. package/lib/block/processAttestationPhase0.d.ts.map +1 -1
  9. package/lib/block/processAttestationPhase0.js +1 -1
  10. package/lib/block/processAttestationPhase0.js.map +1 -1
  11. package/lib/block/processAttestationsAltair.d.ts.map +1 -1
  12. package/lib/block/processAttestationsAltair.js +1 -1
  13. package/lib/block/processAttestationsAltair.js.map +1 -1
  14. package/lib/block/processAttesterSlashing.d.ts +3 -2
  15. package/lib/block/processAttesterSlashing.d.ts.map +1 -1
  16. package/lib/block/processAttesterSlashing.js +3 -3
  17. package/lib/block/processAttesterSlashing.js.map +1 -1
  18. package/lib/block/processBlsToExecutionChange.d.ts +3 -1
  19. package/lib/block/processBlsToExecutionChange.d.ts.map +1 -1
  20. package/lib/block/processBlsToExecutionChange.js +7 -11
  21. package/lib/block/processBlsToExecutionChange.js.map +1 -1
  22. package/lib/block/processExecutionPayload.d.ts.map +1 -1
  23. package/lib/block/processExecutionPayload.js +6 -4
  24. package/lib/block/processExecutionPayload.js.map +1 -1
  25. package/lib/block/processProposerSlashing.d.ts +5 -2
  26. package/lib/block/processProposerSlashing.d.ts.map +1 -1
  27. package/lib/block/processProposerSlashing.js +7 -5
  28. package/lib/block/processProposerSlashing.js.map +1 -1
  29. package/lib/block/processRandao.js +2 -2
  30. package/lib/block/processRandao.js.map +1 -1
  31. package/lib/block/processSyncCommittee.d.ts +3 -1
  32. package/lib/block/processSyncCommittee.d.ts.map +1 -1
  33. package/lib/block/processSyncCommittee.js +6 -4
  34. package/lib/block/processSyncCommittee.js.map +1 -1
  35. package/lib/block/processVoluntaryExit.d.ts.map +1 -1
  36. package/lib/block/processVoluntaryExit.js +2 -1
  37. package/lib/block/processVoluntaryExit.js.map +1 -1
  38. package/lib/block/processWithdrawals.d.ts +3 -3
  39. package/lib/block/processWithdrawals.d.ts.map +1 -1
  40. package/lib/block/processWithdrawals.js +152 -105
  41. package/lib/block/processWithdrawals.js.map +1 -1
  42. package/lib/cache/epochCache.d.ts +6 -12
  43. package/lib/cache/epochCache.d.ts.map +1 -1
  44. package/lib/cache/epochCache.js +33 -105
  45. package/lib/cache/epochCache.js.map +1 -1
  46. package/lib/cache/epochTransitionCache.d.ts +5 -12
  47. package/lib/cache/epochTransitionCache.d.ts.map +1 -1
  48. package/lib/cache/epochTransitionCache.js +4 -14
  49. package/lib/cache/epochTransitionCache.js.map +1 -1
  50. package/lib/cache/stateCache.d.ts +1 -0
  51. package/lib/cache/stateCache.d.ts.map +1 -1
  52. package/lib/cache/stateCache.js +1 -2
  53. package/lib/cache/stateCache.js.map +1 -1
  54. package/lib/epoch/processProposerLookahead.d.ts.map +1 -1
  55. package/lib/epoch/processProposerLookahead.js +3 -6
  56. package/lib/epoch/processProposerLookahead.js.map +1 -1
  57. package/lib/index.d.ts +2 -1
  58. package/lib/index.d.ts.map +1 -1
  59. package/lib/index.js +2 -0
  60. package/lib/index.js.map +1 -1
  61. package/lib/rewards/attestationsRewards.d.ts +6 -0
  62. package/lib/rewards/attestationsRewards.d.ts.map +1 -0
  63. package/lib/rewards/attestationsRewards.js +113 -0
  64. package/lib/rewards/attestationsRewards.js.map +1 -0
  65. package/lib/rewards/blockRewards.d.ts +14 -0
  66. package/lib/rewards/blockRewards.d.ts.map +1 -0
  67. package/lib/rewards/blockRewards.js +96 -0
  68. package/lib/rewards/blockRewards.js.map +1 -0
  69. package/lib/rewards/index.d.ts +4 -0
  70. package/lib/rewards/index.d.ts.map +1 -0
  71. package/lib/rewards/index.js +4 -0
  72. package/lib/rewards/index.js.map +1 -0
  73. package/lib/rewards/syncCommitteeRewards.d.ts +6 -0
  74. package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -0
  75. package/lib/rewards/syncCommitteeRewards.js +35 -0
  76. package/lib/rewards/syncCommitteeRewards.js.map +1 -0
  77. package/lib/signatureSets/attesterSlashings.d.ts +5 -5
  78. package/lib/signatureSets/attesterSlashings.d.ts.map +1 -1
  79. package/lib/signatureSets/attesterSlashings.js +10 -7
  80. package/lib/signatureSets/attesterSlashings.js.map +1 -1
  81. package/lib/signatureSets/blsToExecutionChange.d.ts +1 -2
  82. package/lib/signatureSets/blsToExecutionChange.d.ts.map +1 -1
  83. package/lib/signatureSets/blsToExecutionChange.js +2 -2
  84. package/lib/signatureSets/blsToExecutionChange.js.map +1 -1
  85. package/lib/signatureSets/index.d.ts +3 -2
  86. package/lib/signatureSets/index.d.ts.map +1 -1
  87. package/lib/signatureSets/index.js +10 -10
  88. package/lib/signatureSets/index.js.map +1 -1
  89. package/lib/signatureSets/indexedAttestation.d.ts +6 -6
  90. package/lib/signatureSets/indexedAttestation.d.ts.map +1 -1
  91. package/lib/signatureSets/indexedAttestation.js +12 -9
  92. package/lib/signatureSets/indexedAttestation.js.map +1 -1
  93. package/lib/signatureSets/proposer.d.ts +6 -6
  94. package/lib/signatureSets/proposer.d.ts.map +1 -1
  95. package/lib/signatureSets/proposer.js +12 -11
  96. package/lib/signatureSets/proposer.js.map +1 -1
  97. package/lib/signatureSets/proposerSlashings.d.ts +4 -4
  98. package/lib/signatureSets/proposerSlashings.d.ts.map +1 -1
  99. package/lib/signatureSets/proposerSlashings.js +7 -4
  100. package/lib/signatureSets/proposerSlashings.js.map +1 -1
  101. package/lib/signatureSets/randao.d.ts +3 -3
  102. package/lib/signatureSets/randao.d.ts.map +1 -1
  103. package/lib/signatureSets/randao.js +6 -4
  104. package/lib/signatureSets/randao.js.map +1 -1
  105. package/lib/signatureSets/voluntaryExits.d.ts +5 -5
  106. package/lib/signatureSets/voluntaryExits.d.ts.map +1 -1
  107. package/lib/signatureSets/voluntaryExits.js +10 -7
  108. package/lib/signatureSets/voluntaryExits.js.map +1 -1
  109. package/lib/stateTransition.js +1 -1
  110. package/lib/stateTransition.js.map +1 -1
  111. package/lib/types.d.ts +1 -1
  112. package/lib/types.d.ts.map +1 -1
  113. package/lib/util/epochShuffling.d.ts +1 -34
  114. package/lib/util/epochShuffling.d.ts.map +1 -1
  115. package/lib/util/epochShuffling.js +1 -1
  116. package/lib/util/epochShuffling.js.map +1 -1
  117. package/lib/util/execution.d.ts +11 -1
  118. package/lib/util/execution.d.ts.map +1 -1
  119. package/lib/util/execution.js +26 -1
  120. package/lib/util/execution.js.map +1 -1
  121. package/package.json +14 -11
  122. package/src/block/index.ts +6 -2
  123. package/src/block/isValidIndexedAttestation.ts +22 -12
  124. package/src/block/processAttestationPhase0.ts +3 -1
  125. package/src/block/processAttestationsAltair.ts +7 -1
  126. package/src/block/processAttesterSlashing.ts +16 -4
  127. package/src/block/processBlsToExecutionChange.ts +13 -14
  128. package/src/block/processExecutionPayload.ts +14 -8
  129. package/src/block/processProposerSlashing.ts +21 -6
  130. package/src/block/processRandao.ts +2 -2
  131. package/src/block/processSyncCommittee.ts +15 -4
  132. package/src/block/processVoluntaryExit.ts +4 -1
  133. package/src/block/processWithdrawals.ts +230 -135
  134. package/src/cache/epochCache.ts +40 -118
  135. package/src/cache/epochTransitionCache.ts +9 -34
  136. package/src/cache/stateCache.ts +2 -2
  137. package/src/epoch/processProposerLookahead.ts +3 -7
  138. package/src/index.ts +2 -2
  139. package/src/rewards/attestationsRewards.ts +200 -0
  140. package/src/rewards/blockRewards.ts +150 -0
  141. package/src/rewards/index.ts +3 -0
  142. package/src/rewards/syncCommitteeRewards.ts +56 -0
  143. package/src/signatureSets/attesterSlashings.ts +14 -9
  144. package/src/signatureSets/blsToExecutionChange.ts +2 -3
  145. package/src/signatureSets/index.ts +14 -11
  146. package/src/signatureSets/indexedAttestation.ts +18 -11
  147. package/src/signatureSets/proposer.ts +14 -12
  148. package/src/signatureSets/proposerSlashings.ts +10 -10
  149. package/src/signatureSets/randao.ts +7 -5
  150. package/src/signatureSets/voluntaryExits.ts +14 -9
  151. package/src/stateTransition.ts +1 -1
  152. package/src/types.ts +1 -0
  153. package/src/util/epochShuffling.ts +2 -43
  154. package/src/util/execution.ts +39 -0
@@ -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
+ }
@@ -1,38 +1,43 @@
1
+ import {BeaconConfig} from "@lodestar/config";
1
2
  import {DOMAIN_BEACON_ATTESTER} from "@lodestar/params";
2
- import {AttesterSlashing, IndexedAttestationBigint, SignedBeaconBlock, ssz} from "@lodestar/types";
3
+ import {AttesterSlashing, IndexedAttestationBigint, SignedBeaconBlock, Slot, ssz} from "@lodestar/types";
3
4
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
4
- import {CachedBeaconStateAllForks} from "../types.js";
5
5
  import {ISignatureSet, SignatureSetType, computeSigningRoot, computeStartSlotAtEpoch} from "../util/index.js";
6
6
 
7
7
  /** Get signature sets from all AttesterSlashing objects in a block */
8
8
  export function getAttesterSlashingsSignatureSets(
9
+ config: BeaconConfig,
9
10
  index2pubkey: Index2PubkeyCache,
10
- state: CachedBeaconStateAllForks,
11
11
  signedBlock: SignedBeaconBlock
12
12
  ): ISignatureSet[] {
13
+ // the getDomain() api requires the state slot as 1st param, however it's the same to block.slot in state-transition
14
+ // and the same epoch when we verify blocks in batch in beacon-node. So we can safely use block.slot here.
15
+ const blockSlot = signedBlock.message.slot;
13
16
  return signedBlock.message.body.attesterSlashings.flatMap((attesterSlashing) =>
14
- getAttesterSlashingSignatureSets(index2pubkey, state, attesterSlashing)
17
+ getAttesterSlashingSignatureSets(config, index2pubkey, blockSlot, attesterSlashing)
15
18
  );
16
19
  }
17
20
 
18
21
  /** Get signature sets from a single AttesterSlashing object */
19
22
  export function getAttesterSlashingSignatureSets(
23
+ config: BeaconConfig,
20
24
  index2pubkey: Index2PubkeyCache,
21
- state: CachedBeaconStateAllForks,
25
+ stateSlot: Slot,
22
26
  attesterSlashing: AttesterSlashing
23
27
  ): ISignatureSet[] {
24
28
  return [attesterSlashing.attestation1, attesterSlashing.attestation2].map((attestation) =>
25
- getIndexedAttestationBigintSignatureSet(index2pubkey, state, attestation)
29
+ getIndexedAttestationBigintSignatureSet(config, index2pubkey, stateSlot, attestation)
26
30
  );
27
31
  }
28
32
 
29
33
  export function getIndexedAttestationBigintSignatureSet(
34
+ config: BeaconConfig,
30
35
  index2pubkey: Index2PubkeyCache,
31
- state: CachedBeaconStateAllForks,
36
+ stateSlot: Slot,
32
37
  indexedAttestation: IndexedAttestationBigint
33
38
  ): ISignatureSet {
34
- const slot = computeStartSlotAtEpoch(Number(indexedAttestation.data.target.epoch as bigint));
35
- const domain = state.config.getDomain(state.slot, DOMAIN_BEACON_ATTESTER, slot);
39
+ const messageSlot = computeStartSlotAtEpoch(Number(indexedAttestation.data.target.epoch as bigint));
40
+ const domain = config.getDomain(stateSlot, DOMAIN_BEACON_ATTESTER, messageSlot);
36
41
 
37
42
  return {
38
43
  type: SignatureSetType.aggregate,
@@ -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
  /**
@@ -1,8 +1,9 @@
1
+ import {BeaconConfig} from "@lodestar/config";
1
2
  import {ForkSeq} from "@lodestar/params";
2
3
  import {IndexedAttestation, SignedBeaconBlock, altair, capella} from "@lodestar/types";
3
4
  import {getSyncCommitteeSignatureSet} from "../block/processSyncCommittee.js";
4
5
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
5
- import {CachedBeaconStateAllForks, CachedBeaconStateAltair} from "../types.js";
6
+ import {SyncCommitteeCache} from "../cache/syncCommitteeCache.js";
6
7
  import {ISignatureSet} from "../util/index.js";
7
8
  import {getAttesterSlashingsSignatureSets} from "./attesterSlashings.js";
8
9
  import {getBlsToExecutionChangeSignatureSets} from "./blsToExecutionChange.js";
@@ -26,8 +27,9 @@ export * from "./voluntaryExits.js";
26
27
  * Deposits are not included because they can legally have invalid signatures.
27
28
  */
28
29
  export function getBlockSignatureSets(
30
+ config: BeaconConfig,
29
31
  index2pubkey: Index2PubkeyCache,
30
- state: CachedBeaconStateAllForks,
32
+ currentSyncCommitteeIndexed: SyncCommitteeCache,
31
33
  signedBlock: SignedBeaconBlock,
32
34
  indexedAttestations: IndexedAttestation[],
33
35
  opts?: {
@@ -36,25 +38,26 @@ export function getBlockSignatureSets(
36
38
  }
37
39
  ): ISignatureSet[] {
38
40
  // fork based validations
39
- const fork = state.config.getForkSeq(signedBlock.message.slot);
41
+ const fork = config.getForkSeq(signedBlock.message.slot);
40
42
 
41
43
  const signatureSets = [
42
- getRandaoRevealSignatureSet(index2pubkey, state, signedBlock.message),
43
- ...getProposerSlashingsSignatureSets(index2pubkey, state, signedBlock),
44
- ...getAttesterSlashingsSignatureSets(index2pubkey, state, signedBlock),
45
- ...getAttestationsSignatureSets(index2pubkey, state, signedBlock, indexedAttestations),
46
- ...getVoluntaryExitsSignatureSets(index2pubkey, state, signedBlock),
44
+ getRandaoRevealSignatureSet(config, index2pubkey, signedBlock.message),
45
+ ...getProposerSlashingsSignatureSets(config, index2pubkey, signedBlock),
46
+ ...getAttesterSlashingsSignatureSets(config, index2pubkey, signedBlock),
47
+ ...getAttestationsSignatureSets(config, index2pubkey, signedBlock, indexedAttestations),
48
+ ...getVoluntaryExitsSignatureSets(config, index2pubkey, signedBlock),
47
49
  ];
48
50
 
49
51
  if (!opts?.skipProposerSignature) {
50
- signatureSets.push(getBlockProposerSignatureSet(index2pubkey, state, signedBlock));
52
+ signatureSets.push(getBlockProposerSignatureSet(config, index2pubkey, signedBlock));
51
53
  }
52
54
 
53
55
  // Only after altair fork, validate tSyncCommitteeSignature
54
56
  if (fork >= ForkSeq.altair) {
55
57
  const syncCommitteeSignatureSet = getSyncCommitteeSignatureSet(
58
+ config,
56
59
  index2pubkey,
57
- state as CachedBeaconStateAltair,
60
+ currentSyncCommitteeIndexed,
58
61
  (signedBlock as altair.SignedBeaconBlock).message
59
62
  );
60
63
  // There may be no participants in this syncCommitteeSignature, so it must not be validated
@@ -66,7 +69,7 @@ export function getBlockSignatureSets(
66
69
  // only after capella fork
67
70
  if (fork >= ForkSeq.capella) {
68
71
  const blsToExecutionChangeSignatureSets = getBlsToExecutionChangeSignatureSets(
69
- state.config,
72
+ config,
70
73
  signedBlock as capella.SignedBeaconBlock
71
74
  );
72
75
  if (blsToExecutionChangeSignatureSets.length > 0) {
@@ -1,7 +1,7 @@
1
+ import {BeaconConfig} from "@lodestar/config";
1
2
  import {DOMAIN_BEACON_ATTESTER} from "@lodestar/params";
2
- import {IndexedAttestation, SignedBeaconBlock, phase0, ssz} from "@lodestar/types";
3
+ import {IndexedAttestation, SignedBeaconBlock, Slot, phase0, ssz} from "@lodestar/types";
3
4
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
4
- import {CachedBeaconStateAllForks} from "../types.js";
5
5
  import {
6
6
  ISignatureSet,
7
7
  computeSigningRoot,
@@ -10,44 +10,48 @@ import {
10
10
  } from "../util/index.js";
11
11
 
12
12
  export function getAttestationDataSigningRoot(
13
- state: CachedBeaconStateAllForks,
13
+ config: BeaconConfig,
14
+ stateSlot: Slot,
14
15
  data: phase0.AttestationData
15
16
  ): Uint8Array {
16
- const slot = computeStartSlotAtEpoch(data.target.epoch);
17
- const domain = state.config.getDomain(state.slot, DOMAIN_BEACON_ATTESTER, slot);
17
+ const messageSlot = computeStartSlotAtEpoch(data.target.epoch);
18
+ const domain = config.getDomain(stateSlot, DOMAIN_BEACON_ATTESTER, messageSlot);
18
19
 
19
20
  return computeSigningRoot(ssz.phase0.AttestationData, data, domain);
20
21
  }
21
22
 
22
23
  export function getAttestationWithIndicesSignatureSet(
24
+ config: BeaconConfig,
23
25
  index2pubkey: Index2PubkeyCache,
24
- state: CachedBeaconStateAllForks,
26
+ stateSlot: Slot,
25
27
  attestation: Pick<phase0.Attestation, "data" | "signature">,
26
28
  attestingIndices: number[]
27
29
  ): ISignatureSet {
28
30
  return createAggregateSignatureSetFromComponents(
29
31
  attestingIndices.map((i) => index2pubkey[i]),
30
- getAttestationDataSigningRoot(state, attestation.data),
32
+ getAttestationDataSigningRoot(config, stateSlot, attestation.data),
31
33
  attestation.signature
32
34
  );
33
35
  }
34
36
 
35
37
  export function getIndexedAttestationSignatureSet(
38
+ config: BeaconConfig,
36
39
  index2pubkey: Index2PubkeyCache,
37
- state: CachedBeaconStateAllForks,
40
+ stateSlot: Slot,
38
41
  indexedAttestation: IndexedAttestation
39
42
  ): ISignatureSet {
40
43
  return getAttestationWithIndicesSignatureSet(
44
+ config,
41
45
  index2pubkey,
42
- state,
46
+ stateSlot,
43
47
  indexedAttestation,
44
48
  indexedAttestation.attestingIndices
45
49
  );
46
50
  }
47
51
 
48
52
  export function getAttestationsSignatureSets(
53
+ config: BeaconConfig,
49
54
  index2pubkey: Index2PubkeyCache,
50
- state: CachedBeaconStateAllForks,
51
55
  signedBlock: SignedBeaconBlock,
52
56
  indexedAttestations: IndexedAttestation[]
53
57
  ): ISignatureSet[] {
@@ -56,7 +60,10 @@ export function getAttestationsSignatureSets(
56
60
  `Indexed attestations length mismatch: got ${indexedAttestations.length}, expected ${signedBlock.message.body.attestations.length}`
57
61
  );
58
62
  }
63
+ // the getDomain() api requires the state slot as 1st param, however it's the same to block.slot in state-transition
64
+ // and the same epoch when we verify blocks in batch in beacon-node. So we can safely use block.slot here.
65
+ const blockSlot = signedBlock.message.slot;
59
66
  return indexedAttestations.map((indexedAttestation) =>
60
- getIndexedAttestationSignatureSet(index2pubkey, state, indexedAttestation)
67
+ getIndexedAttestationSignatureSet(config, index2pubkey, blockSlot, indexedAttestation)
61
68
  );
62
69
  }
@@ -1,26 +1,28 @@
1
+ import {BeaconConfig} from "@lodestar/config";
1
2
  import {DOMAIN_BEACON_PROPOSER} from "@lodestar/params";
2
3
  import {SignedBeaconBlock, SignedBlindedBeaconBlock, Slot, isBlindedBeaconBlock, phase0, ssz} from "@lodestar/types";
3
4
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
4
- import {CachedBeaconStateAllForks} from "../types.js";
5
5
  import {computeSigningRoot} from "../util/index.js";
6
6
  import {ISignatureSet, SignatureSetType, verifySignatureSet} from "../util/signatureSets.js";
7
7
 
8
8
  export function verifyProposerSignature(
9
+ config: BeaconConfig,
9
10
  index2pubkey: Index2PubkeyCache,
10
- state: CachedBeaconStateAllForks,
11
11
  signedBlock: SignedBeaconBlock | SignedBlindedBeaconBlock
12
12
  ): boolean {
13
- const signatureSet = getBlockProposerSignatureSet(index2pubkey, state, signedBlock);
13
+ const signatureSet = getBlockProposerSignatureSet(config, index2pubkey, signedBlock);
14
14
  return verifySignatureSet(signatureSet);
15
15
  }
16
16
 
17
17
  export function getBlockProposerSignatureSet(
18
+ config: BeaconConfig,
18
19
  index2pubkey: Index2PubkeyCache,
19
- state: CachedBeaconStateAllForks,
20
20
  signedBlock: SignedBeaconBlock | SignedBlindedBeaconBlock
21
21
  ): ISignatureSet {
22
- const {config} = state;
23
- const domain = config.getDomain(state.slot, DOMAIN_BEACON_PROPOSER, signedBlock.message.slot);
22
+ // the getDomain() api requires the state slot as 1st param, however it's the same to block.slot in state-transition
23
+ // and the same epoch when we verify blocks in batch in beacon-node. So we can safely use block.slot here.
24
+ const blockSlot = signedBlock.message.slot;
25
+ const domain = config.getDomain(blockSlot, DOMAIN_BEACON_PROPOSER, blockSlot);
24
26
 
25
27
  const blockType = isBlindedBeaconBlock(signedBlock.message)
26
28
  ? config.getPostBellatrixForkTypes(signedBlock.message.slot).BlindedBeaconBlock
@@ -35,28 +37,28 @@ export function getBlockProposerSignatureSet(
35
37
  }
36
38
 
37
39
  export function getBlockHeaderProposerSignatureSetByParentStateSlot(
40
+ config: BeaconConfig,
38
41
  index2pubkey: Index2PubkeyCache,
39
- parentState: CachedBeaconStateAllForks,
42
+ parentStateSlot: Slot,
40
43
  signedBlockHeader: phase0.SignedBeaconBlockHeader
41
44
  ) {
42
- return getBlockHeaderProposerSignatureSet(index2pubkey, parentState, signedBlockHeader, parentState.slot);
45
+ return getBlockHeaderProposerSignatureSet(config, index2pubkey, signedBlockHeader, parentStateSlot);
43
46
  }
44
47
 
45
48
  export function getBlockHeaderProposerSignatureSetByHeaderSlot(
49
+ config: BeaconConfig,
46
50
  index2pubkey: Index2PubkeyCache,
47
- headState: CachedBeaconStateAllForks,
48
51
  signedBlockHeader: phase0.SignedBeaconBlockHeader
49
52
  ) {
50
- return getBlockHeaderProposerSignatureSet(index2pubkey, headState, signedBlockHeader, signedBlockHeader.message.slot);
53
+ return getBlockHeaderProposerSignatureSet(config, index2pubkey, signedBlockHeader, signedBlockHeader.message.slot);
51
54
  }
52
55
 
53
56
  function getBlockHeaderProposerSignatureSet(
57
+ config: BeaconConfig,
54
58
  index2pubkey: Index2PubkeyCache,
55
- state: CachedBeaconStateAllForks,
56
59
  signedBlockHeader: phase0.SignedBeaconBlockHeader,
57
60
  domainSlot: Slot
58
61
  ): ISignatureSet {
59
- const {config} = state;
60
62
  const domain = config.getDomain(domainSlot, DOMAIN_BEACON_PROPOSER, signedBlockHeader.message.slot);
61
63
 
62
64
  return {
@@ -1,15 +1,16 @@
1
+ import {BeaconConfig} from "@lodestar/config";
1
2
  import {DOMAIN_BEACON_PROPOSER} from "@lodestar/params";
2
- import {SignedBeaconBlock, phase0, ssz} from "@lodestar/types";
3
+ import {SignedBeaconBlock, Slot, phase0, ssz} from "@lodestar/types";
3
4
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
4
- import {CachedBeaconStateAllForks} from "../types.js";
5
5
  import {ISignatureSet, SignatureSetType, computeSigningRoot} from "../util/index.js";
6
6
 
7
7
  /**
8
8
  * Extract signatures to allow validating all block signatures at once
9
9
  */
10
10
  export function getProposerSlashingSignatureSets(
11
+ config: BeaconConfig,
11
12
  index2pubkey: Index2PubkeyCache,
12
- state: CachedBeaconStateAllForks,
13
+ stateSlot: Slot,
13
14
  proposerSlashing: phase0.ProposerSlashing
14
15
  ): ISignatureSet[] {
15
16
  const pubkey = index2pubkey[proposerSlashing.signedHeader1.message.proposerIndex];
@@ -17,11 +18,7 @@ export function getProposerSlashingSignatureSets(
17
18
  // In state transition, ProposerSlashing headers are only partially validated. Their slot could be higher than the
18
19
  // clock and the slashing would still be valid. Must use bigint variants to hash correctly to all possible values
19
20
  return [proposerSlashing.signedHeader1, proposerSlashing.signedHeader2].map((signedHeader): ISignatureSet => {
20
- const domain = state.config.getDomain(
21
- state.slot,
22
- DOMAIN_BEACON_PROPOSER,
23
- Number(signedHeader.message.slot as bigint)
24
- );
21
+ const domain = config.getDomain(stateSlot, DOMAIN_BEACON_PROPOSER, Number(signedHeader.message.slot as bigint));
25
22
 
26
23
  return {
27
24
  type: SignatureSetType.single,
@@ -33,11 +30,14 @@ export function getProposerSlashingSignatureSets(
33
30
  }
34
31
 
35
32
  export function getProposerSlashingsSignatureSets(
33
+ config: BeaconConfig,
36
34
  index2pubkey: Index2PubkeyCache,
37
- state: CachedBeaconStateAllForks,
38
35
  signedBlock: SignedBeaconBlock
39
36
  ): ISignatureSet[] {
37
+ // the getDomain() api requires the state slot as 1st param, however it's the same to block.slot in state-transition
38
+ // and the same epoch when we verify blocks in batch in beacon-node. So we can safely use block.slot here.
39
+ const blockSlot = signedBlock.message.slot;
40
40
  return signedBlock.message.body.proposerSlashings.flatMap((proposerSlashing) =>
41
- getProposerSlashingSignatureSets(index2pubkey, state, proposerSlashing)
41
+ getProposerSlashingSignatureSets(config, index2pubkey, blockSlot, proposerSlashing)
42
42
  );
43
43
  }
@@ -1,7 +1,7 @@
1
+ import {BeaconConfig} from "@lodestar/config";
1
2
  import {DOMAIN_RANDAO} from "@lodestar/params";
2
3
  import {BeaconBlock, ssz} from "@lodestar/types";
3
4
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
4
- import {CachedBeaconStateAllForks} from "../types.js";
5
5
  import {
6
6
  ISignatureSet,
7
7
  SignatureSetType,
@@ -11,24 +11,26 @@ import {
11
11
  } from "../util/index.js";
12
12
 
13
13
  export function verifyRandaoSignature(
14
+ config: BeaconConfig,
14
15
  index2pubkey: Index2PubkeyCache,
15
- state: CachedBeaconStateAllForks,
16
16
  block: BeaconBlock
17
17
  ): boolean {
18
- return verifySignatureSet(getRandaoRevealSignatureSet(index2pubkey, state, block));
18
+ return verifySignatureSet(getRandaoRevealSignatureSet(config, index2pubkey, block));
19
19
  }
20
20
 
21
21
  /**
22
22
  * Extract signatures to allow validating all block signatures at once
23
23
  */
24
24
  export function getRandaoRevealSignatureSet(
25
+ config: BeaconConfig,
25
26
  index2pubkey: Index2PubkeyCache,
26
- state: CachedBeaconStateAllForks,
27
27
  block: BeaconBlock
28
28
  ): ISignatureSet {
29
29
  // should not get epoch from epochCtx
30
30
  const epoch = computeEpochAtSlot(block.slot);
31
- const domain = state.config.getDomain(state.slot, DOMAIN_RANDAO, block.slot);
31
+ // the getDomain() api requires the state slot as 1st param, however it's the same to block.slot in state-transition
32
+ // and the same epoch when we verify blocks in batch in beacon-node. So we can safely use block.slot here.
33
+ const domain = config.getDomain(block.slot, DOMAIN_RANDAO, block.slot);
32
34
 
33
35
  return {
34
36
  type: SignatureSetType.single,
@@ -1,6 +1,6 @@
1
- import {SignedBeaconBlock, phase0, ssz} from "@lodestar/types";
1
+ import {BeaconConfig} from "@lodestar/config";
2
+ import {SignedBeaconBlock, Slot, phase0, ssz} from "@lodestar/types";
2
3
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
3
- import {CachedBeaconStateAllForks} from "../types.js";
4
4
  import {
5
5
  ISignatureSet,
6
6
  SignatureSetType,
@@ -10,23 +10,25 @@ import {
10
10
  } from "../util/index.js";
11
11
 
12
12
  export function verifyVoluntaryExitSignature(
13
+ config: BeaconConfig,
13
14
  index2pubkey: Index2PubkeyCache,
14
- state: CachedBeaconStateAllForks,
15
+ stateSlot: Slot,
15
16
  signedVoluntaryExit: phase0.SignedVoluntaryExit
16
17
  ): boolean {
17
- return verifySignatureSet(getVoluntaryExitSignatureSet(index2pubkey, state, signedVoluntaryExit));
18
+ return verifySignatureSet(getVoluntaryExitSignatureSet(config, index2pubkey, stateSlot, signedVoluntaryExit));
18
19
  }
19
20
 
20
21
  /**
21
22
  * Extract signatures to allow validating all block signatures at once
22
23
  */
23
24
  export function getVoluntaryExitSignatureSet(
25
+ config: BeaconConfig,
24
26
  index2pubkey: Index2PubkeyCache,
25
- state: CachedBeaconStateAllForks,
27
+ stateSlot: Slot,
26
28
  signedVoluntaryExit: phase0.SignedVoluntaryExit
27
29
  ): ISignatureSet {
28
- const slot = computeStartSlotAtEpoch(signedVoluntaryExit.message.epoch);
29
- const domain = state.config.getDomainForVoluntaryExit(state.slot, slot);
30
+ const messageSlot = computeStartSlotAtEpoch(signedVoluntaryExit.message.epoch);
31
+ const domain = config.getDomainForVoluntaryExit(stateSlot, messageSlot);
30
32
 
31
33
  return {
32
34
  type: SignatureSetType.single,
@@ -37,11 +39,14 @@ export function getVoluntaryExitSignatureSet(
37
39
  }
38
40
 
39
41
  export function getVoluntaryExitsSignatureSets(
42
+ config: BeaconConfig,
40
43
  index2pubkey: Index2PubkeyCache,
41
- state: CachedBeaconStateAllForks,
42
44
  signedBlock: SignedBeaconBlock
43
45
  ): ISignatureSet[] {
46
+ // the getDomain() api requires the state slot as 1st param, however it's the same to block.slot in state-transition
47
+ // and the same epoch when we verify blocks in batch in beacon-node. So we can safely use block.slot here.
48
+ const blockSlot = signedBlock.message.slot;
44
49
  return signedBlock.message.body.voluntaryExits.map((voluntaryExit) =>
45
- getVoluntaryExitSignatureSet(index2pubkey, state, voluntaryExit)
50
+ getVoluntaryExitSignatureSet(config, index2pubkey, blockSlot, voluntaryExit)
46
51
  );
47
52
  }
@@ -111,7 +111,7 @@ export function stateTransition(
111
111
  postState = processSlotsWithTransientCache(postState, blockSlot, options, {metrics, validatorMonitor});
112
112
 
113
113
  // Verify proposer signature only
114
- if (verifyProposer && !verifyProposerSignature(postState.epochCtx.index2pubkey, postState, signedBlock)) {
114
+ if (verifyProposer && !verifyProposerSignature(postState.config, postState.epochCtx.index2pubkey, signedBlock)) {
115
115
  throw new Error("Invalid block signature");
116
116
  }
117
117
 
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";