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

fix: failed to connect to ckb rpc after upgrading electron to v23 #2652

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/components/NetworkEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const NetworkEditor = () => {
onChange={onChange}
label={t('settings.network.edit-network.rpc-url')}
error={editor.urlError}
placeholder="http://localhost:8114"
placeholder="http://127.0.0.1:8114"
required
autoFocus
/>
Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-ui/src/stories/NetworkSetting.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ const states: { [title: string]: State.Network[] } = {
{
id: 'Mainnet',
name: 'Mainnet',
remote: 'http://localhost:8114',
remote: 'http://127.0.0.1:8114',
chain: 'ckb',
type: 0,
},
{
id: 'Testnet',
name: 'Testnet',
remote: 'http://localhost:8114',
remote: 'http://127.0.0.1:8114',
chain: 'ckb_testnet',
type: 1,
},
{
id: 'Local',
name: 'Local',
remote: 'http://localhost:8114',
remote: 'http://127.0.0.1:8114',
chain: 'ckb_devnet',
type: 1,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/stories/NetworkStatus.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import NetworkStatus, { NetworkStatusProps } from 'components/NetworkStatus'
const defaultProps: Omit<NetworkStatusProps, 'syncPercents' | 'syncBlockNumbers'> = {
network: {
name: 'network',
remote: 'http://localhost:3000',
remote: 'http://127.0.0.1:3000',
type: 0,
id: 'd',
chain: 'ckb',
Expand Down Expand Up @@ -67,7 +67,7 @@ stories.add('With knobs', () => {
const props = {
network: {
name: text('Network name', 'network name'),
remote: text('Remote', 'http://localhost:3000'),
remote: text('Remote', 'http://127.0.0.1:3000'),
type: select('Type', [0, 1], 0) as any,
id: text('id', 'd'),
chain: select('Chain', ['ckb', 'ckb_testnet', 'ckb_dev'], 'ckb'),
Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-ui/src/tests/is/isMainnet/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ const fixtures = {
'Should return false when network id cannot be found in network list': {
params: {
networkID: 'testnet',
networks: [{ id: 'mainnet', chain: 'ckb', type: 0 as 0 | 1, name: 'Mainnet', remote: 'http://localhost:8114' }],
networks: [{ id: 'mainnet', chain: 'ckb', type: 0 as 0 | 1, name: 'Mainnet', remote: 'http://127.0.0.1:8114' }],
},
expected: false,
},
"Should return false when network id can be found in network list but it's not Mainnet": {
params: {
networkID: 'testnet',
networks: [
{ id: 'testnet', chain: 'ckb_testnet', type: 0 as 0 | 1, name: 'Mainnet', remote: 'http://localhost:8114' },
{ id: 'testnet', chain: 'ckb_testnet', type: 0 as 0 | 1, name: 'Mainnet', remote: 'http://127.0.0.1:8114' },
],
},
expected: false,
},
"Should return true when network id can be found in network list and it's Mainnet": {
params: {
networkID: 'mainnet',
networks: [{ id: 'mainnet', chain: 'ckb', type: 0 as 0 | 1, name: 'Mainnet', remote: 'http://localhost:8114' }],
networks: [{ id: 'mainnet', chain: 'ckb', type: 0 as 0 | 1, name: 'Mainnet', remote: 'http://127.0.0.1:8114' }],
},
expected: true,
},
Expand Down
23 changes: 22 additions & 1 deletion packages/neuron-wallet/src/services/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import { Validate, Required } from 'utils/validators'
import { UsedName, NetworkNotFound, InvalidFormat } from 'exceptions'
import { MAINNET_GENESIS_HASH, EMPTY_GENESIS_HASH, NetworkType, Network } from 'models/network'
import CommonUtils from 'utils/common'
import { BUNDLED_CKB_URL } from 'utils/const'

const presetNetworks: { selected: string; networks: Network[] } = {
selected: 'mainnet',
networks: [
{
id: 'mainnet',
name: 'Default',
remote: 'http://localhost:8114',
remote: BUNDLED_CKB_URL,
genesisHash: MAINNET_GENESIS_HASH,
type: NetworkType.Default,
chain: 'ckb'
Expand Down Expand Up @@ -47,6 +48,12 @@ export default class NetworksService extends Store {

public getAll = () => {
const networks = this.readSync<Network[]>(NetworksKey.List) || presetNetworks.networks
networks.forEach((network) => {
// Currently, the RPC interface of the CKB node is bound to IPv4 by default.
// Starting from node17, its DNS resolution is no longer `ipv4first`.
// 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
Expand Down Expand Up @@ -176,3 +183,17 @@ export default class NetworksService extends Store {
return network
}
}

function applyLocalhostIPv4Resolve(url: string): string {
Keith-CY marked this conversation as resolved.
Show resolved Hide resolved
let urlObj
try {
urlObj = new URL(url)
} catch (err) {
return url
}

if (urlObj.hostname !== 'localhost') return url

urlObj.hostname = '127.0.0.1'
return urlObj.href
}
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/utils/const.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const MIN_PASSWORD_LENGTH = 8
export const MAX_PASSWORD_LENGTH = 50
export const BUNDLED_CKB_URL = 'http://localhost:8114'
export const BUNDLED_CKB_URL = 'http://127.0.0.1:8114'
export const SETTINGS_WINDOW_TITLE = process.platform === 'darwin' ? 'settings.title.mac' : 'settings.title.normal'
export const SETTINGS_WINDOW_WIDTH = 900
export const DEFAULT_UDT_SYMBOL = 'Unknown'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jest.mock('../../src/services/networks', () => {
return {
getCurrent() {
return {
remote: 'http://localhost:8114'
remote: 'http://127.0.0.1:8114'
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions packages/neuron-wallet/tests/services/networks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const ERROR_MESSAGE = {
describe(`Unit tests of networks service`, () => {
const newNetwork: Network = {
name: `new network`,
remote: `http://localhost:8114`,
remote: `http://127.0.0.1:8114`,
type: 0,
genesisHash: '0x',
id: '',
Expand All @@ -20,7 +20,7 @@ describe(`Unit tests of networks service`, () => {

const newNetworkWithDefaultTypeOf1 = {
name: `new network with the default type of 1`,
remote: `http://localhost:8114`,
remote: `http://127.0.0.1:8114`,
id: '',
}

Expand Down Expand Up @@ -82,12 +82,21 @@ describe(`Unit tests of networks service`, () => {

it(`update the network' address`, async () => {
const network = await service.create(newNetworkWithDefaultTypeOf1.name, newNetworkWithDefaultTypeOf1.remote)
const address = `http://localhost:8115`
const address = `http://127.0.0.1:8115`
await service.update(network.id, { remote: address })
const updated = service.get(network.id)
expect(updated && updated.remote).toBe(address)
})

it(`use ipv4 to resolve the localhost in network' remote`, async () => {
const network = await service.create(newNetworkWithDefaultTypeOf1.name, 'http://localhost:8114/')
const created = service.get(network.id)
expect(created && created.remote).toBe('http://127.0.0.1:8114/')
await service.update(network.id, { remote: 'http://localhost:8114/' })
const updated = service.get(network.id)
expect(updated && updated.remote).toBe('http://127.0.0.1:8114/')
})

it(`set the network to be the current one`, async () => {
const network = await service.create(newNetworkWithDefaultTypeOf1.name, newNetworkWithDefaultTypeOf1.remote)
await service.activate(network.id)
Expand Down Expand Up @@ -151,11 +160,11 @@ describe(`Unit tests of networks service`, () => {

describe(`validation on network existence`, () => {
beforeEach(async () => {
await service.create('Default', 'http://localhost:8114')
await service.create('Default', 'http://127.0.0.1:8114')
});

it(`create network with existing name of Default`, () => {
expect(service.create('Default', 'http://localhost:8114')).rejects.toThrowError(t(ERROR_MESSAGE.NAME_USED))
expect(service.create('Default', 'http://127.0.0.1:8114')).rejects.toThrowError(t(ERROR_MESSAGE.NAME_USED))
})

it(`update network which is not existing`, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/tests/services/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ describe('NodeService', () => {
expect(stubbedTipNumberSubjectCallback).toHaveBeenCalledWith('0')
})
describe('targets to bundled node', () => {
const bundledNodeUrl = 'http://localhost:8114'
const bundledNodeUrl = 'http://127.0.0.1:8114'
beforeEach(async () => {
stubbedCKBSetNode.mockImplementation(() => {
nodeService.ckb.node.url = bundledNodeUrl
Expand Down