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

Add regtest entrypoint to mine blocks #6

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
9ca1431
feat: add address generation + block mining in regtest for Docker Com…
devin-ai-integration[bot] Dec 23, 2024
d1aa409
feat: add OrbitDB readiness checks to ensure nameOps are indexed befo…
devin-ai-integration[bot] Dec 23, 2024
9240b3c
fix: improve test reliability with better error handling and logging
devin-ai-integration[bot] Dec 23, 2024
0dcc578
fix: add comprehensive logging and readiness checks for nameOps indexing
devin-ai-integration[bot] Dec 23, 2024
78d4da8
fix: enhance waitForNameOps with relay connection checks and improved…
devin-ai-integration[bot] Dec 23, 2024
9783273
fix: improve ElectrumX connection handling and timing
devin-ai-integration[bot] Dec 23, 2024
5ca63fd
fix: correct OrbitDB initialization in tests to match production pattern
devin-ai-integration[bot] Dec 23, 2024
4222ec0
test: improve test reliability with nameOps creation and verification
devin-ai-integration[bot] Dec 23, 2024
c989239
fix: add nameOp creation for Silk Road cities and improve service timing
devin-ai-integration[bot] Dec 23, 2024
a9a0afe
fix: improve test reliability and nameOp creation
devin-ai-integration[bot] Dec 23, 2024
38fd4dd
fix: remove duplicate depends_on in docker-compose-test.yml
devin-ai-integration[bot] Dec 23, 2024
112533b
fix: consolidate service wait conditions and improve reliability
devin-ai-integration[bot] Dec 23, 2024
50aa640
fix: expose port 9090 for libp2p connectivity
devin-ai-integration[bot] Dec 23, 2024
d2f527e
fix: improve service initialization and connectivity
devin-ai-integration[bot] Dec 23, 2024
ce3289b
fix: improve service initialization reliability
devin-ai-integration[bot] Dec 23, 2024
717a8b9
fix: improve container networking and test reliability - Update relay…
devin-ai-integration[bot] Dec 23, 2024
2a0b0cb
fix: add electrumx healthcheck and fix service dependencies
devin-ai-integration[bot] Dec 23, 2024
07dae7b
fix: improve security by using GitHub secrets for private key
devin-ai-integration[bot] Dec 23, 2024
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
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
restore-keys: |
${{ runner.os }}-buildx-
- name: Set up Docker Compose
env:
TEST_RELAY_PRIVATE_KEY: ${{ secrets.TEST_RELAY_PRIVATE_KEY }}
uses: hoverkraft-tech/[email protected]
with:
compose-file: "docker-compose-test.yml"
Expand All @@ -35,4 +37,4 @@ jobs:
- name: Running-Tests
run: |
echo "🧪 Running tests..."
docker compose -f docker-compose-test.yml exec relay-service npm test
docker compose -f docker-compose-test.yml exec relay-service npm test
32 changes: 26 additions & 6 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ services:
- "18332:18332"
- "18443:18443"
- "18445:18445"
# command: ["/usr/local/bin/doichaind"]
volumes:
- ./docker/doichain-regtest.conf:/home/doichain/.doichain/doichain.conf
- ./docker/regtest-entrypoint.sh:/home/doichain/regtest-entrypoint.sh
entrypoint: ["/bin/bash", "/home/doichain/regtest-entrypoint.sh"]

electrumx:
container_name: electrumx
Expand All @@ -24,6 +25,12 @@ services:
- "50001:50001"
- "50002:50002"
- "8443:8443"
healthcheck:
test: ["CMD", "nc", "-z", "localhost", "50001"]
interval: 5s
timeout: 3s
retries: 3
start_period: 10s
stdin_open: true
tty: true
privileged: true
Expand All @@ -35,15 +42,28 @@ services:
volumes:
- ./docker/entrypoint.sh:/usr/src/app/entrypoint.sh
# - ./:/usr/src/app/
entrypoint: ["/usr/src/app/entrypoint.sh", "generate-key"]
entrypoint: ["/usr/src/app/entrypoint.sh", "start"]
healthcheck:
test: ["CMD", "nc", "-z", "localhost", "9090"] # Check libp2p port instead
interval: 5s
timeout: 3s
retries: 3
depends_on:
regtest:
condition: service_started
electrumx:
condition: service_healthy
ports:
- "1235:1235"
- "9090:9090" # Required for libp2p connectivity
- "9091:9091"
- "12345:12345"
- "3000:3000"
depends_on:
- regtest
- electrumx
environment:
- RELAY_LISTEN_ADDRESSES=/ip4/0.0.0.0/tcp/9090,/ip4/0.0.0.0/tcp/9091
- RELAY_ANNOUNCE_ADDRESSES=/dns4/relay-service/tcp/9090,/dns4/relay-service/tcp/9091 # Use container DNS name
- RELAY_DEV_MODE=true # Enable dev mode for testing
- RELAY_PRIVATE_KEY=${TEST_RELAY_PRIVATE_KEY} # Use environment variable for security

volumes:
doichain-data:
doichain-data:
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"]
74 changes: 69 additions & 5 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
#!/bin/bash
rm scanning-state.json
rm -f scanning-state.json

# Function to check if basic services are ready
check_basic_services() {
nc -z electrumx 8443 2>/dev/null && nc -z regtest 18443 2>/dev/null
return $?
}

