Skip to content

Commit

Permalink
add timestamp checkpointing event
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Apr 23, 2024
1 parent 5e3ce4f commit f62b086
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 5 deletions.
39 changes: 35 additions & 4 deletions packages/engine/paima-funnel/src/funnels/mina/funnel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { doLog, logError, ChainDataExtensionType, delay, ENV } from '@paima/utils';
import {
doLog,
logError,
ChainDataExtensionType,
delay,
ENV,
InternalEventType,
} from '@paima/utils';
import type { ChainFunnel, ReadPresyncDataFrom } from '@paima/runtime';
import type {
ChainData,
Expand All @@ -11,7 +18,7 @@ import { BaseFunnel } from '../BaseFunnel.js';
import type { FunnelSharedData } from '../BaseFunnel.js';
import type { PoolClient } from 'pg';
import { FUNNEL_PRESYNC_FINISHED, ConfigNetworkType } from '@paima/utils';
import { getPaginationCursors } from '@paima/db';
import { getMinaCheckpoint, getPaginationCursors } from '@paima/db';
import { getActionCdeData, getEventCdeData } from '../../cde/minaGeneric.js';
import type { MinaConfig } from '@paima/utils';
import { MinaFunnelCacheEntry } from '../FunnelCache.js';
Expand Down Expand Up @@ -86,8 +93,10 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel {
await delay(delayForWaitingForFinalityLoop);
}

const fromTimestamp =
this.cache.getState().lastPoint?.timestamp || cachedState.startingSlotTimestamp;
const lastRoundTimestamp = this.cache.getState().lastPoint?.timestamp;
const fromTimestamp = lastRoundTimestamp
? lastRoundTimestamp + 1
: cachedState.startingSlotTimestamp;

const toTimestamp = maxBaseTimestamp;

Expand Down Expand Up @@ -159,6 +168,23 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel {

const composed = composeChainData(baseData, grouped);

for (const chainData of composed) {
if (!chainData.internalEvents) {
chainData.internalEvents = [];
}

chainData.internalEvents.push({
type: InternalEventType.MinaLastTimestamp,

timestamp: baseChainTimestampToMina(
chainData.timestamp,
this.config.confirmationDepth,
this.config.slotDuration
).toString(),
network: this.chainName,
});
}

return composed;
}

Expand Down Expand Up @@ -347,6 +373,11 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel {
});
});

const checkpoint = await getMinaCheckpoint.run({ network: chainName }, dbTx);
if (checkpoint.length > 0) {
newEntry.updateLastPoint(Number.parseInt(checkpoint[0].timestamp, 10));
}

return newEntry;
})();

Expand Down
10 changes: 10 additions & 0 deletions packages/engine/paima-sm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
NO_USER_ID,
updateCardanoEpoch,
updatePaginationCursor,
updateMinaCheckpoint,
} from '@paima/db';
import Prando from '@paima/prando';

Expand Down Expand Up @@ -488,6 +489,15 @@ async function processInternalEvents(
dbTx
);
break;
case InternalEventType.MinaLastTimestamp:
await updateMinaCheckpoint.run(
{
timestamp: event.timestamp,
network: event.network,
},
dbTx
);
break;
default:
assertNever(event);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/engine/paima-sm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,18 @@ export interface ChainData {
internalEvents?: InternalEvent[];
}

export type InternalEvent = CardanoEpochEvent | EvmLastBlockEvent;
export type InternalEvent = CardanoEpochEvent | EvmLastBlockEvent | MinaLastTimestampEvent;
export type CardanoEpochEvent = { type: InternalEventType.CardanoBestEpoch; epoch: number };
export type EvmLastBlockEvent = {
type: InternalEventType.EvmLastBlock;
block: number;
network: string;
};
export type MinaLastTimestampEvent = {
type: InternalEventType.MinaLastTimestamp;
timestamp: string;
network: string;
};

