@lodestar/beacon-node 1.39.0-dev.075956b855 → 1.39.0-dev.100ab480bb

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.
@@ -1,112 +0,0 @@
1
- import { EFFECTIVE_BALANCE_INCREMENT, ForkName, INACTIVITY_PENALTY_QUOTIENT_ALTAIR, MAX_EFFECTIVE_BALANCE, MAX_EFFECTIVE_BALANCE_ELECTRA, PARTICIPATION_FLAG_WEIGHTS, TIMELY_HEAD_FLAG_INDEX, TIMELY_SOURCE_FLAG_INDEX, TIMELY_TARGET_FLAG_INDEX, WEIGHT_DENOMINATOR, isForkPostElectra, } from "@lodestar/params";
2
- import { FLAG_ELIGIBLE_ATTESTER, FLAG_PREV_HEAD_ATTESTER_UNSLASHED, FLAG_PREV_SOURCE_ATTESTER_UNSLASHED, FLAG_PREV_TARGET_ATTESTER_UNSLASHED, beforeProcessEpoch, hasMarkers, isInInactivityLeak, } from "@lodestar/state-transition";
3
- import { fromHex } from "@lodestar/utils";
4
- const defaultAttestationsReward = { head: 0, target: 0, source: 0, inclusionDelay: 0, inactivity: 0 };
5
- const defaultAttestationsPenalty = { target: 0, source: 0 };
6
- export async function computeAttestationsRewards(config, pubkey2index, state, validatorIds) {
7
- const fork = config.getForkName(state.slot);
8
- if (fork === ForkName.phase0) {
9
- throw Error("Unsupported fork. Attestations rewards calculation is not available in phase0");
10
- }
11
- const stateAltair = state;
12
- const transitionCache = beforeProcessEpoch(stateAltair);
13
- const [idealRewards, penalties] = computeIdealAttestationsRewardsAndPenaltiesAltair(config, stateAltair, transitionCache);
14
- const totalRewards = computeTotalAttestationsRewardsAltair(config, pubkey2index, stateAltair, transitionCache, idealRewards, penalties, validatorIds);
15
- return { idealRewards, totalRewards };
16
- }
17
- function computeIdealAttestationsRewardsAndPenaltiesAltair(config, state, transitionCache) {
18
- const baseRewardPerIncrement = transitionCache.baseRewardPerIncrement;
19
- const activeBalanceByIncrement = transitionCache.totalActiveStakeByIncrement;
20
- const fork = config.getForkName(state.slot);
21
- const maxEffectiveBalance = isForkPostElectra(fork) ? MAX_EFFECTIVE_BALANCE_ELECTRA : MAX_EFFECTIVE_BALANCE;
22
- const maxEffectiveBalanceByIncrement = Math.floor(maxEffectiveBalance / EFFECTIVE_BALANCE_INCREMENT);
23
- const idealRewards = Array.from({ length: maxEffectiveBalanceByIncrement + 1 }, (_, effectiveBalanceByIncrement) => ({
24
- ...defaultAttestationsReward,
25
- effectiveBalance: effectiveBalanceByIncrement * EFFECTIVE_BALANCE_INCREMENT,
26
- }));
27
- const attestationsPenalties = Array.from({ length: maxEffectiveBalanceByIncrement + 1 }, (_, effectiveBalanceByIncrement) => ({
28
- ...defaultAttestationsPenalty,
29
- effectiveBalance: effectiveBalanceByIncrement * EFFECTIVE_BALANCE_INCREMENT,
30
- }));
31
- for (let i = 0; i < PARTICIPATION_FLAG_WEIGHTS.length; i++) {
32
- const weight = PARTICIPATION_FLAG_WEIGHTS[i];
33
- let unslashedStakeByIncrement;
34
- let flagName;
35
- switch (i) {
36
- case TIMELY_SOURCE_FLAG_INDEX: {
37
- unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.sourceStakeByIncrement;
38
- flagName = "source";
39
- break;
40
- }
41
- case TIMELY_TARGET_FLAG_INDEX: {
42
- unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.targetStakeByIncrement;
43
- flagName = "target";
44
- break;
45
- }
46
- case TIMELY_HEAD_FLAG_INDEX: {
47
- unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.headStakeByIncrement;
48
- flagName = "head";
49
- break;
50
- }
51
- default: {
52
- throw Error(`Unable to retrieve unslashed stake. Unknown participation flag index: ${i}`);
53
- }
54
- }
55
- for (let effectiveBalanceByIncrement = 0; effectiveBalanceByIncrement <= maxEffectiveBalanceByIncrement; effectiveBalanceByIncrement++) {
56
- const baseReward = effectiveBalanceByIncrement * baseRewardPerIncrement;
57
- const rewardNumerator = baseReward * weight * unslashedStakeByIncrement;
58
- // Both idealReward and penalty are rounded to nearest integer. Loss of precision is minimal as unit is gwei
59
- const idealReward = Math.round(rewardNumerator / activeBalanceByIncrement / WEIGHT_DENOMINATOR);
60
- const penalty = Math.round((baseReward * weight) / WEIGHT_DENOMINATOR); // Positive number indicates penalty
61
- const idealAttestationsReward = idealRewards[effectiveBalanceByIncrement];
62
- idealAttestationsReward[flagName] = isInInactivityLeak(state) ? 0 : idealReward; // No attestations rewards during inactivity leak
63
- if (flagName !== "head") {
64
- const attestationPenalty = attestationsPenalties[effectiveBalanceByIncrement];
65
- attestationPenalty[flagName] = penalty;
66
- }
67
- }
68
- }
69
- return [idealRewards, attestationsPenalties];
70
- }
71
- // Same calculation as `getRewardsAndPenaltiesAltair` but returns the breakdown of rewards instead of aggregated
72
- function computeTotalAttestationsRewardsAltair(config, pubkey2index, state, transitionCache, idealRewards, penalties, validatorIds = []) {
73
- const rewards = [];
74
- const { flags } = transitionCache;
75
- const { epochCtx } = state;
76
- const validatorIndices = validatorIds
77
- .map((id) => (typeof id === "number" ? id : pubkey2index.get(fromHex(id))))
78
- .filter((index) => index !== undefined); // Validator indices to include in the result
79
- const inactivityPenaltyDenominator = config.INACTIVITY_SCORE_BIAS * INACTIVITY_PENALTY_QUOTIENT_ALTAIR;
80
- for (let i = 0; i < flags.length; i++) {
81
- if (validatorIndices.length && !validatorIndices.includes(i)) {
82
- continue;
83
- }
84
- const flag = flags[i];
85
- if (!hasMarkers(flag, FLAG_ELIGIBLE_ATTESTER)) {
86
- continue;
87
- }
88
- const effectiveBalanceIncrement = epochCtx.effectiveBalanceIncrements[i];
89
- const currentRewards = { ...defaultAttestationsReward, validatorIndex: i };
90
- if (hasMarkers(flag, FLAG_PREV_SOURCE_ATTESTER_UNSLASHED)) {
91
- currentRewards.source = idealRewards[effectiveBalanceIncrement].source;
92
- }
93
- else {
94
- currentRewards.source = penalties[effectiveBalanceIncrement].source * -1; // Negative reward to indicate penalty
95
- }
96
- if (hasMarkers(flag, FLAG_PREV_TARGET_ATTESTER_UNSLASHED)) {
97
- currentRewards.target = idealRewards[effectiveBalanceIncrement].target;
98
- }
99
- else {
100
- currentRewards.target = penalties[effectiveBalanceIncrement].target * -1;
101
- // Also incur inactivity penalty if not voting target correctly
102
- const inactivityPenaltyNumerator = effectiveBalanceIncrement * EFFECTIVE_BALANCE_INCREMENT * state.inactivityScores.get(i);
103
- currentRewards.inactivity = Math.floor(inactivityPenaltyNumerator / inactivityPenaltyDenominator) * -1;
104
- }
105
- if (hasMarkers(flag, FLAG_PREV_HEAD_ATTESTER_UNSLASHED)) {
106
- currentRewards.head = idealRewards[effectiveBalanceIncrement].head;
107
- }
108
- rewards.push(currentRewards);
109
- }
110
- return rewards;
111
- }
112
- //# sourceMappingURL=attestationsRewards.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"attestationsRewards.js","sourceRoot":"","sources":["../../../src/chain/rewards/attestationsRewards.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,2BAA2B,EAC3B,QAAQ,EACR,kCAAkC,EAClC,qBAAqB,EACrB,6BAA6B,EAC7B,0BAA0B,EAC1B,sBAAsB,EACtB,wBAAwB,EACxB,wBAAwB,EACxB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAIL,sBAAsB,EACtB,iCAAiC,EACjC,mCAAmC,EACnC,mCAAmC,EACnC,kBAAkB,EAClB,UAAU,EACV,kBAAkB,GACnB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAC,OAAO,EAAC,MAAM,iBAAiB,CAAC;AAQxC,MAAM,yBAAyB,GAAG,EAAC,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,cAAc,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAC,CAAC;AACpG,MAAM,0BAA0B,GAAG,EAAC,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAC,CAAC;AAE1D,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M,CAAC;YACN,cAAc,CAAC,MAAM,GAAG,SAAS,CAAC,yBAAyB,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YAEzE,+DAA+D;YAC/D,MAAM,0BAA0B,GAC9B,yBAAyB,GAAG,2BAA2B,GAAG,KAAK,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC1F,cAAc,CAAC,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,0BAA0B,GAAG,4BAA4B,CAAC,GAAG,CAAC,CAAC,CAAC;QACzG,CAAC;QAED,IAAI,UAAU,CAAC,IAAI,EAAE,iCAAiC,CAAC,EAAE,CAAC;YACxD,cAAc,CAAC,IAAI,GAAG,YAAY,CAAC,yBAAyB,CAAC,CAAC,IAAI,CAAC;QACrE,CAAC;QAED,OAAO,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;IAC/B,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -1,15 +0,0 @@
1
- import { routes } from "@lodestar/api";
2
- import { BeaconConfig } from "@lodestar/config";
3
- import { CachedBeaconStateAllForks } from "@lodestar/state-transition";
4
- import { BeaconBlock } from "@lodestar/types";
5
- export type BlockRewards = routes.beacon.BlockRewards;
6
- /**
7
- * Calculate total proposer block rewards given block and the beacon state of the same slot before the block is applied (preState)
8
- * postState can be passed in to read reward cache if available
9
- * Standard (Non MEV) rewards for proposing a block consists of:
10
- * 1) Including attestations from (beacon) committee
11
- * 2) Including attestations from sync committee
12
- * 3) Reporting slashable behaviours from proposer and attester
13
- */
14
- export declare function computeBlockRewards(config: BeaconConfig, block: BeaconBlock, preState: CachedBeaconStateAllForks, postState?: CachedBeaconStateAllForks): Promise<BlockRewards>;
15
- //# sourceMappingURL=blockRewards.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"blockRewards.d.ts","sourceRoot":"","sources":["../../../src/chain/rewards/blockRewards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,eAAe,CAAC;AACrC,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAO9C,OAAO,EACL,yBAAyB,EAK1B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAC,WAAW,EAAiB,MAAM,iBAAiB,CAAC;AAE5D,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC;AAGtD;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CACvC,MAAM,EAAE,YAAY,EACpB,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,yBAAyB,EACnC,SAAS,CAAC,EAAE,yBAAyB,GACpC,OAAO,CAAC,YAAY,CAAC,CAgCvB"}
@@ -1,94 +0,0 @@
1
- import { ForkName, WHISTLEBLOWER_REWARD_QUOTIENT, WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA, isForkPostElectra, } from "@lodestar/params";
2
- import { getAttesterSlashableIndices, processAttestationsAltair, } from "@lodestar/state-transition";
3
- /**
4
- * Calculate total proposer block rewards given block and the beacon state of the same slot before the block is applied (preState)
5
- * postState can be passed in to read reward cache if available
6
- * Standard (Non MEV) rewards for proposing a block consists of:
7
- * 1) Including attestations from (beacon) committee
8
- * 2) Including attestations from sync committee
9
- * 3) Reporting slashable behaviours from proposer and attester
10
- */
11
- export async function computeBlockRewards(config, block, preState, postState) {
12
- const fork = config.getForkName(block.slot);
13
- const { attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0 } = postState?.proposerRewards ?? {};
14
- let blockAttestationReward = cachedAttestationsReward;
15
- let syncAggregateReward = cachedSyncAggregateReward;
16
- if (blockAttestationReward === 0) {
17
- blockAttestationReward =
18
- fork === ForkName.phase0
19
- ? computeBlockAttestationRewardPhase0(block, preState)
20
- : computeBlockAttestationRewardAltair(config, block, preState);
21
- }
22
- if (syncAggregateReward === 0) {
23
- syncAggregateReward = computeSyncAggregateReward(block, preState);
24
- }
25
- const blockProposerSlashingReward = computeBlockProposerSlashingReward(fork, block, preState);
26
- const blockAttesterSlashingReward = computeBlockAttesterSlashingReward(fork, block, preState);
27
- const total = blockAttestationReward + syncAggregateReward + blockProposerSlashingReward + blockAttesterSlashingReward;
28
- return {
29
- proposerIndex: block.proposerIndex,
30
- total,
31
- attestations: blockAttestationReward,
32
- syncAggregate: syncAggregateReward,
33
- proposerSlashings: blockProposerSlashingReward,
34
- attesterSlashings: blockAttesterSlashingReward,
35
- };
36
- }
37
- /**
38
- * TODO: Calculate rewards received by block proposer for including attestations.
39
- */
40
- function computeBlockAttestationRewardPhase0(_block, _preState) {
41
- throw new Error("Unsupported fork! Block attestation reward calculation is not available in phase0");
42
- }
43
- /**
44
- * Calculate rewards received by block proposer for including attestations since Altair.
45
- * Reuses `processAttestationsAltair()`. Has dependency on RewardCache
46
- */
47
- function computeBlockAttestationRewardAltair(config, block, preState) {
48
- const fork = config.getForkSeq(block.slot);
49
- const { attestations } = block.body;
50
- processAttestationsAltair(fork, preState, attestations, false);
51
- return preState.proposerRewards.attestations;
52
- }
53
- function computeSyncAggregateReward(block, preState) {
54
- if (block.body.syncAggregate !== undefined) {
55
- const { syncCommitteeBits } = block.body.syncAggregate;
56
- const { syncProposerReward } = preState.epochCtx;
57
- return syncCommitteeBits.getTrueBitIndexes().length * Math.floor(syncProposerReward); // syncProposerReward should already be integer
58
- }
59
- return 0; // phase0 block does not have syncAggregate
60
- }
61
- /**
62
- * Calculate rewards received by block proposer for including proposer slashings.
63
- * All proposer slashing rewards go to block proposer and none to whistleblower as of Deneb
64
- */
65
- function computeBlockProposerSlashingReward(fork, block, state) {
66
- let proposerSlashingReward = 0;
67
- for (const proposerSlashing of block.body.proposerSlashings) {
68
- const offendingProposerIndex = proposerSlashing.signedHeader1.message.proposerIndex;
69
- const offendingProposerBalance = state.validators.getReadonly(offendingProposerIndex).effectiveBalance;
70
- const whistleblowerRewardQuotient = isForkPostElectra(fork)
71
- ? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
72
- : WHISTLEBLOWER_REWARD_QUOTIENT;
73
- proposerSlashingReward += Math.floor(offendingProposerBalance / whistleblowerRewardQuotient);
74
- }
75
- return proposerSlashingReward;
76
- }
77
- /**
78
- * Calculate rewards received by block proposer for including attester slashings.
79
- * All attester slashing rewards go to block proposer and none to whistleblower as of Deneb
80
- */
81
- function computeBlockAttesterSlashingReward(fork, block, preState) {
82
- let attesterSlashingReward = 0;
83
- for (const attesterSlashing of block.body.attesterSlashings) {
84
- for (const offendingAttesterIndex of getAttesterSlashableIndices(attesterSlashing)) {
85
- const offendingAttesterBalance = preState.validators.getReadonly(offendingAttesterIndex).effectiveBalance;
86
- const whistleblowerRewardQuotient = isForkPostElectra(fork)
87
- ? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
88
- : WHISTLEBLOWER_REWARD_QUOTIENT;
89
- attesterSlashingReward += Math.floor(offendingAttesterBalance / whistleblowerRewardQuotient);
90
- }
91
- }
92
- return attesterSlashingReward;
93
- }
94
- //# sourceMappingURL=blockRewards.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"blockRewards.js","sourceRoot":"","sources":["../../../src/chain/rewards/blockRewards.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,QAAQ,EACR,6BAA6B,EAC7B,qCAAqC,EACrC,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAIL,2BAA2B,EAC3B,yBAAyB,GAC1B,MAAM,4BAA4B,CAAC;AAMpC;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,MAAoB,EACpB,KAAkB,EAClB,QAAmC,EACnC,SAAqC;IAErC,MAAM,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC5C,MAAM,EAAC,YAAY,EAAE,wBAAwB,GAAG,CAAC,EAAE,aAAa,EAAE,yBAAyB,GAAG,CAAC,EAAC,GAC9F,SAAS,EAAE,eAAe,IAAI,EAAE,CAAC;IACnC,IAAI,sBAA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AmC;IAEnC,IAAI,sBAAsB,GAAG,CAAC,CAAC;IAE/B,KAAK,MAAM,gBAAgB,IAAI,KAAK,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC;QAC5D,KAAK,MAAM,sBAAsB,IAAI,2BAA2B,CAAC,gBAAgB,CAAC,EAAE,CAAC;YACnF,MAAM,wBAAwB,GAAG,QAAQ,CAAC,UAAU,CAAC,WAAW,CAAC,sBAAsB,CAAC,CAAC,gBAAgB,CAAC;YAC1G,MAAM,2BAA2B,GAAG,iBAAiB,CAAC,IAAI,CAAC;gBACzD,CAAC,CAAC,qCAAqC;gBACvC,CAAC,CAAC,6BAA6B,CAAC;YAElC,sBAAsB,IAAI,IAAI,CAAC,KAAK,CAAC,wBAAwB,GAAG,2BAA2B,CAAC,CAAC;QAC/F,CAAC;IACH,CAAC;IAED,OAAO,sBAAsB,CAAC;AAChC,CAAC"}
@@ -1,7 +0,0 @@
1
- import { routes } from "@lodestar/api";
2
- import { BeaconConfig } from "@lodestar/config";
3
- import { CachedBeaconStateAllForks, Index2PubkeyCache } from "@lodestar/state-transition";
4
- import { BeaconBlock, ValidatorIndex } from "@lodestar/types";
5
- export type SyncCommitteeRewards = routes.beacon.SyncCommitteeRewards;
6
- export declare function computeSyncCommitteeRewards(config: BeaconConfig, index2pubkey: Index2PubkeyCache, block: BeaconBlock, preState: CachedBeaconStateAllForks, validatorIds?: (ValidatorIndex | string)[]): Promise<SyncCommitteeRewards>;
7
- //# sourceMappingURL=syncCommitteeRewards.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"syncCommitteeRewards.d.ts","sourceRoot":"","sources":["../../../src/chain/rewards/syncCommitteeRewards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,eAAe,CAAC;AACrC,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAC,yBAAyB,EAA2B,iBAAiB,EAAC,MAAM,4BAA4B,CAAC;AACjH,OAAO,EAAC,WAAW,EAAE,cAAc,EAAS,MAAM,iBAAiB,CAAC;AAEpE,MAAM,MAAM,oBAAoB,GAAG,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC;AAGtE,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,YAAY,EACpB,YAAY,EAAE,iBAAiB,EAC/B,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,yBAAyB,EACnC,YAAY,GAAE,CAAC,cAAc,GAAG,MAAM,CAAC,EAAO,GAC7C,OAAO,CAAC,oBAAoB,CAAC,CA4C/B"}
@@ -1,36 +0,0 @@
1
- import { ForkName, SYNC_COMMITTEE_SIZE } from "@lodestar/params";
2
- export async function computeSyncCommitteeRewards(config, index2pubkey, block, preState, validatorIds = []) {
3
- const fork = config.getForkName(block.slot);
4
- if (fork === ForkName.phase0) {
5
- throw Error("Cannot get sync rewards as phase0 block does not have sync committee");
6
- }
7
- const altairBlock = block;
8
- const preStateAltair = preState;
9
- // Bound syncCommitteeValidatorIndices in case it goes beyond SYNC_COMMITTEE_SIZE just to be safe
10
- const syncCommitteeValidatorIndices = preStateAltair.epochCtx.currentSyncCommitteeIndexed.validatorIndices.slice(0, SYNC_COMMITTEE_SIZE);
11
- const { syncParticipantReward } = preStateAltair.epochCtx;
12
- const { syncCommitteeBits } = altairBlock.body.syncAggregate;
13
- // Use balance of each committee as starting point such that we cap the penalty to avoid balance dropping below 0
14
- const balances = new Map();
15
- for (const i of syncCommitteeValidatorIndices) {
16
- balances.set(i, { val: preStateAltair.balances.get(i) });
17
- }
18
- for (const i of syncCommitteeValidatorIndices) {
19
- const balanceRecord = balances.get(i);
20
- if (syncCommitteeBits.get(i)) {
21
- // Positive rewards for participants
22
- balanceRecord.val += syncParticipantReward;
23
- }
24
- else {
25
- // Negative rewards for non participants
26
- balanceRecord.val = Math.max(0, balanceRecord.val - syncParticipantReward);
27
- }
28
- }
29
- const rewards = Array.from(balances, ([validatorIndex, v]) => ({ validatorIndex, reward: v.val }));
30
- if (validatorIds.length) {
31
- const filtersSet = new Set(validatorIds);
32
- return rewards.filter((reward) => filtersSet.has(reward.validatorIndex) || filtersSet.has(index2pubkey[reward.validatorIndex].toHex()));
33
- }
34
- return rewards;
35
- }
36
- //# sourceMappingURL=syncCommitteeRewards.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"syncCommitteeRewards.js","sourceRoot":"","sources":["../../../src/chain/rewards/syncCommitteeRewards.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,QAAQ,EAAE,mBAAmB,EAAC,MAAM,kBAAkB,CAAC;AAO/D,MAAM,CAAC,KAAK,UAAU,2BAA2B,CAC/C,MAAoB,EACpB,YAA+B,EAC/B,KAAkB,EAClB,QAAmC,EACnC,eAA4C,EAAE;IAE9C,MAAM,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC5C,IAAI,IAAI,KAAK,QAAQ,CAAC,MAAM,EAAE,CAAC;QAC7B,MAAM,KAAK,CAAC,sEAAsE,CAAC,CAAC;IACtF,CAAC;IAED,MAAM,WAAW,GAAG,KAA2B,CAAC;IAChD,MAAM,cAAc,GAAG,QAAmC,CAAC;IAE3D,iGAAiG;IACjG,MAAM,6BAA6B,GAAG,cAAc,CAAC,QAAQ,CAAC,2BAA2B,CAAC,gBAAgB,CAAC,KAAK,CAC9G,CAAC,EACD,mBAAmB,CACpB,CAAC;IACF,MAAM,EAAC,qBAAqB,EAAC,GAAG,cAAc,CAAC,QAAQ,CAAC;IACxD,MAAM,EAAC,iBAAiB,EAAC,GAAG,WAAW,CAAC,IAAI,CAAC,aAAa,CAAC;IAE3D,iHAAiH;IACjH,MAAM,QAAQ,GAAuC,IAAI,GAAG,EAAE,CAAC;IAC/D,KAAK,MAAM,CAAC,IAAI,6BAA6B,EAAE,CAAC;QAC9C,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,EAAC,GAAG,EAAE,cAAc,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAC,CAAC,CAAC;IACzD,CAAC;IAED,KAAK,MAAM,CAAC,IAAI,6BAA6B,EAAE,CAAC;QAC9C,MAAM,aAAa,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAkB,CAAC;QACvD,IAAI,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;YAC7B,oCAAoC;YACpC,aAAa,CAAC,GAAG,IAAI,qBAAqB,CAAC;QAC7C,CAAC;aAAM,CAAC;YACN,wCAAwC;YACxC,aAAa,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,CAAC,GAAG,GAAG,qBAAqB,CAAC,CAAC;QAC7E,CAAC;IACH,CAAC;IAED,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC,cAAc,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAC,cAAc,EAAE,MAAM,EAAE,CAAC,CAAC,GAAG,EAAC,CAAC,CAAC,CAAC;IAEjG,IAAI,YAAY,CAAC,MAAM,EAAE,CAAC;QACxB,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,YAAY,CAAC,CAAC;QACzC,OAAO,OAAO,CAAC,MAAM,CACnB,CAAC,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,cAAc,CAAC,IAAI,UAAU,CAAC,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,KAAK,EAAE,CAAC,CACjH,CAAC;IACJ,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -1,206 +0,0 @@
1
- import {PubkeyIndexMap} from "@chainsafe/pubkey-index-map";
2
- import {routes} from "@lodestar/api";
3
- import {BeaconConfig} from "@lodestar/config";
4
- import {
5
- EFFECTIVE_BALANCE_INCREMENT,
6
- ForkName,
7
- INACTIVITY_PENALTY_QUOTIENT_ALTAIR,
8
- MAX_EFFECTIVE_BALANCE,
9
- MAX_EFFECTIVE_BALANCE_ELECTRA,
10
- PARTICIPATION_FLAG_WEIGHTS,
11
- TIMELY_HEAD_FLAG_INDEX,
12
- TIMELY_SOURCE_FLAG_INDEX,
13
- TIMELY_TARGET_FLAG_INDEX,
14
- WEIGHT_DENOMINATOR,
15
- isForkPostElectra,
16
- } from "@lodestar/params";
17
- import {
18
- CachedBeaconStateAllForks,
19
- CachedBeaconStateAltair,
20
- EpochTransitionCache,
21
- FLAG_ELIGIBLE_ATTESTER,
22
- FLAG_PREV_HEAD_ATTESTER_UNSLASHED,
23
- FLAG_PREV_SOURCE_ATTESTER_UNSLASHED,
24
- FLAG_PREV_TARGET_ATTESTER_UNSLASHED,
25
- beforeProcessEpoch,
26
- hasMarkers,
27
- isInInactivityLeak,
28
- } from "@lodestar/state-transition";
29
- import {ValidatorIndex} from "@lodestar/types";
30
- import {fromHex} from "@lodestar/utils";
31
-
32
- export type AttestationsRewards = routes.beacon.AttestationsRewards;
33
- type IdealAttestationsReward = routes.beacon.IdealAttestationsReward;
34
- type TotalAttestationsReward = routes.beacon.TotalAttestationsReward;
35
- /** Attestations penalty with respect to effective balance in Gwei */
36
- type AttestationsPenalty = {target: number; source: number; effectiveBalance: number};
37
-
38
- const defaultAttestationsReward = {head: 0, target: 0, source: 0, inclusionDelay: 0, inactivity: 0};
39
- const defaultAttestationsPenalty = {target: 0, source: 0};
40
-
41
- export async function computeAttestationsRewards(
42
- config: BeaconConfig,
43
- pubkey2index: PubkeyIndexMap,
44
- state: CachedBeaconStateAllForks,
45
- validatorIds?: (ValidatorIndex | string)[]
46
- ): Promise<AttestationsRewards> {
47
- const fork = config.getForkName(state.slot);
48
- if (fork === ForkName.phase0) {
49
- throw Error("Unsupported fork. Attestations rewards calculation is not available in phase0");
50
- }
51
-
52
- const stateAltair = state as CachedBeaconStateAltair;
53
- const transitionCache = beforeProcessEpoch(stateAltair);
54
-
55
- const [idealRewards, penalties] = computeIdealAttestationsRewardsAndPenaltiesAltair(
56
- config,
57
- stateAltair,
58
- transitionCache
59
- );
60
- const totalRewards = computeTotalAttestationsRewardsAltair(
61
- config,
62
- pubkey2index,
63
- stateAltair,
64
- transitionCache,
65
- idealRewards,
66
- penalties,
67
- validatorIds
68
- );
69
-
70
- return {idealRewards, totalRewards};
71
- }
72
-
73
- function computeIdealAttestationsRewardsAndPenaltiesAltair(
74
- config: BeaconConfig,
75
- state: CachedBeaconStateAllForks,
76
- transitionCache: EpochTransitionCache
77
- ): [IdealAttestationsReward[], AttestationsPenalty[]] {
78
- const baseRewardPerIncrement = transitionCache.baseRewardPerIncrement;
79
- const activeBalanceByIncrement = transitionCache.totalActiveStakeByIncrement;
80
- const fork = config.getForkName(state.slot);
81
- const maxEffectiveBalance = isForkPostElectra(fork) ? MAX_EFFECTIVE_BALANCE_ELECTRA : MAX_EFFECTIVE_BALANCE;
82
- const maxEffectiveBalanceByIncrement = Math.floor(maxEffectiveBalance / EFFECTIVE_BALANCE_INCREMENT);
83
-
84
- const idealRewards = Array.from({length: maxEffectiveBalanceByIncrement + 1}, (_, effectiveBalanceByIncrement) => ({
85
- ...defaultAttestationsReward,
86
- effectiveBalance: effectiveBalanceByIncrement * EFFECTIVE_BALANCE_INCREMENT,
87
- }));
88
-
89
- const attestationsPenalties: AttestationsPenalty[] = Array.from(
90
- {length: maxEffectiveBalanceByIncrement + 1},
91
- (_, effectiveBalanceByIncrement) => ({
92
- ...defaultAttestationsPenalty,
93
- effectiveBalance: effectiveBalanceByIncrement * EFFECTIVE_BALANCE_INCREMENT,
94
- })
95
- );
96
-
97
- for (let i = 0; i < PARTICIPATION_FLAG_WEIGHTS.length; i++) {
98
- const weight = PARTICIPATION_FLAG_WEIGHTS[i];
99
-
100
- let unslashedStakeByIncrement: number;
101
- let flagName: keyof IdealAttestationsReward;
102
-
103
- switch (i) {
104
- case TIMELY_SOURCE_FLAG_INDEX: {
105
- unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.sourceStakeByIncrement;
106
- flagName = "source";
107
- break;
108
- }
109
- case TIMELY_TARGET_FLAG_INDEX: {
110
- unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.targetStakeByIncrement;
111
- flagName = "target";
112
- break;
113
- }
114
- case TIMELY_HEAD_FLAG_INDEX: {
115
- unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.headStakeByIncrement;
116
- flagName = "head";
117
- break;
118
- }
119
- default: {
120
- throw Error(`Unable to retrieve unslashed stake. Unknown participation flag index: ${i}`);
121
- }
122
- }
123
-
124
- for (
125
- let effectiveBalanceByIncrement = 0;
126
- effectiveBalanceByIncrement <= maxEffectiveBalanceByIncrement;
127
- effectiveBalanceByIncrement++
128
- ) {
129
- const baseReward = effectiveBalanceByIncrement * baseRewardPerIncrement;
130
- const rewardNumerator = baseReward * weight * unslashedStakeByIncrement;
131
- // Both idealReward and penalty are rounded to nearest integer. Loss of precision is minimal as unit is gwei
132
- const idealReward = Math.round(rewardNumerator / activeBalanceByIncrement / WEIGHT_DENOMINATOR);
133
- const penalty = Math.round((baseReward * weight) / WEIGHT_DENOMINATOR); // Positive number indicates penalty
134
-
135
- const idealAttestationsReward = idealRewards[effectiveBalanceByIncrement];
136
- idealAttestationsReward[flagName] = isInInactivityLeak(state) ? 0 : idealReward; // No attestations rewards during inactivity leak
137
-
138
- if (flagName !== "head") {
139
- const attestationPenalty = attestationsPenalties[effectiveBalanceByIncrement];
140
- attestationPenalty[flagName] = penalty;
141
- }
142
- }
143
- }
144
-
145
- return [idealRewards, attestationsPenalties];
146
- }
147
-
148
- // Same calculation as `getRewardsAndPenaltiesAltair` but returns the breakdown of rewards instead of aggregated
149
- function computeTotalAttestationsRewardsAltair(
150
- config: BeaconConfig,
151
- pubkey2index: PubkeyIndexMap,
152
- state: CachedBeaconStateAltair,
153
- transitionCache: EpochTransitionCache,
154
- idealRewards: IdealAttestationsReward[],
155
- penalties: AttestationsPenalty[],
156
- validatorIds: (ValidatorIndex | string)[] = []
157
- ): TotalAttestationsReward[] {
158
- const rewards = [];
159
- const {flags} = transitionCache;
160
- const {epochCtx} = state;
161
- const validatorIndices = validatorIds
162
- .map((id) => (typeof id === "number" ? id : pubkey2index.get(fromHex(id))))
163
- .filter((index) => index !== undefined); // Validator indices to include in the result
164
-
165
- const inactivityPenaltyDenominator = config.INACTIVITY_SCORE_BIAS * INACTIVITY_PENALTY_QUOTIENT_ALTAIR;
166
-
167
- for (let i = 0; i < flags.length; i++) {
168
- if (validatorIndices.length && !validatorIndices.includes(i)) {
169
- continue;
170
- }
171
-
172
- const flag = flags[i];
173
- if (!hasMarkers(flag, FLAG_ELIGIBLE_ATTESTER)) {
174
- continue;
175
- }
176
-
177
- const effectiveBalanceIncrement = epochCtx.effectiveBalanceIncrements[i];
178
-
179
- const currentRewards = {...defaultAttestationsReward, validatorIndex: i};
180
-
181
- if (hasMarkers(flag, FLAG_PREV_SOURCE_ATTESTER_UNSLASHED)) {
182
- currentRewards.source = idealRewards[effectiveBalanceIncrement].source;
183
- } else {
184
- currentRewards.source = penalties[effectiveBalanceIncrement].source * -1; // Negative reward to indicate penalty
185
- }
186
-
187
- if (hasMarkers(flag, FLAG_PREV_TARGET_ATTESTER_UNSLASHED)) {
188
- currentRewards.target = idealRewards[effectiveBalanceIncrement].target;
189
- } else {
190
- currentRewards.target = penalties[effectiveBalanceIncrement].target * -1;
191
-
192
- // Also incur inactivity penalty if not voting target correctly
193
- const inactivityPenaltyNumerator =
194
- effectiveBalanceIncrement * EFFECTIVE_BALANCE_INCREMENT * state.inactivityScores.get(i);
195
- currentRewards.inactivity = Math.floor(inactivityPenaltyNumerator / inactivityPenaltyDenominator) * -1;
196
- }
197
-
198
- if (hasMarkers(flag, FLAG_PREV_HEAD_ATTESTER_UNSLASHED)) {
199
- currentRewards.head = idealRewards[effectiveBalanceIncrement].head;
200
- }
201
-
202
- rewards.push(currentRewards);
203
- }
204
-
205
- return rewards;
206
- }
@@ -1,153 +0,0 @@
1
- import {routes} from "@lodestar/api";
2
- import {BeaconConfig} from "@lodestar/config";
3
- import {
4
- ForkName,
5
- WHISTLEBLOWER_REWARD_QUOTIENT,
6
- WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA,
7
- isForkPostElectra,
8
- } from "@lodestar/params";
9
- import {
10
- CachedBeaconStateAllForks,
11
- CachedBeaconStateAltair,
12
- CachedBeaconStatePhase0,
13
- getAttesterSlashableIndices,
14
- processAttestationsAltair,
15
- } from "@lodestar/state-transition";
16
- import {BeaconBlock, altair, phase0} from "@lodestar/types";
17
-
18
- export type BlockRewards = routes.beacon.BlockRewards;
19
- type SubRewardValue = number; // All reward values should be integer
20
-
21
- /**
22
- * Calculate total proposer block rewards given block and the beacon state of the same slot before the block is applied (preState)
23
- * postState can be passed in to read reward cache if available
24
- * Standard (Non MEV) rewards for proposing a block consists of:
25
- * 1) Including attestations from (beacon) committee
26
- * 2) Including attestations from sync committee
27
- * 3) Reporting slashable behaviours from proposer and attester
28
- */
29
- export async function computeBlockRewards(
30
- config: BeaconConfig,
31
- block: BeaconBlock,
32
- preState: CachedBeaconStateAllForks,
33
- postState?: CachedBeaconStateAllForks
34
- ): Promise<BlockRewards> {
35
- const fork = config.getForkName(block.slot);
36
- const {attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0} =
37
- postState?.proposerRewards ?? {};
38
- let blockAttestationReward = cachedAttestationsReward;
39
- let syncAggregateReward = cachedSyncAggregateReward;
40
-
41
- if (blockAttestationReward === 0) {
42
- blockAttestationReward =
43
- fork === ForkName.phase0
44
- ? computeBlockAttestationRewardPhase0(block as phase0.BeaconBlock, preState as CachedBeaconStatePhase0)
45
- : computeBlockAttestationRewardAltair(config, block as altair.BeaconBlock, preState as CachedBeaconStateAltair);
46
- }
47
-
48
- if (syncAggregateReward === 0) {
49
- syncAggregateReward = computeSyncAggregateReward(block as altair.BeaconBlock, preState as CachedBeaconStateAltair);
50
- }
51
-
52
- const blockProposerSlashingReward = computeBlockProposerSlashingReward(fork, block, preState);
53
- const blockAttesterSlashingReward = computeBlockAttesterSlashingReward(fork, block, preState);
54
-
55
- const total =
56
- blockAttestationReward + syncAggregateReward + blockProposerSlashingReward + blockAttesterSlashingReward;
57
-
58
- return {
59
- proposerIndex: block.proposerIndex,
60
- total,
61
- attestations: blockAttestationReward,
62
- syncAggregate: syncAggregateReward,
63
- proposerSlashings: blockProposerSlashingReward,
64
- attesterSlashings: blockAttesterSlashingReward,
65
- };
66
- }
67
-
68
- /**
69
- * TODO: Calculate rewards received by block proposer for including attestations.
70
- */
71
- function computeBlockAttestationRewardPhase0(
72
- _block: phase0.BeaconBlock,
73
- _preState: CachedBeaconStatePhase0
74
- ): SubRewardValue {
75
- throw new Error("Unsupported fork! Block attestation reward calculation is not available in phase0");
76
- }
77
-
78
- /**
79
- * Calculate rewards received by block proposer for including attestations since Altair.
80
- * Reuses `processAttestationsAltair()`. Has dependency on RewardCache
81
- */
82
- function computeBlockAttestationRewardAltair(
83
- config: BeaconConfig,
84
- block: altair.BeaconBlock,
85
- preState: CachedBeaconStateAltair
86
- ): SubRewardValue {
87
- const fork = config.getForkSeq(block.slot);
88
- const {attestations} = block.body;
89
-
90
- processAttestationsAltair(fork, preState, attestations, false);
91
-
92
- return preState.proposerRewards.attestations;
93
- }
94
-
95
- function computeSyncAggregateReward(block: altair.BeaconBlock, preState: CachedBeaconStateAltair): SubRewardValue {
96
- if (block.body.syncAggregate !== undefined) {
97
- const {syncCommitteeBits} = block.body.syncAggregate;
98
- const {syncProposerReward} = preState.epochCtx;
99
-
100
- return syncCommitteeBits.getTrueBitIndexes().length * Math.floor(syncProposerReward); // syncProposerReward should already be integer
101
- }
102
-
103
- return 0; // phase0 block does not have syncAggregate
104
- }
105
-
106
- /**
107
- * Calculate rewards received by block proposer for including proposer slashings.
108
- * All proposer slashing rewards go to block proposer and none to whistleblower as of Deneb
109
- */
110
- function computeBlockProposerSlashingReward(
111
- fork: ForkName,
112
- block: BeaconBlock,
113
- state: CachedBeaconStateAllForks
114
- ): SubRewardValue {
115
- let proposerSlashingReward = 0;
116
-
117
- for (const proposerSlashing of block.body.proposerSlashings) {
118
- const offendingProposerIndex = proposerSlashing.signedHeader1.message.proposerIndex;
119
- const offendingProposerBalance = state.validators.getReadonly(offendingProposerIndex).effectiveBalance;
120
- const whistleblowerRewardQuotient = isForkPostElectra(fork)
121
- ? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
122
- : WHISTLEBLOWER_REWARD_QUOTIENT;
123
-
124
- proposerSlashingReward += Math.floor(offendingProposerBalance / whistleblowerRewardQuotient);
125
- }
126
-
127
- return proposerSlashingReward;
128
- }
129
-
130
- /**
131
- * Calculate rewards received by block proposer for including attester slashings.
132
- * All attester slashing rewards go to block proposer and none to whistleblower as of Deneb
133
- */
134
- function computeBlockAttesterSlashingReward(
135
- fork: ForkName,
136
- block: BeaconBlock,
137
- preState: CachedBeaconStateAllForks
138
- ): SubRewardValue {
139
- let attesterSlashingReward = 0;
140
-
141
- for (const attesterSlashing of block.body.attesterSlashings) {
142
- for (const offendingAttesterIndex of getAttesterSlashableIndices(attesterSlashing)) {
143
- const offendingAttesterBalance = preState.validators.getReadonly(offendingAttesterIndex).effectiveBalance;
144
- const whistleblowerRewardQuotient = isForkPostElectra(fork)
145
- ? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
146
- : WHISTLEBLOWER_REWARD_QUOTIENT;
147
-
148
- attesterSlashingReward += Math.floor(offendingAttesterBalance / whistleblowerRewardQuotient);
149
- }
150
- }
151
-
152
- return attesterSlashingReward;
153
- }