From c9bb826a84aa5408d51c75bc7d47e2937c59ce1e Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Tue, 7 Jan 2025 15:13:44 +0000 Subject: [PATCH] chore: fix beacon proposer cache unit tests (#7335) * chore: fix beacon proposer cache unit tests * Assign values to named constants --- .../unit/chain/beaconProposerCache.test.ts | 45 +++++++++++++++++++ .../test/unit/chain/beaconProposerCache.ts | 37 --------------- 2 files changed, 45 insertions(+), 37 deletions(-) create mode 100644 packages/beacon-node/test/unit/chain/beaconProposerCache.test.ts delete mode 100644 packages/beacon-node/test/unit/chain/beaconProposerCache.ts diff --git a/packages/beacon-node/test/unit/chain/beaconProposerCache.test.ts b/packages/beacon-node/test/unit/chain/beaconProposerCache.test.ts new file mode 100644 index 000000000000..e8f0e410c3f3 --- /dev/null +++ b/packages/beacon-node/test/unit/chain/beaconProposerCache.test.ts @@ -0,0 +1,45 @@ +import {beforeEach, describe, expect, it} from "vitest"; +import {BeaconProposerCache} from "../../../src/chain/beaconProposerCache.js"; + +describe("BeaconProposerCache", () => { + const suggestedFeeRecipient = "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + const feeRecipient1 = "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; + const feeRecipient2 = "0xcccccccccccccccccccccccccccccccccccccccc"; + + const validatorIndex1 = 23; + const validatorIndex2 = 43; + const unknownValidatorIndex = 32; + + let cache: BeaconProposerCache; + + beforeEach(() => { + // max 2 items + cache = new BeaconProposerCache({suggestedFeeRecipient}); + cache.add(1, {validatorIndex: validatorIndex1, feeRecipient: feeRecipient1}); + cache.add(3, {validatorIndex: validatorIndex2, feeRecipient: feeRecipient2}); + }); + + it("get default", () => { + expect(cache.getOrDefault(unknownValidatorIndex)).toBe(suggestedFeeRecipient); + }); + + it("get what has been set", () => { + expect(cache.get(validatorIndex1)).toBe(feeRecipient1); + }); + + it("override and get latest", () => { + const newFeeRecipient = "0xdddddddddddddddddddddddddddddddddddddddd"; + cache.add(5, {validatorIndex: validatorIndex1, feeRecipient: newFeeRecipient}); + expect(cache.get(validatorIndex1)).toBe(newFeeRecipient); + }); + + it("prune", () => { + cache.prune(4); + + // Default for what has been pruned + expect(cache.getOrDefault(validatorIndex1)).toBe(suggestedFeeRecipient); + + // Original for what hasn't been pruned + expect(cache.get(validatorIndex2)).toBe(feeRecipient2); + }); +}); diff --git a/packages/beacon-node/test/unit/chain/beaconProposerCache.ts b/packages/beacon-node/test/unit/chain/beaconProposerCache.ts deleted file mode 100644 index 96178d508732..000000000000 --- a/packages/beacon-node/test/unit/chain/beaconProposerCache.ts +++ /dev/null @@ -1,37 +0,0 @@ -import {beforeEach, describe, expect, it} from "vitest"; -import {BeaconProposerCache} from "../../../src/chain/beaconProposerCache.js"; - -const suggestedFeeRecipient = "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; -describe("BeaconProposerCache", () => { - let cache: BeaconProposerCache; - - beforeEach(() => { - // max 2 items - cache = new BeaconProposerCache({suggestedFeeRecipient}); - cache.add(1, {validatorIndex: 23, feeRecipient: "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"}); - cache.add(3, {validatorIndex: 43, feeRecipient: "0xcccccccccccccccccccccccccccccccccccccccc"}); - }); - - it("get default", () => { - expect(cache.get(32)).toBe("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); - }); - - it("get what has been set", () => { - expect(cache.get(23)).toBe("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); - }); - - it("override and get latest", () => { - cache.add(5, {validatorIndex: 23, feeRecipient: "0xdddddddddddddddddddddddddddddddddddddddd"}); - expect(cache.get(23)).toBe("0xdddddddddddddddddddddddddddddddddddddddd"); - }); - - it("prune", () => { - cache.prune(4); - - // Default for what has been pruned - expect(cache.get(23)).toBe("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); - - // Original for what hasn't been pruned - expect(cache.get(43)).toBe("0xcccccccccccccccccccccccccccccccccccccccc"); - }); -});