Skip to content

Commit 3b486b1

Browse files
committed
refactor: simplify e2e electrum sync
1 parent 6d85a23 commit 3b486b1

File tree

14 files changed

+133
-205
lines changed

14 files changed

+133
-205
lines changed

__tests__/utils/wait-for-electrum.ts

Lines changed: 0 additions & 86 deletions
This file was deleted.

e2e/.eslintrc.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

e2e/backup.e2e.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ import {
77
launchAndWait,
88
completeOnboarding,
99
bitcoinURL,
10-
electrumHost,
11-
electrumPort,
1210
getSeed,
1311
restoreWallet,
1412
} from './helpers';
15-
import initWaitForElectrumToSync from '../__tests__/utils/wait-for-electrum';
13+
import initElectrum from './electrum';
1614

1715
d = checkComplete('backup-1') ? describe.skip : describe;
1816

1917
d('Backup', () => {
20-
let waitForElectrum;
18+
let electrum;
2119
const rpc = new BitcoinJsonRpc(bitcoinURL);
2220

2321
beforeAll(async () => {
@@ -31,19 +29,16 @@ d('Backup', () => {
3129
balance = await rpc.getBalance();
3230
}
3331

34-
waitForElectrum = await initWaitForElectrumToSync(
35-
{ host: electrumHost, port: electrumPort },
36-
bitcoinURL,
37-
);
32+
electrum = await initElectrum();
3833
});
3934

4035
beforeEach(async () => {
4136
await launchAndWait();
42-
await waitForElectrum();
37+
await electrum?.waitForSync();
4338
});
4439

4540
afterEach(() => {
46-
waitForElectrum?.close();
41+
electrum?.stop();
4742
});
4843

4944
it('Can backup metadata, widget, settings and restore them', async () => {
@@ -68,7 +63,7 @@ d('Backup', () => {
6863

6964
await rpc.sendToAddress(wAddress, '1');
7065
await rpc.generateToAddress(1, await rpc.getNewAddress());
71-
await waitForElectrum();
66+
await electrum?.waitForSync();
7267

7368
await waitFor(element(by.id('NewTxPrompt')))
7469
.toBeVisible()

e2e/boost.e2e.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ import {
99
launchAndWait,
1010
completeOnboarding,
1111
bitcoinURL,
12-
electrumHost,
13-
electrumPort,
1412
getSeed,
1513
restoreWallet,
1614
} from './helpers';
17-
import initWaitForElectrumToSync from '../__tests__/utils/wait-for-electrum';
15+
import initElectrum from './electrum';
1816

1917
const __DEV__ = process.env.DEV === 'true';
2018

2119
d = checkComplete(['boost-1', 'boost-2']) ? describe.skip : describe;
2220

2321
d('Boost', () => {
24-
let waitForElectrum;
22+
let electrum;
2523
const rpc = new BitcoinJsonRpc(bitcoinURL);
2624

2725
beforeAll(async () => {
@@ -33,14 +31,11 @@ d('Boost', () => {
3331
balance = await rpc.getBalance();
3432
}
3533

36-
waitForElectrum = await initWaitForElectrumToSync(
37-
{ host: electrumHost, port: electrumPort },
38-
bitcoinURL,
39-
);
34+
electrum = await initElectrum();
4035
});
4136

4237
afterAll(() => {
43-
waitForElectrum?.close();
38+
electrum?.stop();
4439
});
4540

4641
beforeEach(async () => {
@@ -69,7 +64,7 @@ d('Boost', () => {
6964
wAddress = wAddress.replace('bitcoin:', '');
7065
await rpc.sendToAddress(wAddress, '0.001');
7166
await rpc.generateToAddress(1, await rpc.getNewAddress());
72-
await waitForElectrum();
67+
await electrum?.waitForSync();
7368
await waitFor(element(by.id('NewTxPrompt')))
7469
.toBeVisible()
7570
.withTimeout(10000);
@@ -180,7 +175,7 @@ d('Boost', () => {
180175
wAddress = wAddress.replace('bitcoin:', '');
181176
await rpc.sendToAddress(wAddress, '0.001');
182177
await rpc.generateToAddress(1, await rpc.getNewAddress());
183-
await waitForElectrum();
178+
await electrum?.waitForSync();
184179
await waitFor(element(by.id('NewTxPrompt')))
185180
.toBeVisible()
186181
.withTimeout(10000);

e2e/electrum.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import net from 'net';
2+
import tls from 'tls';
3+
import BitcoinJsonRpc from 'bitcoin-json-rpc';
4+
import * as electrum from 'rn-electrum-client/helpers';
5+
import { EProtocol } from 'beignet';
6+
import { bitcoinURL, electrumHost, electrumPort, sleep } from './helpers';
7+
8+
const peer = {
9+
host: electrumHost,
10+
protocol: EProtocol.tcp,
11+
tcp: electrumPort,
12+
ssl: 60002,
13+
};
14+
15+
const TIMEOUT = 30 * 1000; // 30 seconds
16+
17+
// Connect to the Bitcoin Core node and Electrum server to wait for Electrum to sync
18+
const initElectrum = async (): Promise<{
19+
waitForSync: () => Promise<void>;
20+
stop: () => Promise<void>;
21+
}> => {
22+
let electrumHeight = 0;
23+
24+
try {
25+
const nodeRpc = new BitcoinJsonRpc(bitcoinURL);
26+
await electrum.start({
27+
network: 'bitcoinRegtest',
28+
customPeers: [peer],
29+
net,
30+
tls,
31+
});
32+
33+
const { data: header } = await electrum.subscribeHeader({
34+
network: 'bitcoinRegtest',
35+
onReceive: (data) => {
36+
electrumHeight = data[0].height;
37+
},
38+
});
39+
40+
electrumHeight = header.height;
41+
42+
// Wait for Electrum to sync with the Bitcoin node
43+
const waitForSync = async (): Promise<void> => {
44+
const startTime = Date.now();
45+
46+
while (true) {
47+
const nodeHeight = await nodeRpc.getBlockCount();
48+
49+
if (nodeHeight === electrumHeight) {
50+
break;
51+
}
52+
53+
if (Date.now() - startTime > TIMEOUT) {
54+
throw new Error('Electrum sync timeout');
55+
}
56+
57+
await sleep(1000);
58+
}
59+
};
60+
61+
return {
62+
waitForSync,
63+
stop: electrum.stop,
64+
};
65+
} catch (error) {
66+
await electrum.stop();
67+
throw error;
68+
}
69+
};
70+
71+
export default initElectrum;

e2e/lightning.e2e.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ import createLndRpc from '@radar/lnrpc';
22
import BitcoinJsonRpc from 'bitcoin-json-rpc';
33
import { device } from 'detox';
44

5-
import initWaitForElectrumToSync from '../__tests__/utils/wait-for-electrum';
5+
import initElectrum from './electrum';
66
import {
77
bitcoinURL,
88
lndConfig,
99
checkComplete,
1010
completeOnboarding,
11-
electrumHost,
12-
electrumPort,
1311
launchAndWait,
1412
markComplete,
1513
sleep,
@@ -22,7 +20,7 @@ import {
2220
d = checkComplete('lighting-1') ? describe.skip : describe;
2321

2422
d('Lightning', () => {
25-
let waitForElectrum;
23+
let electrum;
2624
const rpc = new BitcoinJsonRpc(bitcoinURL);
2725

2826
beforeAll(async () => {
@@ -34,21 +32,18 @@ d('Lightning', () => {
3432
balance = await rpc.getBalance();
3533
}
3634

37-
waitForElectrum = await initWaitForElectrumToSync(
38-
{ host: electrumHost, port: electrumPort },
39-
bitcoinURL,
40-
);
35+
electrum = await initElectrum();
4136

4237
await completeOnboarding();
4338
});
4439

4540
beforeEach(async () => {
4641
await launchAndWait();
47-
await waitForElectrum();
42+
await electrum?.waitForSync();
4843
});
4944

5045
afterEach(() => {
51-
waitForElectrum?.close();
46+
electrum?.stop();
5247
});
5348

5449
d('Receive and Send', () => {
@@ -71,7 +66,7 @@ d('Lightning', () => {
7166
const { address: lndAddress } = await lnd.newAddress();
7267
await rpc.sendToAddress(lndAddress, '1');
7368
await rpc.generateToAddress(1, await rpc.getNewAddress());
74-
await waitForElectrum();
69+
await electrum?.waitForSync();
7570
const { identityPubkey: lndNodeID } = await lnd.getInfo();
7671

7772
// get LDK Node id
@@ -110,7 +105,7 @@ d('Lightning', () => {
110105
private: true,
111106
});
112107
await rpc.generateToAddress(6, await rpc.getNewAddress());
113-
await waitForElectrum();
108+
await electrum?.waitForSync();
114109

115110
// wait for channel to be active
116111
await waitForActiveChannel(lnd, ldkNodeId);
@@ -353,7 +348,7 @@ d('Lightning', () => {
353348
}
354349

355350
await rpc.generateToAddress(6, await rpc.getNewAddress());
356-
await waitForElectrum();
351+
await electrum?.waitForSync();
357352
await expect(element(by.id('Channel')).atIndex(0)).not.toExist();
358353
await element(by.id('NavigationBack')).atIndex(0).tap();
359354
await element(by.id('NavigationClose')).atIndex(0).tap();

0 commit comments

Comments
 (0)