-
Notifications
You must be signed in to change notification settings - Fork 160
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
feat(cli): add publish batch method for cli sdk #1251
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { expect } from "chai"; | ||
import { getDefaultSigner } from "maci-contracts"; | ||
import { Poll__factory as PollFactory } from "maci-contracts/typechain-types"; | ||
import { SNARK_FIELD_SIZE } from "maci-crypto"; | ||
import { Keypair } from "maci-domainobjs"; | ||
|
||
import type { Signer } from "ethers"; | ||
|
||
import { | ||
deploy, | ||
deployPoll, | ||
deployVkRegistryContract, | ||
setVerifyingKeys, | ||
publishBatch, | ||
signup, | ||
} from "../../ts/commands"; | ||
import { DeployedContracts, IPublishBatchArgs, IPublishMessage, PollContracts } from "../../ts/utils"; | ||
import { deployPollArgs, setVerifyingKeysArgs, deployArgs } from "../constants"; | ||
|
||
describe("publish", () => { | ||
let maciAddresses: DeployedContracts; | ||
let pollAddresses: PollContracts; | ||
let signer: Signer; | ||
|
||
const messages: IPublishMessage[] = [ | ||
{ | ||
stateIndex: 1n, | ||
voteOptionIndex: 1n, | ||
nonce: 1n, | ||
newVoteWeight: 1n, | ||
salt: 1n, | ||
}, | ||
{ | ||
stateIndex: 1n, | ||
voteOptionIndex: 2n, | ||
nonce: 2n, | ||
newVoteWeight: 1n, | ||
}, | ||
]; | ||
|
||
// before all tests we deploy the vk registry contract and set the verifying keys | ||
before(async () => { | ||
signer = await getDefaultSigner(); | ||
|
||
// we deploy the vk registry contract | ||
await deployVkRegistryContract({ signer }); | ||
// we set the verifying keys | ||
await setVerifyingKeys({ ...setVerifyingKeysArgs, signer }); | ||
}); | ||
|
||
describe("publish batch messages", () => { | ||
const user = new Keypair(); | ||
|
||
let defaultArgs: IPublishBatchArgs; | ||
|
||
before(async () => { | ||
// deploy the smart contracts | ||
maciAddresses = await deploy({ ...deployArgs, signer }); | ||
// deploy a poll contract | ||
pollAddresses = await deployPoll({ ...deployPollArgs, signer }); | ||
|
||
defaultArgs = { | ||
maciContractAddress: maciAddresses.maciAddress, | ||
publicKey: user.pubKey.serialize(), | ||
privateKey: user.privKey.serialize(), | ||
messages, | ||
pollId: 0n, | ||
signer, | ||
}; | ||
|
||
await signup({ maciAddress: maciAddresses.maciAddress, maciPubKey: user.pubKey.serialize(), signer }); | ||
}); | ||
|
||
it("should publish messages properly", async () => { | ||
const pollContract = PollFactory.connect(pollAddresses.poll, signer); | ||
const initialNumMessages = await pollContract.numMessages(); | ||
|
||
const { hash } = await publishBatch(defaultArgs); | ||
const numMessages = await pollContract.numMessages(); | ||
|
||
expect(initialNumMessages).to.eq(1n); | ||
expect(hash).to.not.eq(null); | ||
expect(hash).to.not.eq(undefined); | ||
expect(numMessages).to.eq(BigInt(messages.length + 1)); | ||
}); | ||
|
||
it("should throw error if public key is invalid", async () => { | ||
await expect(publishBatch({ ...defaultArgs, publicKey: "invalid" })).eventually.rejectedWith( | ||
"invalid MACI public key", | ||
); | ||
}); | ||
|
||
it("should throw error if private key is invalid", async () => { | ||
await expect(publishBatch({ ...defaultArgs, privateKey: "invalid" })).eventually.rejectedWith( | ||
"invalid MACI private key", | ||
); | ||
}); | ||
|
||
it("should throw error if poll id is invalid", async () => { | ||
await expect(publishBatch({ ...defaultArgs, pollId: -1n })).eventually.rejectedWith("invalid poll id -1"); | ||
}); | ||
|
||
it("should throw error if current poll is not deployed", async () => { | ||
await expect(publishBatch({ ...defaultArgs, pollId: 9000n })).eventually.rejectedWith("PollDoesNotExist(9000)"); | ||
}); | ||
|
||
it("should throw error if message is invalid", async () => { | ||
await expect( | ||
publishBatch({ | ||
...defaultArgs, | ||
messages: [...messages, { ...messages[0], voteOptionIndex: -1n }], | ||
}), | ||
).eventually.rejectedWith("invalid vote option index"); | ||
|
||
await expect( | ||
publishBatch({ | ||
...defaultArgs, | ||
messages: [...messages, { ...messages[0], stateIndex: 0n }], | ||
}), | ||
).eventually.rejectedWith("invalid state index"); | ||
|
||
await expect( | ||
publishBatch({ | ||
...defaultArgs, | ||
messages: [...messages, { ...messages[0], nonce: -1n }], | ||
}), | ||
).eventually.rejectedWith("invalid nonce"); | ||
|
||
await expect( | ||
publishBatch({ | ||
...defaultArgs, | ||
messages: [...messages, { ...messages[0], salt: SNARK_FIELD_SIZE + 1n }], | ||
}), | ||
).eventually.rejectedWith("invalid salt"); | ||
}); | ||
}); | ||
}); |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the point of using the same key here for all messages in the batch, we just assume that all messages in the batch are from the same user either way for simplicity sake (though that doesn't necessarily have to be the case), so I would suggest we move line 226 out of the loop to avoid computing the ECDH key every time; and also we should probably return this key like we do in publishMessage.