diff --git a/lib/src/lib.rs b/lib/src/lib.rs index eb3cf53..f3c4013 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -51,9 +51,6 @@ println!("\nBlake3 Hash: {}", hash1); //let sttring = CString::new(hash1).unwrap(); let e_str = CString::new(format!("{}", hash1)).unwrap(); return e_str.into_raw(); - - - } /// Create a new wallet and return the seed for the newly created wallet. @@ -112,7 +109,6 @@ pub extern fn litelib_initialize_new(dangerous: bool,server: *const c_char) -> * #[no_mangle] pub extern "C" fn litelib_initialize_new_from_phrase(dangerous: bool, server: *const c_char, seed: *const c_char, birthday: u64, number: u64, overwrite: bool) -> *mut c_char { - // Prüfen auf null-Pointer if server.is_null() || seed.is_null() { println!("Server or seed is null"); return ptr::null_mut(); @@ -167,7 +163,6 @@ pub extern "C" fn litelib_initialize_new_from_phrase(dangerous: bool, server: *c return c_str.into_raw(); } - // Initialize a new lightclient and store its value #[no_mangle] pub extern fn litelib_initialize_existing(dangerous: bool, server: *const c_char) -> *mut c_char { @@ -197,7 +192,6 @@ pub extern fn litelib_initialize_existing(dangerous: bool, server: *const c_char // Initialize logging let _ = lightclient.init_logging(); - let lc = Arc::new(lightclient); match LightClient::start_mempool_monitor(lc.clone()) { Ok(_) => {println!("Starting Mempool")}, diff --git a/src/connection.cpp b/src/connection.cpp index e96651a..e430283 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -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(); - isSyncing->storeRelaxed(true); - DEBUG("isSyncing"); + auto me = this; + isSyncing = new QAtomicInteger(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(); - qint64 total = reply["total_blocks"].get(); - 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(); + qint64 total = reply["total_blocks"].get(); + 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(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(); 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. diff --git a/src/controller.cpp b/src/controller.cpp index 7a6e957..8dd175a 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -247,8 +247,6 @@ void Controller::fillTxJsonParams(json& allRecepients, Tx tx) } } - - void Controller::noConnection() { qDebug()<< __func__; diff --git a/src/firsttimewizard.cpp b/src/firsttimewizard.cpp index 857e77a..6574972 100644 --- a/src/firsttimewizard.cpp +++ b/src/firsttimewizard.cpp @@ -759,7 +759,6 @@ if (reply.toUpper().trimmed() != "OK") { qDebug() << __func__ << ": Null response on retry from litelib_initialize_new_from_phrase"; } } - // 4. Finally attempt to save the wallet { QString reply = "";