export interface EvmPresyncChainData {
network: string;
Expand Down
6 changes: 6 additions & 0 deletions packages/node-sdk/paima-db/migrations/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,9 @@ CREATE TABLE cde_cardano_mint_burn(
output_addresses JSONB NOT NULL,
PRIMARY KEY (cde_id, tx_id)
);

CREATE TABLE mina_checkpoint (
timestamp TEXT NOT NULL,
network TEXT NOT NULL,
PRIMARY KEY (network)
);
2 changes: 2 additions & 0 deletions packages/node-sdk/paima-db/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export type * from './sql/cde-cursor-tracking-pagination.queries.js';
export * from './sql/cde-cardano-transfer.queries.js';
export type * from './sql/cde-cardano-transfer.queries.js';
export { cdeCardanoMintBurnInsert } from './sql/cde-cardano-mint-burn.queries.js';
export type * from './sql/mina-checkpoints.queries.js';
export * from './sql/mina-checkpoints.queries.js';

export {
tx,
Expand Down
20 changes: 20 additions & 0 deletions packages/node-sdk/paima-db/src/paima-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,25 @@ const TABLE_DATA_DELEGATIONS: TableData = {
creationQuery: QUERY_CREATE_TABLE_DELEGATIONS,
};

const QUERY_CREATE_TABLE_MINA_CHECKPOINT = `
CREATE TABLE mina_checkpoint (
timestamp TEXT NOT NULL,
network TEXT NOT NULL,
PRIMARY KEY (network)
);
`;

const TABLE_DATA_MINA_CHECKPOINT: TableData = {
tableName: 'mina_checkpoint',
primaryKeyColumns: ['network'],
columnData: packTuples([
['timestamp', 'text', 'NO', ''],
['network', 'text', 'NO', ''],
]),
serialColumns: [],
creationQuery: QUERY_CREATE_TABLE_MINA_CHECKPOINT,
};

const FUNCTION_NOTIFY_WALLET_CONNECT: string = `
create or replace function public.notify_wallet_connect()
returns trigger
Expand Down Expand Up @@ -663,4 +682,5 @@ export const TABLES: TableData[] = [
TABLE_DATA_CDE_TRACKING_CURSOR_PAGINATION,
TABLE_DATA_CDE_CARDANO_TRANSFER,
TABLE_DATA_CDE_CARDANO_MINT_BURN,
TABLE_DATA_MINA_CHECKPOINT,
];
64 changes: 64 additions & 0 deletions packages/node-sdk/paima-db/src/sql/mina-checkpoints.queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/** Types generated for queries found in "src/sql/mina-checkpoints.sql" */
import { PreparedQuery } from '@pgtyped/runtime';

/** 'UpdateMinaCheckpoint' parameters type */
export interface IUpdateMinaCheckpointParams {
network: string;
timestamp: string;
}

/** 'UpdateMinaCheckpoint' return type */
export type IUpdateMinaCheckpointResult = void;

/** 'UpdateMinaCheckpoint' query type */
export interface IUpdateMinaCheckpointQuery {
params: IUpdateMinaCheckpointParams;
result: IUpdateMinaCheckpointResult;
}

const updateMinaCheckpointIR: any = {"usedParamSet":{"timestamp":true,"network":true},"params":[{"name":"timestamp","required":true,"transform":{"type":"scalar"},"locs":[{"a":71,"b":81},{"a":149,"b":159}]},{"name":"network","required":true,"transform":{"type":"scalar"},"locs":[{"a":88,"b":96}]}],"statement":"INSERT INTO mina_checkpoint(\n timestamp,\n network\n) VALUES (\n :timestamp!,\n :network!\n) \nON CONFLICT (network) DO\nUPDATE SET timestamp = :timestamp!"};

/**
* Query generated from SQL:
* ```
* INSERT INTO mina_checkpoint(
* timestamp,
* network
* ) VALUES (
* :timestamp!,
* :network!
* )
* ON CONFLICT (network) DO
* UPDATE SET timestamp = :timestamp!
* ```
*/
export const updateMinaCheckpoint = new PreparedQuery<IUpdateMinaCheckpointParams,IUpdateMinaCheckpointResult>(updateMinaCheckpointIR);


/** 'GetMinaCheckpoint' parameters type */
export interface IGetMinaCheckpointParams {
network: string;
}

/** 'GetMinaCheckpoint' return type */
export interface IGetMinaCheckpointResult {
timestamp: string;
}

/** 'GetMinaCheckpoint' query type */
export interface IGetMinaCheckpointQuery {
params: IGetMinaCheckpointParams;
result: IGetMinaCheckpointResult;
}

const getMinaCheckpointIR: any = {"usedParamSet":{"network":true},"params":[{"name":"network","required":true,"transform":{"type":"scalar"},"locs":[{"a":54,"b":62}]}],"statement":"SELECT timestamp FROM mina_checkpoint WHERE network = :network! LIMIT 1"};

/**
* Query generated from SQL:
* ```
* SELECT timestamp FROM mina_checkpoint WHERE network = :network! LIMIT 1
* ```
*/
export const getMinaCheckpoint = new PreparedQuery<IGetMinaCheckpointParams,IGetMinaCheckpointResult>(getMinaCheckpointIR);


13 changes: 13 additions & 0 deletions packages/node-sdk/paima-db/src/sql/mina-checkpoints.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* @name updateMinaCheckpoint */
INSERT INTO mina_checkpoint(
timestamp,
network
) VALUES (
:timestamp!,
:network!
)
ON CONFLICT (network) DO
UPDATE SET timestamp = :timestamp!;

/* @name getMinaCheckpoint */
SELECT timestamp FROM mina_checkpoint WHERE network = :network! LIMIT 1;
1 change: 1 addition & 0 deletions packages/paima-sdk/paima-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ export const FUNNEL_PRESYNC_FINISHED = 'finished';
export const enum InternalEventType {
CardanoBestEpoch,
EvmLastBlock,
MinaLastTimestamp,
}

0 comments on commit f62b086

Please sign in to comment.