Skip to content

Commit

Permalink
Make sequential RPC method requests when Nitro payments is configured (
Browse files Browse the repository at this point in the history
…#5)

* Start realtime after historical sync serve if nitro payment configured

* Use maxRpcRequestConcurrency in realtime sync service

* Add comment for starting realtime sync service after historical sync completes
  • Loading branch information
nikugogoi authored Oct 4, 2023
1 parent 6fa5d3a commit 2221107
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
18 changes: 14 additions & 4 deletions packages/core/src/Ponder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
)
);
Expand Down Expand Up @@ -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();
}
}
)
);
Expand Down Expand Up @@ -393,6 +397,12 @@ export class Ponder {
this.eventAggregatorService.handleHistoricalSyncComplete({
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();
}
});

realtimeSyncService.on("realtimeCheckpoint", ({ timestamp }) => {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/config/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/realtime-sync/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export class RealtimeSyncService extends Emittery<RealtimeSyncEvents> {
);

// Fetch all missing blocks using a request concurrency limit of 10.
const limit = pLimit(10);
const limit = pLimit(this.network.maxRpcRequestConcurrency);

const missingBlockRequests = missingBlockNumbers.map((number) => {
return limit(async () => {
Expand Down

0 comments on commit 2221107

Please sign in to comment.