Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Change default network name to Internal Node #2813

Merged
merged 2 commits into from Aug 31, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/neuron-ui/src/components/NetworkSetting/index.tsx
Expand Up @@ -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) => {
Expand Down Expand Up @@ -92,12 +91,12 @@ const NetworkSetting = ({ chain = chainState, settings: { networks = [] } }: Sta
),
suffix: (
<div className={styles.suffix}>
{network.chain === LIGHT_CLIENT_TESTNET ? null : (
{network.readonly ? null : (
<button type="button" aria-label={t('common.edit')} onClick={onHandleNetwork}>
<EditNetwork data-action="edit" data-id={network.id} />
</button>
)}
{network.type && network.chain !== LIGHT_CLIENT_TESTNET ? (
{network.type && !network.readonly ? (
<button type="button" aria-label={t('common.delete')} onClick={onHandleNetwork}>
<DeleteNetwork data-action="delete" data-id={network.id} />
</button>
Expand Down
3 changes: 3 additions & 0 deletions packages/neuron-ui/src/stories/NetworkSetting.stories.tsx
Expand Up @@ -13,6 +13,7 @@ const states: { [title: string]: State.Network[] } = {
chain: 'ckb',
type: 0,
genesisHash: '0x92b197aa1fba0f63633922c61c92375c9c074a93e85963554f5499fe1450d0e5',
readonly: true,
},
{
id: 'Testnet',
Expand All @@ -21,6 +22,7 @@ const states: { [title: string]: State.Network[] } = {
chain: 'ckb_testnet',
type: 1,
genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606',
readonly: false,
},
{
id: 'Local',
Expand All @@ -29,6 +31,7 @@ const states: { [title: string]: State.Network[] } = {
chain: 'ckb_devnet',
type: 1,
genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606',
readonly: false,
},
],
}
Expand Down
3 changes: 3 additions & 0 deletions packages/neuron-ui/src/tests/is/isMainnet/fixtures.ts
Expand Up @@ -24,6 +24,7 @@ const fixtures = {
name: 'Mainnet',
remote: 'http://127.0.0.1:8114',
genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606',
readonly: true,
},
],
},
Expand All @@ -40,6 +41,7 @@ const fixtures = {
name: 'Mainnet',
remote: 'http://127.0.0.1:8114',
genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606',
readonly: false,
},
],
},
Expand All @@ -56,6 +58,7 @@ const fixtures = {
name: 'Mainnet',
remote: 'http://127.0.0.1:8114',
genesisHash: '0x10639e0895502b5688a6be8cf69460d76541bfa4821629d86d62ba0aae3f9606',
readonly: true,
},
],
},
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/types/App/index.d.ts
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/package.json
Expand Up @@ -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"
Expand Down
10 changes: 9 additions & 1 deletion packages/neuron-wallet/src/controllers/api.ts
Expand Up @@ -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<Network> }) => {
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-wallet/src/models/network.ts
Expand Up @@ -17,4 +17,5 @@ export interface Network {
type: NetworkType
genesisHash: string
chain: ChainType | string // returned by rpc.getBlockchainInfo
readonly: boolean
}
47 changes: 43 additions & 4 deletions packages/neuron-wallet/src/services/networks.ts
Expand Up @@ -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,
},
],
}
Expand All @@ -32,15 +33,19 @@ const lightClientNetwork: Network[] = [
genesisHash: TESTNET_GENESIS_HASH,
type: NetworkType.Light,
chain: LIGHT_CLIENT_TESTNET,
readonly: true,
},
]

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

Expand All @@ -62,6 +67,7 @@ export default class NetworksService extends Store {
this.updateAll([...networks, ...lightClientNetwork])
this.writeSync(NetworksKey.AddedLightNetwork, true)
}
this.migrateNetwork()
}

public getAll = () => {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -200,6 +204,41 @@ export default class NetworksService extends Store {

return network
}

private migrateNetwork() {
const migrated = this.readSync<boolean>(NetworksKey.MigrateNetwork)
if (!migrated) {
const networks = this.readSync<Network[]>(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 {
Expand Down
85 changes: 84 additions & 1 deletion packages/neuron-wallet/tests/services/networks.test.ts
@@ -1,13 +1,23 @@
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`,
NAME_USED: `Network name is used`,
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`,
Expand All @@ -16,6 +26,7 @@ describe(`Unit tests of networks service`, () => {
genesisHash: '0x',
id: '',
chain: 'ckb',
readonly: true,
}

const newNetworkWithDefaultTypeOf1 = {
Expand Down Expand Up @@ -176,4 +187,76 @@ 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,
},
])
})
})
})