Skip to content

Commit

Permalink
fix connection to nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Polyakov committed Jul 9, 2021
1 parent 5d428ae commit 2a4d9aa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
44 changes: 26 additions & 18 deletions src/store/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,16 @@ const actions = {
throw error
}
},
async setNode ({ commit, dispatch, state }, node) {
async setNode ({ commit, dispatch, getters, state }, node) {
const endpoint = node?.address ?? ''
const connectingNodeChanged = () => endpoint !== state.nodeAddressConnecting
const isTrustedEndpoint = endpoint in getters.defaultNodesHashTable

try {
if (!endpoint) {
throw new Error('Node address is not set')
}

if (!state.chainGenesisHash) {
await dispatch('getNetworkChainGenesisHash')
}

commit(types.SET_NODE_REQUEST, node)

console.info('Connection request to node', endpoint)
Expand All @@ -210,13 +207,24 @@ const actions = {

const nodeChainGenesisHash = connection.api.genesisHash.toHex()

if (nodeChainGenesisHash !== state.chainGenesisHash) {
throw new AppHandledError({
key: 'node.errors.network',
payload: { address: endpoint }
},
`Chain genesis hash doesn't match: "${nodeChainGenesisHash}" recieved, should be "${state.chainGenesisHash}"`
)
// if connected node is custom node, we should check genesis hash
if (!isTrustedEndpoint) {
// if genesis hash is not set in state, fetch it
if (!state.chainGenesisHash) {
await dispatch('getNetworkChainGenesisHash')
}

if (nodeChainGenesisHash !== state.chainGenesisHash) {
throw new AppHandledError({
key: 'node.errors.network',
payload: { address: endpoint }
},
`Chain genesis hash doesn't match: "${nodeChainGenesisHash}" recieved, should be "${state.chainGenesisHash}"`
)
}
} else {
// just update genesis hash (needed for dev, test stands after their reset)
commit(types.SET_NETWORK_CHAIN_GENESIS_HASH, nodeChainGenesisHash)
}

commit(types.SET_NODE_SUCCESS, node)
Expand Down Expand Up @@ -255,13 +263,13 @@ const actions = {
commit(types.SET_CUSTOM_NODES, nodes)
},
async getNetworkChainGenesisHash ({ commit, state }) {
const genesisHash = await Promise.any(state.defaultNodes.map(node => fetchRpc(getRpcEndpoint(node.address), 'chain_getBlockHash', [0])))

if (!genesisHash) {
throw new Error('Failed to fetch network chain genesis hash')
try {
const genesisHash = await Promise.any(state.defaultNodes.map(node => fetchRpc(getRpcEndpoint(node.address), 'chain_getBlockHash', [0])))
commit(types.SET_NETWORK_CHAIN_GENESIS_HASH, genesisHash)
} catch (error) {
commit(types.SET_NETWORK_CHAIN_GENESIS_HASH, '')
throw error
}

commit(types.SET_NETWORK_CHAIN_GENESIS_HASH, genesisHash)
},
setSlippageTolerance ({ commit }, value) {
commit(types.SET_SLIPPAGE_TOLERANCE, value)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from '@/api'

export const getRpcEndpoint = (wsEndpoint: string): string => {
// for soramitsu nodes
if (/^wss:\/\/ws\.(?:.+\.)*sora2\.soramitsu\.co\.jp\/?$/.test(wsEndpoint)) {
if (/^wss:\/\/ws\.(?:.+\.)*(sora\.org|sora2\.soramitsu\.co\.jp)\/?$/.test(wsEndpoint)) {
return wsEndpoint.replace(/^wss:\/\/ws/, 'https://rpc')
}
return wsEndpoint.replace(/^ws(s)?:/, 'http$1:')
Expand Down

0 comments on commit 2a4d9aa

Please sign in to comment.