-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto switch indexer or common sync
- Loading branch information
1 parent
16fa2a4
commit ece382f
Showing
3 changed files
with
129 additions
and
61 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
packages/neuron-wallet/src/startup/sync-block-task/indexer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { remote } from 'electron' | ||
import AddressService from 'services/addresses' | ||
import LockUtils from 'models/lock-utils' | ||
import IndexerQueue from 'services/indexer/queue' | ||
|
||
import { initDatabase } from './init-database' | ||
|
||
const { addressDbChangedSubject } = remote.require('./startup/sync-block-task/params') | ||
|
||
// maybe should call this every time when new address generated | ||
// load all addresses and convert to lockHashes | ||
export const loadAddressesAndConvert = async (): Promise<string[]> => { | ||
const addresses: string[] = (await AddressService.allAddresses()).map(addr => addr.address) | ||
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses) | ||
return lockHashes | ||
} | ||
|
||
// call this after network switched | ||
let indexerQueue: IndexerQueue | undefined | ||
export const switchNetwork = async () => { | ||
// stop all blocks service | ||
if (indexerQueue) { | ||
await indexerQueue.stopAndWait() | ||
} | ||
|
||
// disconnect old connection and connect to new database | ||
await initDatabase() | ||
// load lockHashes | ||
const lockHashes: string[] = await loadAddressesAndConvert() | ||
// start sync blocks service | ||
indexerQueue = new IndexerQueue(lockHashes) | ||
|
||
addressDbChangedSubject.subscribe(async (event: string) => { | ||
// ignore update and remove | ||
if (event === 'AfterInsert') { | ||
const hashes: string[] = await loadAddressesAndConvert() | ||
if (indexerQueue) { | ||
indexerQueue.setLockHashes(hashes) | ||
} | ||
} | ||
}) | ||
|
||
indexerQueue.start() | ||
} |
65 changes: 65 additions & 0 deletions
65
packages/neuron-wallet/src/startup/sync-block-task/sync.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { remote } from 'electron' | ||
import AddressService from 'services/addresses' | ||
import LockUtils from 'models/lock-utils' | ||
import BlockListener from 'services/sync/block-listener' | ||
|
||
import { initDatabase } from './init-database' | ||
|
||
const { nodeService, addressDbChangedSubject, walletCreatedSubject } = remote.require( | ||
'./startup/sync-block-task/params' | ||
) | ||
|
||
// pass to task a main process subject | ||
// AddressesUsedSubject.setSubject(addressesUsedSubject) | ||
|
||
// maybe should call this every time when new address generated | ||
// load all addresses and convert to lockHashes | ||
export const loadAddressesAndConvert = async (): Promise<string[]> => { | ||
const addresses: string[] = (await AddressService.allAddresses()).map(addr => addr.address) | ||
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses) | ||
return lockHashes | ||
} | ||
|
||
// call this after network switched | ||
let blockListener: BlockListener | undefined | ||
export const switchNetwork = async () => { | ||
// stop all blocks service | ||
if (blockListener) { | ||
await blockListener.stopAndWait() | ||
} | ||
|
||
// disconnect old connection and connect to new database | ||
await initDatabase() | ||
// load lockHashes | ||
const lockHashes: string[] = await loadAddressesAndConvert() | ||
// start sync blocks service | ||
blockListener = new BlockListener(lockHashes, nodeService.tipNumberSubject) | ||
|
||
addressDbChangedSubject.subscribe(async (event: string) => { | ||
// ignore update and remove | ||
if (event === 'AfterInsert') { | ||
const hashes: string[] = await loadAddressesAndConvert() | ||
if (blockListener) { | ||
blockListener.setLockHashes(hashes) | ||
} | ||
} | ||
}) | ||
|
||
const regenerateListener = async () => { | ||
if (blockListener) { | ||
await blockListener.stopAndWait() | ||
} | ||
// wait former queue to be drained | ||
const hashes: string[] = await loadAddressesAndConvert() | ||
blockListener = new BlockListener(hashes, nodeService.tipNumberSubject) | ||
await blockListener.start(true) | ||
} | ||
|
||
walletCreatedSubject.subscribe(async (type: string) => { | ||
if (type === 'import') { | ||
await regenerateListener() | ||
} | ||
}) | ||
|
||
blockListener.start() | ||
} |
81 changes: 20 additions & 61 deletions
81
packages/neuron-wallet/src/startup/sync-block-task/task.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters