Skip to content

Commit aa5dee4

Browse files
authored
Merge pull request #2062 from oasisprotocol/mz/chainContext
Delay GetChainContext request until needed
2 parents e90e33c + 12e740b commit aa5dee4

File tree

12 files changed

+88
-20
lines changed

12 files changed

+88
-20
lines changed

.changelog/2062.internal.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Delay GetChainContext request until needed

src/app/components/TransactionPreview/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { TransactionTypeFormatter } from '../TransactionTypeFormatter'
1616
interface Props {
1717
preview: Preview
1818
walletAddress: string
19-
chainContext: string
19+
chainContext?: string
2020
}
2121

2222
export const TransactionPreview = memo((props: Props) => {

src/app/state/importaccounts/saga.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ describe('importAccounts Sagas', () => {
191191
const mockTransport = { close: jest.fn() }
192192

193193
return expectSaga(sign, mockSigner as unknown as LedgerSigner, {} as any)
194-
.withState({ network: {} })
194+
.withState({
195+
network: { chainContext: '0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76' },
196+
})
195197
.provide([
196198
[matchers.call.fn(TransportWebUSB.isSupported), true],
197199
[matchers.call.fn(TransportWebUSB.create), mockTransport],
@@ -212,7 +214,9 @@ describe('importAccounts Sagas', () => {
212214
expect(err).toEqual(new Error('Dummy error'))
213215
}
214216
})
215-
.withState({ network: {} })
217+
.withState({
218+
network: { chainContext: '0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76' },
219+
})
216220
.provide([
217221
[matchers.call.fn(TransportWebUSB.isSupported), true],
218222
[matchers.call.fn(TransportWebUSB.create), mockTransport],

src/app/state/importaccounts/saga.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { all, call, delay, fork, put, select, takeEvery } from 'typed-redux-saga
88
import { ErrorPayload, WalletError, WalletErrors } from 'types/errors'
99
import { LedgerWalletType, WalletType } from 'app/state/wallet/types'
1010
import { importAccountsActions } from '.'
11-
import { selectChainContext } from '../network/selectors'
1211
import { ImportAccountsListAccount, ImportAccountsStep } from './types'
1312
import type Transport from '@ledgerhq/hw-transport'
1413
import {
@@ -22,6 +21,7 @@ import {
2221
import { getAccountBalanceWithFallback } from '../../lib/getAccountBalanceWithFallback'
2322
import BleTransport from '@oasisprotocol/ionic-ledger-hw-transport-ble/lib'
2423
import { ScanResult } from '@capacitor-community/bluetooth-le'
24+
import { getChainContext } from '../network/saga'
2525

2626
function* setStep(step: ImportAccountsStep) {
2727
yield* put(importAccountsActions.setStep(step))
@@ -233,7 +233,7 @@ export function* sign<T>(signer: LedgerSigner, tw: oasis.consensus.TransactionWr
233233
} else {
234234
transport = yield* getUSBTransport()
235235
}
236-
const chainContext = yield* select(selectChainContext)
236+
const chainContext = yield* call(getChainContext)
237237

238238
signer.setTransport(transport)
239239
try {

src/app/state/network/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ export const networkSlice = createSlice({
1515
initialState,
1616
reducers: {
1717
initializeNetwork() {},
18-
selectNetwork(state, action: PayloadAction<NetworkType>) {},
18+
selectNetwork(state, action: PayloadAction<NetworkType>) {
19+
state.chainContext = initialState.chainContext
20+
},
1921
initialNetworkSelected(state, action: PayloadAction<NetworkState>) {
2022
Object.assign(state, action.payload)
2123
},
2224
networkSelected(state, action: PayloadAction<NetworkState>) {
23-
Object.assign(state, action.payload)
25+
Object.assign(state, action.payload, { chainContext: initialState.chainContext })
26+
},
27+
setChainContext(state, action: PayloadAction<string>) {
28+
state.chainContext = action.payload
2429
},
2530
},
2631
})

src/app/state/network/saga.test.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { testSaga } from 'redux-saga-test-plan'
1+
import { expectSaga, testSaga } from 'redux-saga-test-plan'
2+
import * as matchers from 'redux-saga-test-plan/matchers'
3+
import { getChainContext, getOasisNic, networkSaga, selectNetwork } from './saga'
24
import { networkActions } from '.'
3-
import { networkSaga, selectNetwork } from './saga'
45

56
describe('Network Sagas', () => {
67
const env = process.env
@@ -22,4 +23,40 @@ describe('Network Sagas', () => {
2223
.next()
2324
.isDone()
2425
})
26+
27+
describe('getChainContext', () => {
28+
const mockChainContext = '0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76'
29+
const mockSelectedNetwork = 'testnet'
30+
const mockNic = {
31+
consensusGetChainContext: jest.fn().mockResolvedValue(mockChainContext),
32+
}
33+
34+
it('should return existing chainContext if available', () => {
35+
return expectSaga(getChainContext)
36+
.withState({
37+
network: {
38+
chainContext: mockChainContext,
39+
selectedNetwork: mockSelectedNetwork,
40+
},
41+
})
42+
.returns(mockChainContext)
43+
.run()
44+
})
45+
46+
it('should fetch and return chainContext when not present in state', () => {
47+
return expectSaga(getChainContext)
48+
.withState({
49+
network: {
50+
selectedNetwork: mockSelectedNetwork,
51+
},
52+
})
53+
.provide([
54+
[matchers.call.fn(getOasisNic), mockNic],
55+
[matchers.call.fn(mockNic.consensusGetChainContext), mockChainContext],
56+
])
57+
.put(networkActions.setChainContext(mockChainContext))
58+
.returns(mockChainContext)
59+
.run()
60+
})
61+
})
2562
})

src/app/state/network/saga.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { backend, backendApi } from 'vendors/backend'
88

99
import { networkActions } from '.'
1010
import { SyncedRootState } from '../persist/types'
11-
import { selectSelectedNetwork } from './selectors'
11+
import { selectChainContext, selectSelectedNetwork } from './selectors'
1212
import { NetworkType } from './types'
13+
import { WalletError, WalletErrors } from 'types/errors'
1314

1415
/**
1516
* Return a nic client for the specified network,
@@ -32,6 +33,23 @@ export function* getExplorerAPIs() {
3233
return backendApi(url)
3334
}
3435

36+
export function* getChainContext() {
37+
const chainContext = yield* select(selectChainContext)
38+
if (chainContext) {
39+
return chainContext
40+
}
41+
42+
try {
43+
const selectedNetwork = yield* select(selectSelectedNetwork)
44+
const nic = yield* call(getOasisNic, selectedNetwork)
45+
const fetchedChainContext = yield* call([nic, nic.consensusGetChainContext])
46+
yield* put(networkActions.setChainContext(fetchedChainContext))
47+
return fetchedChainContext
48+
} catch (error) {
49+
throw new WalletError(WalletErrors.UnknownGrpcError, 'Could not fetch data')
50+
}
51+
}
52+
3553
export function* selectNetwork({
3654
network,
3755
isInitializing,
@@ -40,12 +58,10 @@ export function* selectNetwork({
4058
isInitializing: boolean
4159
}) {
4260
const nic = yield* call(getOasisNic, network)
43-
const { epoch, chainContext } = yield* all({
61+
const { epoch } = yield* all({
4462
epoch: call([nic, nic.beaconGetEpoch], oasis.consensus.HEIGHT_LATEST),
45-
chainContext: call([nic, nic.consensusGetChainContext]),
4663
})
4764
const networkState = {
48-
chainContext: chainContext,
4965
ticker: config[network].ticker,
5066
epoch: Number(epoch), // Will lose precision in a few billion years at 1 epoch per hour
5167
selectedNetwork: network,

src/app/state/network/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface NetworkState {
99
ticker: string
1010

1111
/** chainContext / Genesis Hash */
12-
chainContext: string
12+
chainContext?: string
1313

1414
/** Current epoch */
1515
epoch: number

src/app/state/persist/syncTabs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const whitelistTabSyncActions: Record<AllActions, boolean> = {
6161
[rootSlices.wallet.actions.walletOpened.type]: true,
6262
[rootSlices.wallet.actions.updateBalance.type]: true,
6363
[rootSlices.network.actions.networkSelected.type]: true,
64+
[rootSlices.network.actions.setChainContext.type]: true,
6465
[rootSlices.persist.actions.setUnlockedRootState.type]: true,
6566
[rootSlices.persist.actions.resetRootState.type]: true,
6667
[rootSlices.persist.actions.skipUnlocking.type]: true,

src/app/state/transaction/saga.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const makeState = (wallet: Partial<Wallet>, rootState: DeepPartialRootState = {}
1919
wallets: { [wallet.address!]: wallet },
2020
selectedWallet: wallet.address,
2121
},
22+
network: {
23+
selectedNetwork: 'testnet',
24+
chainContext: '0b91b8e4e44b2003a7c5e23ddadb5e14ef5345c0ebcb3ddcae07fa2f244cab76',
25+
},
2226
...rootState,
2327
}
2428
}

0 commit comments

Comments
 (0)