Skip to content

Commit

Permalink
carp funnel: add last epoch to the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Dec 13, 2023
1 parent 1526bb7 commit 22dc556
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
9 changes: 8 additions & 1 deletion packages/engine/paima-funnel/src/funnels/FunnelCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ export class RpcCacheEntry implements FunnelCacheEntry {
export type CarpFunnelCacheEntryState = {
startingSlot: number;
lastPoint: { blockHeight: number; timestamp: number } | undefined;
epoch: number | undefined;
};

export class CarpFunnelCacheEntry implements FunnelCacheEntry {
private state: CarpFunnelCacheEntryState | null = null;
public static readonly SYMBOL = Symbol('CarpFunnelStartingSlot');

public updateStartingSlot(startingSlot: number): void {
this.state = { startingSlot, lastPoint: this.state?.lastPoint };
this.state = { startingSlot, lastPoint: this.state?.lastPoint, epoch: this.state?.epoch };
}

public updateLastPoint(blockHeight: number, timestamp: number): void {
Expand All @@ -92,6 +93,12 @@ export class CarpFunnelCacheEntry implements FunnelCacheEntry {
}
}

public updateEpoch(epoch: number): void {
if (this.state) {
this.state.epoch = epoch;
}
}

public initialized(): boolean {
return !!this.state;
}
Expand Down
17 changes: 12 additions & 5 deletions packages/engine/paima-funnel/src/funnels/carp/funnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { query } from '@dcspark/carp-client/client/src/index';
import { Routes } from '@dcspark/carp-client/shared/routes';
import { FUNNEL_PRESYNC_FINISHED, InternalEventType } from '@paima/utils/src/constants';
import { CarpFunnelCacheEntry } from '../FunnelCache.js';
import { getCardanoEpoch } from '@paima/db';

const delayForWaitingForFinalityLoop = 1000;

Expand Down Expand Up @@ -154,10 +155,6 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel {

const composed = composeChainData(this.bufferedData, grouped);

this.bufferedData = null;

let prevEpoch;

for (const data of composed) {
if (!data.internalEvents) {
data.internalEvents = [] as InternalEvent[];
Expand All @@ -166,17 +163,21 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel {
timestampToAbsoluteSlot(data.timestamp, this.confirmationDepth)
);

const prevEpoch = this.cache.getState().epoch;

if (!prevEpoch || epoch !== prevEpoch) {
data.internalEvents?.push({
type: InternalEventType.CardanoBestEpoch,
epoch: epoch,
});
}

prevEpoch = epoch;
this.cache.updateEpoch(epoch);
}
}

this.bufferedData = null;

return composed;
}

Expand Down Expand Up @@ -253,6 +254,12 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel {
)
);

const epoch = await getCardanoEpoch.run(undefined, dbTx);

if (epoch.length === 1) {
newEntry.updateEpoch(epoch[0].epoch);
}

return newEntry;
})();

Expand Down

0 comments on commit 22dc556

Please sign in to comment.