diff --git a/packages/beacon-node/src/chain/rewards/blockRewards.ts b/packages/beacon-node/src/chain/rewards/blockRewards.ts index 2c2df099279d..226e6cc739ce 100644 --- a/packages/beacon-node/src/chain/rewards/blockRewards.ts +++ b/packages/beacon-node/src/chain/rewards/blockRewards.ts @@ -33,7 +33,8 @@ export async function computeBlockRewards( postState?: CachedBeaconStateAllForks ): Promise { const fork = preState.config.getForkName(block.slot); - const {attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0} = postState?.proposerRewards || {}; + const {attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0} = + postState?.proposerRewards || {}; let blockAttestationReward = cachedAttestationsReward; let syncAggregateReward = cachedSyncAggregateReward; diff --git a/packages/beacon-node/test/unit/chain/rewards/blockRewards.test.ts b/packages/beacon-node/test/unit/chain/rewards/blockRewards.test.ts index 1d397d57a318..f0d85ce3220f 100644 --- a/packages/beacon-node/test/unit/chain/rewards/blockRewards.test.ts +++ b/packages/beacon-node/test/unit/chain/rewards/blockRewards.test.ts @@ -128,50 +128,54 @@ describe("chain / rewards / blockRewards", () => { // Check if `computeBlockRewards` consults reward cache in the post state first it("Check reward cache", async () => { - const preState = generatePerfTestCachedStateAltair(); - const {opts} = testCases[0]; // Use opts of `normal case` - const block = getBlockAltair(preState, testCases[0].opts); - // Populate permanent root caches of the block - ssz.altair.BeaconBlock.hashTreeRoot(block.message); - // Populate tree root caches of the state - preState.hashTreeRoot(); - cachedStateAltairPopulateCaches(preState); + const preState = generatePerfTestCachedStateAltair(); + const {opts} = testCases[0]; // Use opts of `normal case` + const block = getBlockAltair(preState, testCases[0].opts); + // Populate permanent root caches of the block + ssz.altair.BeaconBlock.hashTreeRoot(block.message); + // Populate tree root caches of the state + preState.hashTreeRoot(); + cachedStateAltairPopulateCaches(preState); - const postState = stateTransition(preState as CachedBeaconStateAllForks, block, { - executionPayloadStatus: ExecutionPayloadStatus.valid, - dataAvailableStatus: DataAvailableStatus.available, - verifyProposer: false, - verifySignatures: false, - verifyStateRoot: false, - }); + const postState = stateTransition(preState as CachedBeaconStateAllForks, block, { + executionPayloadStatus: ExecutionPayloadStatus.valid, + dataAvailableStatus: DataAvailableStatus.available, + verifyProposer: false, + verifySignatures: false, + verifyStateRoot: false, + }); - // Set postState's reward cache - const rewardCache = postState.proposerRewards; // Grab original reward cache before overwritten - postState.proposerRewards = {attestations: 1000, syncAggregate: 1001, slashing: 1002}; + // Set postState's reward cache + const rewardCache = postState.proposerRewards; // Grab original reward cache before overwritten + postState.proposerRewards = {attestations: 1000, syncAggregate: 1001, slashing: 1002}; - const calculatedBlockReward = await computeBlockRewards(block.message, preState as CachedBeaconStateAllForks, postState); - const {proposerIndex, total, attestations, syncAggregate, proposerSlashings, attesterSlashings} = - calculatedBlockReward; + const calculatedBlockReward = await computeBlockRewards( + block.message, + preState as CachedBeaconStateAllForks, + postState + ); + const {proposerIndex, total, attestations, syncAggregate, proposerSlashings, attesterSlashings} = + calculatedBlockReward; - expect(proposerIndex).toBe(block.message.proposerIndex); - expect(total).toBe(attestations + syncAggregate + proposerSlashings + attesterSlashings); - if (opts.syncCommitteeBitsLen === 0) { - expect(syncAggregate).toBe(0); - } - if (opts.attestationLen === 0) { - expect(attestations).toBe(0); - } - if (opts.proposerSlashingLen === 0) { - expect(proposerSlashings).toBe(0); - } - if (opts.attesterSlashingLen === 0) { - expect(attesterSlashings).toBe(0); - } + expect(proposerIndex).toBe(block.message.proposerIndex); + expect(total).toBe(attestations + syncAggregate + proposerSlashings + attesterSlashings); + if (opts.syncCommitteeBitsLen === 0) { + expect(syncAggregate).toBe(0); + } + if (opts.attestationLen === 0) { + expect(attestations).toBe(0); + } + if (opts.proposerSlashingLen === 0) { + expect(proposerSlashings).toBe(0); + } + if (opts.attesterSlashingLen === 0) { + expect(attesterSlashings).toBe(0); + } - // Cross check with rewardCache - expect(attestations).toBe(1000); - expect(syncAggregate).toBe(1001); - expect((proposerSlashings + attesterSlashings) === 1002).toBeFalsy(); - expect(proposerSlashings + attesterSlashings).toBe(rewardCache.slashing); + // Cross check with rewardCache + expect(attestations).toBe(1000); + expect(syncAggregate).toBe(1001); + expect(proposerSlashings + attesterSlashings).not.toBe(1002); + expect(proposerSlashings + attesterSlashings).toBe(rewardCache.slashing); }); });