Skip to content

Commit 60138db

Browse files
Merge pull request #1587 from multiversx/executionresults-blocks-list
Executionresults blocks list
2 parents ce0e26f + 9088a57 commit 60138db

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

src/common/indexer/elastic/elastic.indexer.service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ export class ElasticIndexerService implements IndexerInterface {
255255
return await this.elasticService.getItem('executionresults', 'hash', hash);
256256
}
257257

258+
async getExecutionResultsForHashes(hashes: string[]): Promise<Block[]> {
259+
if (!hashes || hashes.length === 0) {
260+
return [];
261+
}
262+
263+
const elasticQuery = ElasticQuery.create()
264+
.withPagination({ from: 0, size: hashes.length + 1 })
265+
.withShouldCondition(hashes.map(h => QueryType.Match('_id', h)));
266+
267+
return await this.elasticService.getList('executionresults', 'hash', elasticQuery);
268+
}
269+
258270
async getBlockByMiniBlockHash(miniBlockHash: string): Promise<Block | undefined> {
259271
const elasticQuery = ElasticQuery.create()
260272
.withCondition(QueryConditionOptions.must, [QueryType.Match('miniBlocksHashes', miniBlockHash)])

src/common/indexer/indexer.interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export interface IndexerInterface {
7676

7777
getExecutionResults(hash: string): Promise<Block>
7878

79+
getExecutionResultsForHashes(hashes: string[]): Promise<Block[]>
80+
7981
getBlockByMiniBlockHash(miniBlockHash: string): Promise<Block | undefined>
8082

8183
getMiniBlock(miniBlockHash: string): Promise<MiniBlock>

src/common/indexer/indexer.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ export class IndexerService implements IndexerInterface {
171171
return await this.indexerInterface.getExecutionResults(hash);
172172
}
173173

174+
@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
175+
async getExecutionResultsForHashes(hashes: string[]): Promise<Block[]> {
176+
return await this.indexerInterface.getExecutionResultsForHashes(hashes);
177+
}
178+
174179
@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
175180
async getBlockByMiniBlockHash(miniBlockHash: string): Promise<Block | undefined> {
176181
return await this.indexerInterface.getBlockByMiniBlockHash(miniBlockHash);

src/endpoints/blocks/block.service.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ export class BlockService {
3939
async getBlocks(filter: BlockFilter, queryPagination: QueryPagination, withProposerIdentity?: boolean): Promise<Block[]> {
4040
const result = await this.indexerService.getBlocks(filter, queryPagination);
4141

42+
const executionResultsMap = await this.fetchExecutionResultsForBlocks(result as any[]);
43+
if (executionResultsMap.size > 0) {
44+
for (const item of result as any[]) {
45+
const executionResult = executionResultsMap.get(item.hash);
46+
if (executionResult) {
47+
ApiUtils.mergeObjects(item, executionResult);
48+
}
49+
}
50+
}
51+
4252
const blocks = await Promise.all(result.map(async (item) => {
4353
const blockRaw = await this.computeProposerAndValidators(item);
4454

@@ -58,6 +68,41 @@ export class BlockService {
5868
return blocks;
5969
}
6070

71+
private async fetchExecutionResultsForBlocks(items: any[]): Promise<Map<string, any>> {
72+
const map = new Map<string, any>();
73+
if (!items || items.length === 0) {
74+
return map;
75+
}
76+
77+
const supernovaEnableEpoch = await this.getSupernovaEnableEpoch();
78+
if (supernovaEnableEpoch === -1) {
79+
return map;
80+
}
81+
82+
const eligible = items.filter((r: any) => (r?.epoch ?? -1) >= supernovaEnableEpoch);
83+
if (eligible.length === 0) {
84+
return map;
85+
}
86+
87+
const hashes = eligible.map((r: any) => r.hash).filter(Boolean);
88+
if (hashes.length === 0) {
89+
return map;
90+
}
91+
92+
try {
93+
const executionResults = await this.indexerService.getExecutionResultsForHashes(hashes);
94+
for (const er of executionResults as any[]) {
95+
if (er?.hash) {
96+
map.set(er.hash, er);
97+
}
98+
}
99+
} catch {
100+
return map;
101+
}
102+
103+
return map;
104+
}
105+
61106
private async applyProposerIdentity(blocks: Block[]): Promise<void> {
62107
const proposerBlses = blocks.map(x => x.proposer);
63108

@@ -176,7 +221,7 @@ export class BlockService {
176221

177222
async getSupernovaEnableEpoch(): Promise<number> {
178223
const enableEpochs = await this.getNetworkEnableEpochs();
179-
return enableEpochs["erd_supernova_enable_epoch"] ?? -1;
224+
return enableEpochs?.["erd_supernova_enable_epoch"] ?? -1;
180225
}
181226

182227
async getNetworkEnableEpochs(): Promise<Record<string, number>> {

0 commit comments

Comments
 (0)