Back to home page

Bitcoin sources

 
 

    


0001 // Copyright (c) 2010 Satoshi Nakamoto
0002 // Copyright (c) 2009-2013 The Bitcoin developers
0003 // Distributed under the MIT/X11 software license, see the accompanying
0004 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
0005 
0006 #ifndef BITCOIN_ALERT_H
0007 #define BITCOIN_ALERT_H
0008 
0009 #include "serialize.h"
0010 #include "sync.h"
0011 
0012 #include <map>
0013 #include <set>
0014 #include <stdint.h>
0015 #include <string>
0016 
0017 class CAlert;
0018 class CNode;
0019 class uint256;
0020 
0021 extern std::map<uint256, CAlert> mapAlerts;
0022 extern CCriticalSection cs_mapAlerts;
0023 
0024 /** Alerts are for notifying old versions if they become too obsolete and
0025  * need to upgrade.  The message is displayed in the status bar.
0026  * Alert messages are broadcast as a vector of signed data.  Unserializing may
0027  * not read the entire buffer if the alert is for a newer version, but older
0028  * versions can still relay the original data.
0029  */
0030 class CUnsignedAlert
0031 {
0032 public:
0033     int nVersion;
0034     int64_t nRelayUntil;      // when newer nodes stop relaying to newer nodes
0035     int64_t nExpiration;
0036     int nID;
0037     int nCancel;
0038     std::set<int> setCancel;
0039     int nMinVer;            // lowest version inclusive
0040     int nMaxVer;            // highest version inclusive
0041     std::set<std::string> setSubVer;  // empty matches all
0042     int nPriority;
0043 
0044     // Actions
0045     std::string strComment;
0046     std::string strStatusBar;
0047     std::string strReserved;
0048 
0049     ADD_SERIALIZE_METHODS;
0050 
0051     template <typename Stream, typename Operation>
0052     inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
0053         READWRITE(this->nVersion);
0054         nVersion = this->nVersion;
0055         READWRITE(nRelayUntil);
0056         READWRITE(nExpiration);
0057         READWRITE(nID);
0058         READWRITE(nCancel);
0059         READWRITE(setCancel);
0060         READWRITE(nMinVer);
0061         READWRITE(nMaxVer);
0062         READWRITE(setSubVer);
0063         READWRITE(nPriority);
0064 
0065         READWRITE(LIMITED_STRING(strComment, 65536));
0066         READWRITE(LIMITED_STRING(strStatusBar, 256));
0067         READWRITE(LIMITED_STRING(strReserved, 256));
0068     }
0069 
0070     void SetNull();
0071 
0072     std::string ToString() const;
0073 };
0074 
0075 /** An alert is a combination of a serialized CUnsignedAlert and a signature. */
0076 class CAlert : public CUnsignedAlert
0077 {
0078 public:
0079     std::vector<unsigned char> vchMsg;
0080     std::vector<unsigned char> vchSig;
0081 
0082     CAlert()
0083     {
0084         SetNull();
0085     }
0086 
0087     ADD_SERIALIZE_METHODS;
0088 
0089     template <typename Stream, typename Operation>
0090     inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
0091         READWRITE(vchMsg);
0092         READWRITE(vchSig);
0093     }
0094 
0095     void SetNull();
0096     bool IsNull() const;
0097     uint256 GetHash() const;
0098     bool IsInEffect() const;
0099     bool Cancels(const CAlert& alert) const;
0100     bool AppliesTo(int nVersion, std::string strSubVerIn) const;
0101     bool AppliesToMe() const;
0102     bool RelayTo(CNode* pnode) const;
0103     bool CheckSignature() const;
0104     bool ProcessAlert(bool fThread = true); // fThread means run -alertnotify in a free-running thread
0105     static void Notify(const std::string& strMessage, bool fThread);
0106 
0107     /*
0108      * Get copy of (active) alert object by hash. Returns a null alert if it is not found.
0109      */
0110     static CAlert getAlertByHash(const uint256 &hash);
0111 };
0112 
0113 #endif // BITCOIN_ALERT_H