Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/common/indexer/elastic/elastic.indexer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ export class ElasticIndexerService implements IndexerInterface {
return await this.elasticService.getItem('executionresults', 'hash', hash);
}

async getExecutionResultsForHashes(hashes: string[]): Promise<Block[]> {
if (!hashes || hashes.length === 0) {
return [];
}

const elasticQuery = ElasticQuery.create()
.withPagination({ from: 0, size: hashes.length + 1 })
.withShouldCondition(hashes.map(h => QueryType.Match('_id', h)));

return await this.elasticService.getList('executionresults', 'hash', elasticQuery);
}

async getBlockByMiniBlockHash(miniBlockHash: string): Promise<Block | undefined> {
const elasticQuery = ElasticQuery.create()
.withCondition(QueryConditionOptions.must, [QueryType.Match('miniBlocksHashes', miniBlockHash)])
Expand Down
2 changes: 2 additions & 0 deletions src/common/indexer/indexer.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export interface IndexerInterface {

getExecutionResults(hash: string): Promise<Block>

getExecutionResultsForHashes(hashes: string[]): Promise<Block[]>

getBlockByMiniBlockHash(miniBlockHash: string): Promise<Block | undefined>

getMiniBlock(miniBlockHash: string): Promise<MiniBlock>
Expand Down
5 changes: 5 additions & 0 deletions src/common/indexer/indexer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ export class IndexerService implements IndexerInterface {
return await this.indexerInterface.getExecutionResults(hash);
}

@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
async getExecutionResultsForHashes(hashes: string[]): Promise<Block[]> {
return await this.indexerInterface.getExecutionResultsForHashes(hashes);
}

@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
async getBlockByMiniBlockHash(miniBlockHash: string): Promise<Block | undefined> {
return await this.indexerInterface.getBlockByMiniBlockHash(miniBlockHash);
Expand Down
47 changes: 46 additions & 1 deletion src/endpoints/blocks/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export class BlockService {
async getBlocks(filter: BlockFilter, queryPagination: QueryPagination, withProposerIdentity?: boolean): Promise<Block[]> {
const result = await this.indexerService.getBlocks(filter, queryPagination);

const executionResultsMap = await this.fetchExecutionResultsForBlocks(result as any[]);
if (executionResultsMap.size > 0) {
for (const item of result as any[]) {
const executionResult = executionResultsMap.get(item.hash);
if (executionResult) {
ApiUtils.mergeObjects(item, executionResult);
}
}
}

const blocks = await Promise.all(result.map(async (item) => {
const blockRaw = await this.computeProposerAndValidators(item);

Expand All @@ -58,6 +68,41 @@ export class BlockService {
return blocks;
}

private async fetchExecutionResultsForBlocks(items: any[]): Promise<Map<string, any>> {
const map = new Map<string, any>();
if (!items || items.length === 0) {
return map;
}

const supernovaEnableEpoch = await this.getSupernovaEnableEpoch();
if (supernovaEnableEpoch === -1) {
return map;
}

const eligible = items.filter((r: any) => (r?.epoch ?? -1) >= supernovaEnableEpoch);
if (eligible.length === 0) {
return map;
}

const hashes = eligible.map((r: any) => r.hash).filter(Boolean);
if (hashes.length === 0) {
return map;
}

try {
const executionResults = await this.indexerService.getExecutionResultsForHashes(hashes);
for (const er of executionResults as any[]) {
if (er?.hash) {
map.set(er.hash, er);
}
}
} catch {
return map;
}

return map;
}

private async applyProposerIdentity(blocks: Block[]): Promise<void> {
const proposerBlses = blocks.map(x => x.proposer);

Expand Down Expand Up @@ -176,7 +221,7 @@ export class BlockService {

async getSupernovaEnableEpoch(): Promise<number> {
const enableEpochs = await this.getNetworkEnableEpochs();
return enableEpochs["erd_supernova_enable_epoch"] ?? -1;
return enableEpochs?.["erd_supernova_enable_epoch"] ?? -1;
}

async getNetworkEnableEpochs(): Promise<Record<string, number>> {
Expand Down
Loading