Skip to content

Commit

Permalink
feat: anonymous poll joining milestone 1 (#1625)
Browse files Browse the repository at this point in the history
* feat(poll): add chain hash features

BREAKING CHANGE: message processing is changed

* fix(ipoll): add missing parameter

* fix(poll-tests): add missing parameter maxMessagebatchSize

* feat(poll.ts): add chain hash updating

* test(poll tests): add test for checking chain hash computation

* feat(poll.ts): add batch hashes array computation

* feat(poll.sol): pad zeroes to the maximum size of batch

* feat(messageprocessor): update process messages to use chain hash

* refactor(vkregistry): refactor function call

* feat(processmessages.circom): add chainHash feature in circuits and test for that

* test(processmessages): rearrange test for key-change

* refactor(mergemessages): refactor functions calls which include mergemessages

* refactor(mergemessages): add some more changes about functions call which  include mergemessages

* test(all tests): fixing tests after refactoring code

* refactor(accqueue): remove all calls for accqueue

* fix(currentmessagebatchindex): fix message batch indexing

* refactor(circuit tests): refactor code for circuit testing

* test(ceremonyparams.test): correct constants for CeremonyParams test

* perf(processmessages.circom + contracts): optimize last batch padding, remove unused inputs

* docs(padlastbatch method): update doc comment

* docs(poll.ts): remove stale comments

* docs(test comments): fix typos

* ci(treedepths mock): modify interface for mocked function

* fix(ceremony params test): fix circuit inputs

* test(messagevalidator): fix function calls for messagevalidator circuit in tests

* chore(comments): fix unusefull comments

* refactor(poll.sol): replace external contracts with maci only

* perf(messageprocessor.sol): hardcode initialization for batchHashes array

* docs(comments): fix some more comments

* test(test for pr checks): correct some of tests for PR checks

* ci: 🎡 renamed old ProcessMessages_10-2-1-2_test

* ci: 🎡 correct rapidsnark/build/prover path

* style(reviews): solve some reviews for merging

* refactor(messageaqq): remove more message merging and message aqq

* style(messageaqq): remove more message merging and message aqq

* refactor(messageaqq): remove message aqq from subgraph

* test(coordinator): hide NOT_MERGED_MESSAGE_TREE error

* test(coordinator): fix test about message merging

* test(proveonchain): change chainHash calculation

* test(proveonchain): fix chainHashes declaration

* test(proveonchain): fix chainHash calculation

* test(proveonchain): fix chainHashes calculations

* test(proveonchain): fix chainHashes calculation

* test(proveonchain): fix loop limit

* style(review comments): resolve some of review comments

* style(review comments): resolve some of review comments

* test(lint:ts): fix e2e test because of lint:ts check

* docs(wrong changes): fix wrong changes about documentation that is not in our scope

* refactor(batchsizes): change batchSizes struct with messageBatchSize variable

* refactor(contracts): rollback to provide external contract references

* docs(messageprocessor.sol): fix typo

* refactor(messagebatchsize): chenge messageBatchSize location from Params.sol to Poll.sol

* refactor(maxmessages): remove maxMessages from maxValues

* refactor(sltimestemp): remove slTimestamp from circuits

* refactor(review comments): resolve more review comments

* fix(subgraph): fix bug about maxVoteOptions dunction call

* fix(sltimestamp): fix test for removing slTimestap signal

* refactor(promise.all): refactor promise.all for only one async call

* fix(subgraph): try to fix subgraph build

* revert(.nx folder): remove .nx folder from cli folder

---------

Co-authored-by: radojevicMihailo <[email protected]>
Co-authored-by: Aleksandar Veljković <[email protected]>
Co-authored-by: Boris Cvitak <[email protected]>
  • Loading branch information
4 people authored and 0xmad committed Dec 4, 2024
1 parent ea5b2fb commit 5566cfe
Show file tree
Hide file tree
Showing 111 changed files with 1,708 additions and 4,841 deletions.
5 changes: 0 additions & 5 deletions apps/subgraph/schemas/schema.v1.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type Poll @entity {
pollId: BigInt! # uint256
duration: BigInt! # uint256
treeDepth: BigInt! # uint8
maxMessages: BigInt!
maxVoteOption: BigInt!
messageProcessor: Bytes! # address
tally: Bytes! # address
Expand All @@ -43,10 +42,6 @@ type Poll @entity {
stateRoot: BigInt # uint256
numSignups: BigInt! # uint256
numMessages: BigInt! # uint256
"merge message tree after ended"
numSrQueueOps: BigInt # uint256
messageRoot: BigInt

"relations"
owner: Bytes!
maci: MACI!
Expand Down
22 changes: 9 additions & 13 deletions apps/subgraph/src/maci.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
/* eslint-disable no-underscore-dangle */
import { Address, BigInt as GraphBN } from "@graphprotocol/graph-ts";

import { DeployPoll as DeployPollEvent, SignUp as SignUpEvent, MACI as MaciContract } from "../generated/MACI/MACI";
import { DeployPoll as DeployPollEvent, SignUp as SignUpEvent } from "../generated/MACI/MACI";
import { Poll } from "../generated/schema";
import { Poll as PollTemplate } from "../generated/templates";
import { Poll as PollContract } from "../generated/templates/Poll/Poll";

import { ONE_BIG_INT, MESSAGE_TREE_ARITY } from "./utils/constants";
import { ONE_BIG_INT } from "./utils/constants";
import { createOrLoadMACI, createOrLoadUser, createOrLoadAccount } from "./utils/entity";

export function handleDeployPoll(event: DeployPollEvent): void {
const maci = createOrLoadMACI(event);

const id = event.params._pollId;

const maciContract = MaciContract.bind(Address.fromBytes(maci.id));
const contracts = maciContract.getPoll(id);
const poll = new Poll(contracts.poll);
const contract = PollContract.bind(contracts.poll);
const poll = new Poll(event.params.pollAddr.poll);
const contract = PollContract.bind(event.params.pollAddr.poll);
const maxVoteOptions = contract.maxVoteOptions();
const treeDepths = contract.treeDepths();
const durations = contract.getDeployTimeAndDuration();

poll.pollId = id;
poll.messageProcessor = contracts.messageProcessor;
poll.tally = contracts.tally;
poll.maxMessages = GraphBN.fromI32(MESSAGE_TREE_ARITY ** treeDepths.getMessageTreeDepth());
poll.maxVoteOption = GraphBN.fromI32(MESSAGE_TREE_ARITY ** treeDepths.getVoteOptionTreeDepth());
poll.pollId = event.params._pollId;
poll.messageProcessor = event.params.pollAddr.messageProcessor;
poll.tally = event.params.pollAddr.tally;
poll.maxVoteOption = maxVoteOptions;
poll.treeDepth = GraphBN.fromI32(treeDepths.value0);
poll.duration = durations.value1;
poll.mode = GraphBN.fromI32(event.params._mode);
Expand Down
22 changes: 0 additions & 22 deletions apps/subgraph/src/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import { Poll, Vote, MACI } from "../generated/schema";
import {
MergeMaciState as MergeMaciStateEvent,
MergeMessageAq as MergeMessageAqEvent,
MergeMessageAqSubRoots as MergeMessageAqSubRootsEvent,
PublishMessage as PublishMessageEvent,
} from "../generated/templates/Poll/Poll";

Expand All @@ -29,26 +27,6 @@ export function handleMergeMaciState(event: MergeMaciStateEvent): void {
}
}

export function handleMergeMessageAq(event: MergeMessageAqEvent): void {
const poll = Poll.load(event.address);

if (poll) {
poll.messageRoot = event.params._messageRoot;
poll.updatedAt = event.block.timestamp;
poll.save();
}
}

export function handleMergeMessageAqSubRoots(event: MergeMessageAqSubRootsEvent): void {
const poll = Poll.load(event.address);

if (poll) {
poll.numSrQueueOps = event.params._numSrQueueOps;
poll.updatedAt = event.block.timestamp;
poll.save();
}
}

export function handlePublishMessage(event: PublishMessageEvent): void {
const vote = new Vote(event.transaction.hash.concatI32(event.logIndex.toI32()));
vote.data = event.params._message.data;
Expand Down
4 changes: 0 additions & 4 deletions apps/subgraph/templates/subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ templates:
eventHandlers:
- event: MergeMaciState(indexed uint256,indexed uint256)
handler: handleMergeMaciState
- event: MergeMessageAq(indexed uint256)
handler: handleMergeMessageAq
- event: MergeMessageAqSubRoots(indexed uint256)
handler: handleMergeMessageAqSubRoots
- event: PublishMessage((uint256[10]),(uint256,uint256))
handler: handlePublishMessage
file: ./src/poll.ts
8 changes: 5 additions & 3 deletions apps/subgraph/tests/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ export const DEFAULT_MESSAGE_PROCESSOR_ADDRESS = Address.fromString("0x000000000
export const DEFAULT_TALLY_ADDRESS = Address.fromString("0x0000000000000000000000000000000000000003");

export function mockPollContract(): void {
createMockedFunction(DEFAULT_POLL_ADDRESS, "treeDepths", "treeDepths():(uint8,uint8,uint8,uint8)").returns([
createMockedFunction(DEFAULT_POLL_ADDRESS, "maxVoteOptions", "maxVoteOptions():(uint256)").returns([
ethereum.Value.fromI32(20),
]);

createMockedFunction(DEFAULT_POLL_ADDRESS, "treeDepths", "treeDepths():(uint8,uint8)").returns([
ethereum.Value.fromI32(1),
ethereum.Value.fromI32(2),
ethereum.Value.fromI32(3),
ethereum.Value.fromI32(4),
]);

createMockedFunction(
Expand Down
39 changes: 4 additions & 35 deletions apps/subgraph/tests/poll/poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,16 @@ import { test, describe, afterEach, clearStore, assert, beforeEach } from "match

import { MACI, Poll } from "../../generated/schema";
import { handleDeployPoll } from "../../src/maci";
import {
handleMergeMaciState,
handleMergeMessageAq,
handleMergeMessageAqSubRoots,
handlePublishMessage,
} from "../../src/poll";
import { DEFAULT_POLL_ADDRESS, mockMaciContract, mockPollContract } from "../common";
import { handleMergeMaciState, handlePublishMessage } from "../../src/poll";
import { DEFAULT_POLL_ADDRESS, mockPollContract } from "../common";
import { createDeployPollEvent } from "../maci/utils";

import {
createMergeMaciStateEvent,
createMergeMessageAqEvent,
createMergeMessageAqSubRootsEvent,
createPublishMessageEvent,
} from "./utils";
import { createMergeMaciStateEvent, createPublishMessageEvent } from "./utils";

export { handleMergeMaciState, handleMergeMessageAq, handleMergeMessageAqSubRoots, handlePublishMessage };
export { handleMergeMaciState, handlePublishMessage };

describe("Poll", () => {
beforeEach(() => {
mockMaciContract();
mockPollContract();

// mock the deploy poll event with non qv mode set
Expand Down Expand Up @@ -53,26 +42,6 @@ describe("Poll", () => {
assert.assertTrue(maci.polls.load().length === 1);
});

test("should handle merge message queue properly", () => {
const event = createMergeMessageAqEvent(DEFAULT_POLL_ADDRESS, BigInt.fromI32(1));

handleMergeMessageAq(event);

const poll = Poll.load(DEFAULT_POLL_ADDRESS)!;

assert.fieldEquals("Poll", poll.id.toHex(), "messageRoot", "1");
});

test("should handle merge message queue subroots properly", () => {
const event = createMergeMessageAqSubRootsEvent(DEFAULT_POLL_ADDRESS, BigInt.fromI32(1));

handleMergeMessageAqSubRoots(event);

const poll = Poll.load(event.address)!;

assert.fieldEquals("Poll", poll.id.toHex(), "numSrQueueOps", "1");
});

test("should handle publish message properly", () => {
const event = createPublishMessageEvent(
DEFAULT_POLL_ADDRESS,
Expand Down
25 changes: 1 addition & 24 deletions apps/subgraph/tests/poll/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { Address, BigInt as GraphBN, ethereum } from "@graphprotocol/graph-ts";
// eslint-disable-next-line import/no-extraneous-dependencies
import { newMockEvent } from "matchstick-as";

import {
MergeMaciState,
MergeMessageAq,
MergeMessageAqSubRoots,
PublishMessage,
} from "../../generated/templates/Poll/Poll";
import { MergeMaciState, PublishMessage } from "../../generated/templates/Poll/Poll";

export function createMergeMaciStateEvent(address: Address, stateRoot: GraphBN, numSignups: GraphBN): MergeMaciState {
const event = changetype<MergeMaciState>(newMockEvent());
Expand All @@ -19,24 +14,6 @@ export function createMergeMaciStateEvent(address: Address, stateRoot: GraphBN,
return event;
}

export function createMergeMessageAqEvent(address: Address, messageRoot: GraphBN): MergeMessageAq {
const event = changetype<MergeMessageAq>(newMockEvent());

event.parameters.push(new ethereum.EventParam("_messageRoot", ethereum.Value.fromUnsignedBigInt(messageRoot)));
event.address = address;

return event;
}

export function createMergeMessageAqSubRootsEvent(address: Address, numSrQueueOps: GraphBN): MergeMessageAqSubRoots {
const event = changetype<MergeMessageAqSubRoots>(newMockEvent());

event.parameters.push(new ethereum.EventParam("_numSrQueueOps", ethereum.Value.fromUnsignedBigInt(numSrQueueOps)));
event.address = address;

return event;
}

export function createPublishMessageEvent(
address: Address,
data: GraphBN[],
Expand Down
2 changes: 1 addition & 1 deletion apps/website/static/img/completingAPoll.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 6 additions & 8 deletions packages/circuits/circom/circuits.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
{
"ProcessMessages_10-2-1-2_test": {
"ProcessMessages_10-20-2_test": {
"file": "./core/qv/processMessages",
"template": "ProcessMessages",
"params": [10, 2, 1, 2],
"params": [10, 20, 2],
"pubs": [
"numSignUps",
"index",
"batchEndIndex",
"msgRoot",
"currentSbCommitment",
"newSbCommitment",
"pollEndTimestamp",
"outputBatchHash",
"actualStateTreeDepth",
"coordinatorPublicKeyHash"
]
},
"ProcessMessagesNonQv_10-2-1-2_test": {
"ProcessMessagesNonQv_10-20-2_test": {
"file": "./core/non-qv/processMessages",
"template": "ProcessMessagesNonQv",
"params": [10, 2, 1, 2],
"params": [10, 20, 2],
"pubs": [
"numSignUps",
"index",
"batchEndIndex",
"msgRoot",
"currentSbCommitment",
"newSbCommitment",
"pollEndTimestamp",
"outputBatchHash",
"actualStateTreeDepth",
"coordinatorPublicKeyHash"
]
Expand Down
Loading

0 comments on commit 5566cfe

Please sign in to comment.