danicoin/src/CryptoNoteCore/Checkpoints.cpp

74 lines
2.5 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
#include "Checkpoints.h"
2015-05-27 12:08:46 +00:00
#include "Common/StringTools.h"
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
using namespace Logging;
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
//---------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
Checkpoints::Checkpoints(Logging::ILogger &log) : logger(log, "checkpoints") {}
2015-05-27 12:08:46 +00:00
//---------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool Checkpoints::add_checkpoint(uint32_t height, const std::string &hash_str) {
Crypto::Hash h = NULL_HASH;
2015-05-27 12:08:46 +00:00
if (!Common::podFromHex(hash_str, h)) {
logger(ERROR) << "WRONG HASH IN CHECKPOINTS!!!";
return false;
2014-03-03 22:07:58 +00:00
}
2015-05-27 12:08:46 +00:00
if (!(0 == m_points.count(height))) {
logger(ERROR) << "WRONG HASH IN CHECKPOINTS!!!";
return false;
2014-05-15 16:40:40 +00:00
}
2015-05-27 12:08:46 +00:00
m_points[height] = h;
return true;
}
//---------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool Checkpoints::is_in_checkpoint_zone(uint32_t height) const {
2015-05-27 12:08:46 +00:00
return !m_points.empty() && (height <= (--m_points.end())->first);
}
//---------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool Checkpoints::check_block(uint32_t height, const Crypto::Hash &h,
2015-05-27 12:08:46 +00:00
bool &is_a_checkpoint) const {
auto it = m_points.find(height);
is_a_checkpoint = it != m_points.end();
if (!is_a_checkpoint)
return true;
2014-05-15 16:40:40 +00:00
2015-05-27 12:08:46 +00:00
if (it->second == h) {
logger(Logging::INFO, Logging::GREEN)
<< "CHECKPOINT PASSED FOR HEIGHT " << height << " " << h;
return true;
} else {
logger(Logging::ERROR) << "CHECKPOINT FAILED FOR HEIGHT " << height
<< ". EXPECTED HASH: " << it->second
<< ", FETCHED HASH: " << h;
return false;
2014-05-15 16:40:40 +00:00
}
2014-03-03 22:07:58 +00:00
}
2015-05-27 12:08:46 +00:00
//---------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool Checkpoints::check_block(uint32_t height, const Crypto::Hash &h) const {
2015-05-27 12:08:46 +00:00
bool ignored;
return check_block(height, h, ignored);
}
//---------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool Checkpoints::is_alternative_block_allowed(uint32_t blockchain_height,
uint32_t block_height) const {
2015-05-27 12:08:46 +00:00
if (0 == block_height)
return false;
auto it = m_points.upper_bound(blockchain_height);
// Is blockchain_height before the first checkpoint?
if (it == m_points.begin())
return true;
--it;
2015-07-30 15:22:07 +00:00
uint32_t checkpoint_height = it->first;
2015-05-27 12:08:46 +00:00
return checkpoint_height < block_height;
}
}