-
-
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 Altair block reward (#6178)
* Add block rewards api * Add test * Add unit test * Lint * Address comment * Reduce code redundancy * Read reward cache first before calculate * Lint * Partially address comments * Accept optional postState to get the reward cache * Update test * lint * Update packages/beacon-node/src/chain/rewards/blockRewards.ts Co-authored-by: Nico Flaig <nflaig@protonmail.com> * Update packages/beacon-node/src/chain/rewards/blockRewards.ts Co-authored-by: Nico Flaig <nflaig@protonmail.com> * Update packages/beacon-node/src/chain/rewards/blockRewards.ts Co-authored-by: Nico Flaig <nflaig@protonmail.com> * Update packages/beacon-node/src/chain/rewards/blockRewards.ts Co-authored-by: Nico Flaig <nflaig@protonmail.com> * Rename proposerRewards to blockRewards. Fix import * Remove getBlockRewards from api ignore list * Fix test * Rename state to preState * Add description to fields in BlockRewards * Clean up imports * Use jsdoc to document properties * Apply suggestions from code review * Add `getPreStateSync()` * fix: clone states to compute block rewards --------- Co-authored-by: Nico Flaig <nflaig@protonmail.com> Co-authored-by: Cayman <caymannava@gmail.com> Co-authored-by: Tuyen Nguyen <vutuyen2636@gmail.com>
- Loading branch information
1 parent
cd59df3
commit f47cc18
Showing
13 changed files
with
512 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import {ContainerType} from "@chainsafe/ssz"; | ||
import {ssz, ValidatorIndex} from "@lodestar/types"; | ||
|
||
import { | ||
RoutesData, | ||
ReturnTypes, | ||
Schema, | ||
ReqSerializers, | ||
ContainerDataExecutionOptimistic, | ||
} from "../../../utils/index.js"; | ||
import {HttpStatusCode} from "../../../utils/client/httpStatusCode.js"; | ||
import {ApiClientResponse} from "../../../interfaces.js"; | ||
import {BlockId} from "./block.js"; | ||
|
||
// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes | ||
|
||
/** | ||
* True if the response references an unverified execution payload. Optimistic information may be invalidated at | ||
* a later time. If the field is not present, assume the False value. | ||
*/ | ||
export type ExecutionOptimistic = boolean; | ||
|
||
/** | ||
* Rewards info for a single block. Every reward value is in Gwei. | ||
*/ | ||
export type BlockRewards = { | ||
/** Proposer of the block, the proposer index who receives these rewards */ | ||
proposerIndex: ValidatorIndex; | ||
/** Total block reward, equal to attestations + sync_aggregate + proposer_slashings + attester_slashings */ | ||
total: number; | ||
/** Block reward component due to included attestations */ | ||
attestations: number; | ||
/** Block reward component due to included sync_aggregate */ | ||
syncAggregate: number; | ||
/** Block reward component due to included proposer_slashings */ | ||
proposerSlashings: number; | ||
/** Block reward component due to included attester_slashings */ | ||
attesterSlashings: number; | ||
}; | ||
|
||
export type Api = { | ||
/** | ||
* Get block rewards | ||
* Returns the info of rewards received by the block proposer | ||
* | ||
* @param blockId Block identifier. | ||
* Can be one of: "head" (canonical head in node's view), "genesis", "finalized", \<slot\>, \<hex encoded blockRoot with 0x prefix\>. | ||
*/ | ||
getBlockRewards( | ||
blockId: BlockId | ||
): Promise< | ||
ApiClientResponse< | ||
{[HttpStatusCode.OK]: {data: BlockRewards; executionOptimistic: ExecutionOptimistic}}, | ||
HttpStatusCode.BAD_REQUEST | HttpStatusCode.NOT_FOUND | ||
> | ||
>; | ||
}; | ||
|
||
/** | ||
* Define javascript values for each route | ||
*/ | ||
export const routesData: RoutesData<Api> = { | ||
getBlockRewards: {url: "/eth/v1/beacon/rewards/blocks/{block_id}", method: "GET"}, | ||
}; | ||
|
||
export type ReqTypes = { | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
getBlockRewards: {params: {block_id: string}}; | ||
}; | ||
|
||
export function getReqSerializers(): ReqSerializers<Api, ReqTypes> { | ||
return { | ||
getBlockRewards: { | ||
writeReq: (block_id) => ({params: {block_id: String(block_id)}}), | ||
parseReq: ({params}) => [params.block_id], | ||
schema: {params: {block_id: Schema.StringRequired}}, | ||
}, | ||
}; | ||
} | ||
|
||
export function getReturnTypes(): ReturnTypes<Api> { | ||
const BlockRewardsResponse = new ContainerType( | ||
{ | ||
proposerIndex: ssz.ValidatorIndex, | ||
total: ssz.UintNum64, | ||
attestations: ssz.UintNum64, | ||
syncAggregate: ssz.UintNum64, | ||
proposerSlashings: ssz.UintNum64, | ||
attesterSlashings: ssz.UintNum64, | ||
}, | ||
{jsonCase: "eth2"} | ||
); | ||
|
||
return { | ||
getBlockRewards: ContainerDataExecutionOptimistic(BlockRewardsResponse), | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {routes, ServerApi} from "@lodestar/api"; | ||
import {ApiModules} from "../../types.js"; | ||
import {resolveBlockId} from "../blocks/utils.js"; | ||
|
||
export function getBeaconRewardsApi({chain}: Pick<ApiModules, "chain">): ServerApi<routes.beacon.rewards.Api> { | ||
return { | ||
async getBlockRewards(blockId) { | ||
const {block, executionOptimistic} = await resolveBlockId(chain, blockId); | ||
const data = await chain.getBlockRewards(block.message); | ||
return {data, executionOptimistic}; | ||
}, | ||
}; | ||
} |
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
Oops, something went wrong.