Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ensi321 committed Jan 11, 2024
1 parent 8c64487 commit 4bdbd5e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
3 changes: 2 additions & 1 deletion packages/beacon-node/src/chain/rewards/blockRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export async function computeBlockRewards(
postState?: CachedBeaconStateAllForks
): Promise<BlockRewards> {
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;

Expand Down
84 changes: 44 additions & 40 deletions packages/beacon-node/test/unit/chain/rewards/blockRewards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 4bdbd5e

Please sign in to comment.