Skip to content

Commit

Permalink
Follow convention from block rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
ensi321 committed Feb 23, 2024
1 parent 017565e commit 5302c93
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/beacon/routes/beacon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export * as rewards from "./rewards.js";
export {BroadcastValidation} from "./block.js";
export type {BlockId, BlockHeaderResponse} from "./block.js";
export type {AttestationFilters} from "./pool.js";
export type {BlockRewards} from "./rewards.js";
export type {BlockRewards, SyncCommitteeRewards} from "./rewards.js";
// TODO: Review if re-exporting all these types is necessary
export type {
StateId,
Expand Down
12 changes: 8 additions & 4 deletions packages/api/src/beacon/routes/beacon/rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ export type BlockRewards = {
attesterSlashings: number;
};

export type SyncCommitteeRewardsResponse = {validatorIndex: ValidatorIndex; reward: number}[];
/**
* Rewards info for sync committee participation. Every reward value is in Gwei.
* Note: This excludes sync aggregate reward. The reward for proposer here only reflect the sync committee participation portion
*/
export type SyncCommitteeRewards = {validatorIndex: ValidatorIndex; reward: number}[];

export type Api = {
/**
Expand All @@ -63,7 +67,7 @@ export type Api = {
filters?: ValidatorId[]
): Promise<
ApiClientResponse<
{[HttpStatusCode.OK]: {data: SyncCommitteeRewardsResponse; executionOptimistic: ExecutionOptimistic}},
{[HttpStatusCode.OK]: {data: SyncCommitteeRewards; executionOptimistic: ExecutionOptimistic}},
HttpStatusCode.BAD_REQUEST | HttpStatusCode.NOT_FOUND
>
>;
Expand Down Expand Up @@ -114,7 +118,7 @@ export function getReturnTypes(): ReturnTypes<Api> {
{jsonCase: "eth2"}
);

const SyncCommitteeRewards = new ContainerType(
const SyncCommitteeRewardsResponse = new ContainerType(
{
validatorIndex: ssz.ValidatorIndex,
reward: ssz.UintNum64,
Expand All @@ -124,6 +128,6 @@ export function getReturnTypes(): ReturnTypes<Api> {

return {
getBlockRewards: ContainerDataExecutionOptimistic(BlockRewardsResponse),
getSyncCommitteeRewards: ContainerDataExecutionOptimistic(ArrayOf(SyncCommitteeRewards)),
getSyncCommitteeRewards: ContainerDataExecutionOptimistic(ArrayOf(SyncCommitteeRewardsResponse)),
};
}
11 changes: 7 additions & 4 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,9 +1009,12 @@ export class BeaconChain implements IBeaconChain {
block: allForks.FullOrBlindedBeaconBlock,
filter?: (ValidatorIndex | string)[]
): Promise<SyncCommitteeRewards> {
const preState = (await this.regen.getPreState(block, {dontTransferCache: true}, RegenCaller.restApi)).clone();
preState.slot = block.slot; // regen.getPreState guarantees pre_state of the same epoch but not the same slot
const result = computeSyncCommitteeRewards(block, preState, filter);
return result;
const preState = this.regen.getPreStateSync(block);

if (preState === null) {
throw Error(`Pre-state is unavailable given block's parent root ${toHexString(block.parentRoot)}`);
}

return computeSyncCommitteeRewards(block, preState.clone(), filter);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
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";

// Note: This excludes sync aggregate reward. The reward for proposer here only reflect the sync committee participation portion
export type SyncCommitteeRewards = {
validatorIndex: ValidatorIndex;
reward: number;
}[];
export type SyncCommitteeRewards = routes.beacon.SyncCommitteeRewards;

export async function computeSyncCommitteeRewards(
block: allForks.BeaconBlock,
Expand Down

0 comments on commit 5302c93

Please sign in to comment.