Download Params

This commit is contained in:
Aditya Kulkarni
2018-11-05 21:19:08 -08:00
parent 228f61f225
commit 619ff9fdd6
4 changed files with 125 additions and 44 deletions

View File

@@ -81,56 +81,111 @@ void ConnectionLoader::createZcashConf() {
// Fetch params. // Fetch params.
{ {
QFileInfo fi(Settings::getInstance()->getExecName()); downloadParams([=] () {
auto fetchParamsProgram = fi.dir().filePath("zcash-fetch-params"); startEmbeddedZcashd();
QProcess* p = new QProcess(main); auto config = autoDetectZcashConf();
QObject::connect(p, &QProcess::readyReadStandardOutput, [=] () { if (config.get() != nullptr) {
qDebug() << "stdout:" << p->readAllStandardOutput(); auto connection = makeConnection(config);
}); refreshZcashdState(connection);
QObject::connect(p, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[=](int exitCode, QProcess::ExitStatus exitStatus) {
qDebug() << "finished with code " << exitCode;
p->deleteLater();
});
p->start(fetchParamsProgram);
// stdout: "Zcash - fetch-params.sh\n\nThis script will fetch the Zcash zkSNARK parameters and verify their\nintegrity with sha256sum.\n\nIf they already exist locally, it will exit now and do nothing else.\nThe parameters are currently just under 911MB in size, so plan accordingly\nfor your bandwidth constraints. If the files are already present and\nhave the correct sha256sum, no networking is used.\n\nCreating params directory. For details about this directory, see:\n/home/adityapk/.zcash-params/README\n\n\nRetrieving (wget): https://z.cash/downloads/sprout-proving.key\n" return;
// stdout: "Download successful!\n" } else {
// stdout: "/home/adityapk/.zcash-params/sprout-proving.key.dl: OK\n" qDebug() << "Coulnd't get embedded startup zcashd";
// stdout: "renamed '/home/adityapk/.zcash-params/sprout-proving.key.dl' -> '/home/adityapk/.zcash-params/sprout-proving.key'\n" }
// stdout: "\nRetrieving (wget): https://z.cash/downloads/sprout-verifying.key\n" });
// stdout: "Download successful!\n" }
// stdout: "/home/adityapk/.zcash-params/sprout-verifying.key.dl: OK\n" }
// stdout: "renamed '/home/adityapk/.zcash-params/sprout-verifying.key.dl' -> '/home/adityapk/.zcash-params/sprout-verifying.key'\n"
// stdout: "\nRetrieving (wget): https://z.cash/downloads/sapling-spend.params\n" void ConnectionLoader::downloadParams(std::function<void(void)> cb) {
// stdout: "Download successful!\n" // Add all the files to the download queue
// stdout: "/home/adityapk/.zcash-params/sapling-spend.params.dl: OK\n" downloadQueue = new QQueue<QUrl>();
// stdout: "renamed '/home/adityapk/.zcash-params/sapling-spend.params.dl' -> '/home/adityapk/.zcash-params/sapling-spend.params'\n" client = new QNetworkAccessManager(main);
// stdout: "\nRetrieving (wget): https://z.cash/downloads/sapling-output.params\n"
// stdout: "Download successful!\n" downloadQueue->enqueue(QUrl("https://z.cash/downloads/sapling-output.params"));
// stdout: "/home/adityapk/.zcash-params/sapling-output.params.dl: OK\n" downloadQueue->enqueue(QUrl("https://z.cash/downloads/sapling-spend.params"));
// stdout: "renamed '/home/adityapk/.zcash-params/sapling-output.params.dl' -> '/home/adityapk/.zcash-params/sapling-output.params'\n" downloadQueue->enqueue(QUrl("https://z.cash/downloads/sprout-proving.key"));
// stdout: "\nRetrieving (wget): https://z.cash/downloads/sprout-groth16.params\n" downloadQueue->enqueue(QUrl("https://z.cash/downloads/sprout-verifying.key"));
// stdout: "Download successful!\n" downloadQueue->enqueue(QUrl("https://z.cash/downloads/sprout-groth16.params"));
// stdout: "/home/adityapk/.zcash-params/sprout-groth16.params.dl: OK\n"
// stdout: "renamed '/home/adityapk/.zcash-params/sprout-groth16.params.dl' -> '/home/adityapk/.zcash-params/sprout-groth16.params'\n" doNextDownload(cb);
// finished with code 0
d->show();
}
void ConnectionLoader::doNextDownload(std::function<void(void)> cb) {
qDebug() << "Attempting download";
auto fnSaveFileName = [&] (QUrl url) {
QString path = url.path();
QString basename = QFileInfo(path).fileName();
return basename;
};
if (downloadQueue->isEmpty()) {
delete downloadQueue;
client->deleteLater();
this->showInformation("All Downloads Finished Successfully!");
cb();
return;
} }
{ QUrl url = downloadQueue->dequeue();
startEmbeddedZcashd(); int filesRemaining = downloadQueue->size();
auto config = autoDetectZcashConf(); QString filename = fnSaveFileName(url);
if (config.get() != nullptr) { QString paramsDir = zcashParamsDir();
auto connection = makeConnection(config); currentOutput = new QFile(QDir(paramsDir).filePath(filename));
refreshZcashdState(connection);
return; if (!currentOutput->open(QIODevice::WriteOnly)) {
this->showError("Couldn't download params. Please check the help site for more info.");
}
qDebug() << "Downloading " << url << " to " << filename;
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
currentDownload = client->get(request);
downloadTime.start();
// Download Progress
QObject::connect(currentDownload, &QNetworkReply::downloadProgress, [=] (auto done, auto total) {
// calculate the download speed
double speed = done * 1000.0 / downloadTime.elapsed();
QString unit;
if (speed < 1024) {
unit = "bytes/sec";
} else if (speed < 1024*1024) {
speed /= 1024;
unit = "kB/s";
} else { } else {
qDebug() << "Coulnd't get embedded startup zcashd"; speed /= 1024*1024;
unit = "MB/s";
} }
}
this->showInformation(
"Downloading " % filename % (filesRemaining > 1 ? " ( +" % QString::number(filesRemaining) % " more remaining )" : QString("")),
QString::number(done/1024/1024, 'f', 0) % "MB of " % QString::number(total/1024/1024, 'f', 0) + "MB at " % QString::number(speed, 'f', 2) % unit);
});
// Download Finished
QObject::connect(currentDownload, &QNetworkReply::finished, [=] () {
currentOutput->close();
currentDownload->deleteLater();
currentOutput->deleteLater();
if (currentDownload->error()) {
this->showError("Downloading " + filename + " failed/ Please check the help site for more info");
} else {
doNextDownload(cb);
}
});
// Download new data available.
QObject::connect(currentDownload, &QNetworkReply::readyRead, [=] () {
currentOutput->write(currentDownload->readAll());
});
} }
bool ConnectionLoader::startEmbeddedZcashd() { bool ConnectionLoader::startEmbeddedZcashd() {
@@ -310,6 +365,22 @@ QString ConnectionLoader::zcashConfWritableLocation() {
return confLocation; return confLocation;
} }
QString ConnectionLoader::zcashParamsDir() {
#ifdef Q_OS_LINUX
auto paramsLocation = QDir(QDir(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)).filePath(".zcash-params"));
#elif defined(Q_OS_DARWIN)
//auto paramsLocation = QDir(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)).filePath("/Library/Application Support/Zcash/zcash.conf");
#else
auto paramsLocation = QDir(QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)).filePath("../../ZcashParams"));
#endif
if (!paramsLocation.exists()) {
QDir().mkpath(paramsLocation.absolutePath());
}
return paramsLocation.absolutePath();
}
/** /**
* Try to automatically detect a zcash.conf file in the correct location and load parameters * Try to automatically detect a zcash.conf file in the correct location and load parameters
*/ */

View File

@@ -47,7 +47,10 @@ private:
void createZcashConf(); void createZcashConf();
QString locateZcashConfFile(); QString locateZcashConfFile();
QString zcashConfWritableLocation(); QString zcashConfWritableLocation();
QString zcashParamsDir();
void downloadParams(std::function<void(void)> cb);
void doNextDownload(std::function<void(void)> cb);
bool startEmbeddedZcashd(); bool startEmbeddedZcashd();
void refreshZcashdState(Connection* connection); void refreshZcashdState(Connection* connection);
@@ -65,6 +68,13 @@ private:
MainWindow* main; MainWindow* main;
RPC* rpc; RPC* rpc;
QNetworkReply* currentDownload = nullptr;
QFile* currentOutput = nullptr;
QQueue<QUrl>* downloadQueue = nullptr;
QNetworkAccessManager* client = nullptr;
QTime downloadTime;
}; };
/** /**

View File

@@ -38,6 +38,7 @@
#include <QFileDialog> #include <QFileDialog>
#include <QDebug> #include <QDebug>
#include <QUrl> #include <QUrl>
#include <QQueue>
#include <QProcess> #include <QProcess>
#include <QDesktopServices> #include <QDesktopServices>
#include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkRequest>

View File

@@ -836,8 +836,7 @@ void RPC::shutdownZcashd() {
d.setWindowTitle("Waiting for zcashd to exit"); d.setWindowTitle("Waiting for zcashd to exit");
d.setText("Please wait for zcashd to exit. Don't click OK!"); d.setText("Please wait for zcashd to exit. Don't click OK!");
d.setStandardButtons(QMessageBox::NoButton); d.setStandardButtons(QMessageBox::NoButton);
//d.setWindowFlags(Qt::SplashScreen);
QTimer waiter(main); QTimer waiter(main);
// We capture by reference all the local variables because of the d.exec() // We capture by reference all the local variables because of the d.exec()