Skip to content

Commit 8cc9910

Browse files
authored
refactor: query api optimizations (#6)
* use getHeader instead of getBlock * logging * optimization
1 parent db9d465 commit 8cc9910

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

query/app/api/route.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ async function getBlockNumber(blockHash: string, chainName: string): Promise<num
4444
const api = await initialize(CHAIN_TO_WS_ENDPOINT.get(chainName.toLowerCase()) as string);
4545
const rpc: any = api.rpc;
4646
try {
47-
const block = await rpc.chain.getBlock(blockHash);
47+
const header = await rpc.chain.getHeader(blockHash);
4848
await disconnect();
49-
return block.block.header.number.toNumber();
49+
return header.number.toNumber();
5050
} catch (error) {
5151
console.log(error);
5252
}
@@ -370,6 +370,8 @@ export async function GET(req: NextRequest) {
370370
error: 'Getting the block range covered by the VectorX contract failed!'
371371
});
372372
}
373+
374+
console.log('Block range: ' + blockRange.start + ' - ' + blockRange.end);
373375

374376
if (requestedBlock < blockRange.start || requestedBlock > blockRange.end) {
375377
return NextResponse.json({
@@ -387,6 +389,9 @@ export async function GET(req: NextRequest) {
387389

388390
let [requestedBlockHash, dataCommitmentRange] = await Promise.all(promises);
389391

392+
console.log('Requested block hash: ' + requestedBlockHash);
393+
console.log('Data commitment range: ' + dataCommitmentRange);
394+
390395
if (dataCommitmentRange === null) {
391396
return NextResponse.json({
392397
success: false,
@@ -404,6 +409,8 @@ export async function GET(req: NextRequest) {
404409
chainName!
405410
);
406411

412+
console.log('Got data roots: ' + dataRoots.length);
413+
407414
// Extend the header array to commitmentTreeSize (fill with empty bytes).
408415
if (dataRoots.length < commitmentTreeSize) {
409416
const additionalRoots = new Array(commitmentTreeSize - dataRoots.length).fill(new Uint8Array(32));

query/app/utils/shared.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export async function queryLogs(
8080
toBlock: number,
8181
event: AbiEvent,
8282
) {
83+
console.log('queryLogs', fromBlock, toBlock);
8384
if (process.env[`RPC_${ethereumChainId}`] == undefined) {
8485
throw new Error('Missing RPC URL for chain ' + ethereumChainId);
8586
}
@@ -173,7 +174,7 @@ export async function getBlocksSinceLastLog(ethereumChainId: number, ethereumRpc
173174
Number(ethCurrentBlockNumber) - (diffBlocks * 10),
174175
Number(ethCurrentBlockNumber),
175176
event,
176-
diffBlocks
177+
10000
177178
);
178179

179180
// Sort headUpdateLogs by block number descending.

0 commit comments

Comments
 (0)