checkpoints.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef ECOIN_CHECKPOINT_H
  3. #define ECOIN_CHECKPOINT_H
  4. #include <map>
  5. #include "net.h"
  6. #include "util.h"
  7. #define CHECKPOINT_MAX_SPAN (60) // max 1 hour before latest block
  8. #ifdef WIN32
  9. #undef STRICT
  10. #undef PERMISSIVE
  11. #undef ADVISORY
  12. #endif
  13. class uint256;
  14. class CBlockIndex;
  15. class CSyncCheckpoint;
  16. namespace Checkpoints
  17. {
  18. enum CPMode
  19. {
  20. // Scrict checkpoints policy, perform conflicts verification and resolve conflicts
  21. STRICT = 0,
  22. // Advisory checkpoints policy, perform conflicts verification but don't try to resolve them
  23. ADVISORY = 1,
  24. // Permissive checkpoints policy, don't perform any checking
  25. PERMISSIVE = 2
  26. };
  27. // Returns true if block passes checkpoint checks
  28. bool CheckHardened(int nHeight, const uint256& hash);
  29. // Return conservative estimate of total number of blocks, 0 if unknown
  30. int GetTotalBlocksEstimate();
  31. // Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
  32. CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
  33. // Returns last checkpoint timestamp
  34. int GetLastCheckpointTime();
  35. extern uint256 hashSyncCheckpoint;
  36. extern CSyncCheckpoint checkpointMessage;
  37. extern uint256 hashInvalidCheckpoint;
  38. extern CCriticalSection cs_hashSyncCheckpoint;
  39. CBlockIndex* GetLastSyncCheckpoint();
  40. bool WriteSyncCheckpoint(const uint256& hashCheckpoint);
  41. bool AcceptPendingSyncCheckpoint();
  42. uint256 AutoSelectSyncCheckpoint();
  43. bool CheckSync(const uint256& hashBlock, const CBlockIndex* pindexPrev);
  44. bool WantedByPendingSyncCheckpoint(uint256 hashBlock);
  45. bool ResetSyncCheckpoint();
  46. void AskForPendingSyncCheckpoint(CNode* pfrom);
  47. bool SetCheckpointPrivKey(std::string strPrivKey);
  48. bool SendSyncCheckpoint(uint256 hashCheckpoint);
  49. bool IsMatureSyncCheckpoint();
  50. }
  51. // ecoin: synchronized checkpoint
  52. class CUnsignedSyncCheckpoint
  53. {
  54. public:
  55. int nVersion;
  56. uint256 hashCheckpoint; // checkpoint block
  57. IMPLEMENT_SERIALIZE
  58. (
  59. READWRITE(this->nVersion);
  60. nVersion = this->nVersion;
  61. READWRITE(hashCheckpoint);
  62. )
  63. void SetNull()
  64. {
  65. nVersion = 1;
  66. hashCheckpoint = 0;
  67. }
  68. std::string ToString() const
  69. {
  70. return strprintf(
  71. "CSyncCheckpoint(\n"
  72. " nVersion = %d\n"
  73. " hashCheckpoint = %s\n"
  74. ")\n",
  75. nVersion,
  76. hashCheckpoint.ToString().c_str());
  77. }
  78. void print() const
  79. {
  80. printf("%s", ToString().c_str());
  81. }
  82. };
  83. class CSyncCheckpoint : public CUnsignedSyncCheckpoint
  84. {
  85. public:
  86. static const std::string strMasterPubKey;
  87. static std::string strMasterPrivKey;
  88. std::vector<unsigned char> vchMsg;
  89. std::vector<unsigned char> vchSig;
  90. CSyncCheckpoint()
  91. {
  92. SetNull();
  93. }
  94. IMPLEMENT_SERIALIZE
  95. (
  96. READWRITE(vchMsg);
  97. READWRITE(vchSig);
  98. )
  99. void SetNull()
  100. {
  101. CUnsignedSyncCheckpoint::SetNull();
  102. vchMsg.clear();
  103. vchSig.clear();
  104. }
  105. bool IsNull() const
  106. {
  107. return (hashCheckpoint == 0);
  108. }
  109. uint256 GetHash() const
  110. {
  111. return SerializeHash(*this);
  112. }
  113. bool RelayTo(CNode* pnode) const
  114. {
  115. // returns true if wasn't already sent
  116. if (pnode->hashCheckpointKnown != hashCheckpoint)
  117. {
  118. pnode->hashCheckpointKnown = hashCheckpoint;
  119. pnode->PushMessage("checkpoint", *this);
  120. return true;
  121. }
  122. return false;
  123. }
  124. bool CheckSignature();
  125. bool ProcessSyncCheckpoint(CNode* pfrom);
  126. };
  127. #endif