close zcashd properly at exit

This commit is contained in:
Aditya Kulkarni
2018-11-02 23:15:39 -07:00
parent 46f2f7f02e
commit ce16f53f69
5 changed files with 51 additions and 23 deletions

View File

@@ -134,11 +134,13 @@ void ConnectionLoader::createZcashConf() {
} }
bool ConnectionLoader::startEmbeddedZcashd() { bool ConnectionLoader::startEmbeddedZcashd() {
static bool erroredOut = false; if (ezcashd != nullptr) {
if (ezcashd->state() == QProcess::NotRunning) {
if (erroredOut) { qDebug() << "Process started and then crashed";
qDebug() << "Last zcashd start attempted errored, so not restarting"; return false;
return false; } else {
return true;
}
} }
// Finally, start zcashd // Finally, start zcashd
@@ -146,23 +148,23 @@ bool ConnectionLoader::startEmbeddedZcashd() {
QFileInfo fi(Settings::getInstance()->getExecName()); QFileInfo fi(Settings::getInstance()->getExecName());
auto zcashdProgram = fi.dir().filePath("zcashd"); auto zcashdProgram = fi.dir().filePath("zcashd");
QProcess* p = new QProcess(main); ezcashd = new QProcess(main);
QObject::connect(p, &QProcess::started, [=] () { QObject::connect(ezcashd, &QProcess::started, [=] () {
qDebug() << "zcashd started";
Settings::getInstance()->setEmbeddedZcashdRunning(true); Settings::getInstance()->setEmbeddedZcashdRunning(true);
}); });
QObject::connect(p, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), QObject::connect(ezcashd, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[=](int exitCode, QProcess::ExitStatus exitStatus) { [=](int exitCode, QProcess::ExitStatus exitStatus) {
qDebug() << "zcashd finished with code " << exitCode; qDebug() << "zcashd finished with code " << exitCode << "," << exitStatus;
p->deleteLater();
}); });
QObject::connect(p, &QProcess::errorOccurred, [&] (auto error) mutable { QObject::connect(ezcashd, &QProcess::errorOccurred, [&] (auto error) mutable {
qDebug() << "Couldn't start zcashd: " << error; qDebug() << "Couldn't start zcashd: " << error;
erroredOut = true;
}); });
p->start(zcashdProgram); ezcashd->start(zcashdProgram);
return true; return true;
} }
@@ -187,6 +189,10 @@ void ConnectionLoader::doManualConnect() {
} }
void ConnectionLoader::doRPCSetConnection(Connection* conn) { void ConnectionLoader::doRPCSetConnection(Connection* conn) {
if (ezcashd != nullptr) {
rpc->setEZcashd(ezcashd);
}
rpc->setConnection(conn); rpc->setConnection(conn);
delete this; delete this;
} }
@@ -257,7 +263,7 @@ void ConnectionLoader::refreshZcashdState(Connection* connection) {
this->showError(explanation); this->showError(explanation);
} else if (err == QNetworkReply::NetworkError::InternalServerError && !res.is_discarded()) { } else if (err == QNetworkReply::NetworkError::InternalServerError && !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.\n\n" % status); showInformation("Your zcashd is starting up. Please wait.\n\n" % status);
// Refresh after one second // Refresh after one second

View File

@@ -58,6 +58,8 @@ private:
void doRPCSetConnection(Connection* conn); void doRPCSetConnection(Connection* conn);
QProcess* ezcashd = nullptr;
QDialog* d; QDialog* d;
Ui_ConnectionDialog* connD; Ui_ConnectionDialog* connD;

View File

@@ -89,7 +89,7 @@ void MainWindow::closeEvent(QCloseEvent* event) {
s.setValue("tratablegeometry", ui->transactionsTable->horizontalHeader()->saveState()); s.setValue("tratablegeometry", ui->transactionsTable->horizontalHeader()->saveState());
// Let the RPC know to shutdown any running service. // Let the RPC know to shutdown any running service.
rpc->closeEvent(); rpc->shutdownZcashd();
// Bubble up // Bubble up
QMainWindow::closeEvent(event); QMainWindow::closeEvent(event);

View File

@@ -62,6 +62,10 @@ RPC::~RPC() {
delete conn; delete conn;
} }
void RPC::setEZcashd(QProcess* p) {
ezcashd = p;
}
void RPC::setConnection(Connection* c) { void RPC::setConnection(Connection* c) {
if (c == nullptr) return; if (c == nullptr) return;
@@ -478,7 +482,7 @@ void RPC::getInfoThenRefresh(bool force) {
conn->doRPCIgnoreError(payload, [=](const json& reply) { conn->doRPCIgnoreError(payload, [=](const json& reply) {
auto progress = reply["verificationprogress"].get<double>(); auto progress = reply["verificationprogress"].get<double>();
bool isSyncing = progress < 0.999; // 99.9% bool isSyncing = progress < 0.995; // 99.59%
int blockNumber = reply["blocks"].get<json::number_unsigned_t>(); int blockNumber = reply["blocks"].get<json::number_unsigned_t>();
Settings::getInstance()->setSyncing(isSyncing); Settings::getInstance()->setSyncing(isSyncing);
@@ -816,10 +820,25 @@ void RPC::shutdownZcashd() {
}; };
conn->doRPCWithDefaultErrorHandling(payload, [=](auto) {}); conn->doRPCWithDefaultErrorHandling(payload, [=](auto) {});
}
void RPC::closeEvent() { QMessageBox d(main);
if (Settings::getInstance()->isEmbeddedZcashdRunning()) { d.setIcon(QMessageBox::Icon::Information);
shutdownZcashd(); d.setWindowTitle("Waiting for zcashd to exit");
} d.setText("Please wait for zcashd to exit. Don't click OK!");
} d.setStandardButtons(QMessageBox::NoButton);
d.setWindowFlags(Qt::SplashScreen);
QTimer waiter(main);
QObject::connect(&waiter, &QTimer::timeout, [&] () {
if (ezcashd->atEnd()) {
qDebug() << "Ended";
d.accept();
} else {
qDebug() << "Not ended";
}
});
waiter.start(1000);
// Wait for the zcash process to exit.
d.exec();
}

View File

@@ -32,6 +32,7 @@ public:
~RPC(); ~RPC();
void setConnection(Connection* c); void setConnection(Connection* c);
void setEZcashd(QProcess* p);
void refresh(bool force = false); void refresh(bool force = false);
@@ -63,7 +64,6 @@ public:
Turnstile* getTurnstile() { return turnstile; } Turnstile* getTurnstile() { return turnstile; }
Connection* getConnection() { return conn; } Connection* getConnection() { return conn; }
void closeEvent();
private: private:
void noConnection(); void noConnection();
@@ -90,6 +90,7 @@ private:
void handleTxError (const QString& error); void handleTxError (const QString& error);
Connection* conn = nullptr; Connection* conn = nullptr;
QProcess* ezcashd = nullptr;
QList<UnspentOutput>* utxos = nullptr; QList<UnspentOutput>* utxos = nullptr;
QMap<QString, double>* allBalances = nullptr; QMap<QString, double>* allBalances = nullptr;