From ceebd3a98424267fa92fedd7b7cd336416bdecb1 Mon Sep 17 00:00:00 2001 From: Anton <14254374+0xmad@users.noreply.github.com> Date: Fri, 14 Jun 2024 00:06:43 -0500 Subject: [PATCH] feat(subgraph): optimize subgraph schema (maci) (#1564) - [x] Add polls relation for maci and poll - [x] Use maci address as entity id --- subgraph/schemas/schema.v1.graphql | 6 +++++- subgraph/src/maci.ts | 1 + subgraph/src/poll.ts | 16 ++++++++------- subgraph/src/utils/constants.ts | 1 - subgraph/src/utils/entity.ts | 6 ++---- subgraph/tests/maci/maci.test.ts | 31 +++++++++++++++++------------- subgraph/tests/poll/poll.test.ts | 16 +++++++-------- 7 files changed, 43 insertions(+), 34 deletions(-) diff --git a/subgraph/schemas/schema.v1.graphql b/subgraph/schemas/schema.v1.graphql index 462d591943..a48de57f5e 100644 --- a/subgraph/schemas/schema.v1.graphql +++ b/subgraph/schemas/schema.v1.graphql @@ -1,5 +1,5 @@ type MACI @entity { - id: ID! + id: Bytes! # address stateTreeDepth: BigInt! # uint8 updatedAt: BigInt! @@ -7,6 +7,9 @@ type MACI @entity { numSignUps: BigInt! numPoll: BigInt! latestPoll: Bytes! + + "relations" + polls: [Poll!]! @derivedFrom(field: "maci") } type User @entity(immutable: true) { @@ -46,6 +49,7 @@ type Poll @entity { "relations" owner: Bytes! + maci: MACI! votes: [Vote!]! @derivedFrom(field: "poll") } diff --git a/subgraph/src/maci.ts b/subgraph/src/maci.ts index 0c5633937b..7d1fe278f8 100644 --- a/subgraph/src/maci.ts +++ b/subgraph/src/maci.ts @@ -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); diff --git a/subgraph/src/poll.ts b/subgraph/src/poll.ts index f768761aff..7c9065d023 100644 --- a/subgraph/src/poll.ts +++ b/subgraph/src/poll.ts @@ -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, @@ -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); @@ -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 { diff --git a/subgraph/src/utils/constants.ts b/subgraph/src/utils/constants.ts index a5fbcbf5e4..cdd436764c 100644 --- a/subgraph/src/utils/constants.ts +++ b/subgraph/src/utils/constants.ts @@ -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); diff --git a/subgraph/src/utils/entity.ts b/subgraph/src/utils/entity.ts index 2e2d60d3b6..201024b859 100644 --- a/subgraph/src/utils/entity.ts +++ b/subgraph/src/utils/entity.ts @@ -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(); diff --git a/subgraph/tests/maci/maci.test.ts b/subgraph/tests/maci/maci.test.ts index 05d826a630..a06ba22ce6 100644 --- a/subgraph/tests/maci/maci.test.ts +++ b/subgraph/tests/maci/maci.test.ts @@ -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, @@ -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); }); @@ -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", () => { @@ -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); }); }); diff --git a/subgraph/tests/poll/poll.test.ts b/subgraph/tests/poll/poll.test.ts index 8210af091f..de70756dba 100644 --- a/subgraph/tests/poll/poll.test.ts +++ b/subgraph/tests/poll/poll.test.ts @@ -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, @@ -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", () => { @@ -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"); }); @@ -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");