Skip to content

Commit

Permalink
feat: process new sources events in same transaction (#246)
Browse files Browse the repository at this point in the history
Previously sources added at transaction A would not be processed at
that transaction, only when next transaction is processed.

This PR changes this behaviour causing new sources to be processed
immediatelly, within the same transaction they were created (assuming
start block expects this).
  • Loading branch information
Sekhmet authored Sep 6, 2023
1 parent 5ac819b commit 57af54c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ export default class Checkpoint {

const templateSources = await this.store.getTemplateSources();
await Promise.all(
templateSources.map(source => {
templateSources.map(source =>
this.executeTemplate(
source.template,
{
contract: source.contractAddress,
start: source.startBlock
},
false
);
})
)
)
);

const blockNum = await this.getStartBlockNum();
Expand Down
17 changes: 15 additions & 2 deletions src/providers/starknet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import type {
PendingTransaction,
Event,
EventsMap,
ParsedEvent
ParsedEvent,
ContractSourceConfig
} from '../../types';

export class StarknetProvider extends BaseProvider {
Expand Down Expand Up @@ -179,7 +180,11 @@ export class StarknetProvider extends BaseProvider {
}
}

for (const source of this.instance.getCurrentSources(blockNumber)) {
let lastSources = this.instance.getCurrentSources(blockNumber);
const sourcesQueue = [...lastSources];

let source: ContractSourceConfig | undefined;
while ((source = sourcesQueue.shift())) {
let foundContractData = false;
const contract = validateAndParseAddress(source.contract);

Expand Down Expand Up @@ -243,6 +248,14 @@ export class StarknetProvider extends BaseProvider {
if (foundContractData) {
await this.instance.insertCheckpoints([{ blockNumber, contractAddress: source.contract }]);
}

const nextSources = this.instance.getCurrentSources(blockNumber);
const newSources = nextSources.filter(
nextSource => !lastSources.find(lastSource => lastSource.contract === nextSource.contract)
);

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

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

0 comments on commit 57af54c

Please sign in to comment.