Skip to content

Commit

Permalink
Merge pull request #1399 from LiskHQ/1398-fix-custom-node-login-race-…
Browse files Browse the repository at this point in the history
…condition

Prevent random fail of login to custom node - Closes #1398
  • Loading branch information
slaweet authored Oct 26, 2018
2 parents 329cb73 + e179fea commit b7ccc91
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions src/actions/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,43 @@ const peerSet = (data, config) => ({
type: actionTypes.activePeerSet,
});

const login = (dispatch, getState, data, config) => {
if (data.passphrase) {
const store = getState();
const { lockDuration } = accountConfig;
const { passphrase } = data;
const { code } = data.network;
const publicKey = passphrase ? extractPublicKey(passphrase) : data.publicKey;
const activePeer = store.peers.data ||
new Lisk.APIClient(config.nodes, { nethash: config.nethash });
const address = extractAddress(publicKey);
const accountBasics = {
passphrase,
publicKey,
address,
network: code || 0,
peerAddress: data.network.nodes[0],
};

dispatch(accountLoading());

// redirect to main/transactions
getAccount(activePeer, address).then((accountData) => {
const duration = (passphrase && store.settings.autoLog) ?
Date.now() + lockDuration : 0;
const accountUpdated = {
...accountData,
...accountBasics,
expireTime: duration,
};
dispatch(accountLoggedIn(accountUpdated));
}).catch(() => {
dispatch(errorToastDisplayed({ label: i18next.t('Unable to connect to the node') }));
dispatch(accountLoggedOut());
});
}
};

/**
* Returns required action object to set
* the given peer data as active peer
Expand Down Expand Up @@ -49,47 +86,14 @@ export const activePeerSet = data =>
dispatch(loadingFinished('getConstants'));
config.nethash = response.data.nethash;
dispatch(peerSet(data, config));
login(dispatch, getState, data, config);
}).catch(() => {
dispatch(loadingFinished('getConstants'));
dispatch(errorToastDisplayed({ label: i18next.t('Unable to connect to the node') }));
});
} else {
dispatch(peerSet(data, config));
}

if (data.passphrase) {
const store = getState();
const { lockDuration } = accountConfig;
const { passphrase } = data;
const { code } = data.network;
const publicKey = passphrase ? extractPublicKey(passphrase) : data.publicKey;
const activePeer = store.peers.data ||
new Lisk.APIClient(config.nodes, { nethash: config.nethash });
const address = extractAddress(publicKey);
const accountBasics = {
passphrase,
publicKey,
address,
network: code || 0,
peerAddress: data.network.nodes[0],
};

dispatch(accountLoading());

// redirect to main/transactions
getAccount(activePeer, address).then((accountData) => {
const duration = (passphrase && store.settings.autoLog) ?
Date.now() + lockDuration : 0;
const accountUpdated = {
...accountData,
...accountBasics,
expireTime: duration,
};
dispatch(accountLoggedIn(accountUpdated));
}).catch(() => {
dispatch(errorToastDisplayed({ label: i18next.t('Unable to connect to the node') }));
dispatch(accountLoggedOut());
});
login(dispatch, getState, data, config);
}
};

Expand Down

0 comments on commit b7ccc91

Please sign in to comment.