@lodestar/state-transition 1.39.0-dev.b6bba4cb8c → 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.
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/rewards/attestationsRewards.d.ts +6 -0
- package/lib/rewards/attestationsRewards.d.ts.map +1 -0
- package/lib/rewards/attestationsRewards.js +113 -0
- package/lib/rewards/attestationsRewards.js.map +1 -0
- package/lib/rewards/blockRewards.d.ts +13 -0
- package/lib/rewards/blockRewards.d.ts.map +1 -0
- package/lib/rewards/blockRewards.js +95 -0
- package/lib/rewards/blockRewards.js.map +1 -0
- package/lib/rewards/index.d.ts +4 -0
- package/lib/rewards/index.d.ts.map +1 -0
- package/lib/rewards/index.js +4 -0
- package/lib/rewards/index.js.map +1 -0
- package/lib/rewards/syncCommitteeRewards.d.ts +6 -0
- package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -0
- package/lib/rewards/syncCommitteeRewards.js +36 -0
- package/lib/rewards/syncCommitteeRewards.js.map +1 -0
- package/package.json +6 -6
- package/src/index.ts +1 -0
- package/src/rewards/attestationsRewards.ts +200 -0
- package/src/rewards/blockRewards.ts +147 -0
- package/src/rewards/index.ts +3 -0
- package/src/rewards/syncCommitteeRewards.ts +59 -0
package/lib/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export { type BeaconStateCache, createCachedBeaconState, isCachedBeaconState, is
|
|
|
15
15
|
export * from "./constants/index.js";
|
|
16
16
|
export type { EpochTransitionStep } from "./epoch/index.js";
|
|
17
17
|
export { type BeaconStateTransitionMetrics, getMetrics } from "./metrics.js";
|
|
18
|
+
export * from "./rewards/index.js";
|
|
18
19
|
export * from "./signatureSets/index.js";
|
|
19
20
|
export * from "./stateTransition.js";
|
|
20
21
|
export type { BeaconStateAllForks, BeaconStateAltair, BeaconStateBellatrix, BeaconStateCapella, BeaconStateDeneb, BeaconStateElectra, BeaconStateExecutions, BeaconStateFulu, BeaconStateGloas, BeaconStatePhase0, CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStateBellatrix, CachedBeaconStateCapella, CachedBeaconStateDeneb, CachedBeaconStateElectra, CachedBeaconStateExecutions, CachedBeaconStateFulu, CachedBeaconStateGloas, CachedBeaconStatePhase0, } from "./types.js";
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,iBAAiB,EACtB,sBAAsB,EACtB,sBAAsB,GACvB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAC,iCAAiC,EAAE,yBAAyB,EAAC,MAAM,sCAAsC,CAAC;AAClH,OAAO,EAAC,2BAA2B,EAAC,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAC,2BAA2B,EAAC,MAAM,wCAAwC,CAAC;AAEnF,OAAO,EAAC,kBAAkB,EAAC,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAC,2BAA2B,EAAC,MAAM,oCAAoC,CAAC;AAE/E,OAAO,EAAC,qBAAqB,EAAE,wBAAwB,EAAE,oBAAoB,EAAC,MAAM,iCAAiC,CAAC;AAEtH,OAAO,EAAC,sBAAsB,EAAC,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAC,kBAAkB,EAAC,MAAM,kBAAkB,CAAC;AACpD,OAAO,EACL,KAAK,0BAA0B,EAC/B,oCAAoC,EACpC,mCAAmC,GACpC,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EACL,UAAU,EACV,eAAe,EACf,mBAAmB,EACnB,KAAK,uBAAuB,EAC5B,kCAAkC,GACnC,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAC,KAAK,oBAAoB,EAAE,kBAAkB,EAAC,MAAM,iCAAiC,CAAC;AAC9F,OAAO,EAAC,KAA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6 +18,7 @@ export { syncPubkeys } from "./cache/pubkeyCache.js";
|
|
|
18
18
|
export { createCachedBeaconState, isCachedBeaconState, isStateBalancesNodesPopulated, isStateValidatorsNodesPopulated, loadCachedBeaconState, } from "./cache/stateCache.js";
|
|
19
19
|
export * from "./constants/index.js";
|
|
20
20
|
export { getMetrics } from "./metrics.js";
|
|
21
|
+
export * from "./rewards/index.js";
|
|
21
22
|
export * from "./signatureSets/index.js";
|
|
22
23
|
export * from "./stateTransition.js";
|
|
23
24
|
export * from "./util/index.js";
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,sBAAsB,EACtB,sBAAsB,GACvB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAC,iCAAiC,EAAE,yBAAyB,EAAC,MAAM,sCAAsC,CAAC;AAClH,OAAO,EAAC,2BAA2B,EAAC,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAC,2BAA2B,EAAC,MAAM,wCAAwC,CAAC;AACnF,qCAAqC;AACrC,OAAO,EAAC,kBAAkB,EAAC,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAC,2BAA2B,EAAC,MAAM,oCAAoC,CAAC;AAC/E,yBAAyB;AACzB,OAAO,EAAC,qBAAqB,EAAE,wBAAwB,EAAE,oBAAoB,EAAC,MAAM,iCAAiC,CAAC;AACtH,6BAA6B;AAC7B,OAAO,EAAC,sBAAsB,EAAC,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAC,kBAAkB,EAAC,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAEL,oCAAoC,EACpC,mCAAmC,GACpC,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EACL,UAAU,EACV,eAAe,EACf,mBAAmB,EAEnB,kCAAkC,GACnC,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAA4B,kBAAkB,EAAC,MAAM,iCAAiC,C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,WAAW,EAAC,MAAM,wBAAwB,CAAC;AAC3E,oBAAoB;AACpB,OAAO,EAEL,uBAAuB,EACvB,mBAAmB,EACnB,6BAA6B,EAC7B,+BAA+B,EAC/B,qBAAqB,GACtB,MAAM,uBAAuB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AAErC,OAAO,EAAoC,UAAU,EAAC,MAAM,cAAc,CAAC;AAC3E,cAAc,oBAAoB,CAAC;AACnC,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AAwBrC,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { PubkeyIndexMap } from "@chainsafe/pubkey-index-map";
|
|
2
|
+
import { BeaconConfig } from "@lodestar/config";
|
|
3
|
+
import { ValidatorIndex, rewards } from "@lodestar/types";
|
|
4
|
+
import { CachedBeaconStateAllForks } from "../types.js";
|
|
5
|
+
export declare function computeAttestationsRewards(config: BeaconConfig, pubkey2index: PubkeyIndexMap, state: CachedBeaconStateAllForks, validatorIds?: (ValidatorIndex | string)[]): Promise<rewards.AttestationsRewards>;
|
|
6
|
+
//# sourceMappingURL=attestationsRewards.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attestationsRewards.d.ts","sourceRoot":"","sources":["../../src/rewards/attestationsRewards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAc9C,OAAO,EAAC,cAAc,EAAE,OAAO,EAAC,MAAM,iBAAiB,CAAC;AAGxD,OAAO,EAAC,yBAAyB,EAA0B,MAAM,aAAa,CAAC;AAgB/E,wBAAsB,0BAA0B,CAC9C,MAAM,EAAE,YAAY,EACpB,YAAY,EAAE,cAAc,EAC5B,KAAK,EAAE,yBAAyB,EAChC,YAAY,CAAC,EAAE,CAAC,cAAc,GAAG,MAAM,CAAC,EAAE,GACzC,OAAO,CAAC,OAAO,CAAC,mBAAmB,CAAC,CAyBtC"}
|
|
@@ -0,0 +1,113 @@
|
|
|
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 { fromHex } from "@lodestar/utils";
|
|
3
|
+
import { beforeProcessEpoch } from "../cache/epochTransitionCache.js";
|
|
4
|
+
import { FLAG_ELIGIBLE_ATTESTER, FLAG_PREV_HEAD_ATTESTER_UNSLASHED, FLAG_PREV_SOURCE_ATTESTER_UNSLASHED, FLAG_PREV_TARGET_ATTESTER_UNSLASHED, hasMarkers, isInInactivityLeak, } from "../util/index.js";
|
|
5
|
+
const defaultAttestationsReward = { head: 0, target: 0, source: 0, inclusionDelay: 0, inactivity: 0 };
|
|
6
|
+
const defaultAttestationsPenalty = { target: 0, source: 0 };
|
|
7
|
+
export async function computeAttestationsRewards(config, pubkey2index, state, validatorIds) {
|
|
8
|
+
const fork = config.getForkName(state.slot);
|
|
9
|
+
if (fork === ForkName.phase0) {
|
|
10
|
+
throw Error("Unsupported fork. Attestations rewards calculation is not available in phase0");
|
|
11
|
+
}
|
|
12
|
+
const stateAltair = state;
|
|
13
|
+
const transitionCache = beforeProcessEpoch(stateAltair);
|
|
14
|
+
const [idealRewards, penalties] = computeIdealAttestationsRewardsAndPenaltiesAltair(config, stateAltair, transitionCache);
|
|
15
|
+
const totalRewards = computeTotalAttestationsRewardsAltair(config, pubkey2index, stateAltair, transitionCache, idealRewards, penalties, validatorIds);
|
|
16
|
+
return { idealRewards, totalRewards };
|
|
17
|
+
}
|
|
18
|
+
function computeIdealAttestationsRewardsAndPenaltiesAltair(config, state, transitionCache) {
|
|
19
|
+
const baseRewardPerIncrement = transitionCache.baseRewardPerIncrement;
|
|
20
|
+
const activeBalanceByIncrement = transitionCache.totalActiveStakeByIncrement;
|
|
21
|
+
const fork = config.getForkName(state.slot);
|
|
22
|
+
const maxEffectiveBalance = isForkPostElectra(fork) ? MAX_EFFECTIVE_BALANCE_ELECTRA : MAX_EFFECTIVE_BALANCE;
|
|
23
|
+
const maxEffectiveBalanceByIncrement = Math.floor(maxEffectiveBalance / EFFECTIVE_BALANCE_INCREMENT);
|
|
24
|
+
const idealRewards = Array.from({ length: maxEffectiveBalanceByIncrement + 1 }, (_, effectiveBalanceByIncrement) => ({
|
|
25
|
+
...defaultAttestationsReward,
|
|
26
|
+
effectiveBalance: effectiveBalanceByIncrement * EFFECTIVE_BALANCE_INCREMENT,
|
|
27
|
+
}));
|
|
28
|
+
const attestationsPenalties = Array.from({ length: maxEffectiveBalanceByIncrement + 1 }, (_, effectiveBalanceByIncrement) => ({
|
|
29
|
+
...defaultAttestationsPenalty,
|
|
30
|
+
effectiveBalance: effectiveBalanceByIncrement * EFFECTIVE_BALANCE_INCREMENT,
|
|
31
|
+
}));
|
|
32
|
+
for (let i = 0; i < PARTICIPATION_FLAG_WEIGHTS.length; i++) {
|
|
33
|
+
const weight = PARTICIPATION_FLAG_WEIGHTS[i];
|
|
34
|
+
let unslashedStakeByIncrement;
|
|
35
|
+
let flagName;
|
|
36
|
+
switch (i) {
|
|
37
|
+
case TIMELY_SOURCE_FLAG_INDEX: {
|
|
38
|
+
unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.sourceStakeByIncrement;
|
|
39
|
+
flagName = "source";
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
case TIMELY_TARGET_FLAG_INDEX: {
|
|
43
|
+
unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.targetStakeByIncrement;
|
|
44
|
+
flagName = "target";
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case TIMELY_HEAD_FLAG_INDEX: {
|
|
48
|
+
unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.headStakeByIncrement;
|
|
49
|
+
flagName = "head";
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
default: {
|
|
53
|
+
throw Error(`Unable to retrieve unslashed stake. Unknown participation flag index: ${i}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
for (let effectiveBalanceByIncrement = 0; effectiveBalanceByIncrement <= maxEffectiveBalanceByIncrement; effectiveBalanceByIncrement++) {
|
|
57
|
+
const baseReward = effectiveBalanceByIncrement * baseRewardPerIncrement;
|
|
58
|
+
const rewardNumerator = baseReward * weight * unslashedStakeByIncrement;
|
|
59
|
+
// Both idealReward and penalty are rounded to nearest integer. Loss of precision is minimal as unit is gwei
|
|
60
|
+
const idealReward = Math.round(rewardNumerator / activeBalanceByIncrement / WEIGHT_DENOMINATOR);
|
|
61
|
+
const penalty = Math.round((baseReward * weight) / WEIGHT_DENOMINATOR); // Positive number indicates penalty
|
|
62
|
+
const idealAttestationsReward = idealRewards[effectiveBalanceByIncrement];
|
|
63
|
+
idealAttestationsReward[flagName] = isInInactivityLeak(state) ? 0 : idealReward; // No attestations rewards during inactivity leak
|
|
64
|
+
if (flagName !== "head") {
|
|
65
|
+
const attestationPenalty = attestationsPenalties[effectiveBalanceByIncrement];
|
|
66
|
+
attestationPenalty[flagName] = penalty;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return [idealRewards, attestationsPenalties];
|
|
71
|
+
}
|
|
72
|
+
// Same calculation as `getRewardsAndPenaltiesAltair` but returns the breakdown of rewards instead of aggregated
|
|
73
|
+
function computeTotalAttestationsRewardsAltair(config, pubkey2index, state, transitionCache, idealRewards, penalties, validatorIds = []) {
|
|
74
|
+
const rewards = [];
|
|
75
|
+
const { flags } = transitionCache;
|
|
76
|
+
const { epochCtx } = state;
|
|
77
|
+
const validatorIndices = validatorIds
|
|
78
|
+
.map((id) => (typeof id === "number" ? id : pubkey2index.get(fromHex(id))))
|
|
79
|
+
.filter((index) => index !== undefined); // Validator indices to include in the result
|
|
80
|
+
const inactivityPenaltyDenominator = config.INACTIVITY_SCORE_BIAS * INACTIVITY_PENALTY_QUOTIENT_ALTAIR;
|
|
81
|
+
for (let i = 0; i < flags.length; i++) {
|
|
82
|
+
if (validatorIndices.length && !validatorIndices.includes(i)) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const flag = flags[i];
|
|
86
|
+
if (!hasMarkers(flag, FLAG_ELIGIBLE_ATTESTER)) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const effectiveBalanceIncrement = epochCtx.effectiveBalanceIncrements[i];
|
|
90
|
+
const currentRewards = { ...defaultAttestationsReward, validatorIndex: i };
|
|
91
|
+
if (hasMarkers(flag, FLAG_PREV_SOURCE_ATTESTER_UNSLASHED)) {
|
|
92
|
+
currentRewards.source = idealRewards[effectiveBalanceIncrement].source;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
currentRewards.source = penalties[effectiveBalanceIncrement].source * -1; // Negative reward to indicate penalty
|
|
96
|
+
}
|
|
97
|
+
if (hasMarkers(flag, FLAG_PREV_TARGET_ATTESTER_UNSLASHED)) {
|
|
98
|
+
currentRewards.target = idealRewards[effectiveBalanceIncrement].target;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
currentRewards.target = penalties[effectiveBalanceIncrement].target * -1;
|
|
102
|
+
// Also incur inactivity penalty if not voting target correctly
|
|
103
|
+
const inactivityPenaltyNumerator = effectiveBalanceIncrement * EFFECTIVE_BALANCE_INCREMENT * state.inactivityScores.get(i);
|
|
104
|
+
currentRewards.inactivity = Math.floor(inactivityPenaltyNumerator / inactivityPenaltyDenominator) * -1;
|
|
105
|
+
}
|
|
106
|
+
if (hasMarkers(flag, FLAG_PREV_HEAD_ATTESTER_UNSLASHED)) {
|
|
107
|
+
currentRewards.head = idealRewards[effectiveBalanceIncrement].head;
|
|
108
|
+
}
|
|
109
|
+
rewards.push(currentRewards);
|
|
110
|
+
}
|
|
111
|
+
return rewards;
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=attestationsRewards.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attestationsRewards.js","sourceRoot":"","sources":["../../src/rewards/attestationsRewards.ts"],"names":[],"mappings":"AAEA,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;AAE1B,OAAO,EAAC,OAAO,EAAC,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAuB,kBAAkB,EAAC,MAAM,kCAAkC,CAAC;AAE1F,OAAO,EACL,sBAAsB,EACtB,iCAAiC,EACjC,mCAAmC,EACnC,mCAAmC,EACnC,UAAU,EACV,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAK1B,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,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC9C,MAAoB,EACpB,YAA4B,EAC5B,KAAgC,EAChC,YAA0C;IAE1C,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,+EAA+E,CAAC,CAAC;IAC/F,CAAC;IAED,MAAM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B,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"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BeaconConfig } from "@lodestar/config";
|
|
2
|
+
import { BeaconBlock, rewards } from "@lodestar/types";
|
|
3
|
+
import { CachedBeaconStateAllForks } from "../cache/stateCache.js";
|
|
4
|
+
/**
|
|
5
|
+
* Calculate total proposer block rewards given block and the beacon state of the same slot before the block is applied (preState)
|
|
6
|
+
* postState can be passed in to read reward cache if available
|
|
7
|
+
* Standard (Non MEV) rewards for proposing a block consists of:
|
|
8
|
+
* 1) Including attestations from (beacon) committee
|
|
9
|
+
* 2) Including attestations from sync committee
|
|
10
|
+
* 3) Reporting slashable behaviours from proposer and attester
|
|
11
|
+
*/
|
|
12
|
+
export declare function computeBlockRewards(config: BeaconConfig, block: BeaconBlock, preState: CachedBeaconStateAllForks, postState?: CachedBeaconStateAllForks): Promise<rewards.BlockRewards>;
|
|
13
|
+
//# sourceMappingURL=blockRewards.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blockRewards.d.ts","sourceRoot":"","sources":["../../src/rewards/blockRewards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAO9C,OAAO,EAAC,WAAW,EAAkB,OAAO,EAAC,MAAM,iBAAiB,CAAC;AAErE,OAAO,EAAC,yBAAyB,EAAmD,MAAM,wBAAwB,CAAC;AAKnH;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CACvC,MAAM,EAAE,YAAY,EACpB,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,yBAAyB,EACnC,SAAS,CAAC,EAAE,yBAAyB,GACpC,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,CAgC/B"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { ForkName, WHISTLEBLOWER_REWARD_QUOTIENT, WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA, isForkPostElectra, } from "@lodestar/params";
|
|
2
|
+
import { processAttestationsAltair } from "../block/processAttestationsAltair.js";
|
|
3
|
+
import { getAttesterSlashableIndices } from "../util/attestation.js";
|
|
4
|
+
/**
|
|
5
|
+
* Calculate total proposer block rewards given block and the beacon state of the same slot before the block is applied (preState)
|
|
6
|
+
* postState can be passed in to read reward cache if available
|
|
7
|
+
* Standard (Non MEV) rewards for proposing a block consists of:
|
|
8
|
+
* 1) Including attestations from (beacon) committee
|
|
9
|
+
* 2) Including attestations from sync committee
|
|
10
|
+
* 3) Reporting slashable behaviours from proposer and attester
|
|
11
|
+
*/
|
|
12
|
+
export async function computeBlockRewards(config, block, preState, postState) {
|
|
13
|
+
const fork = config.getForkName(block.slot);
|
|
14
|
+
const { attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0 } = postState?.proposerRewards ?? {};
|
|
15
|
+
let blockAttestationReward = cachedAttestationsReward;
|
|
16
|
+
let syncAggregateReward = cachedSyncAggregateReward;
|
|
17
|
+
if (blockAttestationReward === 0) {
|
|
18
|
+
blockAttestationReward =
|
|
19
|
+
fork === ForkName.phase0
|
|
20
|
+
? computeBlockAttestationRewardPhase0(block, preState)
|
|
21
|
+
: computeBlockAttestationRewardAltair(config, block, preState);
|
|
22
|
+
}
|
|
23
|
+
if (syncAggregateReward === 0) {
|
|
24
|
+
syncAggregateReward = computeSyncAggregateReward(block, preState);
|
|
25
|
+
}
|
|
26
|
+
const blockProposerSlashingReward = computeBlockProposerSlashingReward(fork, block, preState);
|
|
27
|
+
const blockAttesterSlashingReward = computeBlockAttesterSlashingReward(fork, block, preState);
|
|
28
|
+
const total = blockAttestationReward + syncAggregateReward + blockProposerSlashingReward + blockAttesterSlashingReward;
|
|
29
|
+
return {
|
|
30
|
+
proposerIndex: block.proposerIndex,
|
|
31
|
+
total,
|
|
32
|
+
attestations: blockAttestationReward,
|
|
33
|
+
syncAggregate: syncAggregateReward,
|
|
34
|
+
proposerSlashings: blockProposerSlashingReward,
|
|
35
|
+
attesterSlashings: blockAttesterSlashingReward,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* TODO: Calculate rewards received by block proposer for including attestations.
|
|
40
|
+
*/
|
|
41
|
+
function computeBlockAttestationRewardPhase0(_block, _preState) {
|
|
42
|
+
throw new Error("Unsupported fork! Block attestation reward calculation is not available in phase0");
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Calculate rewards received by block proposer for including attestations since Altair.
|
|
46
|
+
* Reuses `processAttestationsAltair()`. Has dependency on RewardCache
|
|
47
|
+
*/
|
|
48
|
+
function computeBlockAttestationRewardAltair(config, block, preState) {
|
|
49
|
+
const fork = config.getForkSeq(block.slot);
|
|
50
|
+
const { attestations } = block.body;
|
|
51
|
+
processAttestationsAltair(fork, preState, attestations, false);
|
|
52
|
+
return preState.proposerRewards.attestations;
|
|
53
|
+
}
|
|
54
|
+
function computeSyncAggregateReward(block, preState) {
|
|
55
|
+
if (block.body.syncAggregate !== undefined) {
|
|
56
|
+
const { syncCommitteeBits } = block.body.syncAggregate;
|
|
57
|
+
const { syncProposerReward } = preState.epochCtx;
|
|
58
|
+
return syncCommitteeBits.getTrueBitIndexes().length * Math.floor(syncProposerReward); // syncProposerReward should already be integer
|
|
59
|
+
}
|
|
60
|
+
return 0; // phase0 block does not have syncAggregate
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Calculate rewards received by block proposer for including proposer slashings.
|
|
64
|
+
* All proposer slashing rewards go to block proposer and none to whistleblower as of Deneb
|
|
65
|
+
*/
|
|
66
|
+
function computeBlockProposerSlashingReward(fork, block, state) {
|
|
67
|
+
let proposerSlashingReward = 0;
|
|
68
|
+
for (const proposerSlashing of block.body.proposerSlashings) {
|
|
69
|
+
const offendingProposerIndex = proposerSlashing.signedHeader1.message.proposerIndex;
|
|
70
|
+
const offendingProposerBalance = state.validators.getReadonly(offendingProposerIndex).effectiveBalance;
|
|
71
|
+
const whistleblowerRewardQuotient = isForkPostElectra(fork)
|
|
72
|
+
? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
|
|
73
|
+
: WHISTLEBLOWER_REWARD_QUOTIENT;
|
|
74
|
+
proposerSlashingReward += Math.floor(offendingProposerBalance / whistleblowerRewardQuotient);
|
|
75
|
+
}
|
|
76
|
+
return proposerSlashingReward;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Calculate rewards received by block proposer for including attester slashings.
|
|
80
|
+
* All attester slashing rewards go to block proposer and none to whistleblower as of Deneb
|
|
81
|
+
*/
|
|
82
|
+
function computeBlockAttesterSlashingReward(fork, block, preState) {
|
|
83
|
+
let attesterSlashingReward = 0;
|
|
84
|
+
for (const attesterSlashing of block.body.attesterSlashings) {
|
|
85
|
+
for (const offendingAttesterIndex of getAttesterSlashableIndices(attesterSlashing)) {
|
|
86
|
+
const offendingAttesterBalance = preState.validators.getReadonly(offendingAttesterIndex).effectiveBalance;
|
|
87
|
+
const whistleblowerRewardQuotient = isForkPostElectra(fork)
|
|
88
|
+
? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
|
|
89
|
+
: WHISTLEBLOWER_REWARD_QUOTIENT;
|
|
90
|
+
attesterSlashingReward += Math.floor(offendingAttesterBalance / whistleblowerRewardQuotient);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return attesterSlashingReward;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=blockRewards.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blockRewards.js","sourceRoot":"","sources":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mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rewards/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { BeaconConfig } from "@lodestar/config";
|
|
2
|
+
import { BeaconBlock, ValidatorIndex, rewards } from "@lodestar/types";
|
|
3
|
+
import { Index2PubkeyCache } from "../cache/pubkeyCache.js";
|
|
4
|
+
import { CachedBeaconStateAllForks } from "../cache/stateCache.js";
|
|
5
|
+
export declare function computeSyncCommitteeRewards(config: BeaconConfig, index2pubkey: Index2PubkeyCache, block: BeaconBlock, preState: CachedBeaconStateAllForks, validatorIds?: (ValidatorIndex | string)[]): Promise<rewards.SyncCommitteeRewards>;
|
|
6
|
+
//# sourceMappingURL=syncCommitteeRewards.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"syncCommitteeRewards.d.ts","sourceRoot":"","sources":["../../src/rewards/syncCommitteeRewards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAC,WAAW,EAAE,cAAc,EAAU,OAAO,EAAC,MAAM,iBAAiB,CAAC;AAC7E,OAAO,EAAC,iBAAiB,EAAC,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAC,yBAAyB,EAA0B,MAAM,wBAAwB,CAAC;AAI1F,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,OAAO,CAAC,oBAAoB,CAAC,CA4CvC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"syncCommitteeRewards.js","sourceRoot":"","sources":["../../src/rewards/syncCommitteeRewards.ts"],"names":[],"mappings":"AACA,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"}
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/ChainSafe/lodestar/issues"
|
|
13
13
|
},
|
|
14
|
-
"version": "1.39.0-dev.
|
|
14
|
+
"version": "1.39.0-dev.c630c55067",
|
|
15
15
|
"type": "module",
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
@@ -62,10 +62,10 @@
|
|
|
62
62
|
"@chainsafe/pubkey-index-map": "^3.0.0",
|
|
63
63
|
"@chainsafe/ssz": "^1.2.2",
|
|
64
64
|
"@chainsafe/swap-or-not-shuffle": "^1.2.1",
|
|
65
|
-
"@lodestar/config": "1.39.0-dev.
|
|
66
|
-
"@lodestar/params": "1.39.0-dev.
|
|
67
|
-
"@lodestar/types": "1.39.0-dev.
|
|
68
|
-
"@lodestar/utils": "1.39.0-dev.
|
|
65
|
+
"@lodestar/config": "1.39.0-dev.c630c55067",
|
|
66
|
+
"@lodestar/params": "1.39.0-dev.c630c55067",
|
|
67
|
+
"@lodestar/types": "1.39.0-dev.c630c55067",
|
|
68
|
+
"@lodestar/utils": "1.39.0-dev.c630c55067",
|
|
69
69
|
"bigint-buffer": "^1.1.5"
|
|
70
70
|
},
|
|
71
71
|
"keywords": [
|
|
@@ -74,5 +74,5 @@
|
|
|
74
74
|
"beacon",
|
|
75
75
|
"blockchain"
|
|
76
76
|
],
|
|
77
|
-
"gitHead": "
|
|
77
|
+
"gitHead": "ead055ee9d3179f198a25ba5c984e8509dea31d7"
|
|
78
78
|
}
|
package/src/index.ts
CHANGED
|
@@ -40,6 +40,7 @@ export {
|
|
|
40
40
|
export * from "./constants/index.js";
|
|
41
41
|
export type {EpochTransitionStep} from "./epoch/index.js";
|
|
42
42
|
export {type BeaconStateTransitionMetrics, getMetrics} from "./metrics.js";
|
|
43
|
+
export * from "./rewards/index.js";
|
|
43
44
|
export * from "./signatureSets/index.js";
|
|
44
45
|
export * from "./stateTransition.js";
|
|
45
46
|
export type {
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import {PubkeyIndexMap} from "@chainsafe/pubkey-index-map";
|
|
2
|
+
import {BeaconConfig} from "@lodestar/config";
|
|
3
|
+
import {
|
|
4
|
+
EFFECTIVE_BALANCE_INCREMENT,
|
|
5
|
+
ForkName,
|
|
6
|
+
INACTIVITY_PENALTY_QUOTIENT_ALTAIR,
|
|
7
|
+
MAX_EFFECTIVE_BALANCE,
|
|
8
|
+
MAX_EFFECTIVE_BALANCE_ELECTRA,
|
|
9
|
+
PARTICIPATION_FLAG_WEIGHTS,
|
|
10
|
+
TIMELY_HEAD_FLAG_INDEX,
|
|
11
|
+
TIMELY_SOURCE_FLAG_INDEX,
|
|
12
|
+
TIMELY_TARGET_FLAG_INDEX,
|
|
13
|
+
WEIGHT_DENOMINATOR,
|
|
14
|
+
isForkPostElectra,
|
|
15
|
+
} from "@lodestar/params";
|
|
16
|
+
import {ValidatorIndex, rewards} from "@lodestar/types";
|
|
17
|
+
import {fromHex} from "@lodestar/utils";
|
|
18
|
+
import {EpochTransitionCache, beforeProcessEpoch} from "../cache/epochTransitionCache.js";
|
|
19
|
+
import {CachedBeaconStateAllForks, CachedBeaconStateAltair} from "../types.js";
|
|
20
|
+
import {
|
|
21
|
+
FLAG_ELIGIBLE_ATTESTER,
|
|
22
|
+
FLAG_PREV_HEAD_ATTESTER_UNSLASHED,
|
|
23
|
+
FLAG_PREV_SOURCE_ATTESTER_UNSLASHED,
|
|
24
|
+
FLAG_PREV_TARGET_ATTESTER_UNSLASHED,
|
|
25
|
+
hasMarkers,
|
|
26
|
+
isInInactivityLeak,
|
|
27
|
+
} from "../util/index.js";
|
|
28
|
+
|
|
29
|
+
/** Attestations penalty with respect to effective balance in Gwei */
|
|
30
|
+
type AttestationsPenalty = {target: number; source: number; effectiveBalance: number};
|
|
31
|
+
|
|
32
|
+
const defaultAttestationsReward = {head: 0, target: 0, source: 0, inclusionDelay: 0, inactivity: 0};
|
|
33
|
+
const defaultAttestationsPenalty = {target: 0, source: 0};
|
|
34
|
+
|
|
35
|
+
export async function computeAttestationsRewards(
|
|
36
|
+
config: BeaconConfig,
|
|
37
|
+
pubkey2index: PubkeyIndexMap,
|
|
38
|
+
state: CachedBeaconStateAllForks,
|
|
39
|
+
validatorIds?: (ValidatorIndex | string)[]
|
|
40
|
+
): Promise<rewards.AttestationsRewards> {
|
|
41
|
+
const fork = config.getForkName(state.slot);
|
|
42
|
+
if (fork === ForkName.phase0) {
|
|
43
|
+
throw Error("Unsupported fork. Attestations rewards calculation is not available in phase0");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const stateAltair = state as CachedBeaconStateAltair;
|
|
47
|
+
const transitionCache = beforeProcessEpoch(stateAltair);
|
|
48
|
+
|
|
49
|
+
const [idealRewards, penalties] = computeIdealAttestationsRewardsAndPenaltiesAltair(
|
|
50
|
+
config,
|
|
51
|
+
stateAltair,
|
|
52
|
+
transitionCache
|
|
53
|
+
);
|
|
54
|
+
const totalRewards = computeTotalAttestationsRewardsAltair(
|
|
55
|
+
config,
|
|
56
|
+
pubkey2index,
|
|
57
|
+
stateAltair,
|
|
58
|
+
transitionCache,
|
|
59
|
+
idealRewards,
|
|
60
|
+
penalties,
|
|
61
|
+
validatorIds
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return {idealRewards, totalRewards};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function computeIdealAttestationsRewardsAndPenaltiesAltair(
|
|
68
|
+
config: BeaconConfig,
|
|
69
|
+
state: CachedBeaconStateAllForks,
|
|
70
|
+
transitionCache: EpochTransitionCache
|
|
71
|
+
): [rewards.IdealAttestationsReward[], AttestationsPenalty[]] {
|
|
72
|
+
const baseRewardPerIncrement = transitionCache.baseRewardPerIncrement;
|
|
73
|
+
const activeBalanceByIncrement = transitionCache.totalActiveStakeByIncrement;
|
|
74
|
+
const fork = config.getForkName(state.slot);
|
|
75
|
+
const maxEffectiveBalance = isForkPostElectra(fork) ? MAX_EFFECTIVE_BALANCE_ELECTRA : MAX_EFFECTIVE_BALANCE;
|
|
76
|
+
const maxEffectiveBalanceByIncrement = Math.floor(maxEffectiveBalance / EFFECTIVE_BALANCE_INCREMENT);
|
|
77
|
+
|
|
78
|
+
const idealRewards = Array.from({length: maxEffectiveBalanceByIncrement + 1}, (_, effectiveBalanceByIncrement) => ({
|
|
79
|
+
...defaultAttestationsReward,
|
|
80
|
+
effectiveBalance: effectiveBalanceByIncrement * EFFECTIVE_BALANCE_INCREMENT,
|
|
81
|
+
}));
|
|
82
|
+
|
|
83
|
+
const attestationsPenalties: AttestationsPenalty[] = Array.from(
|
|
84
|
+
{length: maxEffectiveBalanceByIncrement + 1},
|
|
85
|
+
(_, effectiveBalanceByIncrement) => ({
|
|
86
|
+
...defaultAttestationsPenalty,
|
|
87
|
+
effectiveBalance: effectiveBalanceByIncrement * EFFECTIVE_BALANCE_INCREMENT,
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
for (let i = 0; i < PARTICIPATION_FLAG_WEIGHTS.length; i++) {
|
|
92
|
+
const weight = PARTICIPATION_FLAG_WEIGHTS[i];
|
|
93
|
+
|
|
94
|
+
let unslashedStakeByIncrement: number;
|
|
95
|
+
let flagName: keyof rewards.IdealAttestationsReward;
|
|
96
|
+
|
|
97
|
+
switch (i) {
|
|
98
|
+
case TIMELY_SOURCE_FLAG_INDEX: {
|
|
99
|
+
unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.sourceStakeByIncrement;
|
|
100
|
+
flagName = "source";
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
case TIMELY_TARGET_FLAG_INDEX: {
|
|
104
|
+
unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.targetStakeByIncrement;
|
|
105
|
+
flagName = "target";
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case TIMELY_HEAD_FLAG_INDEX: {
|
|
109
|
+
unslashedStakeByIncrement = transitionCache.prevEpochUnslashedStake.headStakeByIncrement;
|
|
110
|
+
flagName = "head";
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
default: {
|
|
114
|
+
throw Error(`Unable to retrieve unslashed stake. Unknown participation flag index: ${i}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
for (
|
|
119
|
+
let effectiveBalanceByIncrement = 0;
|
|
120
|
+
effectiveBalanceByIncrement <= maxEffectiveBalanceByIncrement;
|
|
121
|
+
effectiveBalanceByIncrement++
|
|
122
|
+
) {
|
|
123
|
+
const baseReward = effectiveBalanceByIncrement * baseRewardPerIncrement;
|
|
124
|
+
const rewardNumerator = baseReward * weight * unslashedStakeByIncrement;
|
|
125
|
+
// Both idealReward and penalty are rounded to nearest integer. Loss of precision is minimal as unit is gwei
|
|
126
|
+
const idealReward = Math.round(rewardNumerator / activeBalanceByIncrement / WEIGHT_DENOMINATOR);
|
|
127
|
+
const penalty = Math.round((baseReward * weight) / WEIGHT_DENOMINATOR); // Positive number indicates penalty
|
|
128
|
+
|
|
129
|
+
const idealAttestationsReward = idealRewards[effectiveBalanceByIncrement];
|
|
130
|
+
idealAttestationsReward[flagName] = isInInactivityLeak(state) ? 0 : idealReward; // No attestations rewards during inactivity leak
|
|
131
|
+
|
|
132
|
+
if (flagName !== "head") {
|
|
133
|
+
const attestationPenalty = attestationsPenalties[effectiveBalanceByIncrement];
|
|
134
|
+
attestationPenalty[flagName] = penalty;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return [idealRewards, attestationsPenalties];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Same calculation as `getRewardsAndPenaltiesAltair` but returns the breakdown of rewards instead of aggregated
|
|
143
|
+
function computeTotalAttestationsRewardsAltair(
|
|
144
|
+
config: BeaconConfig,
|
|
145
|
+
pubkey2index: PubkeyIndexMap,
|
|
146
|
+
state: CachedBeaconStateAltair,
|
|
147
|
+
transitionCache: EpochTransitionCache,
|
|
148
|
+
idealRewards: rewards.IdealAttestationsReward[],
|
|
149
|
+
penalties: AttestationsPenalty[],
|
|
150
|
+
validatorIds: (ValidatorIndex | string)[] = []
|
|
151
|
+
): rewards.TotalAttestationsReward[] {
|
|
152
|
+
const rewards = [];
|
|
153
|
+
const {flags} = transitionCache;
|
|
154
|
+
const {epochCtx} = state;
|
|
155
|
+
const validatorIndices = validatorIds
|
|
156
|
+
.map((id) => (typeof id === "number" ? id : pubkey2index.get(fromHex(id))))
|
|
157
|
+
.filter((index) => index !== undefined); // Validator indices to include in the result
|
|
158
|
+
|
|
159
|
+
const inactivityPenaltyDenominator = config.INACTIVITY_SCORE_BIAS * INACTIVITY_PENALTY_QUOTIENT_ALTAIR;
|
|
160
|
+
|
|
161
|
+
for (let i = 0; i < flags.length; i++) {
|
|
162
|
+
if (validatorIndices.length && !validatorIndices.includes(i)) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const flag = flags[i];
|
|
167
|
+
if (!hasMarkers(flag, FLAG_ELIGIBLE_ATTESTER)) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const effectiveBalanceIncrement = epochCtx.effectiveBalanceIncrements[i];
|
|
172
|
+
|
|
173
|
+
const currentRewards = {...defaultAttestationsReward, validatorIndex: i};
|
|
174
|
+
|
|
175
|
+
if (hasMarkers(flag, FLAG_PREV_SOURCE_ATTESTER_UNSLASHED)) {
|
|
176
|
+
currentRewards.source = idealRewards[effectiveBalanceIncrement].source;
|
|
177
|
+
} else {
|
|
178
|
+
currentRewards.source = penalties[effectiveBalanceIncrement].source * -1; // Negative reward to indicate penalty
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (hasMarkers(flag, FLAG_PREV_TARGET_ATTESTER_UNSLASHED)) {
|
|
182
|
+
currentRewards.target = idealRewards[effectiveBalanceIncrement].target;
|
|
183
|
+
} else {
|
|
184
|
+
currentRewards.target = penalties[effectiveBalanceIncrement].target * -1;
|
|
185
|
+
|
|
186
|
+
// Also incur inactivity penalty if not voting target correctly
|
|
187
|
+
const inactivityPenaltyNumerator =
|
|
188
|
+
effectiveBalanceIncrement * EFFECTIVE_BALANCE_INCREMENT * state.inactivityScores.get(i);
|
|
189
|
+
currentRewards.inactivity = Math.floor(inactivityPenaltyNumerator / inactivityPenaltyDenominator) * -1;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (hasMarkers(flag, FLAG_PREV_HEAD_ATTESTER_UNSLASHED)) {
|
|
193
|
+
currentRewards.head = idealRewards[effectiveBalanceIncrement].head;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
rewards.push(currentRewards);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return rewards;
|
|
200
|
+
}
|
|
@@ -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,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
|
+
}
|