From 24546d0bc2c1ef62f2323d8df9bba570411f5ee0 Mon Sep 17 00:00:00 2001 From: Enzo Cioppettini <48031343+ecioppettini@users.noreply.github.com> Date: Mon, 29 Apr 2024 01:14:07 -0300 Subject: [PATCH] allow overriding archive's confirmation parameters --- .../paima-funnel/src/cde/minaGeneric.ts | 2 +- .../paima-funnel/src/funnels/mina/funnel.ts | 50 +++++++++++++------ .../paima-utils/src/config/loading.ts | 7 ++- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/packages/engine/paima-funnel/src/cde/minaGeneric.ts b/packages/engine/paima-funnel/src/cde/minaGeneric.ts index 5488cb97..149cd32f 100644 --- a/packages/engine/paima-funnel/src/cde/minaGeneric.ts +++ b/packages/engine/paima-funnel/src/cde/minaGeneric.ts @@ -170,7 +170,7 @@ function canonicalChainCTE( FROM blocks b WHERE - chain_status = 'canonical' + 1=1 ${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``} diff --git a/packages/engine/paima-funnel/src/funnels/mina/funnel.ts b/packages/engine/paima-funnel/src/funnels/mina/funnel.ts index 7583c178..855c8521 100644 --- a/packages/engine/paima-funnel/src/funnels/mina/funnel.ts +++ b/packages/engine/paima-funnel/src/funnels/mina/funnel.ts @@ -26,20 +26,39 @@ import postgres from 'postgres'; const delayForWaitingForFinalityLoop = 1000; -async function findMinaConfirmedTimestamp(pg: postgres.Sql): Promise { - const row = - await pg`select timestamp from blocks where chain_status = 'canonical' order by height desc limit 1;`; +async function findMinaConfirmedTimestamp( + pg: postgres.Sql, + confirmationDepth?: number, +): Promise { + let row; + if (confirmationDepth) { + row = await pg` + WITH RECURSIVE chain AS ( + (SELECT parent_id, id, timestamp, height FROM blocks b WHERE height = (select MAX(height) from blocks) + ORDER BY timestamp ASC + LIMIT 1) + UNION ALL + SELECT b.parent_id, b.id, b.timestamp, b.height FROM blocks b + INNER JOIN chain + ON b.id = chain.parent_id AND chain.id <> chain.parent_id + ) SELECT timestamp FROM chain c + LIMIT 1 + OFFSET ${confirmationDepth}; + `; + } else { + row = + await pg`select timestamp from blocks where chain_status = 'canonical' order by height desc limit 1;`; + } - return Number.parseInt(row[0]['timestamp'], 10); + return Number.parseInt(row[0]["timestamp"], 10); } // mina timestamps are in milliseconds, while evm timestamps are in seconds. function baseChainTimestampToMina( baseChainTimestamp: number, - confirmationDepth: number, - slotDuration: number + delay: number, ): number { - return Math.max(baseChainTimestamp * 1000 - slotDuration * 1000 * confirmationDepth, 0); + return Math.max((baseChainTimestamp - delay) * 1000, 0); } export class MinaFunnel extends BaseFunnel implements ChainFunnel { @@ -73,12 +92,14 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel { const maxBaseTimestamp = baseChainTimestampToMina( baseData[baseData.length - 1].timestamp, - this.config.confirmationDepth, - this.config.slotDuration + this.config.delay, ); while (true) { - const confirmedTimestamp = await findMinaConfirmedTimestamp(cachedState.pg); + const confirmedTimestamp = await findMinaConfirmedTimestamp( + cachedState.pg, + this.config.confirmationDepth, + ); if (confirmedTimestamp >= maxBaseTimestamp) { break; @@ -99,8 +120,7 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel { state.curr < baseData.length && baseChainTimestampToMina( baseData[state.curr].timestamp, - this.config.confirmationDepth, - this.config.slotDuration + this.config.delay, ) <= ts ) state.curr++; @@ -174,8 +194,7 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel { timestamp: baseChainTimestampToMina( chainData.timestamp, - this.config.confirmationDepth, - this.config.slotDuration + this.config.delay, ).toString(), network: this.chainName, }); @@ -342,8 +361,7 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel { const minaTimestamp = baseChainTimestampToMina( startingBlockTimestamp, - config.confirmationDepth, - config.slotDuration + config.delay, ); newEntry.updateStartingTimestamp(minaTimestamp, pg); diff --git a/packages/paima-sdk/paima-utils/src/config/loading.ts b/packages/paima-sdk/paima-utils/src/config/loading.ts index 1a42c519..18e68611 100644 --- a/packages/paima-sdk/paima-utils/src/config/loading.ts +++ b/packages/paima-sdk/paima-utils/src/config/loading.ts @@ -63,10 +63,9 @@ export type CardanoConfig = Static; export const MinaConfigSchema = Type.Object({ archiveConnectionString: Type.String(), - // k - confirmationDepth: Type.Number(), + delay: Type.Number(), paginationLimit: Type.Number({ default: 50 }), - slotDuration: Type.Number(), + confirmationDepth: Type.Optional(Type.Number()), }); export type MinaConfig = Static; @@ -135,7 +134,7 @@ const cardanoConfigDefaults = { const minaConfigDefaults = { // lightnet defaults confirmationDepth: 30, - slotDuration: 20, + delay: 30 * 40, }; // used as a placeholder name for the ENV fallback mechanism