-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2469 from KomodoPlatform/enhance/time-sync-warning
Enhance/time sync warning
- Loading branch information
Showing
7 changed files
with
232 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/core/atomicdex/services/sync/timesync.checker.service.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/****************************************************************************** | ||
* Copyright © 2013-2024 The Komodo Platform Developers. * | ||
* * | ||
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * | ||
* the top-level directory of this distribution for the individual copyright * | ||
* holder information and the developer policies on copyright and licensing. * | ||
* * | ||
* Unless otherwise agreed in a custom licensing agreement, no part of the * | ||
* Komodo Platform software, including this file may be copied, modified, * | ||
* propagated or distributed except according to the terms contained in the * | ||
* LICENSE file * | ||
* * | ||
* Removal or modification of this copyright notice is prohibited. * | ||
* * | ||
******************************************************************************/ | ||
|
||
#include <nlohmann/json.hpp> | ||
#include "atomicdex/services/sync/timesync.checker.service.hpp" | ||
#include "atomicdex/utilities/cpprestsdk.utilities.hpp" | ||
|
||
namespace | ||
{ | ||
constexpr const char* g_timesync_endpoint = "https://worldtimeapi.org"; | ||
web::http::client::http_client_config g_timesync_cfg{[]() | ||
{ | ||
web::http::client::http_client_config cfg; | ||
cfg.set_validate_certificates(false); | ||
cfg.set_timeout(std::chrono::seconds(5)); | ||
return cfg; | ||
}()}; | ||
t_http_client_ptr g_timesync_client = std::make_unique<web::http::client::http_client>(FROM_STD_STR(g_timesync_endpoint), g_timesync_cfg); | ||
pplx::cancellation_token_source g_synctoken_source; | ||
|
||
pplx::task<web::http::http_response> | ||
async_fetch_timesync() | ||
{ | ||
web::http::http_request req; | ||
req.set_method(web::http::methods::GET); | ||
req.set_request_uri(FROM_STD_STR("api/timezone/UTC")); | ||
return g_timesync_client->request(req, g_synctoken_source.get_token()); | ||
} | ||
|
||
bool get_timesync_info_rpc(web::http::http_response resp_http) | ||
{ | ||
using namespace std::string_literals; | ||
nlohmann::json resp; | ||
bool sync_ok = false; | ||
std::string resp_str = TO_STD_STR(resp_http.extract_string(true).get()); | ||
if (resp_http.status_code() != 200) | ||
{ | ||
SPDLOG_ERROR("Cannot reach the endpoint [{}]: {}", g_timesync_endpoint); | ||
} | ||
else | ||
{ | ||
resp = nlohmann::json::parse(resp_str); | ||
int64_t epoch_ts = resp["unixtime"]; | ||
int64_t current_ts = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count(); | ||
int64_t ts_diff = epoch_ts - current_ts; | ||
if (abs(ts_diff) < 60) | ||
{ | ||
sync_ok = true; | ||
} | ||
} | ||
return sync_ok; | ||
} | ||
} // namespace | ||
|
||
|
||
namespace atomic_dex | ||
{ | ||
timesync_checker_service::timesync_checker_service(entt::registry& registry, QObject* parent) : QObject(parent), system(registry) | ||
{ | ||
m_timesync_clock = std::chrono::high_resolution_clock::now(); | ||
m_timesync_status = true; | ||
fetch_timesync_status(); | ||
} | ||
|
||
void timesync_checker_service::update() | ||
{ | ||
using namespace std::chrono_literals; | ||
|
||
int64_t m_timesync_clock_ts = std::chrono::duration_cast<std::chrono::seconds>(m_timesync_clock.time_since_epoch()).count(); | ||
int64_t now_ts = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count(); | ||
int64_t ts_diff = now_ts - m_timesync_clock_ts; | ||
if (abs(ts_diff) >= 60) | ||
{ | ||
fetch_timesync_status(); | ||
m_timesync_clock = std::chrono::high_resolution_clock::now(); | ||
} | ||
} | ||
|
||
void timesync_checker_service::fetch_timesync_status() | ||
{ | ||
if (is_timesync_fetching) | ||
{ | ||
return; | ||
} | ||
is_timesync_fetching = true; | ||
emit isTimesyncFetchingChanged(); | ||
async_fetch_timesync() | ||
.then([this](web::http::http_response resp) { | ||
this->m_timesync_status = get_timesync_info_rpc(resp); | ||
emit timesyncInfoChanged(); | ||
}) | ||
.then(&handle_exception_pplx_task); | ||
is_timesync_fetching = false; | ||
emit isTimesyncFetchingChanged(); | ||
|
||
} | ||
|
||
bool timesync_checker_service::get_timesync_info() const | ||
{ | ||
return *m_timesync_status; | ||
} | ||
|
||
} // namespace atomic_dex | ||
|
||
|
60 changes: 60 additions & 0 deletions
60
src/core/atomicdex/services/sync/timesync.checker.service.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/****************************************************************************** | ||
* Copyright © 2013-2024 The Komodo Platform Developers. * | ||
* * | ||
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * | ||
* the top-level directory of this distribution for the individual copyright * | ||
* holder information and the developer policies on copyright and licensing. * | ||
* * | ||
* Unless otherwise agreed in a custom licensing agreement, no part of the * | ||
* Komodo Platform software, including this file may be copied, modified, * | ||
* propagated or distributed except according to the terms contained in the * | ||
* LICENSE file * | ||
* * | ||
* Removal or modification of this copyright notice is prohibited. * | ||
* * | ||
******************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include <QObject> | ||
#include <QVariant> | ||
|
||
#include <boost/thread/synchronized_value.hpp> | ||
#include <nlohmann/json_fwd.hpp> | ||
|
||
#include <antara/gaming/ecs/system.hpp> | ||
|
||
namespace atomic_dex | ||
{ | ||
class timesync_checker_service final : public QObject, public ag::ecs::pre_update_system<timesync_checker_service> | ||
{ | ||
Q_OBJECT | ||
|
||
Q_PROPERTY(QVariant timesyncInfo READ get_timesync_info NOTIFY timesyncInfoChanged) | ||
Q_PROPERTY(bool isTimesyncFetching READ get_is_timesync_fetching NOTIFY isTimesyncFetchingChanged) | ||
|
||
using t_timesync_time_point = std::chrono::high_resolution_clock::time_point; | ||
using t_bool_synchronized = boost::synchronized_value<bool>; | ||
|
||
t_bool_synchronized m_timesync_status; | ||
t_timesync_time_point m_timesync_clock; | ||
t_bool_synchronized is_timesync_fetching; | ||
|
||
void fetch_timesync_status(); | ||
|
||
public: | ||
explicit timesync_checker_service(entt::registry& registry, QObject* parent = nullptr); | ||
~timesync_checker_service() final = default; | ||
|
||
void update() final; | ||
|
||
[[nodiscard]] bool get_timesync_info() const; | ||
[[nodiscard]] bool get_is_timesync_fetching() const noexcept { return *is_timesync_fetching; } | ||
|
||
signals: | ||
void timesyncInfoChanged(); | ||
void isTimesyncFetchingChanged(); | ||
}; | ||
} // namespace atomic_dex | ||
|
||
REFL_AUTO(type(atomic_dex::timesync_checker_service)) |