Skip to content

Commit

Permalink
feat: not start from zero in normal sync when create wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Aug 20, 2019
1 parent 342061d commit 50a3c73
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 24 deletions.
32 changes: 32 additions & 0 deletions packages/neuron-wallet/src/services/sync/block-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Queue from './queue'
import RangeForCheck from './range-for-check'
import BlockNumber from './block-number'
import GetBlocks from './get-blocks'
import Utils from './utils'

export default class BlockListener {
private lockHashes: string[]
Expand Down Expand Up @@ -34,6 +35,15 @@ export default class BlockListener {
this.queue.setLockHashes(lockHashes)
}

public appendLockHashes = (lockHashes: string[]) => {
const hashes = this.lockHashes.concat(lockHashes)
this.setLockHashes(hashes)
}

public getLockHashes = (): string[] => {
return this.lockHashes
}

// start listening
public start = async (restart: boolean = false) => {
if (restart) {
Expand All @@ -57,6 +67,28 @@ export default class BlockListener {
})
}

/* eslint no-await-in-loop: "off" */
/* eslint no-restricted-syntax: "off" */
public setToTip = async () => {
const timeout = 5000
let number: bigint = BigInt(0)
const tipNumberListener = this.tipNumberSubject.subscribe(async num => {
if (num) {
number = BigInt(num)
}
})
const startAt = +new Date()
while (number === BigInt(0)) {
const now = +new Date()
if (now - startAt > timeout) {
return
}
await Utils.sleep(100)
}
await this.currentBlockNumber.updateCurrent(this.tipBlockNumber)
tipNumberListener.unsubscribe()
}

public stop = () => {
if (this.tipNumberListener) {
this.tipNumberListener.unsubscribe()
Expand Down
30 changes: 16 additions & 14 deletions packages/neuron-wallet/src/services/sync/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,22 @@ export default class Queue {
try {
this.inProcess = true

const current: bigint = await this.currentBlockNumber.getCurrent()
const startNumber: bigint = current + BigInt(1)
const endNumber: bigint = current + BigInt(this.fetchSize)
const realEndNumber: bigint = endNumber < this.endBlockNumber ? endNumber : this.endBlockNumber

if (realEndNumber >= this.endBlockNumber) {
this.yieldTime = 1000
} else {
this.yieldTime = 1
}

if (realEndNumber >= startNumber) {
const rangeArr = Utils.rangeForBigInt(startNumber, realEndNumber).map(num => num.toString())
await this.pipeline(rangeArr)
if (this.lockHashes.length !== 0) {
const current: bigint = await this.currentBlockNumber.getCurrent()
const startNumber: bigint = current + BigInt(1)
const endNumber: bigint = current + BigInt(this.fetchSize)
const realEndNumber: bigint = endNumber < this.endBlockNumber ? endNumber : this.endBlockNumber

if (realEndNumber >= this.endBlockNumber) {
this.yieldTime = 1000
} else {
this.yieldTime = 1
}

if (realEndNumber >= startNumber) {
const rangeArr = Utils.rangeForBigInt(startNumber, realEndNumber).map(num => num.toString())
await this.pipeline(rangeArr)
}
}
} catch (err) {
if (err.message.startsWith('connect ECONNREFUSED')) {
Expand Down
36 changes: 26 additions & 10 deletions packages/neuron-wallet/src/startup/sync-block-task/sync.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { remote } from 'electron'
import AddressService from 'services/addresses'
import AddressService, { AddressWithWay } 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'
)
const { nodeService, addressCreatedSubject, walletCreatedSubject } = remote.require('./startup/sync-block-task/params')

export interface LockHashInfo {
lockHash: string
isImport: boolean | undefined
}

// pass to task a main process subject
// AddressesUsedSubject.setSubject(addressesUsedSubject)
Expand Down Expand Up @@ -35,13 +38,26 @@ export const switchNetwork = async () => {
// 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)
// listen to address created
addressCreatedSubject.subscribe(async (addressWithWay: AddressWithWay[]) => {
if (blockListener) {
const infos: LockHashInfo[] = (await Promise.all(
addressWithWay.map(async aw => {
const hashes: string[] = await LockUtils.addressToAllLockHashes(aw.address.address)
return hashes.map(h => {
return {
lockHash: h,
isImport: aw.isImport,
}
})
})
)).reduce((acc, val) => acc.concat(val), [])
const oldLockHashes: string[] = blockListener.getLockHashes()
const anyIsImport: boolean = infos.some(info => info.isImport === true)
if (oldLockHashes.length === 0 && !anyIsImport) {
await blockListener.setToTip()
}
blockListener.appendLockHashes(infos.map(info => info.lockHash))
}
})

Expand Down

0 comments on commit 50a3c73

Please sign in to comment.