Skip to content

Commit

Permalink
refactor: add changed identities in interval
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Nov 7, 2024
1 parent f706e70 commit 4f62bd5
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions src/modules/did/did.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class DIDService implements OnModuleInit, OnModuleDestroy {
private readonly didRegistry: EthereumDIDRegistry;
private readonly resolver: Resolver;
private readonly JOBS_CLEANUP_DELAY = 1000;
private readonly MAX_EVENTS_QUERY_INTERVAL = 1000000;

constructor(
private readonly config: ConfigService,
Expand Down Expand Up @@ -363,44 +364,30 @@ export class DIDService implements OnModuleInit, OnModuleDestroy {

private async syncDocuments() {
this.logger.debug(`Beginning sync of DID Documents`);
const didEventFilters = [
this.didRegistry.filters.DIDAttributeChanged(null),
this.didRegistry.filters.DIDDelegateChanged(null),
this.didRegistry.filters.DIDOwnerChanged(null),
];
const syncs = await this.lastDidSyncRepository.find({
order: { createdDate: 'DESC' },
});
const fromBlock = syncs.length > 0 ? syncs[0].block : 0;
const topBlock = await this.provider.getBlockNumber();
const maxSyncInterval = 5000000;
const toBlock = Math.min(topBlock, fromBlock + maxSyncInterval);
this.logger.debug(
`Getting DID update events from block ${fromBlock} to block ${toBlock}...`
`Getting DID update events from block ${fromBlock} to block ${topBlock}...`
);
const changedIdentities = await this.getChangedIdentities(
fromBlock,
topBlock
);
const events = (
await Promise.all(
didEventFilters.map((filter) =>
this.didRegistry.queryFilter(filter, fromBlock, toBlock)
)
)
).flat();
let changedIdentities = events.map((event) => {
return event.args.identity;
});
// Deduplicate identities if they are appears in multiple events
changedIdentities = [...new Set(changedIdentities).values()];
const didsToSynchronize = (
await this.didRepository.find({ select: ['id'] })
).filter((doc) => {
const identity = doc.id.split(':')[3];
return changedIdentities.includes(identity);
});
didsToSynchronize.forEach(async (did) => {
this.logger.debug(`Synchronizing DID ${did.id}`);
await this.pinDocument(did.id);
});

await this.lastDidSyncRepository.save({ block: toBlock });
await this.lastDidSyncRepository.save({ block: topBlock });
}

private parseLogs(logs: string) {
Expand Down Expand Up @@ -534,4 +521,41 @@ export class DIDService implements OnModuleInit, OnModuleDestroy {
this.logger.debug(inspect(jobsCounts, { depth: 2, colors: true }));
}
}

private async getChangedIdentities(
fromBlock: number,
topBlock: number
): Promise<string[]> {
const didEventFilters = [
this.didRegistry.filters.DIDAttributeChanged(null),
this.didRegistry.filters.DIDDelegateChanged(null),
this.didRegistry.filters.DIDOwnerChanged(null),
];
const events = [];
while (fromBlock < topBlock) {
const toBlock = Math.min(
topBlock,
fromBlock + this.MAX_EVENTS_QUERY_INTERVAL
);

const intervalEvents = (
await Promise.all(
didEventFilters.map((filter) =>
this.didRegistry.queryFilter(filter, fromBlock, toBlock)
)
)
).flat();
this.logger.debug(
`Fetched ${intervalEvents.length} DID events from interval [${fromBlock}, ${toBlock}]`
);
events.push(...intervalEvents);
fromBlock += this.MAX_EVENTS_QUERY_INTERVAL;
}
this.logger.debug(`Update document events count ${events.length}`);
const changedIdentities = events.map((event) => {
return event.args.identity;
});
// Deduplicate identities if they are appears in multiple events
return [...new Set(changedIdentities).values()];
}
}

0 comments on commit 4f62bd5

Please sign in to comment.