From edb39d26aa2ad1a8fc88dcfeac94779b566fa4ff Mon Sep 17 00:00:00 2001 From: Enzo Cioppettini <48031343+ecioppettini@users.noreply.github.com> Date: Thu, 18 Apr 2024 13:33:47 -0300 Subject: [PATCH] replace startSlot for startBlockHeight in the extensions --- .../paima-funnel/src/cde/minaGeneric.ts | 36 +++++++++++----- .../paima-funnel/src/funnels/FunnelCache.ts | 2 +- .../paima-funnel/src/funnels/mina/funnel.ts | 41 ++++--------------- packages/engine/paima-sm/src/types.ts | 4 +- 4 files changed, 37 insertions(+), 46 deletions(-) diff --git a/packages/engine/paima-funnel/src/cde/minaGeneric.ts b/packages/engine/paima-funnel/src/cde/minaGeneric.ts index 7421a094..5488cb97 100644 --- a/packages/engine/paima-funnel/src/cde/minaGeneric.ts +++ b/packages/engine/paima-funnel/src/cde/minaGeneric.ts @@ -17,6 +17,7 @@ export async function getEventCdeData(args: { isPresync: boolean; cursor?: string; limit?: number; + fromBlockHeight?: number; }): Promise<(CdeMinaActionGenericDatum | CdeMinaEventGenericDatum)[]> { return getCdeData( getEventsQuery, @@ -29,7 +30,8 @@ export async function getEventCdeData(args: { args.network, args.isPresync, args.cursor, - args.limit + args.limit, + args.fromBlockHeight ); } @@ -43,6 +45,7 @@ export async function getActionCdeData(args: { isPresync: boolean; cursor?: string; limit?: number; + fromBlockHeight?: number; }): Promise<(CdeMinaActionGenericDatum | CdeMinaEventGenericDatum)[]> { return getCdeData( getActionsQuery, @@ -55,7 +58,8 @@ export async function getActionCdeData(args: { args.network, args.isPresync, args.cursor, - args.limit + args.limit, + args.fromBlockHeight ); } @@ -72,7 +76,8 @@ export async function getCdeData( network: string, isPresync: boolean, cursor?: string, - limit?: number + limit?: number, + fromBlockHeight?: number ): Promise<(CdeMinaActionGenericDatum | CdeMinaEventGenericDatum)[]> { const result = [] as (CdeMinaActionGenericDatum | CdeMinaEventGenericDatum)[]; @@ -83,7 +88,8 @@ export async function getCdeData( toTimestamp.toString(), fromTimestamp.toString(), cursor, - limit?.toString() + limit?.toString(), + fromBlockHeight?.toString() ); const grouped = groupByTx(unmapped); @@ -151,9 +157,14 @@ function groupByTx(events: postgres.RowList) { return grouped; } -function fullChainCTE(db_client: postgres.Sql, toTimestamp?: string, fromTimestamp?: string) { +function canonicalChainCTE( + db_client: postgres.Sql, + toTimestamp?: string, + fromTimestamp?: string, + fromBlockHeight?: string +) { return db_client` - full_chain AS ( + canonical_chain AS ( SELECT id, state_hash, height, global_slot_since_genesis, timestamp FROM @@ -162,6 +173,7 @@ function fullChainCTE(db_client: postgres.Sql, toTimestamp?: string, fromTimesta chain_status = 'canonical' ${fromTimestamp ? db_client`AND b.timestamp::decimal >= ${fromTimestamp}::decimal` : db_client``} ${toTimestamp ? db_client`AND b.timestamp::decimal <= ${toTimestamp}::decimal` : db_client``} + ${fromBlockHeight ? db_client`AND b.height::decimal >= ${fromBlockHeight}::decimal` : db_client``} ORDER BY height ) `; @@ -196,7 +208,7 @@ function blocksAccessedCTE(db_client: postgres.Sql) { FROM account_identifier ai INNER JOIN accounts_accessed aa ON ai.requesting_zkapp_account_identifier_id = aa.account_identifier_id - INNER JOIN full_chain b ON aa.block_id = b.id + INNER JOIN canonical_chain b ON aa.block_id = b.id )`; } @@ -290,11 +302,12 @@ export function getEventsQuery( toTimestamp?: string, fromTimestamp?: string, after?: string, - limit?: string + limit?: string, + fromBlockHeight?: string ) { return db_client` WITH - ${fullChainCTE(db_client, toTimestamp, fromTimestamp)}, + ${canonicalChainCTE(db_client, toTimestamp, fromTimestamp, fromBlockHeight)}, ${accountIdentifierCTE(db_client, address)}, ${blocksAccessedCTE(db_client)}, ${emittedZkAppCommandsCTE(db_client, after)}, @@ -329,11 +342,12 @@ export function getActionsQuery( toTimestamp?: string, fromTimestamp?: string, after?: string, - limit?: string + limit?: string, + fromBlockHeight?: string ) { return db_client` WITH - ${fullChainCTE(db_client, toTimestamp, fromTimestamp)}, + ${canonicalChainCTE(db_client, toTimestamp, fromTimestamp, fromBlockHeight)}, ${accountIdentifierCTE(db_client, address)}, ${blocksAccessedCTE(db_client)}, ${emittedZkAppCommandsCTE(db_client, after)}, diff --git a/packages/engine/paima-funnel/src/funnels/FunnelCache.ts b/packages/engine/paima-funnel/src/funnels/FunnelCache.ts index 3c9ba1a8..df778c91 100644 --- a/packages/engine/paima-funnel/src/funnels/FunnelCache.ts +++ b/packages/engine/paima-funnel/src/funnels/FunnelCache.ts @@ -197,7 +197,7 @@ export class MinaFunnelCacheEntry implements FunnelCacheEntry { private state: MinaFunnelCacheEntryState | null = null; public static readonly SYMBOL = Symbol('MinaFunnelStartingSlot'); - public updateStartingSlot( + public updateStartingTimestamp( startingSlotTimestamp: number, genesisTime: number, pg: postgres.Sql diff --git a/packages/engine/paima-funnel/src/funnels/mina/funnel.ts b/packages/engine/paima-funnel/src/funnels/mina/funnel.ts index d278ab60..b2ea34e9 100644 --- a/packages/engine/paima-funnel/src/funnels/mina/funnel.ts +++ b/packages/engine/paima-funnel/src/funnels/mina/funnel.ts @@ -32,14 +32,7 @@ async function findMinaConfirmedTimestamp(pg: postgres.Sql): Promise { return Number.parseInt(row[0]['timestamp'], 10); } -function slotToMinaTimestamp(slot: number, genesisTime: number, slotDuration: number): number { - return slot * slotDuration * 1000 + genesisTime; -} - -function minaTimestampToSlot(ts: number, genesisTime: number, slotDuration: number): number { - return Math.max(Math.floor((ts - genesisTime) / (slotDuration * 1000)), 0); -} - +// mina timestamps are in milliseconds, while evm timestamps are in seconds. function baseChainTimestampToMina( baseChainTimestamp: number, confirmationDepth: number, @@ -226,18 +219,13 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel { }) .map(async extension => { if (extension.cdeType === ChainDataExtensionType.MinaEventGeneric) { - let fromTimestamp = slotToMinaTimestamp( - extension.startSlot, - cache.genesisTime, - this.config.slotDuration - ); - const cursor = cursors && cursors[extension.cdeId]; const data = await getEventCdeData({ pg: cache.pg, extension, - fromTimestamp, + fromTimestamp: 0, + fromBlockHeight: extension.startBlockHeight, toTimestamp: startingSlotTimestamp - 1, // the handler for this cde stores the block height unmodified // (even if the event is scheduled at the correct height), so @@ -256,18 +244,13 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel { data, }; } else if (extension.cdeType === ChainDataExtensionType.MinaActionGeneric) { - let fromTimestamp = slotToMinaTimestamp( - extension.startSlot, - cache.genesisTime, - this.config.slotDuration - ); - const cursor = cursors && cursors[extension.cdeId]; const data = await getActionCdeData({ pg: cache.pg, extension, - fromTimestamp, + fromTimestamp: 0, + fromBlockHeight: extension.startBlockHeight, toTimestamp: startingSlotTimestamp - 1, getBlockNumber: _x => ENV.SM_START_BLOCKHEIGHT + 1, network: this.chainName, @@ -337,19 +320,13 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel { const startingBlockTimestamp = (await sharedData.web3.eth.getBlock(startingBlockHeight)) .timestamp as number; - const slot = minaTimestampToSlot( - baseChainTimestampToMina( - startingBlockTimestamp, - config.confirmationDepth, - config.slotDuration - ), - genesisTime, + const minaTimestamp = baseChainTimestampToMina( + startingBlockTimestamp, + config.confirmationDepth, config.slotDuration ); - const slotAsMinaTimestamp = slotToMinaTimestamp(slot, genesisTime, config.slotDuration); - - newEntry.updateStartingSlot(slotAsMinaTimestamp, genesisTime, pg); + newEntry.updateStartingTimestamp(minaTimestamp, genesisTime, pg); const cursors = await getPaginationCursors.run(undefined, dbTx); diff --git a/packages/engine/paima-sm/src/types.ts b/packages/engine/paima-sm/src/types.ts index 8ba80c0f..91dc35e0 100644 --- a/packages/engine/paima-sm/src/types.ts +++ b/packages/engine/paima-sm/src/types.ts @@ -455,7 +455,7 @@ export const ChainDataExtensionMinaEventGenericConfig = Type.Object({ type: Type.Literal(CdeEntryTypeName.MinaEventGeneric), address: Type.String(), scheduledPrefix: Type.String(), - startSlot: Type.Number(), + startBlockHeight: Type.Number(), name: Type.String(), }); @@ -471,7 +471,7 @@ export const ChainDataExtensionMinaActionGenericConfig = Type.Object({ type: Type.Literal(CdeEntryTypeName.MinaActionGeneric), address: Type.String(), scheduledPrefix: Type.String(), - startSlot: Type.Number(), + startBlockHeight: Type.Number(), name: Type.String(), });