Skip to content

Commit 2cdba31

Browse files
committed
fix: context.js tried to read docker/lnd/admin.macaroon before LND finished initializing
1 parent 8b02435 commit 2cdba31

File tree

3 files changed

+110
-19
lines changed

3 files changed

+110
-19
lines changed

.github/workflows/mocha-android.yml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,29 @@ jobs:
7979
chmod 777 lnd clightning
8080
docker compose up -d --quiet-pull
8181
82-
- name: Wait for electrum server
83-
timeout-minutes: 2
84-
run: while ! nc -z '127.0.0.1' 60001; do sleep 1; done
82+
- name: Wait for electrum server, LND and CLightning
83+
timeout-minutes: 5
84+
run: |
85+
# Wait for Electrum
86+
while ! nc -z '127.0.0.1' 60001; do
87+
echo "Waiting for Electrum..."
88+
sleep 1
89+
done
90+
echo "Electrum is ready!"
91+
92+
# Wait for LND macaroon file
93+
sudo bash -c "while [ ! -f example/docker/lnd/admin.macaroon ]; do echo 'Waiting for LND macaroon...'; sleep 2; done"
94+
sudo chmod -R 777 example/docker/lnd
95+
echo "LND macaroon found!"
96+
97+
# Wait for CLightning to be ready
98+
while ! docker logs clightning 2>&1 | grep -q "Server started with public key"; do
99+
echo "Waiting for CLightning..."
100+
sleep 2
101+
done
102+
echo "CLightning is ready!"
103+
104+
echo "All services ready!"
85105
86106
- name: Install lib dependencies
87107
working-directory: lib
@@ -122,6 +142,7 @@ jobs:
122142
npx react-native run-android --no-packager
123143
124144
- name: run tests
145+
timeout-minutes: 30
125146
uses: reactivecircus/android-emulator-runner@v2
126147
with:
127148
api-level: 34

.github/workflows/mocha-ios.yml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,32 @@ jobs:
4545
chmod 777 lnd clightning
4646
docker compose up -d --quiet-pull
4747
48-
- name: Wait for electrum server
49-
timeout-minutes: 2
50-
run: while ! nc -z '127.0.0.1' 60001; do sleep 1; done
48+
- name: Wait for electrum server, LND and CLightning
49+
timeout-minutes: 5
50+
run: |
51+
# Wait for Electrum
52+
while ! nc -z '127.0.0.1' 60001; do
53+
echo "Waiting for Electrum..."
54+
sleep 1
55+
done
56+
echo "Electrum is ready!"
57+
58+
# Wait for LND macaroon file
59+
cd example
60+
while [ ! -f docker/lnd/admin.macaroon ]; do
61+
echo "Waiting for LND macaroon..."
62+
sleep 2
63+
done
64+
echo "LND macaroon found!"
65+
66+
# Wait for CLightning to be ready
67+
while ! docker logs clightning 2>&1 | grep -q "Server started with public key"; do
68+
echo "Waiting for CLightning..."
69+
sleep 2
70+
done
71+
echo "CLightning is ready!"
72+
73+
echo "All services ready!"
5174
5275
- name: Node
5376
uses: actions/setup-node@v4
@@ -97,6 +120,7 @@ jobs:
97120
run: npx react-native run-ios --no-packager --simulator='iPhone 15'
98121

99122
- name: Test iOS app
123+
timeout-minutes: 30
100124
working-directory: example
101125
run: yarn test:mocha:ios
102126

example/tests/context.js

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,64 @@
11
const fs = require('fs');
22
const run = require('child_process').execSync;
33

4-
// read lnd macaroon
5-
const lndmacaroon = fs
6-
.readFileSync('docker/lnd/admin.macaroon')
7-
.toString('hex')
8-
.toUpperCase();
4+
// Helper function to retry file reads
5+
function readFileWithRetry(path, maxRetries = 30, delay = 1000) {
6+
for (let i = 0; i < maxRetries; i++) {
7+
try {
8+
return fs.readFileSync(path);
9+
} catch (err) {
10+
if (i === maxRetries - 1) {
11+
console.error(`Failed to read ${path} after ${maxRetries} attempts`);
12+
throw err;
13+
}
14+
console.error(
15+
`Attempt ${i + 1}/${maxRetries}: Waiting for ${path}...`,
16+
);
17+
// Sleep synchronously (only works on Unix-like systems)
18+
run(`sleep ${delay / 1000}`);
19+
}
20+
}
21+
}
922

10-
// run command to read clightnng rune
11-
const clightning = run(
12-
'cd docker; docker compose exec --user clightning clightning lightning-cli createrune --regtest',
13-
);
14-
const lcrune = JSON.parse(clightning).rune;
23+
// Helper function to retry docker commands
24+
function runWithRetry(cmd, maxRetries = 30, delay = 1000) {
25+
for (let i = 0; i < maxRetries; i++) {
26+
try {
27+
return run(cmd, { encoding: 'utf8' });
28+
} catch (err) {
29+
if (i === maxRetries - 1) {
30+
console.error(`Failed to run command after ${maxRetries} attempts: ${cmd}`);
31+
throw err;
32+
}
33+
console.error(
34+
`Attempt ${i + 1}/${maxRetries}: Command failed, retrying: ${cmd}`,
35+
);
36+
run(`sleep ${delay / 1000}`);
37+
}
38+
}
39+
}
1540

16-
const context = { lndmacaroon, lcrune };
17-
const encoded = Buffer.from(JSON.stringify(context)).toString('hex');
18-
console.log(encoded);
41+
try {
42+
// Read lnd macaroon with retry
43+
console.log('Waiting for LND macaroon...');
44+
const lndmacaroon = readFileWithRetry('docker/lnd/admin.macaroon')
45+
.toString('hex')
46+
.toUpperCase();
47+
console.log('LND macaroon loaded successfully');
48+
49+
// Run command to read clightning rune with retry
50+
console.log('Waiting for CLightning to be ready...');
51+
const clightning = runWithRetry(
52+
'cd docker; docker compose exec --user clightning clightning lightning-cli createrune --regtest',
53+
);
54+
const lcrune = JSON.parse(clightning).rune;
55+
console.log('CLightning rune generated successfully');
56+
57+
const context = { lndmacaroon, lcrune };
58+
const encoded = Buffer.from(JSON.stringify(context)).toString('hex');
59+
console.log(encoded);
60+
} catch (error) {
61+
console.error('Failed to prepare test context:', error.message);
62+
console.error(error.stack);
63+
process.exit(1);
64+
}

0 commit comments

Comments
 (0)