12 #include "ui_interface.h"
16 map<uint256, CAlert> mapAlerts;
17 CCriticalSection cs_mapAlerts;
19 static const string strMainKey = "043fa441fd4203d03f5df2b75ea14e36f20d39f43e7a61aa7552ab9bcd7ecb0e77a3be4585b13fcdaa22ef6e51f1ff6f2929bec2494385b086fb86610e33193195";
21 // TestNet alerts pubKey
22 static const string strTestKey = "0471dc165db490094d35cde15b1f5d755fa6ad6f2b5ed0f340e3f17f57389c3c2af113a8cbcc885bde73305a553b5640c83021128008ddf882e856336269080496";
24 // TestNet alerts private key
25 // "308201130201010420b665cff1884e53da26376fd1b433812c9a5a8a4d5221533b15b9629789bb7e42a081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a1440342000471dc165db490094d35cde15b1f5d755fa6ad6f2b5ed0f340e3f17f57389c3c2af113a8cbcc885bde73305a553b5640c83021128008ddf882e856336269080496"
27 void CUnsignedAlert::SetNull()
45 string CUnsignedAlert::ToString() const
48 for(int32_t n : setCancel)
49 strSetCancel += strprintf("%" PRId32 " ", n);
51 for(string str : setSubVer)
52 strSetSubVer += "\"" + str + "\" ";
55 " nVersion = %" PRId32 "\n"
56 " nRelayUntil = %" PRId64 "\n"
57 " nExpiration = %" PRId64 "\n"
58 " nID = %" PRId32 "\n"
59 " nCancel = %" PRId32 "\n"
61 " nMinVer = %" PRId32 "\n"
62 " nMaxVer = %" PRId32 "\n"
64 " nPriority = %" PRId32 "\n"
65 " strComment = \"%s\"\n"
66 " strStatusBar = \"%s\"\n"
79 strStatusBar.c_str());
82 void CAlert::SetNull()
84 CUnsignedAlert::SetNull();
89 bool CAlert::IsNull() const
91 return (nExpiration == 0);
94 uint256 CAlert::GetHash() const
96 return Hash(this->vchMsg.begin(), this->vchMsg.end());
99 bool CAlert::IsInEffect() const
101 return (GetAdjustedTime() < nExpiration);
104 bool CAlert::Cancels(const CAlert& alert) const
107 return false; // this was a no-op before 31403
108 return (alert.nID <= nCancel || setCancel.count(alert.nID));
111 bool CAlert::AppliesTo(int nVersion, string strSubVerIn) const
113 // TODO: rework for client-version-embedded-in-strSubVer ?
114 return (IsInEffect() &&
115 nMinVer <= nVersion && nVersion <= nMaxVer &&
116 (setSubVer.empty() || setSubVer.count(strSubVerIn)));
119 bool CAlert::AppliesToMe() const
121 return AppliesTo(PROTOCOL_VERSION, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, vector<string>()));
124 bool CAlert::RelayTo(CNode* pnode) const
128 // don't relay to nodes which haven't sent their version message
129 if (pnode->nVersion == 0)
131 // returns true if wasn't already contained in the set
132 if (pnode->setKnown.insert(GetHash()).second)
134 if (AppliesTo(pnode->nVersion, pnode->strSubVer) ||
136 GetAdjustedTime() < nRelayUntil)
138 pnode->PushMessage("alert", *this);
145 bool CAlert::CheckSignature() const
148 key.Set(ParseHex(fTestNet ? strTestKey : strMainKey));
149 if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
150 return error("CAlert::CheckSignature() : verify signature failed");
152 // Now unserialize the data
153 CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION);
154 sMsg >> *(CUnsignedAlert*)this;
158 CAlert CAlert::getAlertByHash(const uint256 &hash)
163 auto mi = mapAlerts.find(hash);
164 if(mi != mapAlerts.end())
170 bool CAlert::ProcessAlert()
172 if (!CheckSignature())
177 // alert.nID=max is reserved for if the alert key is
178 // compromised. It must have a pre-defined message,
179 // must never expire, must apply to all versions,
180 // and must cancel all previous
181 // alerts or it will be ignored (so an attacker can't
182 // send an "everything is OK, don't panic" version that
183 // cannot be overridden):
184 int maxInt = numeric_limits<int>::max();
188 nExpiration == maxInt &&
189 nCancel == (maxInt-1) &&
193 nPriority == maxInt &&
194 strStatusBar == "URGENT: Alert key compromised, upgrade required"
201 // Cancel previous alerts
202 for (auto mi = mapAlerts.begin(); mi != mapAlerts.end();)
204 const auto& alert = (*mi).second;
207 printf("cancelling alert %" PRId32 "\n", alert.nID);
208 uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
209 mapAlerts.erase(mi++);
211 else if (!alert.IsInEffect())
213 printf("expiring alert %" PRId32 "\n", alert.nID);
214 uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
215 mapAlerts.erase(mi++);
221 // Check if this alert has been cancelled
222 for(auto& item : mapAlerts)
224 const auto& alert = item.second;
225 if (alert.Cancels(*this))
227 printf("alert already cancelled by %" PRId32 "\n", alert.nID);
233 mapAlerts.insert(make_pair(GetHash(), *this));
234 // Notify UI if it applies to me
236 uiInterface.NotifyAlertChanged(GetHash(), CT_NEW);
239 printf("accepted alert %" PRId32 ", AppliesToMe()=%" PRId32 "\n", nID, AppliesToMe());