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

feat(subgraph): optimize subgraph schema (maci) #1564

Merged
merged 1 commit into from
Jun 14, 2024
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
6 changes: 5 additions & 1 deletion subgraph/schemas/schema.v1.graphql
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
type MACI @entity {
id: ID!
id: Bytes! # address
stateTreeDepth: BigInt! # uint8
updatedAt: BigInt!

"state"
numSignUps: BigInt!
numPoll: BigInt!
latestPoll: Bytes!

"relations"
polls: [Poll!]! @derivedFrom(field: "maci")
}

type User @entity(immutable: true) {
Expand Down Expand Up @@ -46,6 +49,7 @@ type Poll @entity {

"relations"
owner: Bytes!
maci: MACI!
votes: [Vote!]! @derivedFrom(field: "poll")
}

Expand Down
1 change: 1 addition & 0 deletions subgraph/src/maci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function handleDeployPoll(event: DeployPollEvent): void {

poll.numSignups = maci.numSignUps;
poll.numMessages = GraphBN.zero();
poll.maci = maci.id;
poll.save();

maci.numPoll = maci.numPoll.plus(ONE_BIG_INT);
Expand Down
16 changes: 9 additions & 7 deletions subgraph/src/poll.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-underscore-dangle */

import { Poll, Vote } from "../generated/schema";
import { Poll, Vote, MACI } from "../generated/schema";
import {
MergeMaciState as MergeMaciStateEvent,
MergeMessageAq as MergeMessageAqEvent,
Expand All @@ -9,7 +9,6 @@ import {
} from "../generated/templates/Poll/Poll";

import { ONE_BIG_INT } from "./utils/constants";
import { createOrLoadMACI } from "./utils/entity";

export function handleMergeMaciState(event: MergeMaciStateEvent): void {
const poll = Poll.load(event.address);
Expand All @@ -19,12 +18,15 @@ export function handleMergeMaciState(event: MergeMaciStateEvent): void {
poll.numSignups = event.params._numSignups;
poll.updatedAt = event.block.timestamp;
poll.save();
}

const maci = createOrLoadMACI(event);
maci.numSignUps = event.params._numSignups;
maci.updatedAt = event.block.timestamp;
maci.save();
const maci = MACI.load(poll.maci);

if (maci) {
maci.numSignUps = event.params._numSignups;
maci.updatedAt = event.block.timestamp;
maci.save();
}
}
}

export function handleMergeMessageAq(event: MergeMessageAqEvent): void {
Expand Down
1 change: 0 additions & 1 deletion subgraph/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { BigInt as GraphBN } from "@graphprotocol/graph-ts";

export const DEFAULT_MACI_ID = "maci";
export const ONE_BIG_INT = GraphBN.fromU32(1);
6 changes: 2 additions & 4 deletions subgraph/src/utils/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { BigInt as GraphBN, Bytes, ethereum } from "@graphprotocol/graph-ts";

import { Account, MACI, User } from "../../generated/schema";

import { DEFAULT_MACI_ID } from "./constants";

export const createOrLoadMACI = (event: ethereum.Event, stateTreeDepth: GraphBN = GraphBN.fromI32(10)): MACI => {
let maci = MACI.load(DEFAULT_MACI_ID);
let maci = MACI.load(event.address);

if (!maci) {
maci = new MACI(DEFAULT_MACI_ID);
maci = new MACI(event.address);
maci.stateTreeDepth = stateTreeDepth;
maci.updatedAt = event.block.timestamp;
maci.numPoll = GraphBN.zero();
Expand Down
31 changes: 18 additions & 13 deletions subgraph/tests/maci/maci.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { test, describe, afterEach, clearStore, assert, beforeAll } from "matchs

import { Account, MACI, Poll, User } from "../../generated/schema";
import { handleSignUp, handleDeployPoll } from "../../src/maci";
import { DEFAULT_MACI_ID } from "../../src/utils/constants";
import {
DEFAULT_MESSAGE_PROCESSOR_ADDRESS,
DEFAULT_POLL_ADDRESS,
Expand Down Expand Up @@ -37,16 +36,18 @@ describe("MACI", () => {
handleSignUp(event);

const userId = `${event.params._userPubKeyX.toString()} ${event.params._userPubKeyY.toString()}`;
const maciAddress = event.address;
const user = User.load(userId)!;
const account = Account.load(event.params._stateIndex.toString())!;
const maci = MACI.load(DEFAULT_MACI_ID)!;
const maci = MACI.load(maciAddress)!;
const poll = Poll.load(maci.latestPoll);

assert.fieldEquals("User", user.id, "id", userId);
assert.fieldEquals("Account", account.id, "id", event.params._stateIndex.toString());
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "numPoll", "0");
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "numSignUps", "1");
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "latestPoll", "0x00000000");
assert.fieldEquals("MACI", maciAddress.toHexString(), "numPoll", "0");
assert.fieldEquals("MACI", maciAddress.toHexString(), "numSignUps", "1");
assert.fieldEquals("MACI", maciAddress.toHexString(), "latestPoll", "0x00000000");
assert.assertTrue(maci.polls.load().length === 0);
assert.assertNull(poll);
});

Expand All @@ -62,13 +63,15 @@ describe("MACI", () => {

handleDeployPoll(event);

const maci = MACI.load(DEFAULT_MACI_ID)!;
const maciAddress = event.address;
const maci = MACI.load(maciAddress)!;
const poll = Poll.load(maci.latestPoll)!;

assert.fieldEquals("Poll", poll.id.toHex(), "id", DEFAULT_POLL_ADDRESS.toHexString());
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "numPoll", "1");
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "numSignUps", "0");
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "latestPoll", poll.id.toHex());
assert.fieldEquals("MACI", maciAddress.toHexString(), "numPoll", "1");
assert.fieldEquals("MACI", maciAddress.toHexString(), "numSignUps", "0");
assert.fieldEquals("MACI", maciAddress.toHexString(), "latestPoll", poll.id.toHex());
assert.assertTrue(maci.polls.load().length === 1);
});

