Skip to content

Commit

Permalink
fix: sync when start node
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Nov 16, 2019
1 parent e8aa042 commit 219d99c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
59 changes: 40 additions & 19 deletions packages/neuron-wallet/src/startup/sync-block-task/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import AddressService from 'services/addresses'
import genesisBlockHash from './genesis'
import InitDatabase from './init-database'
import DataUpdateSubject from 'models/subjects/data-update'
import logger from 'utils/logger'
import NodeService from 'services/node'
import NetworksService from 'services/networks'
import { distinctUntilChanged } from 'rxjs/operators'

export { genesisBlockHash }

Expand All @@ -22,32 +26,49 @@ export interface DatabaseInitParams {
chain: string
}

// network switch or network connect
const networkChange = async (network: NetworkWithID) => {
await InitDatabase.getInstance().stopAndWait()
const info = await InitDatabase.getInstance().init(network)

DataUpdateSubject.next({
dataType: 'transaction',
actionType: 'update',
})

if (info !== 'killed') {
const databaseInitParams: DatabaseInitParams = {
network,
genesisBlockHash: info.hash,
chain: info.chain
}
databaseInitSubject.next(databaseInitParams)
// re init txCount in addresses if switch network
await updateAllAddressesTxCount(network.remote)
}
}

export const databaseInitSubject = new ReplaySubject<DatabaseInitParams>(1)

networkSwitchSubject.subscribe(async (network: NetworkWithID | undefined) => {
if (network) {
// TODO: only switch if genesisHash is different

await InitDatabase.getInstance().stopAndWait()
const info = await InitDatabase.getInstance().init(network)

DataUpdateSubject.next({
dataType: 'transaction',
actionType: 'update',
})
await networkChange(network)
}
})

if (info !== 'killed') {
const databaseInitParams: DatabaseInitParams = {
network,
genesisBlockHash: info.hash,
chain: info.chain
NodeService
.getInstance()
.connectionStatusSubject
.pipe(distinctUntilChanged())
.subscribe(async (status: boolean) => {
if (status && InitDatabase.getInstance().isUsingPrevious()) {
const network = NetworksService.getInstance().getCurrent()
logger.debug('networkConnect:', network)
if (network) {
await networkChange(network)
}
databaseInitSubject.next(databaseInitParams)
// re init txCount in addresses if switch network
await updateAllAddressesTxCount(network.remote)
}
}
})
})

const loadURL = `file://${path.join(__dirname, 'index.html')}`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export class InitDatabase {

private killed: boolean = false

private usingPrevious: boolean = false

public isUsingPrevious = (): boolean => {
return this.usingPrevious
}

public init = async (network: NetworkWithID) => {
if (InitDatabase.previous) {
await InitDatabase.previous.stopAndWait()
Expand All @@ -39,6 +45,7 @@ export class InitDatabase {
let chain: string = ''
while (!this.stopped && !this.success) {
try {
this.usingPrevious = false
hash = await genesisBlockHash(network.remote)
await initConnection(hash)
chain = await getChain(network.remote)
Expand Down Expand Up @@ -68,6 +75,7 @@ export class InitDatabase {
DaoUtils.setDaoScript(metaInfo.daoScriptInfo)
hash = metaInfo.genesisBlockHash
this.success = true
this.usingPrevious = true
} catch (error) {
logger.error('get cached meta info error:', err)
Utils.sleep(5000)
Expand Down

0 comments on commit 219d99c

Please sign in to comment.