-
-
Notifications
You must be signed in to change notification settings - Fork 416
feat: add endpoint for sync committee reward #6260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 19 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
fb2eb85
Add block rewards api
ensi321 7b460e2
Add test
ensi321 8ef70db
Merge branch 'unstable' into rewards-api
ensi321 cea591c
Add unit test
ensi321 ca45352
Lint
ensi321 68b99d1
Address comment
ensi321 05c5cd7
Reduce code redundancy
ensi321 08a2540
Merge branch 'unstable' into rewards-api
ensi321 61db8ab
Read reward cache first before calculate
ensi321 c2eae82
Lint
ensi321 03b249a
Add endpoint definition for sync rewards
ensi321 dd31509
Merge branch 'ChainSafe:unstable' into sync-reward
ensi321 a9934b6
Merge branch 'ChainSafe:unstable' into sync-reward
ensi321 28bf477
Merge branch 'unstable' into sync-reward
ensi321 503ca60
Add calculation logic
ensi321 4470cd5
Lint
ensi321 017565e
Merge branch 'unstable' into sync-reward
ensi321 5302c93
Follow convention from block rewards
ensi321 b7447aa
Include getSyncCommitteeRewards in unit test
ensi321 ac6c427
Update packages/beacon-node/src/chain/rewards/syncCommitteeRewards.ts
ensi321 2034dcc
Update packages/beacon-node/src/api/impl/beacon/rewards/index.ts
ensi321 d889c40
Improve filtering logic
ensi321 39e057a
Early throw on empty preState in getBlockRewards
ensi321 56a7941
Add jsdoc
ensi321 45a158b
Address comment
ensi321 41ca887
Clarify comment
ensi321 945d9b0
Address comment
ensi321 f96efc4
Update packages/beacon-node/src/chain/rewards/syncCommitteeRewards.ts
ensi321 7ab065b
Improve naming of filters
ensi321 2f10a9a
Lint
ensi321 387cee7
Update packages/beacon-node/src/chain/rewards/syncCommitteeRewards.ts
ensi321 0fde64e
ids -> validatorIds
ensi321 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
58 changes: 58 additions & 0 deletions
58
packages/beacon-node/src/chain/rewards/syncCommitteeRewards.ts
This file contains hidden or 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,58 @@ | ||
| 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; | ||
|
|
||
| export async function computeSyncCommitteeRewards( | ||
| block: allForks.BeaconBlock, | ||
| preState: CachedBeaconStateAllForks, | ||
| filters?: (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!"); | ||
ensi321 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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, {val: number}> = new Map( | ||
| committeeIndices.map((i) => [i, {val: preStateAltair.balances.get(i)}]) | ||
| ); // Use val for convenient way to increment/decrement balance | ||
ensi321 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for (const i of committeeIndices) { | ||
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
| const balanceRecord = balances.get(i)!; // We are certain i is in balances | ||
| 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 (filters !== undefined) { | ||
| // Might be a bit slow. But this is only called by rewards api which is insensitive to performance | ||
| return rewards.filter( | ||
ensi321 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| (reward) => | ||
| filters.includes(reward.validatorIndex) || filters.includes(index2pubkey[reward.validatorIndex].toHex()) | ||
| ); | ||
| } else { | ||
| return Array.from(balances, ([validatorIndex, v]) => ({validatorIndex, reward: v.val})); | ||
ensi321 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
ensi321 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.