Skip to content

Commit

Permalink
add delay setting for ParallelEvmFunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed May 3, 2024
1 parent e5f5f73 commit 8358aa3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
22 changes: 11 additions & 11 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,20 @@ 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;

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 +52,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 (data.timestamp - (this.config.delay ?? 0) <= Number(latestBlock.timestamp)) {
chainData.push(data);
}
}
Expand Down Expand Up @@ -88,7 +88,7 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {
const ts = Number(block.timestamp);
const earliestParallelChainBlock = await findBlockByTimestamp(
this.web3,
ts,
ts - (this.config.delay ?? 0),
this.chainName
);
// earliestParallelChainBlock is the earliest block that we might need to include
Expand All @@ -97,7 +97,7 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {
}
}

const maxTimestamp = chainData[chainData.length - 1].timestamp;
const maxTimestamp = chainData[chainData.length - 1].timestamp - (this.config.delay ?? 0);

const blocks = [];

Expand Down Expand Up @@ -210,7 +210,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 (chainData[currIndex].timestamp - (this.config.delay ?? 0) >= parallelChainBlock[0]) {
sidechainToMainchainBlockHeightMapping[parallelChainBlock[1]] =
chainData[currIndex].blockNumber;

Expand Down Expand Up @@ -450,7 +450,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 Down Expand Up @@ -481,7 +481,7 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel {
const startingBlock = await sharedData.web3.eth.getBlock(startingBlockHeight);
const mappedStartingBlockHeight = await findBlockByTimestamp(
web3,
Number(startingBlock.timestamp),
Number(startingBlock.timestamp) - (config.delay ?? 0),
chainName
);

Expand Down Expand Up @@ -536,7 +536,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
11 changes: 10 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,10 @@ 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()),
type: Type.Literal(ConfigNetworkType.EVM_OTHER),
}),
]);

export const EvmConfigSchema = Type.Intersect([
Expand All @@ -51,6 +55,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 @@ -35,6 +36,7 @@ export {
doLog,
GlobalConfig,
EvmConfig,
OtherEvmConfig,
CardanoConfig,
MinaConfig,
ConfigNetworkType,
Expand Down

0 comments on commit 8358aa3

Please sign in to comment.