Skip to content

Commit

Permalink
don't forget to add type and default query to old request
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash committed Dec 2, 2024
1 parent aa67f45 commit 05fd295
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions evm/evm-processor/src/ds-archive/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<DataRequest>) {
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<Block, DataRequest> {
constructor(private client: PortalClient) {}
Expand All @@ -55,57 +64,55 @@ export class EvmPortal implements DataSource<Block, DataRequest> {
}

async getBlockHash(height: number): Promise<Bytes32> {
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<DataRequest>[], stopOnHead?: boolean | undefined): AsyncIterable<Batch<Block>> {
async *getFinalizedBlocks(
requests: RangeRequest<DataRequest>[],
stopOnHead?: boolean | undefined
): AsyncIterable<Batch<Block>> {
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()
}
}
}
}

0 comments on commit 05fd295

Please sign in to comment.