@lodestar/state-transition 1.39.0-dev.c151a164f2 → 1.39.0-dev.c630c55067

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 (99) 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.map +1 -1
  5. package/lib/block/isValidIndexedAttestation.js +2 -2
  6. package/lib/block/isValidIndexedAttestation.js.map +1 -1
  7. package/lib/block/processAttestationsAltair.js +1 -1
  8. package/lib/block/processAttestationsAltair.js.map +1 -1
  9. package/lib/block/processExecutionPayload.d.ts.map +1 -1
  10. package/lib/block/processExecutionPayload.js +6 -4
  11. package/lib/block/processExecutionPayload.js.map +1 -1
  12. package/lib/block/processProposerSlashing.js +1 -1
  13. package/lib/block/processProposerSlashing.js.map +1 -1
  14. package/lib/block/processRandao.js +2 -2
  15. package/lib/block/processRandao.js.map +1 -1
  16. package/lib/block/processSyncCommittee.d.ts +2 -1
  17. package/lib/block/processSyncCommittee.d.ts.map +1 -1
  18. package/lib/block/processSyncCommittee.js +6 -4
  19. package/lib/block/processSyncCommittee.js.map +1 -1
  20. package/lib/block/processVoluntaryExit.js +1 -1
  21. package/lib/block/processVoluntaryExit.js.map +1 -1
  22. package/lib/index.d.ts +2 -1
  23. package/lib/index.d.ts.map +1 -1
  24. package/lib/index.js +2 -0
  25. package/lib/index.js.map +1 -1
  26. package/lib/rewards/attestationsRewards.d.ts +6 -0
  27. package/lib/rewards/attestationsRewards.d.ts.map +1 -0
  28. package/lib/rewards/attestationsRewards.js +113 -0
  29. package/lib/rewards/attestationsRewards.js.map +1 -0
  30. package/lib/rewards/blockRewards.d.ts +13 -0
  31. package/lib/rewards/blockRewards.d.ts.map +1 -0
  32. package/lib/rewards/blockRewards.js +95 -0
  33. package/lib/rewards/blockRewards.js.map +1 -0
  34. package/lib/rewards/index.d.ts +4 -0
  35. package/lib/rewards/index.d.ts.map +1 -0
  36. package/lib/rewards/index.js +4 -0
  37. package/lib/rewards/index.js.map +1 -0
  38. package/lib/rewards/syncCommitteeRewards.d.ts +6 -0
  39. package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -0
  40. package/lib/rewards/syncCommitteeRewards.js +36 -0
  41. package/lib/rewards/syncCommitteeRewards.js.map +1 -0
  42. package/lib/signatureSets/attesterSlashings.d.ts +4 -5
  43. package/lib/signatureSets/attesterSlashings.d.ts.map +1 -1
  44. package/lib/signatureSets/attesterSlashings.js +10 -7
  45. package/lib/signatureSets/attesterSlashings.js.map +1 -1
  46. package/lib/signatureSets/index.d.ts +2 -2
  47. package/lib/signatureSets/index.d.ts.map +1 -1
  48. package/lib/signatureSets/index.js +10 -10
  49. package/lib/signatureSets/index.js.map +1 -1
  50. package/lib/signatureSets/indexedAttestation.d.ts +5 -6
  51. package/lib/signatureSets/indexedAttestation.d.ts.map +1 -1
  52. package/lib/signatureSets/indexedAttestation.js +12 -9
  53. package/lib/signatureSets/indexedAttestation.js.map +1 -1
  54. package/lib/signatureSets/proposer.d.ts +2 -2
  55. package/lib/signatureSets/proposer.d.ts.map +1 -1
  56. package/lib/signatureSets/proposer.js +7 -4
  57. package/lib/signatureSets/proposer.js.map +1 -1
  58. package/lib/signatureSets/proposerSlashings.d.ts +3 -4
  59. package/lib/signatureSets/proposerSlashings.d.ts.map +1 -1
  60. package/lib/signatureSets/proposerSlashings.js +7 -4
  61. package/lib/signatureSets/proposerSlashings.js.map +1 -1
  62. package/lib/signatureSets/randao.d.ts +2 -3
  63. package/lib/signatureSets/randao.d.ts.map +1 -1
  64. package/lib/signatureSets/randao.js +6 -4
  65. package/lib/signatureSets/randao.js.map +1 -1
  66. package/lib/signatureSets/voluntaryExits.d.ts +4 -5
  67. package/lib/signatureSets/voluntaryExits.d.ts.map +1 -1
  68. package/lib/signatureSets/voluntaryExits.js +10 -7
  69. package/lib/signatureSets/voluntaryExits.js.map +1 -1
  70. package/lib/stateTransition.d.ts.map +1 -1
  71. package/lib/stateTransition.js +1 -2
  72. package/lib/stateTransition.js.map +1 -1
  73. package/lib/util/execution.d.ts +11 -1
  74. package/lib/util/execution.d.ts.map +1 -1
  75. package/lib/util/execution.js +26 -1
  76. package/lib/util/execution.js.map +1 -1
  77. package/package.json +6 -6
  78. package/src/block/index.ts +6 -2
  79. package/src/block/isValidIndexedAttestation.ts +4 -2
  80. package/src/block/processAttestationsAltair.ts +1 -1
  81. package/src/block/processExecutionPayload.ts +14 -8
  82. package/src/block/processProposerSlashing.ts +1 -1
  83. package/src/block/processRandao.ts +2 -2
  84. package/src/block/processSyncCommittee.ts +7 -4
  85. package/src/block/processVoluntaryExit.ts +1 -1
  86. package/src/index.ts +2 -2
  87. package/src/rewards/attestationsRewards.ts +200 -0
  88. package/src/rewards/blockRewards.ts +147 -0
  89. package/src/rewards/index.ts +3 -0
  90. package/src/rewards/syncCommitteeRewards.ts +59 -0
  91. package/src/signatureSets/attesterSlashings.ts +10 -9
  92. package/src/signatureSets/index.ts +11 -11
  93. package/src/signatureSets/indexedAttestation.ts +12 -11
  94. package/src/signatureSets/proposer.ts +5 -4
  95. package/src/signatureSets/proposerSlashings.ts +7 -6
  96. package/src/signatureSets/randao.ts +4 -5
  97. package/src/signatureSets/voluntaryExits.ts +10 -9
  98. package/src/stateTransition.ts +1 -4
  99. package/src/util/execution.ts +39 -0
