You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

alert.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2015 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_ALERT_H
  6. #define BITCOIN_ALERT_H
  7. #include "serialize.h"
  8. #include "sync.h"
  9. #include <map>
  10. #include <set>
  11. #include <stdint.h>
  12. #include <string>
  13. class CAlert;
  14. class CNode;
  15. class uint256;
  16. extern std::map<uint256, CAlert> mapAlerts;
  17. extern CCriticalSection cs_mapAlerts;
  18. /** Alerts are for notifying old versions if they become too obsolete and
  19. * need to upgrade. The message is displayed in the status bar.
  20. * Alert messages are broadcast as a vector of signed data. Unserializing may
  21. * not read the entire buffer if the alert is for a newer version, but older
  22. * versions can still relay the original data.
  23. */
  24. class CUnsignedAlert
  25. {
  26. public:
  27. int nVersion;
  28. int64_t nRelayUntil; // when newer nodes stop relaying to newer nodes
  29. int64_t nExpiration;
  30. int nID;
  31. int nCancel;
  32. std::set<int> setCancel;
  33. int nMinVer; // lowest version inclusive
  34. int nMaxVer; // highest version inclusive
  35. std::set<std::string> setSubVer; // empty matches all
  36. int nPriority;
  37. // Actions
  38. std::string strComment;
  39. std::string strStatusBar;
  40. std::string strReserved;
  41. ADD_SERIALIZE_METHODS;
  42. template <typename Stream, typename Operation>
  43. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  44. READWRITE(this->nVersion);
  45. nVersion = this->nVersion;
  46. READWRITE(nRelayUntil);
  47. READWRITE(nExpiration);
  48. READWRITE(nID);
  49. READWRITE(nCancel);
  50. READWRITE(setCancel);
  51. READWRITE(nMinVer);
  52. READWRITE(nMaxVer);
  53. READWRITE(setSubVer);
  54. READWRITE(nPriority);
  55. READWRITE(LIMITED_STRING(strComment, 65536));
  56. READWRITE(LIMITED_STRING(strStatusBar, 256));
  57. READWRITE(LIMITED_STRING(strReserved, 256));
  58. }
  59. void SetNull();
  60. std::string ToString() const;
  61. };
  62. /** An alert is a combination of a serialized CUnsignedAlert and a signature. */
  63. class CAlert : public CUnsignedAlert
  64. {
  65. public:
  66. std::vector<unsigned char> vchMsg;
  67. std::vector<unsigned char> vchSig;
  68. CAlert()
  69. {
  70. SetNull();
  71. }
  72. ADD_SERIALIZE_METHODS;
  73. template <typename Stream, typename Operation>
  74. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  75. READWRITE(vchMsg);
  76. READWRITE(vchSig);
  77. }
  78. void SetNull();
  79. bool IsNull() const;
  80. uint256 GetHash() const;
  81. bool IsInEffect() const;
  82. bool Cancels(const CAlert& alert) const;
  83. bool AppliesTo(int nVersion, const std::string& strSubVerIn) const;
  84. bool AppliesToMe() const;
  85. bool RelayTo(CNode* pnode) const;
  86. bool CheckSignature(const std::vector<unsigned char>& alertKey) const;
  87. bool ProcessAlert(const std::vector<unsigned char>& alertKey, bool fThread = true); // fThread means run -alertnotify in a free-running thread
  88. static void Notify(const std::string& strMessage, bool fThread);
  89. /*
  90. * Get copy of (active) alert object by hash. Returns a null alert if it is not found.
  91. */
  92. static CAlert getAlertByHash(const uint256 &hash);
  93. };
  94. #endif // BITCOIN_ALERT_H