Skip to content

Commit

Permalink
fix(sdk): pass handlerName from template to base processor (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnons authored Jan 29, 2025
1 parent 40e39c0 commit ce1778e
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 31 deletions.
5 changes: 3 additions & 2 deletions packages/sdk/src/aptos/aptos-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,12 @@ export class AptosResourcesProcessor {
timeInterval: HandleInterval | undefined,
versionInterval: HandleInterval | undefined,
type: string | undefined,
fetchConfig: Partial<MoveAccountFetchConfig> | undefined
fetchConfig: Partial<MoveAccountFetchConfig> | undefined,
handlerName = getHandlerName()
): this {
const processor = this
this.resourceIntervalHandlers.push({
handlerName: getHandlerName(),
handlerName,
handler: async function (data) {
if (data.timestampMicros > Number.MAX_SAFE_INTEGER) {
throw new ServerError(Status.INVALID_ARGUMENT, 'timestamp is too large')
Expand Down
13 changes: 11 additions & 2 deletions packages/sdk/src/aptos/aptos-resource-processor-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AptosResourcesProcessor, DEFAULT_RESOURCE_FETCH_CONFIG } from './aptos-
import { HandleInterval, MoveAccountFetchConfig } from '@sentio/protos'
import { MoveResource } from '@aptos-labs/ts-sdk'
import { PromiseOrVoid } from '../core/index.js'
import { proxyProcessor } from '../utils/metrics.js'
import { getHandlerName, proxyProcessor } from '../utils/metrics.js'

export class AptosResourceProcessorTemplateState extends ListStateStorage<AptosResourceProcessorTemplate> {
static INSTANCE = new AptosResourceProcessorTemplateState()
Expand All @@ -16,6 +16,7 @@ class Handler {
type?: string
checkpointInterval?: HandleInterval
timeIntervalInMinutes?: HandleInterval
handlerName: string
handler: (resources: MoveResource[], ctx: AptosResourcesContext) => PromiseOrVoid
fetchConfig: MoveAccountFetchConfig
}
Expand Down Expand Up @@ -49,7 +50,14 @@ export class AptosResourceProcessorTemplate {

const processor = this.createProcessor(options)
for (const h of this.handlers) {
processor.onInterval(h.handler, h.timeIntervalInMinutes, h.checkpointInterval, h.type, h.fetchConfig)
processor.onInterval(
h.handler,
h.timeIntervalInMinutes,
h.checkpointInterval,
h.type,
h.fetchConfig,
h.handlerName
)
}
const config = processor.config

Expand Down Expand Up @@ -85,6 +93,7 @@ export class AptosResourceProcessorTemplate {
fetchConfig: Partial<MoveAccountFetchConfig> | undefined
): this {
this.handlers.push({
handlerName: getHandlerName(),
handler: handler,
timeIntervalInMinutes: timeInterval,
checkpointInterval: checkpointInterval,
Expand Down
21 changes: 17 additions & 4 deletions packages/sdk/src/eth/base-processor-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BlockParams } from 'ethers/providers'
import { DeferredTopicFilter } from 'ethers/contract'
import { TypedEvent, TypedCallTrace } from './eth.js'
import { TemplateInstanceState } from '../core/template.js'
import { proxyProcessor } from '../utils/metrics.js'
import { getHandlerName, proxyProcessor } from '../utils/metrics.js'

export class ProcessorTemplateProcessorState extends ListStateStorage<
BaseProcessorTemplate<BaseContract, BoundContractView<BaseContract, any>>
Expand All @@ -24,6 +24,7 @@ export abstract class BaseProcessorTemplate<
id: number
binds = new Set<string>()
blockHandlers: {
handlerName: string
handler: (block: BlockParams, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid
preprocessHandler: (
block: BlockParams,
Expand All @@ -36,6 +37,7 @@ export abstract class BaseProcessorTemplate<
}[] = []
traceHandlers: {
signature: string
handlerName: string
handler: (trace: TypedCallTrace, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid
preprocessHandler: (
trace: TypedCallTrace,
Expand All @@ -45,6 +47,7 @@ export abstract class BaseProcessorTemplate<
fetchConfig?: EthFetchConfig
}[] = []
eventHandlers: {
handlerName: string
handler: (event: TypedEvent, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid
preprocessHandler: (
event: TypedEvent,
Expand Down Expand Up @@ -81,14 +84,21 @@ export abstract class BaseProcessorTemplate<

for (const eh of this.eventHandlers) {
// @ts-ignore friendly
processor.onEthEvent(eh.handler, eh.filter, eh.fetchConfig, eh.preprocessHandler)
processor.onEthEvent(eh.handler, eh.filter, eh.fetchConfig, eh.preprocessHandler, eh.handlerName)
}
for (const th of this.traceHandlers) {
// @ts-ignore friendly
processor.onEthTrace(th.signature, th.handler, th.fetchConfig, th.preprocessHandler)
processor.onEthTrace(th.signature, th.handler, th.fetchConfig, th.preprocessHandler, th.handlerName)
}
for (const bh of this.blockHandlers) {
processor.onInterval(bh.handler, bh.timeIntervalInMinutes, bh.blockInterval, bh.fetchConfig, bh.preprocessHandler)
processor.onInterval(
bh.handler,
bh.timeIntervalInMinutes,
bh.blockInterval,
bh.fetchConfig,
bh.preprocessHandler,
bh.handlerName
)
}

const instance: TemplateInstance = {
Expand Down Expand Up @@ -124,6 +134,7 @@ export abstract class BaseProcessorTemplate<
) => Promise<PreprocessResult> = defaultPreprocessHandler
) {
this.eventHandlers.push({
handlerName: getHandlerName(),
handler: handler,
preprocessHandler,
filter: filter,
Expand Down Expand Up @@ -187,6 +198,7 @@ export abstract class BaseProcessorTemplate<
) => Promise<PreprocessResult> = defaultPreprocessHandler
) {
this.blockHandlers.push({
handlerName: getHandlerName(),
handler,
preprocessHandler,
timeIntervalInMinutes: timeInterval,
Expand All @@ -208,6 +220,7 @@ export abstract class BaseProcessorTemplate<
) {
this.traceHandlers.push({
signature,
handlerName: getHandlerName(),
handler,
preprocessHandler,
fetchConfig: EthFetchConfig.fromPartial(fetchConfig || {})
Expand Down
15 changes: 9 additions & 6 deletions packages/sdk/src/eth/base-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ export abstract class BaseProcessor<
event: TypedEvent,
ctx: ContractContext<TContract, TBoundContractView>,
preprocessStore: { [k: string]: any }
) => Promise<PreprocessResult> = defaultPreprocessHandler
) => Promise<PreprocessResult> = defaultPreprocessHandler,
handlerName = getHandlerName()
): this {
const chainId = this.getChainId()
let _filters: DeferredTopicFilter[] = []
Expand All @@ -445,7 +446,7 @@ export abstract class BaseProcessor<
this.eventHandlers.push({
filters: _filters,
fetchConfig: EthFetchConfig.fromPartial(fetchConfig || {}),
handlerName: getHandlerName(),
handlerName,
handler: async function (data: Data_EthLog, preparedData?: PreparedData) {
const { log, block, transaction, transactionReceipt } = formatEthData(data)
if (!log) {
Expand Down Expand Up @@ -605,14 +606,15 @@ export abstract class BaseProcessor<
block: RichBlock,
ctx: ContractContext<TContract, TBoundContractView>,
preprocessStore: { [k: string]: any }
) => Promise<PreprocessResult> = defaultPreprocessHandler
) => Promise<PreprocessResult> = defaultPreprocessHandler,
handlerName = getHandlerName()
): this {
const chainId = this.getChainId()
const processor = this
const contractName = this.config.name

this.blockHandlers.push({
handlerName: getHandlerName(),
handlerName,
handler: async function (data: Data_EthBlock, preparedData?: PreparedData) {
const { block } = formatEthData(data)

Expand Down Expand Up @@ -676,7 +678,8 @@ export abstract class BaseProcessor<
trace: TypedCallTrace,
ctx: ContractContext<TContract, TBoundContractView>,
preprocessStore: { [k: string]: any }
) => Promise<PreprocessResult> = defaultPreprocessHandler
) => Promise<PreprocessResult> = defaultPreprocessHandler,
handlerName = getHandlerName()
): this {
const chainId = this.getChainId()
const contractName = this.config.name
Expand All @@ -688,7 +691,7 @@ export abstract class BaseProcessor<
this.traceHandlers.push({
signatures,
fetchConfig: EthFetchConfig.fromPartial(fetchConfig || {}),
handlerName: getHandlerName(),
handlerName,
handler: async function (data: Data_EthTrace, preparedData?: PreparedData) {
const contractView = processor.CreateBoundContractView()
const contractInterface = contractView.rawContract.interface
Expand Down
14 changes: 10 additions & 4 deletions packages/sdk/src/fuel/fuel-processor-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Contract } from 'fuels'
import { FuelBlock, FuelLog, FuelTransaction } from './types.js'
import { DEFAULT_FUEL_FETCH_CONFIG, FuelFetchConfig } from './transaction.js'
import { FuelProcessor, FuelProcessorConfig, getOptionsSignature } from './fuel-processor.js'
import { proxyProcessor } from '../utils/metrics.js'
import { getHandlerName, proxyProcessor } from '../utils/metrics.js'

export class FuelProcessorTemplateProcessorState extends ListStateStorage<FuelBaseProcessorTemplate<Contract>> {
static INSTANCE = new FuelProcessorTemplateProcessorState()
Expand All @@ -17,6 +17,7 @@ export abstract class FuelBaseProcessorTemplate<TContract extends Contract> {
id: number
binds = new Set<string>()
blockHandlers: {
handlerName: string
handler: (block: FuelBlock, ctx: FuelContractContext<TContract>) => PromiseOrVoid
blockInterval?: HandleInterval
timeIntervalInMinutes?: HandleInterval
Expand All @@ -25,11 +26,13 @@ export abstract class FuelBaseProcessorTemplate<TContract extends Contract> {

logHandlers: {
logIdFilter: string | string[]
handlerName: string
handler: (logs: FuelLog<any>, ctx: FuelContractContext<TContract>) => PromiseOrVoid
// fetchConfig?: FuelFetchConfig
}[] = []

transactionHandlers: {
handlerName: string
handler: (transaction: FuelTransaction, ctx: FuelContractContext<TContract>) => PromiseOrVoid
fetchConfig: FuelFetchConfig
}[] = []
Expand Down Expand Up @@ -59,13 +62,13 @@ export abstract class FuelBaseProcessorTemplate<TContract extends Contract> {
const processor = this.bindInternal({ ...options, chainId: ctx.chainId })

for (const eh of this.logHandlers) {
processor.onLog(eh.logIdFilter, eh.handler)
processor.onLog(eh.logIdFilter, eh.handler, eh.handlerName)
}
for (const bh of this.blockHandlers) {
processor.onInterval(bh.handler, bh.timeIntervalInMinutes, bh.blockInterval)
processor.onInterval(bh.handler, bh.timeIntervalInMinutes, bh.blockInterval, bh.handlerName)
}
for (const th of this.transactionHandlers) {
processor.onTransaction(th.handler)
processor.onTransaction(th.handler, undefined, th.handlerName)
}

const instance: TemplateInstance = {
Expand Down Expand Up @@ -96,6 +99,7 @@ export abstract class FuelBaseProcessorTemplate<TContract extends Contract> {
) {
this.logHandlers.push({
logIdFilter,
handlerName: getHandlerName(),
handler
// fetchConfig: { ...fetchConfig}
})
Expand Down Expand Up @@ -140,6 +144,7 @@ export abstract class FuelBaseProcessorTemplate<TContract extends Contract> {
// fetchConfig?: FuelFetchConfig
) {
this.blockHandlers.push({
handlerName: getHandlerName(),
handler,
timeIntervalInMinutes: timeInterval,
blockInterval
Expand All @@ -153,6 +158,7 @@ export abstract class FuelBaseProcessorTemplate<TContract extends Contract> {
config: FuelFetchConfig = DEFAULT_FUEL_FETCH_CONFIG
) {
this.transactionHandlers.push({
handlerName: getHandlerName(),
handler,
fetchConfig: config
})
Expand Down
15 changes: 9 additions & 6 deletions packages/sdk/src/fuel/fuel-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ export class FuelProcessor<TContract extends Contract> implements FuelBaseProces

public onTransaction(
handler: (transaction: FuelTransaction, ctx: FuelContractContext<TContract>) => PromiseOrVoid,
config: FuelFetchConfig = DEFAULT_FUEL_FETCH_CONFIG
config: FuelFetchConfig = DEFAULT_FUEL_FETCH_CONFIG,
handlerName = getHandlerName()
) {
const callHandler = {
handlerName: getHandlerName(),
handlerName,
handler: async (call: Data_FuelTransaction) => {
const abiMap = this.config.abi
? {
Expand Down Expand Up @@ -181,12 +182,13 @@ export class FuelProcessor<TContract extends Contract> implements FuelBaseProces

public onLog<T>(
logIdFilter: string | string[],
handler: (logs: FuelLog<T>, ctx: FuelContractContext<TContract>) => PromiseOrVoid
handler: (logs: FuelLog<T>, ctx: FuelContractContext<TContract>) => PromiseOrVoid,
handlerName = getHandlerName()
) {
const logIds = new Set(Array.isArray(logIdFilter) ? logIdFilter : [logIdFilter])

const logHandler = {
handlerName: getHandlerName(),
handlerName,
handler: async ({ transaction, receiptIndex, timestamp }: Data_FuelReceipt) => {
try {
const tx = decodeFuelTransaction(transaction, this.provider)
Expand Down Expand Up @@ -272,7 +274,8 @@ export class FuelProcessor<TContract extends Contract> implements FuelBaseProces
public onInterval(
handler: (block: FuelBlock, ctx: FuelContractContext<TContract>) => PromiseOrVoid,
timeInterval: HandleInterval | undefined,
blockInterval: HandleInterval | undefined
blockInterval: HandleInterval | undefined,
handlerName = getHandlerName()
// fetchConfig: Partial<FuelFetchConfig> | undefined
): this {
if (timeInterval) {
Expand All @@ -286,7 +289,7 @@ export class FuelProcessor<TContract extends Contract> implements FuelBaseProces
this.blockHandlers.push({
blockInterval,
timeIntervalInMinutes: timeInterval,
handlerName: getHandlerName(),
handlerName,
handler: async function (data: Data_FuelBlock) {
const header = data.block
if (!header) {
Expand Down
13 changes: 11 additions & 2 deletions packages/sdk/src/sui/sui-object-processor-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import { TemplateInstanceState } from '../core/template.js'
import { SuiBindOptions } from './sui-processor.js'
import { TransactionFilter, accountAddressString } from '../move/index.js'
import { ServerError, Status } from 'nice-grpc'
import { proxyProcessor } from '../utils/metrics.js'
import { getHandlerName, proxyProcessor } from '../utils/metrics.js'

class ObjectHandler<HandlerType> {
type?: string
checkpointInterval?: HandleInterval
timeIntervalInMinutes?: HandleInterval
handlerName: string
handler: HandlerType
fetchConfig: MoveAccountFetchConfig
}
Expand Down Expand Up @@ -68,7 +69,14 @@ export abstract class SuiObjectOrAddressProcessorTemplate<

const processor = this.createProcessor(options)
for (const h of this.objectHandlers) {
processor.onInterval(h.handler, h.timeIntervalInMinutes, h.checkpointInterval, h.type, h.fetchConfig)
processor.onInterval(
h.handler,
h.timeIntervalInMinutes,
h.checkpointInterval,
h.type,
h.fetchConfig,
h.handlerName
)
}
const config = processor.config

Expand Down Expand Up @@ -162,6 +170,7 @@ export abstract class SuiObjectOrAddressProcessorTemplate<
fetchConfig: Partial<MoveAccountFetchConfig> | undefined
): this {
this.objectHandlers.push({
handlerName: getHandlerName(),
handler: handler,
timeIntervalInMinutes: timeInterval,
checkpointInterval: checkpointInterval,
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/sui/sui-object-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ export abstract class SuiBaseObjectOrAddressProcessor<HandlerType> {
timeInterval: HandleInterval | undefined,
checkpointInterval: HandleInterval | undefined,
type: string | undefined,
fetchConfig: Partial<MoveAccountFetchConfig> | undefined
fetchConfig: Partial<MoveAccountFetchConfig> | undefined,
handlerName = getHandlerName()
): this {
const processor = this
this.objectHandlers.push({
handlerName: getHandlerName(),
handlerName,
handler: async function (data) {
const ctx = new SuiObjectContext(
processor.config.network,
Expand Down
3 changes: 0 additions & 3 deletions packages/sdk/src/utils/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ export function getHandlerName() {
export function proxyProcessor(cls: any) {
return new Proxy(cls, {
get: (target, prop, receiver) => {
if (prop.toString() == 'bind') {
return Reflect.get(target, prop, receiver)
}
return metricsStorage.run(metricsStorage.getStore() || `${cls.constructor.name}.${prop.toString()}`, () => {
const fn = (target as any)[prop]
if (typeof fn == 'function') {
Expand Down

0 comments on commit ce1778e

Please sign in to comment.