Skip to content

Commit

Permalink
add delay config to parallel evm funnel (PaimaStudios#360)
Browse files Browse the repository at this point in the history
* add delay setting for ParallelEvmFunnel

* small refactor

* add confirmationDepth setting too
  • Loading branch information
ecioppettini authored and GGAlanSmithee committed May 16, 2024
1 parent 0fe60be commit 1538741
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
34 changes: 20 additions & 14 deletions packages/engine/paima-funnel/src/funnels/parallelEvm/funnel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EvmConfig, Web3 } from '@paima/utils';
import type { OtherEvmConfig, Web3 } from '@paima/utils';
import { doLog, initWeb3, logError, timeout, delay, InternalEventType } from '@paima/utils';
import type { ChainFunnel, ReadPresyncDataFrom } from '@paima/runtime';
import type { ChainData, EvmPresyncChainData, PresyncChainData } from '@paima/sm';
Expand All @@ -9,20 +9,24 @@ import type { FunnelSharedData } from '../BaseFunnel.js';
import type { EvmFunnelCacheEntryState } from '../FunnelCache.js';
import { EvmFunnelCacheEntry, RpcCacheEntry, RpcRequestState } from '../FunnelCache.js';
import type { PoolClient } from 'pg';
import { FUNNEL_PRESYNC_FINISHED, ConfigNetworkType } from '@paima/utils';
import { FUNNEL_PRESYNC_FINISHED } from '@paima/utils';
import { getMultipleBlockData } from '../../reading.js';
import { getLatestProcessedCdeBlockheight } from '@paima/db';

const GET_BLOCK_NUMBER_TIMEOUT = 5000;

function applyDelay(config: OtherEvmConfig, baseTimestamp: number): number {
return Math.max(baseTimestamp - (config.delay ?? 0), 0);
}

