Added ability to read chechpoint hashes from json file in data folder

This commit is contained in:
Tomer Konforty 2014-09-24 17:36:04 +03:00 committed by Riccardo Spagni
parent 9956b68b18
commit 06a4578bf2
4 changed files with 65 additions and 0 deletions

View File

@ -93,4 +93,13 @@ namespace cryptonote
uint64_t checkpoint_height = it->first; uint64_t checkpoint_height = it->first;
return checkpoint_height < block_height; return checkpoint_height < block_height;
} }
uint64_t checkpoints::get_max_height()
{
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;
}
} }

View File

@ -44,6 +44,7 @@ namespace cryptonote
bool check_block(uint64_t height, const crypto::hash& h) const; bool check_block(uint64_t height, const crypto::hash& h) const;
bool check_block(uint64_t height, const crypto::hash& h, bool& is_a_checkpoint) const; bool check_block(uint64_t height, const crypto::hash& h, bool& is_a_checkpoint) const;
bool is_alternative_block_allowed(uint64_t blockchain_height, uint64_t block_height) const; bool is_alternative_block_allowed(uint64_t blockchain_height, uint64_t block_height) const;
uint64_t get_max_height();
private: private:
std::map<uint64_t, crypto::hash> m_points; std::map<uint64_t, crypto::hash> m_points;

View File

@ -34,6 +34,24 @@
#include "misc_log_ex.h" #include "misc_log_ex.h"
#define ADD_CHECKPOINT(h, hash) CHECK_AND_ASSERT(checkpoints.add_checkpoint(h, hash), false); #define ADD_CHECKPOINT(h, hash) CHECK_AND_ASSERT(checkpoints.add_checkpoint(h, hash), false);
#define JSON_HASH_FILE_NAME "checkpoints.json"
struct t_hashline
{
uint64_t height;
std::string hash;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(height)
KV_SERIALIZE(hash)
END_KV_SERIALIZE_MAP()
};
struct t_hash_json {
std::vector<t_hashline> hashlines;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(hashlines)
END_KV_SERIALIZE_MAP()
};
namespace cryptonote { namespace cryptonote {
inline bool create_checkpoints(cryptonote::checkpoints& checkpoints) inline bool create_checkpoints(cryptonote::checkpoints& checkpoints)
@ -59,4 +77,36 @@ namespace cryptonote {
return true; return true;
} }
inline bool load_checkpoins_from_json(cryptonote::checkpoints& checkpoints, std::string json_hashfile_fullpath)
{
boost::system::error_code errcode;
if (! (boost::filesystem::exists(json_hashfile_fullpath, errcode)))
{
LOG_PRINT_L0("Blockchain checkpoints file not found");
return true;
}
LOG_PRINT_L0("Adding checkpoints from blockchain hashfile");
uint64_t prev_max_height = checkpoints.get_max_height();
LOG_PRINT_L0("Hard-coded max checkpoint height is " << prev_max_height);
t_hash_json hashes;
epee::serialization::load_t_from_json_file(hashes, json_hashfile_fullpath);
for (std::vector<t_hashline>::const_iterator it = hashes.hashlines.begin(); it != hashes.hashlines.end(); )
{
uint64_t height;
height = it->height;
if (height <= prev_max_height) {
LOG_PRINT_L0("ignoring checkpoint height " << height);
} else {
std::string blockhash = it->hash;
LOG_PRINT_L0("Adding checkpoint height " << height << ", hash=" << blockhash);
ADD_CHECKPOINT(height, blockhash);
}
++it;
}
return true;
}
} }

View File

@ -44,6 +44,7 @@ using namespace epee;
#include "p2p/net_node.h" #include "p2p/net_node.h"
#include "cryptonote_config.h" #include "cryptonote_config.h"
#include "cryptonote_core/checkpoints_create.h" #include "cryptonote_core/checkpoints_create.h"
#include "cryptonote_core/checkpoints.h"
#include "cryptonote_core/cryptonote_core.h" #include "cryptonote_core/cryptonote_core.h"
#include "rpc/core_rpc_server.h" #include "rpc/core_rpc_server.h"
#include "cryptonote_protocol/cryptonote_protocol_handler.h" #include "cryptonote_protocol/cryptonote_protocol_handler.h"
@ -203,6 +204,10 @@ int main(int argc, char* argv[])
cryptonote::checkpoints checkpoints; cryptonote::checkpoints checkpoints;
res = cryptonote::create_checkpoints(checkpoints); res = cryptonote::create_checkpoints(checkpoints);
CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize checkpoints"); CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize checkpoints");
boost::filesystem::path json(JSON_HASH_FILE_NAME);
boost::filesystem::path checkpoint_json_hashfile_fullpath = data_dir / json;
res = cryptonote::load_checkpoins_from_json(checkpoints, checkpoint_json_hashfile_fullpath.string().c_str());
CHECK_AND_ASSERT_MES(res, 1, "Failed to load initial checkpoints");
//create objects and link them //create objects and link them
cryptonote::core ccore(NULL); cryptonote::core ccore(NULL);