/
verifyBlock.test.ts
58 lines (48 loc) 路 2.27 KB
/
verifyBlock.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sinon, {SinonStubbedInstance} from "sinon";
import {config} from "@lodestar/config/default";
import {ForkChoice, ProtoBlock} from "@lodestar/fork-choice";
import {allForks, ssz} from "@lodestar/types";
import {verifyBlockSanityChecks, VerifyBlockModules} from "../../../../src/chain/blocks/verifyBlock.js";
import {LocalClock} from "../../../../src/chain/clock/index.js";
import {BlockErrorCode} from "../../../../src/chain/errors/index.js";
import {expectThrowsLodestarError} from "../../../utils/errors.js";
describe("chain / blocks / verifyBlock", function () {
let forkChoice: SinonStubbedInstance<ForkChoice>;
let clock: LocalClock;
let modules: VerifyBlockModules;
let block: allForks.SignedBeaconBlock;
const currentSlot = 1;
beforeEach(function () {
block = ssz.phase0.SignedBeaconBlock.defaultValue();
block.message.slot = currentSlot;
forkChoice = sinon.createStubInstance(ForkChoice);
forkChoice.getFinalizedCheckpoint.returns({epoch: 0, root: Buffer.alloc(32), rootHex: ""});
clock = {currentSlot} as LocalClock;
modules = ({config, forkChoice, clock} as Partial<VerifyBlockModules>) as VerifyBlockModules;
// On first call, parentRoot is known
forkChoice.getBlockHex.returns({} as ProtoBlock);
});
it("PARENT_UNKNOWN", function () {
forkChoice.getBlockHex.returns(null);
expectThrowsLodestarError(() => verifyBlockSanityChecks(modules, block), BlockErrorCode.PARENT_UNKNOWN);
});
it("GENESIS_BLOCK", function () {
block.message.slot = 0;
expectThrowsLodestarError(() => verifyBlockSanityChecks(modules, block), BlockErrorCode.GENESIS_BLOCK);
});
it("ALREADY_KNOWN", function () {
forkChoice.hasBlockHex.returns(true);
expectThrowsLodestarError(() => verifyBlockSanityChecks(modules, block), BlockErrorCode.ALREADY_KNOWN);
});
it("WOULD_REVERT_FINALIZED_SLOT", function () {
forkChoice.getFinalizedCheckpoint.returns({epoch: 5, root: Buffer.alloc(32), rootHex: ""});
expectThrowsLodestarError(
() => verifyBlockSanityChecks(modules, block),
BlockErrorCode.WOULD_REVERT_FINALIZED_SLOT
);
});
it("FUTURE_SLOT", function () {
block.message.slot = currentSlot + 1;
expectThrowsLodestarError(() => verifyBlockSanityChecks(modules, block), BlockErrorCode.FUTURE_SLOT);
});
});