export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {
config: EvmConfig;
config: OtherEvmConfig;
chainName: string;

protected constructor(
sharedData: FunnelSharedData,
dbTx: PoolClient,
config: EvmConfig,
config: OtherEvmConfig,
chainName: string,
private readonly baseFunnel: ChainFunnel,
private readonly web3: Web3
Expand Down Expand Up @@ -52,7 +56,7 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {

// filter the data so that we are sure we can get all the blocks in the range
for (const data of cachedState.bufferedChainData) {
if (data.timestamp <= Number(latestBlock.timestamp)) {
if (applyDelay(this.config, data.timestamp) <= Number(latestBlock.timestamp)) {
chainData.push(data);
}
}
Expand Down Expand Up @@ -88,7 +92,7 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {
const ts = Number(block.timestamp);
const earliestParallelChainBlock = await findBlockByTimestamp(
this.web3,
ts,
applyDelay(this.config, ts),
this.chainName
);
// earliestParallelChainBlock is the earliest block that we might need to include
Expand All @@ -97,7 +101,7 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {
}
}

const maxTimestamp = chainData[chainData.length - 1].timestamp;
const maxTimestamp = applyDelay(this.config, chainData[chainData.length - 1].timestamp);

const blocks = [];

Expand Down Expand Up @@ -210,7 +214,7 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {

for (const parallelChainBlock of cachedState.timestampToBlockNumber) {
while (currIndex < chainData.length) {
if (chainData[currIndex].timestamp >= parallelChainBlock[0]) {
if (applyDelay(this.config, chainData[currIndex].timestamp) >= parallelChainBlock[0]) {
sidechainToMainchainBlockHeightMapping[parallelChainBlock[1]] =
chainData[currIndex].blockNumber;

Expand Down Expand Up @@ -450,7 +454,7 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {
dbTx: PoolClient,
baseFunnel: ChainFunnel,
chainName: string,
config: EvmConfig,
config: OtherEvmConfig,
startingBlockHeight: number
): Promise<ParallelEvmFunnel> {
const web3 = await initWeb3(config.chainUri);
Expand All @@ -465,7 +469,7 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {
return newEntry;
})();

cacheEntry.updateState(config.chainId, latestBlock);
cacheEntry.updateState(config.chainId, latestBlock - (config.confirmationDepth ?? 0));

const evmCacheEntry = ((): EvmFunnelCacheEntry => {
const entry = sharedData.cacheManager.cacheEntries[EvmFunnelCacheEntry.SYMBOL];
Expand All @@ -481,7 +485,7 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {
const startingBlock = await sharedData.web3.eth.getBlock(startingBlockHeight);
const mappedStartingBlockHeight = await findBlockByTimestamp(
web3,
Number(startingBlock.timestamp),
applyDelay(config, Number(startingBlock.timestamp)),
chainName
);

Expand Down Expand Up @@ -509,12 +513,14 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {
GET_BLOCK_NUMBER_TIMEOUT
);

const delayedBlock = newLatestBlock - Math.max(this.config.confirmationDepth ?? 0, 0);

this.sharedData.cacheManager.cacheEntries[RpcCacheEntry.SYMBOL]?.updateState(
this.config.chainId,
newLatestBlock
delayedBlock
);

return newLatestBlock;
return delayedBlock;
}

private getState(): EvmFunnelCacheEntryState {
Expand All @@ -536,7 +542,7 @@ export async function wrapToParallelEvmFunnel(
dbTx: PoolClient,
startingBlockHeight: number,
chainName: string,
config: EvmConfig
config: OtherEvmConfig
): Promise<ChainFunnel> {
try {
const ebp = await ParallelEvmFunnel.recoverState(
Expand Down
12 changes: 11 additions & 1 deletion packages/paima-sdk/paima-utils/src/config/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum ConfigNetworkType {
export type EvmConfig = Static<typeof EvmConfigSchema>;

export type MainEvmConfig = Static<typeof MainEvmConfigSchema>;
export type OtherEvmConfig = Static<typeof OtherEvmConfigSchema>;

const EvmConfigSchemaRequiredProperties = Type.Object({
chainUri: Type.String(),
Expand All @@ -37,7 +38,11 @@ const MainNetworkDiscrimination = Type.Union([
paimaL2ContractAddress: PaimaL2ContractType,
type: Type.Literal(ConfigNetworkType.EVM),
}),
Type.Object({ type: Type.Literal(ConfigNetworkType.EVM_OTHER) }),
Type.Object({
delay: Type.Optional(Type.Number()),
confirmationDepth: Type.Optional(Type.Number()),
type: Type.Literal(ConfigNetworkType.EVM_OTHER),
}),
]);

export const EvmConfigSchema = Type.Intersect([
Expand All @@ -51,6 +56,11 @@ const MainEvmConfigSchema = Type.Intersect([
Type.Object({ type: Type.Literal(ConfigNetworkType.EVM) }),
]);

const OtherEvmConfigSchema = Type.Intersect([
EvmConfigSchema,
Type.Object({ type: Type.Literal(ConfigNetworkType.EVM_OTHER) }),
]);

export const CardanoConfigSchema = Type.Object({
carpUrl: Type.String(),
network: Type.String(),
Expand Down
6 changes: 3 additions & 3 deletions packages/paima-sdk/paima-utils/src/config/singleton.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Static } from '@sinclair/typebox';
import type {
CardanoConfig,
EvmConfig,
MainEvmConfig,
BaseConfigWithDefaults,
MinaConfig,
OtherEvmConfig,
} from './loading.js';
import { loadConfig, ConfigNetworkType } from './loading.js';

Expand Down Expand Up @@ -57,12 +57,12 @@ export class GlobalConfig {
return undefined;
}

public static async otherEvmConfig(): Promise<[string, EvmConfig][]> {
public static async otherEvmConfig(): Promise<[string, OtherEvmConfig][]> {
const instance = await GlobalConfig.getInstance();

return Object.keys(instance)
.filter(key => instance[key].type === ConfigNetworkType.EVM_OTHER)
.map(key => [key, instance[key] as EvmConfig]);
.map(key => [key, instance[key] as OtherEvmConfig]);
}

public static async minaConfig(): Promise<[string, MinaConfig][]> {
Expand Down
2 changes: 2 additions & 0 deletions packages/paima-sdk/paima-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CardanoConfig,
MinaConfig,
ConfigNetworkType,
OtherEvmConfig,
defaultEvmMainNetworkName,
defaultCardanoNetworkName,
defaultMinaNetworkName,
Expand All @@ -36,6 +37,7 @@ export {
doLog,
GlobalConfig,
EvmConfig,
OtherEvmConfig,
CardanoConfig,
MinaConfig,
ConfigNetworkType,
Expand Down

0 comments on commit 1538741

Please sign in to comment.