Skip to content

Commit 819793a

Browse files
committed
feat: Do not update network info too often
1 parent 82a0c8d commit 819793a

1 file changed

Lines changed: 15 additions & 48 deletions

File tree

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

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { Validate, Required } from 'decorators'
99
import { UsedName, NetworkNotFound, InvalidFormat } from 'exceptions'
1010
import { NetworkListSubject, CurrentNetworkIDSubject } from 'models/subjects/networks'
1111
import { MAINNET_GENESIS_HASH, EMPTY_GENESIS_HASH, NetworkID, NetworkName, NetworkRemote, NetworksKey, NetworkType, Network, NetworkWithID } from 'types/network'
12-
import logger from 'utils/logger'
1312

1413
export const networkSwitchSubject = new BehaviorSubject<undefined | NetworkWithID>(undefined)
1514

@@ -43,28 +42,12 @@ export default class NetworksService extends Store {
4342
const currentNetworkList = this.getAll()
4443
NetworkListSubject.next({ currentNetworkList })
4544

46-
Promise.all(currentNetworkList.map(n => {
47-
if (n.type == NetworkType.Default) {
48-
return n
49-
} else {
50-
const core = new Core(n.remote)
51-
return Promise.all([
52-
core.rpc.getBlockchainInfo(),
53-
core.rpc.getBlockHash('0x0')
54-
]).then(([info, genesisHash]) => ({
55-
...n,
56-
chain: info.chain,
57-
genesisHash
58-
}))
59-
}
60-
})).then(networkList => {
61-
this.updateAll(networkList)
62-
}).catch((err: Error) => {
63-
logger.error(err)
64-
})
65-
6645
const currentNetwork = this.getCurrent()
6746
if (currentNetwork) {
47+
if (currentNetwork.type !== NetworkType.Default) {
48+
this.update(currentNetwork.id, {}) // Update to trigger chain/genesis hash refresh
49+
}
50+
6851
CurrentNetworkIDSubject.next({ currentNetworkID: currentNetwork.id })
6952
networkSwitchSubject.next(currentNetwork)
7053
}
@@ -95,13 +78,11 @@ export default class NetworksService extends Store {
9578
}
9679

9780
public getAll = () => {
98-
const list = this.readSync<NetworkWithID[]>(NetworksKey.List)
99-
return list || presetNetworks.networks
81+
return this.readSync<NetworkWithID[]>(NetworksKey.List) || presetNetworks.networks
10082
}
10183

10284
public getCurrent(): NetworkWithID {
103-
const currentID = this.getCurrentID()
104-
return this.get(currentID) || this.defaultOne()! // Should always have at least one network
85+
return this.get(this.getCurrentID()) || this.defaultOne()! // Should always have at least one network
10586
}
10687

10788
public get(@Required id: NetworkID) {
@@ -171,9 +152,10 @@ export default class NetworksService extends Store {
171152
}
172153

173154
this.updateAll(list)
174-
const currentID = this.getCurrentID()
175-
if (currentID === id) {
176-
await this.activate(id)
155+
156+
if (this.getCurrentID() === id) {
157+
CurrentNetworkIDSubject.next({ currentNetworkID: id })
158+
networkSwitchSubject.next(network)
177159
}
178160
}
179161

@@ -198,36 +180,21 @@ export default class NetworksService extends Store {
198180
if (!network) {
199181
throw new NetworkNotFound(id)
200182
}
201-
this.writeSync(NetworksKey.Current, id)
202183

203-
// No need to update the default mainnet
204-
if (network.type === NetworkType.Default) {
205-
return
184+
// No need to update the default mainnet's genesis hash
185+
if (network.type !== NetworkType.Default) {
186+
this.update(id, {})
206187
}
207188

208-
const core = new Core(network.remote)
209-
210-
const chain = await core.rpc
211-
.getBlockchainInfo()
212-
.then(info => info.chain)
213-
.catch(() => '')
214-
215-
const genesisHash = await core.rpc
216-
.getBlockHash('0x0')
217-
.catch(() => EMPTY_GENESIS_HASH)
218-
219-
if (chain && chain !== network.chain && genesisHash && genesisHash !== network.genesisHash) {
220-
this.update(id, { chain, genesisHash })
221-
}
189+
this.writeSync(NetworksKey.Current, id)
222190
}
223191

224192
public getCurrentID = () => {
225193
return this.readSync<string>(NetworksKey.Current) || 'mainnet'
226194
}
227195

228196
public defaultOne = () => {
229-
const list = this.getAll()
230-
return list.find(item => item.type === NetworkType.Default) || presetNetworks.networks[0]
197+
return this.getAll().find(item => item.type === NetworkType.Default) || presetNetworks.networks[0]
231198
}
232199

233200
public isMainnet = (): boolean => {

0 commit comments

Comments
 (0)