Skip to content

Commit

Permalink
presync checkpointing for cardano cde
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Nov 24, 2023
1 parent e5a4b25 commit 94ddeaf
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 66 deletions.
12 changes: 10 additions & 2 deletions packages/engine/paima-runtime/src/runtime-loops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async function runPresync(
}

// eslint-disable-next-line @typescript-eslint/no-loop-func
let newHeight = (await tx)(gameStateMachine.getReadWriteDbConn(), async dbTx => {
let newHeight = tx(gameStateMachine.getReadWriteDbConn(), async dbTx => {
const chainFunnel = await funnelFactory.generateFunnel(dbTx);
return await runPresyncRound(
gameStateMachine,
Expand Down Expand Up @@ -162,8 +162,16 @@ async function runPresyncRound(
const filteredPresyncDataList = Object.values(latestPresyncDataList)
.flatMap(data => (data !== FUNNEL_PRESYNC_FINISHED ? data : []))
.filter(unit => unit.extensionDatums.length > 0);

const dbTx = chainFunnel.getDbTx();

for (const presyncData of filteredPresyncDataList) {
await gameStateMachine.presyncProcess(chainFunnel.getDbTx(), presyncData);
await gameStateMachine.presyncProcess(dbTx, presyncData);
}

const cardanoFrom = from.find(arg => arg.network === Network.CARDANO);
if (cardanoFrom) {
await gameStateMachine.markCardanoPresyncMilestone(dbTx, cardanoFrom.to);
}

return Object.fromEntries(
Expand Down
1 change: 1 addition & 0 deletions packages/engine/paima-runtime/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ export interface GameStateMachine {
process: (dbTx: PoolClient, chainData: ChainData) => Promise<void>;
presyncProcess: (dbTx: PoolClient, latestCdeData: PresyncChainData) => Promise<void>;
markPresyncMilestone: (blockHeight: number) => Promise<void>;
markCardanoPresyncMilestone: (dbTx: PoolClient, slot: number) => Promise<void>;
dryRun: (gameInput: string, userAddress: string) => Promise<boolean>;
}

Expand Down
4 changes: 1 addition & 3 deletions packages/engine/paima-sm/src/cde-cardano-pool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { Pool } from 'pg';

import { doLog, ENV } from '@paima/utils';
import { ENV } from '@paima/utils';
import type { CdeCardanoPoolDatum } from '@paima/runtime';
import { createScheduledData, cdeCardanoPoolInsertData } from '@paima/db';
import type { SQLUpdate } from '@paima/db';
Expand Down
7 changes: 5 additions & 2 deletions packages/engine/paima-sm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
saveLastBlockHeight,
markCdeBlockheightProcessed,
getLatestProcessedCdeBlockheight,
getCardanoLatestProcessedCdeBlockheight,
getCardanoLatestProcessedCdeSlot,
markCardanoCdeSlotProcessed,
} from '@paima/db';
import Prando from '@paima/prando';
Expand Down Expand Up @@ -60,7 +60,7 @@ const SM: GameStateMachineInitializer = {
getPresyncCardanoSlotHeight: async (
dbTx: PoolClient | Pool = readonlyDBConn
): Promise<number> => {
const [b] = await getCardanoLatestProcessedCdeBlockheight.run(undefined, dbTx);
const [b] = await getCardanoLatestProcessedCdeSlot.run(undefined, dbTx);
const slot = b?.slot ?? 0;
return slot;
},
Expand Down Expand Up @@ -108,6 +108,9 @@ const SM: GameStateMachineInitializer = {
): Promise<void> => {
await markCdeBlockheightProcessed.run({ block_height: blockHeight }, dbTx);
},
markCardanoPresyncMilestone: async (dbTx: PoolClient, slot: number): Promise<void> => {
await markCardanoCdeSlotProcessed.run({ slot: slot }, dbTx);
},
dryRun: async (
gameInput: string,
userAddress: string,
Expand Down
3 changes: 2 additions & 1 deletion packages/node-sdk/paima-db/migrations/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ CREATE TABLE cde_tracking (
);

CREATE TABLE cde_tracking_cardano (
slot INTEGER PRIMARY KEY
id INTEGER PRIMARY KEY,
slot INTEGER NOT NULL
);

CREATE TABLE chain_data_extensions (
Expand Down
10 changes: 7 additions & 3 deletions packages/node-sdk/paima-db/src/paima-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,18 @@ const TABLE_DATA_CDE_TRACKING: TableData = {

const QUERY_CREATE_TABLE_CDE_TRACKING_CARDANO = `
CREATE TABLE cde_tracking_cardano (
slot INTEGER PRIMARY KEY
id INTEGER PRIMARY KEY,
slot INTEGER NOT NULL
);
`;

const TABLE_DATA_CDE_TRACKING_CARDANO: TableData = {
tableName: 'cde_tracking_cardano',
primaryKeyColumns: ['slot'],
columnData: packTuples([['slot', 'integer', 'NO', '']]),
primaryKeyColumns: ['id'],
columnData: packTuples([
['id', 'integer', 'NO', ''],
['slot', 'integer', 'NO', ''],
]),
serialColumns: [],
creationQuery: QUERY_CREATE_TABLE_CDE_TRACKING_CARDANO,
};
Expand Down
62 changes: 17 additions & 45 deletions packages/node-sdk/paima-db/src/sql/cde-tracking-cardano.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,70 +15,42 @@ export interface IMarkCardanoCdeSlotProcessedQuery {
result: IMarkCardanoCdeSlotProcessedResult;
}

const markCardanoCdeSlotProcessedIR: any = {"usedParamSet":{"slot":true},"params":[{"name":"slot","required":true,"transform":{"type":"scalar"},"locs":[{"a":47,"b":52}]}],"statement":"INSERT INTO cde_tracking_cardano(slot)\nVALUES (:slot!)"};
const markCardanoCdeSlotProcessedIR: any = {"usedParamSet":{"slot":true},"params":[{"name":"slot","required":true,"transform":{"type":"scalar"},"locs":[{"a":53,"b":58},{"a":100,"b":105}]}],"statement":"INSERT INTO cde_tracking_cardano(id,slot)\nVALUES (0, :slot!)\nON CONFLICT (id) \nDO UPDATE SET slot = :slot!"};

/**
* Query generated from SQL:
* ```
* INSERT INTO cde_tracking_cardano(slot)
* VALUES (:slot!)
* INSERT INTO cde_tracking_cardano(id,slot)
* VALUES (0, :slot!)
* ON CONFLICT (id)
* DO UPDATE SET slot = :slot!
* ```
*/
export const markCardanoCdeSlotProcessed = new PreparedQuery<IMarkCardanoCdeSlotProcessedParams,IMarkCardanoCdeSlotProcessedResult>(markCardanoCdeSlotProcessedIR);


/** 'GetCardanoSpecificCdeBlockheight' parameters type */
export interface IGetCardanoSpecificCdeBlockheightParams {
slot: number;
}

/** 'GetCardanoSpecificCdeBlockheight' return type */
export interface IGetCardanoSpecificCdeBlockheightResult {
slot: number;
}
/** 'GetCardanoLatestProcessedCdeSlot' parameters type */
export type IGetCardanoLatestProcessedCdeSlotParams = void;

/** 'GetCardanoSpecificCdeBlockheight' query type */
export interface IGetCardanoSpecificCdeBlockheightQuery {
params: IGetCardanoSpecificCdeBlockheightParams;
result: IGetCardanoSpecificCdeBlockheightResult;
}

const getCardanoSpecificCdeBlockheightIR: any = {"usedParamSet":{"slot":true},"params":[{"name":"slot","required":true,"transform":{"type":"scalar"},"locs":[{"a":48,"b":53}]}],"statement":"SELECT * FROM cde_tracking_cardano\nWHERE slot = :slot!"};

/**
* Query generated from SQL:
* ```
* SELECT * FROM cde_tracking_cardano
* WHERE slot = :slot!
* ```
*/
export const getCardanoSpecificCdeBlockheight = new PreparedQuery<IGetCardanoSpecificCdeBlockheightParams,IGetCardanoSpecificCdeBlockheightResult>(getCardanoSpecificCdeBlockheightIR);


/** 'GetCardanoLatestProcessedCdeBlockheight' parameters type */
export type IGetCardanoLatestProcessedCdeBlockheightParams = void;

/** 'GetCardanoLatestProcessedCdeBlockheight' return type */
export interface IGetCardanoLatestProcessedCdeBlockheightResult {
slot: number;
/** 'GetCardanoLatestProcessedCdeSlot' return type */
export interface IGetCardanoLatestProcessedCdeSlotResult {
slot: number | null;
}

/** 'GetCardanoLatestProcessedCdeBlockheight' query type */
export interface IGetCardanoLatestProcessedCdeBlockheightQuery {
params: IGetCardanoLatestProcessedCdeBlockheightParams;
result: IGetCardanoLatestProcessedCdeBlockheightResult;
/** 'GetCardanoLatestProcessedCdeSlot' query type */
export interface IGetCardanoLatestProcessedCdeSlotQuery {
params: IGetCardanoLatestProcessedCdeSlotParams;
result: IGetCardanoLatestProcessedCdeSlotResult;
}

const getCardanoLatestProcessedCdeBlockheightIR: any = {"usedParamSet":{},"params":[],"statement":"SELECT * FROM cde_tracking_cardano\nORDER BY slot DESC\nLIMIT 1"};
const getCardanoLatestProcessedCdeSlotIR: any = {"usedParamSet":{},"params":[],"statement":"SELECT slot FROM cde_tracking_cardano LIMIT 1"};

/**
* Query generated from SQL:
* ```
* SELECT * FROM cde_tracking_cardano
* ORDER BY slot DESC
* LIMIT 1
* SELECT slot FROM cde_tracking_cardano LIMIT 1
* ```
*/
export const getCardanoLatestProcessedCdeBlockheight = new PreparedQuery<IGetCardanoLatestProcessedCdeBlockheightParams,IGetCardanoLatestProcessedCdeBlockheightResult>(getCardanoLatestProcessedCdeBlockheightIR);
export const getCardanoLatestProcessedCdeSlot = new PreparedQuery<IGetCardanoLatestProcessedCdeSlotParams,IGetCardanoLatestProcessedCdeSlotResult>(getCardanoLatestProcessedCdeSlotIR);


16 changes: 6 additions & 10 deletions packages/node-sdk/paima-db/src/sql/cde-tracking-cardano.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/* @name markCardanoCdeSlotProcessed */
INSERT INTO cde_tracking_cardano(slot)
VALUES (:slot!);
INSERT INTO cde_tracking_cardano(id,slot)
VALUES (0, :slot!)
ON CONFLICT (id)
DO UPDATE SET slot = :slot!;

/* @name getCardanoSpecificCdeBlockheight */
SELECT * FROM cde_tracking_cardano
WHERE slot = :slot!;

/* @name getCardanoLatestProcessedCdeBlockheight */
SELECT * FROM cde_tracking_cardano
ORDER BY slot DESC
LIMIT 1;
/* @name getCardanoLatestProcessedCdeSlot */
SELECT slot FROM cde_tracking_cardano LIMIT 1;

0 comments on commit 94ddeaf

Please sign in to comment.