From c4548c9f7fe18fdbf1606ceb7dd09e2566624995 Mon Sep 17 00:00:00 2001 From: Michal Zielenkiewicz Date: Wed, 25 Sep 2024 09:54:48 +0200 Subject: [PATCH] Delay GetChainContext request until needed --- .changelog/2062.internal.md | 1 + .../components/TransactionPreview/index.tsx | 2 +- src/app/state/importaccounts/saga.test.ts | 8 +++++-- src/app/state/importaccounts/saga.ts | 4 ++-- src/app/state/network/index.ts | 7 +++++- src/app/state/network/saga.ts | 24 +++++++++++++++---- src/app/state/network/types.ts | 2 +- src/app/state/persist/syncTabs.ts | 1 + src/app/state/transaction/saga.test.ts | 4 ++++ src/app/state/transaction/saga.ts | 8 +++---- 10 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 .changelog/2062.internal.md diff --git a/.changelog/2062.internal.md b/.changelog/2062.internal.md new file mode 100644 index 0000000000..801e9a696b --- /dev/null +++ b/.changelog/2062.internal.md @@ -0,0 +1 @@ +Delay GetChainContext request until needed diff --git a/src/app/components/TransactionPreview/index.tsx b/src/app/components/TransactionPreview/index.tsx index 66d9e6cbfe..94ca9b76ad 100644 --- a/src/app/components/TransactionPreview/index.tsx +++ b/src/app/components/TransactionPreview/index.tsx @@ -16,7 +16,7 @@ import { TransactionTypeFormatter } from '../TransactionTypeFormatter' interface Props { preview: Preview walletAddress: string - chainContext: string + chainContext?: string } export const TransactionPreview = memo((props: Props) => { diff --git a/src/app/state/importaccounts/saga.test.ts b/src/app/state/importaccounts/saga.test.ts index b67e0f21a8..be96531d05 100644 --- a/src/app/state/importaccounts/saga.test.ts +++ b/src/app/state/importaccounts/saga.test.ts @@ -191,7 +191,9 @@ describe('importAccounts Sagas', () => { const mockTransport = { close: jest.fn() } return expectSaga(sign, mockSigner as unknown as LedgerSigner, {} as any) - .withState({ network: {} }) + .withState({ + network: { chainContext: '0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76' }, + }) .provide([ [matchers.call.fn(TransportWebUSB.isSupported), true], [matchers.call.fn(TransportWebUSB.create), mockTransport], @@ -212,7 +214,9 @@ describe('importAccounts Sagas', () => { expect(err).toEqual(new Error('Dummy error')) } }) - .withState({ network: {} }) + .withState({ + network: { chainContext: '0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76' }, + }) .provide([ [matchers.call.fn(TransportWebUSB.isSupported), true], [matchers.call.fn(TransportWebUSB.create), mockTransport], diff --git a/src/app/state/importaccounts/saga.ts b/src/app/state/importaccounts/saga.ts index c658dde534..568053041e 100644 --- a/src/app/state/importaccounts/saga.ts +++ b/src/app/state/importaccounts/saga.ts @@ -8,7 +8,6 @@ import { all, call, delay, fork, put, select, takeEvery } from 'typed-redux-saga import { ErrorPayload, WalletError, WalletErrors } from 'types/errors' import { LedgerWalletType, WalletType } from 'app/state/wallet/types' import { importAccountsActions } from '.' -import { selectChainContext } from '../network/selectors' import { ImportAccountsListAccount, ImportAccountsStep } from './types' import type Transport from '@ledgerhq/hw-transport' import { @@ -22,6 +21,7 @@ import { import { getAccountBalanceWithFallback } from '../../lib/getAccountBalanceWithFallback' import BleTransport from '@oasisprotocol/ionic-ledger-hw-transport-ble/lib' import { ScanResult } from '@capacitor-community/bluetooth-le' +import { getChainContext } from '../network/saga' function* setStep(step: ImportAccountsStep) { yield* put(importAccountsActions.setStep(step)) @@ -233,7 +233,7 @@ export function* sign(signer: LedgerSigner, tw: oasis.consensus.TransactionWr } else { transport = yield* getUSBTransport() } - const chainContext = yield* select(selectChainContext) + const chainContext = yield* call(getChainContext) signer.setTransport(transport) try { diff --git a/src/app/state/network/index.ts b/src/app/state/network/index.ts index 863435ce08..e3add9d638 100644 --- a/src/app/state/network/index.ts +++ b/src/app/state/network/index.ts @@ -15,13 +15,18 @@ export const networkSlice = createSlice({ initialState, reducers: { initializeNetwork() {}, - selectNetwork(state, action: PayloadAction) {}, + selectNetwork(state, action: PayloadAction) { + state.chainContext = initialState.chainContext + }, initialNetworkSelected(state, action: PayloadAction) { Object.assign(state, action.payload) }, networkSelected(state, action: PayloadAction) { Object.assign(state, action.payload) }, + setChainContext(state, action: PayloadAction) { + state.chainContext = action.payload + }, }, }) diff --git a/src/app/state/network/saga.ts b/src/app/state/network/saga.ts index d2154f3350..484350fb91 100644 --- a/src/app/state/network/saga.ts +++ b/src/app/state/network/saga.ts @@ -8,8 +8,9 @@ import { backend, backendApi } from 'vendors/backend' import { networkActions } from '.' import { SyncedRootState } from '../persist/types' -import { selectSelectedNetwork } from './selectors' +import { selectChainContext, selectSelectedNetwork } from './selectors' import { NetworkType } from './types' +import { WalletError, WalletErrors } from 'types/errors' /** * Return a nic client for the specified network, @@ -32,6 +33,23 @@ export function* getExplorerAPIs() { return backendApi(url) } +export function* getChainContext() { + const chainContext = yield* select(selectChainContext) + if (chainContext) { + return chainContext + } + + try { + const selectedNetwork = yield* select(selectSelectedNetwork) + const nic = yield* call(getOasisNic, selectedNetwork) + const fetchedChainContext = yield* call([nic, nic.consensusGetChainContext]) + yield* put(networkActions.setChainContext(fetchedChainContext)) + return fetchedChainContext + } catch (error) { + throw new WalletError(WalletErrors.UnknownGrpcError, 'Could not fetch data') + } +} + export function* selectNetwork({ network, isInitializing, @@ -40,12 +58,10 @@ export function* selectNetwork({ isInitializing: boolean }) { const nic = yield* call(getOasisNic, network) - const { epoch, chainContext } = yield* all({ + const { epoch } = yield* all({ epoch: call([nic, nic.beaconGetEpoch], oasis.consensus.HEIGHT_LATEST), - chainContext: call([nic, nic.consensusGetChainContext]), }) const networkState = { - chainContext: chainContext, ticker: config[network].ticker, epoch: Number(epoch), // Will lose precision in a few billion years at 1 epoch per hour selectedNetwork: network, diff --git a/src/app/state/network/types.ts b/src/app/state/network/types.ts index 1d33e8f9d1..ecdd00085b 100644 --- a/src/app/state/network/types.ts +++ b/src/app/state/network/types.ts @@ -9,7 +9,7 @@ export interface NetworkState { ticker: string /** chainContext / Genesis Hash */ - chainContext: string + chainContext?: string /** Current epoch */ epoch: number diff --git a/src/app/state/persist/syncTabs.ts b/src/app/state/persist/syncTabs.ts index 9e28ebd31b..563fac0ea9 100644 --- a/src/app/state/persist/syncTabs.ts +++ b/src/app/state/persist/syncTabs.ts @@ -61,6 +61,7 @@ export const whitelistTabSyncActions: Record = { [rootSlices.wallet.actions.walletOpened.type]: true, [rootSlices.wallet.actions.updateBalance.type]: true, [rootSlices.network.actions.networkSelected.type]: true, + [rootSlices.network.actions.setChainContext.type]: true, [rootSlices.persist.actions.setUnlockedRootState.type]: true, [rootSlices.persist.actions.resetRootState.type]: true, [rootSlices.persist.actions.skipUnlocking.type]: true, diff --git a/src/app/state/transaction/saga.test.ts b/src/app/state/transaction/saga.test.ts index c302312f21..5f657a6ad4 100644 --- a/src/app/state/transaction/saga.test.ts +++ b/src/app/state/transaction/saga.test.ts @@ -19,6 +19,10 @@ const makeState = (wallet: Partial, rootState: DeepPartialRootState = {} wallets: { [wallet.address!]: wallet }, selectedWallet: wallet.address, }, + network: { + selectedNetwork: 'testnet', + chainContext: '0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76', + }, ...rootState, } } diff --git a/src/app/state/transaction/saga.ts b/src/app/state/transaction/saga.ts index 0b8197029e..b64ccdc5a0 100644 --- a/src/app/state/transaction/saga.ts +++ b/src/app/state/transaction/saga.ts @@ -9,9 +9,9 @@ import { call, delay, put, race, select, take, takeEvery } from 'typed-redux-sag import { ErrorPayload, ExhaustedTypeError, WalletError, WalletErrors } from 'types/errors' import { transactionActions } from '.' import { sign } from '../importaccounts/saga' -import { getOasisNic } from '../network/saga' +import { getChainContext, getOasisNic } from '../network/saga' import { selectAccountAddress, selectAccountAllowances } from '../account/selectors' -import { selectChainContext, selectSelectedNetwork } from '../network/selectors' +import { selectSelectedNetwork } from '../network/selectors' import { selectActiveWallet } from '../wallet/selectors' import { Wallet, WalletType } from '../wallet/types' import { Transaction, TransactionPayload, TransactionStatus, TransactionStep, TransactionType } from './types' @@ -137,7 +137,7 @@ function* prepareReclaimEscrow(signer: Signer, shares: bigint, validator: string export function* doTransaction(action: PayloadAction) { const wallet = yield* select(selectActiveWallet) const nic = yield* call(getOasisNic) - const chainContext = yield* select(selectChainContext) + const chainContext = yield* call(getChainContext) const networkType = yield* select(selectSelectedNetwork) try { @@ -282,7 +282,7 @@ export function* submitParaTimeTransaction(runtime: Runtime, transaction: ParaTi ? yield* call(getEvmBech32Address, privateToEthAddress(transaction.ethPrivateKey)) : yield* select(selectAccountAddress) const nic = yield* call(getOasisNic) - const chainContext = yield* select(selectChainContext) + const chainContext = yield* call(getChainContext) const paraTimeTransactionSigner = transaction.ethPrivateKey ? yield* call(signerFromEthPrivateKey, misc.fromHex(transaction.ethPrivateKey)) : yield* getSigner()