chore: simplify computeBlockRewards()

This commit is contained in:
Tuyen Nguyen
2026-01-07 15:19:54 +07:00
parent a96ecd7459
commit 48f8490b45
3 changed files with 7 additions and 7 deletions

View File

@@ -1298,9 +1298,9 @@ export class BeaconChain implements IBeaconChain {
preState = processSlots(preState, block.slot); // Dial preState's slot to block.slot preState = processSlots(preState, block.slot); // Dial preState's slot to block.slot
const postState = this.regen.getStateSync(toRootHex(block.stateRoot)) ?? undefined; const proposerRewards = this.regen.getStateSync(toRootHex(block.stateRoot))?.proposerRewards ?? undefined;
return computeBlockRewards(this.config, block, preState, postState); return computeBlockRewards(this.config, block, preState, proposerRewards);
} }
async getAttestationsRewards( async getAttestationsRewards(

View File

@@ -7,6 +7,7 @@ import {
} from "@lodestar/params"; } from "@lodestar/params";
import {BeaconBlock, altair, phase0, rewards} from "@lodestar/types"; import {BeaconBlock, altair, phase0, rewards} from "@lodestar/types";
import {processAttestationsAltair} from "../block/processAttestationsAltair.js"; import {processAttestationsAltair} from "../block/processAttestationsAltair.js";
import {RewardCache} from "../cache/rewardCache.js";
import {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStatePhase0} from "../cache/stateCache.js"; import {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStatePhase0} from "../cache/stateCache.js";
import {getAttesterSlashableIndices} from "../util/attestation.js"; import {getAttesterSlashableIndices} from "../util/attestation.js";
@@ -24,14 +25,13 @@ export async function computeBlockRewards(
config: BeaconConfig, config: BeaconConfig,
block: BeaconBlock, block: BeaconBlock,
preStateIn: CachedBeaconStateAllForks, preStateIn: CachedBeaconStateAllForks,
postStateIn?: CachedBeaconStateAllForks proposerRewards?: RewardCache
): Promise<rewards.BlockRewards> { ): Promise<rewards.BlockRewards> {
const preState = preStateIn.clone(); const preState = preStateIn.clone();
const postState = postStateIn?.clone();
const fork = config.getForkName(block.slot); const fork = config.getForkName(block.slot);
const {attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0} = const {attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0} =
postState?.proposerRewards ?? {}; proposerRewards ?? {};
let blockAttestationReward = cachedAttestationsReward; let blockAttestationReward = cachedAttestationsReward;
let syncAggregateReward = cachedSyncAggregateReward; let syncAggregateReward = cachedSyncAggregateReward;

View File

@@ -151,13 +151,13 @@ describe("chain / rewards / blockRewards", () => {
// Set postState's reward cache // Set postState's reward cache
const rewardCache = postState.proposerRewards; // Grab original reward cache before overwritten const rewardCache = postState.proposerRewards; // Grab original reward cache before overwritten
postState.proposerRewards = {attestations: 1000, syncAggregate: 1001, slashing: 1002}; const proposerRewards = {attestations: 1000, syncAggregate: 1001, slashing: 1002};
const calculatedBlockReward = await computeBlockRewards( const calculatedBlockReward = await computeBlockRewards(
config, config,
block.message, block.message,
preState as CachedBeaconStateAllForks, preState as CachedBeaconStateAllForks,
postState proposerRewards
); );
const {proposerIndex, total, attestations, syncAggregate, proposerSlashings, attesterSlashings} = const {proposerIndex, total, attestations, syncAggregate, proposerSlashings, attesterSlashings} =
calculatedBlockReward; calculatedBlockReward;