From 84f0a5227266b5424ecfd1dc768285e3cce41896 Mon Sep 17 00:00:00 2001 From: preethamr Date: Sun, 15 Oct 2023 23:38:16 -0700 Subject: [PATCH] fix: add lh unit tests --- packages/agents/lighthouse/test/mock.ts | 2 + .../tasks/prover/operations/process.spec.ts | 1 + .../tasks/prover/operations/publisher.spec.ts | 48 ++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/agents/lighthouse/test/mock.ts b/packages/agents/lighthouse/test/mock.ts index 464422e725..0f36ff8bb7 100644 --- a/packages/agents/lighthouse/test/mock.ts +++ b/packages/agents/lighthouse/test/mock.ts @@ -11,6 +11,7 @@ import { RootMessage, RelayerType, ReceivedAggregateRoot, + ModeType, } from "@connext/nxtp-utils"; import { Relayer } from "@connext/nxtp-adapters-relayer"; import { mockRelayer } from "@connext/nxtp-adapters-relayer/test/mock"; @@ -102,6 +103,7 @@ export const mock = { }, config: mock.config(), chainData: mock.chainData(), + mode: ModeType.SlowMode, }; }, processFromRootCtx: (): ProcessFromRootContext => { diff --git a/packages/agents/lighthouse/test/tasks/prover/operations/process.spec.ts b/packages/agents/lighthouse/test/tasks/prover/operations/process.spec.ts index 75ab946d9c..7e40a3a8d4 100644 --- a/packages/agents/lighthouse/test/tasks/prover/operations/process.spec.ts +++ b/packages/agents/lighthouse/test/tasks/prover/operations/process.spec.ts @@ -16,6 +16,7 @@ const mockBrokerMesage: BrokerMessage = { messageRootCount: 2, aggregateRoot: mkBytes32("0x222"), aggregateRootCount: 2, + snapshotRoots: [], }; const requestContext = createRequestContext("Publisher"); diff --git a/packages/agents/lighthouse/test/tasks/prover/operations/publisher.spec.ts b/packages/agents/lighthouse/test/tasks/prover/operations/publisher.spec.ts index 042be2037c..feb84e8a21 100644 --- a/packages/agents/lighthouse/test/tasks/prover/operations/publisher.spec.ts +++ b/packages/agents/lighthouse/test/tasks/prover/operations/publisher.spec.ts @@ -1,4 +1,4 @@ -import { expect, createRequestContext, mkBytes32 } from "@connext/nxtp-utils"; +import { expect, createRequestContext, mkBytes32, ModeType } from "@connext/nxtp-utils"; import { SinonStub, stub } from "sinon"; import { @@ -6,6 +6,8 @@ import { prefetch, createBrokerMessage, getUnProcessedMessagesByIndex, + acquireLock, + releaseLock, } from "../../../../src/tasks/prover/operations/publisher"; import * as PublisherFns from "../../../../src/tasks/prover/operations/publisher"; import { mock, mockXMessage1, mockXMessage2, mockRootMessage, mockReceivedRoot } from "../../../mock"; @@ -22,6 +24,7 @@ const mockBrokerMesage: BrokerMessage = { messageRootCount: 2, aggregateRoot: mkBytes32("0x222"), aggregateRootCount: 2, + snapshotRoots: [], }; const requestContext = createRequestContext("Publisher"); @@ -70,6 +73,44 @@ describe("Operations: Publisher", () => { expect(createBrokerMessageStub.callCount).to.be.eq(4); }); + it("happy case should enqueue a broker messages in op mode", async () => { + acquireLock(); + const channel = await proverCtxMock.adapters.mqClient.createChannel(); + const publishStub = (channel.publish as SinonStub).resolves(); + proverCtxMock.mode = ModeType.OptimisticMode; + + (proverCtxMock.adapters.database.getLatestAggregateRoots as SinonStub).resolves([mockReceivedRoot]); + (proverCtxMock.adapters.database.getLatestMessageRoot as SinonStub).resolves(mockRootMessage); + (proverCtxMock.adapters.database.getPendingAggregateRoot as SinonStub).resolves(mock.entity.snapshot()); + (proverCtxMock.adapters.database.getRootMessage as SinonStub).resolves(mockRootMessage); + (proverCtxMock.adapters.database.getMessageRootCount as SinonStub).resolves(1); + (proverCtxMock.adapters.database.getMessageRootIndex as SinonStub).resolves(1); + (proverCtxMock.adapters.database.getAggregateRootCount as SinonStub).resolves(1); + (proverCtxMock.adapters.cache.messages.setStatus as SinonStub).resolves(); + createBrokerMessageStub.resolves(mockBrokerMesage); + await enqueue(); + releaseLock(); + expect(createBrokerMessageStub.callCount).to.be.eq(4); + }); + + it("should not enqueue if no snapshot root in op mode", async () => { + const channel = await proverCtxMock.adapters.mqClient.createChannel(); + const publishStub = (channel.publish as SinonStub).resolves(); + proverCtxMock.mode = ModeType.OptimisticMode; + + (proverCtxMock.adapters.database.getLatestAggregateRoots as SinonStub).resolves([mockReceivedRoot]); + (proverCtxMock.adapters.database.getLatestMessageRoot as SinonStub).resolves(mockRootMessage); + (proverCtxMock.adapters.database.getPendingAggregateRoot as SinonStub).resolves(undefined); + (proverCtxMock.adapters.database.getRootMessage as SinonStub).resolves(mockRootMessage); + (proverCtxMock.adapters.database.getMessageRootCount as SinonStub).resolves(1); + (proverCtxMock.adapters.database.getMessageRootIndex as SinonStub).resolves(1); + (proverCtxMock.adapters.database.getAggregateRootCount as SinonStub).resolves(1); + (proverCtxMock.adapters.cache.messages.setStatus as SinonStub).resolves(); + createBrokerMessageStub.resolves(mockBrokerMesage); + await enqueue(); + expect(createBrokerMessageStub.callCount).to.be.eq(0); + }); + it("should catch error if no pending aggregate root", async () => { (proverCtxMock.adapters.database.getLatestAggregateRoots as SinonStub).resolves([mockReceivedRoot]); (proverCtxMock.adapters.database.getLatestMessageRoot as SinonStub).resolves(mockRootMessage); @@ -80,7 +121,7 @@ describe("Operations: Publisher", () => { (proverCtxMock.adapters.cache.messages.setNonce as SinonStub).resolves(); createBrokerMessageStub.resolves(mockBrokerMesage); expect(await enqueue()).to.throw; - expect(createBrokerMessageStub.callCount).to.be.eq(0); + expect(createBrokerMessageStub.callCount).to.be.eq(4); }); it("should catch error if no received aggregate root", async () => { (proverCtxMock.adapters.database.getLatestAggregateRoots as SinonStub).resolves([]); @@ -142,6 +183,7 @@ describe("Operations: Publisher", () => { mockBrokerMesage.messageRootCount, mockBrokerMesage.aggregateRoot, mockBrokerMesage.aggregateRootCount, + mockBrokerMesage.snapshotRoots, requestContext, ); expect(brokerMessage).to.be.deep.eq(mockBrokerMesage); @@ -157,6 +199,7 @@ describe("Operations: Publisher", () => { mockBrokerMesage.messageRootCount, mockBrokerMesage.aggregateRoot, mockBrokerMesage.aggregateRootCount, + mockBrokerMesage.snapshotRoots, requestContext, ), ).to.eventually.be.rejectedWith(NoDestinationDomainForProof); @@ -175,6 +218,7 @@ describe("Operations: Publisher", () => { mockBrokerMesage.messageRootCount, mockBrokerMesage.aggregateRoot, mockBrokerMesage.aggregateRootCount, + mockBrokerMesage.snapshotRoots, requestContext, ); expect(brokerMessage).to.be.undefined;