Skip to content
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

filter bad rpcs #1328

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
146 changes: 146 additions & 0 deletions src/api/rpc/rpcs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChainId } from '../../../packages/address-book/address-book';
import { mapValues, partition } from 'lodash';

const rpcs: Record<ChainId, string[]> = {
[ChainId.ethereum]: [
Expand Down Expand Up @@ -247,4 +248,149 @@ const rpcs: Record<ChainId, string[]> = {
],
};

export type ErrorRpcResponse = {
error: {
code: number;
message: string;
};
id: number;
jsonrpc: '2.0';
};

export type SuccessRpcResponse<TResult> = {
result: TResult;
id: number;
jsonrpc: '2.0';
};

export type RpcResponse<TResult> = ErrorRpcResponse | SuccessRpcResponse<TResult>;

const LOG_ERRORS: boolean = false;
const LOG_INFO: boolean = false;

export async function initRpcs(): Promise<void> {
const counts = mapValues(rpcs, rpcs => rpcs.length);

await Promise.all(
Object.keys(rpcs).map(async key => {
const chainId = Number(key) as keyof typeof rpcs; // object keys are strings
const chainName = ChainId[chainId];
const endpoints = rpcs[chainId]!;
const blockNumbers = await Promise.all(
endpoints.map(async (rpc, i) => {
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 3000);
const response = await fetch(rpc, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Origin: 'https://api.beefy.finance',
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_blockNumber',
params: [],
id: i,
}),
signal: controller.signal,
});
clearTimeout(timeout);

const json = (await response.json()) as RpcResponse<string>;
if ('error' in json) {
if (LOG_ERRORS) {
console.error('initRpcs::error', chainName, rpc, json);
}
return undefined;
}

return BigInt(json.result);
} catch (e) {
if (LOG_ERRORS) {
console.error('initRpcs::error', chainName, rpc, e);
}
return undefined;
}
})
);

const endpointsToRemove = new Set<string>();
const endpointsToCheck: { rpc: string; blockNumber: bigint }[] = [];

for (let i = 0; i < endpoints.length; i++) {
if (!blockNumbers[i]) {
if (LOG_INFO) {
console.log(
'initRpcs::info',
chainName,
'removing endpoint',
endpoints[i],
'no block number returned'
);
}
endpointsToRemove.add(endpoints[i]);
} else {
endpointsToCheck.push({ rpc: endpoints[i], blockNumber: blockNumbers[i] });
}
}

if (endpointsToCheck.length === 0) {
if (LOG_ERRORS) {
console.error(
'initRpcs::error',
chainName,
'no block numbers for chain',
'allowing all rpcs'
);
}
return;
}

// Allow +/- 2 blocks from the median block number
const sortedBlockNumbers = blockNumbers.filter(Boolean).sort((a, b) => Number(a - b));
const medianBlockNumber = sortedBlockNumbers[Math.floor(sortedBlockNumbers.length / 2)];
const minBlockNumber = medianBlockNumber - 2n;
const maxBlockNumber = medianBlockNumber + 2n;

for (const endpoint of endpointsToCheck) {
if (endpoint.blockNumber < minBlockNumber || endpoint.blockNumber > maxBlockNumber) {
endpointsToRemove.add(endpoint.rpc);
if (LOG_INFO) {
console.log(
'initRpcs::info',
chainId,
'removing endpoint',
endpoint.rpc,
'blockNumber',
endpoint.blockNumber,
'minBlockNumber',
minBlockNumber,
'maxBlockNumber',
maxBlockNumber
);
}
}
}

if (endpointsToRemove.size > 0) {
console.log('initRpcs', chainName, 'removing endpoints', Array.from(endpointsToRemove));
const validChains = rpcs[chainId].filter(rpc => !endpointsToRemove.has(rpc));
if (validChains.length) {
rpcs[chainId] = validChains;
} else {
if (LOG_ERRORS) {
console.error('initRpcs::error', chainName, 'no valid rpcs left, allowing all');
}
}
}
})
);

for (const key of Object.keys(rpcs)) {
const endpoints = rpcs[key]!;
console.log('initRpcs', ChainId[key], endpoints.length, '/', counts[key], 'endpoints');
}
}

export const getChainRpcs = (chainId: ChainId): string[] => rpcs[chainId] ?? [];
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require('dotenv').config();

import { initCache } from './utils/cache';
import { initRpcs } from './api/rpc/rpcs';
import { initBoostService } from './api/boosts/getBoosts';
import { initPriceService } from './api/stats/getAmmPrices';
import { initApyService } from './api/stats/getApys';
Expand Down Expand Up @@ -47,6 +48,7 @@ const port = process.env.PORT || 3000;

const start = async () => {
await initCache();
await initRpcs();

initApyService();
initPriceService();
Expand Down