Skip to content

Commit

Permalink
Refactor config.js (#2129)
Browse files Browse the repository at this point in the history
* refactor global config variables to use constants instead of strings

* Add return to credentials when fatal error is in advanced mode

* fix lint

* review fixes
  • Loading branch information
vctt94 authored and alexlyp committed Jun 11, 2019
1 parent d325a64 commit 1688833
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 132 deletions.
12 changes: 9 additions & 3 deletions app/actions/DaemonActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { semverCompatible } from "./VersionActions";
import * as wallet from "wallet";
import { push as pushHistory, goBack } from "react-router-redux";
import { ipcRenderer } from "electron";
import { setMustOpenForm, getWalletCfg, getAppdataPath, getRemoteCredentials, getGlobalCfg, setLastHeight } from "../config";
import { setMustOpenForm, getWalletCfg, getAppdataPath, getRemoteCredentials, getGlobalCfg, setLastHeight, getDaemonIsAdvanced } from "../config";
import { isTestNet } from "selectors";
import axios from "axios";
import { STANDARD_EXTERNAL_REQUESTS } from "main_dev/externalRequests";
Expand Down Expand Up @@ -56,6 +56,7 @@ export const CONNECTDAEMON_SUCCESS = "CONNECTDAEMON_SUCCESS";
export const CONNECTDAEMON_FAILURE = "CONNECTDAEMON_FAILURE";
export const SYNC_DAEMON_ATTEMPT = "SYNC_DAEMON_ATTEMPT";
export const SYNC_DAEMON_FAILED = "SYNC_DAEMON_FAILED";
export const BACK_TO_CREDENTIALS = "BACK_TO_CREDENTIALS";

export const checkDecreditonVersion = () => (dispatch, getState) =>{
const detectedVersion = getState().daemon.appVersion;
Expand Down Expand Up @@ -157,12 +158,12 @@ export const startDaemon = (params) => (dispatch, getState) => {
dispatch({ type: DAEMONSTART_ATTEMPT });
const { daemonStarted } = getState().daemon;
if (daemonStarted) {
return dispatch({ type: DAEMONSTART_SUCCESS });
return dispatch({ type: DAEMONSTART_SUCCESS, daemonAdvanced: getDaemonIsAdvanced() });
}

wallet.startDaemon(params, isTestNet(getState()))
.then(rpcCreds => {
dispatch({ type: DAEMONSTART_SUCCESS, credentials: rpcCreds, appdata });
dispatch({ type: DAEMONSTART_SUCCESS, credentials: rpcCreds, appdata, daemonAdvanced: getDaemonIsAdvanced() });
dispatch(connectDaemon(rpcCreds, appdata));
})
.catch((err) => dispatch({ err, type: DAEMONSTART_FAILURE }));
Expand Down Expand Up @@ -192,6 +193,11 @@ export const registerForErrors = () => (dispatch) => {
});
};

export const backToCredentials = () => (dispatch) => {
dispatch({ type: BACK_TO_CREDENTIALS });
dispatch(pushHistory("/getstarted"));
};

export const deleteDaemonData = () => (dispatch, getState) => {
const { appData } = getState().daemon;
dispatch({ type: DELETE_DCRD_ATTEMPT });
Expand Down
2 changes: 1 addition & 1 deletion app/actions/SettingsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function updateStateSettingsChanged(settings, norestart) {
if (newDiffersFromTemp) {
const newDiffersFromCurrent = settingsFields
.reduce((d, f) => (d || newSettings[f] !== currentSettings[f]), false);
const needNetworkReset = !norestart && Object.keys(networkChange)
const needNetworkReset = !norestart && Object.keys(networkChange)
.reduce((d, f) => (d || newSettings[f] !== currentSettings[f]), false);
newDiffersFromCurrent
? dispatch({ tempSettings: newSettings, needNetworkReset, type: SETTINGS_CHANGED })
Expand Down
58 changes: 39 additions & 19 deletions app/components/views/FatalErrorPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,41 @@ const corruptedError = "corrupted";
const checkSumError = "checksum does not match";
class FatalErrorPage extends React.Component {

getErrorAction() {
const { daemonError, deleteDaemonData } = this.props;
let errorMessage;

switch (true) {
case (daemonError.indexOf(resourcesUnavailableError) !== -1):
errorMessage = <T id="fatal.suggestion.resources" m="This error typically means you have another instance of daemon running. You should check your taskmanager or profiler to shutdown any still running daemon and then try again." />;
break;
case (daemonError.indexOf(DIFF_CONNECTION_ERROR) !== -1):
errorMessage = <T id="fatal.suggestion.diffConnection" m="This error typically means you have the testnet flag in your dcrd.conf file. You should check your dcrd.conf file and remove the testnet=1." />;
break;
case ((daemonError.indexOf(corruptedError) !== -1 || daemonError.indexOf(checkSumError) !== -1 )):
errorMessage = (
<>
<div className="fatal-error-reset-blockchain">
<T id="fatal.suggestion.corrupted" m="This error means your blockchain data has somehow become corrupted. Typically, this is caused by a sector on the HDD/SDD that went bad and its built-in SMART didn't repair it, or the more likely case, there was a memory issue which corrupted the data. To resolve, you must delete your blockchain data and re-download. Press the button below to complete the process. When you restart Decrediton, it will automatically begin your blockchain download. Please come to our support channel on slack/matrix/discord/rocketchat to get advice about running disk utilities. " />
</div>
<RemoveDaemonButton
className="fatal-remove-button"
modalTitle={<T id="fatal.removeConfirmModal.title" m="Remove daemon data"/>}
modalContent={<T id="fatal.removeConfirmModal.content" m="Warning this action is permanent! Please make sure you want to remove your blockchain data before proceeding. Decrediton will automatically shutdown after deleting the folder. Please manually restart it afterwards."/>}
onSubmit={deleteDaemonData}
buttonLabel={ <T id="fatal.button.delete" m="Delete and Shutdown"/>}/>
</>);
break;
default:
errorMessage = <T id="fatal.suggestion.fallthrough" m="Please note the error above and go to the support channel on slack/matrix/rockchat for help resolving the issue." />;
break;
}

return errorMessage;
}

render() {
const { daemonError, walletError, shutdownApp, deleteDaemonData } = this.props;
const { daemonError, walletError, shutdownApp, isAdvancedDaemon, backToCredentials } = this.props;
return (
<div className="page-body getstarted">
<div className="fatal-error-page">
Expand All @@ -38,26 +71,13 @@ class FatalErrorPage extends React.Component {
</div>
<div className="fatal-error-title"><T id="fatal.suggestion.title" m="Suggested action to resolve error" />:</div>
<div className="fatal-error-suggestion">
{daemonError && daemonError.indexOf(resourcesUnavailableError) > 0 ?
<T id="fatal.suggestion.resources" m="This error typically means you have another instance of daemon running. You should check your taskmanager or profiler to shutdown any still running daemon and then try again." /> :
daemonError && daemonError.indexOf(DIFF_CONNECTION_ERROR) !== -1 ?
<T id="fatal.suggestion.diffConnection" m="This error typically means you have the testnet flag in your dcrd.conf file. You should check your dcrd.conf file and remove the testnet=1." /> :
daemonError && (daemonError.indexOf(corruptedError) > 0 || daemonError.indexOf(checkSumError) > 0) ?
<>
<div className="fatal-error-reset-blockchain">
<T id="fatal.suggestion.corrupted" m="This error means your blockchain data has somehow become corrupted. Typically, this is caused by a sector on the HDD/SDD that went bad and its built-in SMART didn't repair it, or the more likely case, there was a memory issue which corrupted the data. To resolve, you must delete your blockchain data and re-download. Press the button below to complete the process. When you restart Decrediton, it will automatically begin your blockchain download. Please come to our support channel on slack/matrix/discord/rocketchat to get advice about running disk utilities. " />
</div>
<RemoveDaemonButton
className="fatal-remove-button"
modalTitle={<T id="fatal.removeConfirmModal.title" m="Remove daemon data"/>}
modalContent={<T id="fatal.removeConfirmModal.content" m="Warning this action is permanent! Please make sure you want to remove your blockchain data before proceeding. Decrediton will automatically shutdown after deleting the folder. Please manually restart it afterwards."/>}
onSubmit={deleteDaemonData}
buttonLabel={ <T id="fatal.button.delete" m="Delete and Shutdown"/>}/>
</> :
<T id="fatal.suggestion.fallthrough" m="Please note the error above and go to the support channel on slack/matrix/rockchat for help resolving the issue." />
}
{daemonError && this.getErrorAction()}
</div>
<div className="fatal-error-toolbar">
{ isAdvancedDaemon &&
<KeyBlueButton onClick={backToCredentials}>
<T id="fatal.retry.connection.button" m="Return to Daemon Connection"/>
</KeyBlueButton>}
<KeyBlueButton onClick={shutdownApp}>
<T id="fatal.button" m="Close Decrediton"/>
</KeyBlueButton>
Expand Down
6 changes: 1 addition & 5 deletions app/components/views/GetStartedPage/AdvancedStartup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ class AdvancedStartupBody extends React.Component {
return;
}
const { rpc_user, rpc_pass, rpc_cert, rpc_host, rpc_port } = this.state;
setRemoteCredentials("rpc_user", rpc_user);
setRemoteCredentials("rpc_pass", rpc_pass);
setRemoteCredentials("rpc_host", rpc_host);
setRemoteCredentials("rpc_port", rpc_port);
setRemoteCredentials("rpc_cert", rpc_cert);
setRemoteCredentials(rpc_user, rpc_pass, rpc_cert, rpc_host, rpc_port);
let args = { rpc_user, rpc_pass, rpc_cert, rpc_host, rpc_port };
this.props.onStartDaemon({ rpcCreds: args });
}
Expand Down
144 changes: 40 additions & 104 deletions app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Store from "electron-store";
import ini from "ini";
import { stakePoolInfo } from "./middleware/stakepoolapi";
import { appDataDirectory, getGlobalCfgPath, dcrdCfg, getWalletPath, dcrwalletCfg, getDcrdRpcCert, getDcrdPath } from "./main_dev/paths";
import * as cfgConstants from "./main_dev/configConstants.js";

export function getGlobalCfg() {
const config = new Store();
Expand Down Expand Up @@ -76,90 +77,24 @@ function cleanWalletCfg(config) {

export function initGlobalCfg() {
const config = new Store();
if (!config.has("theme")) {
config.set("theme", "theme-light");
}
if (!config.has("daemon_start_advanced")) {
config.set("daemon_start_advanced", false);
}
if (!config.has("must_open_form")) {
config.set("must_open_form",true);
}
if (!config.has("locale")) {
config.set("locale","");
}
if (!config.has("network")) {
config.set("network","mainnet");
}
if (!config.has("set_language")) {
config.set("set_language","true");
}
if (!config.has("ui_animations")) {
config.set("ui_animations", true);
}
if (!config.has("show_tutorial")) {
config.set("show_tutorial","true");
}
if (!config.has("show_privacy")) {
config.set("show_privacy", true);
}
if (!config.has("show_spvchoice")) {
config.set("show_spvchoice", true);
}
if (!config.has("allowed_external_requests")) {
config.set("allowed_external_requests", []);
}
if (!config.has("proxy_type")) {
config.set("proxy_type", null);
}
if (!config.has("proxy_location")) {
config.set("proxy_location", null);
}
if (!config.has("remote_credentials")) {
const credentialKeys = {
rpc_user : "",
rpc_pass : "",
rpc_cert : "",
rpc_host : "",
rpc_port : "",
};
config.set("remote_credentials",credentialKeys);
}
if (!config.has("appdata_path")) {
config.set("appdata_path", "");
}
if (!config.has("spv_mode")) {
config.set("spv_mode", false);
}
if (!config.has("spv_connect")) {
config.set("spv_connect", []);
}
if (!config.has("max_wallet_count")) {
config.set("max_wallet_count", 3);
}
if (!config.has("timezone")) {
config.set("timezone", "local");
}
if (!config.has("disable_hardware_accel")) {
config.set("disable_hardware_accel", false);
}
Object.keys(cfgConstants.INITIAL_VALUES).map(key => {
if (!config.has(key)) {
config.set(key, cfgConstants.INITIAL_VALUES[key]);
}
});
cleanGlobalCfg(config);
return(config);
}

function cleanGlobalCfg(config) {
var key;
const globalCfgFields = [ "theme", "daemon_start_advanced", "must_open_form",
"locale", "network", "set_language", "ui_animations", "show_spvchoice",
"show_tutorial", "show_privacy", "allowed_external_requests", "proxy_type",
"proxy_location", "remote_credentials", "spv_mode", "spv_connect",
"max_wallet_count", "timezone", "last_height", "appdata_path",
"disable_hardware_accel" ];
for (key in config.store) {
var found = false;
for (var i = 0; i < globalCfgFields.length; i++) {
if (key == globalCfgFields[i]) {
const globalCfgFields = Object.keys(cfgConstants.INITIAL_VALUES);

for (let key in config.store) {
let found = false;
for (let i = 0; i < globalCfgFields.length; i++) {
if (key === globalCfgFields[i]) {
found = true;
break;
}
}
if (!found) {
Expand All @@ -169,7 +104,7 @@ function cleanGlobalCfg(config) {
}

export function validateGlobalCfgFile() {
var fileContents;
let fileContents;
try {
fileContents = fs.readFileSync(getGlobalCfgPath(), "utf8");
}
Expand Down Expand Up @@ -301,45 +236,46 @@ export function updateStakePoolConfig(config, foundStakePoolConfigs) {
}
}

export function getAppdataPath() {
export function getConfigData(configKey) {
const config = getGlobalCfg();
return config.get("appdata_path");
return config.get(configKey);
}

export function setAppdataPath(appdataPath) {
const config = getGlobalCfg();
const credentialKeys = {
rpc_user : "",
rpc_pass : "",
rpc_cert : "",
rpc_host : "",
rpc_port : "",
};
config.set("remote_credentials",credentialKeys);
return config.set("appdata_path",appdataPath);
export function getRemoteCredentials() {
return getConfigData(cfgConstants.REMOTE_CREDENTIALS);
}

export function getRemoteCredentials() {
const config = getGlobalCfg();
return config.get("remote_credentials");
export function getAppdataPath() {
return getConfigData(cfgConstants.APPDATA);
}

export function getDaemonIsAdvanced() {
return getConfigData(cfgConstants.DAEMON_ADVANCED);
}

export function setRemoteCredentials(key, value) {
export function setConfigData(key, value) {
const config = getGlobalCfg();
config.set("appdata_path","");
let credentials = config.get("remote_credentials");
credentials[key] = value;
return config.set("remote_credentials",credentials);
return config.set(key, value);
}

export function setAppdataPath(appdataPath) {
const credentialKeys = cfgConstants.setDaemonRemoteCredentials("", "", "", "", "");
setConfigData(cfgConstants.REMOTE_CREDENTIALS, credentialKeys);
return setConfigData(cfgConstants.APPDATA, appdataPath);
}

export function setRemoteCredentials(rpcuser, rpcpass, rpccert, rpchost, rpcport) {
setConfigData(cfgConstants.APPDATA, "");
const credentials = cfgConstants.setDaemonRemoteCredentials(rpcuser, rpcpass, rpccert, rpchost, rpcport);
return setConfigData(cfgConstants.REMOTE_CREDENTIALS, credentials);
}

export function setMustOpenForm(openForm) {
const config = getGlobalCfg();
return config.set("must_open_form", openForm);
return setConfigData(cfgConstants.OPEN_FORM, openForm);
}

export function setLastHeight(height) {
const config = getGlobalCfg();
return config.set("last_height", height);
return setConfigData(cfgConstants.LAST_HEIGHT, height);
}

function makeRandomString(length) {
Expand Down
2 changes: 2 additions & 0 deletions app/connectors/fatalErrorPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import * as da from "../actions/DaemonActions";
const mapStateToProps = selectorMap({
daemonError: sel.daemonError,
walletError: sel.walletError,
isAdvancedDaemon: sel.isAdvancedDaemon,
});

const mapDispatchToProps = dispatch => bindActionCreators({
shutdownApp: da.shutdownApp,
deleteDaemonData: da.deleteDaemonData,
backToCredentials: da.backToCredentials,
}, dispatch);

export default connect(mapStateToProps, mapDispatchToProps);
Loading

0 comments on commit 1688833

Please sign in to comment.