mirror of
https://github.com/trezor/trezor-wallet
synced 2025-01-12 00:50:58 +00:00
Merge pull request #615 from trezor/fix/backend-reconnection
Fix/backend reconnection
This commit is contained in:
commit
9844d2cc21
@ -128,8 +128,8 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"explorer": {
|
"explorer": {
|
||||||
"tx": "https://sisyfos.trezor.io/ripple-testnet-explorer/tx/",
|
"tx": "https://test.bithomp.com/explorer/",
|
||||||
"address": "https://sisyfos.trezor.io/ripple-testnet-explorer/address/"
|
"address": "https://test.bithomp.com/explorer/"
|
||||||
},
|
},
|
||||||
"hasSignVerify": false
|
"hasSignVerify": false
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
|
|
||||||
import * as BLOCKCHAIN from 'actions/constants/blockchain';
|
import * as BLOCKCHAIN from 'actions/constants/blockchain';
|
||||||
import * as DiscoveryActions from 'actions/DiscoveryActions';
|
|
||||||
import * as EthereumBlockchainActions from 'actions/ethereum/BlockchainActions';
|
import * as EthereumBlockchainActions from 'actions/ethereum/BlockchainActions';
|
||||||
import * as RippleBlockchainActions from 'actions/ripple/BlockchainActions';
|
import * as RippleBlockchainActions from 'actions/ripple/BlockchainActions';
|
||||||
|
import { resolveAfter } from 'utils/promiseUtils';
|
||||||
|
|
||||||
import type { Dispatch, GetState, PromiseAction, BlockchainFeeLevel } from 'flowtype';
|
import type { Dispatch, GetState, PromiseAction, BlockchainFeeLevel } from 'flowtype';
|
||||||
import type { BlockchainBlock, BlockchainNotification, BlockchainError } from 'trezor-connect';
|
import type { BlockchainBlock, BlockchainNotification, BlockchainError } from 'trezor-connect';
|
||||||
@ -20,10 +20,6 @@ export type BlockchainAction =
|
|||||||
| {
|
| {
|
||||||
type: typeof BLOCKCHAIN.START_SUBSCRIBE,
|
type: typeof BLOCKCHAIN.START_SUBSCRIBE,
|
||||||
shortcut: string,
|
shortcut: string,
|
||||||
}
|
|
||||||
| {
|
|
||||||
type: typeof BLOCKCHAIN.FAIL_SUBSCRIBE,
|
|
||||||
shortcut: string,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Conditionally subscribe to blockchain backend
|
// Conditionally subscribe to blockchain backend
|
||||||
@ -153,23 +149,13 @@ const autoReconnect = (shortcut: string): PromiseAction<void> => async (
|
|||||||
dispatch: Dispatch,
|
dispatch: Dispatch,
|
||||||
getState: GetState
|
getState: GetState
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
const MAX_ATTEMPTS = 4;
|
|
||||||
let blockchain = getState().blockchain.find(b => b.shortcut === shortcut);
|
let blockchain = getState().blockchain.find(b => b.shortcut === shortcut);
|
||||||
// try to automatically reconnect and wait after each attemp (5s * #attempt) untill max number of attemps is reached
|
if (!blockchain || blockchain.reconnectionAttempts >= 5) return;
|
||||||
for (let i = 0; i < MAX_ATTEMPTS; i++) {
|
|
||||||
const waitTime = 5000 * (i + 1); /// 5s * #attempt
|
|
||||||
if (!blockchain || blockchain.connected) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
blockchain = getState().blockchain.find(b => b.shortcut === shortcut);
|
await resolveAfter(5000 * (blockchain.reconnectionAttempts + 1));
|
||||||
|
|
||||||
// reconnect with 7s timeout
|
blockchain = getState().blockchain.find(b => b.shortcut === shortcut);
|
||||||
// eslint-disable-next-line no-await-in-loop
|
if (!blockchain || blockchain.connected || blockchain.connecting) return;
|
||||||
await dispatch(DiscoveryActions.reconnect(shortcut, 7000));
|
|
||||||
|
|
||||||
// wait before next try
|
await dispatch(subscribe(shortcut));
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
await new Promise(resolve => setTimeout(resolve, waitTime));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { FormattedMessage } from 'react-intl';
|
import { FormattedMessage } from 'react-intl';
|
||||||
import TrezorConnect, { UI } from 'trezor-connect';
|
import TrezorConnect, { UI } from 'trezor-connect';
|
||||||
import * as BLOCKCHAIN_ACTION from 'actions/constants/blockchain';
|
|
||||||
import * as DISCOVERY from 'actions/constants/discovery';
|
import * as DISCOVERY from 'actions/constants/discovery';
|
||||||
import * as ACCOUNT from 'actions/constants/account';
|
import * as ACCOUNT from 'actions/constants/account';
|
||||||
import * as NOTIFICATION from 'actions/constants/notification';
|
import * as NOTIFICATION from 'actions/constants/notification';
|
||||||
@ -10,7 +9,6 @@ import * as NOTIFICATION from 'actions/constants/notification';
|
|||||||
import type {
|
import type {
|
||||||
ThunkAction,
|
ThunkAction,
|
||||||
AsyncAction,
|
AsyncAction,
|
||||||
PromiseAction,
|
|
||||||
PayloadAction,
|
PayloadAction,
|
||||||
GetState,
|
GetState,
|
||||||
Dispatch,
|
Dispatch,
|
||||||
@ -331,30 +329,7 @@ const finish = (device: TrezorDevice, discoveryProcess: Discovery): AsyncAction
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const reconnect = (network: string, timeout: number = 30): PromiseAction<void> => async (
|
// Called after DEVICE.CONNECT ('trezor-connect') or CONNECT.AUTH_DEVICE or BLOCKCHAIN.CONNECT actions in WalletService
|
||||||
dispatch: Dispatch
|
|
||||||
): Promise<void> => {
|
|
||||||
// Runs two promises.
|
|
||||||
// First promise is a subscribe action which will never resolve in case of completely lost connection to the backend
|
|
||||||
// That's why there is a second promise that rejects after the specified timeout.
|
|
||||||
return Promise.race([
|
|
||||||
dispatch(BlockchainActions.subscribe(network)),
|
|
||||||
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout)),
|
|
||||||
])
|
|
||||||
.catch(() => {
|
|
||||||
// catch error from first promises that rejects (most likely timeout)
|
|
||||||
dispatch({
|
|
||||||
type: BLOCKCHAIN_ACTION.FAIL_SUBSCRIBE,
|
|
||||||
shortcut: network,
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
// dispatch restore when subscribe promise resolves
|
|
||||||
dispatch(restore());
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// Called after DEVICE.CONNECT ('trezor-connect') or CONNECT.AUTH_DEVICE actions in WalletService
|
|
||||||
// OR after BlockchainSubscribe in this.reconnect
|
// OR after BlockchainSubscribe in this.reconnect
|
||||||
export const restore = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
export const restore = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
||||||
// check if current url has "network" parameter
|
// check if current url has "network" parameter
|
||||||
|
@ -160,7 +160,9 @@ export const init = (): AsyncAction => async (
|
|||||||
if (buildUtils.isDev()) {
|
if (buildUtils.isDev()) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
window.__TREZOR_CONNECT_SRC =
|
window.__TREZOR_CONNECT_SRC =
|
||||||
typeof LOCAL === 'string' ? LOCAL : 'https://sisyfos.sldev.cz/connect/'; // eslint-disable-line no-underscore-dangle
|
typeof LOCAL === 'string'
|
||||||
|
? LOCAL
|
||||||
|
: 'https://connect.corp.sldev.cz/fix/v7-ripple-lib-error/'; // eslint-disable-line no-underscore-dangle
|
||||||
// window.__TREZOR_CONNECT_SRC = typeof LOCAL === 'string' ? LOCAL : 'https://localhost:8088/'; // eslint-disable-line no-underscore-dangle
|
// window.__TREZOR_CONNECT_SRC = typeof LOCAL === 'string' ? LOCAL : 'https://localhost:8088/'; // eslint-disable-line no-underscore-dangle
|
||||||
window.TrezorConnect = TrezorConnect;
|
window.TrezorConnect = TrezorConnect;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
|
|
||||||
export const START_SUBSCRIBE: 'blockchain__start_subscribe' = 'blockchain__start_subscribe';
|
export const START_SUBSCRIBE: 'blockchain__start_subscribe' = 'blockchain__start_subscribe';
|
||||||
export const FAIL_SUBSCRIBE: 'blockchain__fail_subscribe' = 'blockchain__fail_subscribe';
|
|
||||||
export const READY: 'blockchain__ready' = 'blockchain__ready';
|
export const READY: 'blockchain__ready' = 'blockchain__ready';
|
||||||
export const UPDATE_FEE: 'blockchain__update_fee' = 'blockchain__update_fee';
|
export const UPDATE_FEE: 'blockchain__update_fee' = 'blockchain__update_fee';
|
||||||
|
@ -161,31 +161,47 @@ export const onNotification = (
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedAccount = await TrezorConnect.rippleGetAccountInfo({
|
// In case of tx sent between two Trezor accounts there is a possibility that only 1 notification will be received
|
||||||
account: {
|
// therefore we need to find target account and update data for it as well
|
||||||
descriptor: account.descriptor,
|
const accountsToUpdate = [account];
|
||||||
from: account.block,
|
const targetAddress =
|
||||||
history: false,
|
notification.type === 'send'
|
||||||
},
|
? notification.outputs[0].addresses[0]
|
||||||
coin: account.network,
|
: notification.inputs[0].addresses[0];
|
||||||
|
|
||||||
|
const targetAccount = getState().accounts.find(a => a.descriptor === targetAddress);
|
||||||
|
if (targetAccount) {
|
||||||
|
accountsToUpdate.push(targetAccount);
|
||||||
|
}
|
||||||
|
|
||||||
|
accountsToUpdate.forEach(async a => {
|
||||||
|
const response = await TrezorConnect.rippleGetAccountInfo({
|
||||||
|
account: {
|
||||||
|
descriptor: a.descriptor,
|
||||||
|
from: a.block,
|
||||||
|
history: false,
|
||||||
|
},
|
||||||
|
coin: a.network,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.success) {
|
||||||
|
const updatedAccount = response.payload;
|
||||||
|
const empty = updatedAccount.sequence <= 0 && updatedAccount.balance === '0';
|
||||||
|
dispatch(
|
||||||
|
AccountsActions.update({
|
||||||
|
networkType: 'ripple',
|
||||||
|
...a,
|
||||||
|
balance: toDecimalAmount(updatedAccount.balance, network.decimals),
|
||||||
|
availableBalance: toDecimalAmount(
|
||||||
|
updatedAccount.availableBalance,
|
||||||
|
network.decimals
|
||||||
|
),
|
||||||
|
block: updatedAccount.block,
|
||||||
|
sequence: updatedAccount.sequence,
|
||||||
|
reserve: toDecimalAmount(updatedAccount.reserve, network.decimals),
|
||||||
|
empty,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!updatedAccount.success) return;
|
|
||||||
|
|
||||||
const empty = updatedAccount.payload.sequence <= 0 && updatedAccount.payload.balance === '0';
|
|
||||||
dispatch(
|
|
||||||
AccountsActions.update({
|
|
||||||
networkType: 'ripple',
|
|
||||||
...account,
|
|
||||||
balance: toDecimalAmount(updatedAccount.payload.balance, network.decimals),
|
|
||||||
availableBalance: toDecimalAmount(
|
|
||||||
updatedAccount.payload.availableBalance,
|
|
||||||
network.decimals
|
|
||||||
),
|
|
||||||
block: updatedAccount.payload.block,
|
|
||||||
sequence: updatedAccount.payload.sequence,
|
|
||||||
reserve: '0',
|
|
||||||
empty,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
@ -14,17 +14,21 @@ export default (props: Props) => {
|
|||||||
if (notification.type === 'backend') {
|
if (notification.type === 'backend') {
|
||||||
// special case: backend is down
|
// special case: backend is down
|
||||||
// TODO: this is a different component with "auto resolve" button
|
// TODO: this is a different component with "auto resolve" button
|
||||||
|
const inProgress = blockchain && blockchain.connecting;
|
||||||
|
const status = inProgress
|
||||||
|
? l10nMessages.TR_RECONNECTING
|
||||||
|
: l10nMessages.TR_CONNECT_TO_BACKEND;
|
||||||
return (
|
return (
|
||||||
<Notification
|
<Notification
|
||||||
variant="error"
|
variant="error"
|
||||||
title={notification.title}
|
title={notification.title}
|
||||||
message={notification.message}
|
message={notification.message}
|
||||||
isActionInProgress={blockchain && blockchain.connecting}
|
isActionInProgress={inProgress}
|
||||||
actions={[
|
actions={[
|
||||||
{
|
{
|
||||||
label: props.intl.formatMessage(l10nMessages.TR_CONNECT_TO_BACKEND),
|
label: props.intl.formatMessage(status),
|
||||||
callback: async () => {
|
callback: async () => {
|
||||||
await props.blockchainReconnect(network.shortcut);
|
if (!inProgress) props.blockchainReconnect(network.shortcut);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
|
@ -7,6 +7,10 @@ const definedMessages: Messages = defineMessages({
|
|||||||
id: 'TR_CONNECT_TO_BACKEND',
|
id: 'TR_CONNECT_TO_BACKEND',
|
||||||
defaultMessage: 'Connect',
|
defaultMessage: 'Connect',
|
||||||
},
|
},
|
||||||
|
TR_RECONNECTING: {
|
||||||
|
id: 'TR_RECONNECTING',
|
||||||
|
defaultMessage: 'Reconnecting...',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default definedMessages;
|
export default definedMessages;
|
||||||
|
@ -7,7 +7,7 @@ import type { IntlShape } from 'react-intl';
|
|||||||
|
|
||||||
import type { State, Dispatch } from 'flowtype';
|
import type { State, Dispatch } from 'flowtype';
|
||||||
|
|
||||||
import { reconnect } from 'actions/DiscoveryActions';
|
import { subscribe } from 'actions/BlockchainActions';
|
||||||
import * as NotificationActions from 'actions/NotificationActions';
|
import * as NotificationActions from 'actions/NotificationActions';
|
||||||
|
|
||||||
import StaticNotifications from './components/Static';
|
import StaticNotifications from './components/Static';
|
||||||
@ -29,7 +29,7 @@ export type StateProps = {|
|
|||||||
|
|
||||||
export type DispatchProps = {|
|
export type DispatchProps = {|
|
||||||
close: typeof NotificationActions.close,
|
close: typeof NotificationActions.close,
|
||||||
blockchainReconnect: typeof reconnect,
|
blockchainReconnect: typeof subscribe,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
export type Props = {| ...OwnProps, ...StateProps, ...DispatchProps |};
|
export type Props = {| ...OwnProps, ...StateProps, ...DispatchProps |};
|
||||||
@ -52,7 +52,7 @@ const mapStateToProps = (state: State): StateProps => ({
|
|||||||
|
|
||||||
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
|
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
|
||||||
close: bindActionCreators(NotificationActions.close, dispatch),
|
close: bindActionCreators(NotificationActions.close, dispatch),
|
||||||
blockchainReconnect: bindActionCreators(reconnect, dispatch),
|
blockchainReconnect: bindActionCreators(subscribe, dispatch),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default injectIntl<OwnProps>(
|
export default injectIntl<OwnProps>(
|
||||||
|
@ -17,6 +17,7 @@ export type BlockchainNetwork = {
|
|||||||
feeLevels: Array<BlockchainFeeLevel>,
|
feeLevels: Array<BlockchainFeeLevel>,
|
||||||
connected: boolean,
|
connected: boolean,
|
||||||
connecting: boolean,
|
connecting: boolean,
|
||||||
|
reconnectionAttempts: number,
|
||||||
block: number,
|
block: number,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -41,30 +42,7 @@ const onStartSubscribe = (state: State, shortcut: string): State => {
|
|||||||
shortcut,
|
shortcut,
|
||||||
connected: false,
|
connected: false,
|
||||||
connecting: true,
|
connecting: true,
|
||||||
block: 0,
|
reconnectionAttempts: 0,
|
||||||
feeTimestamp: 0,
|
|
||||||
feeLevels: [],
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
|
|
||||||
const onFailSubscribe = (state: State, shortcut: string): State => {
|
|
||||||
const network = state.find(b => b.shortcut === shortcut);
|
|
||||||
if (network) {
|
|
||||||
const others = state.filter(b => b !== network);
|
|
||||||
return others.concat([
|
|
||||||
{
|
|
||||||
...network,
|
|
||||||
connecting: false,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return state.concat([
|
|
||||||
{
|
|
||||||
shortcut,
|
|
||||||
connected: false,
|
|
||||||
connecting: false,
|
|
||||||
block: 0,
|
block: 0,
|
||||||
feeTimestamp: 0,
|
feeTimestamp: 0,
|
||||||
feeLevels: [],
|
feeLevels: [],
|
||||||
@ -84,6 +62,7 @@ const onConnect = (state: State, action: BlockchainConnect): State => {
|
|||||||
block: info.block,
|
block: info.block,
|
||||||
connected: true,
|
connected: true,
|
||||||
connecting: false,
|
connecting: false,
|
||||||
|
reconnectionAttempts: 0,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@ -93,6 +72,7 @@ const onConnect = (state: State, action: BlockchainConnect): State => {
|
|||||||
shortcut,
|
shortcut,
|
||||||
connected: true,
|
connected: true,
|
||||||
connecting: false,
|
connecting: false,
|
||||||
|
reconnectionAttempts: 0,
|
||||||
block: info.block,
|
block: info.block,
|
||||||
feeTimestamp: 0,
|
feeTimestamp: 0,
|
||||||
feeLevels: [],
|
feeLevels: [],
|
||||||
@ -110,6 +90,7 @@ const onError = (state: State, action: BlockchainError): State => {
|
|||||||
...network,
|
...network,
|
||||||
connected: false,
|
connected: false,
|
||||||
connecting: false,
|
connecting: false,
|
||||||
|
reconnectionAttempts: network.reconnectionAttempts + 1,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@ -119,6 +100,7 @@ const onError = (state: State, action: BlockchainError): State => {
|
|||||||
shortcut,
|
shortcut,
|
||||||
connected: false,
|
connected: false,
|
||||||
connecting: false,
|
connecting: false,
|
||||||
|
reconnectionAttempts: 0,
|
||||||
block: 0,
|
block: 0,
|
||||||
feeTimestamp: 0,
|
feeTimestamp: 0,
|
||||||
feeLevels: [],
|
feeLevels: [],
|
||||||
@ -160,8 +142,6 @@ export default (state: State = initialState, action: Action): State => {
|
|||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case BLOCKCHAIN_ACTION.START_SUBSCRIBE:
|
case BLOCKCHAIN_ACTION.START_SUBSCRIBE:
|
||||||
return onStartSubscribe(state, action.shortcut);
|
return onStartSubscribe(state, action.shortcut);
|
||||||
case BLOCKCHAIN_ACTION.FAIL_SUBSCRIBE:
|
|
||||||
return onFailSubscribe(state, action.shortcut);
|
|
||||||
case BLOCKCHAIN_EVENT.CONNECT:
|
case BLOCKCHAIN_EVENT.CONNECT:
|
||||||
return onConnect(state, action);
|
return onConnect(state, action);
|
||||||
case BLOCKCHAIN_EVENT.ERROR:
|
case BLOCKCHAIN_EVENT.ERROR:
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
import { DEVICE } from 'trezor-connect';
|
import { DEVICE, BLOCKCHAIN } from 'trezor-connect';
|
||||||
import { LOCATION_CHANGE } from 'connected-react-router';
|
import { LOCATION_CHANGE } from 'connected-react-router';
|
||||||
import * as WALLET from 'actions/constants/wallet';
|
import * as WALLET from 'actions/constants/wallet';
|
||||||
import * as CONNECT from 'actions/constants/TrezorConnect';
|
import * as CONNECT from 'actions/constants/TrezorConnect';
|
||||||
@ -132,6 +132,12 @@ const WalletService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa
|
|||||||
api.dispatch(DiscoveryActions.stop());
|
api.dispatch(DiscoveryActions.stop());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// try to restore discovery on BLOCKCHAIN.CONNECT event
|
||||||
|
// edge case when backend throws error during discovery
|
||||||
|
if (action.type === BLOCKCHAIN.CONNECT) {
|
||||||
|
api.dispatch(DiscoveryActions.restore());
|
||||||
|
}
|
||||||
|
|
||||||
return action;
|
return action;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -489,14 +489,13 @@ const AccountSend = (props: Props) => {
|
|||||||
</AdvancedForm>
|
</AdvancedForm>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{props.selectedAccount.pending.length > 0 ||
|
{props.selectedAccount.pending.length > 0 && (
|
||||||
(account.imported && (
|
<PendingTransactions
|
||||||
<PendingTransactions
|
pending={props.selectedAccount.pending}
|
||||||
pending={props.selectedAccount.pending}
|
tokens={props.selectedAccount.tokens}
|
||||||
tokens={props.selectedAccount.tokens}
|
network={network}
|
||||||
network={network}
|
/>
|
||||||
/>
|
)}
|
||||||
))}
|
|
||||||
</Content>
|
</Content>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user