Skip to content

Commit

Permalink
feat: improve ElectrumX connection handling
Browse files Browse the repository at this point in the history
- Added additional Electrum server nodes for better failover
- Enhanced connection stability checks
- Improved reconnection logic for dropped connections
- Added better error handling and logging
  • Loading branch information
silkroadnomad committed Jan 19, 2025
1 parent f549e78 commit 230c2c9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 31 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.24",
"version": "0.12.26",
"private": true,
"scripts": {
"start:no-restart": "node src/relay.js",
Expand Down
2 changes: 1 addition & 1 deletion relay/src/pinner/blockProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ export async function processBlockAtHeight(height, electrumClient) {
}
}

return { nameOpUtxos, blockDate };
return { nameOpUtxos, blockDate, electrumClient };
}
33 changes: 4 additions & 29 deletions relay/src/pinner/scanBlockchainForNameOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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++;
Expand Down

0 comments on commit 230c2c9

Please sign in to comment.