test("should handle signup with deployed poll properly", () => {
Expand All @@ -93,16 +96,18 @@ describe("MACI", () => {
handleSignUp(signUpEvent);

const userId = `${signUpEvent.params._userPubKeyX.toString()} ${signUpEvent.params._userPubKeyY.toString()}`;
const maciAddress = deployPollEvent.address;
const user = User.load(userId)!;
const account = Account.load(signUpEvent.params._stateIndex.toString())!;
const maci = MACI.load(DEFAULT_MACI_ID)!;
const maci = MACI.load(maciAddress)!;
const poll = Poll.load(maci.latestPoll)!;

assert.fieldEquals("User", user.id, "id", userId);
assert.fieldEquals("Account", account.id, "id", signUpEvent.params._stateIndex.toString());
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "numPoll", "1");
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "numSignUps", "1");
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "latestPoll", poll.id.toHex());
assert.fieldEquals("MACI", maciAddress.toHexString(), "numPoll", "1");
assert.fieldEquals("MACI", maciAddress.toHexString(), "numSignUps", "1");
assert.fieldEquals("MACI", maciAddress.toHexString(), "latestPoll", poll.id.toHex());
assert.assertTrue(maci.polls.load().length === 1);
assert.assertNotNull(poll);
});
});
16 changes: 8 additions & 8 deletions subgraph/tests/poll/poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
handleMergeMessageAqSubRoots,
handlePublishMessage,
} from "../../src/poll";
import { DEFAULT_MACI_ID } from "../../src/utils/constants";
import {
DEFAULT_MESSAGE_PROCESSOR_ADDRESS,
DEFAULT_POLL_ADDRESS,
Expand Down Expand Up @@ -53,14 +52,15 @@ describe("Poll", () => {

handleMergeMaciState(event);

const maci = MACI.load(DEFAULT_MACI_ID)!;
const poll = Poll.load(maci.latestPoll)!;
const poll = Poll.load(event.address)!;
const maci = MACI.load(poll.maci)!;

assert.fieldEquals("Poll", poll.id.toHex(), "stateRoot", "1");
assert.fieldEquals("Poll", poll.id.toHex(), "numSignups", "3");
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "numPoll", "1");
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "numSignUps", "3");
assert.fieldEquals("MACI", DEFAULT_MACI_ID, "latestPoll", poll.id.toHex());
assert.fieldEquals("MACI", maci.id.toHexString(), "numPoll", "1");
assert.fieldEquals("MACI", maci.id.toHexString(), "numSignUps", "3");
assert.fieldEquals("MACI", maci.id.toHexString(), "latestPoll", poll.id.toHex());
assert.assertTrue(maci.polls.load().length === 1);
});

test("should handle merge message queue properly", () => {
Expand All @@ -78,7 +78,7 @@ describe("Poll", () => {

handleMergeMessageAqSubRoots(event);

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

assert.fieldEquals("Poll", poll.id.toHex(), "numSrQueueOps", "1");
});
Expand All @@ -104,7 +104,7 @@ describe("Poll", () => {

handlePublishMessage(event);

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

assert.entityCount("Vote", 1);
assert.fieldEquals("Poll", poll.id.toHex(), "numMessages", "1");
Expand Down
Loading