Skip to content

Commit ece382f

Browse files
committed
feat: auto switch indexer or common sync
1 parent 16fa2a4 commit ece382f

File tree

3 files changed

+129
-61
lines changed

3 files changed

+129
-61
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { remote } from 'electron'
2+
import AddressService from 'services/addresses'
3+
import LockUtils from 'models/lock-utils'
4+
import IndexerQueue from 'services/indexer/queue'
5+
6+
import { initDatabase } from './init-database'
7+
8+
const { addressDbChangedSubject } = remote.require('./startup/sync-block-task/params')
9+
10+
// maybe should call this every time when new address generated
11+
// load all addresses and convert to lockHashes
12+
export const loadAddressesAndConvert = async (): Promise<string[]> => {
13+
const addresses: string[] = (await AddressService.allAddresses()).map(addr => addr.address)
14+
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses)
15+
return lockHashes
16+
}
17+
18+
// call this after network switched
19+
let indexerQueue: IndexerQueue | undefined
20+
export const switchNetwork = async () => {
21+
// stop all blocks service
22+
if (indexerQueue) {
23+
await indexerQueue.stopAndWait()
24+
}
25+
26+
// disconnect old connection and connect to new database
27+
await initDatabase()
28+
// load lockHashes
29+
const lockHashes: string[] = await loadAddressesAndConvert()
30+
// start sync blocks service
31+
indexerQueue = new IndexerQueue(lockHashes)
32+
33+
addressDbChangedSubject.subscribe(async (event: string) => {
34+
// ignore update and remove
35+
if (event === 'AfterInsert') {
36+
const hashes: string[] = await loadAddressesAndConvert()
37+
if (indexerQueue) {
38+
indexerQueue.setLockHashes(hashes)
39+
}
40+
}
41+
})
42+
43+
indexerQueue.start()
44+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { remote } from 'electron'
2+
import AddressService from 'services/addresses'
3+
import LockUtils from 'models/lock-utils'
4+
import BlockListener from 'services/sync/block-listener'
5+
6+
import { initDatabase } from './init-database'
7+
8+
const { nodeService, addressDbChangedSubject, walletCreatedSubject } = remote.require(
9+
'./startup/sync-block-task/params'
10+
)
11+
12+
// pass to task a main process subject
13+
// AddressesUsedSubject.setSubject(addressesUsedSubject)
14+
15+
// maybe should call this every time when new address generated
16+
// load all addresses and convert to lockHashes
17+
export const loadAddressesAndConvert = async (): Promise<string[]> => {
18+
const addresses: string[] = (await AddressService.allAddresses()).map(addr => addr.address)
19+
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses)
20+
return lockHashes
21+
}
22+
23+
// call this after network switched
24+
let blockListener: BlockListener | undefined
25+
export const switchNetwork = async () => {
26+
// stop all blocks service
27+
if (blockListener) {
28+
await blockListener.stopAndWait()
29+
}
30+
31+
// disconnect old connection and connect to new database
32+
await initDatabase()
33+
// load lockHashes
34+
const lockHashes: string[] = await loadAddressesAndConvert()
35+
// start sync blocks service
36+
blockListener = new BlockListener(lockHashes, nodeService.tipNumberSubject)
37+
38+
addressDbChangedSubject.subscribe(async (event: string) => {
39+
// ignore update and remove
40+
if (event === 'AfterInsert') {
41+
const hashes: string[] = await loadAddressesAndConvert()
42+
if (blockListener) {
43+
blockListener.setLockHashes(hashes)
44+
}
45+
}
46+
})
47+
48+
const regenerateListener = async () => {
49+
if (blockListener) {
50+
await blockListener.stopAndWait()
51+
}
52+
// wait former queue to be drained
53+
const hashes: string[] = await loadAddressesAndConvert()
54+
blockListener = new BlockListener(hashes, nodeService.tipNumberSubject)
55+
await blockListener.start(true)
56+
}
57+
58+
walletCreatedSubject.subscribe(async (type: string) => {
59+
if (type === 'import') {
60+
await regenerateListener()
61+
}
62+
})
63+
64+
blockListener.start()
65+
}

