Skip to content

Commit 1979e8a

Browse files
committed
feat: Preset mainnet network configuration
TODO: set genesis hash.
1 parent df3eb0e commit 1979e8a

5 files changed

Lines changed: 85 additions & 93 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ export default class ApiController {
246246
}
247247

248248
@MapApiResponse
249-
public static async createNetwork({ name, remote, type = NetworkType.Normal, chain = 'ckb' }: Network) {
250-
return NetworksController.create({ name, remote, type, chain })
249+
public static async createNetwork({ name, remote, type = NetworkType.Normal, genesisHash = '0x', chain = 'ckb' }: Network) {
250+
return NetworksController.create({ name, remote, type, genesisHash, chain })
251251
}
252252

253253
@MapApiResponse

packages/neuron-wallet/src/env.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { app as electronApp, remote } from 'electron'
22
import os from 'os'
33
import * as path from 'path'
4-
import { NetworkWithID } from 'types/network'
54

65
const app = electronApp || (remote && remote.app) || {
76
getPath(aPath: string): string {
@@ -35,36 +34,13 @@ interface ENV {
3534
fileBasePath: string
3635
mainURL: string
3736
remote: string
38-
presetNetworks: {
39-
current: 'testnet'
40-
list: NetworkWithID[]
41-
}
4237
isTestMode: boolean
4338
}
4439
const env: ENV = {
4540
isDevMode,
4641
fileBasePath: path.resolve(app.getPath('userData'), fileBase),
4742
mainURL: isDevMode ? 'http://localhost:3000' : `file://${path.join(__dirname, '../dist/neuron-ui/index.html')}`,
4843
remote: 'http://localhost:8114',
49-
presetNetworks: {
50-
current: 'testnet',
51-
list: [
52-
{
53-
id: 'testnet',
54-
name: 'Testnet',
55-
remote: 'http://localhost:8114',
56-
type: 0,
57-
chain: '',
58-
},
59-
{
60-
id: 'local',
61-
name: 'Local',
62-
remote: 'http://localhost:8114',
63-
type: 1,
64-
chain: '',
65-
},
66-
],
67-
},
6844
isTestMode,
6945
}
7046

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

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,29 @@ import { BehaviorSubject } from 'rxjs'
44
import { LackOfDefaultNetwork, DefaultNetworkUnremovable } from 'exceptions/network'
55

66
import Store from 'models/store'
7-
import env from 'env'
87

98
import { Validate, Required } from 'decorators'
109
import { UsedName, NetworkNotFound, InvalidFormat } from 'exceptions'
1110
import { NetworkListSubject, CurrentNetworkIDSubject } from 'models/subjects/networks'
12-
import { NetworkID, NetworkName, NetworkRemote, NetworksKey, NetworkType, Network, NetworkWithID } from 'types/network'
11+
import { MAINNET_GENESIS_HASH, NetworkID, NetworkName, NetworkRemote, NetworksKey, NetworkType, Network, NetworkWithID } from 'types/network'
1312
import logger from 'utils/logger'
1413

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

16+
const presetNetworks: { selected: string, networks: NetworkWithID[] } = {
17+
selected: 'mainnet',
18+
networks: [
19+
{
20+
id: 'mainnet',
21+
name: 'Mainnet',
22+
remote: 'http://localhost:8114',
23+
genesisHash: MAINNET_GENESIS_HASH,
24+
type: NetworkType.Default,
25+
chain: 'ckb',
26+
}
27+
]
28+
}
29+
1730
export default class NetworksService extends Store {
1831
private static instance: NetworksService
1932

@@ -25,7 +38,7 @@ export default class NetworksService extends Store {
2538
}
2639

2740
constructor() {
28-
super('networks', 'index.json', JSON.stringify(env.presetNetworks))
41+
super('networks', 'index.json', JSON.stringify(presetNetworks))
2942

3043
this.getAll().then(currentNetworkList => {
3144
if (currentNetworkList) {
@@ -88,7 +101,7 @@ export default class NetworksService extends Store {
88101

89102
public getAll = async () => {
90103
const list = await this.read<NetworkWithID[]>(NetworksKey.List)
91-
return list || []
104+
return list || presetNetworks.networks
92105
}
93106

94107
@Validate
@@ -102,15 +115,11 @@ export default class NetworksService extends Store {
102115
if (!Array.isArray(networks)) {
103116
throw new InvalidFormat('Networks')
104117
}
105-
await this.writeSync(NetworksKey.List, networks)
118+
this.writeSync(NetworksKey.List, networks)
106119
}
107120

108121
@Validate
109-
public async create(
110-
@Required name: NetworkName,
111-
@Required remote: NetworkRemote,
112-
type: NetworkType = NetworkType.Normal,
113-
) {
122+
public async create(@Required name: NetworkName, @Required remote: NetworkRemote, type: NetworkType = NetworkType.Normal) {
114123
const list = await this.getAll()
115124
if (list.some(item => item.name === name)) {
116125
throw new UsedName('Network')
@@ -122,11 +131,15 @@ export default class NetworksService extends Store {
122131
.getBlockchainInfo()
123132
.then(info => info.chain)
124133
.catch(() => '')
134+
const genesisHash = await core.rpc
135+
.getBlockHash('0x0')
136+
.catch(() => '0x')
125137

126138
const newOne = {
127139
id: uuid(),
128140
name,
129141
remote,
142+
genesisHash,
130143
type,
131144
chain,
132145
}
@@ -146,11 +159,17 @@ export default class NetworksService extends Store {
146159
Object.assign(network, options)
147160
if (!options.chain) {
148161
const core = new Core(network.remote)
162+
149163
const chain = await core.rpc
150164
.getBlockchainInfo()
151165
.then(info => info.chain)
152166
.catch(() => '')
153167
network.chain = chain
168+
169+
const genesisHash = await core.rpc
170+
.getBlockHash('0x0')
171+
.catch(() => '0x')
172+
network.genesisHash = genesisHash
154173
}
155174

156175
this.updateAll(list)
@@ -183,15 +202,24 @@ export default class NetworksService extends Store {
183202
}
184203
this.writeSync(NetworksKey.Current, id)
185204

205+
// No need to update the default mainnet
206+
if (network.type === NetworkType.Default) {
207+
return
208+
}
209+
186210
const core = new Core(network.remote)
187211

188212
const chain = await core.rpc
189213
.getBlockchainInfo()
190214
.then(info => info.chain)
191215
.catch(() => '')
192216

193-
if (chain && chain !== network.chain) {
194-
this.update(id, { chain })
217+
const genesisHash = await core.rpc
218+
.getBlockHash('0x0')
219+
.catch(() => '0x')
220+
221+
if (chain && chain !== network.chain && genesisHash && genesisHash !== network.genesisHash) {
222+
this.update(id, { chain, genesisHash })
195223
}
196224
}
197225

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
export type NetworkID = string
22
export type NetworkName = string
33
export type NetworkRemote = string
4+
export type NetworkGenesisHash = string
5+
46
export enum NetworksKey {
5-
List = 'list',
6-
Current = 'current',
7+
List = 'networks',
8+
Current = 'selected',
79
}
810

911
export enum NetworkType {
10-
Default,
12+
Default, // Preset mainnet node
1113
Normal,
1214
}
1315

16+
export const MAINNET_GENESIS_HASH = "0x" // TODO: set this when mainnet launches!
17+
1418
export interface Network {
1519
name: NetworkName
1620
remote: NetworkRemote
1721
type: NetworkType
22+
genesisHash: NetworkGenesisHash
1823
chain: 'ckb' | 'ckb_testnet' | 'ckb_dev' | string // returned by rpc.getBlockchainInfo
1924
}
25+
2026
export interface NetworkWithID extends Network {
2127
id: NetworkID
2228
}

packages/neuron-wallet/tests/services/networks.test.ts

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import NetworksService from '../../src/services/networks'
22
import { NetworkWithID } from '../../src/types/network'
3-
import env from '../../src/env'
43
import i18n from '../../src/utils/i18n'
54

65
const ERROR_MESSAGE = {
@@ -9,23 +8,19 @@ const ERROR_MESSAGE = {
98
NETWORK_ID_NOT_FOUND: `messages.network-not-found`,
109
}
1110

12-
const {
13-
presetNetworks: { current, list },
14-
} = env
15-
const [testnetNetwork, localNetwork] = list
16-
1711
describe(`Unit tests of networks service`, () => {
1812
const newNetwork: NetworkWithID = {
1913
name: `new network`,
20-
remote: `http://new-network.localhost.com`,
14+
remote: `http://localhost:8114`,
2115
type: 0,
16+
genesisHash: '',
2217
id: '',
2318
chain: '',
2419
}
2520

2621
const newNetworkWithDefaultTypeOf1 = {
2722
name: `new network with the default type of 1`,
28-
remote: `http://test.localhost.com`,
23+
remote: `http://localhost:8114`,
2924
id: '',
3025
}
3126

@@ -52,28 +47,18 @@ describe(`Unit tests of networks service`, () => {
5247

5348
it(`has preset networks`, async () => {
5449
const networks = await service.getAll()
55-
expect(networks).toEqual(list)
50+
expect(networks.length).toBe(1)
51+
expect(networks[0].id).toEqual('mainnet')
5652
})
5753

5854
it(`get the default network`, async () => {
5955
const network = await service.defaultOne()
6056
expect(network && network.type).toBe(0)
6157
})
6258

63-
it(`testnet should be type of default network`, async () => {
64-
const defaultNetwork = await service.defaultOne()
65-
expect(defaultNetwork).toEqual(testnetNetwork)
66-
})
67-
68-
it(`testnet should be the current one by default`, async () => {
59+
it(`mainnet should be the current one by default`, async () => {
6960
const currentNetworkID = await service.getCurrentID()
70-
expect(currentNetworkID).toBe(current)
71-
expect(currentNetworkID).toBe(testnetNetwork.id)
72-
})
73-
74-
it(`get network by id ${current}`, async () => {
75-
const currentNetwork = await service.get(current)
76-
expect(currentNetwork).toEqual(list.find(network => network.id === current))
61+
expect(currentNetworkID).toBe('mainnet')
7762
})
7863

7964
it(`getting a non-exsiting network should return null`, async () => {
@@ -94,35 +79,31 @@ describe(`Unit tests of networks service`, () => {
9479
expect(res.type).toBe(1)
9580
})
9681

97-
it(`update the local networks's name`, async () => {
98-
const name = `new local network name`
99-
await service.update(localNetwork.id, { name })
100-
const network = await service.get(localNetwork.id)
101-
expect(network && network.name).toBe(name)
102-
})
103-
104-
it(`update the local network address`, async () => {
105-
const addr = `http://updated-address.com`
106-
await service.update(localNetwork.id, { remote: addr })
107-
const network = await service.get(localNetwork.id)
108-
expect(network && network.remote).toBe(addr)
82+
it(`update the networks's name`, async () => {
83+
const network = await service.create(newNetworkWithDefaultTypeOf1.name, newNetworkWithDefaultTypeOf1.remote)
84+
const name = `new network name`
85+
await service.update(network.id, { name })
86+
const updated = await service.get(network.id)
87+
expect(updated && updated.name).toBe(name)
10988
})
11089

111-
it(`update the local network type to 1`, async () => {
112-
const type = 1
113-
await service.update(localNetwork.id, { type })
114-
const network = await service.get(localNetwork.id)
115-
expect(network && network.type).toBe(type)
90+
it(`update the network' address`, async () => {
91+
const network = await service.create(newNetworkWithDefaultTypeOf1.name, newNetworkWithDefaultTypeOf1.remote)
92+
const address = `http://localhost:8115`
93+
await service.update(network.id, { remote: address })
94+
const updated = await service.get(network.id)
95+
expect(updated && updated.remote).toBe(address)
11696
})
11797

118-
it(`set the local network to be the current one`, async () => {
119-
await service.activate(localNetwork.id)
98+
it(`set the network to be the current one`, async () => {
99+
const network = await service.create(newNetworkWithDefaultTypeOf1.name, newNetworkWithDefaultTypeOf1.remote)
100+
await service.activate(network.id)
120101
const currentNetworkID = await service.getCurrentID()
121-
expect(currentNetworkID).toBe(localNetwork.id)
102+
expect(currentNetworkID).toBe(network.id)
122103
})
123104

124105
it(`delete an inactive network`, async () => {
125-
const inactiveNetwork = localNetwork
106+
const inactiveNetwork = await service.create(newNetworkWithDefaultTypeOf1.name, newNetworkWithDefaultTypeOf1.remote)
126107
const prevCurrentID = (await service.getCurrentID()) || ''
127108
const prevNetworks = await service.getAll()
128109
await service.delete(inactiveNetwork.id)
@@ -134,12 +115,13 @@ describe(`Unit tests of networks service`, () => {
134115
expect(currentID).toBe(prevCurrentID)
135116
})
136117

137-
it(`activate the local network and delete it, the current networks should switch to the testnet network`, async () => {
138-
await service.activate(localNetwork.id)
118+
it(`activate a network and delete it, the current networks should switch to the default network`, async () => {
119+
const network = await service.create(newNetworkWithDefaultTypeOf1.name, newNetworkWithDefaultTypeOf1.remote)
120+
await service.activate(network.id)
139121
const prevCurrentID = await service.getCurrentID()
140122
const prevNetworks = await service.getAll()
141-
expect(prevCurrentID).toBe(localNetwork.id)
142-
expect(prevNetworks.map(n => n.id)).toEqual(list.map(n => n.id))
123+
expect(prevCurrentID).toBe(network.id)
124+
expect(prevNetworks.map(n => n.id)).toEqual(['Mainnet', network.id])
143125
await service.delete(prevCurrentID || '')
144126
const currentNetworks = await service.getAll()
145127
expect(currentNetworks.map(n => n.id)).toEqual(prevNetworks.filter(n => n.id !== prevCurrentID).map(n => n.id))
@@ -148,16 +130,16 @@ describe(`Unit tests of networks service`, () => {
148130
service.getCurrentID().then(cID => resolve(cID))
149131
}, 500)
150132
})
151-
expect(currentID).toBe(testnetNetwork.id)
133+
expect(currentID).toBe('mainnet')
152134
})
153135

154136
it(`reset the netowrks`, async () => {
155137
await service.create(newNetwork.name, newNetwork.remote)
156138
const newNetworkList = await service.getAll()
157-
expect(newNetworkList.length).toBe(list.length + 1)
139+
expect(newNetworkList.length).toBe(2)
158140
service.clear()
159141
const networks = await service.getAll()
160-
expect(networks.length).toBe(list.length)
142+
expect(networks.length).toBe(1)
161143
})
162144
})
163145

@@ -192,8 +174,8 @@ describe(`Unit tests of networks service`, () => {
192174
})
193175

194176
describe(`validation on network existence`, () => {
195-
it(`create network with existing name of ${list[0].name}`, () => {
196-
expect(service.create(list[0].name, list[0].remote)).rejects.toThrowError(i18n.t(ERROR_MESSAGE.NAME_USED))
177+
it(`create network with existing name of Mainnet`, () => {
178+
expect(service.create('Mainnet', 'http://localhost:8114')).rejects.toThrowError(i18n.t(ERROR_MESSAGE.NAME_USED))
197179
})
198180

199181
it(`update network which is not existing`, () => {

0 commit comments

Comments
 (0)