Skip to content

feat: support multiple JITO endpoints with round-robin retry #2664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions apps/price_pusher/src/solana/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export default {
type: "number",
default: 50000,
} as Options,
"jito-endpoint": {
description: "Jito endpoint",
"jito-endpoints": {
description: "Jito endpoint(s) - comma-separated list of endpoints",
type: "string",
optional: true,
} as Options,
Expand Down Expand Up @@ -117,7 +117,7 @@ export default {
pythContractAddress,
pushingFrequency,
pollingFrequency,
jitoEndpoint,
jitoEndpoints,
jitoKeypairFile,
jitoTipLamports,
dynamicJitoTips,
Expand Down Expand Up @@ -209,7 +209,13 @@ export default {
Uint8Array.from(JSON.parse(fs.readFileSync(jitoKeypairFile, "ascii"))),
);

const jitoClient = searcherClient(jitoEndpoint, jitoKeypair);
const jitoEndpointsList = jitoEndpoints
.split(",")
.map((endpoint: string) => endpoint.trim());
const jitoClients: SearcherClient[] = jitoEndpointsList.map(
(endpoint: string) => searcherClient(endpoint, jitoKeypair),
);

solanaPricePusher = new SolanaPricePusherJito(
pythSolanaReceiver,
hermesClient,
Expand All @@ -218,13 +224,16 @@ export default {
jitoTipLamports,
dynamicJitoTips,
maxJitoTipLamports,
jitoClient,
jitoClients,
jitoBundleSize,
updatesPerJitoBundle,
60000, // Default max retry time of 60 seconds
lookupTableAccount,
);

onBundleResult(jitoClient, logger.child({ module: "JitoClient" }));
jitoClients.forEach((client, index) => {
onBundleResult(client, logger.child({ module: `JitoClient-${index}` }));
});
} else {
solanaPricePusher = new SolanaPricePusher(
pythSolanaReceiver,
Expand Down
50 changes: 29 additions & 21 deletions apps/price_pusher/src/solana/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,10 @@ export class SolanaPricePusherJito implements IPricePusher {
private defaultJitoTipLamports: number,
private dynamicJitoTips: boolean,
private maxJitoTipLamports: number,
private searcherClient: SearcherClient,
private searcherClients: SearcherClient[],
private jitoBundleSize: number,
private updatesPerJitoBundle: number,
private maxRetryTimeMs: number = 60000, // Default to 60 seconds max retry time
private addressLookupTableAccount?: AddressLookupTableAccount,
) {}

Expand Down Expand Up @@ -242,27 +243,34 @@ export class SolanaPricePusherJito implements IPricePusher {
jitoBundleSize: this.jitoBundleSize,
});

let retries = 60;
while (retries > 0) {
try {
await sendTransactionsJito(
transactions,
this.searcherClient,
this.pythSolanaReceiver.wallet,
);
break;
} catch (err: any) {
if (err.code === 8 && err.details?.includes("Rate limit exceeded")) {
this.logger.warn("Rate limit hit, waiting before retry...");
await this.sleep(1100); // Wait slightly more than 1 second
retries--;
if (retries === 0) {
this.logger.error("Max retries reached for rate limit");
throw err;
}
} else {
throw err;
try {
await sendTransactionsJito(
transactions,
this.searcherClients,
this.pythSolanaReceiver.wallet,
{ maxRetryTimeMs: this.maxRetryTimeMs },
);
} catch (err: any) {
if (err.code === 8 && err.details?.includes("Rate limit exceeded")) {
this.logger.warn("Rate limit hit, waiting before retry...");
await this.sleep(1100); // Wait slightly more than 1 second
try {
await sendTransactionsJito(
transactions,
this.searcherClients,
this.pythSolanaReceiver.wallet,
{ maxRetryTimeMs: this.maxRetryTimeMs },
);
} catch (retryErr: any) {
this.logger.error("Failed after rate limit retry");
throw retryErr;
}
} else {
this.logger.error(
{ err },
"Failed to send transactions via all JITO endpoints",
);
throw err;
}
}

Expand Down
2 changes: 1 addition & 1 deletion target_chains/solana/sdk/js/solana_utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/solana-utils",
"version": "0.4.4",
"version": "0.4.5",
"description": "Utility functions for Solana",
"homepage": "https://pyth.network",
"main": "lib/index.js",
Expand Down
37 changes: 34 additions & 3 deletions target_chains/solana/sdk/js/solana_utils/src/jito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,23 @@ export async function sendTransactionsJito(
tx: VersionedTransaction;
signers?: Signer[] | undefined;
}[],
searcherClient: SearcherClient,
searcherClients: SearcherClient | SearcherClient[],
wallet: Wallet,
options: {
maxRetryTimeMs?: number;
} = {},
): Promise<string> {
const clients = Array.isArray(searcherClients)
? searcherClients
: [searcherClients];

if (clients.length === 0) {
throw new Error("No searcher clients provided");
}

const maxRetryTimeMs = options.maxRetryTimeMs || 60000; // Default to 60 seconds
const startTime = Date.now();

const signedTransactions = [];

for (const transaction of transactions) {
Expand All @@ -64,7 +78,24 @@ export async function sendTransactionsJito(
);

const bundle = new Bundle(signedTransactions, 2);
await searcherClient.sendBundle(bundle);

return firstTransactionSignature;
let lastError: Error | null = null;
let clientIndex = 0;

while (Date.now() - startTime < maxRetryTimeMs) {
const currentClient = clients[clientIndex];
try {
await currentClient.sendBundle(bundle);
return firstTransactionSignature;
} catch (err: any) {
lastError = err;
clientIndex = (clientIndex + 1) % clients.length;
await new Promise((resolve) => setTimeout(resolve, 500));
}
}
Comment on lines +85 to +95
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(aside) @guibescos do you know how many jito endpoints we can round robin against? the ratelimit retry logic is a little funky here if there are only 2 or 3.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(aside) cc @ali-bahjati


throw (
lastError ||
new Error("Failed to send transactions via JITO after maximum retry time")
);
}
Loading