Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/neuron-wallet/src/services/syncBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export default class SyncBlocksService {

private tryBlocksTime = 0

private stopFlag = false

constructor(lockHashes: string[]) {
this.lockHashList = lockHashes
}
Expand All @@ -53,12 +55,16 @@ export default class SyncBlocksService {

// continue to loop blocks, follow chain height
async loopBlocks() {
const flag = true
while (flag) {
while (!this.stopFlag) {
await this.resolveBatchBlocks()
}
}

// stop loop
stop() {
this.stopFlag = true
}

// resolve block to
async resolveBatchBlocks() {
// using database to sync currentBlockNumber value
Expand Down
46 changes: 41 additions & 5 deletions packages/neuron-wallet/src/startup/loopTask/create.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { BrowserWindow } from 'electron'
import path from 'path'
import { Subject, BehaviorSubject } from 'rxjs'
import SyncBlocksService from '../../services/syncBlocks'
import initConnection from '../../typeorm'
import Address from '../../services/addresses'
import TransactionsService from '../../services/transactions'

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

const stopLoopSubject = new Subject()

// TODO: mock as an address subject
const addressChangeSubject = new Subject()

// TODO: mock as an network init or change subject
// init-message means network not initialized
const networkChangeSubject = new BehaviorSubject('init-message')
networkChangeSubject.next('testnet')

// maybe should call this every time when new address generated
// load all addresses and convert to lockHashes
const loadAddressesAndConvert = async (): Promise<string[]> => {
Expand All @@ -19,6 +30,30 @@ const loadAddressesAndConvert = async (): Promise<string[]> => {
return lockHashes
}

// call this after network switched
// TODO: listen to network switch
const switchNetwork = async (networkId: string) => {
// stop all blocks service
stopLoopSubject.next('stop')
// disconnect old connection and connect to new database
await initConnection(networkId)
// load lockHashes
const lockHashes: string[] = await loadAddressesAndConvert()
// start sync blocks service
const syncBlocksService = new SyncBlocksService(lockHashes)

// TODO: should change to listen real address module event
addressChangeSubject.subscribe(async () => {
syncBlocksService.lockHashes = await loadAddressesAndConvert()
})

stopLoopSubject.subscribe(() => {
syncBlocksService.stop()
})

await syncBlocksService.loopBlocks()
}

// create a background task to sync transactions
// this task is a renderer process
const createLoopTask = () => {
Expand All @@ -36,11 +71,12 @@ const createLoopTask = () => {
loopWindow.on('ready-to-show', async () => {
loopWindow!.hide()

// TODO: call this function after get network name
await initConnection('testnet')
const lockHashes: string[] = await loadAddressesAndConvert()
const syncBlocksService = new SyncBlocksService(lockHashes)
await syncBlocksService.loopBlocks()
// TODO: add an event on networkId for when it init and changed, it should broadcast a message with networkId
networkChangeSubject.subscribe(async (networkId: string) => {
if (networkId !== 'init-message') {
await switchNetwork(networkId)
}
})
})

loopWindow.on('closed', () => {
Expand Down