Revert "Merge branch 'dev'"

This reverts commit 4a7dd7f959, reversing
changes made to a89a0cc1c6.
This commit is contained in:
onryo
2024-01-06 16:27:48 +01:00
parent 4a7dd7f959
commit 3c2414028b
127 changed files with 1747 additions and 770 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2019-2024 The Hush developers
// Copyright 2019-2023 The Hush developers
// Released under the GPLv3
#include "connection.h"
#include "mainwindow.h"
@@ -9,8 +9,6 @@
#include "controller.h"
#include "../lib/silentdragonlitelib.h"
#include "precompiled.h"
#include <QThreadPool>
#include "sdl.h"
using json = nlohmann::json;
@@ -34,7 +32,7 @@ ConnectionLoader::ConnectionLoader(MainWindow* main, Controller* rpc)
connD->setupUi(d);
auto theme = Settings::getInstance()->get_theme_name();
DEBUG("theme " << theme << " has loaded");
qDebug() << theme << "theme " << theme << " has loaded";
auto size = QSize(512,512);
if (theme == "Dark" || theme == "Midnight") {
@@ -57,7 +55,6 @@ ConnectionLoader::ConnectionLoader(MainWindow* main, Controller* rpc)
ConnectionLoader::~ConnectionLoader()
{
DEBUG("destroying ConnectionLoader");
delete isSyncing;
delete connD;
delete d;
@@ -65,7 +62,6 @@ ConnectionLoader::~ConnectionLoader()
void ConnectionLoader::loadConnection()
{
DEBUG("calling doAutoConnect");
QTimer::singleShot(1, [=]() { this->doAutoConnect(); });
if (!Settings::getInstance()->isHeadless())
d->exec();
@@ -73,22 +69,7 @@ void ConnectionLoader::loadConnection()
void ConnectionLoader::loadProgress()
{
bool failed = false;
QTimer::singleShot(1, [=]() mutable {
DEBUG("failed=" << failed);
// continually retry ShowProgress() until it succeeds
// by running without an exception
do {
try {
this->ShowProgress();
failed = false;
} catch (const std::exception& e) {
DEBUG("caught exception " << e.what() );
failed = true;
}
} while (failed);
});
QTimer::singleShot(1, [=]() { this->ShowProgress(); });
if (!Settings::getInstance()->isHeadless())
d->exec();
}
@@ -102,62 +83,56 @@ void ConnectionLoader::ShowProgress()
auto connection = makeConnection(config);
auto me = this;
DEBUG("server=" << config->server << " connection=" << connection << " me=" << me);
qDebug() << __func__ << ": server=" << config->server << " connection=" << connection << " me=" << me;
isSyncing = new QAtomicInteger<bool>();
isSyncing->store(true);
DEBUG("isSyncing");
main->logger->write("isSyncing");
// Do a sync after import
syncTimer = new QTimer(main);
DEBUG("Beginning sync after import wif");
connection->doRPC("sync", "", [=](auto) {
DEBUG("finished syncing");
main->logger->write("Beginning sync after import wif");
connection->doRPCWithDefaultErrorHandling("sync", "", [=](auto) {
isSyncing->store(false);
// Cancel the timer
syncTimer->deleteLater();
// When sync is done, set the connection
this->doRPCSetConnectionShield(connection);
}, [=](auto) {
DEBUG("sync rpc error! server=" << config->server);
});
// While it is syncing, we'll show the status updates while it is alive.
QObject::connect(syncTimer, &QTimer::timeout, [=]() {
DEBUG("Check the sync status");
// Check the sync status
if (isSyncing != nullptr && isSyncing->load()) {
DEBUG("Get the sync status");
// Get the sync status
try {
connection->doRPC("syncstatus", "", [=](json reply) {
if (isSyncing != nullptr && reply.find("synced_blocks") != reply.end()) {
qint64 synced = reply["synced_blocks"].get<json::number_unsigned_t>();
qint64 total = reply["total_blocks"].get<json::number_unsigned_t>();
me->showInformation(
"Syncing... " + QString::number(synced) + " / " + QString::number(total)
);
}
}, [=](QString err) {
DEBUG("Sync error " << err);
// We may have gotten "Unexpected compression flag: 60"
// or some other error, so let's try another server
config->server = Settings::getRandomServer();
DEBUG("Changed server to " << config->server );
});
} catch (const std::exception& e) {
DEBUG("syncstatus exception: " << e.what() );
main->logger->write("catch sync progress reply");
// rethrow exception so loadProgress can catch
// it and retry the entire ShowProgress() function again
throw new std::runtime_error(std::string("syncstatus failed"));
}
connection->doRPC("syncstatus", "", [=](json reply) {
if (isSyncing != nullptr && reply.find("synced_blocks") != reply.end())
{
qint64 synced = reply["synced_blocks"].get<json::number_unsigned_t>();
qint64 total = reply["total_blocks"].get<json::number_unsigned_t>();
me->showInformation(
"Syncing... " + QString::number(synced) + " / " + QString::number(total)
);
}
},
[=](QString err) {
qDebug() << "Sync error" << err;
});
}catch (...)
{
main->logger->write("catch sync progress reply");
}
});
}
});
syncTimer->setInterval(1* 1000);
syncTimer->start();
main->logger->write("Start sync timer");
int interval = 1*1000;
syncTimer->setInterval(interval);
syncTimer->start();
DEBUG("Start sync timer with interval=" << interval);
}
void ConnectionLoader::doAutoConnect()
@@ -165,53 +140,43 @@ void ConnectionLoader::doAutoConnect()
auto config = std::shared_ptr<ConnectionConfig>(new ConnectionConfig());
config->dangerous = false;
config->server = Settings::getInstance()->getSettings().server;
DEBUG(" server=" << config->server);
qDebug() << __func__ << " server=" << config->server;
// Initialize the library
DEBUG("Attempting to initialize library with "<< config->server);
main->logger->write(QObject::tr("Attempting to initialize library with ") + config->server);
// Check to see if there's an existing wallet
if (litelib_wallet_exists(Settings::getDefaultChainName().toStdString().c_str())) {
DEBUG("using existing wallet");
qDebug() << __func__ << ": using existing wallet";
main->logger->write(QObject::tr("Using existing wallet."));
QString response = "";
try {
char* resp = litelib_initialize_existing(
config->dangerous,
config->server.toStdString().c_str()
);
response = litelib_process_response(resp);
} catch (const std::exception& e) {
DEBUG("caught an exception, ignoring: " << e.what());
}
char* resp = litelib_initialize_existing(
config->dangerous,
config->server.toStdString().c_str()
);
QString response = litelib_process_response(resp);
if (response.toUpper().trimmed() != "OK") {
config->server = Settings::getRandomServer();
try {
char* resp = litelib_initialize_existing(
config->dangerous,
config->server.toStdString().c_str()
);
response = litelib_process_response(resp);
} catch (const std::exception& e) {
DEBUG("caught an exception, ignoring: " << e.what());
}
resp = litelib_initialize_existing(
config->dangerous,
config->server.toStdString().c_str()
);
response = litelib_process_response(resp);
if (response.toUpper().trimmed() != "OK") {
QString resp = "Error when connecting to " + config->server + ": " + response;
showError(resp);
return;
} else {
DEBUG("Successfully connected to random server: " << config->server << " !!!");
qDebug() << __func__ << ": Successfully connected to random server: " << config->server << " !!!";
}
} else {
DEBUG("Successfully connected to " << config->server << " !!!");
qDebug() << __func__ << ": Successfully connected to " << config->server << " !!!";
}
} else {
DEBUG("no existing wallet");
qDebug() << __func__ << ": no existing wallet";
main->logger->write(QObject::tr("Create/restore wallet."));
createOrRestore(config->dangerous, config->server);
d->show();
@@ -222,77 +187,61 @@ void ConnectionLoader::doAutoConnect()
qDebug() << __func__ << ": server=" << config->server
<< " connection=" << connection << " me=" << me << endl;
// After the lib is initialized, try to do get info
connection->doRPC("info", "", [=](auto reply) {
// If success, set the connection
DEBUG("Connection is online.");
main->logger->write("Connection is online.");
connection->setInfo(reply);
DEBUG("getting Connection reply");
main->logger->write("getting Connection reply");
isSyncing = new QAtomicInteger<bool>();
isSyncing->store(true);
DEBUG("isSyncing");
main->logger->write("isSyncing");
// Do a sync at startup
syncTimer = new QTimer(main);
DEBUG("Beginning sync");
connection->doRPC("sync", "", [=](auto) {
DEBUG("finished syncing");
main->logger->write("Beginning sync");
connection->doRPCWithDefaultErrorHandling("sync", "", [=](auto) {
isSyncing->store(false);
// Cancel the timer
syncTimer->deleteLater();
// When sync is done, set the connection
this->doRPCSetConnection(connection);
}, [=](auto) mutable {
DEBUG("sync rpc error! server=" << config->server);
// continually retry sync RPC until it succeeds
// don't change server each time it fails
bool failed = true;
do {
// config->server = Settings::getRandomServer();
// auto connection = makeConnection(config);
// DEBUG("changed server to " << config->server);
connection->doRPC("sync", "", [=](auto) mutable {
DEBUG("sync success with server=" << config->server);
failed = false;
isSyncing->store(false);
// Cancel the timer
syncTimer->deleteLater();
// When sync is done, set the connection
this->doRPCSetConnection(connection);
}, [=](auto) {
DEBUG("sync failed with server=" << config->server << " . continuing sync loop" );
});
} while (failed);
});
// While it is syncing, we'll show the status updates while it is alive.
QObject::connect(syncTimer, &QTimer::timeout, [=]() {
DEBUG("Check the sync status");
// Check the sync status
if (isSyncing != nullptr && isSyncing->load()) {
DEBUG("Getting the sync status");
// Get the sync status
try {
connection->doRPC("syncstatus", "", [=](json reply) {
if (isSyncing != nullptr && reply.find("synced_blocks") != reply.end()) {
if (isSyncing != nullptr && reply.find("synced_blocks") != reply.end())
{
qint64 synced = reply["synced_blocks"].get<json::number_unsigned_t>();
qint64 total = reply["total_blocks"].get<json::number_unsigned_t>();
me->showInformation(
"Syncing... " + QString::number(synced) + " / " + QString::number(total)
);
}
},
[=](QString err) {
DEBUG("syncstatus error" << err);
});
} catch (const std::exception& e) {
DEBUG("caught exception from syncstatus: " << e.what());
}
},
[=](QString err) {
qDebug() << "Sync error" << err;
});
}catch (...)
{
main->logger->write("catch sync progress reply");
}
}
});
int interval = 1*1000;
syncTimer->setInterval(interval);
syncTimer->setInterval(1* 1000);
syncTimer->start();
DEBUG("Start sync timer with interval=" << interval);
main->logger->write("Start sync timer");
}, [=](QString err) {
showError(err);
@@ -306,13 +255,13 @@ void ConnectionLoader::createOrRestore(bool dangerous, QString server)
d->hide();
// Create a wizard
FirstTimeWizard wizard(dangerous,server);
DEBUG("Start new Wallet with FirstimeWizard");
main->logger->write("Start new Wallet with FirstimeWizard");
wizard.exec();
}
void ConnectionLoader::doRPCSetConnection(Connection* conn)
{
DEBUG("Connectionloader finished, setting connection");
qDebug() << "Connectionloader finished, setting connection";
main->logger->write("Connectionloader finished, setting connection");
rpc->setConnection(conn);
d->accept();
@@ -323,16 +272,17 @@ void ConnectionLoader::doRPCSetConnection(Connection* conn)
main->logger->write("Path to Wallet.dat : " );
qDebug() << __func__ << ": wallet path =" << plaintextWallet;
plaintextWallet.remove();
} catch (const std::exception& e) {
DEBUG("Caught exception" << e.what() );
DEBUG("No plaintext wallet found! file=" << plaintextWallet);
} catch (...) {
qDebug() << "No plaintext wallet found! file=" << plaintextWallet;
main->logger->write("no Plaintext wallet.dat");
}
}
void ConnectionLoader::doRPCSetConnectionShield(Connection* conn)
{
DEBUG("Importing finished, setting connection");
qDebug() << "Importing finished, setting connection";
rpc->setConnection(conn);
d->accept();
main->getRPC()->shield([=] (auto) {});
@@ -343,10 +293,9 @@ void ConnectionLoader::doRPCSetConnectionShield(Connection* conn)
main->logger->write("Path to Wallet.dat : " );
qDebug() << __func__ << ": wallet path =" << plaintextWallet;
plaintextWallet.remove();
} catch (const std::exception& e) {
DEBUG("Caught exception" << e.what() );
} catch (...) {
main->logger->write("no Plaintext wallet.dat");
DEBUG("No plaintext wallet found! file=" << plaintextWallet);
qDebug() << "No plaintext wallet found! file=" << plaintextWallet;
}
}
@@ -397,50 +346,19 @@ QString litelib_process_response(char* resp)
************************************************************************************/
void Executor::run()
{
auto config = std::shared_ptr<ConnectionConfig>(new ConnectionConfig());
DEBUG("cmd=" << cmd << " args=" << args << " server=" << config->server);
QString response = "";
try {
char* resp = litelib_execute(this->cmd.toStdString().c_str(), this->args.toStdString().c_str());
response = litelib_process_response(resp);
} catch (const std::exception& e) {
DEBUG("ignoring exception: " << e.what() );
}
char* resp = litelib_execute(this->cmd.toStdString().c_str(), this->args.toStdString().c_str());
QString reply = litelib_process_response(resp);
auto parsed = json::parse(
reply.toStdString().c_str(),
nullptr,
false
);
if (parsed.is_discarded() || parsed.is_null())
emit handleError(reply);
//TODO: we can do stricter error checking
if (response.isEmpty()) {
config->server = Settings::getRandomServer();
try {
char* resp = litelib_initialize_existing(
config->dangerous,
config->server.toStdString().c_str()
);
response = litelib_process_response(resp);
resp = litelib_execute(this->cmd.toStdString().c_str(), this->args.toStdString().c_str());
response = litelib_process_response(resp);
} catch (const std::exception& e) {
DEBUG("server= " << config->server << " gave exception: " << e.what() );
emit handleError(response);
}
}
try {
auto parsed = json::parse(
response.toStdString().c_str(),
nullptr,
false
);
if (parsed.is_discarded() || parsed.is_null()) {
emit handleError(response);
} else {
emit responseReady(parsed);
}
} catch (const std::exception& e) {
DEBUG("exception when parsing json: " << e.what() );
emit handleError(response);
}
else
emit responseReady(parsed);
}
void Callback::processRPCCallback(json resp)
@@ -468,12 +386,11 @@ Connection::Connection(MainWindow* m, std::shared_ptr<ConnectionConfig> conf)
void Connection::doRPC(const QString cmd, const QString args, const std::function<void(json)>& cb, const std::function<void(QString)>& errCb)
{
if (shutdownInProgress) {
DEBUG("Ignoring RPC because shutdown in progress");
if (shutdownInProgress)
// Ignoring RPC because shutdown in progress
return;
}
DEBUG("cmd=" << cmd << " args=" << args);
qDebug() << __func__ << ": " << cmd;
// Create a runner.
auto runner = new Executor(cmd, args);
@@ -488,7 +405,7 @@ void Connection::doRPC(const QString cmd, const QString args, const std::functio
void Connection::doRPCWithDefaultErrorHandling(const QString cmd, const QString args, const std::function<void(json)>& cb)
{
DEBUG("cmd=" << cmd << " args=" << args);
qDebug() << __func__ << ": " << cmd;
doRPC(cmd, args, cb, [=] (QString err) {
this->showTxError(err);
});
@@ -496,7 +413,7 @@ void Connection::doRPCWithDefaultErrorHandling(const QString cmd, const QString
void Connection::doRPCIgnoreError(const QString cmd, const QString args, const std::function<void(json)>& cb)
{
DEBUG("cmd=" << cmd << " args=" << args);
qDebug() << __func__ << ": " << cmd;
doRPC(cmd, args, cb, [=] (auto) {
// Ignored error handling
});
@@ -528,6 +445,5 @@ void Connection::showTxError(const QString& error)
*/
void Connection::shutdown()
{
DEBUG("shutting down");
shutdownInProgress = true;
}