-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
222 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { Code, ConnectError, createRouterTransport } from '@connectrpc/connect'; | ||
import { TendermintProxyService } from '@penumbra-zone/protobuf/penumbra/util/tendermint_proxy/v1/tendermint_proxy_connect'; | ||
import { fetchBlockHeightWithFallback } from './latest-block-height'; | ||
import { GetStatusResponse } from '@penumbra-zone/protobuf/penumbra/util/tendermint_proxy/v1/tendermint_proxy_pb'; | ||
|
||
const endpoints = ['rpc1.example.com', 'rpc2.example.com', 'rpc3.example.com']; | ||
|
||
const getMock = (fn: () => GetStatusResponse) => { | ||
return createRouterTransport(router => { | ||
router.service(TendermintProxyService, { | ||
getStatus() { | ||
return fn(); | ||
}, | ||
}); | ||
}); | ||
}; | ||
|
||
describe('fetchBlockHeightWithFallback', () => { | ||
it('should fetch block height successfully from the first endpoint', async () => { | ||
const mockTransport = getMock( | ||
() => new GetStatusResponse({ syncInfo: { latestBlockHeight: 800n } }), | ||
); | ||
const result = await fetchBlockHeightWithFallback(endpoints, mockTransport); | ||
expect(result.blockHeight).toEqual(800); | ||
expect(endpoints.includes(result.rpc)).toBeTruthy(); | ||
}); | ||
|
||
it('should fallback to the second endpoint if the first fails', async () => { | ||
let called = false; | ||
const mockTransport = getMock(() => { | ||
if (!called) { | ||
called = true; | ||
throw new ConnectError('Error calling service', Code.Unknown); | ||
} | ||
return new GetStatusResponse({ syncInfo: { latestBlockHeight: 800n } }); | ||
}); | ||
const result = await fetchBlockHeightWithFallback(endpoints, mockTransport); | ||
expect(result.blockHeight).toEqual(800); | ||
expect(endpoints.includes(result.rpc)).toBeTruthy(); | ||
expect(called).toBeTruthy(); | ||
}); | ||
|
||
it('should fallback through all endpoints and throw an error if all fail', async () => { | ||
let timesCalled = 0; | ||
const mockTransport = getMock(() => { | ||
timesCalled++; | ||
throw new ConnectError('Error calling service', Code.Unknown); | ||
}); | ||
await expect(() => fetchBlockHeightWithFallback(endpoints, mockTransport)).rejects.toThrow( | ||
new Error('All RPC endpoints failed to fetch the block height.'), | ||
); | ||
expect(timesCalled).toEqual(3); | ||
}); | ||
|
||
it('should throw an error immediately if the endpoints array is empty', async () => { | ||
let timesCalled = 0; | ||
const mockTransport = getMock(() => { | ||
timesCalled++; | ||
throw new ConnectError('Error calling service', Code.Unknown); | ||
}); | ||
await expect(() => fetchBlockHeightWithFallback([], mockTransport)).rejects.toThrow( | ||
new Error('All RPC endpoints failed to fetch the block height.'), | ||
); | ||
expect(timesCalled).toEqual(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
apps/extension/src/routes/page/onboarding/password/hooks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useAddWallet } from '../../../../hooks/onboarding'; | ||
import { usePageNav } from '../../../../utils/navigate'; | ||
import { FormEvent, useCallback, useState } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
import { getSeedPhraseOrigin, setOnboardingValuesInStorage } from './utils'; | ||
import { PagePath } from '../../paths'; | ||
import { localExtStorage } from '../../../../storage/local'; | ||
|
||
export const useFinalizeOnboarding = () => { | ||
const addWallet = useAddWallet(); | ||
const navigate = usePageNav(); | ||
const [error, setError] = useState<string>(); | ||
const [loading, setLoading] = useState(false); | ||
const location = useLocation(); | ||
|
||
const handleSubmit = useCallback(async (event: FormEvent, password: string) => { | ||
event.preventDefault(); | ||
try { | ||
setLoading(true); | ||
setError(undefined); | ||
await addWallet(password); | ||
const origin = getSeedPhraseOrigin(location); | ||
await setOnboardingValuesInStorage(origin); | ||
navigate(PagePath.ONBOARDING_SUCCESS); | ||
} catch (e) { | ||
setError(String(e)); | ||
// If something fails, roll back the wallet addition so it forces onboarding if they leave and click popup again | ||
await localExtStorage.remove('wallets'); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}, []); | ||
|
||
return { handleSubmit, error, loading }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export enum SEED_PHRASE_ORIGIN { | ||
IMPORTED = 'IMPORTED', | ||
NEWLY_GENERATED = 'NEWLY_GENERATED', | ||
} | ||
|
||
export interface LocationState { | ||
origin?: SEED_PHRASE_ORIGIN; | ||
} |
65 changes: 65 additions & 0 deletions
65
apps/extension/src/routes/page/onboarding/password/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Location } from 'react-router-dom'; | ||
import { LocationState, SEED_PHRASE_ORIGIN } from './types'; | ||
import { PagePath } from '../../paths'; | ||
import { usePageNav } from '../../../../utils/navigate'; | ||
import { ChainRegistryClient } from '@penumbra-labs/registry'; | ||
import { sample } from 'lodash'; | ||
import { createPromiseClient } from '@connectrpc/connect'; | ||
import { createGrpcWebTransport } from '@connectrpc/connect-web'; | ||
import { localExtStorage } from '../../../../storage/local'; | ||
import { AppService } from '@penumbra-zone/protobuf'; | ||
import { fetchBlockHeightWithFallback } from '../../../../hooks/latest-block-height'; | ||
|
||
export const getSeedPhraseOrigin = (location: Location): SEED_PHRASE_ORIGIN => { | ||
const state = location.state as Partial<LocationState> | undefined; | ||
if ( | ||
state && | ||
typeof state.origin === 'string' && | ||
Object.values(SEED_PHRASE_ORIGIN).includes(state.origin) | ||
) { | ||
return state.origin; | ||
} | ||
// Default to IMPORTED if the origin is not valid as it won't generate a walletCreationHeight | ||
return SEED_PHRASE_ORIGIN.IMPORTED; | ||
}; | ||
|
||
export const navigateToPasswordPage = ( | ||
nav: ReturnType<typeof usePageNav>, | ||
origin: SEED_PHRASE_ORIGIN, | ||
) => nav(PagePath.SET_PASSWORD, { state: { origin } }); | ||
|
||
// A request-level timeout that supersedes the channel transport-level timeout to prevent hanging requests. | ||
const DEFAULT_TRANSPORT_OPTS = { timeoutMs: 5000 }; | ||
|
||
export const setOnboardingValuesInStorage = async (seedPhraseOrigin: SEED_PHRASE_ORIGIN) => { | ||
const chainRegistryClient = new ChainRegistryClient(); | ||
const { rpcs, frontends } = await chainRegistryClient.remote.globals(); | ||
const randomFrontend = sample(frontends); | ||
if (!randomFrontend) { | ||
throw new Error('Registry missing frontends'); | ||
} | ||
|
||
// Queries for blockHeight regardless of SEED_PHRASE_ORIGIN as a means of testing endpoint for liveness | ||
const { blockHeight, rpc } = await fetchBlockHeightWithFallback(rpcs.map(r => r.url)); | ||
|
||
const { appParameters } = await createPromiseClient( | ||
AppService, | ||
createGrpcWebTransport({ baseUrl: rpc }), | ||
).appParameters({}, DEFAULT_TRANSPORT_OPTS); | ||
if (!appParameters?.chainId) { | ||
throw new Error('No chain id'); | ||
} | ||
|
||
if (seedPhraseOrigin === SEED_PHRASE_ORIGIN.NEWLY_GENERATED) { | ||
await localExtStorage.set('walletCreationBlockHeight', blockHeight); | ||
} | ||
|
||
const { numeraires } = await chainRegistryClient.remote.get(appParameters.chainId); | ||
|
||
await localExtStorage.set('grpcEndpoint', rpc); | ||
await localExtStorage.set('frontendUrl', randomFrontend.url); | ||
await localExtStorage.set( | ||
'numeraires', | ||
numeraires.map(n => n.toJsonString()), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters