fix: rpc endpoint setting ultra-light mode logic (#547)

This commit is contained in:
Adam Uhlíř
2022-09-15 05:42:53 -07:00
committed by GitHub
parent 5295bd5b01
commit e2dd077118
4 changed files with 37 additions and 40 deletions
-3
View File
@@ -94,13 +94,10 @@ export interface BeeConfig {
'swap-initial-deposit': bigint
mainnet: boolean
'full-node': boolean
'chain-enable': boolean
'cors-allowed-origins': string
'resolver-options': string
'use-postage-snapshot': boolean
'data-dir': string
transaction: string
'block-hash': string
'swap-endpoint'?: string
}
+31 -13
View File
@@ -5,6 +5,8 @@ import ExpandableListItemInput from '../../components/ExpandableListItemInput'
import { Context as BeeContext } from '../../providers/Bee'
import { Context as SettingsContext } from '../../providers/Settings'
import { useSnackbar } from 'notistack'
import { restartBeeNode, setJsonRpcInDesktop } from '../../utils/desktop'
import { BeeModes } from '@ethersphere/bee-js'
export default function SettingsPage(): ReactElement {
const {
@@ -19,10 +21,36 @@ export default function SettingsPage(): ReactElement {
rpcProviderUrl,
isLoading,
isDesktop,
desktopUrl,
setAndPersistJsonRpcProvider,
} = useContext(SettingsContext)
const { refresh } = useContext(BeeContext)
const { enqueueSnackbar } = useSnackbar()
const { refresh, nodeInfo } = useContext(BeeContext)
const { enqueueSnackbar, closeSnackbar } = useSnackbar()
async function handleSetRpcUrl(value: string) {
try {
setAndPersistJsonRpcProvider(value)
// We can't set the RPC URL to the `swap-endpoint` Bee config value unless the Bee node is already in
// light mode as setting this config value, basically upgrades the node to light mode.
if (isDesktop && nodeInfo?.beeMode === BeeModes.LIGHT) {
await setJsonRpcInDesktop(desktopUrl, rpcProviderUrl)
const snackKey = enqueueSnackbar('RPC endpoint successfully changed, restarting Bee node...', {
variant: 'success',
})
await restartBeeNode(desktopUrl)
closeSnackbar(snackKey)
enqueueSnackbar('Bee node restarted', { variant: 'success' })
} else {
enqueueSnackbar('RPC endpoint successfully changed', { variant: 'success' })
}
await refresh()
} catch (e) {
console.error(e) //eslint-disable-line
enqueueSnackbar(`Failed to change RPC endpoint. Error: ${e}`, { variant: 'error' })
}
}
if (isLoading) {
return (
@@ -52,17 +80,7 @@ export default function SettingsPage(): ReactElement {
value={rpcProviderUrl}
helperText="Changing the value will restart your bee node."
confirmLabel="Save and restart"
onConfirm={value => {
setAndPersistJsonRpcProvider(value)
.then(() => {
refresh()
enqueueSnackbar('Settings changed, restarting bee node...', { variant: 'success' })
})
.catch(error => {
console.log(error) //eslint-disable-line
enqueueSnackbar(`Failed to change RPC endpoint. Error: ${error}`, { variant: 'success' })
})
}}
onConfirm={handleSetRpcUrl}
/>
</ExpandableList>
{isDesktop && (
+6 -23
View File
@@ -2,7 +2,6 @@ import { Bee, BeeDebug } from '@ethersphere/bee-js'
import { providers } from 'ethers'
import { createContext, ReactNode, ReactElement, useEffect, useState } from 'react'
import { useGetBeeConfig } from '../hooks/apiHooks'
import { restartBeeNode, setJsonRpcInDesktop } from '../utils/desktop'
import { DEFAULT_BEE_API_HOST, DEFAULT_BEE_DEBUG_API_HOST, DEFAULT_RPC_URL } from '../constants'
const LocalStorageKeys = {
@@ -25,7 +24,7 @@ interface ContextInterface {
ensResolver: string | null
setApiUrl: (url: string) => void
setDebugApiUrl: (url: string) => void
setAndPersistJsonRpcProvider: (url: string) => Promise<void>
setAndPersistJsonRpcProvider: (url: string) => void
isLoading: boolean
error: Error | null
}
@@ -139,12 +138,7 @@ export function Provider({ children, ...propsSettings }: Props): ReactElement {
cors: config?.['cors-allowed-origins'] ?? null,
dataDir: config?.['data-dir'] ?? null,
ensResolver: config?.['resolver-options'] ?? null,
setAndPersistJsonRpcProvider: setAndPersistJsonRpcProviderClosure(
isDesktop,
desktopUrl,
setRpcProviderUrl,
setRpcProvider,
),
setAndPersistJsonRpcProvider: setAndPersistJsonRpcProviderClosure(setRpcProviderUrl, setRpcProvider),
isLoading,
error,
}}
@@ -163,23 +157,12 @@ function makeHttpUrl(string: string): string {
}
function setAndPersistJsonRpcProviderClosure(
isDesktop: boolean,
desktopUrl: string,
setProviderUrl: (url: string) => void,
setProvider: (prov: providers.JsonRpcProvider) => void,
) {
return async (providerUrl: string) => {
try {
localStorage.setItem(LocalStorageKeys.providerUrl, providerUrl)
setProviderUrl(providerUrl)
setProvider(new providers.JsonRpcProvider(providerUrl))
if (isDesktop) {
await setJsonRpcInDesktop(desktopUrl, providerUrl)
await restartBeeNode(desktopUrl)
}
} catch (error) {
console.error(error) // eslint-disable-line
}
return (providerUrl: string) => {
localStorage.setItem(LocalStorageKeys.providerUrl, providerUrl)
setProviderUrl(providerUrl)
setProvider(new providers.JsonRpcProvider(providerUrl))
}
}
-1
View File
@@ -12,7 +12,6 @@ export async function getBzzPriceAsDai(desktopUrl: string): Promise<Token> {
export async function upgradeToLightNode(desktopUrl: string, rpcProvider: string): Promise<void> {
await updateDesktopConfiguration(desktopUrl, {
'chain-enable': true,
'swap-enable': true,
'swap-endpoint': rpcProvider,
})