packages/neuron-wallet/src/startup/sync-block-task/task.ts

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,45 @@
11
import { remote } from 'electron'
22
import { initConnection as initAddressConnection } from 'database/address/ormconfig'
3-
import AddressService from 'services/addresses'
4-
import LockUtils from 'models/lock-utils'
53
import AddressesUsedSubject from 'models/subjects/addresses-used-subject'
6-
import BlockListener from 'services/sync/block-listener'
74
import { NetworkWithID } from 'services/networks'
85
import { register as registerTxStatusListener } from 'listeners/tx-status'
96
import { register as registerAddressListener } from 'listeners/address'
7+
import IndexerRPC from 'services/indexer/indexer-rpc'
8+
import Utils from 'services/sync/utils'
109

11-
import { initDatabase } from './init-database'
10+
import { switchNetwork as syncSwitchNetwork } from './sync'
11+
import { switchNetwork as indexerSwitchNetwork } from './indexer'
1212

1313
// register to listen address updates
1414
registerAddressListener()
1515

16-
const {
17-
nodeService,
18-
addressDbChangedSubject,
19-
addressesUsedSubject,
20-
databaseInitSubject,
21-
walletCreatedSubject,
22-
} = remote.require('./startup/sync-block-task/params')
16+
const { addressesUsedSubject, databaseInitSubject } = remote.require('./startup/sync-block-task/params')
2317

2418
// pass to task a main process subject
2519
AddressesUsedSubject.setSubject(addressesUsedSubject)
2620

27-
// maybe should call this every time when new address generated
28-
// load all addresses and convert to lockHashes
29-
export const loadAddressesAndConvert = async (): Promise<string[]> => {
30-
const addresses: string[] = (await AddressService.allAddresses()).map(addr => addr.address)
31-
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses)
32-
return lockHashes
33-
}
34-
35-
// call this after network switched
36-
let blockListener: BlockListener | undefined
37-
export const switchNetwork = async () => {
38-
// stop all blocks service
39-
if (blockListener) {
40-
await blockListener.stopAndWait()
21+
export const testIndexer = async (): Promise<boolean> => {
22+
const indexerRPC = new IndexerRPC()
23+
try {
24+
await Utils.retry(3, 100, () => {
25+
return indexerRPC.getLockHashIndexStates()
26+
})
27+
return true
28+
} catch {
29+
return false
4130
}
42-
43-
// disconnect old connection and connect to new database
44-
await initDatabase()
45-
// load lockHashes
46-
const lockHashes: string[] = await loadAddressesAndConvert()
47-
// start sync blocks service
48-
blockListener = new BlockListener(lockHashes, nodeService.tipNumberSubject)
49-
50-
addressDbChangedSubject.subscribe(async (event: string) => {
51-
// ignore update and remove
52-
if (event === 'AfterInsert') {
53-
const hashes: string[] = await loadAddressesAndConvert()
54-
if (blockListener) {
55-
blockListener.setLockHashes(hashes)
56-
}
57-
}
58-
})
59-
60-
const regenerateListener = async () => {
61-
if (blockListener) {
62-
await blockListener.stopAndWait()
63-
}
64-
// wait former queue to be drained
65-
const hashes: string[] = await loadAddressesAndConvert()
66-
blockListener = new BlockListener(hashes, nodeService.tipNumberSubject)
67-
await blockListener.start(true)
68-
}
69-
70-
walletCreatedSubject.subscribe(async (type: string) => {
71-
if (type === 'import') {
72-
await regenerateListener()
73-
}
74-
})
75-
76-
blockListener.start()
7731
}
7832

7933
export const run = async () => {
8034
await initAddressConnection()
8135
databaseInitSubject.subscribe(async (network: NetworkWithID | undefined) => {
8236
if (network) {
83-
await switchNetwork()
37+
const indexerEnable = await testIndexer()
38+
if (indexerEnable) {
39+
await indexerSwitchNetwork()
40+
} else {
41+
await syncSwitchNetwork()
42+
}
8443
}
8544
})
8645
registerTxStatusListener()

0 commit comments

Comments
 (0)