From 0922507b3cba6efd31240671ed6c39d82c7f7499 Mon Sep 17 00:00:00 2001
From: yanguoyu <841185308@qq.com>
Date: Fri, 25 Aug 2023 15:45:50 +0800
Subject: [PATCH 1/2] feat: Change default network name to Internal Node
---
.../src/components/NetworkSetting/index.tsx | 5 +-
.../src/stories/NetworkSetting.stories.tsx | 3 +
.../src/tests/is/isMainnet/fixtures.ts | 3 +
packages/neuron-ui/src/types/App/index.d.ts | 1 +
packages/neuron-wallet/src/controllers/api.ts | 10 ++-
packages/neuron-wallet/src/models/network.ts | 1 +
.../neuron-wallet/src/services/networks.ts | 47 ++++++++++-
.../tests/services/networks.test.ts | 83 ++++++++++++++++++-
8 files changed, 144 insertions(+), 9 deletions(-)
diff --git a/packages/neuron-ui/src/components/NetworkSetting/index.tsx b/packages/neuron-ui/src/components/NetworkSetting/index.tsx
index 9aebf18381..31ccf84d0b 100644
--- a/packages/neuron-ui/src/components/NetworkSetting/index.tsx
+++ b/packages/neuron-ui/src/components/NetworkSetting/index.tsx
@@ -10,7 +10,6 @@ import { chainState } from 'states'
import { setCurrentNetwork, deleteNetwork } from 'services/remote'
import RadioGroup from 'widgets/RadioGroup'
import { useOnWindowResize, useToggleChoiceGroupBorder, getNetworkLabelI18nkey } from 'utils'
-import { LIGHT_CLIENT_TESTNET } from 'utils/const'
import styles from './networkSetting.module.scss'
const NetworkSetting = ({ chain = chainState, settings: { networks = [] } }: State.AppWithNeuronWallet) => {
@@ -92,12 +91,12 @@ const NetworkSetting = ({ chain = chainState, settings: { networks = [] } }: Sta
),
suffix: (
- {network.chain === LIGHT_CLIENT_TESTNET ? null : (
+ {network.readonly ? null : (
)}
- {network.type && network.chain !== LIGHT_CLIENT_TESTNET ? (
+ {network.type && !network.readonly ? (
diff --git a/packages/neuron-ui/src/stories/NetworkSetting.stories.tsx b/packages/neuron-ui/src/stories/NetworkSetting.stories.tsx
index eb5e5bcb0f..25f70783a6 100644
--- a/packages/neuron-ui/src/stories/NetworkSetting.stories.tsx
+++ b/packages/neuron-ui/src/stories/NetworkSetting.stories.tsx
@@ -13,6 +13,7 @@ const states: { [title: string]: State.Network[] } = {
chain: 'ckb',
type: 0,
genesisHash: '0x92b197aa1fba0f63633922c61c92375c9c074a93e85963554f5499fe1450d0e5',
+ readonly: true,
},
{
id: 'Testnet',
@@ -21,6 +22,7 @@ const states: { [title: string]: State.Network[] } = {
chain: 'ckb_testnet',
type: 1,
genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606',
+ readonly: false,
},
{
id: 'Local',
@@ -29,6 +31,7 @@ const states: { [title: string]: State.Network[] } = {
chain: 'ckb_devnet',
type: 1,
genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606',
+ readonly: false,
},
],
}
diff --git a/packages/neuron-ui/src/tests/is/isMainnet/fixtures.ts b/packages/neuron-ui/src/tests/is/isMainnet/fixtures.ts
index 56dc338194..2939092581 100644
--- a/packages/neuron-ui/src/tests/is/isMainnet/fixtures.ts
+++ b/packages/neuron-ui/src/tests/is/isMainnet/fixtures.ts
@@ -24,6 +24,7 @@ const fixtures = {
name: 'Mainnet',
remote: 'http://127.0.0.1:8114',
genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606',
+ readonly: true,
},
],
},
@@ -40,6 +41,7 @@ const fixtures = {
name: 'Mainnet',
remote: 'http://127.0.0.1:8114',
genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606',
+ readonly: false,
},
],
},
@@ -56,6 +58,7 @@ const fixtures = {
name: 'Mainnet',
remote: 'http://127.0.0.1:8114',
genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606',
+ readonly: true,
},
],
},
diff --git a/packages/neuron-ui/src/types/App/index.d.ts b/packages/neuron-ui/src/types/App/index.d.ts
index dfa736f910..f9473e036b 100644
--- a/packages/neuron-ui/src/types/App/index.d.ts
+++ b/packages/neuron-ui/src/types/App/index.d.ts
@@ -177,6 +177,7 @@ declare namespace State {
chain: 'ckb' | 'ckb_testnet' | 'ckb_dev' | string
type: 0 | 1 | 2
genesisHash: string
+ readonly: boolean
}
interface Network extends NetworkProperty {
diff --git a/packages/neuron-wallet/src/controllers/api.ts b/packages/neuron-wallet/src/controllers/api.ts
index 6d6f56208a..f54ed36331 100644
--- a/packages/neuron-wallet/src/controllers/api.ts
+++ b/packages/neuron-wallet/src/controllers/api.ts
@@ -527,7 +527,15 @@ export default class ApiController {
})
handle('create-network', async (_, { name, remote, type = NetworkType.Normal }: Network) => {
- return this.#networksController.create({ name, remote, type, genesisHash: '0x', chain: 'ckb', id: '' })
+ return this.#networksController.create({
+ name,
+ remote,
+ type,
+ genesisHash: '0x',
+ chain: 'ckb',
+ id: '',
+ readonly: false,
+ })
})
handle('update-network', async (_, { networkID, options }: { networkID: string; options: Partial }) => {
diff --git a/packages/neuron-wallet/src/models/network.ts b/packages/neuron-wallet/src/models/network.ts
index 5260cd9995..b612e0a069 100644
--- a/packages/neuron-wallet/src/models/network.ts
+++ b/packages/neuron-wallet/src/models/network.ts
@@ -17,4 +17,5 @@ export interface Network {
type: NetworkType
genesisHash: string
chain: ChainType | string // returned by rpc.getBlockchainInfo
+ readonly: boolean
}
diff --git a/packages/neuron-wallet/src/services/networks.ts b/packages/neuron-wallet/src/services/networks.ts
index e1351b3857..67730c586b 100644
--- a/packages/neuron-wallet/src/services/networks.ts
+++ b/packages/neuron-wallet/src/services/networks.ts
@@ -15,11 +15,12 @@ const presetNetworks: { selected: string; networks: Network[] } = {
networks: [
{
id: 'mainnet',
- name: 'Default',
+ name: 'Internal Node',
remote: BUNDLED_CKB_URL,
genesisHash: MAINNET_GENESIS_HASH,
type: NetworkType.Default,
chain: 'ckb',
+ readonly: true,
},
],
}
@@ -32,6 +33,7 @@ const lightClientNetwork: Network[] = [
genesisHash: TESTNET_GENESIS_HASH,
type: NetworkType.Light,
chain: LIGHT_CLIENT_TESTNET,
+ readonly: true,
},
]
@@ -39,8 +41,11 @@ enum NetworksKey {
List = 'networks',
Current = 'selected',
AddedLightNetwork = 'AddedLightNetwork',
+ MigrateNetwork = 'MigrateNetwork',
}
+const oldDefaultNames = ['Default', 'default node', presetNetworks.networks[0].name]
+
export default class NetworksService extends Store {
private static instance: NetworksService
@@ -62,6 +67,7 @@ export default class NetworksService extends Store {
this.updateAll([...networks, ...lightClientNetwork])
this.writeSync(NetworksKey.AddedLightNetwork, true)
}
+ this.migrateNetwork()
}
public getAll = () => {
@@ -72,9 +78,6 @@ export default class NetworksService extends Store {
// Therefore, to ensure normal connection to the ckb node, manual resolution needs to be done here.
network.remote = applyLocalhostIPv4Resolve(network.remote)
})
- const defaultNetwork = networks[0]
- const isOldDefaultName = ['Default', 'Mainnet'].includes(networks[0].name)
- defaultNetwork.name = isOldDefaultName ? 'default node' : defaultNetwork.name
return networks
}
@@ -108,6 +111,7 @@ export default class NetworksService extends Store {
type,
genesisHash: EMPTY_GENESIS_HASH,
chain: 'ckb_dev',
+ readonly: false,
}
const network = await CommonUtils.timeout(2000, this.refreshChainInfo(properties), properties).catch(
() => properties
@@ -200,6 +204,41 @@ export default class NetworksService extends Store {
return network
}
+
+ private migrateNetwork() {
+ const migrated = this.readSync(NetworksKey.MigrateNetwork)
+ if (!migrated) {
+ const networks = this.readSync(NetworksKey.List)
+ const defaultMainnetNetwork = presetNetworks.networks[0]
+ const oldMainnetNetwork = networks.find(v => v.id === defaultMainnetNetwork.id)
+ if (oldMainnetNetwork) {
+ if (
+ // make sure that user has not change the network name
+ oldDefaultNames.includes(oldMainnetNetwork.name) &&
+ oldMainnetNetwork.remote === defaultMainnetNetwork.remote &&
+ oldMainnetNetwork.type === defaultMainnetNetwork.type
+ ) {
+ this.updateAll([
+ defaultMainnetNetwork,
+ ...lightClientNetwork,
+ ...networks
+ .filter(v => v.id !== defaultMainnetNetwork.id && v.type !== NetworkType.Light)
+ .map(v => ({ ...v, readonly: false })),
+ ])
+ } else {
+ oldMainnetNetwork.id = uuid()
+ oldMainnetNetwork.type = NetworkType.Normal
+ this.updateAll([
+ defaultMainnetNetwork,
+ ...lightClientNetwork,
+ ...networks.filter(v => v.type !== NetworkType.Light).map(v => ({ ...v, readonly: false })),
+ ])
+ this.activate(oldMainnetNetwork.id)
+ }
+ }
+ this.writeSync(NetworksKey.MigrateNetwork, true)
+ }
+ }
}
function applyLocalhostIPv4Resolve(url: string): string {
diff --git a/packages/neuron-wallet/tests/services/networks.test.ts b/packages/neuron-wallet/tests/services/networks.test.ts
index dbd727da98..09d23f287a 100644
--- a/packages/neuron-wallet/tests/services/networks.test.ts
+++ b/packages/neuron-wallet/tests/services/networks.test.ts
@@ -1,6 +1,7 @@
import { t } from 'i18next'
import NetworksService from '../../src/services/networks'
-import { Network } from '../../src/models/network'
+import { MAINNET_GENESIS_HASH, Network, NetworkType, TESTNET_GENESIS_HASH } from '../../src/models/network'
+import { BUNDLED_CKB_URL, BUNDLED_LIGHT_CKB_URL, LIGHT_CLIENT_TESTNET } from '../../src/utils/const'
const ERROR_MESSAGE = {
MISSING_ARG: `Missing required argument`,
@@ -8,6 +9,15 @@ const ERROR_MESSAGE = {
NETWORK_ID_NOT_FOUND: `messages.network-not-found`,
}
+const uuidV4Mock = jest.fn()
+uuidV4Mock.mockReturnValue('mock uuid')
+
+jest.mock('uuid', () => {
+ return {
+ v4: () => uuidV4Mock()
+ }
+})
+
describe(`Unit tests of networks service`, () => {
const newNetwork: Network = {
name: `new network`,
@@ -16,6 +26,7 @@ describe(`Unit tests of networks service`, () => {
genesisHash: '0x',
id: '',
chain: 'ckb',
+ readonly: true,
}
const newNetworkWithDefaultTypeOf1 = {
@@ -176,4 +187,74 @@ describe(`Unit tests of networks service`, () => {
expect(service.activate(id)).rejects.toThrowError(t(ERROR_MESSAGE.NETWORK_ID_NOT_FOUND, { id }))
})
})
+
+ describe('test migrate network', () => {
+ const readSyncMock = jest.fn()
+ const writeSyncMock = jest.fn()
+ const updateAllMock = jest.fn()
+ const defaultMainnetNetwork = {
+ id: 'mainnet',
+ name: 'Internal Node',
+ remote: BUNDLED_CKB_URL,
+ genesisHash: MAINNET_GENESIS_HASH,
+ chain: "ckb",
+ type: NetworkType.Default,
+ readonly: true,
+ }
+ const defaultLightClientNetwork = {
+ id: 'light_client_testnet',
+ name: 'Light Client Testnet',
+ remote: BUNDLED_LIGHT_CKB_URL,
+ genesisHash: TESTNET_GENESIS_HASH,
+ type: NetworkType.Light,
+ chain: LIGHT_CLIENT_TESTNET,
+ readonly: true,
+ }
+ beforeEach(() => {
+ service.readSync = readSyncMock
+ service.writeSync = writeSyncMock
+ service.updateAll = updateAllMock
+ })
+ afterEach(() => {
+ readSyncMock.mockReset()
+ })
+ it('has migrate', () => {
+ readSyncMock.mockReturnValue(true)
+ //@ts-ignore private-method
+ service.migrateNetwork()
+ expect(writeSyncMock).toBeCalledTimes(0)
+ })
+ it('not find the default network', () => {
+ readSyncMock.mockReturnValueOnce(false).mockReturnValueOnce([])
+ //@ts-ignore private-method
+ service.migrateNetwork()
+ expect(writeSyncMock).toBeCalledWith('MigrateNetwork', true)
+ expect(updateAllMock).toBeCalledTimes(0)
+ })
+ it('not change the default network', () => {
+ readSyncMock.mockReturnValueOnce(false).mockReturnValueOnce([defaultMainnetNetwork, defaultLightClientNetwork])
+ //@ts-ignore private-method
+ service.migrateNetwork()
+ expect(writeSyncMock).toBeCalledWith('MigrateNetwork', true)
+ expect(updateAllMock).toBeCalledWith([defaultMainnetNetwork, defaultLightClientNetwork])
+ })
+ it('change the default network', () => {
+ readSyncMock.mockReturnValueOnce(false).mockReturnValueOnce([{ ...defaultMainnetNetwork, name: 'changed name' }, defaultLightClientNetwork])
+ uuidV4Mock.mockReturnValueOnce('uuidv4')
+ //@ts-ignore private-method
+ service.migrateNetwork()
+ expect(writeSyncMock).toBeCalledWith('MigrateNetwork', true)
+ expect(updateAllMock).toBeCalledWith([
+ defaultMainnetNetwork,
+ defaultLightClientNetwork,
+ {
+ ...defaultMainnetNetwork,
+ id: 'uuidv4',
+ name: 'changed name',
+ readonly: false,
+ type: NetworkType.Normal,
+ }
+ ])
+ })
+ })
})
From def7d52552f0af3c29086b0207cf7cd6c9bcc84d Mon Sep 17 00:00:00 2001
From: yanguoyu <841185308@qq.com>
Date: Fri, 25 Aug 2023 16:29:31 +0800
Subject: [PATCH 2/2] fix: Fix code style
---
packages/neuron-wallet/package.json | 2 +-
packages/neuron-wallet/tests/services/networks.test.ts | 10 ++++++----
2 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/packages/neuron-wallet/package.json b/packages/neuron-wallet/package.json
index 3b2cb48622..ea781459da 100644
--- a/packages/neuron-wallet/package.json
+++ b/packages/neuron-wallet/package.json
@@ -35,7 +35,7 @@
"eslint --fix",
"git add"
],
- "test/**/*.{js,cjs,mjs,jsx,ts,tsx}": [
+ "tests/**/*.{js,cjs,mjs,jsx,ts,tsx}": [
"prettier --ignore-path ../../.prettierignore --write",
"eslint --fix",
"git add"
diff --git a/packages/neuron-wallet/tests/services/networks.test.ts b/packages/neuron-wallet/tests/services/networks.test.ts
index 09d23f287a..ef108d1462 100644
--- a/packages/neuron-wallet/tests/services/networks.test.ts
+++ b/packages/neuron-wallet/tests/services/networks.test.ts
@@ -14,7 +14,7 @@ uuidV4Mock.mockReturnValue('mock uuid')
jest.mock('uuid', () => {
return {
- v4: () => uuidV4Mock()
+ v4: () => uuidV4Mock(),
}
})
@@ -197,7 +197,7 @@ describe(`Unit tests of networks service`, () => {
name: 'Internal Node',
remote: BUNDLED_CKB_URL,
genesisHash: MAINNET_GENESIS_HASH,
- chain: "ckb",
+ chain: 'ckb',
type: NetworkType.Default,
readonly: true,
}
@@ -239,7 +239,9 @@ describe(`Unit tests of networks service`, () => {
expect(updateAllMock).toBeCalledWith([defaultMainnetNetwork, defaultLightClientNetwork])
})
it('change the default network', () => {
- readSyncMock.mockReturnValueOnce(false).mockReturnValueOnce([{ ...defaultMainnetNetwork, name: 'changed name' }, defaultLightClientNetwork])
+ readSyncMock
+ .mockReturnValueOnce(false)
+ .mockReturnValueOnce([{ ...defaultMainnetNetwork, name: 'changed name' }, defaultLightClientNetwork])
uuidV4Mock.mockReturnValueOnce('uuidv4')
//@ts-ignore private-method
service.migrateNetwork()
@@ -253,7 +255,7 @@ describe(`Unit tests of networks service`, () => {
name: 'changed name',
readonly: false,
type: NetworkType.Normal,
- }
+ },
])
})
})