@lodestar/state-transition 1.39.0-dev.882891d89c → 1.39.0-dev.90493ebb47

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 (126) 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 +4 -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.js +1 -1
  12. package/lib/block/processAttestationsAltair.js.map +1 -1
  13. package/lib/block/processAttesterSlashing.d.ts +3 -2
  14. package/lib/block/processAttesterSlashing.d.ts.map +1 -1
  15. package/lib/block/processAttesterSlashing.js +3 -3
  16. package/lib/block/processAttesterSlashing.js.map +1 -1
  17. package/lib/block/processBlsToExecutionChange.d.ts +3 -1
  18. package/lib/block/processBlsToExecutionChange.d.ts.map +1 -1
  19. package/lib/block/processBlsToExecutionChange.js +7 -11
  20. package/lib/block/processBlsToExecutionChange.js.map +1 -1
  21. package/lib/block/processExecutionPayload.d.ts.map +1 -1
  22. package/lib/block/processExecutionPayload.js +6 -4
  23. package/lib/block/processExecutionPayload.js.map +1 -1
  24. package/lib/block/processProposerSlashing.d.ts +5 -2
  25. package/lib/block/processProposerSlashing.d.ts.map +1 -1
  26. package/lib/block/processProposerSlashing.js +7 -5
  27. package/lib/block/processProposerSlashing.js.map +1 -1
  28. package/lib/block/processRandao.js +2 -2
  29. package/lib/block/processRandao.js.map +1 -1
  30. package/lib/block/processSyncCommittee.d.ts +2 -1
  31. package/lib/block/processSyncCommittee.d.ts.map +1 -1
  32. package/lib/block/processSyncCommittee.js +6 -4
  33. package/lib/block/processSyncCommittee.js.map +1 -1
  34. package/lib/block/processVoluntaryExit.js +1 -1
  35. package/lib/block/processVoluntaryExit.js.map +1 -1
  36. package/lib/block/processWithdrawals.d.ts +3 -3
  37. package/lib/block/processWithdrawals.d.ts.map +1 -1
  38. package/lib/block/processWithdrawals.js +152 -105
  39. package/lib/block/processWithdrawals.js.map +1 -1
  40. package/lib/index.d.ts +2 -1
  41. package/lib/index.d.ts.map +1 -1
  42. package/lib/index.js +2 -0
  43. package/lib/index.js.map +1 -1
  44. package/lib/rewards/attestationsRewards.d.ts +6 -0
  45. package/lib/rewards/attestationsRewards.d.ts.map +1 -0
  46. package/lib/rewards/attestationsRewards.js +113 -0
  47. package/lib/rewards/attestationsRewards.js.map +1 -0
  48. package/lib/rewards/blockRewards.d.ts +13 -0
  49. package/lib/rewards/blockRewards.d.ts.map +1 -0
  50. package/lib/rewards/blockRewards.js +95 -0
  51. package/lib/rewards/blockRewards.js.map +1 -0
  52. package/lib/rewards/index.d.ts +4 -0
  53. package/lib/rewards/index.d.ts.map +1 -0
  54. package/lib/rewards/index.js +4 -0
  55. package/lib/rewards/index.js.map +1 -0
  56. package/lib/rewards/syncCommitteeRewards.d.ts +6 -0
  57. package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -0
  58. package/lib/rewards/syncCommitteeRewards.js +36 -0
  59. package/lib/rewards/syncCommitteeRewards.js.map +1 -0
  60. package/lib/signatureSets/attesterSlashings.d.ts +4 -5
  61. package/lib/signatureSets/attesterSlashings.d.ts.map +1 -1
  62. package/lib/signatureSets/attesterSlashings.js +10 -7
  63. package/lib/signatureSets/attesterSlashings.js.map +1 -1
  64. package/lib/signatureSets/blsToExecutionChange.d.ts +1 -2
  65. package/lib/signatureSets/blsToExecutionChange.d.ts.map +1 -1
  66. package/lib/signatureSets/blsToExecutionChange.js +2 -2
  67. package/lib/signatureSets/blsToExecutionChange.js.map +1 -1
  68. package/lib/signatureSets/index.d.ts +2 -2
  69. package/lib/signatureSets/index.d.ts.map +1 -1
  70. package/lib/signatureSets/index.js +10 -10
  71. package/lib/signatureSets/index.js.map +1 -1
  72. package/lib/signatureSets/indexedAttestation.d.ts +5 -6
  73. package/lib/signatureSets/indexedAttestation.d.ts.map +1 -1
  74. package/lib/signatureSets/indexedAttestation.js +12 -9
  75. package/lib/signatureSets/indexedAttestation.js.map +1 -1
  76. package/lib/signatureSets/proposer.d.ts +5 -6
  77. package/lib/signatureSets/proposer.d.ts.map +1 -1
  78. package/lib/signatureSets/proposer.js +12 -10
  79. package/lib/signatureSets/proposer.js.map +1 -1
  80. package/lib/signatureSets/proposerSlashings.d.ts +3 -4
  81. package/lib/signatureSets/proposerSlashings.d.ts.map +1 -1
  82. package/lib/signatureSets/proposerSlashings.js +7 -4
  83. package/lib/signatureSets/proposerSlashings.js.map +1 -1
  84. package/lib/signatureSets/randao.d.ts +2 -3
  85. package/lib/signatureSets/randao.d.ts.map +1 -1
  86. package/lib/signatureSets/randao.js +6 -4
  87. package/lib/signatureSets/randao.js.map +1 -1
  88. package/lib/signatureSets/voluntaryExits.d.ts +4 -5
  89. package/lib/signatureSets/voluntaryExits.d.ts.map +1 -1
  90. package/lib/signatureSets/voluntaryExits.js +10 -7
  91. package/lib/signatureSets/voluntaryExits.js.map +1 -1
  92. package/lib/stateTransition.d.ts.map +1 -1
  93. package/lib/stateTransition.js +1 -2
  94. package/lib/stateTransition.js.map +1 -1
  95. package/lib/util/execution.d.ts +11 -1
  96. package/lib/util/execution.d.ts.map +1 -1
  97. package/lib/util/execution.js +26 -1
  98. package/lib/util/execution.js.map +1 -1
  99. package/package.json +14 -11
  100. package/src/block/index.ts +6 -2
  101. package/src/block/isValidIndexedAttestation.ts +19 -12
  102. package/src/block/processAttestationPhase0.ts +2 -1
  103. package/src/block/processAttestationsAltair.ts +1 -1
  104. package/src/block/processAttesterSlashing.ts +16 -4
  105. package/src/block/processBlsToExecutionChange.ts +13 -14
  106. package/src/block/processExecutionPayload.ts +14 -8
  107. package/src/block/processProposerSlashing.ts +21 -11
  108. package/src/block/processRandao.ts +2 -2
  109. package/src/block/processSyncCommittee.ts +7 -4
  110. package/src/block/processVoluntaryExit.ts +1 -1
  111. package/src/block/processWithdrawals.ts +230 -135
  112. package/src/index.ts +2 -2
  113. package/src/rewards/attestationsRewards.ts +200 -0
  114. package/src/rewards/blockRewards.ts +147 -0
  115. package/src/rewards/index.ts +3 -0
  116. package/src/rewards/syncCommitteeRewards.ts +59 -0
  117. package/src/signatureSets/attesterSlashings.ts +10 -9
  118. package/src/signatureSets/blsToExecutionChange.ts +2 -3
  119. package/src/signatureSets/index.ts +11 -11
  120. package/src/signatureSets/indexedAttestation.ts +12 -11
  121. package/src/signatureSets/proposer.ts +11 -11
  122. package/src/signatureSets/proposerSlashings.ts +7 -6
  123. package/src/signatureSets/randao.ts +4 -5
  124. package/src/signatureSets/voluntaryExits.ts +10 -9
  125. package/src/stateTransition.ts +1 -4
  126. package/src/util/execution.ts +39 -0
