Browse Source

Try another server if current is down when restoring a wallet from seed or saving a wallet

pull/125/head
Duke 1 year ago
parent
commit
557e10e5e8
  1. 117
      src/firsttimewizard.cpp

117
src/firsttimewizard.cpp

@ -623,21 +623,49 @@ bool NewSeedPage::validatePage() {
dialog.exec(); dialog.exec();
if ((verifyseed.verify->toPlainText() == seed) && (verifyseed.verifyBirthday->toPlainText() == birthday)) QString reply = "";
{ if ((verifyseed.verify->toPlainText() == seed) && (verifyseed.verifyBirthday->toPlainText() == birthday)) {
char* resp = litelib_execute("save", ""); try {
QString reply = litelib_process_response(resp); char* resp = litelib_execute("save", "");
reply = litelib_process_response(resp);
} catch (const std::exception& e) {
qDebug() << __func__ << ": caught an exception, ignoring: " << e.what();
}
auto parsed = json::parse(reply.toStdString().c_str(), nullptr, false); qDebug() << __func__ << ": reply=" << reply;
if (parsed.is_discarded() || parsed.is_null() || parsed.find("result") == parsed.end()) {
QMessageBox::warning(this, tr("Failed to save wallet"), // TODO: this is duplicated code that should be refactored
tr("Couldn't save the wallet") + "\n" + reply, // into a dedicated function
QMessageBox::Ok); if (reply.toUpper().trimmed() != "OK") {
return false; qDebug() << "Lite server " << parent->server << " is down, getting a random one";
} else { parent->server = Settings::getRandomServer();
return true; qDebug() << __func__ << ": new server is " << parent->server;
}
// make a new connection to the new server
char* resp = litelib_initialize_new(parent->dangerous,parent->server.toStdString().c_str());
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();
}
}
qDebug() << __func__ << ": reply=" << reply;
auto parsed = json::parse(reply.toStdString().c_str(), nullptr, false);
if (parsed.is_discarded() || parsed.is_null() || parsed.find("result") == parsed.end()) {
QMessageBox::warning(this, tr("Failed to save wallet"),
tr("Couldn't save the wallet") + "\n" + reply,
QMessageBox::Ok);
return false;
} else {
return true;
}
}else{ }else{
qDebug()<<"Wrong Seed"; qDebug()<<"Wrong Seed";
QFile file(dirwalletencfirst); QFile file(dirwalletencfirst);
@ -645,9 +673,7 @@ bool NewSeedPage::validatePage() {
file.remove(); file.remove();
file1.remove(); file1.remove();
QMessageBox::warning(this, tr("Wrong Seed"), QMessageBox::warning(this, tr("Wrong Seed"), tr("Please try again") + "\n" , QMessageBox::Ok);
tr("Please try again") + "\n" ,
QMessageBox::Ok);
form.birthday->setVisible(true); form.birthday->setVisible(true);
form.txtSeed->setVisible(true); form.txtSeed->setVisible(true);
return false; return false;
@ -702,22 +728,67 @@ bool RestoreSeedPage::validatePage() {
qint64 number = number_str.toUInt(); qint64 number = number_str.toUInt();
// 3. Attempt to restore wallet with the seed phrase // 3. Attempt to restore wallet with the seed phrase
{ {
char* resp = litelib_initialize_new_from_phrase(parent->dangerous, parent->server.toStdString().c_str(), QString reply = "";
seed.toStdString().c_str(), birthday, number); try {
QString reply = litelib_process_response(resp); char* resp = litelib_initialize_new_from_phrase(parent->dangerous, parent->server.toStdString().c_str(),
seed.toStdString().c_str(), birthday, number);
reply = litelib_process_response(resp);
} catch (const std::exception& e) {
qDebug() << __func__ << ": caught an exception, ignoring: " << e.what();
}
qDebug() << __func__ << ": reply=" << reply;
if (reply.toUpper().trimmed() != "OK") {
qDebug() << "Lite server " << parent->server << " is down, getting a random one";
parent->server = Settings::getRandomServer();
qDebug() << __func__ << ": new server is " << parent->server;
// retry with the new server
char* resp = litelib_initialize_new(parent->dangerous,parent->server.toStdString().c_str());
reply = litelib_process_response(resp);
}
if (reply.toUpper().trimmed() != "OK") { if (reply.toUpper().trimmed() != "OK") {
QMessageBox::warning(this, tr("Failed to restore wallet"), QMessageBox::warning(this, tr("Failed to restore wallet"),
tr("Couldn't restore the wallet") + "\n" + reply, tr("Couldn't restore the wallet") + "\n" + reply,
QMessageBox::Ok); QMessageBox::Ok);
return false; return false;
} }
} }
// 4. Finally attempt to save the wallet // 4. Finally attempt to save the wallet
{ {
char* resp = litelib_execute("save", ""); QString reply = "";
QString reply = litelib_process_response(resp); try {
char* resp = litelib_execute("save", "");
reply = litelib_process_response(resp);
} catch (const std::exception& e) {
qDebug() << __func__ << ": caught an exception, ignoring: " << e.what();
}
// TODO: this is duplicated code that should be refactored
// into a dedicated function
if (reply.toUpper().trimmed() != "OK") {
qDebug() << "Lite server " << parent->server << " is down, getting a random one";
parent->server = Settings::getRandomServer();
qDebug() << __func__ << ": new server is " << parent->server;
// make a new connection to the new server
char* resp = litelib_initialize_new(parent->dangerous,parent->server.toStdString().c_str());
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();
}
}
qDebug() << __func__ << ": reply=" << reply;
auto parsed = json::parse(reply.toStdString().c_str(), nullptr, false); auto parsed = json::parse(reply.toStdString().c_str(), nullptr, false);
if (parsed.is_discarded() || parsed.is_null() || parsed.find("result") == parsed.end()) { if (parsed.is_discarded() || parsed.is_null() || parsed.find("result") == parsed.end()) {
@ -728,6 +799,6 @@ bool RestoreSeedPage::validatePage() {
return false; return false;
} else { } else {
return true; return true;
} }
} }
} }

Loading…
Cancel
Save