-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(relayer): fetch message batches
- [x] Add message batches fetch api method - [x] Refactoring for integration tests
- Loading branch information
Showing
16 changed files
with
506 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import hardhat from "hardhat"; | ||
import { deploy, deployPoll, deployVkRegistryContract, joinPoll, setVerifyingKeys, signup } from "maci-cli"; | ||
import { genMaciStateFromContract } from "maci-contracts"; | ||
import { Keypair } from "maci-domainobjs"; | ||
|
||
import { | ||
INT_STATE_TREE_DEPTH, | ||
MESSAGE_BATCH_SIZE, | ||
STATE_TREE_DEPTH, | ||
VOTE_OPTION_TREE_DEPTH, | ||
pollJoinedZkey, | ||
pollJoiningZkey, | ||
processMessagesZkeyPathNonQv, | ||
tallyVotesZkeyPathNonQv, | ||
pollWasm, | ||
pollWitgen, | ||
rapidsnark, | ||
} from "./constants"; | ||
|
||
interface IContractsData { | ||
initialized: boolean; | ||
user?: Keypair; | ||
voiceCredits?: number; | ||
timestamp?: number; | ||
stateLeafIndex?: number; | ||
maciContractAddress?: string; | ||
maciState?: Awaited<ReturnType<typeof genMaciStateFromContract>>; | ||
} | ||
|
||
export class TestDeploy { | ||
private static INSTANCE?: TestDeploy; | ||
|
||
readonly contractsData: IContractsData = { | ||
initialized: false, | ||
}; | ||
|
||
static async getInstance(): Promise<TestDeploy> { | ||
if (!TestDeploy.INSTANCE) { | ||
TestDeploy.INSTANCE = new TestDeploy(); | ||
await TestDeploy.INSTANCE.contractsInit(); | ||
} | ||
|
||
return TestDeploy.INSTANCE; | ||
} | ||
|
||
private async contractsInit(): Promise<void> { | ||
const [signer] = await hardhat.ethers.getSigners(); | ||
const coordinatorKeypair = new Keypair(); | ||
const user = new Keypair(); | ||
|
||
const vkRegistry = await deployVkRegistryContract({ signer }); | ||
await setVerifyingKeys({ | ||
quiet: true, | ||
vkRegistry, | ||
stateTreeDepth: STATE_TREE_DEPTH, | ||
intStateTreeDepth: INT_STATE_TREE_DEPTH, | ||
voteOptionTreeDepth: VOTE_OPTION_TREE_DEPTH, | ||
messageBatchSize: MESSAGE_BATCH_SIZE, | ||
processMessagesZkeyPathNonQv, | ||
tallyVotesZkeyPathNonQv, | ||
pollJoiningZkeyPath: pollJoiningZkey, | ||
pollJoinedZkeyPath: pollJoinedZkey, | ||
useQuadraticVoting: false, | ||
signer, | ||
}); | ||
|
||
const maciAddresses = await deploy({ stateTreeDepth: 10, signer }); | ||
|
||
await deployPoll({ | ||
pollDuration: 30, | ||
intStateTreeDepth: INT_STATE_TREE_DEPTH, | ||
messageBatchSize: MESSAGE_BATCH_SIZE, | ||
voteOptionTreeDepth: VOTE_OPTION_TREE_DEPTH, | ||
coordinatorPubkey: coordinatorKeypair.pubKey.serialize(), | ||
useQuadraticVoting: false, | ||
signer, | ||
}); | ||
|
||
await signup({ maciAddress: maciAddresses.maciAddress, maciPubKey: user.pubKey.serialize(), signer }); | ||
|
||
const { pollStateIndex, timestamp, voiceCredits } = await joinPoll({ | ||
maciAddress: maciAddresses.maciAddress, | ||
pollId: 0n, | ||
privateKey: user.privKey.serialize(), | ||
stateIndex: 1n, | ||
pollJoiningZkey, | ||
pollWasm, | ||
pollWitgen, | ||
rapidsnark, | ||
signer, | ||
useWasm: true, | ||
quiet: true, | ||
}); | ||
|
||
const maciState = await genMaciStateFromContract( | ||
signer.provider, | ||
maciAddresses.maciAddress, | ||
coordinatorKeypair, | ||
0n, | ||
); | ||
|
||
this.contractsData.maciState = maciState; | ||
this.contractsData.maciContractAddress = maciAddresses.maciAddress; | ||
this.contractsData.stateLeafIndex = Number(pollStateIndex); | ||
this.contractsData.timestamp = Number(timestamp); | ||
this.contractsData.voiceCredits = Number(voiceCredits); | ||
this.contractsData.user = user; | ||
this.contractsData.initialized = true; | ||
} | ||
} | ||
|
||
const testDeploy = await TestDeploy.getInstance(); | ||
|
||
export async function waitForInitialization(): Promise<void> { | ||
return new Promise((resolve) => { | ||
const checkInitialization = () => { | ||
if (testDeploy.contractsData.initialized) { | ||
resolve(); | ||
} else { | ||
setTimeout(checkInitialization, 1000); | ||
} | ||
}; | ||
|
||
setTimeout(checkInitialization, 2000); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { jest } from "@jest/globals"; | ||
import { HttpStatus, ValidationPipe, type INestApplication } from "@nestjs/common"; | ||
import { Test } from "@nestjs/testing"; | ||
import { formatProofForVerifierContract, genProofSnarkjs } from "maci-contracts"; | ||
import { Keypair } from "maci-domainobjs"; | ||
import request from "supertest"; | ||
|
||
import type { App } from "supertest/types"; | ||
|
||
import { AppModule } from "../ts/app.module"; | ||
|
||
import { pollJoinedWasm, pollJoinedZkey } from "./constants"; | ||
import { TestDeploy, waitForInitialization } from "./deploy"; | ||
|
||
jest.unmock("maci-contracts/typechain-types"); | ||
|
||
describe("Integration message batches", () => { | ||
let app: INestApplication; | ||
let stateLeafIndex: number; | ||
let maciContractAddress: string; | ||
let user: Keypair; | ||
|
||
beforeAll(async () => { | ||
await waitForInitialization(); | ||
const testDeploy = await TestDeploy.getInstance(); | ||
const poll = testDeploy.contractsData.maciState!.polls.get(0n); | ||
|
||
poll!.updatePoll(BigInt(testDeploy.contractsData.maciState!.pubKeys.length)); | ||
|
||
stateLeafIndex = Number(testDeploy.contractsData.stateLeafIndex); | ||
maciContractAddress = testDeploy.contractsData.maciContractAddress!; | ||
user = testDeploy.contractsData.user!; | ||
|
||
const moduleFixture = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(); | ||
app.useGlobalPipes(new ValidationPipe({ transform: true })); | ||
await app.listen(3001); | ||
|
||
const circuitInputs = poll!.joinedCircuitInputs({ | ||
maciPrivKey: testDeploy.contractsData.user!.privKey, | ||
stateLeafIndex: BigInt(testDeploy.contractsData.stateLeafIndex!), | ||
voiceCreditsBalance: BigInt(testDeploy.contractsData.voiceCredits!), | ||
joinTimestamp: BigInt(testDeploy.contractsData.timestamp!), | ||
}); | ||
|
||
const { proof } = await genProofSnarkjs({ | ||
inputs: circuitInputs as unknown as Record<string, bigint>, | ||
zkeyPath: pollJoinedZkey, | ||
wasmPath: pollJoinedWasm, | ||
}); | ||
|
||
const keypair = new Keypair(); | ||
|
||
await request(app.getHttpServer() as App) | ||
.post("/v1/messages/publish") | ||
.send({ | ||
messages: [ | ||
{ | ||
data: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], | ||
publicKey: keypair.pubKey.serialize(), | ||
}, | ||
], | ||
poll: 0, | ||
maciContractAddress, | ||
stateLeafIndex, | ||
proof: formatProofForVerifierContract(proof), | ||
}) | ||
.expect(HttpStatus.CREATED); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
describe("/v1/messageBatches/get", () => { | ||
test("should throw an error if dto is invalid", async () => { | ||
const result = await request(app.getHttpServer() as App) | ||
.get("/v1/messageBatches/get") | ||
.send({ | ||
limit: 0, | ||
poll: -1, | ||
maciContractAddress: "invalid", | ||
publicKey: "invalid", | ||
ipfsHashes: ["invalid1", "invalid2"], | ||
}) | ||
.expect(HttpStatus.BAD_REQUEST); | ||
|
||
expect(result.body).toStrictEqual({ | ||
error: "Bad Request", | ||
statusCode: HttpStatus.BAD_REQUEST, | ||
message: [ | ||
"limit must be a positive number", | ||
"poll must not be less than 0", | ||
"maciContractAddress must be an Ethereum address", | ||
"IPFS hash is invalid", | ||
"Public key (invalid) is invalid", | ||
], | ||
}); | ||
}); | ||
|
||
test("should get message batches properly", async () => { | ||
const result = await request(app.getHttpServer() as App) | ||
.get("/v1/messageBatches/get") | ||
.send({ | ||
limit: 10, | ||
poll: 0, | ||
maciContractAddress, | ||
publicKey: user!.pubKey.serialize(), | ||
}) | ||
.expect(HttpStatus.OK); | ||
|
||
expect(result.status).toBe(HttpStatus.OK); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.