Skip to content

Commit

Permalink
fix: Maximum call stack size exceeded with array.push (Bug: last_inde…
Browse files Browse the repository at this point in the history
…xed_block reset to Zero after reaching the seed blocks checkpoint-labs#300)

When indexing contracts with a big number of events the array.push method will throw an error. When you use spread operator all items of the source array are stored in the stack as arguments list, so having a large number of items (~ > 100K) will cause the this stack size exceed. The most simple work-around is to manually push all items one by one.
  • Loading branch information
shardAstronaut committed Jun 27, 2024
1 parent b427b15 commit 95fcfca
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/providers/starknet/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export class StarknetProvider extends BaseProvider {
}

let lastSources = this.instance.getCurrentSources(blockNumber);
const sourcesQueue = [...lastSources];
let sourcesQueue = [...lastSources];

let source: ContractSourceConfig | undefined;
while ((source = sourcesQueue.shift())) {
Expand Down Expand Up @@ -299,15 +299,15 @@ export class StarknetProvider extends BaseProvider {
nextSource => !lastSources.find(lastSource => lastSource.contract === nextSource.contract)
);

sourcesQueue.push(...newSources);
sourcesQueue = sourcesQueue.concat(newSources);
lastSources = nextSources;
}

this.log.debug({ txIndex }, 'handling transaction done');
}

private async getEvents(blockNumber: number): Promise<EventsMap> {
const events: Event[] = [];
let events: Event[] = [];

let continuationToken: string | undefined;
do {
Expand All @@ -318,7 +318,7 @@ export class StarknetProvider extends BaseProvider {
continuation_token: continuationToken
});

events.push(...result.events);
events = events.concat(result.events);

continuationToken = result.continuation_token;
} while (continuationToken);
Expand All @@ -345,7 +345,7 @@ export class StarknetProvider extends BaseProvider {
toBlock: number,
address: string
): Promise<CheckpointRecord[]> {
const events: Event[] = [];
let events: Event[] = [];

let continuationToken: string | undefined;
do {
Expand All @@ -357,7 +357,7 @@ export class StarknetProvider extends BaseProvider {
continuation_token: continuationToken
});

events.push(...result.events);
events = events.concat(result.events);

continuationToken = result.continuation_token;
} while (continuationToken);
Expand All @@ -369,15 +369,15 @@ export class StarknetProvider extends BaseProvider {
}

async getCheckpointsRange(fromBlock: number, toBlock: number): Promise<CheckpointRecord[]> {
const events: CheckpointRecord[] = [];
let events: CheckpointRecord[] = [];

for (const source of this.instance.getCurrentSources(fromBlock)) {
const addressEvents = await this.getCheckpointsRangeForAddress(
fromBlock,
toBlock,
source.contract
);
events.push(...addressEvents);
events = events.concat(addressEvents);
}

return events;
Expand Down

0 comments on commit 95fcfca

Please sign in to comment.