Skip to content

Commit

Permalink
add CARDANO_CONFIRMATION_DEPTH config
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Dec 5, 2023
1 parent 17b265f commit a2c3b33
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
36 changes: 26 additions & 10 deletions packages/engine/paima-funnel/src/funnels/carp/funnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { Routes } from '@dcspark/carp-client/shared/routes';
import { FUNNEL_PRESYNC_FINISHED } from '@paima/utils/src/constants';
import { CarpFunnelCacheEntry } from '../FunnelCache.js';

const confirmationDepth = 10;
const delayForWaitingForFinalityLoop = 1000;

function knownShelleyTime(): number {
Expand All @@ -42,7 +41,7 @@ function knownShelleyTime(): number {
}
}

function timestampToAbsoluteSlot(timestamp: number): number {
function timestampToAbsoluteSlot(timestamp: number, confirmationDepth: number): number {
const cardanoAvgBlockPeriod = 20;
// map timestamps with a delta, since we are waiting for blocks.
const confirmationTimeDelta = cardanoAvgBlockPeriod * confirmationDepth;
Expand All @@ -56,7 +55,8 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel {
dbTx: PoolClient,
private readonly baseFunnel: ChainFunnel,
private readonly carpUrl: string,
private cache: CarpFunnelCacheEntry
private cache: CarpFunnelCacheEntry,
private readonly confirmationDepth: number
) {
super(sharedData, dbTx);
// TODO: replace once TS5 decorators are better supported
Expand Down Expand Up @@ -102,7 +102,8 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel {
this.carpUrl,
this.sharedData.extensions,
lastTimestamp,
this.cache
this.cache,
this.confirmationDepth
);

const composed = composeChainData(this.bufferedData, grouped);
Expand Down Expand Up @@ -165,6 +166,12 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel {
carpUrl: string,
startingBlockHeight: number
): Promise<CarpFunnel> {
if (!ENV.CARDANO_CONFIRMATION_DEPTH) {
throw new Error('[carp-funnel] Missing CARDANO_CONFIRMATION_DEPTH setting.');
}

const confirmationDepth = ENV.CARDANO_CONFIRMATION_DEPTH;

const cacheEntry = (async (): Promise<CarpFunnelCacheEntry> => {
const entry = sharedData.cacheManager.cacheEntries[CarpFunnelCacheEntry.SYMBOL];
if (entry != null) return entry;
Expand All @@ -174,14 +181,22 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel {

newEntry.updateStartingSlot(
timestampToAbsoluteSlot(
(await sharedData.web3.eth.getBlock(startingBlockHeight)).timestamp as number
(await sharedData.web3.eth.getBlock(startingBlockHeight)).timestamp as number,
confirmationDepth
)
);

return newEntry;
})();

return new CarpFunnel(sharedData, dbTx, baseFunnel, carpUrl, await cacheEntry);
return new CarpFunnel(
sharedData,
dbTx,
baseFunnel,
carpUrl,
await cacheEntry,
confirmationDepth
);
}
}

Expand All @@ -190,14 +205,15 @@ async function readDataInternal(
carpUrl: string,
extensions: ChainDataExtension[],
lastTimestamp: number,
cache: CarpFunnelCacheEntry
cache: CarpFunnelCacheEntry,
confirmationDepth: number
): Promise<PresyncChainData[]> {
// the lower range is exclusive
const min = timestampToAbsoluteSlot(lastTimestamp);
const min = timestampToAbsoluteSlot(lastTimestamp, confirmationDepth);
// the upper range is inclusive
const maxElement = data[data.length - 1];

const max = timestampToAbsoluteSlot(maxElement.timestamp);
const max = timestampToAbsoluteSlot(maxElement.timestamp, confirmationDepth);

cache.updateLastPoint(maxElement.blockNumber, maxElement.timestamp);

Expand All @@ -222,7 +238,7 @@ async function readDataInternal(

const blockNumbers = data.reduce(
(dict, data) => {
dict[timestampToAbsoluteSlot(data.timestamp)] = data.blockNumber;
dict[timestampToAbsoluteSlot(data.timestamp, confirmationDepth)] = data.blockNumber;
return dict;
},
{} as { [slot: number]: number }
Expand Down
5 changes: 3 additions & 2 deletions packages/paima-sdk/paima-utils/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ export class ENV {
return process.env.BATCHER_URI || '';
}

// TODO: put this in the right place
static get CARP_URL(): string | undefined {
return process.env.CARP_URL;
}
// TODO: put this in the right place
static get CARDANO_NETWORK(): string | undefined {
return process.env.CARDANO_NETWORK;
}
static get CARDANO_CONFIRMATION_DEPTH(): number | undefined {
return Number(process.env.CARDANO_CONFIRMATION_DEPTH);
}
}

0 comments on commit a2c3b33

Please sign in to comment.