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 'swap-initial-deposit': bigint
mainnet: boolean mainnet: boolean
'full-node': boolean 'full-node': boolean
'chain-enable': boolean
'cors-allowed-origins': string 'cors-allowed-origins': string
'resolver-options': string 'resolver-options': string
'use-postage-snapshot': boolean 'use-postage-snapshot': boolean
'data-dir': string 'data-dir': string
transaction: string
'block-hash': string
'swap-endpoint'?: 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 BeeContext } from '../../providers/Bee'
import { Context as SettingsContext } from '../../providers/Settings' import { Context as SettingsContext } from '../../providers/Settings'
import { useSnackbar } from 'notistack' import { useSnackbar } from 'notistack'
import { restartBeeNode, setJsonRpcInDesktop } from '../../utils/desktop'
import { BeeModes } from '@ethersphere/bee-js'
export default function SettingsPage(): ReactElement { export default function SettingsPage(): ReactElement {
const { const {
@@ -19,10 +21,36 @@ export default function SettingsPage(): ReactElement {
rpcProviderUrl, rpcProviderUrl,
isLoading, isLoading,
isDesktop, isDesktop,
desktopUrl,
setAndPersistJsonRpcProvider, setAndPersistJsonRpcProvider,
} = useContext(SettingsContext) } = useContext(SettingsContext)
const { refresh } = useContext(BeeContext) const { refresh, nodeInfo } = useContext(BeeContext)
const { enqueueSnackbar } = useSnackbar() 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) { if (isLoading) {
return ( return (
@@ -52,17 +80,7 @@ export default function SettingsPage(): ReactElement {
value={rpcProviderUrl} value={rpcProviderUrl}
helperText="Changing the value will restart your bee node." helperText="Changing the value will restart your bee node."
confirmLabel="Save and restart" confirmLabel="Save and restart"
onConfirm={value => { onConfirm={handleSetRpcUrl}
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' })
})
}}
/> />
</ExpandableList> </ExpandableList>
{isDesktop && ( {isDesktop && (
+6 -23
View File
@@ -2,7 +2,6 @@ import { Bee, BeeDebug } from '@ethersphere/bee-js'
import { providers } from 'ethers' import { providers } from 'ethers'
import { createContext, ReactNode, ReactElement, useEffect, useState } from 'react' import { createContext, ReactNode, ReactElement, useEffect, useState } from 'react'
import { useGetBeeConfig } from '../hooks/apiHooks' 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' import { DEFAULT_BEE_API_HOST, DEFAULT_BEE_DEBUG_API_HOST, DEFAULT_RPC_URL } from '../constants'
const LocalStorageKeys = { const LocalStorageKeys = {
@@ -25,7 +24,7 @@ interface ContextInterface {
ensResolver: string | null ensResolver: string | null
setApiUrl: (url: string) => void setApiUrl: (url: string) => void
setDebugApiUrl: (url: string) => void setDebugApiUrl: (url: string) => void
setAndPersistJsonRpcProvider: (url: string) => Promise<void> setAndPersistJsonRpcProvider: (url: string) => void
isLoading: boolean isLoading: boolean
error: Error | null error: Error | null
} }
@@ -139,12 +138,7 @@ export function Provider({ children, ...propsSettings }: Props): ReactElement {
cors: config?.['cors-allowed-origins'] ?? null, cors: config?.['cors-allowed-origins'] ?? null,
dataDir: config?.['data-dir'] ?? null, dataDir: config?.['data-dir'] ?? null,
ensResolver: config?.['resolver-options'] ?? null, ensResolver: config?.['resolver-options'] ?? null,
setAndPersistJsonRpcProvider: setAndPersistJsonRpcProviderClosure( setAndPersistJsonRpcProvider: setAndPersistJsonRpcProviderClosure(setRpcProviderUrl, setRpcProvider),
isDesktop,
desktopUrl,
setRpcProviderUrl,
setRpcProvider,
),
isLoading, isLoading,
error, error,
}} }}
@@ -163,23 +157,12 @@ function makeHttpUrl(string: string): string {
} }
function setAndPersistJsonRpcProviderClosure( function setAndPersistJsonRpcProviderClosure(
isDesktop: boolean,
desktopUrl: string,
setProviderUrl: (url: string) => void, setProviderUrl: (url: string) => void,
setProvider: (prov: providers.JsonRpcProvider) => void, setProvider: (prov: providers.JsonRpcProvider) => void,
) { ) {
return async (providerUrl: string) => { return (providerUrl: string) => {
try { localStorage.setItem(LocalStorageKeys.providerUrl, providerUrl)
localStorage.setItem(LocalStorageKeys.providerUrl, providerUrl) setProviderUrl(providerUrl)
setProviderUrl(providerUrl) setProvider(new providers.JsonRpcProvider(providerUrl))
setProvider(new providers.JsonRpcProvider(providerUrl))
if (isDesktop) {
await setJsonRpcInDesktop(desktopUrl, providerUrl)
await restartBeeNode(desktopUrl)
}
} catch (error) {
console.error(error) // eslint-disable-line
}
} }
} }
-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> { export async function upgradeToLightNode(desktopUrl: string, rpcProvider: string): Promise<void> {
await updateDesktopConfiguration(desktopUrl, { await updateDesktopConfiguration(desktopUrl, {
'chain-enable': true,
'swap-enable': true, 'swap-enable': true,
'swap-endpoint': rpcProvider, 'swap-endpoint': rpcProvider,
}) })