Skip to content

Commit 4128c77

Browse files
committed
chore: evidence-and-withdrawal-event-updates
1 parent a3c9f91 commit 4128c77

File tree

8 files changed

+75
-90
lines changed

8 files changed

+75
-90
lines changed

subgraph/core/schema.graphql

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,11 @@ interface Contribution {
4242
contributor: User!
4343
}
4444

45-
interface EvidenceGroup {
46-
id: ID!
47-
evidences: [Evidence!]! @derivedFrom(field: "evidenceGroup")
48-
nextEvidenceIndex: BigInt!
49-
}
50-
5145
interface Evidence {
5246
id: ID!
5347
evidence: String!
54-
evidenceGroup: EvidenceGroup!
55-
evidenceIndex: String!
48+
evidenceIndex: BigInt!
49+
dispute: Dispute!
5650
sender: User!
5751
senderAddress: String!
5852
timestamp: BigInt!
@@ -63,7 +57,6 @@ interface Evidence {
6357
fileTypeExtension: String
6458
}
6559

66-
6760
############
6861
# Entities #
6962
############
@@ -88,7 +81,7 @@ type User @entity {
8881
coherenceScore: BigInt!
8982
votes: [Vote!]! @derivedFrom(field: "juror")
9083
contributions: [Contribution!]! @derivedFrom(field: "contributor")
91-
evidences: [Evidence!]! @derivedFrom(field: "sender")
84+
evidences: [ClassicEvidence!]! @derivedFrom(field: "sender")
9285
penalties: [Penalty!]! @derivedFrom(field: "juror")
9386
}
9487

@@ -182,11 +175,14 @@ type Dispute @entity {
182175
shifts: [JurorRewardPenalty!]! @derivedFrom(field: "dispute")
183176
disputeKitDispute: [DisputeKitDispute!]! @derivedFrom(field: "coreDispute")
184177
isCrossChain: Boolean
185-
arbitrableChainId:BigInt
186-
externalDisputeId:BigInt
187-
templateId:BigInt
188-
rulingTimestamp:BigInt
189-
rulingTransactionHash:String
178+
arbitrableChainId: BigInt
179+
externalDisputeId: BigInt
180+
templateId: BigInt
181+
rulingTimestamp: BigInt
182+
rulingTransactionHash: String
183+
# required to keep evidence unique
184+
evidenceCount: BigInt!
185+
evidences: [ClassicEvidence!]! @derivedFrom(field: "dispute")
190186
}
191187

192188
type PeriodIndexCounter @entity {
@@ -285,7 +281,7 @@ type Answer @entity {
285281
count: BigInt!
286282
paidFee: BigInt!
287283
funded: Boolean!
288-
localRound: ClassicRound!
284+
localRound: ClassicRound!
289285
}
290286

291287
type ClassicRound implements DisputeKitRound @entity {
@@ -332,17 +328,11 @@ type ClassicJustification @entity {
332328
timestamp: BigInt!
333329
}
334330

335-
type ClassicEvidenceGroup implements EvidenceGroup @entity {
336-
id: ID!
337-
evidences: [Evidence!]! @derivedFrom(field: "evidenceGroup")
338-
nextEvidenceIndex: BigInt!
339-
}
340-
341331
type ClassicEvidence implements Evidence @entity(immutable: true) {
342332
id: ID! # classicEvidenceGroup.id-nextEvidenceIndex
343333
evidence: String!
344-
evidenceGroup: EvidenceGroup!
345-
evidenceIndex: String!
334+
evidenceIndex: BigInt!
335+
dispute: Dispute!
346336
sender: User!
347337
senderAddress: String!
348338
timestamp: BigInt!
@@ -370,5 +360,10 @@ type _Schema_
370360
name: "evidenceSearch"
371361
language: en
372362
algorithm: rank
373-
include: [{ entity: "ClassicEvidence", fields: [{ name: "name" }, { name: "description" },{ name: "senderAddress"},{ name: "evidenceIndex"}] }]
374-
)
363+
include: [
364+
{
365+
entity: "ClassicEvidence"
366+
fields: [{ name: "name" }, { name: "description" }, { name: "senderAddress" }, { name: "evidenceIndex" }]
367+
}
368+
]
369+
)

subgraph/core/src/DisputeKitClassic.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export function handleVoteCast(event: VoteCast): void {
113113
}
114114

115115
export function handleContributionEvent(event: ContributionEvent): void {
116-
ensureClassicContributionFromEvent(event);
116+
ensureClassicContributionFromEvent(event, event.params._coreRoundID);
117117
updateChoiceFundingFromContributionEvent(event);
118118
}
119119

@@ -174,36 +174,37 @@ export function handleChoiceFunded(event: ChoiceFunded): void {
174174
}
175175

176176
export function handleWithdrawal(event: Withdrawal): void {
177-
const contribution = ensureClassicContributionFromEvent(event);
178-
if (!contribution) return;
179-
contribution.rewardWithdrawn = true;
180-
contribution.rewardAmount = event.params._amount;
181-
182177
// check if all appeal fees have been withdrawn
183178
const coreDisputeID = event.params._coreDisputeID.toString();
184-
185-
// TODO: handle the removal of _coreRoundID from the event
186-
const coreRoundIndex = 0; // event.params._coreRoundID.toString();
187-
188179
const coreDispute = Dispute.load(coreDisputeID);
189180
if (!coreDispute) return;
190181

191-
const roundId = `${coreDisputeID}-${coreRoundIndex}`;
192-
const coreRound = Round.load(roundId);
193-
if (!coreRound) return;
194-
const disputeKitID = coreRound.disputeKit;
182+
const numberOfRounds = coreDispute.currentRoundIndex.toI32();
183+
for (let i = 0; i < numberOfRounds; i++) {
184+
const coreRoundIndex = i;
195185

196-
const roundID = `${disputeKitID}-${coreDisputeID}-${coreRoundIndex}`;
186+
const contribution = ensureClassicContributionFromEvent(event, new BigInt(coreRoundIndex));
187+
if (!contribution) return;
188+
contribution.rewardWithdrawn = true;
189+
contribution.rewardAmount = event.params._amount;
197190

198-
const localRound = ClassicRound.load(roundID);
199-
if (!localRound) return;
191+
const roundId = `${coreDisputeID}-${coreRoundIndex}`;
192+
const coreRound = Round.load(roundId);
193+
if (!coreRound) return;
194+
const disputeKitID = coreRound.disputeKit;
200195

201-
localRound.totalFeeDispersed = localRound.totalFeeDispersed.plus(event.params._amount);
196+
const roundID = `${disputeKitID}-${coreDisputeID}-${coreRoundIndex}`;
202197

203-
if (localRound.totalFeeDispersed.equals(localRound.feeRewards)) {
204-
localRound.appealFeesDispersed = true;
205-
}
198+
const localRound = ClassicRound.load(roundID);
199+
if (!localRound) return;
206200

207-
contribution.save();
208-
localRound.save();
201+
localRound.totalFeeDispersed = localRound.totalFeeDispersed.plus(event.params._amount);
202+
203+
if (localRound.totalFeeDispersed.equals(localRound.feeRewards)) {
204+
localRound.appealFeesDispersed = true;
205+
}
206+
207+
contribution.save();
208+
localRound.save();
209+
}
209210
}

subgraph/core/src/EvidenceModule.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
import { json, JSONValueKind, log } from "@graphprotocol/graph-ts";
22
import { Evidence as EvidenceEvent } from "../generated/EvidenceModule/EvidenceModule";
3-
import { ClassicEvidence } from "../generated/schema";
4-
import { ensureClassicEvidenceGroup } from "./entities/ClassicEvidenceGroup";
3+
import { ClassicEvidence, Dispute } from "../generated/schema";
54
import { ensureUser } from "./entities/User";
65
import { ONE } from "./utils";
76
import { JSONValueToMaybeString } from "../../utils";
87

98
export function handleEvidenceEvent(event: EvidenceEvent): void {
10-
// TODO: handle the replacement of _externalDisputeID with _arbitratorDisputeID
11-
// TODO: no more evidenceGroupID
12-
const evidenceGroupID = event.params._arbitratorDisputeID.toString();
9+
const coreDisputeID = event.params._arbitratorDisputeID;
10+
const dispute = Dispute.load(coreDisputeID.toString());
11+
12+
if (!dispute) {
13+
log.error(`EvidenceEvent: Dispute not found for id: {}`, [coreDisputeID.toString()]);
14+
return;
15+
}
16+
17+
dispute.evidenceCount = dispute.evidenceCount.plus(ONE);
18+
dispute.save();
19+
20+
const numberOfEvidences = dispute.evidenceCount;
21+
const evidenceId = `${coreDisputeID}-${numberOfEvidences.toString()}`;
1322

14-
const evidenceGroup = ensureClassicEvidenceGroup(evidenceGroupID);
15-
const evidenceIndex = evidenceGroup.nextEvidenceIndex;
16-
evidenceGroup.nextEvidenceIndex = evidenceGroup.nextEvidenceIndex.plus(ONE);
17-
evidenceGroup.save();
18-
const evidenceId = `${evidenceGroupID}-${evidenceIndex.toString()}`;
1923
const evidence = new ClassicEvidence(evidenceId);
20-
evidence.evidenceIndex = evidenceIndex.plus(ONE).toString();
2124
const userId = event.params._party.toHexString();
25+
2226
evidence.timestamp = event.block.timestamp;
2327
evidence.transactionHash = event.transaction.hash;
2428
evidence.evidence = event.params._evidence;
25-
evidence.evidenceGroup = evidenceGroupID.toString();
2629
evidence.sender = userId;
2730
evidence.senderAddress = userId;
31+
evidence.evidenceIndex = numberOfEvidences;
2832
ensureUser(userId);
2933

3034
let jsonObjValueAndSuccess = json.try_fromString(event.params._evidence);

subgraph/core/src/entities/ClassicContribution.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { ClassicContribution, Dispute, Round } from "../../generated/schema";
22
import { Contribution as ContributionEvent, Withdrawal } from "../../generated/DisputeKitClassic/DisputeKitClassic";
33
import { ensureUser } from "./User";
4+
import { BigInt } from "@graphprotocol/graph-ts";
45

5-
export function ensureClassicContributionFromEvent<T>(event: T): ClassicContribution | null {
6+
export function ensureClassicContributionFromEvent<T>(event: T, coreRoundIndex: BigInt): ClassicContribution | null {
67
if (!(event instanceof ContributionEvent) && !(event instanceof Withdrawal)) return null;
78
const coreDisputeID = event.params._coreDisputeID.toString();
89

9-
// TODO: handle the removal of _coreRoundID from the event
10-
const coreRoundIndex = 0; // event.params._coreRoundID.toString();
11-
1210
const coreDispute = Dispute.load(coreDisputeID);
1311
if (!coreDispute) return null;
1412

subgraph/core/src/entities/ClassicEvidenceGroup.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

subgraph/core/src/entities/Dispute.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function createDisputeFromEvent(event: DisputeCreation): void {
2222
dispute.lastPeriodChangeBlockNumber = event.block.number;
2323
dispute.periodNotificationIndex = getAndIncrementPeriodCounter(dispute.period);
2424
dispute.transactionHash = event.transaction.hash.toHexString();
25+
dispute.evidenceCount = ZERO;
2526
const court = Court.load(courtID);
2627
if (!court) return;
2728
dispute.periodDeadline = event.block.timestamp.plus(court.timesPerPeriod[0]);

web/src/hooks/queries/useEvidences.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,48 @@ export const evidenceFragment = graphql(`
1515
sender {
1616
id
1717
}
18+
evidenceIndex
1819
timestamp
1920
transactionHash
2021
name
2122
description
2223
fileURI
2324
fileTypeExtension
24-
evidenceIndex
2525
}
2626
`);
2727

2828
const evidencesQuery = graphql(`
29-
query Evidences($evidenceGroupID: String) {
30-
evidences(where: { evidenceGroup: $evidenceGroupID }, orderBy: timestamp, orderDirection: asc) {
29+
query Evidences($disputeID: String) {
30+
evidences(where: { dispute: $disputeID }, orderBy: timestamp, orderDirection: asc) {
3131
...EvidenceDetails
3232
}
3333
}
3434
`);
3535

3636
const evidenceSearchQuery = graphql(`
37-
query EvidenceSearch($keywords: String!, $evidenceGroupID: String) {
38-
evidenceSearch(text: $keywords, where: { evidenceGroup: $evidenceGroupID }) {
37+
query EvidenceSearch($keywords: String!, $disputeID: String) {
38+
evidenceSearch(text: $keywords, where: { dispute: $disputeID }) {
3939
...EvidenceDetails
4040
}
4141
}
4242
`);
4343

44-
export const useEvidences = (evidenceGroup?: string, keywords?: string) => {
45-
const isEnabled = evidenceGroup !== undefined;
44+
export const useEvidences = (disputeID?: string, keywords?: string) => {
45+
const isEnabled = disputeID !== undefined;
4646
const { graphqlBatcher } = useGraphqlBatcher();
4747

4848
const document = keywords ? evidenceSearchQuery : evidencesQuery;
4949
const transformedKeywords = transformSearch(keywords);
5050

5151
return useQuery<{ evidences: EvidenceDetailsFragment[] }>({
52-
queryKey: [keywords ? `evidenceSearchQuery${evidenceGroup}-${keywords}` : `evidencesQuery${evidenceGroup}`],
52+
queryKey: keywords ? [`EvidenceSearchQuery`, disputeID, keywords] : [`EvidencesQuery`, disputeID],
5353
enabled: isEnabled,
5454
refetchInterval: REFETCH_INTERVAL,
5555
queryFn: async () => {
5656
const result = await graphqlBatcher.fetch({
5757
id: crypto.randomUUID(),
5858
document: document,
59-
variables: { evidenceGroupID: evidenceGroup?.toString(), keywords: transformedKeywords },
59+
variables: { disputeID, keywords: transformedKeywords },
6060
});
6161

6262
return keywords ? { evidences: [...result.evidenceSearch] } : result;

web/src/pages/Cases/CaseDetails/Evidence/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ const Evidence: React.FC = () => {
8585
const [search, setSearch] = useState<string>();
8686
const [debouncedSearch, setDebouncedSearch] = useState<string>();
8787
const [showSpam, setShowSpam] = useState(false);
88-
const { data: spamEvidences } = useSpamEvidence(disputeData?.dispute?.externalDisputeId?.toString());
88+
const { data: spamEvidences } = useSpamEvidence(id!);
8989

90-
const { data } = useEvidences(disputeData?.dispute?.externalDisputeId?.toString(), debouncedSearch);
90+
const { data } = useEvidences(id!, debouncedSearch);
9191

9292
useDebounce(() => setDebouncedSearch(search), 500, [search]);
9393

0 commit comments

Comments
 (0)