Skip to content

Commit

Permalink
test(relayer): add more e2e tests
Browse files Browse the repository at this point in the history
- [x] Minor refactoring for relayer sdk
- [x] Add relayed messages for integration tests
- [x] Add relayed messages for e2e cli tests
- [x] Minor refactoring for maci state generation
  • Loading branch information
0xmad committed Feb 18, 2025
1 parent dcb1dc0 commit 0cbfc40
Show file tree
Hide file tree
Showing 42 changed files with 2,581 additions and 1,397 deletions.
1 change: 1 addition & 0 deletions .github/workflows/relayer-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ env:
MONGODB_PASSWORD: ${{ secrets.MONGODB_PASSWORD }}
MONGODB_DATABASE: ${{ secrets.MONGODB_DATABASE }}
MNEMONIC: ${{ secrets.RELAYER_MNEMONIC }}
CRON_EXPRESSION: ${{ secrets.CRON_EXPRESSION }}

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
Expand Down
4 changes: 4 additions & 0 deletions apps/relayer/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ PORT=
MNEMONIC=""

MAX_MESSAGES=20

# Cron expression for scheduled message publishing
CRON_EXPRESSION=

1 change: 1 addition & 0 deletions apps/relayer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
- MONGODB_PASSWORD=${MONGODB_PASSWORD}
- MONGODB_DATABASE=${MONGODB_DATABASE}
- MNEMONIC=${MNEMONIC}
- CRON_EXPRESSION=${CRON_EXPRESSION}
depends_on:
- mongodb
networks:
Expand Down
10 changes: 5 additions & 5 deletions apps/relayer/tests/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ export class TestDeploy {
quiet: true,
});

const maciState = await genMaciStateFromContract(
signer.provider,
maciAddresses.maciAddress,
const maciState = await genMaciStateFromContract({
provider: signer.provider,
address: maciAddresses.maciAddress,
coordinatorKeypair,
0n,
);
pollId: 0n,
});

this.contractsData.maciState = maciState;
this.contractsData.maciContractAddress = maciAddresses.maciAddress;
Expand Down
2 changes: 1 addition & 1 deletion apps/relayer/ts/message/message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class MessageService {
* @param args publish messages dto
* @returns transaction and ipfs hashes
*/
@Cron(CronExpression.EVERY_HOUR, { name: "publishMessages" })
@Cron(process.env.CRON_EXPRESSION || CronExpression.EVERY_HOUR, { name: "publishMessages" })
async publishMessages(): Promise<boolean> {
const messages = await this.messageRepository.find({ messageBatch: { $exists: false } });

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { jest } from "@jest/globals";
import { ZeroAddress } from "ethers";
import { MACI__factory as MACIFactory, Poll__factory as PollFactory } from "maci-sdk";

import { IpfsService } from "../../ipfs/ipfs.service.js";
import { MessageBatchDto } from "../messageBatch.dto.js";
Expand All @@ -11,12 +9,7 @@ import { defaultIpfsHash, defaultMessageBatches } from "./utils.js";

jest.mock("maci-sdk", (): unknown => ({
getDefaultSigner: jest.fn(),
MACI__factory: {
connect: jest.fn(),
},
Poll__factory: {
connect: jest.fn(),
},
relayMessages: jest.fn(),
}));

describe("MessageBatchService", () => {
Expand All @@ -31,26 +24,12 @@ describe("MessageBatchService", () => {
find: jest.fn().mockImplementation(() => Promise.resolve(defaultMessageBatches)),
};

const mockMaciContract = {
polls: jest.fn().mockImplementation(() => Promise.resolve({ poll: ZeroAddress })),
};

const mockPollContract = {
hashMessageAndEncPubKey: jest.fn().mockImplementation(() => Promise.resolve("hash")),
relayMessagesBatch: jest
.fn()
.mockImplementation(() => Promise.resolve({ wait: jest.fn().mockImplementation(() => Promise.resolve()) })),
};

beforeEach(() => {
mockRepository.create = jest.fn().mockImplementation(() => Promise.resolve(defaultMessageBatches));
mockRepository.find = jest.fn().mockImplementation(() => Promise.resolve(defaultMessageBatches));
mockIpfsService.add = jest.fn().mockImplementation(() => Promise.resolve(defaultIpfsHash));
mockIpfsService.cidToBytes32 = jest.fn().mockImplementation(() => Promise.resolve(defaultIpfsHash));
mockIpfsService.init = jest.fn();

MACIFactory.connect = jest.fn().mockImplementation(() => mockMaciContract) as typeof MACIFactory.connect;
PollFactory.connect = jest.fn().mockImplementation(() => mockPollContract) as typeof PollFactory.connect;
});

afterEach(() => {
Expand All @@ -68,7 +47,6 @@ describe("MessageBatchService", () => {

expect(result).toStrictEqual(defaultMessageBatches);
expect(messageBatches).toStrictEqual(defaultMessageBatches);
expect(mockPollContract.relayMessagesBatch).toHaveBeenCalledTimes(1);
});

test("should throw an error if can't find message batches", async () => {
Expand Down
13 changes: 2 additions & 11 deletions apps/relayer/ts/messageBatch/messageBatch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { validate } from "class-validator";
import flatten from "lodash/flatten.js";
import uniqBy from "lodash/uniqBy.js";
import { PubKey } from "maci-domainobjs";
import { getDefaultSigner, MACI__factory as MACIFactory, Poll__factory as PollFactory } from "maci-sdk";

import type { MessageBatch } from "./messageBatch.schema.js";
import type { RootFilterQuery } from "mongoose";
Expand Down Expand Up @@ -100,19 +99,11 @@ export class MessageBatchService {
"maciContractAddress",
);

const { getDefaultSigner, relayMessages } = await import("maci-sdk");
const signer = await getDefaultSigner();
const maciContract = MACIFactory.connect(maciAddress, signer);
const pollAddresses = await maciContract.polls(pollId);
const pollContract = PollFactory.connect(pollAddresses.poll, signer);

const messageHashes = await Promise.all(
allMessages.map(({ data, publicKey }) =>
pollContract.hashMessageAndEncPubKey({ data }, { x: BigInt(publicKey[0]), y: BigInt(publicKey[1]) }),
),
);

const bytes32IpfsHash = await this.ipfsService.cidToBytes32(ipfsHash);
await pollContract.relayMessagesBatch(messageHashes, bytes32IpfsHash).then((tx) => tx.wait());
await relayMessages({ maciAddress, pollId, ipfsHash: bytes32IpfsHash, messages: allMessages, signer });

return messageBatches;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ zkeys
state.json
deployed-contracts.json
deploy-config.json
backup

Loading

0 comments on commit 0cbfc40

Please sign in to comment.