5 #include <boost/foreach.hpp>
12 #include "ui_interface.h"
16 map<uint256, CAlert> mapAlerts;
17 CCriticalSection cs_mapAlerts;
19 static const char* pszMainKey = "043fa441fd4203d03f5df2b75ea14e36f20d39f43e7a61aa7552ab9bcd7ecb0e77a3be4585b13fcdaa22ef6e51f1ff6f2929bec2494385b086fb86610e33193195";
21 // TestNet alerts pubKey
22 static const char* pszTestKey = "0471dc165db490094d35cde15b1f5d755fa6ad6f2b5ed0f340e3f17f57389c3c2af113a8cbcc885bde73305a553b5640c83021128008ddf882e856336269080496";
24 // TestNet alerts private key
25 // "308201130201010420b665cff1884e53da26376fd1b433812c9a5a8a4d5221533b15b9629789bb7e42a081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a1440342000471dc165db490094d35cde15b1f5d755fa6ad6f2b5ed0f340e3f17f57389c3c2af113a8cbcc885bde73305a553b5640c83021128008ddf882e856336269080496"
27 void CUnsignedAlert::SetNull()
45 std::string CUnsignedAlert::ToString() const
47 std::string strSetCancel;
48 BOOST_FOREACH(int n, setCancel)
49 strSetCancel += strprintf("%d ", n);
50 std::string strSetSubVer;
51 BOOST_FOREACH(std::string str, setSubVer)
52 strSetSubVer += "\"" + str + "\" ";
56 " nRelayUntil = %"PRI64d"\n"
57 " nExpiration = %"PRI64d"\n"
65 " strComment = \"%s\"\n"
66 " strStatusBar = \"%s\"\n"
79 strStatusBar.c_str());
82 void CUnsignedAlert::print() const
84 printf("%s", ToString().c_str());
87 void CAlert::SetNull()
89 CUnsignedAlert::SetNull();
94 bool CAlert::IsNull() const
96 return (nExpiration == 0);
99 uint256 CAlert::GetHash() const
101 return Hash(this->vchMsg.begin(), this->vchMsg.end());
104 bool CAlert::IsInEffect() const
106 return (GetAdjustedTime() < nExpiration);
109 bool CAlert::Cancels(const CAlert& alert) const
112 return false; // this was a no-op before 31403
113 return (alert.nID <= nCancel || setCancel.count(alert.nID));
116 bool CAlert::AppliesTo(int nVersion, std::string strSubVerIn) const
118 // TODO: rework for client-version-embedded-in-strSubVer ?
119 return (IsInEffect() &&
120 nMinVer <= nVersion && nVersion <= nMaxVer &&
121 (setSubVer.empty() || setSubVer.count(strSubVerIn)));
124 bool CAlert::AppliesToMe() const
126 return AppliesTo(PROTOCOL_VERSION, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<std::string>()));
129 bool CAlert::RelayTo(CNode* pnode) const
133 // returns true if wasn't already contained in the set
134 if (pnode->setKnown.insert(GetHash()).second)
136 if (AppliesTo(pnode->nVersion, pnode->strSubVer) ||
138 GetAdjustedTime() < nRelayUntil)
140 pnode->PushMessage("alert", *this);
147 bool CAlert::CheckSignature() const
150 if (!key.SetPubKey(ParseHex(fTestNet ? pszTestKey : pszMainKey)))
151 return error("CAlert::CheckSignature() : SetPubKey failed");
152 if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
153 return error("CAlert::CheckSignature() : verify signature failed");
155 // Now unserialize the data
156 CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION);
157 sMsg >> *(CUnsignedAlert*)this;
161 CAlert CAlert::getAlertByHash(const uint256 &hash)
166 map<uint256, CAlert>::iterator mi = mapAlerts.find(hash);
167 if(mi != mapAlerts.end())
173 bool CAlert::ProcessAlert()
175 if (!CheckSignature())
180 // alert.nID=max is reserved for if the alert key is
181 // compromised. It must have a pre-defined message,
182 // must never expire, must apply to all versions,
183 // and must cancel all previous
184 // alerts or it will be ignored (so an attacker can't
185 // send an "everything is OK, don't panic" version that
186 // cannot be overridden):
187 int maxInt = std::numeric_limits<int>::max();
191 nExpiration == maxInt &&
192 nCancel == (maxInt-1) &&
196 nPriority == maxInt &&
197 strStatusBar == "URGENT: Alert key compromised, upgrade required"
204 // Cancel previous alerts
205 for (map<uint256, CAlert>::iterator mi = mapAlerts.begin(); mi != mapAlerts.end();)
207 const CAlert& alert = (*mi).second;
210 printf("cancelling alert %d\n", alert.nID);
211 uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
212 mapAlerts.erase(mi++);
214 else if (!alert.IsInEffect())
216 printf("expiring alert %d\n", alert.nID);
217 uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
218 mapAlerts.erase(mi++);
224 // Check if this alert has been cancelled
225 BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
227 const CAlert& alert = item.second;
228 if (alert.Cancels(*this))
230 printf("alert already cancelled by %d\n", alert.nID);
236 mapAlerts.insert(make_pair(GetHash(), *this));
237 // Notify UI if it applies to me
239 uiInterface.NotifyAlertChanged(GetHash(), CT_NEW);
242 printf("accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe());