Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 4945 unit test coverage checks staging #4989

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/adapters/database/test/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ export const mockDatabase = (): Database => {
saveFinalizedRoots: stub().resolves(),
savePropagatedOptimisticRoots: stub().resolves(),
saveSnapshotRoots: stub().resolves(),
getPendingSnapshots: stub().resolves([mock.entity.snapshotRoot()]),
};
};
3 changes: 3 additions & 0 deletions packages/agents/lighthouse/.nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"src/tasks/sendOutboundRoot/sendOutboundRoot.ts",
"src/tasks/sendOutboundRoot/operations/index.ts",
"src/tasks/sendOutboundRoot/helpers/index.ts",
"src/tasks/propose/**/*.ts",
"src/tasks/propagate/**/*.ts",
"src/tasks/prover/**/*.ts",
"test/**/*.ts",
"./globalTestHook.ts"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SnapshotRoot,
SparseMerkleTree,
jsonifyError,
domainToChainId,
} from "@connext/nxtp-utils";
import { BigNumber } from "ethers";

Expand All @@ -26,11 +27,17 @@ export const propose = async () => {
const {
logger,
config,
chainData,
adapters: { database },
} = getContext();
const { requestContext, methodContext } = createLoggingContext(propose.name);
logger.info("Starting propose operation", requestContext, methodContext);

const hubChainId = chainData.get(config.hubDomain)?.chainId;
if (!hubChainId) {
throw new NoChainIdForHubDomain(config.hubDomain, requestContext, methodContext);
}

// Get the latest pending snapshots
// Generate aggreagate root given latest snapshot
// Encode params data for contract call
Expand Down Expand Up @@ -69,7 +76,7 @@ export const propose = async () => {
orderedSnapshotRoots.push(...snapshotRoots!.filter((s) => s.spokeDomain === Number(domain)));
});

