Skip to content

Commit

Permalink
allow overriding archive's confirmation parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Apr 29, 2024
1 parent 3ef89e8 commit 24546d0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 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 @@ -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``}
Expand Down
50 changes: 34 additions & 16 deletions packages/engine/paima-funnel/src/funnels/mina/funnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,39 @@ import postgres from 'postgres';

const delayForWaitingForFinalityLoop = 1000;

async function findMinaConfirmedTimestamp(pg: postgres.Sql): Promise<number> {
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<number> {
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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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++;
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions packages/paima-sdk/paima-utils/src/config/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ export type CardanoConfig = Static<typeof CardanoConfigSchema>;

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<typeof MinaConfigSchema>;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 24546d0

Please sign in to comment.