Skip to content

Commit

Permalink
replace startSlot for startBlockHeight in the extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Apr 18, 2024
1 parent 4757360 commit edb39d2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 46 deletions.
36 changes: 25 additions & 11 deletions packages/engine/paima-funnel/src/cde/minaGeneric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export async function getEventCdeData(args: {
isPresync: boolean;
cursor?: string;
limit?: number;
fromBlockHeight?: number;
}): Promise<(CdeMinaActionGenericDatum | CdeMinaEventGenericDatum)[]> {
return getCdeData(
getEventsQuery,
Expand All @@ -29,7 +30,8 @@ export async function getEventCdeData(args: {
args.network,
args.isPresync,
args.cursor,
args.limit
args.limit,
args.fromBlockHeight
);
}

Expand All @@ -43,6 +45,7 @@ export async function getActionCdeData(args: {
isPresync: boolean;
cursor?: string;
limit?: number;
fromBlockHeight?: number;
}): Promise<(CdeMinaActionGenericDatum | CdeMinaEventGenericDatum)[]> {
return getCdeData(
getActionsQuery,
Expand All @@ -55,7 +58,8 @@ export async function getActionCdeData(args: {
args.network,
args.isPresync,
args.cursor,
args.limit
args.limit,
args.fromBlockHeight
);
}

Expand All @@ -72,7 +76,8 @@ export async function getCdeData(
network: string,
isPresync: boolean,
cursor?: string,
limit?: number
limit?: number,
fromBlockHeight?: number
): Promise<(CdeMinaActionGenericDatum | CdeMinaEventGenericDatum)[]> {
const result = [] as (CdeMinaActionGenericDatum | CdeMinaEventGenericDatum)[];

Expand All @@ -83,7 +88,8 @@ export async function getCdeData(
toTimestamp.toString(),
fromTimestamp.toString(),
cursor,
limit?.toString()
limit?.toString(),
fromBlockHeight?.toString()
);

const grouped = groupByTx(unmapped);
Expand Down Expand Up @@ -151,9 +157,14 @@ function groupByTx(events: postgres.RowList<PerBlock[]>) {
return grouped;
}

function fullChainCTE(db_client: postgres.Sql, toTimestamp?: string, fromTimestamp?: string) {
function canonicalChainCTE(
db_client: postgres.Sql,
toTimestamp?: string,
fromTimestamp?: string,
fromBlockHeight?: string
) {
return db_client`
full_chain AS (
canonical_chain AS (
SELECT
id, state_hash, height, global_slot_since_genesis, timestamp
FROM
Expand All @@ -162,6 +173,7 @@ function fullChainCTE(db_client: postgres.Sql, toTimestamp?: string, fromTimesta
chain_status = 'canonical'
${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``}
ORDER BY height
)
`;
Expand Down Expand Up @@ -196,7 +208,7 @@ function blocksAccessedCTE(db_client: postgres.Sql) {
FROM
account_identifier ai
INNER JOIN accounts_accessed aa ON ai.requesting_zkapp_account_identifier_id = aa.account_identifier_id
INNER JOIN full_chain b ON aa.block_id = b.id
INNER JOIN canonical_chain b ON aa.block_id = b.id
)`;
}

Expand Down Expand Up @@ -290,11 +302,12 @@ export function getEventsQuery(
toTimestamp?: string,
fromTimestamp?: string,
after?: string,
limit?: string
limit?: string,
fromBlockHeight?: string
) {
return db_client<PerBlock[]>`
WITH
${fullChainCTE(db_client, toTimestamp, fromTimestamp)},
${canonicalChainCTE(db_client, toTimestamp, fromTimestamp, fromBlockHeight)},
${accountIdentifierCTE(db_client, address)},
${blocksAccessedCTE(db_client)},
${emittedZkAppCommandsCTE(db_client, after)},
Expand Down Expand Up @@ -329,11 +342,12 @@ export function getActionsQuery(
toTimestamp?: string,
fromTimestamp?: string,
after?: string,
limit?: string
limit?: string,
fromBlockHeight?: string
) {
return db_client<PerBlock[]>`
WITH
${fullChainCTE(db_client, toTimestamp, fromTimestamp)},
${canonicalChainCTE(db_client, toTimestamp, fromTimestamp, fromBlockHeight)},
${accountIdentifierCTE(db_client, address)},
${blocksAccessedCTE(db_client)},
${emittedZkAppCommandsCTE(db_client, after)},
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/paima-funnel/src/funnels/FunnelCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class MinaFunnelCacheEntry implements FunnelCacheEntry {
private state: MinaFunnelCacheEntryState | null = null;
public static readonly SYMBOL = Symbol('MinaFunnelStartingSlot');

public updateStartingSlot(
public updateStartingTimestamp(
startingSlotTimestamp: number,
genesisTime: number,
pg: postgres.Sql
Expand Down
41 changes: 9 additions & 32 deletions packages/engine/paima-funnel/src/funnels/mina/funnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ async function findMinaConfirmedTimestamp(pg: postgres.Sql): Promise<number> {
return Number.parseInt(row[0]['timestamp'], 10);
}

function slotToMinaTimestamp(slot: number, genesisTime: number, slotDuration: number): number {
return slot * slotDuration * 1000 + genesisTime;
}

function minaTimestampToSlot(ts: number, genesisTime: number, slotDuration: number): number {
return Math.max(Math.floor((ts - genesisTime) / (slotDuration * 1000)), 0);
}

// mina timestamps are in milliseconds, while evm timestamps are in seconds.
function baseChainTimestampToMina(
baseChainTimestamp: number,
confirmationDepth: number,
Expand Down Expand Up @@ -226,18 +219,13 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel {
})
.map(async extension => {
if (extension.cdeType === ChainDataExtensionType.MinaEventGeneric) {
let fromTimestamp = slotToMinaTimestamp(
extension.startSlot,
cache.genesisTime,
this.config.slotDuration
);

const cursor = cursors && cursors[extension.cdeId];

const data = await getEventCdeData({
pg: cache.pg,
extension,
fromTimestamp,
fromTimestamp: 0,
fromBlockHeight: extension.startBlockHeight,
toTimestamp: startingSlotTimestamp - 1,
// the handler for this cde stores the block height unmodified
// (even if the event is scheduled at the correct height), so
Expand All @@ -256,18 +244,13 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel {
data,
};
} else if (extension.cdeType === ChainDataExtensionType.MinaActionGeneric) {
let fromTimestamp = slotToMinaTimestamp(
extension.startSlot,
cache.genesisTime,
this.config.slotDuration
);

const cursor = cursors && cursors[extension.cdeId];

const data = await getActionCdeData({
pg: cache.pg,
extension,
fromTimestamp,
fromTimestamp: 0,
fromBlockHeight: extension.startBlockHeight,
toTimestamp: startingSlotTimestamp - 1,
getBlockNumber: _x => ENV.SM_START_BLOCKHEIGHT + 1,
network: this.chainName,
Expand Down Expand Up @@ -337,19 +320,13 @@ export class MinaFunnel extends BaseFunnel implements ChainFunnel {
const startingBlockTimestamp = (await sharedData.web3.eth.getBlock(startingBlockHeight))
.timestamp as number;

const slot = minaTimestampToSlot(
baseChainTimestampToMina(
startingBlockTimestamp,
config.confirmationDepth,
config.slotDuration
),
genesisTime,
const minaTimestamp = baseChainTimestampToMina(
startingBlockTimestamp,
config.confirmationDepth,
config.slotDuration
);

const slotAsMinaTimestamp = slotToMinaTimestamp(slot, genesisTime, config.slotDuration);

newEntry.updateStartingSlot(slotAsMinaTimestamp, genesisTime, pg);
newEntry.updateStartingTimestamp(minaTimestamp, genesisTime, pg);

const cursors = await getPaginationCursors.run(undefined, dbTx);

Expand Down
4 changes: 2 additions & 2 deletions packages/engine/paima-sm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ export const ChainDataExtensionMinaEventGenericConfig = Type.Object({
type: Type.Literal(CdeEntryTypeName.MinaEventGeneric),
address: Type.String(),
scheduledPrefix: Type.String(),
startSlot: Type.Number(),
startBlockHeight: Type.Number(),
name: Type.String(),
});

Expand All @@ -471,7 +471,7 @@ export const ChainDataExtensionMinaActionGenericConfig = Type.Object({
type: Type.Literal(CdeEntryTypeName.MinaActionGeneric),
address: Type.String(),
scheduledPrefix: Type.String(),
startSlot: Type.Number(),
startBlockHeight: Type.Number(),
name: Type.String(),
});

Expand Down

0 comments on commit edb39d2

Please sign in to comment.