@@ -2,27 +2,27 @@ 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
 
9
8
  export function verifyProposerSignature(
10
9
  config: BeaconConfig,
11
10
  index2pubkey: Index2PubkeyCache,
12
- state: CachedBeaconStateAllForks,
13
11
  signedBlock: SignedBeaconBlock | SignedBlindedBeaconBlock
14
12
  ): boolean {
15
- const signatureSet = getBlockProposerSignatureSet(config, index2pubkey, state, signedBlock);
13
+ const signatureSet = getBlockProposerSignatureSet(config, index2pubkey, signedBlock);
16
14
  return verifySignatureSet(signatureSet);
17
15
  }
18
16
 
19
17
  export function getBlockProposerSignatureSet(
20
18
  config: BeaconConfig,
21
19
  index2pubkey: Index2PubkeyCache,
22
- state: CachedBeaconStateAllForks,
23
20
  signedBlock: SignedBeaconBlock | SignedBlindedBeaconBlock
24
21
  ): ISignatureSet {
25
- 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);
26
26
 
27
27
  const blockType = isBlindedBeaconBlock(signedBlock.message)
28
28
  ? config.getPostBellatrixForkTypes(signedBlock.message.slot).BlindedBeaconBlock
@@ -37,28 +37,28 @@ export function getBlockProposerSignatureSet(
37
37
  }
38
38
 
39
39
  export function getBlockHeaderProposerSignatureSetByParentStateSlot(
40
+ config: BeaconConfig,
40
41
  index2pubkey: Index2PubkeyCache,
41
- parentState: CachedBeaconStateAllForks,
42
+ parentStateSlot: Slot,
42
43
  signedBlockHeader: phase0.SignedBeaconBlockHeader
43
44
  ) {
44
- return getBlockHeaderProposerSignatureSet(index2pubkey, parentState, signedBlockHeader, parentState.slot);
45
+ return getBlockHeaderProposerSignatureSet(config, index2pubkey, signedBlockHeader, parentStateSlot);
45
46
  }
46
47
 
47
48
  export function getBlockHeaderProposerSignatureSetByHeaderSlot(
49
+ config: BeaconConfig,
48
50
  index2pubkey: Index2PubkeyCache,
49
- headState: CachedBeaconStateAllForks,
50
51
  signedBlockHeader: phase0.SignedBeaconBlockHeader
51
52
  ) {
52
- return getBlockHeaderProposerSignatureSet(index2pubkey, headState, signedBlockHeader, signedBlockHeader.message.slot);
53
+ return getBlockHeaderProposerSignatureSet(config, index2pubkey, signedBlockHeader, signedBlockHeader.message.slot);
53
54
  }
54
55
 
55
56
  function getBlockHeaderProposerSignatureSet(
57
+ config: BeaconConfig,
56
58
  index2pubkey: Index2PubkeyCache,
57
- state: CachedBeaconStateAllForks,
58
59
  signedBlockHeader: phase0.SignedBeaconBlockHeader,
59
60
  domainSlot: Slot
60
61
  ): ISignatureSet {
61
- const {config} = state;
62
62
  const domain = config.getDomain(domainSlot, DOMAIN_BEACON_PROPOSER, signedBlockHeader.message.slot);
63
63
 
64
64
  return {
@@ -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;