Skip to content

Commit

Permalink
add loop to wait for finality
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Apr 15, 2024
1 parent 0ea9da4 commit 7a68ee8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/engine/paima-funnel/src/cde/minaGeneric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
38 changes: 24 additions & 14 deletions packages/engine/paima-funnel/src/funnels/mina/funnel.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<number> {
const row = await pg`select timestamp from blocks where height = 1;`;

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7a68ee8

Please sign in to comment.