Dash Core  0.12.2.1
P2P Digital Currency
sendalert.cpp
Go to the documentation of this file.
1 #include "alert.h"
2 #include "clientversion.h"
3 #include "chainparams.h"
4 #include "init.h"
5 #include "net.h"
6 #include "utilstrencodings.h"
7 #include "utiltime.h"
8 
9 /*
10 If you need to broadcast an alert, here's what to do:
11 
12 1. Modify alert parameters below, see alert.* and comments in the code
13  for what does what.
14 
15 2. run dashd with -printalert or -sendalert like this:
16  /path/to/dashd -printalert
17 
18 One minute after starting up the alert will be broadcast. It is then
19 flooded through the network until the nRelayUntil time, and will be
20 active until nExpiration OR the alert is cancelled.
21 
22 If you screw up something, send another alert with nCancel set to cancel
23 the bad alert.
24 */
25 
26 void ThreadSendAlert(CConnman& connman)
27 {
28  if (!mapArgs.count("-sendalert") && !mapArgs.count("-printalert"))
29  return;
30 
31  // Wait one minute so we get well connected. If we only need to print
32  // but not to broadcast - do this right away.
33  if (mapArgs.count("-sendalert"))
34  MilliSleep(60*1000);
35 
36  //
37  // Alerts are relayed around the network until nRelayUntil, flood
38  // filling to every node.
39  // After the relay time is past, new nodes are told about alerts
40  // when they connect to peers, until either nExpiration or
41  // the alert is cancelled by a newer alert.
42  // Nodes never save alerts to disk, they are in-memory-only.
43  //
44  CAlert alert;
45  alert.nRelayUntil = GetAdjustedTime() + 15 * 60;
46  alert.nExpiration = GetAdjustedTime() + 30 * 60 * 60;
47  alert.nID = 1; // keep track of alert IDs somewhere
48  alert.nCancel = 0; // cancels previous messages up to this ID number
49 
50  // These versions are protocol versions
51  alert.nMinVer = 70000;
52  alert.nMaxVer = 70103;
53 
54  //
55  // 1000 for Misc warnings like out of disk space and clock is wrong
56  // 2000 for longer invalid proof-of-work chain
57  // Higher numbers mean higher priority
58  alert.nPriority = 5000;
59  alert.strComment = "";
60  alert.strStatusBar = "URGENT: Upgrade required: see https://www.dash.org";
61 
62  // Set specific client version/versions here. If setSubVer is empty, no filtering on subver is done:
63  // alert.setSubVer.insert(std::string("/Dash Core:0.12.0.58/"));
64 
65  // Sign
66  if(!alert.Sign())
67  {
68  LogPrintf("ThreadSendAlert() : could not sign alert\n");
69  return;
70  }
71 
72  // Test
74  sBuffer << alert;
75  CAlert alert2;
76  sBuffer >> alert2;
77  if (!alert2.CheckSignature(Params().AlertKey()))
78  {
79  printf("ThreadSendAlert() : CheckSignature failed\n");
80  return;
81  }
82  assert(alert2.vchMsg == alert.vchMsg);
83  assert(alert2.vchSig == alert.vchSig);
84  alert.SetNull();
85  printf("\nThreadSendAlert:\n");
86  printf("hash=%s\n", alert2.GetHash().ToString().c_str());
87  printf("%s", alert2.ToString().c_str());
88  printf("vchMsg=%s\n", HexStr(alert2.vchMsg).c_str());
89  printf("vchSig=%s\n", HexStr(alert2.vchSig).c_str());
90 
91  // Confirm
92  if (!mapArgs.count("-sendalert"))
93  return;
94  while (connman.GetNodeCount(CConnman::CONNECTIONS_ALL) == 0 && !ShutdownRequested())
95  MilliSleep(500);
96  if (ShutdownRequested())
97  return;
98 
99  // Send
100  printf("ThreadSendAlert() : Sending alert\n");
101  int nSent = 0;
102  {
103  connman.ForEachNode([&alert2, &connman, &nSent](CNode* pnode) {
104  if (alert2.RelayTo(pnode, connman))
105  {
106  printf("ThreadSendAlert() : Sent alert to %s\n", pnode->addr.ToString().c_str());
107  nSent++;
108  }
109  });
110  }
111  printf("ThreadSendAlert() : Alert sent to %d nodes\n", nSent);
112 }
void MilliSleep(int64_t n)
Definition: utiltime.cpp:63
void ThreadSendAlert(CConnman &connman)
Definition: sendalert.cpp:26
bool ShutdownRequested()
Definition: init.cpp:168
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
CAddress addr
Definition: net.h:688
Definition: net.h:108
std::string ToString(bool fUseGetnameinfo=true) const
Definition: netaddress.cpp:568
Definition: alert.h:77
int64_t nRelayUntil
Definition: alert.h:35
bool Sign()
Definition: alert.cpp:149
size_t GetNodeCount(NumConnections num)
Definition: net.cpp:2429
int64_t nExpiration
Definition: alert.h:36
std::string strComment
Definition: alert.h:46
#define LogPrintf(...)
Definition: util.h:98
Definition: net.h:661
int nMaxVer
Definition: alert.h:41
int nMinVer
Definition: alert.h:40
int nCancel
Definition: alert.h:38
const CChainParams & Params()
int64_t GetAdjustedTime()
Definition: timedata.cpp:33
std::string strStatusBar
Definition: alert.h:47
static const int CLIENT_VERSION
Definition: clientversion.h:54
void ForEachNode(const Condition &cond, Callable &&func)
Definition: net.h:239
int nPriority
Definition: alert.h:43
map< string, string > mapArgs
Definition: util.cpp:122