|
|
@ -100,64 +100,62 @@ void ConnectionLoader::ShowProgress() |
|
|
|
config->dangerous = false; |
|
|
|
config->server = Settings::getInstance()->getSettings().server; |
|
|
|
|
|
|
|
DEBUG("Creating connection with server=" << config->server); |
|
|
|
auto connection = makeConnection(config); |
|
|
|
auto me = this; |
|
|
|
DEBUG("server=" << config->server << " connection=" << connection << " me=" << me); |
|
|
|
if (!connection) { |
|
|
|
DEBUG("Failed to create connection"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
isSyncing = new QAtomicInteger<bool>(); |
|
|
|
isSyncing->storeRelaxed(true); |
|
|
|
DEBUG("isSyncing"); |
|
|
|
auto me = this; |
|
|
|
isSyncing = new QAtomicInteger<bool>(true); |
|
|
|
DEBUG("isSyncing set to true"); |
|
|
|
|
|
|
|
// Do a sync after import
|
|
|
|
syncTimer = new QTimer(main); |
|
|
|
DEBUG("Beginning sync after import wif"); |
|
|
|
DEBUG("Created syncTimer"); |
|
|
|
connection->doRPC("sync", "", [=](auto) { |
|
|
|
qDebug()<< "finished syncing"; |
|
|
|
qDebug()<< "Finished syncing"; |
|
|
|
isSyncing->storeRelaxed(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"); |
|
|
|
if (isSyncing != nullptr && isSyncing->loadRelaxed()) { |
|
|
|
DEBUG("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"); |
|
|
|
if (!isSyncing || !isSyncing->loadRelaxed()) { |
|
|
|
DEBUG("Syncing complete or isSyncing is null, stopping timer"); |
|
|
|
syncTimer->stop(); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// rethrow exception so loadProgress can catch
|
|
|
|
// it and retry the entire ShowProgress() function again
|
|
|
|
throw new std::runtime_error(std::string("syncstatus failed")); |
|
|
|
} |
|
|
|
DEBUG("Checking sync status"); |
|
|
|
try { |
|
|
|
connection->doRPC("syncstatus", "", [=](json reply) { |
|
|
|
if (isSyncing && 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>(); |
|
|
|
DEBUG("Sync status: " << synced << " / " << total); |
|
|
|
me->showInformation( |
|
|
|
"Syncing... " + QString::number(synced) + " / " + QString::number(total) |
|
|
|
); |
|
|
|
} |
|
|
|
}, [=](QString err) { |
|
|
|
DEBUG("Sync status error: " << err); |
|
|
|
config->server = Settings::getRandomServer(); |
|
|
|
DEBUG("Changed server to " << config->server); |
|
|
|
}); |
|
|
|
} catch (const std::exception& e) { |
|
|
|
DEBUG("Exception caught in syncstatus: " << e.what()); |
|
|
|
throw; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
int interval = 1*1000; |
|
|
|
int interval = 1 * 1000; |
|
|
|
syncTimer->setInterval(interval); |
|
|
|
syncTimer->start(); |
|
|
|
DEBUG("Start sync timer with interval=" << interval); |
|
|
|
DEBUG("Sync timer started with interval=" << interval); |
|
|
|
} |
|
|
|
|
|
|
|
void ConnectionLoader::doAutoConnect() |
|
|
@ -165,10 +163,10 @@ void ConnectionLoader::doAutoConnect() |
|
|
|
auto config = std::shared_ptr<ConnectionConfig>(new ConnectionConfig()); |
|
|
|
config->dangerous = false; |
|
|
|
config->server = Settings::getInstance()->getSettings().server; |
|
|
|
DEBUG(" server=" << config->server); |
|
|
|
DEBUG("Creating connection with server=" << config->server); |
|
|
|
|
|
|
|
// Initialize the library
|
|
|
|
DEBUG("Attempting to initialize library with "<< config->server); |
|
|
|
DEBUG("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())) { |
|
|
@ -219,22 +217,21 @@ void ConnectionLoader::doAutoConnect() |
|
|
|
|
|
|
|
auto connection = makeConnection(config); |
|
|
|
auto me = this; |
|
|
|
qDebug() << __func__ << ": server=" << config->server |
|
|
|
qDebug() << __func__ << ": server=" << config->server |
|
|
|
<< " connection=" << connection << " me=" << me << Qt::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."); |
|
|
|
connection->setInfo(reply); |
|
|
|
DEBUG("getting Connection reply"); |
|
|
|
isSyncing = new QAtomicInteger<bool>(); |
|
|
|
isSyncing->storeRelaxed(true); |
|
|
|
DEBUG("isSyncing"); |
|
|
|
DEBUG("isSyncing set to true"); |
|
|
|
|
|
|
|
// Do a sync at startup
|
|
|
|
syncTimer = new QTimer(main); |
|
|
|
DEBUG("Beginning sync"); |
|
|
|
DEBUG("Beginning sync at startup"); |
|
|
|
connection->doRPC("sync", "", [=](auto) { |
|
|
|
qDebug()<<"finished syncing startup"; |
|
|
|
isSyncing->storeRelaxed(false); |
|
|
@ -244,25 +241,19 @@ void ConnectionLoader::doAutoConnect() |
|
|
|
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);
|
|
|
|
// Attempt to retry sync RPC with a delay
|
|
|
|
QTimer::singleShot(5000, [=]() { // 5-second delay
|
|
|
|
connection->doRPC("sync", "", [=](auto) mutable { |
|
|
|
qDebug()<<"sync success with server=" << config->server; |
|
|
|
failed = false; |
|
|
|
isSyncing->storeRelaxed(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" ); |
|
|
|
DEBUG("sync failed with server=" << config->server << " . retrying after delay"); |
|
|
|
}); |
|
|
|
} while (failed); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
// While it is syncing, we'll show the status updates while it is alive.
|
|
|
|