From 7a68ee82343e9cb315790c3748a986e6b6155042 Mon Sep 17 00:00:00 2001 From: Enzo Cioppettini <48031343+ecioppettini@users.noreply.github.com> Date: Mon, 15 Apr 2024 20:44:11 -0300 Subject: [PATCH] add loop to wait for finality --- .../paima-funnel/src/cde/minaGeneric.ts | 2 +- .../paima-funnel/src/funnels/mina/funnel.ts | 38 ++++++++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/engine/paima-funnel/src/cde/minaGeneric.ts b/packages/engine/paima-funnel/src/cde/minaGeneric.ts index 22912e8a..5c28fdf1 100644 --- a/packages/engine/paima-funnel/src/cde/minaGeneric.ts +++ b/packages/engine/paima-funnel/src/cde/minaGeneric.ts @@ -75,7 +75,7 @@ export async function getCdeData( cursor?: string, limit?: number ): Promise<(CdeMinaActionGenericDatum | CdeMinaEventGenericDatum)[]> { - const result = [] as ChainDataExtensionDatum[]; + const result = [] as (CdeMinaActionGenericDatum | CdeMinaEventGenericDatum)[]; console.log('outside', cursor, limit, fromTimestamp, toTimestamp); while (true) { diff --git a/packages/engine/paima-funnel/src/funnels/mina/funnel.ts b/packages/engine/paima-funnel/src/funnels/mina/funnel.ts index 6f149767..924c4789 100644 --- a/packages/engine/paima-funnel/src/funnels/mina/funnel.ts +++ b/packages/engine/paima-funnel/src/funnels/mina/funnel.ts @@ -1,4 +1,4 @@ -import { doLog, logError, ChainDataExtensionType } from '@paima/utils'; +import { doLog, logError, ChainDataExtensionType, delay } from '@paima/utils'; import type { ChainFunnel, ReadPresyncDataFrom } from '@paima/runtime'; import type { ChainData, @@ -17,6 +17,8 @@ import type { MinaConfig } from '@paima/utils'; import { MinaFunnelCacheEntry } from '../FunnelCache.js'; import postgres from 'postgres'; +const delayForWaitingForFinalityLoop = 1000; + async function getGenesisTime(pg: postgres.Sql): Promise { const row = await pg`select timestamp from blocks where height = 1;`; @@ -75,25 +77,33 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel { let cachedState = this.cache.getState(); - const confirmedSlot = await findMinaConfirmedSlot(cachedState.pg); - - const confirmedTimestamp = slotToMinaTimestamp( - confirmedSlot, - cachedState.genesisTime, + const maxBaseTimestamp = baseChainTimestampToMina( + baseData[baseData.length - 1].timestamp, + this.config.confirmationDepth, this.config.slotDuration ); + let confirmedTimestamp; + while (true) { + const confirmedSlot = await findMinaConfirmedSlot(cachedState.pg); + + confirmedTimestamp = slotToMinaTimestamp( + confirmedSlot, + cachedState.genesisTime, + this.config.slotDuration + ); + + if (confirmedTimestamp >= maxBaseTimestamp) { + break; + } + + await delay(delayForWaitingForFinalityLoop); + } + const fromTimestamp = this.cache.getState().lastPoint?.timestamp || cachedState.startingSlotTimestamp; - const toTimestamp = Math.max( - confirmedTimestamp, - baseChainTimestampToMina( - baseData[baseData.length - 1].timestamp, - this.config.confirmationDepth, - this.config.slotDuration - ) - ); + const toTimestamp = Math.max(confirmedTimestamp, maxBaseTimestamp); const fromSlot = minaTimestampToSlot( fromTimestamp,