Skip to content

Commit 24f32dc

Browse files
committed
perf: improved blocks and events queries; added regression tests to help on development
1 parent c159c2a commit 24f32dc

File tree

8 files changed

+988
-57
lines changed

8 files changed

+988
-57
lines changed

indexer/jest.config.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
export default {
2-
preset: 'ts-jest',
3-
testEnvironment: 'node',
2+
preset: 'ts-jest/presets/default-esm',
43
transform: {
5-
'^.+\\.ts$': 'ts-jest',
4+
'^.+\\.tsx?$': [
5+
'ts-jest',
6+
{
7+
useESM: true,
8+
},
9+
],
610
},
711
extensionsToTreatAsEsm: ['.ts'],
812
moduleNameMapper: {

indexer/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"express": "^4.18.2",
2727
"graphql": "^16.10.0",
2828
"graphql-query-complexity": "^1.0.0",
29+
"graphql-request": "5.0.0",
2930
"graphql-scalars": "^1.23.0",
3031
"graphql-subscriptions": "^2.0.0",
3132
"graphql-tag": "^2.12.6",
@@ -76,6 +77,7 @@
7677
"graphql:generate-types": "npx graphql-codegen",
7778
"migrate:up": "dotenv -e .env npx sequelize-cli db:migrate",
7879
"migrate:down": "dotenv -e .env npx sequelize-cli db:migrate:undo",
79-
"test": "jest tests/unit/*.test.ts"
80+
"test:unit": "jest tests/unit/*.test.ts",
81+
"test:integration": "jest tests/integration/*.test.ts"
8082
}
8183
}

