Clean up connection code
This commit is contained in:
@@ -37,14 +37,36 @@ void ConnectionLoader::loadConnection() {
|
|||||||
|
|
||||||
if (config.get() != nullptr) {
|
if (config.get() != nullptr) {
|
||||||
auto connection = makeConnection(config);
|
auto connection = makeConnection(config);
|
||||||
refreshZcashdState(connection);
|
|
||||||
|
|
||||||
return;
|
refreshZcashdState(connection, [=] () {
|
||||||
} else if (!Settings::getInstance()->getIsManualConnection()){
|
// Refused connection. So try and start embedded zcashd
|
||||||
// zcash.conf was not found, so create one
|
if (Settings::getInstance()->useEmbedded()) {
|
||||||
createZcashConf();
|
this->showInformation("Starting Embedded zcashd");
|
||||||
|
if (this->startEmbeddedZcashd()) {
|
||||||
|
// Embedded zcashd started up. Wait a second and then refresh the connection
|
||||||
|
QTimer::singleShot(1000, [=]() { loadConnection(); } );
|
||||||
|
} else {
|
||||||
|
// Errored out, show error and exit
|
||||||
|
QString explanation = QString() % "Couldn't start the embedded zcashd.\n\n" %
|
||||||
|
"This is most likely because of corrupt zcash-params. Please delete your zcash-params directory and restart.\n\n" %
|
||||||
|
(ezcashd ? "The process returned:\n\n" % ezcashd->errorString() : QString(""));
|
||||||
|
this->showError(explanation);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// zcash.conf exists, there's no connection, and the user asked us not to start zcashd. Error!
|
||||||
|
QString explanation = QString() % "Couldn't connect to zcashd configured in zcash.conf.\n\n" %
|
||||||
|
"Not starting embedded zcashd because --no-embedded was passed";
|
||||||
|
this->showError(explanation);
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
doManualConnect();
|
if (Settings::getInstance()->useEmbedded()) {
|
||||||
|
// zcash.conf was not found, so create one
|
||||||
|
createZcashConf();
|
||||||
|
} else {
|
||||||
|
// Fall back to manual connect
|
||||||
|
doManualConnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,22 +96,8 @@ void ConnectionLoader::createZcashConf() {
|
|||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch params.
|
// Fetch params. After params are fetched, try loading the connection again
|
||||||
{
|
downloadParams([=] () { this->loadConnection(); });
|
||||||
downloadParams([=] () {
|
|
||||||
startEmbeddedZcashd();
|
|
||||||
|
|
||||||
auto config = autoDetectZcashConf();
|
|
||||||
if (config.get() != nullptr) {
|
|
||||||
auto connection = makeConnection(config);
|
|
||||||
refreshZcashdState(connection);
|
|
||||||
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
qDebug() << "Coulnd't get embedded startup zcashd";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionLoader::downloadParams(std::function<void(void)> cb) {
|
void ConnectionLoader::downloadParams(std::function<void(void)> cb) {
|
||||||
@@ -135,6 +143,8 @@ void ConnectionLoader::doNextDownload(std::function<void(void)> cb) {
|
|||||||
if (currentOutput->exists()) {
|
if (currentOutput->exists()) {
|
||||||
qDebug() << filename << " already exists, skipping ";
|
qDebug() << filename << " already exists, skipping ";
|
||||||
doNextDownload(cb);
|
doNextDownload(cb);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!currentOutput->open(QIODevice::WriteOnly)) {
|
if (!currentOutput->open(QIODevice::WriteOnly)) {
|
||||||
@@ -187,6 +197,9 @@ void ConnectionLoader::doNextDownload(std::function<void(void)> cb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ConnectionLoader::startEmbeddedZcashd() {
|
bool ConnectionLoader::startEmbeddedZcashd() {
|
||||||
|
if (!Settings::getInstance()->useEmbedded())
|
||||||
|
return false;
|
||||||
|
|
||||||
if (ezcashd != nullptr) {
|
if (ezcashd != nullptr) {
|
||||||
if (ezcashd->state() == QProcess::NotRunning) {
|
if (ezcashd->state() == QProcess::NotRunning) {
|
||||||
qDebug() << "Process started and then crashed";
|
qDebug() << "Process started and then crashed";
|
||||||
@@ -213,7 +226,6 @@ bool ConnectionLoader::startEmbeddedZcashd() {
|
|||||||
ezcashd->setWorkingDirectory(fi.dir().absolutePath());
|
ezcashd->setWorkingDirectory(fi.dir().absolutePath());
|
||||||
QObject::connect(ezcashd, &QProcess::started, [=] () {
|
QObject::connect(ezcashd, &QProcess::started, [=] () {
|
||||||
qDebug() << "zcashd started";
|
qDebug() << "zcashd started";
|
||||||
Settings::getInstance()->setEmbeddedZcashdRunning(true);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
QObject::connect(ezcashd, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
QObject::connect(ezcashd, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
||||||
@@ -237,7 +249,7 @@ void ConnectionLoader::doManualConnect() {
|
|||||||
// Nothing configured, show an error
|
// Nothing configured, show an error
|
||||||
QString explanation = QString()
|
QString explanation = QString()
|
||||||
% "A manual connection was requested, but the settings are not configured.\n\n"
|
% "A manual connection was requested, but the settings are not configured.\n\n"
|
||||||
% "Please set the host/port and user/password in the File->Settings menu.";
|
% "Please set the host/port and user/password in the Edit->Settings menu.";
|
||||||
|
|
||||||
showError(explanation);
|
showError(explanation);
|
||||||
doRPCSetConnection(nullptr);
|
doRPCSetConnection(nullptr);
|
||||||
@@ -246,7 +258,16 @@ void ConnectionLoader::doManualConnect() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto connection = makeConnection(config);
|
auto connection = makeConnection(config);
|
||||||
refreshZcashdState(connection);
|
refreshZcashdState(connection, [=] () {
|
||||||
|
QString explanation = QString()
|
||||||
|
% "Could not connect to zcashd configured in settings.\n\n"
|
||||||
|
% "Please set the host/port and user/password in the Edit->Settings menu.";
|
||||||
|
|
||||||
|
showError(explanation);
|
||||||
|
doRPCSetConnection(nullptr);
|
||||||
|
|
||||||
|
return;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionLoader::doRPCSetConnection(Connection* conn) {
|
void ConnectionLoader::doRPCSetConnection(Connection* conn) {
|
||||||
@@ -277,7 +298,7 @@ Connection* ConnectionLoader::makeConnection(std::shared_ptr<ConnectionConfig> c
|
|||||||
return new Connection(main, client, request, config);
|
return new Connection(main, client, request, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionLoader::refreshZcashdState(Connection* connection) {
|
void ConnectionLoader::refreshZcashdState(Connection* connection, std::function<void(void)> refused) {
|
||||||
json payload = {
|
json payload = {
|
||||||
{"jsonrpc", "1.0"},
|
{"jsonrpc", "1.0"},
|
||||||
{"id", "someid"},
|
{"id", "someid"},
|
||||||
@@ -290,36 +311,26 @@ void ConnectionLoader::refreshZcashdState(Connection* connection) {
|
|||||||
this->doRPCSetConnection(connection);
|
this->doRPCSetConnection(connection);
|
||||||
},
|
},
|
||||||
[=] (auto reply, auto res) {
|
[=] (auto reply, auto res) {
|
||||||
auto err = reply->error();
|
|
||||||
// Failed, see what it is.
|
// Failed, see what it is.
|
||||||
|
auto err = reply->error();
|
||||||
//qDebug() << err << ":" << QString::fromStdString(res.dump());
|
//qDebug() << err << ":" << QString::fromStdString(res.dump());
|
||||||
|
|
||||||
if (err == QNetworkReply::NetworkError::ConnectionRefusedError) {
|
if (err == QNetworkReply::NetworkError::ConnectionRefusedError) {
|
||||||
// Start embedded zcasd
|
refused();
|
||||||
this->showInformation("Starting Embedded zcashd");
|
|
||||||
if (this->startEmbeddedZcashd()) {
|
|
||||||
// Refresh after one second
|
|
||||||
QTimer::singleShot(1000, [=]() { this->refreshZcashdState(connection); });
|
|
||||||
} else {
|
|
||||||
// Errored out, show error and exit
|
|
||||||
QString explanation = "Couldn't start the embedded zcashd. The process returned:\n\n" % ezcashd->errorString();
|
|
||||||
this->showError(explanation);
|
|
||||||
}
|
|
||||||
} else if (err == QNetworkReply::NetworkError::AuthenticationRequiredError) {
|
} else if (err == QNetworkReply::NetworkError::AuthenticationRequiredError) {
|
||||||
QString explanation = QString()
|
QString explanation = QString()
|
||||||
% "Authentication failed. The username / password you specified was "
|
% "Authentication failed. The username / password you specified was "
|
||||||
% "not accepted by zcashd. Try changing it in the Edit->Settings menu";
|
% "not accepted by zcashd. Try changing it in the Edit->Settings menu";
|
||||||
|
|
||||||
this->showError(explanation);
|
this->showError(explanation);
|
||||||
} else if (err == QNetworkReply::NetworkError::InternalServerError && !res.is_discarded()) {
|
} else if (err == QNetworkReply::NetworkError::InternalServerError &&
|
||||||
d->show();
|
!res.is_discarded()) {
|
||||||
|
|
||||||
// The server is loading, so just poll until it succeeds
|
// The server is loading, so just poll until it succeeds
|
||||||
QString status = QString::fromStdString(res["error"]["message"]);
|
QString status = QString::fromStdString(res["error"]["message"]);
|
||||||
showInformation("Your zcashd is starting up. Please wait.", status);
|
showInformation("Your zcashd is starting up. Please wait.", status);
|
||||||
|
|
||||||
// Refresh after one second
|
// Refresh after one second
|
||||||
QTimer::singleShot(1000, [=]() { this->refreshZcashdState(connection); });
|
QTimer::singleShot(1000, [=]() { this->refreshZcashdState(connection, refused); });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -335,6 +346,7 @@ void ConnectionLoader::showInformation(QString info, QString detail) {
|
|||||||
*/
|
*/
|
||||||
void ConnectionLoader::showError(QString explanation) {
|
void ConnectionLoader::showError(QString explanation) {
|
||||||
d->hide();
|
d->hide();
|
||||||
|
main->ui->statusBar->showMessage("Connection Error");
|
||||||
QMessageBox::critical(main, "Connection Error", explanation, QMessageBox::Ok);
|
QMessageBox::critical(main, "Connection Error", explanation, QMessageBox::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ private:
|
|||||||
void doNextDownload(std::function<void(void)> cb);
|
void doNextDownload(std::function<void(void)> cb);
|
||||||
bool startEmbeddedZcashd();
|
bool startEmbeddedZcashd();
|
||||||
|
|
||||||
void refreshZcashdState(Connection* connection);
|
void refreshZcashdState(Connection* connection, std::function<void(void)> refused);
|
||||||
int getProgressFromStatus(QString status);
|
int getProgressFromStatus(QString status);
|
||||||
|
|
||||||
void showError(QString explanation);
|
void showError(QString explanation);
|
||||||
|
|||||||
@@ -23,7 +23,9 @@ int main(int argc, char *argv[])
|
|||||||
Settings::getInstance()->setExecName(argv[0]);
|
Settings::getInstance()->setExecName(argv[0]);
|
||||||
|
|
||||||
if (argc >= 2 && QString::fromStdString(argv[1]) == "--no-embedded") {
|
if (argc >= 2 && QString::fromStdString(argv[1]) == "--no-embedded") {
|
||||||
Settings::getInstance()->setManualConnection(true);
|
Settings::getInstance()->setUseEmbedded(false);
|
||||||
|
} else {
|
||||||
|
Settings::getInstance()->setUseEmbedded(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
QCoreApplication::setOrganizationName("zec-qt-wallet-org");
|
QCoreApplication::setOrganizationName("zec-qt-wallet-org");
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ using json = nlohmann::json;
|
|||||||
|
|
||||||
RPC::RPC(MainWindow* main) {
|
RPC::RPC(MainWindow* main) {
|
||||||
auto cl = new ConnectionLoader(main, this);
|
auto cl = new ConnectionLoader(main, this);
|
||||||
|
|
||||||
|
// Show a default no connection message until we can connect.
|
||||||
cl->loadConnection();
|
cl->loadConnection();
|
||||||
|
|
||||||
this->main = main;
|
this->main = main;
|
||||||
@@ -72,6 +74,8 @@ void RPC::setConnection(Connection* c) {
|
|||||||
delete conn;
|
delete conn;
|
||||||
this->conn = c;
|
this->conn = c;
|
||||||
|
|
||||||
|
ui->statusBar->showMessage("Ready!");
|
||||||
|
|
||||||
refreshZECPrice();
|
refreshZECPrice();
|
||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
@@ -495,6 +499,7 @@ void RPC::getInfoThenRefresh(bool force) {
|
|||||||
ui->solrate->setText(QString::number(solrate) % " Sol/s");
|
ui->solrate->setText(QString::number(solrate) % " Sol/s");
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
qDebug() << "removing tab!";
|
||||||
ui->tabWidget->removeTab(4);
|
ui->tabWidget->removeTab(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,14 +29,11 @@ public:
|
|||||||
bool isSyncing();
|
bool isSyncing();
|
||||||
void setSyncing(bool syncing);
|
void setSyncing(bool syncing);
|
||||||
|
|
||||||
bool getIsManualConnection() { return _manualConn; }
|
|
||||||
void setManualConnection(bool manual) {_manualConn = manual;}
|
|
||||||
|
|
||||||
QString getExecName() { return _executable; }
|
QString getExecName() { return _executable; }
|
||||||
void setExecName(QString name) { _executable = name; }
|
void setExecName(QString name) { _executable = name; }
|
||||||
|
|
||||||
void setEmbeddedZcashdRunning(bool r) { _isEmbeddedZcashd = r; }
|
void setUseEmbedded(bool r) { _useEmbedded = r; }
|
||||||
bool isEmbeddedZcashdRunning() { return _isEmbeddedZcashd; }
|
bool useEmbedded() { return _useEmbedded; }
|
||||||
|
|
||||||
int getBlockNumber();
|
int getBlockNumber();
|
||||||
void setBlockNumber(int number);
|
void setBlockNumber(int number);
|
||||||
@@ -69,8 +66,7 @@ private:
|
|||||||
bool _isTestnet = false;
|
bool _isTestnet = false;
|
||||||
bool _isSyncing = false;
|
bool _isSyncing = false;
|
||||||
int _blockNumber = 0;
|
int _blockNumber = 0;
|
||||||
bool _manualConn = false;
|
bool _useEmbedded = false;
|
||||||
bool _isEmbeddedZcashd = false;
|
|
||||||
|
|
||||||
double zecPrice = 0.0;
|
double zecPrice = 0.0;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user