Skip to content

Commit

Permalink
fix(block-scanning): improve error handling and server selection
Browse files Browse the repository at this point in the history
- Retry same block on errors with height++ adjustment
- Always reconnect to ElectrumX with random server selection
- Remove error type checking for simpler logic
- Random selection improves load balancing
  • Loading branch information
silkroadnomad committed Jan 18, 2025
1 parent cdf1a26 commit f45350b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
2 changes: 1 addition & 1 deletion relay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libp2p-relay",
"version": "0.12.22",
"version": "0.12.23",
"private": true,
"scripts": {
"start:no-restart": "node src/relay.js",
Expand Down
15 changes: 5 additions & 10 deletions relay/src/doichain/connectElectrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import logger from '../logger.js';
const MAX_RETRIES = 25;
const RETRY_DELAY = 5000;

let currentServerIndex = -1;
let connectedClients = [];

export const useNextElectrum = async (_network, updateStore) => {
Expand All @@ -26,10 +25,10 @@ export const useNextElectrum = async (_network, updateStore) => {
throw new Error('No Electrum servers available for the given network.');
}

currentServerIndex = (currentServerIndex + 1) % networkNodes.length;
const nextServer = networkNodes[currentServerIndex];
// Random server selection
const nextServer = networkNodes[Math.floor(Math.random() * networkNodes.length)];

// Check if we already have a connected client for this server
// Check for existing connection
let existingClient = connectedClients.find(
(c) =>
c.host === nextServer.host &&
Expand All @@ -41,7 +40,7 @@ export const useNextElectrum = async (_network, updateStore) => {
return existingClient;
}

// If not, create a new client and connect
// Create new connection
const newClient = new ElectrumxClient(
nextServer.host,
nextServer.port,
Expand All @@ -59,10 +58,6 @@ export const useNextElectrum = async (_network, updateStore) => {
protocol: nextServer.protocol,
});

// Remove this line as it's causing the duplicate server.version request
// const serverVersion = await newClient.request('server.version');
// updateStore('electrumServerVersion', serverVersion);

const connectedServer = `${nextServer.protocol}://${nextServer.host}:${nextServer.port}`;
updateStore('connectedServer', connectedServer);

Expand All @@ -74,7 +69,7 @@ export const useNextElectrum = async (_network, updateStore) => {
protocol: nextServer.protocol,
error: error.message,
});
// If connection fails, try the next server
// Try another random server
return useNextElectrum(_network, updateStore);
}
};
Expand Down
15 changes: 8 additions & 7 deletions relay/src/pinner/scanBlockchainForNameOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,14 @@ async function processBlocks(
} catch (error) {
logger.error(`Error processing block at height ${height}:`, { error });
errorRate.inc(); // Increment the error rate counter
if (error.message.includes('ElectrumX connection')) {
logger.warn('ElectrumX connection lost, attempting to reconnect...');
await reconnectElectrumClient(electrumClient);
height++; // Retry the current block
} else {
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait before retrying
}

// Always attempt to reconnect to ElectrumX
logger.warn('Attempting to reconnect to ElectrumX...');
await reconnectElectrumClient(electrumClient);

// Increase height to retry the same block
height++;
continue;
} finally {
endTimer(); // End timing block processing
}
Expand Down

0 comments on commit f45350b

Please sign in to comment.