Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump: v0.27.0-rc.2 #447

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const types = {
NETWORKSETTINGS_UPDATE_INVALID: 'NETWORKSETTINGS_UPDATE_INVALID',
/* It indicates the update request has failed. */
NETWORKSETTINGS_UPDATE_FAILURE: 'NETWORK_SETTINGS_UPDATE_FAILURE',
/* It updates the redux state of network settings status */
/* It updates the redux state of network settings status. */
NETWORKSETTINGS_UPDATE_READY: 'NETWORK_SETTINGS_UPDATE_READY',
};

Expand Down
10 changes: 10 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export const WALLET_SERVICE_FEATURE_TOGGLE = 'wallet-service-mobile.rollout';
export const PUSH_NOTIFICATION_FEATURE_TOGGLE = 'push-notification.rollout';
export const WALLET_CONNECT_FEATURE_TOGGLE = 'wallet-connect-mobile.rollout';
export const NETWORK_SETTINGS_FEATURE_TOGGLE = 'network-settings.rollout';
export const NANO_CONTRACT_FEATURE_TOGGLE = 'nano-contract.rollout';

/**
* Default feature toggle values.
Expand All @@ -172,6 +173,7 @@ export const FEATURE_TOGGLE_DEFAULTS = {
[PUSH_NOTIFICATION_FEATURE_TOGGLE]: false,
[WALLET_CONNECT_FEATURE_TOGGLE]: false,
[NETWORK_SETTINGS_FEATURE_TOGGLE]: false,
[NANO_CONTRACT_FEATURE_TOGGLE]: false,
};

// Project id configured in https://walletconnect.com
Expand Down Expand Up @@ -244,3 +246,11 @@ export const HTTP_REQUEST_TIMEOUT = 3000;
* Any network that is not mainnet or testnet should be a privatenet.
*/
export const NETWORK_PRIVATENET = 'privatenet';

/**
* The following constants are used on a progressive retry mechanism.
* @see `src/sagas/helper.js@progressiveRetryRequest`
*/
export const MAX_RETRIES = 8;
export const INITIAL_RETRY_LATENCY = 300; // ms
export const LATENCY_MULTIPLIER = 30; // multiplier per iteration
54 changes: 54 additions & 0 deletions src/sagas/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
take,
call,
select,
delay,
} from 'redux-saga/effects';
import { t } from 'ttag';
import axiosWrapperCreateRequestInstance from '@hathor/wallet-lib/lib/api/axiosWrapper';
Expand All @@ -28,6 +29,9 @@ import {
WALLET_SERVICE_FEATURE_TOGGLE,
WALLET_SERVICE_REQUEST_TIMEOUT,
networkSettingsKeyMap,
MAX_RETRIES,
INITIAL_RETRY_LATENCY,
LATENCY_MULTIPLIER,
} from '../constants';
import { STORE } from '../store';

Expand Down Expand Up @@ -275,3 +279,53 @@ export function getNetworkSettings(state) {
// has precedence, once it indicates a custom network.
return STORE.getItem(networkSettingsKeyMap.networkSettings) ?? state.networkSettings;
}

/**
* A request abstraction that applies a progressive retry strategy.
* One can define how many retries it should make or use the default value.
*
* @param {Promise<any>} request The async callback function to be executed.
* @param {number} maxRetries The max retries allowed, with default value.
* Notice this param should be at least 1 to make sense.
* @returns {any} A success object from the request.
* @throws An error after retries exhausted.
*
* @example
* yield call(progressiveRetryRequest, async () => asyncFn());
* // use default maxRetries
*
* @example
* yield call(progressiveRetryRequest, async () => asyncFn(), 3);
* // use custom maxRetries equal to 3
*/
export function* progressiveRetryRequest(request, maxRetries = MAX_RETRIES) {
let lastError = null;

// eslint-disable-next-line no-plusplus
for (let i = 0; i <= maxRetries; i++) {
try {
// return if success
return yield call(request)
} catch (error) {
lastError = error;
}

// skip delay for last call
if (i === maxRetries) {
continue;
}

// attempt 0: 300ms
// attempt 1: 330ms
// attempt 2: 420ms
// attempt 3: 570ms
// attempt 4: 780ms
// attempt 5: 1050ms
// attempt 6: 1380ms
// attempt 7: 1770ms
yield delay(INITIAL_RETRY_LATENCY + LATENCY_MULTIPLIER * (i * i));
}

// throw last error after retries exhausted
throw lastError;
}
4 changes: 2 additions & 2 deletions src/sagas/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export function* fetchTokenMetadata({ tokenId }) {
}
}

export function* fetchTokenData(tokenId) {
export function* fetchTokenData(tokenId, force = false) {
const fetchBalanceResponse = yield call(
dispatchAndWait,
tokenFetchBalanceRequested(tokenId),
Expand All @@ -256,7 +256,7 @@ export function* fetchTokenData(tokenId) {
);
const fetchHistoryResponse = yield call(
dispatchAndWait,
tokenFetchHistoryRequested(tokenId),
tokenFetchHistoryRequested(tokenId, force),
specificTypeAndPayload(types.TOKEN_FETCH_HISTORY_SUCCESS, {
tokenId,
}),
Expand Down
21 changes: 17 additions & 4 deletions src/sagas/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
getRegisteredTokens,
getNetworkSettings,
getRegisteredTokenUids,
progressiveRetryRequest,
} from './helpers';
import { setKeychainPin } from '../utils';

Expand Down Expand Up @@ -287,7 +288,7 @@ export function* startWallet(action) {

/**
* This saga will load both HTR and DEFAULT_TOKEN (if they are different)
* and dispatch actions to asynchronously load all registered tokens.
* and dispatch actions to asynchronously load all registered tokens forcefully.
*
* Will throw an error if the download fails for any token.
* @returns {string[]} Array of token uid
Expand All @@ -298,9 +299,12 @@ export function* loadTokens() {

// fetchTokenData will throw an error if the download failed, we should just
// let it crash as throwing an error is the default behavior for loadTokens
yield call(fetchTokenData, htrUid);
yield call(fetchTokenData, htrUid, true);

if (customTokenUid !== htrUid) {
// custom tokens doesn't need to be forced to download because its history status
// will be marked as invalidated, and history will get requested the next time a user
// enters the history screen.
yield call(fetchTokenData, customTokenUid);
}

Expand Down Expand Up @@ -485,9 +489,17 @@ export function* handleTx(action) {
return acc;
}, [{}, new Set([])],);

const txWalletAddresses = yield call(wallet.checkAddressesMine.bind(wallet), [...txAddresses]);
const tokensToDownload = [];
let txWalletAddresses = null;
try {
const request = async () => wallet.checkAddressesMine.bind(wallet)([...txAddresses]);
txWalletAddresses = yield call(progressiveRetryRequest, request);
} catch (error) {
// Emmit a fatal error feedback to user and halts tx processing.
yield put(onExceptionCaptured(error, true));
return;
}

const tokensToDownload = [];
for (const [tokenUid, addresses] of Object.entries(tokenAddressesMap)) {
for (const [address] of addresses.entries()) {
// txWalletAddresses should always have the address we requested, but we should double check
Expand Down Expand Up @@ -628,6 +640,7 @@ export function* onWalletReloadData() {
}

try {
// Here we force the download of tokens history
const registeredTokens = yield call(loadTokens);

const customTokenUid = DEFAULT_TOKEN.uid;
Expand Down
Loading