From 6deb90bb1c8b3bff4228e6ce9241e2518e54c3ba Mon Sep 17 00:00:00 2001 From: Nabarun Date: Tue, 3 Oct 2023 20:21:19 +0530 Subject: [PATCH 1/3] Start realtime after historical sync serve if nitro payment configured --- packages/core/src/Ponder.ts | 16 ++++++++++++---- packages/core/src/config/networks.ts | 6 +++++- packages/core/src/realtime-sync/service.ts | 1 + 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/core/src/Ponder.ts b/packages/core/src/Ponder.ts index c603719ad..4f3cdb301 100644 --- a/packages/core/src/Ponder.ts +++ b/packages/core/src/Ponder.ts @@ -264,9 +264,11 @@ export class Ponder { async ({ historicalSyncService, realtimeSyncService }) => { const blockNumbers = await realtimeSyncService.setup(); await historicalSyncService.setup(blockNumbers); - historicalSyncService.start(); - realtimeSyncService.start(); + + if (!this.paymentService) { + realtimeSyncService.start(); + } } ) ); @@ -301,9 +303,11 @@ export class Ponder { async ({ historicalSyncService, realtimeSyncService }) => { const blockNumbers = await realtimeSyncService.setup(); await historicalSyncService.setup(blockNumbers); - historicalSyncService.start(); - realtimeSyncService.start(); + + if (!this.paymentService) { + realtimeSyncService.start(); + } } ) ); @@ -393,6 +397,10 @@ export class Ponder { this.eventAggregatorService.handleHistoricalSyncComplete({ chainId, }); + + if (this.paymentService) { + realtimeSyncService.start(); + } }); realtimeSyncService.on("realtimeCheckpoint", ({ timestamp }) => { diff --git a/packages/core/src/config/networks.ts b/packages/core/src/config/networks.ts index 2f5ad22d2..a2dbb074c 100644 --- a/packages/core/src/config/networks.ts +++ b/packages/core/src/config/networks.ts @@ -4,7 +4,11 @@ import { mainnet } from "viem/chains"; import type { ResolvedConfig } from "@/config/config"; import { PaymentService } from "@/payment/service"; -const PAID_RPC_METHODS = ["eth_getLogs"]; +const PAID_RPC_METHODS = [ + "eth_getLogs", + "eth_getBlockByNumber", + "eth_getBlockByHash", +]; export type Network = { name: string; diff --git a/packages/core/src/realtime-sync/service.ts b/packages/core/src/realtime-sync/service.ts index 0dee26967..dfa83268f 100644 --- a/packages/core/src/realtime-sync/service.ts +++ b/packages/core/src/realtime-sync/service.ts @@ -404,6 +404,7 @@ export class RealtimeSyncService extends Emittery { ); // Fetch all missing blocks using a request concurrency limit of 10. + // TODO: Make realtime block fetch concurrency configurable. const limit = pLimit(10); const missingBlockRequests = missingBlockNumbers.map((number) => { From 6c7fdde61529bcf960e432581f4b6bb4ac531121 Mon Sep 17 00:00:00 2001 From: Nabarun Date: Wed, 4 Oct 2023 10:16:03 +0530 Subject: [PATCH 2/3] Use maxRpcRequestConcurrency in realtime sync service --- packages/core/src/realtime-sync/service.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/core/src/realtime-sync/service.ts b/packages/core/src/realtime-sync/service.ts index dfa83268f..68ead2fdf 100644 --- a/packages/core/src/realtime-sync/service.ts +++ b/packages/core/src/realtime-sync/service.ts @@ -404,8 +404,7 @@ export class RealtimeSyncService extends Emittery { ); // Fetch all missing blocks using a request concurrency limit of 10. - // TODO: Make realtime block fetch concurrency configurable. - const limit = pLimit(10); + const limit = pLimit(this.network.maxRpcRequestConcurrency); const missingBlockRequests = missingBlockNumbers.map((number) => { return limit(async () => { From 62051fd92ba91675825929d487181fe6e4a0f002 Mon Sep 17 00:00:00 2001 From: Nabarun Date: Wed, 4 Oct 2023 11:38:59 +0530 Subject: [PATCH 3/3] Add comment for starting realtime sync service after historical sync completes --- packages/core/src/Ponder.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/core/src/Ponder.ts b/packages/core/src/Ponder.ts index 4f3cdb301..b19f72af7 100644 --- a/packages/core/src/Ponder.ts +++ b/packages/core/src/Ponder.ts @@ -398,6 +398,8 @@ export class Ponder { chainId, }); + // If payment service is setup, start the realtime sync service after historical sync service. + // This will avoid parallel requests to RPC endpoint if (this.paymentService) { realtimeSyncService.start(); }