2015-12-31 07:39:56 +01:00
|
|
|
// Copyright (c) 2014-2016, The Monero Project
|
2014-07-23 15:03:52 +02:00
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
// materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
|
|
// used to endorse or promote products derived from this software without specific
|
|
|
|
// prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
2014-03-03 23:07:58 +01:00
|
|
|
|
|
|
|
#include "include_base_utils.h"
|
|
|
|
using namespace epee;
|
|
|
|
|
|
|
|
#include "checkpoints.h"
|
|
|
|
|
|
|
|
namespace cryptonote
|
|
|
|
{
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
checkpoints::checkpoints()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool checkpoints::add_checkpoint(uint64_t height, const std::string& hash_str)
|
|
|
|
{
|
|
|
|
crypto::hash h = null_hash;
|
|
|
|
bool r = epee::string_tools::parse_tpod_from_hex_string(hash_str, h);
|
2014-09-25 00:43:03 +02:00
|
|
|
CHECK_AND_ASSERT_MES(r, false, "Failed to parse checkpoint hash string into binary representation!");
|
|
|
|
|
|
|
|
// return false if adding at a height we already have AND the hash is different
|
|
|
|
if (m_points.count(height))
|
|
|
|
{
|
|
|
|
CHECK_AND_ASSERT_MES(h == m_points[height], false, "Checkpoint at given height already exists, and hash for new checkpoint was different!");
|
|
|
|
}
|
2014-03-03 23:07:58 +01:00
|
|
|
m_points[height] = h;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool checkpoints::is_in_checkpoint_zone(uint64_t height) const
|
|
|
|
{
|
|
|
|
return !m_points.empty() && (height <= (--m_points.end())->first);
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2014-05-25 19:06:40 +02:00
|
|
|
bool checkpoints::check_block(uint64_t height, const crypto::hash& h, bool& is_a_checkpoint) const
|
2014-03-03 23:07:58 +01:00
|
|
|
{
|
|
|
|
auto it = m_points.find(height);
|
2014-05-25 19:06:40 +02:00
|
|
|
is_a_checkpoint = it != m_points.end();
|
|
|
|
if(!is_a_checkpoint)
|
2014-03-03 23:07:58 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if(it->second == h)
|
|
|
|
{
|
2014-09-30 08:56:03 +02:00
|
|
|
LOG_PRINT_GREEN("CHECKPOINT PASSED FOR HEIGHT " << height << " " << h, LOG_LEVEL_1);
|
2014-03-03 23:07:58 +01:00
|
|
|
return true;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
LOG_ERROR("CHECKPOINT FAILED FOR HEIGHT " << height << ". EXPECTED HASH: " << it->second << ", FETCHED HASH: " << h);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-25 19:06:40 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool checkpoints::check_block(uint64_t height, const crypto::hash& h) const
|
|
|
|
{
|
|
|
|
bool ignored;
|
|
|
|
return check_block(height, h, ignored);
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2015-01-09 11:56:51 +01:00
|
|
|
// this basically says if the blockchain is smaller than the first
|
|
|
|
// checkpoint then alternate blocks are allowed. Alternatively, if the
|
|
|
|
// last checkpoint *before* the end of the current chain is also before
|
|
|
|
// the block to be added, then this is fine.
|
2014-05-25 19:06:40 +02:00
|
|
|
bool checkpoints::is_alternative_block_allowed(uint64_t blockchain_height, uint64_t block_height) const
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
uint64_t checkpoint_height = it->first;
|
|
|
|
return checkpoint_height < block_height;
|
|
|
|
}
|
2014-09-25 07:15:28 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2014-12-06 23:46:51 +01:00
|
|
|
uint64_t checkpoints::get_max_height() const
|
2014-09-24 16:36:04 +02:00
|
|
|
{
|
|
|
|
std::map< uint64_t, crypto::hash >::const_iterator highest =
|
|
|
|
std::max_element( m_points.begin(), m_points.end(),
|
|
|
|
( boost::bind(&std::map< uint64_t, crypto::hash >::value_type::first, _1) <
|
|
|
|
boost::bind(&std::map< uint64_t, crypto::hash >::value_type::first, _2 ) ) );
|
|
|
|
return highest->first;
|
|
|
|
}
|
2014-09-25 07:15:28 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2014-12-06 23:46:51 +01:00
|
|
|
const std::map<uint64_t, crypto::hash>& checkpoints::get_points() const
|
2014-09-25 07:15:28 +02:00
|
|
|
{
|
|
|
|
return m_points;
|
|
|
|
}
|
2014-09-24 16:36:04 +02:00
|
|
|
|
2014-12-06 23:46:51 +01:00
|
|
|
bool checkpoints::check_for_conflicts(const checkpoints& other) const
|
2014-09-29 22:30:47 +02:00
|
|
|
{
|
|
|
|
for (auto& pt : other.get_points())
|
|
|
|
{
|
|
|
|
if (m_points.count(pt.first))
|
|
|
|
{
|
2014-12-06 23:46:51 +01:00
|
|
|
CHECK_AND_ASSERT_MES(pt.second == m_points.at(pt.first), false, "Checkpoint at given height already exists, and hash for new checkpoint was different!");
|
2014-09-29 22:30:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-03 23:07:58 +01:00
|
|
|
}
|