Skip to content

Commit

Permalink
Rework a bit load checkpoints from dns (#162)
Browse files Browse the repository at this point in the history
Replace async dns checkpoints getter
  • Loading branch information
aivve authored Oct 4, 2021
1 parent f03c18b commit 20c951b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
28 changes: 10 additions & 18 deletions src/Checkpoints/Checkpoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
#include <sstream>
#include <vector>
#include <iterator>
#include <mutex>
#include <chrono>
#include <thread>
#include <condition_variable>
#include <future>

#include "Checkpoints.h"
#include "../CryptoNoteConfig.h"
Expand Down Expand Up @@ -152,35 +151,28 @@ std::vector<uint32_t> Checkpoints::getCheckpointHeights() const {
//---------------------------------------------------------------------------
bool Checkpoints::load_checkpoints_from_dns()
{
std::mutex m;
std::condition_variable cv;
std::string domain(CryptoNote::DNS_CHECKPOINTS_HOST);
std::vector<std::string>records;
bool res = true;
auto start = std::chrono::steady_clock::now();
logger(Logging::DEBUGGING) << "Fetching DNS checkpoint records from " << domain;

try {
std::thread t([&cv, &domain, &res, &records]()
{
auto future = std::async(std::launch::async, [this, &res, &domain, &records]() {
res = Common::fetch_dns_txt(domain, records);
cv.notify_one();
});

t.detach();
std::future_status status;

{
std::unique_lock<std::mutex> l(m);
if (cv.wait_for(l, std::chrono::milliseconds(400)) == std::cv_status::timeout) {
logger(Logging::DEBUGGING) << "Timeout lookup DNS checkpoint records from " << domain;
return false;
}
}
status = future.wait_for(std::chrono::milliseconds(200));

if (!res) {
logger(Logging::DEBUGGING) << "Failed to lookup DNS checkpoint records from " + domain;
if (status == std::future_status::timeout) {
logger(Logging::DEBUGGING) << "Timeout lookup DNS checkpoint records from " << domain;
return false;
}
else if (status == std::future_status::ready) {
future.get();
}
}
catch (std::runtime_error& e) {
logger(Logging::DEBUGGING) << e.what();
Expand Down Expand Up @@ -209,7 +201,7 @@ bool Checkpoints::load_checkpoints_from_dns()
logger(DEBUGGING) << "Checkpoint already exists for height: " << height << ". Ignoring DNS checkpoint.";
} else {
add_checkpoint(height, hash_str);
logger(DEBUGGING) << "Added DNS checkpoint: " << height_str << ":" << hash_str;
logger(DEBUGGING) << "Added DNS checkpoint: " << height_str << ":" << hash_str;
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/Checkpoints/Checkpoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
// along with Karbo. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <map>
#include <mutex>

#include <CryptoNoteCore/CryptoNoteBasicImpl.h>
#include <Logging/LoggerRef.h>

Expand All @@ -29,6 +32,21 @@ namespace CryptoNote
public:
Checkpoints(Logging::ILogger& log);

Checkpoints& operator=(Checkpoints const& other)
{
if (&other != this)
{
// lock both objects
std::unique_lock<std::mutex> lock_this(m_mutex, std::defer_lock);
std::unique_lock<std::mutex> lock_other(other.m_mutex, std::defer_lock);
std::lock(lock_this, lock_other); // ensure no deadlock
m_points = other.m_points;
logger = other.logger;
}

return *this;
}

bool add_checkpoint(uint32_t height, const std::string& hash_str);
bool load_checkpoints_from_file(const std::string& fileName);
bool is_in_checkpoint_zone(uint32_t height) const;
Expand All @@ -43,5 +61,6 @@ namespace CryptoNote
private:
std::map<uint32_t, Crypto::Hash> m_points;
Logging::LoggerRef logger;
mutable std::mutex m_mutex;
};
}

0 comments on commit 20c951b

Please sign in to comment.