Skip to content

Commit

Permalink
Merge pull request #1556 from privacy-scaling-explorations/feature/op…
Browse files Browse the repository at this point in the history
…timize-subgraph

feat(subgraph): optimize subgraph schema
  • Loading branch information
0xmad authored Jun 13, 2024
2 parents 01dd81b + 44094f4 commit 674c815
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 62 deletions.
2 changes: 2 additions & 0 deletions subgraph/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ subgraph.yaml
schema.graphql
tests/.bin/
tests/.latest.json
config/*.json
!config/[network].json

4 changes: 2 additions & 2 deletions subgraph/config/optimism-sepolia.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"network": "optimism-sepolia",
"maciContractAddress": "0xD18Ca45b6cC1f409380731C40551BD66932046c3",
"maciContractStartBlock": 11052407
"maciContractAddress": "0xbE6e250bf8B5F689c65Cc79667589A9EBF6Fe8E3",
"maciContractStartBlock": 13160483
}
31 changes: 10 additions & 21 deletions subgraph/schemas/schema.v1.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ type MACI @entity {
id: ID!
stateTreeDepth: BigInt! # uint8
updatedAt: BigInt!
blockNumber: BigInt!
txHash: Bytes!

"state"
numSignUps: BigInt!
Expand All @@ -13,45 +11,38 @@ type MACI @entity {

type User @entity(immutable: true) {
id: ID! # pubkey
timestamp: BigInt! # uint256
blockNumber: BigInt!
txHash: Bytes!

createdAt: BigInt! # uint256
"relations"
accounts: [Account!]! @derivedFrom(field: "owner")
}

type Account @entity {
id: ID! # stateIndex
voiceCreditBalance: BigInt! # uint256
timestamp: BigInt! # uint256
blockNumber: BigInt!
txHash: Bytes!

createdAt: BigInt! # uint256
"relations"
owner: User!
}

type Poll @entity {
id: Bytes! # poll address
pollId: BigInt # uint256
pollId: BigInt! # uint256
duration: BigInt! # uint256
treeDepth: BigInt! # uint8
maxMessages: BigInt
maxVoteOption: BigInt
messageProcessor: Bytes # address
tally: Bytes # address
maxMessages: BigInt!
maxVoteOption: BigInt!
messageProcessor: Bytes! # address
tally: Bytes! # address
createdAt: BigInt!
updatedAt: BigInt!

"merge state after ended"
stateRoot: BigInt # uint256
numSignups: BigInt! # uint256
numMessages: BigInt! # uint256
"merge message tree after ended"
numSrQueueOps: BigInt # uint256
messageRoot: BigInt
createdAt: BigInt! # startedAt
updatedAt: BigInt!
blockNumber: BigInt!
txHash: Bytes!

"relations"
owner: Bytes!
Expand All @@ -62,8 +53,6 @@ type Vote @entity(immutable: true) {
id: Bytes!
data: [BigInt!]! # uint256[10]
timestamp: BigInt!
blockNumber: BigInt!
txHash: Bytes!

"relations"
poll: Poll!
Expand Down
34 changes: 16 additions & 18 deletions subgraph/src/maci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,36 @@ import { createOrLoadMACI, createOrLoadUser, createOrLoadAccount } from "./utils
export function handleDeployPoll(event: DeployPollEvent): void {
const maci = createOrLoadMACI(event);

const entity = new Poll(event.params.pollAddr.poll);
const poll = new Poll(event.params.pollAddr.poll);
const contract = PollContract.bind(event.params.pollAddr.poll);
const maxValues = contract.maxValues();
const treeDepths = contract.treeDepths();
const durations = contract.getDeployTimeAndDuration();

entity.pollId = event.params._pollId;
entity.messageProcessor = event.params.pollAddr.messageProcessor;
entity.tally = event.params.pollAddr.tally;
entity.maxMessages = maxValues.value0;
entity.maxVoteOption = maxValues.value1;
entity.treeDepth = GraphBN.fromI32(treeDepths.value0);
entity.duration = durations.value1;
poll.pollId = event.params._pollId;
poll.messageProcessor = event.params.pollAddr.messageProcessor;
poll.tally = event.params.pollAddr.tally;
poll.maxMessages = maxValues.value0;
poll.maxVoteOption = maxValues.value1;
poll.treeDepth = GraphBN.fromI32(treeDepths.value0);
poll.duration = durations.value1;

entity.blockNumber = event.block.number;
entity.createdAt = event.block.timestamp;
entity.updatedAt = event.block.timestamp;
entity.txHash = event.transaction.hash;
entity.owner = event.transaction.from;
poll.createdAt = event.block.timestamp;
poll.updatedAt = event.block.timestamp;
poll.owner = event.transaction.from;

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

maci.numPoll = maci.numPoll.plus(ONE_BIG_INT);
maci.latestPoll = entity.id;
maci.latestPoll = poll.id;
maci.updatedAt = event.block.timestamp;
maci.save();

// Start indexing the poll; `event.params.pollAddr.poll` is the
// address of the new poll contract
PollTemplate.create(Address.fromBytes(entity.id));
PollTemplate.create(Address.fromBytes(poll.id));
}

export function handleSignUp(event: SignUpEvent): void {
Expand Down
12 changes: 0 additions & 12 deletions subgraph/src/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export function handleMergeMaciState(event: MergeMaciStateEvent): void {
if (poll) {
poll.stateRoot = event.params._stateRoot;
poll.numSignups = event.params._numSignups;

poll.blockNumber = event.block.number;
poll.updatedAt = event.block.timestamp;
poll.txHash = event.transaction.hash;
poll.save();
}

Expand All @@ -35,10 +32,7 @@ export function handleMergeMessageAq(event: MergeMessageAqEvent): void {

if (poll) {
poll.messageRoot = event.params._messageRoot;

poll.blockNumber = event.block.number;
poll.updatedAt = event.block.timestamp;
poll.txHash = event.transaction.hash;
poll.save();
}
}
Expand All @@ -48,10 +42,7 @@ export function handleMergeMessageAqSubRoots(event: MergeMessageAqSubRootsEvent)

if (poll) {
poll.numSrQueueOps = event.params._numSrQueueOps;

poll.blockNumber = event.block.number;
poll.updatedAt = event.block.timestamp;
poll.txHash = event.transaction.hash;
poll.save();
}
}
Expand All @@ -60,10 +51,7 @@ export function handlePublishMessage(event: PublishMessageEvent): void {
const vote = new Vote(event.transaction.hash.concatI32(event.logIndex.toI32()));
vote.data = event.params._message.data;
vote.poll = event.address;

vote.blockNumber = event.block.number;
vote.timestamp = event.block.timestamp;
vote.txHash = event.transaction.hash;
vote.save();

const poll = Poll.load(event.address);
Expand Down
11 changes: 2 additions & 9 deletions subgraph/src/utils/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ export const createOrLoadMACI = (event: ethereum.Event, stateTreeDepth: GraphBN
maci = new MACI(DEFAULT_MACI_ID);
maci.stateTreeDepth = stateTreeDepth;
maci.updatedAt = event.block.timestamp;
maci.blockNumber = event.block.number;
maci.txHash = event.transaction.hash;

maci.numPoll = GraphBN.zero();
maci.numSignUps = GraphBN.zero();
maci.latestPoll = Bytes.empty();
Expand All @@ -30,9 +27,7 @@ export const createOrLoadUser = (publicKeyX: GraphBN, publicKeyY: GraphBN, event

if (!user) {
user = new User(publicKey);
user.timestamp = event.block.timestamp;
user.blockNumber = event.block.number;
user.txHash = event.transaction.hash;
user.createdAt = event.block.timestamp;
user.save();
}

Expand All @@ -52,9 +47,7 @@ export const createOrLoadAccount = (
account = new Account(id);
account.owner = owner;
account.voiceCreditBalance = voiceCreditBalance;
account.timestamp = event.block.timestamp;
account.blockNumber = event.block.number;
account.txHash = event.transaction.hash;
account.createdAt = event.block.timestamp;
account.save();
}

Expand Down

0 comments on commit 674c815

Please sign in to comment.