@@ -0,0 +1,147 @@
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 {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStatePhase0} from "../cache/stateCache.js";
11
+ import {getAttesterSlashableIndices} from "../util/attestation.js";
12
+
13
+ type SubRewardValue = number; // All reward values should be integer
14
+
15
+ /**
16
+ * Calculate total proposer block rewards given block and the beacon state of the same slot before the block is applied (preState)
17
+ * postState can be passed in to read reward cache if available
18
+ * Standard (Non MEV) rewards for proposing a block consists of:
19
+ * 1) Including attestations from (beacon) committee
20
+ * 2) Including attestations from sync committee
21
+ * 3) Reporting slashable behaviours from proposer and attester
22
+ */
23
+ export async function computeBlockRewards(
24
+ config: BeaconConfig,
25
+ block: BeaconBlock,
26
+ preState: CachedBeaconStateAllForks,
27
+ postState?: CachedBeaconStateAllForks
28
+ ): Promise<rewards.BlockRewards> {
29
+ const fork = config.getForkName(block.slot);
30
+ const {attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0} =
31
+ postState?.proposerRewards ?? {};
32
+ let blockAttestationReward = cachedAttestationsReward;
33
+ let syncAggregateReward = cachedSyncAggregateReward;
34
+
35
+ if (blockAttestationReward === 0) {
36
+ blockAttestationReward =
37
+ fork === ForkName.phase0
38
+ ? computeBlockAttestationRewardPhase0(block as phase0.BeaconBlock, preState as CachedBeaconStatePhase0)
39
+ : computeBlockAttestationRewardAltair(config, block as altair.BeaconBlock, preState as CachedBeaconStateAltair);
40
+ }
41
+
42
+ if (syncAggregateReward === 0) {
43
+ syncAggregateReward = computeSyncAggregateReward(block as altair.BeaconBlock, preState as CachedBeaconStateAltair);
44
+ }
45
+
46
+ const blockProposerSlashingReward = computeBlockProposerSlashingReward(fork, block, preState);
47
+ const blockAttesterSlashingReward = computeBlockAttesterSlashingReward(fork, block, preState);
48
+
49
+ const total =
50
+ blockAttestationReward + syncAggregateReward + blockProposerSlashingReward + blockAttesterSlashingReward;
51
+
52
+ return {
53
+ proposerIndex: block.proposerIndex,
54
+ total,
55
+ attestations: blockAttestationReward,
56
+ syncAggregate: syncAggregateReward,
57
+ proposerSlashings: blockProposerSlashingReward,
58
+ attesterSlashings: blockAttesterSlashingReward,
59
+ };
60
+ }
61
+
62
+ /**
63
+ * TODO: Calculate rewards received by block proposer for including attestations.
64
+ */
65
+ function computeBlockAttestationRewardPhase0(
66
+ _block: phase0.BeaconBlock,
67
+ _preState: CachedBeaconStatePhase0
68
+ ): SubRewardValue {
69
+ throw new Error("Unsupported fork! Block attestation reward calculation is not available in phase0");
70
+ }
71
+
72
+ /**
73
+ * Calculate rewards received by block proposer for including attestations since Altair.
74
+ * Reuses `processAttestationsAltair()`. Has dependency on RewardCache
75
+ */
76
+ function computeBlockAttestationRewardAltair(
77
+ config: BeaconConfig,
78
+ block: altair.BeaconBlock,
79
+ preState: CachedBeaconStateAltair
80
+ ): SubRewardValue {
81
+ const fork = config.getForkSeq(block.slot);
82
+ const {attestations} = block.body;
83
+
84
+ processAttestationsAltair(fork, preState, attestations, false);
85
+
86
+ return preState.proposerRewards.attestations;
87
+ }
88
+
89
+ function computeSyncAggregateReward(block: altair.BeaconBlock, preState: CachedBeaconStateAltair): SubRewardValue {
90
+ if (block.body.syncAggregate !== undefined) {
91
+ const {syncCommitteeBits} = block.body.syncAggregate;
92
+ const {syncProposerReward} = preState.epochCtx;
93
+
94
+ return syncCommitteeBits.getTrueBitIndexes().length * Math.floor(syncProposerReward); // syncProposerReward should already be integer
95
+ }
96
+
97
+ return 0; // phase0 block does not have syncAggregate
98
+ }
99
+
100
+ /**
101
+ * Calculate rewards received by block proposer for including proposer slashings.
102
+ * All proposer slashing rewards go to block proposer and none to whistleblower as of Deneb
103
+ */
104
+ function computeBlockProposerSlashingReward(
105
+ fork: ForkName,
106
+ block: BeaconBlock,
107
+ state: CachedBeaconStateAllForks
108
+ ): SubRewardValue {
109
+ let proposerSlashingReward = 0;
110
+
111
+ for (const proposerSlashing of block.body.proposerSlashings) {
112
+ const offendingProposerIndex = proposerSlashing.signedHeader1.message.proposerIndex;
113
+ const offendingProposerBalance = state.validators.getReadonly(offendingProposerIndex).effectiveBalance;
114
+ const whistleblowerRewardQuotient = isForkPostElectra(fork)
115
+ ? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
116
+ : WHISTLEBLOWER_REWARD_QUOTIENT;
117
+
118
+ proposerSlashingReward += Math.floor(offendingProposerBalance / whistleblowerRewardQuotient);
119
+ }
120
+
121
+ return proposerSlashingReward;
122
+ }
123
+
124
+ /**
125
+ * Calculate rewards received by block proposer for including attester slashings.
126
+ * All attester slashing rewards go to block proposer and none to whistleblower as of Deneb
127
+ */
128
+ function computeBlockAttesterSlashingReward(
129
+ fork: ForkName,
130
+ block: BeaconBlock,
131
+ preState: CachedBeaconStateAllForks
132
+ ): SubRewardValue {
133
+ let attesterSlashingReward = 0;
134
+
135
+ for (const attesterSlashing of block.body.attesterSlashings) {
136
+ for (const offendingAttesterIndex of getAttesterSlashableIndices(attesterSlashing)) {
137
+ const offendingAttesterBalance = preState.validators.getReadonly(offendingAttesterIndex).effectiveBalance;
138
+ const whistleblowerRewardQuotient = isForkPostElectra(fork)
139
+ ? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
140
+ : WHISTLEBLOWER_REWARD_QUOTIENT;
141
+
142
+ attesterSlashingReward += Math.floor(offendingAttesterBalance / whistleblowerRewardQuotient);
143
+ }
144
+ }
145
+
146
+ return attesterSlashingReward;
147
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./attestationsRewards.js";
2
+ export * from "./blockRewards.js";
3
+ export * from "./syncCommitteeRewards.js";
@@ -0,0 +1,59 @@
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
+ type BalanceRecord = {val: number}; // Use val for convenient way to increment/decrement balance
8
+
9
+ export async function computeSyncCommitteeRewards(
10
+ config: BeaconConfig,
11
+ index2pubkey: Index2PubkeyCache,
12
+ block: BeaconBlock,
13
+ preState: CachedBeaconStateAllForks,
14
+ validatorIds: (ValidatorIndex | string)[] = []
15
+ ): Promise<rewards.SyncCommitteeRewards> {
16
+ const fork = config.getForkName(block.slot);
17
+ if (fork === ForkName.phase0) {
18
+ throw Error("Cannot get sync rewards as phase0 block does not have sync committee");
19
+ }
20
+
21
+ const altairBlock = block as altair.BeaconBlock;
22
+ const preStateAltair = preState as CachedBeaconStateAltair;
23
+
24
+ // Bound syncCommitteeValidatorIndices in case it goes beyond SYNC_COMMITTEE_SIZE just to be safe
25
+ const syncCommitteeValidatorIndices = preStateAltair.epochCtx.currentSyncCommitteeIndexed.validatorIndices.slice(
26
+ 0,
27
+ SYNC_COMMITTEE_SIZE
28
+ );
29
+ const {syncParticipantReward} = preStateAltair.epochCtx;
30
+ const {syncCommitteeBits} = altairBlock.body.syncAggregate;
31
+
32
+ // Use balance of each committee as starting point such that we cap the penalty to avoid balance dropping below 0
33
+ const balances: Map<ValidatorIndex, BalanceRecord> = new Map();
34
+ for (const i of syncCommitteeValidatorIndices) {
35
+ balances.set(i, {val: preStateAltair.balances.get(i)});
36
+ }
37
+
38
+ for (const i of syncCommitteeValidatorIndices) {
39
+ const balanceRecord = balances.get(i) as BalanceRecord;
40
+ if (syncCommitteeBits.get(i)) {
41
+ // Positive rewards for participants
42
+ balanceRecord.val += syncParticipantReward;
43
+ } else {
44
+ // Negative rewards for non participants
45
+ balanceRecord.val = Math.max(0, balanceRecord.val - syncParticipantReward);
46
+ }
47
+ }
48
+
49
+ const rewards = Array.from(balances, ([validatorIndex, v]) => ({validatorIndex, reward: v.val}));
50
+
51
+ if (validatorIds.length) {
52
+ const filtersSet = new Set(validatorIds);
53
+ return rewards.filter(
54
+ (reward) => filtersSet.has(reward.validatorIndex) || filtersSet.has(index2pubkey[reward.validatorIndex].toHex())
55
+ );
56
+ }
57
+
58
+ return rewards;
59
+ }
@@ -1,19 +1,20 @@
1
1
  import {BeaconConfig} from "@lodestar/config";
2
2
  import {DOMAIN_BEACON_ATTESTER} from "@lodestar/params";
3
- import {AttesterSlashing, IndexedAttestationBigint, SignedBeaconBlock, ssz} from "@lodestar/types";
3
+ import {AttesterSlashing, IndexedAttestationBigint, SignedBeaconBlock, Slot, ssz} from "@lodestar/types";
4
4
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
5
- import {CachedBeaconStateAllForks} from "../types.js";
6
5
  import {ISignatureSet, SignatureSetType, computeSigningRoot, computeStartSlotAtEpoch} from "../util/index.js";
7
6
 
8
7
  /** Get signature sets from all AttesterSlashing objects in a block */
9
8
  export function getAttesterSlashingsSignatureSets(
10
9
  config: BeaconConfig,
11
10
  index2pubkey: Index2PubkeyCache,
12
- state: CachedBeaconStateAllForks,
13
11
  signedBlock: SignedBeaconBlock
14
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;
15
16
  return signedBlock.message.body.attesterSlashings.flatMap((attesterSlashing) =>
16
- getAttesterSlashingSignatureSets(config, index2pubkey, state, attesterSlashing)
17
+ getAttesterSlashingSignatureSets(config, index2pubkey, blockSlot, attesterSlashing)
17
18
  );
18
19
  }
19
20
 
@@ -21,22 +22,22 @@ export function getAttesterSlashingsSignatureSets(
21
22
  export function getAttesterSlashingSignatureSets(
22
23
  config: BeaconConfig,
23
24
  index2pubkey: Index2PubkeyCache,
24
- state: CachedBeaconStateAllForks,
25
+ stateSlot: Slot,
25
26
  attesterSlashing: AttesterSlashing
26
27
  ): ISignatureSet[] {
27
28
  return [attesterSlashing.attestation1, attesterSlashing.attestation2].map((attestation) =>
28
- getIndexedAttestationBigintSignatureSet(config, index2pubkey, state, attestation)
29
+ getIndexedAttestationBigintSignatureSet(config, index2pubkey, stateSlot, attestation)
29
30
  );
30
31
  }
31
32
 
32
33
  export function getIndexedAttestationBigintSignatureSet(
33
34
  config: BeaconConfig,
34
35
  index2pubkey: Index2PubkeyCache,
35
- state: CachedBeaconStateAllForks,
36
+ stateSlot: Slot,
36
37
  indexedAttestation: IndexedAttestationBigint
37
38
  ): ISignatureSet {
38
- const slot = computeStartSlotAtEpoch(Number(indexedAttestation.data.target.epoch as bigint));
39
- const domain = 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);
40
41
 
41
42
  return {
42
43
  type: SignatureSetType.aggregate,
@@ -3,7 +3,7 @@ import {ForkSeq} from "@lodestar/params";
3
3
  import {IndexedAttestation, SignedBeaconBlock, altair, capella} from "@lodestar/types";
4
4
  import {getSyncCommitteeSignatureSet} from "../block/processSyncCommittee.js";
5
5
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
6
- import {CachedBeaconStateAllForks, CachedBeaconStateAltair} from "../types.js";
6
+ import {SyncCommitteeCache} from "../cache/syncCommitteeCache.js";
7
7
  import {ISignatureSet} from "../util/index.js";
8
8
  import {getAttesterSlashingsSignatureSets} from "./attesterSlashings.js";
9
9
  import {getBlsToExecutionChangeSignatureSets} from "./blsToExecutionChange.js";
@@ -29,7 +29,7 @@ export * from "./voluntaryExits.js";
29
29
  export function getBlockSignatureSets(
30
30
  config: BeaconConfig,
31
31
  index2pubkey: Index2PubkeyCache,
32
- state: CachedBeaconStateAllForks,
32
+ currentSyncCommitteeIndexed: SyncCommitteeCache,
33
33
  signedBlock: SignedBeaconBlock,
34
34
  indexedAttestations: IndexedAttestation[],
35
35
  opts?: {
@@ -38,18 +38,18 @@ export function getBlockSignatureSets(
38
38
  }
39
39
  ): ISignatureSet[] {
40
40
  // fork based validations
41
- const fork = state.config.getForkSeq(signedBlock.message.slot);
41
+ const fork = config.getForkSeq(signedBlock.message.slot);
42
42
 
43
43
  const signatureSets = [
44
- getRandaoRevealSignatureSet(config, index2pubkey, state, signedBlock.message),
45
- ...getProposerSlashingsSignatureSets(config, index2pubkey, state, signedBlock),
46
- ...getAttesterSlashingsSignatureSets(config, index2pubkey, state, signedBlock),
47
- ...getAttestationsSignatureSets(config, index2pubkey, state, signedBlock, indexedAttestations),
48
- ...getVoluntaryExitsSignatureSets(config, 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),
49
49
  ];
50
50
 
51
51
  if (!opts?.skipProposerSignature) {
52
- signatureSets.push(getBlockProposerSignatureSet(config, index2pubkey, state, signedBlock));
52
+ signatureSets.push(getBlockProposerSignatureSet(config, index2pubkey, signedBlock));
53
53
  }
54
54
 
55
55
  // Only after altair fork, validate tSyncCommitteeSignature
@@ -57,7 +57,7 @@ export function getBlockSignatureSets(
57
57
  const syncCommitteeSignatureSet = getSyncCommitteeSignatureSet(
58
58
  config,
59
59
  index2pubkey,
60
- state as CachedBeaconStateAltair,
60
+ currentSyncCommitteeIndexed,
61
61
  (signedBlock as altair.SignedBeaconBlock).message
62
62
  );
63
63
  // There may be no participants in this syncCommitteeSignature, so it must not be validated
@@ -69,7 +69,7 @@ export function getBlockSignatureSets(
69
69
  // only after capella fork
70
70
  if (fork >= ForkSeq.capella) {
71
71
  const blsToExecutionChangeSignatureSets = getBlsToExecutionChangeSignatureSets(
72
- state.config,
72
+ config,
73
73
  signedBlock as capella.SignedBeaconBlock
74
74
  );
75
75
  if (blsToExecutionChangeSignatureSets.length > 0) {
@@ -1,8 +1,7 @@
1
1
  import {BeaconConfig} from "@lodestar/config";
2
2
  import {DOMAIN_BEACON_ATTESTER} from "@lodestar/params";
3
- import {IndexedAttestation, SignedBeaconBlock, phase0, ssz} from "@lodestar/types";
3
+ import {IndexedAttestation, SignedBeaconBlock, Slot, phase0, ssz} from "@lodestar/types";
4
4
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
5
- import {CachedBeaconStateAllForks} from "../types.js";
6
5
  import {
7
6
  ISignatureSet,
8
7
  computeSigningRoot,
@@ -12,11 +11,11 @@ import {
12
11
 
13
12
  export function getAttestationDataSigningRoot(
14
13
  config: BeaconConfig,
15
- state: CachedBeaconStateAllForks,
14
+ stateSlot: Slot,
16
15
  data: phase0.AttestationData
17
16
  ): Uint8Array {
18
- const slot = computeStartSlotAtEpoch(data.target.epoch);
19
- const domain = 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);
20
19
 
21
20
  return computeSigningRoot(ssz.phase0.AttestationData, data, domain);
22
21
  }
@@ -24,13 +23,13 @@ export function getAttestationDataSigningRoot(
24
23
  export function getAttestationWithIndicesSignatureSet(
25
24
  config: BeaconConfig,
26
25
  index2pubkey: Index2PubkeyCache,
27
- state: CachedBeaconStateAllForks,
26
+ stateSlot: Slot,
28
27
  attestation: Pick<phase0.Attestation, "data" | "signature">,
29
28
  attestingIndices: number[]
30
29
  ): ISignatureSet {
31
30
  return createAggregateSignatureSetFromComponents(
32
31
  attestingIndices.map((i) => index2pubkey[i]),
33
- getAttestationDataSigningRoot(config, state, attestation.data),
32
+ getAttestationDataSigningRoot(config, stateSlot, attestation.data),
34
33
  attestation.signature
35
34
  );
36
35
  }
@@ -38,13 +37,13 @@ export function getAttestationWithIndicesSignatureSet(
38
37
  export function getIndexedAttestationSignatureSet(
39
38
  config: BeaconConfig,
40
39
  index2pubkey: Index2PubkeyCache,
41
- state: CachedBeaconStateAllForks,
40
+ stateSlot: Slot,
42
41
  indexedAttestation: IndexedAttestation
43
42
  ): ISignatureSet {
44
43
  return getAttestationWithIndicesSignatureSet(
45
44
  config,
46
45
  index2pubkey,
47
- state,
46
+ stateSlot,
48
47
  indexedAttestation,
49
48
  indexedAttestation.attestingIndices
50
49
  );
@@ -53,7 +52,6 @@ export function getIndexedAttestationSignatureSet(
53
52
  export function getAttestationsSignatureSets(
54
53
  config: BeaconConfig,
55
54
  index2pubkey: Index2PubkeyCache,
56
- state: CachedBeaconStateAllForks,
57
55
  signedBlock: SignedBeaconBlock,
58
56
  indexedAttestations: IndexedAttestation[]
59
57
  ): ISignatureSet[] {
@@ -62,7 +60,10 @@ export function getAttestationsSignatureSets(
62
60
  `Indexed attestations length mismatch: got ${indexedAttestations.length}, expected ${signedBlock.message.body.attestations.length}`
63
61
  );
64
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;
65
66
  return indexedAttestations.map((indexedAttestation) =>
66
- getIndexedAttestationSignatureSet(config, index2pubkey, state, indexedAttestation)
67
+ getIndexedAttestationSignatureSet(config, index2pubkey, blockSlot, indexedAttestation)
67
68
  );
68
69
  }
@@ -9,20 +9,21 @@ import {ISignatureSet, SignatureSetType, verifySignatureSet} from "../util/signa
9
9
  export function verifyProposerSignature(
10
10
  config: BeaconConfig,
11
11
  index2pubkey: Index2PubkeyCache,
12
- state: CachedBeaconStateAllForks,
13
12
  signedBlock: SignedBeaconBlock | SignedBlindedBeaconBlock
14
13
  ): boolean {
15
- const signatureSet = getBlockProposerSignatureSet(config, index2pubkey, state, signedBlock);
14
+ const signatureSet = getBlockProposerSignatureSet(config, index2pubkey, signedBlock);
16
15
  return verifySignatureSet(signatureSet);
17
16
  }
18
17
 
19
18
  export function getBlockProposerSignatureSet(
20
19
  config: BeaconConfig,
21
20
  index2pubkey: Index2PubkeyCache,
22
- state: CachedBeaconStateAllForks,
23
21
  signedBlock: SignedBeaconBlock | SignedBlindedBeaconBlock
24
22
  ): ISignatureSet {
25
- const domain = config.getDomain(state.slot, DOMAIN_BEACON_PROPOSER, signedBlock.message.slot);
23
+ // the getDomain() api requires the state slot as 1st param, however it's the same to block.slot in state-transition
24
+ // and the same epoch when we verify blocks in batch in beacon-node. So we can safely use block.slot here.
25
+ const blockSlot = signedBlock.message.slot;
26
+ const domain = config.getDomain(blockSlot, DOMAIN_BEACON_PROPOSER, blockSlot);
26
27
 
27
28
  const blockType = isBlindedBeaconBlock(signedBlock.message)
28
29
  ? config.getPostBellatrixForkTypes(signedBlock.message.slot).BlindedBeaconBlock
@@ -1,8 +1,7 @@
1
1
  import {BeaconConfig} from "@lodestar/config";
2
2
  import {DOMAIN_BEACON_PROPOSER} from "@lodestar/params";
3
- import {SignedBeaconBlock, phase0, ssz} from "@lodestar/types";
3
+ import {SignedBeaconBlock, Slot, phase0, ssz} from "@lodestar/types";
4
4
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
5
- import {CachedBeaconStateAllForks} from "../types.js";
6
5
  import {ISignatureSet, SignatureSetType, computeSigningRoot} from "../util/index.js";
7
6
 
8
7
  /**
@@ -11,7 +10,7 @@ import {ISignatureSet, SignatureSetType, computeSigningRoot} from "../util/index
11
10
  export function getProposerSlashingSignatureSets(
12
11
  config: BeaconConfig,
13
12
  index2pubkey: Index2PubkeyCache,
14
- state: CachedBeaconStateAllForks,
13
+ stateSlot: Slot,
15
14
  proposerSlashing: phase0.ProposerSlashing
16
15
  ): ISignatureSet[] {
17
16
  const pubkey = index2pubkey[proposerSlashing.signedHeader1.message.proposerIndex];
@@ -19,7 +18,7 @@ export function getProposerSlashingSignatureSets(
19
18
  // In state transition, ProposerSlashing headers are only partially validated. Their slot could be higher than the
20
19
  // clock and the slashing would still be valid. Must use bigint variants to hash correctly to all possible values
21
20
  return [proposerSlashing.signedHeader1, proposerSlashing.signedHeader2].map((signedHeader): ISignatureSet => {
22
- const domain = config.getDomain(state.slot, DOMAIN_BEACON_PROPOSER, Number(signedHeader.message.slot as bigint));
21
+ const domain = config.getDomain(stateSlot, DOMAIN_BEACON_PROPOSER, Number(signedHeader.message.slot as bigint));
23
22
 
24
23
  return {
25
24
  type: SignatureSetType.single,
@@ -33,10 +32,12 @@ export function getProposerSlashingSignatureSets(
33
32
  export function getProposerSlashingsSignatureSets(
34
33
  config: BeaconConfig,
35
34
  index2pubkey: Index2PubkeyCache,
36
- state: CachedBeaconStateAllForks,
37
35
  signedBlock: SignedBeaconBlock
38
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;
39
40
  return signedBlock.message.body.proposerSlashings.flatMap((proposerSlashing) =>
40
- getProposerSlashingSignatureSets(config, index2pubkey, state, proposerSlashing)
41
+ getProposerSlashingSignatureSets(config, index2pubkey, blockSlot, proposerSlashing)
41
42
  );
42
43
  }
@@ -2,7 +2,6 @@ import {BeaconConfig} from "@lodestar/config";
2
2
  import {DOMAIN_RANDAO} from "@lodestar/params";
3
3
  import {BeaconBlock, ssz} from "@lodestar/types";
4
4
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
5
- import {CachedBeaconStateAllForks} from "../types.js";
6
5
  import {
7
6
  ISignatureSet,
8
7
  SignatureSetType,
@@ -14,10 +13,9 @@ import {
14
13
  export function verifyRandaoSignature(
15
14
  config: BeaconConfig,
16
15
  index2pubkey: Index2PubkeyCache,
17
- state: CachedBeaconStateAllForks,
18
16
  block: BeaconBlock
19
17
  ): boolean {
20
- return verifySignatureSet(getRandaoRevealSignatureSet(config, index2pubkey, state, block));
18
+ return verifySignatureSet(getRandaoRevealSignatureSet(config, index2pubkey, block));
21
19
  }
22
20
 
23
21
  /**
@@ -26,12 +24,13 @@ export function verifyRandaoSignature(
26
24
  export function getRandaoRevealSignatureSet(
27
25
  config: BeaconConfig,
28
26
  index2pubkey: Index2PubkeyCache,
29
- state: CachedBeaconStateAllForks,
30
27
  block: BeaconBlock
31
28
  ): ISignatureSet {
32
29
  // should not get epoch from epochCtx
33
30
  const epoch = computeEpochAtSlot(block.slot);
34
- const domain = 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);
35
34
 
36
35
  return {
37
36
  type: SignatureSetType.single,
@@ -1,7 +1,6 @@
1
1
  import {BeaconConfig} from "@lodestar/config";
2
- import {SignedBeaconBlock, phase0, ssz} from "@lodestar/types";
2
+ import {SignedBeaconBlock, Slot, phase0, ssz} from "@lodestar/types";
3
3
  import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
4
- import {CachedBeaconStateAllForks} from "../types.js";
5
4
  import {
6
5
  ISignatureSet,
7
6
  SignatureSetType,
@@ -13,10 +12,10 @@ import {
13
12
  export function verifyVoluntaryExitSignature(
14
13
  config: BeaconConfig,
15
14
  index2pubkey: Index2PubkeyCache,
16
- state: CachedBeaconStateAllForks,
15
+ stateSlot: Slot,
17
16
  signedVoluntaryExit: phase0.SignedVoluntaryExit
18
17
  ): boolean {
19
- return verifySignatureSet(getVoluntaryExitSignatureSet(config, index2pubkey, state, signedVoluntaryExit));
18
+ return verifySignatureSet(getVoluntaryExitSignatureSet(config, index2pubkey, stateSlot, signedVoluntaryExit));
20
19
  }
21
20
 
22
21
  /**
@@ -25,11 +24,11 @@ export function verifyVoluntaryExitSignature(
25
24
  export function getVoluntaryExitSignatureSet(
26
25
  config: BeaconConfig,
27
26
  index2pubkey: Index2PubkeyCache,
28
- state: CachedBeaconStateAllForks,
27
+ stateSlot: Slot,
29
28
  signedVoluntaryExit: phase0.SignedVoluntaryExit
30
29
  ): ISignatureSet {
31
- const slot = computeStartSlotAtEpoch(signedVoluntaryExit.message.epoch);
32
- const domain = config.getDomainForVoluntaryExit(state.slot, slot);
30
+ const messageSlot = computeStartSlotAtEpoch(signedVoluntaryExit.message.epoch);
31
+ const domain = config.getDomainForVoluntaryExit(stateSlot, messageSlot);
33
32
 
34
33
  return {
35
34
  type: SignatureSetType.single,
@@ -42,10 +41,12 @@ export function getVoluntaryExitSignatureSet(
42
41
  export function getVoluntaryExitsSignatureSets(
43
42
  config: BeaconConfig,
44
43
  index2pubkey: Index2PubkeyCache,
45
- state: CachedBeaconStateAllForks,
46
44
  signedBlock: SignedBeaconBlock
47
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;
48
49
  return signedBlock.message.body.voluntaryExits.map((voluntaryExit) =>
49
- getVoluntaryExitSignatureSet(config, index2pubkey, state, voluntaryExit)
50
+ getVoluntaryExitSignatureSet(config, index2pubkey, blockSlot, voluntaryExit)
50
51
  );
51
52
  }
@@ -111,10 +111,7 @@ export function stateTransition(
111
111
  postState = processSlotsWithTransientCache(postState, blockSlot, options, {metrics, validatorMonitor});
112
112
 
113
113
  // Verify proposer signature only
114
- if (
115
- verifyProposer &&
116
- !verifyProposerSignature(postState.config, postState.epochCtx.index2pubkey, postState, signedBlock)
117
- ) {
114
+ if (verifyProposer && !verifyProposerSignature(postState.config, postState.epochCtx.index2pubkey, signedBlock)) {
118
115
  throw new Error("Invalid block signature");
119
116
  }
120
117
 
@@ -2,6 +2,7 @@ import {ForkName, ForkPostBellatrix, ForkPreGloas, ForkSeq} from "@lodestar/para
2
2
  import {
3
3
  BeaconBlock,
4
4
  BeaconBlockBody,
5
+ BlindedBeaconBlock,
5
6
  BlindedBeaconBlockBody,
6
7
  ExecutionPayload,
7
8
  ExecutionPayloadHeader,
@@ -9,16 +10,54 @@ import {
9
10
  capella,
10
11
  deneb,
11
12
  isBlindedBeaconBlockBody,
13
+ isExecutionPayload,
12
14
  ssz,
13
15
  } from "@lodestar/types";
14
16
  import {
15
17
  BeaconStateAllForks,
18
+ BeaconStateBellatrix,
16
19
  BeaconStateCapella,
17
20
  BeaconStateExecutions,
18
21
  CachedBeaconStateAllForks,
19
22
  CachedBeaconStateExecutions,
20
23
  } from "../types.js";
21
24
 
25
+ /**
26
+ * Execution enabled = merge is done.
27
+ * When (A) state has execution data OR (B) block has execution data
28
+ */
29
+ export function isExecutionEnabled(state: BeaconStateExecutions, block: BeaconBlock | BlindedBeaconBlock): boolean {
30
+ if (isMergeTransitionComplete(state)) {
31
+ return true;
32
+ }
33
+
34
+ // Throws if not post-bellatrix block. A fork-guard before isExecutionEnabled() prevents this from happening
35
+ const payload = getFullOrBlindedPayload(block);
36
+
37
+ return isExecutionPayload(payload)
38
+ ? !ssz.bellatrix.ExecutionPayload.equals(payload, ssz.bellatrix.ExecutionPayload.defaultValue())
39
+ : !ssz.bellatrix.ExecutionPayloadHeader.equals(
40
+ state.latestExecutionPayloadHeader,
41
+ ssz.bellatrix.ExecutionPayloadHeader.defaultValue()
42
+ );
43
+ }
44
+
45
+ /**
46
+ * Merge is complete when the state includes execution layer data:
47
+ * state.latestExecutionPayloadHeader NOT EMPTY or state is post-capella
48
+ */
49
+ export function isMergeTransitionComplete(state: BeaconStateExecutions): boolean {
50
+ if (isCapellaStateType(state)) {
51
+ // All networks have completed the merge transition before capella
52
+ return true;
53
+ }
54
+
55
+ return !ssz.bellatrix.ExecutionPayloadHeader.equals(
56
+ (state as BeaconStateBellatrix).latestExecutionPayloadHeader,
57
+ ssz.bellatrix.ExecutionPayloadHeader.defaultValue()
58
+ );
59
+ }
60
+
22
61
  /** Type guard for bellatrix.BeaconState */
23
62
  export function isExecutionStateType(state: BeaconStateAllForks): state is BeaconStateExecutions {
24
63
  return (state as BeaconStateExecutions).latestExecutionPayloadHeader !== undefined;