aboutsummaryrefslogtreecommitdiff
path: root/src/cryptonote_core/blockchain_storage.cpp
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2014-10-02 16:48:01 +0200
committerRiccardo Spagni <ric@spagni.net>2014-10-02 16:48:05 +0200
commit1cf22b27a6b95e1e44c35157f5c5ccb73efec9da (patch)
tree770a470c84cd8c7c7ef816dd2c34078d5aebbc7a /src/cryptonote_core/blockchain_storage.cpp
parent0e777d0e6587b41b139f8c8105c3855994584e95 (diff)
parentc0bdd511c3309e78488051f6a21b0b30ed25af44 (diff)
downloadmonzero-core-1cf22b27a6b95e1e44c35157f5c5ccb73efec9da.tar.gz
monzero-core-1cf22b27a6b95e1e44c35157f5c5ccb73efec9da.tar.xz
monzero-core-1cf22b27a6b95e1e44c35157f5c5ccb73efec9da.zip
Merge pull request #165
c0bdd51 Daemon should now exit on conflicting checkpoints (Thomas Winget) f0b4138 various changes to runtime checkpoint updating (Thomas Winget) 7568f89 Fixed segfault with checkpoints loading (Thomas Winget) b261d92 DNS checkpoint updating added, and daemon flag to enforce them (Thomas Winget) 30caebf reload checkpoints file every ~hr and print if any fail (Thomas Winget) 0e14491 updated DNSResolver/things that use it for DNSSEC (Thomas Winget) 6f2c2e1 Adding an identical existing checkpoint should not error (Thomas Winget)
Diffstat (limited to 'src/cryptonote_core/blockchain_storage.cpp')
-rw-r--r--src/cryptonote_core/blockchain_storage.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/cryptonote_core/blockchain_storage.cpp b/src/cryptonote_core/blockchain_storage.cpp
index e664a39c5..6f1b4121c 100644
--- a/src/cryptonote_core/blockchain_storage.cpp
+++ b/src/cryptonote_core/blockchain_storage.cpp
@@ -47,6 +47,7 @@
#include "common/boost_serialization_helper.h"
#include "warnings.h"
#include "crypto/hash.h"
+#include "cryptonote_core/checkpoints_create.h"
//#include "serialization/json_archive.h"
using namespace cryptonote;
@@ -441,6 +442,13 @@ difficulty_type blockchain_storage::get_difficulty_for_next_block()
bool blockchain_storage::rollback_blockchain_switching(std::list<block>& original_chain, size_t rollback_height)
{
CRITICAL_REGION_LOCAL(m_blockchain_lock);
+
+ // fail if rollback_height passed is too high
+ if (rollback_height > m_blocks.size())
+ {
+ return true;
+ }
+
//remove failed subchain
for(size_t i = m_blocks.size()-1; i >=rollback_height; i--)
{
@@ -1773,3 +1781,75 @@ bool blockchain_storage::add_new_block(const block& bl_, block_verification_cont
return handle_block_to_main_chain(bl, id, bvc);
}
+//------------------------------------------------------------------
+void blockchain_storage::check_against_checkpoints(checkpoints& points, bool enforce)
+{
+ const auto& pts = points.get_points();
+
+ for (const auto& pt : pts)
+ {
+ // if the checkpoint is for a block we don't have yet, move on
+ if (pt.first >= m_blocks.size())
+ {
+ continue;
+ }
+
+ if (!m_checkpoints.check_block(pt.first, get_block_hash(m_blocks[pt.first].bl)))
+ {
+ // if asked to enforce checkpoints, roll back to a couple of blocks before the checkpoint
+ if (enforce)
+ {
+ LOG_ERROR("Checkpoint failed when adding new checkpoints, rolling back!");
+ std::list<block> empty;
+ rollback_blockchain_switching(empty, pt.first - 2);
+ }
+ else
+ {
+ LOG_ERROR("Checkpoint failed when adding new checkpoints, this could be very bad.");
+ }
+ }
+ }
+}
+//------------------------------------------------------------------
+// returns false if any of the checkpoints loading returns false.
+// That should happen only if a checkpoint is added that conflicts
+// with an existing checkpoint.
+bool blockchain_storage::update_checkpoints(const std::string& file_path, bool check_dns)
+{
+ if (!cryptonote::load_checkpoints_from_json(m_checkpoints, file_path))
+ {
+ return false;
+ }
+
+ // if we're checking both dns and json, load checkpoints from dns.
+ // if we're not hard-enforcing dns checkpoints, handle accordingly
+ if (m_enforce_dns_checkpoints && check_dns)
+ {
+ if (!cryptonote::load_checkpoints_from_dns(m_checkpoints))
+ {
+ return false;
+ }
+ }
+ else if (check_dns)
+ {
+ checkpoints dns_points;
+ cryptonote::load_checkpoints_from_dns(dns_points);
+ if (m_checkpoints.check_for_conflicts(dns_points))
+ {
+ check_against_checkpoints(dns_points, false);
+ }
+ else
+ {
+ LOG_PRINT_L0("One or more checkpoints fetched from DNS conflicted with existing checkpoints!");
+ }
+ }
+
+ check_against_checkpoints(m_checkpoints, true);
+
+ return true;
+}
+//------------------------------------------------------------------
+void blockchain_storage::set_enforce_dns_checkpoints(bool enforce_checkpoints)
+{
+ m_enforce_dns_checkpoints = enforce_checkpoints;
+}