From 9befa3450fdeea8a76bae324811f49b0593a151f Mon Sep 17 00:00:00 2001 From: Duke Date: Sun, 26 Mar 2023 08:41:52 -0700 Subject: [PATCH] Try another server if current is down when saving wallet via a rescan --- src/connection.cpp | 9 ++++----- src/mainwindow.cpp | 29 +++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/connection.cpp b/src/connection.cpp index 9f6ec58..e8582c6 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -364,12 +364,11 @@ void Executor::run() nullptr, false ); - if (parsed.is_discarded() || parsed.is_null()) + if (parsed.is_discarded() || parsed.is_null()) { emit handleError(reply); - - else - - emit responseReady(parsed); + } else { + emit responseReady(parsed); + } } void Callback::processRPCCallback(json resp) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index d39ae4d..32809c0 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -217,6 +217,7 @@ MainWindow::MainWindow(QWidget *parent) : } catch (const std::exception& e) { qDebug() << __func__ << ": caught an exception, ignoring: " << e.what(); } + if (reply.toUpper().trimmed() != "OK") { qDebug() << "Lite server " << config->server << " is down, getting a random one"; config->server = Settings::getRandomServer(); @@ -237,8 +238,32 @@ MainWindow::MainWindow(QWidget *parent) : // 4. Finally attempt to save the wallet { - char* resp = litelib_execute("save", ""); - QString reply = litelib_process_response(resp); + QString reply = ""; + try { + char* resp = litelib_execute("save", ""); + QString reply = litelib_process_response(resp); + } catch (const std::exception& e) { + qDebug() << __func__ << ": caught an exception, ignoring: " << e.what(); + } + + if (reply.toUpper().trimmed() != "OK") { + qDebug() << "Lite server " << config->server << " is down, getting a random one"; + config->server = Settings::getRandomServer(); + qDebug() << __func__ << ": new server is " << config->server; + // make a new connection to the new server + char* resp = litelib_initialize_new_from_phrase(config->dangerous,config->server.toStdString().c_str(), + seed.toStdString().c_str(), birthday, number); + reply = litelib_process_response(resp); + + // retry with the new server + try { + resp = litelib_execute("save", ""); + reply = litelib_process_response(resp); + } catch (const std::exception& e) { + qDebug() << __func__ << ": caught an exception with new server, something is fucky: " << e.what(); + } + } + QByteArray ba_reply = reply.toUtf8(); QJsonDocument jd_reply = QJsonDocument::fromJson(ba_reply);