indexer/src/kadena-server/repository/infra/repository/block-db-repository.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ export default class BlockDbRepository implements BlockRepository {
103103

104104
if (chainIds?.length) {
105105
queryParams.push(chainIds);
106-
conditions += `\nAND b."chainId" = $${queryParams.length}`;
106+
conditions += `\nAND b."chainId" = ANY($${queryParams.length})`;
107107
}
108108

109-
if (endHeight) {
109+
if (endHeight && endHeight - startHeight < 20) {
110110
queryParams.push(endHeight);
111111
conditions += `\nAND b."height" <= $${queryParams.length}`;
112112
}
@@ -128,7 +128,7 @@ export default class BlockDbRepository implements BlockRepository {
128128
FROM "Blocks" b
129129
WHERE b.height >= $2
130130
${conditions}
131-
ORDER BY b.id ${order}
131+
ORDER BY b.height ${order}
132132
LIMIT $1;
133133
`;
134134

indexer/src/kadena-server/repository/infra/repository/event-db-repository.ts

Lines changed: 99 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ export default class EventDbRepository implements EventRepository {
113113
}
114114

115115
async getEventsWithQualifiedName(params: GetEventsParams) {
116+
const HEIGHT_BATCH_SIZE = 200;
117+
const localOperator = (paramsLength: number) => (paramsLength > 2 ? `\nAND` : 'WHERE');
118+
116119
const {
117120
qualifiedEventName,
118121
blockHash,
@@ -138,73 +141,121 @@ export default class EventDbRepository implements EventRepository {
138141
const name = splitted.pop() ?? '';
139142
const module = splitted.join('.');
140143

141-
const queryParams: (string | number)[] = [limit];
144+
const queryParams: (string | number)[] = [limit, module, name];
145+
const blockQueryParams: (string | number)[] = [];
142146
let conditions = '';
147+
let eventConditions = '';
143148

144-
queryParams.push(module);
145-
conditions += `WHERE e.module = $${queryParams.length}`;
146-
queryParams.push(name);
147-
conditions += `\nAND e.name = $${queryParams.length}`;
148-
149-
if (blockHash) {
150-
queryParams.push(blockHash);
151-
conditions += `\nAND b.hash = $${queryParams.length}`;
149+
if (before) {
150+
queryParams.push(before);
151+
eventConditions += `\nAND e.id > $${queryParams.length}`;
152152
}
153153

154-
if (chainId) {
155-
queryParams.push(chainId);
156-
conditions += `\nAND b."chainId" = $${queryParams.length}`;
154+
if (after) {
155+
queryParams.push(after);
156+
eventConditions += `\nAND e.id < $${queryParams.length}`;
157157
}
158158

159-
if (minHeight) {
160-
queryParams.push(minHeight);
161-
conditions += `\nAND b."height" >= $${queryParams.length}`;
159+
if (requestKey) {
160+
blockQueryParams.push(requestKey);
161+
const op = localOperator(blockQueryParams.length);
162+
conditions += `${op} t."requestkey" = $${blockQueryParams.length + queryParams.length}`;
162163
}
163164

164-
if (maxHeight) {
165-
queryParams.push(maxHeight);
166-
conditions += `\nAND b."height" <= $${queryParams.length}`;
165+
if (blockHash) {
166+
blockQueryParams.push(blockHash);
167+
const op = localOperator(blockQueryParams.length);
168+
conditions += `${op} b.hash = $${blockQueryParams.length + queryParams.length}`;
167169
}
168170

169-
if (minimumDepth) {
170-
queryParams.push(minimumDepth);
171-
conditions += `\nAND b."height" <= $${queryParams.length}`;
171+
if (chainId) {
172+
blockQueryParams.push(chainId);
173+
const op = localOperator(blockQueryParams.length);
174+
conditions += `${op} b."chainId" = $${blockQueryParams.length + queryParams.length}`;
172175
}
173176

174-
if (requestKey) {
175-
queryParams.push(requestKey);
176-
conditions += `\nAND t."requestkey" = $${queryParams.length}`;
177+
let fromHeight = 0;
178+
let toHeight = 0;
179+
if (minHeight && maxHeight) {
180+
fromHeight = minHeight;
181+
toHeight = maxHeight - minHeight > 100 ? minHeight + HEIGHT_BATCH_SIZE : toHeight;
182+
} else if (minHeight) {
183+
fromHeight = minHeight;
184+
toHeight = minHeight + HEIGHT_BATCH_SIZE;
185+
} else if (maxHeight) {
186+
fromHeight = maxHeight - HEIGHT_BATCH_SIZE;
187+
toHeight = maxHeight;
177188
}
178189

179-
if (before) {
180-
queryParams.push(before);
181-
conditions += `\nAND e.id > $${queryParams.length}`;
190+
if (fromHeight && toHeight) {
191+
queryParams.push(fromHeight);
192+
let op = localOperator(blockQueryParams.length);
193+
conditions += `${op} b."height" >= $${blockQueryParams.length + queryParams.length}`;
194+
queryParams.push(toHeight);
195+
conditions += `\nAND b."height" <= $${blockQueryParams.length + queryParams.length}`;
182196
}
183197

184-
if (after) {
185-
queryParams.push(after);
186-
conditions += `\nAND e.id < $${queryParams.length}`;
198+
if (minimumDepth) {
199+
queryParams.push(minimumDepth);
200+
const op = localOperator(blockQueryParams.length);
201+
conditions += `${op} b."height" <= $${blockQueryParams.length + queryParams.length}`;
187202
}
188203

189-
const query = `
190-
select e.id as id,
191-
t.requestkey as "requestKey",
192-
b."chainId" as "chainId",
193-
b.height as height,
194-
e."orderIndex" as "orderIndex",
195-
e.module as "moduleName",
196-
e.name as name,
197-
e.params as parameters,
198-
b.hash as "blockHash"
199-
from "Events" e
200-
join "Transactions" t on e."transactionId" = t.id
201-
join "Blocks" b on b.id = t."blockId"
202-
${conditions}
203-
ORDER BY id ${order}
204-
LIMIT $1
205-
`;
204+
let query = '';
205+
if (fromHeight || toHeight || blockHash || chainId) {
206+
query = `
207+
WITH block_filtered AS (
208+
select *
209+
from "Blocks" b
210+
${conditions}
211+
)
212+
SELECT
213+
e.id as id,
214+
e.requestkey as "requestKey",
215+
e."chainId" as "chainId",
216+
b.height as height,
217+
e."orderIndex" as "orderIndex",
218+
e.module as "moduleName",
219+
e.name as name,
220+
e.params as parameters,
221+
b.hash as "blockHash"
222+
FROM block_filtered b
223+
join "Transactions" t ON t."blockId" = b.id
224+
join "Events" e ON e."transactionId" = t.id
225+
WHERE e.module = $2
226+
AND e.name = $3
227+
${eventConditions}
228+
LIMIT $1
229+
`;
230+
} else {
231+
query = `
232+
WITH event_filtered AS (
233+
select *
234+
from "Events" e
235+
WHERE e.module = $2
236+
AND e.name = $3
237+
${eventConditions}
238+
ORDER BY e.module, e.name ${order}
239+
)
240+
SELECT
241+
e.id as id,
242+
e.requestkey as "requestKey",
243+
e."chainId" as "chainId",
244+
b.height as height,
245+
e."orderIndex" as "orderIndex",
246+
e.module as "moduleName",
247+
e.name as name,
248+
e.params as parameters,
249+
b.hash as "blockHash"
250+
FROM event_filtered e
251+
join "Transactions" t ON t.id = e."transactionId"
252+
join "Blocks" b ON b.id = t."blockId"
253+
${conditions}
254+
LIMIT $1
255+
`;
256+
}
206257

207-
const { rows } = await rootPgPool.query(query, queryParams);
258+
const { rows } = await rootPgPool.query(query, [...queryParams, ...blockQueryParams]);
208259

209260
const edges = rows.map(row => ({
210261
cursor: row.id.toString(),

0 commit comments

Comments
 (0)