5 #include <boost/foreach.hpp>
12 #include "ui_interface.h"
16 map<uint256, CAlert> mapAlerts;
17 CCriticalSection cs_mapAlerts;
19 void CUnsignedAlert::SetNull()
37 std::string CUnsignedAlert::ToString() const
39 std::string strSetCancel;
40 BOOST_FOREACH(int n, setCancel)
41 strSetCancel += strprintf("%d ", n);
42 std::string strSetSubVer;
43 BOOST_FOREACH(std::string str, setSubVer)
44 strSetSubVer += "\"" + str + "\" ";
48 " nRelayUntil = %"PRI64d"\n"
49 " nExpiration = %"PRI64d"\n"
57 " strComment = \"%s\"\n"
58 " strStatusBar = \"%s\"\n"
71 strStatusBar.c_str());
74 void CUnsignedAlert::print() const
76 printf("%s", ToString().c_str());
79 void CAlert::SetNull()
81 CUnsignedAlert::SetNull();
86 bool CAlert::IsNull() const
88 return (nExpiration == 0);
91 uint256 CAlert::GetHash() const
93 return Hash(this->vchMsg.begin(), this->vchMsg.end());
96 bool CAlert::IsInEffect() const
98 return (GetAdjustedTime() < nExpiration);
101 bool CAlert::Cancels(const CAlert& alert) const
104 return false; // this was a no-op before 31403
105 return (alert.nID <= nCancel || setCancel.count(alert.nID));
108 bool CAlert::AppliesTo(int nVersion, std::string strSubVerIn) const
110 // TODO: rework for client-version-embedded-in-strSubVer ?
111 return (IsInEffect() &&
112 nMinVer <= nVersion && nVersion <= nMaxVer &&
113 (setSubVer.empty() || setSubVer.count(strSubVerIn)));
116 bool CAlert::AppliesToMe() const
118 return AppliesTo(PROTOCOL_VERSION, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<std::string>()));
121 bool CAlert::RelayTo(CNode* pnode) const
125 // returns true if wasn't already contained in the set
126 if (pnode->setKnown.insert(GetHash()).second)
128 if (AppliesTo(pnode->nVersion, pnode->strSubVer) ||
130 GetAdjustedTime() < nRelayUntil)
132 pnode->PushMessage("alert", *this);
139 bool CAlert::CheckSignature() const
142 if (!key.SetPubKey(ParseHex("04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284")))
143 return error("CAlert::CheckSignature() : SetPubKey failed");
144 if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
145 return error("CAlert::CheckSignature() : verify signature failed");
147 // Now unserialize the data
148 CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION);
149 sMsg >> *(CUnsignedAlert*)this;
153 CAlert CAlert::getAlertByHash(const uint256 &hash)
158 map<uint256, CAlert>::iterator mi = mapAlerts.find(hash);
159 if(mi != mapAlerts.end())
165 bool CAlert::ProcessAlert()
167 if (!CheckSignature())
172 // alert.nID=max is reserved for if the alert key is
173 // compromised. It must have a pre-defined message,
174 // must never expire, must apply to all versions,
175 // and must cancel all previous
176 // alerts or it will be ignored (so an attacker can't
177 // send an "everything is OK, don't panic" version that
178 // cannot be overridden):
179 int maxInt = std::numeric_limits<int>::max();
183 nExpiration == maxInt &&
184 nCancel == (maxInt-1) &&
188 nPriority == maxInt &&
189 strStatusBar == "URGENT: Alert key compromised, upgrade required"
196 // Cancel previous alerts
197 for (map<uint256, CAlert>::iterator mi = mapAlerts.begin(); mi != mapAlerts.end();)
199 const CAlert& alert = (*mi).second;
202 printf("cancelling alert %d\n", alert.nID);
203 uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
204 mapAlerts.erase(mi++);
206 else if (!alert.IsInEffect())
208 printf("expiring alert %d\n", alert.nID);
209 uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
210 mapAlerts.erase(mi++);
216 // Check if this alert has been cancelled
217 BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
219 const CAlert& alert = item.second;
220 if (alert.Cancels(*this))
222 printf("alert already cancelled by %d\n", alert.nID);
228 mapAlerts.insert(make_pair(GetHash(), *this));
229 // Notify UI if it applies to me
231 uiInterface.NotifyAlertChanged(GetHash(), CT_NEW);
234 printf("accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe());