-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add endpoint for sync committee reward (#6260)
* Add block rewards api * Add test * Add unit test * Lint * Address comment * Reduce code redundancy * Read reward cache first before calculate * Lint * Add endpoint definition for sync rewards * Add calculation logic * Lint * Follow convention from block rewards * Include getSyncCommitteeRewards in unit test * Update packages/beacon-node/src/chain/rewards/syncCommitteeRewards.ts Co-authored-by: Nico Flaig <nflaig@protonmail.com> * Update packages/beacon-node/src/api/impl/beacon/rewards/index.ts Co-authored-by: Nico Flaig <nflaig@protonmail.com> * Improve filtering logic * Early throw on empty preState in getBlockRewards * Add jsdoc * Address comment * Clarify comment * Address comment * Update packages/beacon-node/src/chain/rewards/syncCommitteeRewards.ts Co-authored-by: Nico Flaig <nflaig@protonmail.com> * Improve naming of filters * Lint * Update packages/beacon-node/src/chain/rewards/syncCommitteeRewards.ts Co-authored-by: Nico Flaig <nflaig@protonmail.com> * ids -> validatorIds --------- Co-authored-by: Nico Flaig <nflaig@protonmail.com>
- Loading branch information
Showing
8 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/beacon-node/src/chain/rewards/syncCommitteeRewards.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import {CachedBeaconStateAllForks, CachedBeaconStateAltair} from "@lodestar/state-transition"; | ||
import {ValidatorIndex, allForks, altair} from "@lodestar/types"; | ||
import {ForkName, SYNC_COMMITTEE_SIZE} from "@lodestar/params"; | ||
import {routes} from "@lodestar/api"; | ||
|
||
export type SyncCommitteeRewards = routes.beacon.SyncCommitteeRewards; | ||
type BalanceRecord = {val: number}; // Use val for convenient way to increment/decrement balance | ||
|
||
export async function computeSyncCommitteeRewards( | ||
block: allForks.BeaconBlock, | ||
preState: CachedBeaconStateAllForks, | ||
validatorIds?: (ValidatorIndex | string)[] | ||
): Promise<SyncCommitteeRewards> { | ||
const fork = preState.config.getForkName(block.slot); | ||
if (fork === ForkName.phase0) { | ||
throw Error("Cannot get sync rewards as phase0 block does not have sync committee"); | ||
} | ||
|
||
const altairBlock = block as altair.BeaconBlock; | ||
const preStateAltair = preState as CachedBeaconStateAltair; | ||
const {index2pubkey} = preStateAltair.epochCtx; | ||
|
||
// Bound committeeIndices in case it goes beyond SYNC_COMMITTEE_SIZE just to be safe | ||
const committeeIndices = preStateAltair.epochCtx.currentSyncCommitteeIndexed.validatorIndices.slice( | ||
0, | ||
SYNC_COMMITTEE_SIZE | ||
); | ||
const {syncParticipantReward} = preStateAltair.epochCtx; | ||
const {syncCommitteeBits} = altairBlock.body.syncAggregate; | ||
|
||
// Use balance of each committee as starting point such that we cap the penalty to avoid balance dropping below 0 | ||
const balances: Map<ValidatorIndex, BalanceRecord> = new Map( | ||
committeeIndices.map((i) => [i, {val: preStateAltair.balances.get(i)}]) | ||
); | ||
|
||
for (const i of committeeIndices) { | ||
const balanceRecord = balances.get(i) as BalanceRecord; | ||
if (syncCommitteeBits.get(i)) { | ||
// Positive rewards for participants | ||
balanceRecord.val += syncParticipantReward; | ||
} else { | ||
// Negative rewards for non participants | ||
balanceRecord.val = Math.max(0, balanceRecord.val - syncParticipantReward); | ||
} | ||
} | ||
|
||
const rewards = Array.from(balances, ([validatorIndex, v]) => ({validatorIndex, reward: v.val})); | ||
|
||
if (validatorIds !== undefined) { | ||
const filtersSet = new Set(validatorIds); | ||
return rewards.filter( | ||
(reward) => filtersSet.has(reward.validatorIndex) || filtersSet.has(index2pubkey[reward.validatorIndex].toHex()) | ||
); | ||
} else { | ||
return rewards; | ||
} | ||
} |