# Function to check if relay service is ready
check_relay_service() {
# Check both HTTP and libp2p ports
(nc -z localhost 3000 2>/dev/null && nc -z localhost 9090 2>/dev/null && nc -z localhost 9091 2>/dev/null) || return 1

# Try to get nameOps count from HTTP API
response=$(curl -s http://localhost:3000/api/v1/nameops/count 2>/dev/null)
if [ $? -eq 0 ] && [ "$response" != "" ]; then
echo "Relay API responding with nameOps count: $response"
return 0
fi
return 1
}

# Function to wait for services with timeout
wait_for_services() {
echo "Waiting for basic services to be ready..."
local timeout=180
local count=0

# First wait for ElectrumX and regtest
while ! check_basic_services; do
count=$((count + 1))
if [ $count -gt $timeout ]; then
echo "Timeout waiting for ElectrumX and regtest"
return 1
fi
echo "Attempt $count/$timeout: Waiting for ElectrumX and regtest..."
sleep 1
done
echo "ElectrumX and regtest are ready!"

# Then wait for relay service to be ready
count=0
echo "Starting relay service..."
npm run start &

echo "Waiting for relay service to initialize..."
while ! check_relay_service; do
count=$((count + 1))
if [ $count -gt $timeout ]; then
echo "Timeout waiting for relay service"
return 1
fi
echo "Attempt $count/$timeout: Waiting for relay service..."
sleep 1
done
echo "Relay service is ready!"
return 0
}

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

# When generating key, just do that and exit
echo "Private key generated: $key_output"
exit 0
elif [ "$1" == "start" ]; then
echo "Starting node..."
# Wait for services and start only once
if ! wait_for_services; then
echo "Failed to connect to required services"
exit 1
fi

# Start the service (only once)
npm run start
else
echo "Invalid command. Use 'generate-key' or 'start'."
exit 1
fi

# Start the application if a valid command was provided
# exec "$@"
59 changes: 59 additions & 0 deletions docker/regtest-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash
set -e

echo "Starting doichaind in regtest mode..."
doichaind -regtest -daemon

echo "Waiting for doichaind to be ready..."
while ! doichain-cli -regtest -rpcuser=admin -rpcpassword=adminpw -rpcwait getblockchaininfo > /dev/null 2>&1; do
echo "Waiting for doichaind..."
sleep 1
done
echo "doichaind is ready!"

echo "Generating a new address..."
NEWADDR=$(doichain-cli -regtest -rpcuser=admin -rpcpassword=adminpw -rpcwait getnewaddress)

echo "Mining 200 blocks to $NEWADDR..."
doichain-cli -regtest -rpcuser=admin -rpcpassword=adminpw -rpcwait generatetoaddress 200 "$NEWADDR"

echo "Mined 200 blocks to $NEWADDR"

echo "Creating nameOps for Silk Road cities..."
CITIES=("Xi'an" "Dunhuang" "Kashgar" "Samarkand" "Bukhara" "Merv" "Baghdad" "Damascus" "Constantinople" "Venice" "Chang'an" "Karakorum" "Balkh" "Nishapur" "Ctesiphon" "Antioch" "Tyre" "Alexandria" "Rome" "Luoyang")

for city in "${CITIES[@]}"; do
echo "Creating nameOp for $city..."
TXID=$(doichain-cli -regtest -rpcuser=admin -rpcpassword=adminpw -rpcwait name_doi "$city" "value_for_$city")
if [ $? -ne 0 ]; then
echo "Failed to create nameOp for $city"
exit 1
fi
echo "Created nameOp with txid: $TXID"

# Mine a block to confirm the transaction
BLOCK=$(doichain-cli -regtest -rpcuser=admin -rpcpassword=adminpw -rpcwait generatetoaddress 1 "$NEWADDR")
if [ $? -ne 0 ]; then
echo "Failed to mine block for $city nameOp"
exit 1
fi
echo "Mined block $BLOCK to confirm nameOp for $city"

# Verify the name exists
NAME_INFO=$(doichain-cli -regtest -rpcuser=admin -rpcpassword=adminpw -rpcwait name_show "$city")
if [ $? -ne 0 ]; then
echo "Failed to verify nameOp for $city"
exit 1
fi
echo "Verified nameOp exists for $city: $NAME_INFO"
done

echo "Created nameOps for all cities and mined confirmation blocks"

# Mine additional blocks periodically to ensure transactions are confirmed
echo "Starting periodic block mining..."
while true; do
echo "Mining additional block..."
doichain-cli -regtest -rpcuser=admin -rpcpassword=adminpw -rpcwait generatetoaddress 1 "$NEWADDR"
sleep 30
done
34 changes: 27 additions & 7 deletions relay/src/nodeFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,33 @@ export async function createNode(privKeyHex, datastore, blockstore, listenAddres
]
});

// Create OrbitDB instance
const orbitdb = await createOrbitDB({
ipfs: helia,
directory: './orbitdb', // Base directory for OrbitDB data
id: 'doichain-relay', // Optional identifier
});
logger.info('OrbitDB initialized');
// Create OrbitDB instance with retries
let orbitdb;
const maxRetries = 10;
let attempt = 0;

while (attempt < maxRetries) {
try {
orbitdb = await createOrbitDB({
ipfs: helia,
directory: './orbitdb',
id: 'doichain-relay',
});
logger.info('OrbitDB initialized successfully', {
attempt: attempt + 1,
address: orbitdb.address,
id: orbitdb.id
});
break;
} catch (error) {
attempt++;
logger.warn(`Failed to initialize OrbitDB (attempt ${attempt}/${maxRetries})`, { error });
if (attempt === maxRetries) {
throw new Error(`Failed to initialize OrbitDB after ${maxRetries} attempts`);
}
await new Promise(resolve => setTimeout(resolve, 2000)); // Wait 2 seconds before retrying
}
}

console.log('Helia peerId:', helia.libp2p.peerId.toString());
console.log('Configured listen addresses:', listenAddresses);
Expand Down
Loading
Loading