From 230c2c94f532dda46dd7a252b4aa40a143a107a5 Mon Sep 17 00:00:00 2001 From: Nico Krause Date: Sun, 19 Jan 2025 22:58:04 +0500 Subject: [PATCH] feat: improve ElectrumX connection handling - Added additional Electrum server nodes for better failover - Enhanced connection stability checks - Improved reconnection logic for dropped connections - Added better error handling and logging --- relay/package.json | 2 +- relay/src/pinner/blockProcessor.js | 2 +- relay/src/pinner/scanBlockchainForNameOps.js | 33 +++----------------- 3 files changed, 6 insertions(+), 31 deletions(-) diff --git a/relay/package.json b/relay/package.json index 04764dd..91cd702 100644 --- a/relay/package.json +++ b/relay/package.json @@ -1,6 +1,6 @@ { "name": "libp2p-relay", - "version": "0.12.24", + "version": "0.12.26", "private": true, "scripts": { "start:no-restart": "node src/relay.js", diff --git a/relay/src/pinner/blockProcessor.js b/relay/src/pinner/blockProcessor.js index ce698a3..82e6efc 100644 --- a/relay/src/pinner/blockProcessor.js +++ b/relay/src/pinner/blockProcessor.js @@ -63,5 +63,5 @@ export async function processBlockAtHeight(height, electrumClient) { } } - return { nameOpUtxos, blockDate }; + return { nameOpUtxos, blockDate, electrumClient }; } diff --git a/relay/src/pinner/scanBlockchainForNameOps.js b/relay/src/pinner/scanBlockchainForNameOps.js index 33fa30c..9c65543 100644 --- a/relay/src/pinner/scanBlockchainForNameOps.js +++ b/relay/src/pinner/scanBlockchainForNameOps.js @@ -89,18 +89,9 @@ async function processBlocks( for (let height = startHeight; height > MIN_HEIGHT; height--) { if (stopToken.isStopped) break; try { - // if (Date.now() - lastConnectionCheck > CONNECTION_CHECK_INTERVAL) { - // console.warn('ElectrumX connection lost, attempting to reconnect...'); - // electrumClient = await reconnectElectrumClient(electrumClient); - // lastConnectionCheck = Date.now(); - // } - console.info(`Processing block at height ${height}`); - const { nameOpUtxos, blockDate } = await processBlockAtHeight( - height, - electrumClient - ) - + const { nameOpUtxos, blockDate, electrumClient: _electrumClient } = await processBlockAtHeight(height, electrumClient) + electrumClient = _electrumClient console.info(`nameOpUtxos ${nameOpUtxos} at ${blockDate}`); const blockDay = moment.utc(blockDate).format('YYYY-MM-DD'); if (blockDay !== currentDay) { @@ -163,33 +154,17 @@ async function processBlocks( } catch (error) { console.error(`Error processing block at height ${height}:`, { error }); - if (error.code === -101 && error.message === 'excessive resource usage') { try { // Switch to new ElectrumX server - const newClient = await useNextElectrum( - { name: process.env.DOICHAIN_NETWORK }, - (key, value) => { - if (key === 'electrumClient') { - electrumClient = value; - } - } - ); - + electrumClient = await useNextElectrum() console.info('Switched to new ElectrumX server after error', { - newServer: `${newClient.host}:${newClient.port}` + newServer: `${electrumClient.host}:${electrumClient.port}` }); - // Retry the same block with new client - height++; continue; } catch (switchError) { console.error('Failed to switch ElectrumX server', { error: switchError }); } - } - - // Always attempt to reconnect to ElectrumX - console.warn('Attempting to reconnect to ElectrumX...'); - await reconnectElectrumClient(electrumClient); // Increase height to retry the same block height++;