From 05fd295779fc4869d94bf220bef617d5f07766cd Mon Sep 17 00:00:00 2001 From: belopash Date: Mon, 2 Dec 2024 21:43:30 +0500 Subject: [PATCH] don't forget to add type and default query to old request --- evm/evm-processor/src/ds-archive/portal.ts | 67 ++++++++++++---------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/evm/evm-processor/src/ds-archive/portal.ts b/evm/evm-processor/src/ds-archive/portal.ts index f2c9dedf..6c0895e6 100644 --- a/evm/evm-processor/src/ds-archive/portal.ts +++ b/evm/evm-processor/src/ds-archive/portal.ts @@ -9,7 +9,6 @@ import {mapBlock} from './mapping' import {PortalClient} from '@subsquid/portal-client' import {FieldSelection} from '../interfaces/data' - const ALWAYS_SELECTED_FIELDS = { block: { number: true, @@ -35,17 +34,27 @@ const ALWAYS_SELECTED_FIELDS = { }, } as const - -function addAlwaysSelectedFields(fields?: FieldSelection): FieldSelection { +function getFields(fields?: FieldSelection): FieldSelection { return { block: {...fields?.block, ...ALWAYS_SELECTED_FIELDS.block}, transaction: {...fields?.transaction, ...ALWAYS_SELECTED_FIELDS.transaction}, log: {...fields?.log, ...ALWAYS_SELECTED_FIELDS.log}, trace: {...fields?.trace, ...ALWAYS_SELECTED_FIELDS.trace}, - stateDiff: {...fields?.stateDiff, ...ALWAYS_SELECTED_FIELDS.stateDiff, kind: true} + stateDiff: {...fields?.stateDiff, ...ALWAYS_SELECTED_FIELDS.stateDiff, kind: true}, } } +function makeQuery(req: RangeRequest) { + let {fields, ...request} = req.request + + return { + type: 'evm', + fromBlock: req.range.from, + toBlock: req.range.to, + fields: getFields(fields), + ...request, + } +} export class EvmPortal implements DataSource { constructor(private client: PortalClient) {} @@ -55,52 +64,51 @@ export class EvmPortal implements DataSource { } async getBlockHash(height: number): Promise { - let blocks = await this.client.query({ - fromBlock: height, - toBlock: height, - includeAllBlocks: true + let query = makeQuery({ + range: {from: height, to: height}, + request: {includeAllBlocks: true}, }) + let blocks = await this.client.query(query) assert(blocks.length == 1) return blocks[0].header.hash } - async *getFinalizedBlocks(requests: RangeRequest[], stopOnHead?: boolean | undefined): AsyncIterable> { + async *getFinalizedBlocks( + requests: RangeRequest[], + stopOnHead?: boolean | undefined + ): AsyncIterable> { let height = new Throttler(() => this.client.getHeight(), 20_000) - + let top = await height.get() for (let req of requests) { - let fromBlock = req.range.from - let toBlock = req.range.to - let fields = addAlwaysSelectedFields(req.request.fields) + let from = req.range.from + let to = req.range.to + if (top < from && stopOnHead) return - if (top < fromBlock && stopOnHead) return - - for await (let batch of this.client.stream({ - ...req.request, - type: 'evm', - fromBlock, - toBlock, - fields, - }, stopOnHead)) { + let query = makeQuery({ + ...req, + range: {from, to}, + }) + for await (let batch of this.client.stream(query, stopOnHead)) { assert(batch.length > 0, 'boundary blocks are expected to be included') let lastBlock = last(batch).header.number - assert(lastBlock >= fromBlock) - fromBlock = lastBlock + 1 + assert(lastBlock >= from) + from = lastBlock + 1 - let blocks = batch.map(b => { + let blocks = batch.map((b) => { try { - return mapBlock(b, fields) - } catch(err: any) { + return mapBlock(b, req.request.fields || {}) + } catch (err: any) { throw addErrorContext(err, { blockHeight: b.header.number, - blockHash: b.header.hash + blockHash: b.header.hash, }) } }) yield { blocks, - isHead: fromBlock > top + isHead: from > top, } top = await height.get() @@ -108,4 +116,3 @@ export class EvmPortal implements DataSource { } } } -