Skip to content

Commit

Permalink
fix: improve ElectrumX connection handling and timing
Browse files Browse the repository at this point in the history
Co-Authored-By: Nico Krause <[email protected]>
  • Loading branch information
devin-ai-integration[bot] and silkroadnomad committed Dec 23, 2024
1 parent 78d4da8 commit 9783273
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docker-compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ services:
volumes:
- ./docker/entrypoint.sh:/usr/src/app/entrypoint.sh
# - ./:/usr/src/app/
entrypoint: ["/bin/sh", "-c", "sleep 30 && /usr/src/app/entrypoint.sh generate-key && sleep 30"]
entrypoint: ["/bin/sh", "-c", "/usr/src/app/entrypoint.sh generate-key"]
ports:
- "1235:1235"
- "9091:9091"
Expand Down
11 changes: 10 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
FROM node:20

# Install netcat for connection checks
RUN apt-get update && apt-get install -y netcat-openbsd && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app
COPY relay/ ./
COPY docker/entrypoint.sh ./
COPY relay/.env.example ./.env
RUN rm -rf node_modules && npm install

# Make entrypoint script executable and install dependencies
RUN chmod +x ./entrypoint.sh && \
rm -rf node_modules && \
npm install

ENTRYPOINT [ "./entrypoint.sh" ]
CMD [ "node", "src/relay.js"]
47 changes: 41 additions & 6 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
#!/bin/bash
rm scanning-state.json
rm -f scanning-state.json

# Function to check if ElectrumX is ready
check_electrumx() {
nc -z electrumx 8443 2>/dev/null
return $?
}

# Function to wait for ElectrumX with timeout
wait_for_electrumx() {
echo "Waiting for ElectrumX to be ready..."
local timeout=60
local count=0
while ! check_electrumx; do
count=$((count + 1))
if [ $count -gt $timeout ]; then
echo "Timeout waiting for ElectrumX"
return 1
fi
echo "Attempt $count/$timeout: ElectrumX not ready yet..."
sleep 1
done
echo "ElectrumX is ready!"
return 0
}

if [ "$1" == "generate-key" ]; then
echo "Generating private key..."
Expand All @@ -15,14 +39,25 @@ if [ "$1" == "generate-key" ]; then
echo "$key_output" >> .env
fi
cat .env
npm run start

# Wait for ElectrumX to be ready before starting
if wait_for_electrumx; then
echo "Starting relay service..."
npm run start
else
echo "Failed to connect to ElectrumX"
exit 1
fi
elif [ "$1" == "start" ]; then
echo "Starting node..."
npm run start
# Wait for ElectrumX to be ready before starting
if wait_for_electrumx; then
npm run start
else
echo "Failed to connect to ElectrumX"
exit 1
fi
else
echo "Invalid command. Use 'generate-key' or 'start'."
exit 1
fi

# Start the application if a valid command was provided
# exec "$@"
10 changes: 9 additions & 1 deletion relay/src/pinner/nameOpsFileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,16 @@ export async function updateDailyNameOpsFile(orbitdb, nameOpUtxos, blockDate, bl
_id: docId,
nameOp, // Store the entire nameOp object
blockHeight,
blockDate
blockDate,
timestamp: new Date().toISOString() // Add timestamp for debugging
});

// Verify the document was stored
const doc = await db.get(docId);
if (!doc) {
throw new Error(`Failed to verify document storage for ${docId}`);
}
logger.debug(`[updateDailyNameOpsFile] Verified storage of nameOp ${docId}`);
logger.debug(`[updateDailyNameOpsFile] Successfully stored nameOp ${docId}`);
}

Expand Down
29 changes: 20 additions & 9 deletions relay/tests/relay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Doichain Relay Pinning Service Test', function() {
const TIMEOUT = 5000;

// Helper function to check if OrbitDB has nameOps and relay connection is established
async function waitForNameOps(orbitdb, maxAttempts = 30) {
async function waitForNameOps(orbitdb, maxAttempts = 60) {
console.log('[waitForNameOps] Starting check for nameOps and relay connection...');

for (let attempt = 0; attempt < maxAttempts; attempt++) {
Expand Down Expand Up @@ -54,20 +54,31 @@ describe('Doichain Relay Pinning Service Test', function() {
continue;
}

// Wait for DB to be ready
await new Promise(resolve => setTimeout(resolve, 2000));
// Wait for DB to load and sync
await new Promise(resolve => setTimeout(resolve, 5000));

// Verify DB is ready and has data
const allDocs = await db.all();
console.log(`[waitForNameOps] Found ${allDocs.length} documents in OrbitDB`);

if (allDocs.length > 0) {
console.log('[waitForNameOps] Successfully found nameOps');
// Additional wait to ensure all data is synced
await new Promise(resolve => setTimeout(resolve, 3000));
return true;
// Additional verification: check if we can actually query the data
try {
const testQuery = await db.query((doc) => doc.value.nameOp !== undefined);
console.log(`[waitForNameOps] Query test returned ${testQuery.length} documents`);

if (testQuery.length > 0) {
console.log('[waitForNameOps] Successfully verified nameOps data access');
// Additional wait to ensure complete sync
await new Promise(resolve => setTimeout(resolve, 10000));
return true;
}
} catch (queryError) {
console.error('[waitForNameOps] Error querying data:', queryError);
}
}

console.log('[waitForNameOps] No nameOps found yet, waiting 5 seconds...');
console.log('[waitForNameOps] No valid nameOps found yet, waiting 5 seconds...');
await new Promise(resolve => setTimeout(resolve, 5000));
} catch (error) {
console.error(`[waitForNameOps] Error in attempt ${attempt + 1}:`, error);
Expand All @@ -81,7 +92,7 @@ describe('Doichain Relay Pinning Service Test', function() {
}

before(async function() {
this.timeout(300000); // Increase timeout for initialization
this.timeout(600000); // Increase timeout for initialization and sync

try {
console.log('[Setup] Starting test node setup...');
Expand Down

0 comments on commit 9783273

Please sign in to comment.