Skip to content

Commit 50a3c73

Browse files
committed
feat: not start from zero in normal sync when create wallet
1 parent 342061d commit 50a3c73

3 files changed

Lines changed: 74 additions & 24 deletions

File tree

packages/neuron-wallet/src/services/sync/block-listener.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Queue from './queue'
66
import RangeForCheck from './range-for-check'
77
import BlockNumber from './block-number'
88
import GetBlocks from './get-blocks'
9+
import Utils from './utils'
910

1011
export default class BlockListener {
1112
private lockHashes: string[]
@@ -34,6 +35,15 @@ export default class BlockListener {
3435
this.queue.setLockHashes(lockHashes)
3536
}
3637

38+
public appendLockHashes = (lockHashes: string[]) => {
39+
const hashes = this.lockHashes.concat(lockHashes)
40+
this.setLockHashes(hashes)
41+
}
42+
43+
public getLockHashes = (): string[] => {
44+
return this.lockHashes
45+
}
46+
3747
// start listening
3848
public start = async (restart: boolean = false) => {
3949
if (restart) {
@@ -57,6 +67,28 @@ export default class BlockListener {
5767
})
5868
}
5969

70+
/* eslint no-await-in-loop: "off" */
71+
/* eslint no-restricted-syntax: "off" */
72+
public setToTip = async () => {
73+
const timeout = 5000
74+
let number: bigint = BigInt(0)
75+
const tipNumberListener = this.tipNumberSubject.subscribe(async num => {
76+
if (num) {
77+
number = BigInt(num)
78+
}
79+
})
80+
const startAt = +new Date()
81+
while (number === BigInt(0)) {
82+
const now = +new Date()
83+
if (now - startAt > timeout) {
84+
return
85+
}
86+
await Utils.sleep(100)
87+
}
88+
await this.currentBlockNumber.updateCurrent(this.tipBlockNumber)
89+
tipNumberListener.unsubscribe()
90+
}
91+
6092
public stop = () => {
6193
if (this.tipNumberListener) {
6294
this.tipNumberListener.unsubscribe()

packages/neuron-wallet/src/services/sync/queue.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,22 @@ export default class Queue {
5151
try {
5252
this.inProcess = true
5353

54-
const current: bigint = await this.currentBlockNumber.getCurrent()
55-
const startNumber: bigint = current + BigInt(1)
56-
const endNumber: bigint = current + BigInt(this.fetchSize)
57-
const realEndNumber: bigint = endNumber < this.endBlockNumber ? endNumber : this.endBlockNumber
58-
59-
if (realEndNumber >= this.endBlockNumber) {
60-
this.yieldTime = 1000
61-
} else {
62-
this.yieldTime = 1
63-
}
64-
65-
if (realEndNumber >= startNumber) {
66-
const rangeArr = Utils.rangeForBigInt(startNumber, realEndNumber).map(num => num.toString())
67-
await this.pipeline(rangeArr)
54+
if (this.lockHashes.length !== 0) {
55+
const current: bigint = await this.currentBlockNumber.getCurrent()
56+
const startNumber: bigint = current + BigInt(1)
57+
const endNumber: bigint = current + BigInt(this.fetchSize)
58+
const realEndNumber: bigint = endNumber < this.endBlockNumber ? endNumber : this.endBlockNumber
59+
60+
if (realEndNumber >= this.endBlockNumber) {
61+
this.yieldTime = 1000
62+
} else {
63+
this.yieldTime = 1
64+
}
65+
66+
if (realEndNumber >= startNumber) {
67+
const rangeArr = Utils.rangeForBigInt(startNumber, realEndNumber).map(num => num.toString())
68+
await this.pipeline(rangeArr)
69+
}
6870
}
6971
} catch (err) {
7072
if (err.message.startsWith('connect ECONNREFUSED')) {

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { remote } from 'electron'
2-
import AddressService from 'services/addresses'
2+
import AddressService, { AddressWithWay } from 'services/addresses'
33
import LockUtils from 'models/lock-utils'
44
import BlockListener from 'services/sync/block-listener'
55

66
import { initDatabase } from './init-database'
77

8-
const { nodeService, addressDbChangedSubject, walletCreatedSubject } = remote.require(
9-
'./startup/sync-block-task/params'
10-
)
8+
const { nodeService, addressCreatedSubject, walletCreatedSubject } = remote.require('./startup/sync-block-task/params')
9+
10+
export interface LockHashInfo {
11+
lockHash: string
12+
isImport: boolean | undefined
13+
}
1114

1215
// pass to task a main process subject
1316
// AddressesUsedSubject.setSubject(addressesUsedSubject)
@@ -35,13 +38,26 @@ export const switchNetwork = async () => {
3538
// start sync blocks service
3639
blockListener = new BlockListener(lockHashes, nodeService.tipNumberSubject)
3740

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)
41+
// listen to address created
42+
addressCreatedSubject.subscribe(async (addressWithWay: AddressWithWay[]) => {
43+
if (blockListener) {
44+
const infos: LockHashInfo[] = (await Promise.all(
45+
addressWithWay.map(async aw => {
46+
const hashes: string[] = await LockUtils.addressToAllLockHashes(aw.address.address)
47+
return hashes.map(h => {
48+
return {
49+
lockHash: h,
50+
isImport: aw.isImport,
51+
}
52+
})
53+
})
54+
)).reduce((acc, val) => acc.concat(val), [])
55+
const oldLockHashes: string[] = blockListener.getLockHashes()
56+
const anyIsImport: boolean = infos.some(info => info.isImport === true)
57+
if (oldLockHashes.length === 0 && !anyIsImport) {
58+
await blockListener.setToTip()
4459
}
60+
blockListener.appendLockHashes(infos.map(info => info.lockHash))
4561
}
4662
})
4763

0 commit comments

Comments
 (0)