proposeSnapshot(
await proposeSnapshot(
latestSnapshotId,
orderedSnapshotRoots.map((s) => s.root),
requestContext,
Expand All @@ -84,18 +91,13 @@ export const proposeSnapshot = async (snapshotId: string, snapshotRoots: string[
logger,
adapters: { contracts, relayers, database, chainreader, subgraph },
config,
chainData,
} = getContext();
const { requestContext, methodContext } = createLoggingContext("proposeSnapshot", _requestContext);
const rootManagerMeta: RootManagerMeta = await subgraph.getRootManagerMeta(config.hubDomain);
const domains = rootManagerMeta.domains;

const hubChainId = chainData.get(config.hubDomain)?.chainId;
if (!hubChainId) {
throw new NoChainIdForHubDomain(config.hubDomain, requestContext, methodContext);
}

const relayerProxyHubAddress = config.chains[config.hubDomain].deployments.relayerProxy;
const hubChainId = domainToChainId(+config.hubDomain);
// const _totalFee = constants.Zero;

const baseAggregateRoot = await database.getBaseAggregateRoot();
Expand Down
6 changes: 6 additions & 0 deletions packages/agents/lighthouse/test/globalTestHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { Relayer } from "@connext/nxtp-adapters-relayer";

import { ProverContext } from "../src/tasks/prover/context";
import { ProcessFromRootContext } from "../src/tasks/processFromRoot/context";
import { ProposeContext } from "../src/tasks/propose/context";
import { mock, mockTaskId } from "./mock";
import * as ProverFns from "../src/tasks/prover/prover";
import * as ProcessFromRootFns from "../src/tasks/processFromRoot/processFromRoot";
import * as PropagateFns from "../src/tasks/propagate/propagate";
import * as SendOutboundRootFns from "../src/tasks/sendOutboundRoot/sendOutboundRoot";
import * as ProposeFns from "../src/tasks/propose/propose";
import * as Mockable from "../src/mockable";
import { PropagateContext } from "../src/tasks/propagate/context";
import { SendOutboundRootContext } from "../src/tasks/sendOutboundRoot/context";
Expand All @@ -20,6 +22,7 @@ export let proverCtxMock: ProverContext;
export let processFromRootCtxMock: ProcessFromRootContext;
export let propagateCtxMock: PropagateContext;
export let sendOutboundRootCtxMock: SendOutboundRootContext;
export let proposeCtxMock: ProposeContext;

export let chainReaderMock: SinonStubbedInstance<ChainReader>;
export let existsSyncStub: SinonStub;
Expand Down Expand Up @@ -76,6 +79,9 @@ export const mochaHooks = {
} as any);

getBestProviderMock = stub(Mockable, "getBestProvider").resolves("http://example.com");

proposeCtxMock = mock.proposeCtx();
stub(ProposeFns, "getContext").returns(proposeCtxMock);
},
afterEach() {
restore();
Expand Down
20 changes: 20 additions & 0 deletions packages/agents/lighthouse/test/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ProcessFromRootContext } from "../src/tasks/processFromRoot/context";
import { PropagateContext } from "../src/tasks/propagate/context";
import { mockSubgraph } from "@connext/nxtp-adapters-subgraph/test/mock";
import { SendOutboundRootContext } from "../src/tasks/sendOutboundRoot/context";
import { ProposeContext } from "../src/tasks/propose/context";

export const mockTaskId = mkBytes32("0xabcdef123");
export const mockRelayerAddress = mkAddress("0xabcdef123");
Expand Down Expand Up @@ -93,6 +94,7 @@ export const mock = {
contracts: mock.adapters.contracts(),
relayers: mock.adapters.relayers(),
database: mock.adapters.database(),
subgraph: mock.adapters.subgraph(),
databaseWriter: { database: mock.adapters.database(), pool: mockDatabasePool() },
cache: mockCache() as any,
mqClient: mockMqClient() as any,
Expand All @@ -109,6 +111,7 @@ export const mock = {
contracts: mock.adapters.deployments(),
relayers: mock.adapters.relayers(),
database: mock.adapters.database(),
subgraph: mock.adapters.subgraph(),
},
config: mock.config(),
chainData: mock.chainData(),
Expand All @@ -122,6 +125,7 @@ export const mock = {
deployments: mock.adapters.deployments(),
contracts: mock.adapters.contracts(),
relayers: mock.adapters.relayers(),
database: mock.adapters.database(),
subgraph: mock.adapters.subgraph(),
ambs: mock.adapters.ambs(),
},
Expand All @@ -144,6 +148,22 @@ export const mock = {
chainData: mock.chainData(),
};
},
proposeCtx: (): ProposeContext => {
return {
logger: new Logger({ name: "mock", level: process.env.LOG_LEVEL || "silent" }),
adapters: {
chainreader: mock.adapters.chainreader() as unknown as ChainReader,
deployments: mock.adapters.deployments(),
contracts: mock.adapters.contracts(),
relayers: mock.adapters.relayers(),
database: mock.adapters.database(),
subgraph: mock.adapters.subgraph(),
ambs: mock.adapters.ambs(),
},
config: mock.config(),
chainData: mock.chainData(),
};
},
config: (): NxtpLighthouseConfig => ({
chains: {
[mock.domain.A]: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect, getNtpTimeSeconds, getRandomBytes32 } from "@connext/nxtp-utils";
import { SinonStub, stub } from "sinon";

import { NoChainIdForHubDomain } from "../../../../src/tasks/propose/errors";
import { propose } from "../../../../src/tasks/propose/operations";
import * as ProposeFns from "../../../../src/tasks/propose/operations/propose";
import { proposeCtxMock, sendWithRelayerWithBackupStub } from "../../../globalTestHook";
import { mock } from "../../../mock";
import * as Mockable from "../../../../src/mockable";

describe("Operations: Propose", () => {
describe("#propose", () => {
beforeEach(() => {});

it("should throw an error if no hub domain id", async () => {
proposeCtxMock.chainData = new Map();
await expect(propose()).to.eventually.be.rejectedWith(NoChainIdForHubDomain);
});

it("should call propose snapshot succesfully", async () => {
let proposeSnapshotStub = stub(ProposeFns, "proposeSnapshot").resolves();

(proposeCtxMock.adapters.database.getPendingSnapshots as SinonStub).resolves([mock.entity.snapshotRoot()]);

await propose();
expect(proposeSnapshotStub).callCount(1);
});
});
});
9 changes: 7 additions & 2 deletions packages/agents/watcher/test/mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Wallet } from "ethers";
import { createStubInstance } from "sinon";
import { WatcherAdapter } from "@connext/nxtp-adapters-watcher";
import { OpModeMonitor, WatcherAdapter } from "@connext/nxtp-adapters-watcher";
import { Logger, mkHash, mock as _mock, mockSequencer } from "@connext/nxtp-utils";
import { mockSubgraph } from "@connext/nxtp-adapters-subgraph/test/mock";

Expand All @@ -15,13 +15,18 @@ export const mock = {
subgraph: mockSubgraph(),
wallet: createStubInstance(Wallet, { getAddress: Promise.resolve(mockSequencer) }),
watcher: createStubInstance(WatcherAdapter, {
checkInvariants: Promise.resolve(true),
checkInvariants: Promise.resolve({ needsPause: true }),
pause: Promise.resolve([
{ domain: mock.domain.A, error: "foo", paused: true, relevantTransaction: mkHash("0x1") },
{ domain: mock.domain.B, error: "bar", paused: true, relevantTransaction: mkHash("0x1") },
]),
alert: Promise.resolve(),
}),
monitor: createStubInstance(OpModeMonitor, {
validateProposal: Promise.resolve({ needsSwitch: false, reason: "" }),
switch: Promise.resolve(),
alert: Promise.resolve(),
}),
},
config: mock.config(),
chainData: mock.chainData(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { BaseRequestContext, createRequestContext, expect, mkHash } from "@connext/nxtp-utils";
import { PauseResponse, ReportEventType } from "@connext/nxtp-adapters-watcher";
import { stub, SinonStub } from "sinon";
import * as validateAndSwitchFns from "../../src/operations/validateAndSwitch";
import { ctxMock } from "../globalTestHook";

const requestContext = createRequestContext("test");

describe("Operations:validateAndSwitch", () => {
describe("validateAndSwitch", () => {
let validateAndSwitchStub: SinonStub<
[requestContext: BaseRequestContext, reason: string],
Promise<PauseResponse[]>
>;
beforeEach(() => {
validateAndSwitchStub = stub(validateAndSwitchFns, "switchAndAlert").resolves();
});

it("should not call switchAndAlert if it doesnt need pause", async () => {
(ctxMock.adapters.monitor.validateProposal as SinonStub).resolves({ needsSwitch: false });
await validateAndSwitchFns.validateAndSwitch();
expect(validateAndSwitchStub.callCount).to.be.eq(0);
});

it("should call switchAndAlert if needs pause", async () => {
(ctxMock.adapters.monitor.validateProposal as SinonStub).resolves({ needsSwitch: true });
await validateAndSwitchFns.validateAndSwitch();
expect(validateAndSwitchStub).to.have.been.calledOnce;
});
});

describe("switchAndAlert", () => {
it("should switch and alert", async () => {
(ctxMock.adapters.monitor.switch as SinonStub).resolves();
await validateAndSwitchFns.switchAndAlert(requestContext, "reason");
expect(ctxMock.adapters.monitor.alert).to.be.calledOnce;
});
});
});