Skip to content

Commit 2382582

Browse files
committed
chore: optimizations
1 parent 1b015b8 commit 2382582

File tree

2 files changed

+17
-65
lines changed

2 files changed

+17
-65
lines changed

subgraph/core/src/entities/Court.ts

+10-44
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,25 @@ import { ZERO } from "../utils";
55

66
// This function calculates the "effective" stake, which is the specific stake
77
// of the current court + the specific stake of all of its children courts
8-
export function updateEffectiveStake(courtID: string): void {
8+
export function updateEffectiveStake(courtID: string, delta: BigInt): void {
99
let court = Court.load(courtID);
1010
if (!court) return;
11-
12-
while (court) {
13-
let totalStake = court.stake;
14-
15-
const childrenCourts = court.children.load();
16-
17-
for (let i = 0; i < childrenCourts.length; i++) {
18-
const childCourt = Court.load(childrenCourts[i].id);
19-
if (childCourt) {
20-
totalStake = totalStake.plus(childCourt.effectiveStake);
21-
}
22-
}
23-
24-
court.effectiveStake = totalStake;
25-
court.save();
26-
27-
if (court.parent && court.parent !== null) {
28-
court = Court.load(court.parent as string);
29-
} else {
30-
break;
31-
}
11+
court.effectiveStake = court.effectiveStake.plus(delta);
12+
court.save();
13+
if (court.parent) {
14+
updateEffectiveStake(court.parent as string, delta);
3215
}
3316
}
3417

3518
// This function calculates the "effective" numberStakedJurors, which is the specific numberStakedJurors
3619
// of the current court + the specific numberStakedJurors of all of its children courts
37-
export function updateEffectiveNumberStakedJurors(courtID: string): void {
20+
export function updateEffectiveNumberStakedJurors(courtID: string, delta: BigInt): void {
3821
let court = Court.load(courtID);
3922
if (!court) return;
40-
41-
while (court) {
42-
let totalJurors = court.numberStakedJurors;
43-
44-
const childrenCourts = court.children.load();
45-
46-
for (let i = 0; i < childrenCourts.length; i++) {
47-
const childCourt = Court.load(childrenCourts[i].id);
48-
if (childCourt) {
49-
totalJurors = totalJurors.plus(childCourt.effectiveNumberStakedJurors);
50-
}
51-
}
52-
53-
court.effectiveNumberStakedJurors = totalJurors;
54-
court.save();
55-
56-
if (court.parent && court.parent !== null) {
57-
court = Court.load(court.parent as string);
58-
} else {
59-
break;
60-
}
23+
court.effectiveNumberStakedJurors = court.effectiveNumberStakedJurors.plus(delta);
24+
court.save();
25+
if (court.parent) {
26+
updateEffectiveNumberStakedJurors(court.parent as string, delta);
6127
}
6228
}
6329

subgraph/core/src/entities/JurorTokensPerCourt.ts

+7-21
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,16 @@ export function createJurorTokensPerCourt(jurorAddress: string, courtID: string)
3232
return jurorTokens;
3333
}
3434

35-
export function updateJurorEffectiveStake(jurorAddress: string, courtID: string): void {
35+
export function updateJurorEffectiveStake(jurorAddress: string, courtID: string, delta: BigInt): void {
3636
let court = Court.load(courtID);
37-
if (!court) {
38-
return;
39-
}
37+
if (!court) return;
4038

4139
while (court) {
4240
const jurorTokensPerCourt = ensureJurorTokensPerCourt(jurorAddress, court.id);
43-
let totalStake = jurorTokensPerCourt.staked;
44-
const childrenCourts = court.children.load();
45-
46-
for (let i = 0; i < childrenCourts.length; i++) {
47-
const childCourtID = childrenCourts[i].id;
48-
const childCourt = Court.load(childCourtID);
49-
if (childCourt) {
50-
const childJurorTokensPerCourt = ensureJurorTokensPerCourt(jurorAddress, childCourt.id);
51-
totalStake = totalStake.plus(childJurorTokensPerCourt.effectiveStake);
52-
}
53-
}
54-
55-
jurorTokensPerCourt.effectiveStake = totalStake;
41+
jurorTokensPerCourt.effectiveStake = jurorTokensPerCourt.effectiveStake.plus(delta);
5642
jurorTokensPerCourt.save();
5743

58-
if (court.parent && court.parent !== null) {
44+
if (court.parent) {
5945
court = Court.load(court.parent as string);
6046
} else {
6147
break;
@@ -92,9 +78,9 @@ export function updateJurorStake(
9278
updateActiveJurors(activeJurorsDelta, timestamp);
9379
juror.save();
9480
court.save();
95-
updateEffectiveStake(courtID);
96-
updateEffectiveNumberStakedJurors(courtID);
97-
updateJurorEffectiveStake(jurorAddress, courtID);
81+
updateEffectiveStake(courtID, stakeDelta);
82+
updateJurorEffectiveStake(jurorAddress, courtID, stakeDelta);
83+
updateEffectiveNumberStakedJurors(courtID, stakedJurorsDelta);
9884
updateCourtStateVariable(courtID, court.effectiveStake, timestamp, "effectiveStake");
9985
}
10086

0 commit comments

Comments
 (0)