Skip to content

Commit 9b5b52c

Browse files
committed
feat: Prefer network's chain and genesis hash when syncing
App should always return data even when node is not available.
1 parent 74b6fa3 commit 9b5b52c

7 files changed

Lines changed: 47 additions & 44 deletions

File tree

packages/neuron-ui/src/services/remote/app.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { apiMethodWrapper } from './apiMethodWrapper'
22

3-
export const getNeuronWalletState = apiMethodWrapper<void>(controller => () => controller.loadInitData())
3+
export const getNeuronWalletState = apiMethodWrapper<void>(api => () => api.loadInitData())
44

5-
export const handleViewError = apiMethodWrapper<string>(controller => errorMessage =>
6-
controller.handleViewError(errorMessage)
7-
)
8-
export const contextMenu = apiMethodWrapper<{ type: string; id: string }>(controller => params =>
9-
controller.contextMenu(params)
10-
)
5+
export const handleViewError = apiMethodWrapper<string>(api => errorMessage => api.handleViewError(errorMessage))
6+
export const contextMenu = apiMethodWrapper<{ type: string; id: string }>(api => params => api.contextMenu(params))
117

128
export default {
139
getNeuronWalletState,

packages/neuron-wallet/src/controllers/api.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ export default class ApiController {
2626
public static async loadInitData() {
2727
const walletsService = WalletsService.getInstance()
2828
const networksService = NetworksService.getInstance()
29+
30+
const currentWallet = walletsService.getCurrent()
31+
const wallets = walletsService.getAll()
32+
2933
const [
30-
currentWallet = null,
31-
wallets = [],
3234
currentNetworkID = '',
3335
networks = [],
3436
syncedBlockNumber = '0',
3537
connectionStatus = false,
3638
codeHash = '',
3739
] = await Promise.all([
38-
walletsService.getCurrent(),
39-
walletsService.getAll(),
4040
networksService.getCurrentID(),
4141
networksService.getAll(),
4242

@@ -48,18 +48,21 @@ export default class ApiController {
4848
return '0'
4949
})
5050
.catch(() => '0'),
51+
5152
new Promise(resolve => {
5253
ConnectionStatusSubject.pipe(take(1)).subscribe(
53-
status => {
54-
resolve(status)
55-
},
56-
() => {
57-
resolve(false)
58-
},
54+
status => { resolve(status) },
55+
() => { resolve(false) },
56+
() => { resolve(false) }
5957
)
6058
}),
59+
6160
new Promise(resolve => {
62-
SystemScriptSubject.pipe(take(1)).subscribe(({ codeHash: currentCodeHash }) => resolve(currentCodeHash))
61+
SystemScriptSubject.pipe(take(1)).subscribe(
62+
({ codeHash: currentCodeHash }) => resolve(currentCodeHash),
63+
() => { resolve('') },
64+
() => { resolve('') }
65+
)
6366
}),
6467
])
6568

@@ -78,7 +81,7 @@ export default class ApiController {
7881

7982
const initState = {
8083
currentWallet,
81-
wallets: [...wallets.map(({ name, id }) => ({ id, name }))],
84+
wallets: wallets,
8285
currentNetworkID,
8386
networks,
8487
addresses,
@@ -268,9 +271,7 @@ export default class ApiController {
268271
// Transactions
269272

270273
@MapApiResponse
271-
public static async getTransactionList(
272-
params: Controller.Params.TransactionsByKeywords
273-
) {
274+
public static async getTransactionList(params: Controller.Params.TransactionsByKeywords) {
274275
return TransactionsController.getAllByKeywords(params)
275276
}
276277

packages/neuron-wallet/src/controllers/transactions.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ export default class TransactionsController {
3131
): Promise<Controller.Response<PaginationResult<Transaction> & Controller.Params.TransactionsByKeywords>> {
3232
const { pageNo = 1, pageSize = 15, keywords = '', walletID = '' } = params
3333

34-
const addresses = (await AddressesService.allAddressesByWalletId(walletID)).map(addr => addr.address)
34+
const addresses = AddressesService.allAddressesByWalletId(walletID).map(addr => addr.address)
3535

36-
const transactions = await TransactionsService.getAllByAddresses({ pageNo, pageSize, addresses }, keywords.trim())
36+
const transactions = await TransactionsService
37+
.getAllByAddresses({ pageNo, pageSize, addresses }, keywords.trim())
38+
.catch(() => ({
39+
totalCount: 0,
40+
items: []
41+
}))
3742

38-
if (!transactions) {
39-
throw new ServiceHasNoResponse('Transactions')
40-
}
4143
return {
4244
status: ResponseCode.Success,
4345
result: {
@@ -64,7 +66,7 @@ export default class TransactionsController {
6466
if (!wallet) {
6567
throw new CurrentWalletNotSet()
6668
}
67-
searchAddresses = (await AddressesService.allAddressesByWalletId(wallet.id)).map(addr => addr.address)
69+
searchAddresses = AddressesService.allAddressesByWalletId(wallet.id).map(addr => addr.address)
6870
}
6971

7072
const transactions = await TransactionsService.getAllByAddresses({ pageNo, pageSize, addresses: searchAddresses })

packages/neuron-wallet/src/services/networks.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Store from 'models/store'
88
import { Validate, Required } from 'decorators'
99
import { UsedName, NetworkNotFound, InvalidFormat } from 'exceptions'
1010
import { NetworkListSubject, CurrentNetworkIDSubject } from 'models/subjects/networks'
11-
import { MAINNET_GENESIS_HASH, NetworkID, NetworkName, NetworkRemote, NetworksKey, NetworkType, Network, NetworkWithID } from 'types/network'
11+
import { MAINNET_GENESIS_HASH, EMPTY_GENESIS_HASH, NetworkID, NetworkName, NetworkRemote, NetworksKey, NetworkType, Network, NetworkWithID } from 'types/network'
1212
import logger from 'utils/logger'
1313

1414
export const networkSwitchSubject = new BehaviorSubject<undefined | NetworkWithID>(undefined)
@@ -137,7 +137,7 @@ export default class NetworksService extends Store {
137137
.catch(() => 'ckb_dev')
138138
const genesisHash = await core.rpc
139139
.getBlockHash('0x0')
140-
.catch(() => '0x')
140+
.catch(() => EMPTY_GENESIS_HASH)
141141

142142
const newOne = {
143143
id: uuid(),
@@ -172,7 +172,7 @@ export default class NetworksService extends Store {
172172

173173
const genesisHash = await core.rpc
174174
.getBlockHash('0x0')
175-
.catch(() => '0x')
175+
.catch(() => EMPTY_GENESIS_HASH)
176176
network.genesisHash = genesisHash
177177
}
178178

@@ -220,7 +220,7 @@ export default class NetworksService extends Store {
220220

221221
const genesisHash = await core.rpc
222222
.getBlockHash('0x0')
223-
.catch(() => '0x')
223+
.catch(() => EMPTY_GENESIS_HASH)
224224

225225
if (chain && chain !== network.chain && genesisHash && genesisHash !== network.genesisHash) {
226226
this.update(id, { chain, genesisHash })

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ networkSwitchSubject.subscribe(async (network: NetworkWithID | undefined) => {
2929
// TODO: only switch if genesisHash is different
3030

3131
await InitDatabase.getInstance().stopAndWait()
32-
const info = await InitDatabase.getInstance().init(network.remote)
32+
const info = await InitDatabase.getInstance().init(network)
3333

3434
DataUpdateSubject.next({
3535
dataType: 'transaction',

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import LockUtils from 'models/lock-utils'
55
import logger from 'utils/logger'
66
import genesisBlockHash, { getChain } from './genesis'
77
import ChainInfo from 'models/chain-info'
8-
import DaoUtils from '../../models/dao-utils';
8+
import DaoUtils from '../../models/dao-utils'
9+
import { NetworkWithID, EMPTY_GENESIS_HASH } from 'types/network'
910

1011
// only used by main process
1112
export class InitDatabase {
@@ -19,9 +20,7 @@ export class InitDatabase {
1920
}
2021

2122
private stopped: boolean = false
22-
// private nodeURL: string
2323
private inProcess: boolean = false
24-
2524
private success: boolean = false
2625

2726
public id: number = +new Date()
@@ -30,25 +29,29 @@ export class InitDatabase {
3029

3130
private killed: boolean = false
3231

33-
public init = async (url: string) => {
32+
public init = async (network: NetworkWithID) => {
3433
if (InitDatabase.previous) {
3534
await InitDatabase.previous.stopAndWait()
3635
}
3736

3837
this.inProcess = true
3938

40-
let hash: string | undefined
41-
let chain: string | undefined
39+
let hash: string = network.genesisHash
40+
let chain: string = network.chain
4241
while (!this.stopped && !this.success) {
4342
try {
44-
hash = await genesisBlockHash(url)
43+
if (hash === EMPTY_GENESIS_HASH) {
44+
hash = await genesisBlockHash(network.remote)
45+
}
4546
await initConnection(hash)
46-
chain = await getChain(url)
47+
if (chain === '') {
48+
chain = await getChain(network.remote)
49+
}
4750
ChainInfo.getInstance().setChain(chain)
4851

4952
try {
50-
const systemScriptInfo = await LockUtils.systemScript(url)
51-
const daoScriptInfo = await DaoUtils.daoScript(url)
53+
const systemScriptInfo = await LockUtils.systemScript(network.remote)
54+
const daoScriptInfo = await DaoUtils.daoScript(network.remote)
5255
updateMetaInfo({ genesisBlockHash: hash, systemScriptInfo, chain, daoScriptInfo })
5356
} catch (err) {
5457
logger.error('update systemScriptInfo failed:', err)

packages/neuron-wallet/src/types/network.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export enum NetworkType {
1313
Normal,
1414
}
1515

16-
export const MAINNET_GENESIS_HASH = "0x" // TODO: set this when mainnet launches!
16+
export const MAINNET_GENESIS_HASH = "0xeeee" // TODO: set this when mainnet launches!
17+
export const EMPTY_GENESIS_HASH = "0x"
1718

1819
export interface Network {
1920
name: NetworkName

0 commit comments

Comments
 (0)