PRCYCoin  2.0.0.7rc1
P2P Digital Currency
activemasternode.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2016 The Dash developers
2 // Copyright (c) 2015-2018 The PIVX developers
3 // Copyright (c) 2018-2020 The DAPS Project developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "activemasternode.h"
8 
9 #include "addrman.h"
10 #include "masternode-sync.h"
11 #include "masternode.h"
12 #include "masternodeconfig.h"
13 #include "masternodeman.h"
14 #include "messagesigner.h"
15 #include "netbase.h"
16 #include "protocol.h"
17 
18 //
19 // Bootup the Masternode, look for a 5000 PRCY input and register on the network
20 //
22 {
23  if (!fMasterNode) return;
24 
25  LogPrint(BCLog::MASTERNODE, "CActiveMasternode::ManageStatus() - Begin\n");
26 
27  //need correct blocks to send ping
28  if (!Params().IsRegTestNet() && !masternodeSync.IsBlockchainSynced()) {
30  LogPrintf("CActiveMasternode::ManageStatus() - %s\n", GetStatusMessage());
31  return;
32  }
33 
35 
37  CMasternode* pmn;
39  if (pmn != nullptr) {
40  pmn->Check();
41  if (pmn->IsEnabled() && pmn->protocolVersion == PROTOCOL_VERSION)
42  EnableHotColdMasterNode(pmn->vin, pmn->addr);
43  }
44  }
45 
47  // Set defaults
49  notCapableReason = "";
50 
51  if (strMasterNodeAddr.empty()) {
52  if (!GetLocal(service)) {
53  notCapableReason = "Can't detect external address. Please use the masternodeaddr configuration option.";
54  LogPrintf("CActiveMasternode::ManageStatus() - not capable: %s\n", notCapableReason);
55  return;
56  }
57  } else {
58  int nPort;
59  std::string strHost;
60  SplitHostPort(strMasterNodeAddr, nPort, strHost);
61  service = LookupNumeric(strHost.c_str(), nPort);
62  }
63 
64  // The service needs the correct default port to work properly
65  if (!CMasternodeBroadcast::CheckDefaultPort(service, notCapableReason, "CActiveMasternode::ManageStatus()")) {
66  return;
67  }
68 
69  LogPrintf("CActiveMasternode::ManageStatus() - Checking inbound connection to '%s'\n", service.ToString());
70 
72  if (!OpenNetworkConnection(addr, true, nullptr)) {
73  notCapableReason = "Could not connect to " + service.ToString();
74  LogPrintf("CActiveMasternode::ManageStatus() - not capable: %s\n", notCapableReason);
75  return;
76  }
77 
78  notCapableReason = "Waiting for start message from controller.";
79  return;
80  }
81 
82  //send to all peers
83  std::string errorMessage;
84  if (!SendMasternodePing(errorMessage)) {
85  LogPrintf("CActiveMasternode::ManageStatus() - Error on Ping: %s\n", errorMessage);
86  }
87 }
88 
90 {
92  ManageStatus();
93 }
94 
96 {
97  switch (status) {
99  return "Node just started, not yet activated";
101  return "Sync in progress. Must wait until sync is complete to start Masternode";
103  return "Not capable masternode: " + notCapableReason;
105  return "Masternode successfully started";
106  default:
107  return "unknown";
108  }
109 }
110 
111 bool CActiveMasternode::SendMasternodePing(std::string& errorMessage)
112 {
114  errorMessage = "Masternode is not in a running status";
115  return false;
116  }
117 
119  CKey keyMasternode;
120 
122  errorMessage = "Error upon calling SetKey.\n";
123  return false;
124  }
125 
126  std::string stl(vin.masternodeStealthAddress.begin(), vin.masternodeStealthAddress.end());
127  CMasternodePing mnp(vin);
128  if (!mnp.Sign(keyMasternode, pubKeyMasternode)) {
129  errorMessage = "Couldn't sign Masternode Ping";
130  return false;
131  }
132 
133  // Update lastPing for our masternode in Masternode list
134  CMasternode* pmn = mnodeman.Find(vin);
135  if (pmn != nullptr) {
137  errorMessage = "Too early to send Masternode Ping";
138  return false;
139  }
140 
141  pmn->lastPing = mnp;
142  mnodeman.mapSeenMasternodePing.insert(std::make_pair(mnp.GetHash(), mnp));
143 
144  //mnodeman.mapSeenMasternodeBroadcast.lastPing is probably outdated, so we'll update it
145  CMasternodeBroadcast mnb(*pmn);
146  uint256 hash = mnb.GetHash();
147  if (mnodeman.mapSeenMasternodeBroadcast.count(hash)) mnodeman.mapSeenMasternodeBroadcast[hash].lastPing = mnp;
148 
149  mnp.Relay();
150 
151  return true;
152  } else {
153  // Seems like we are trying to send a ping while the Masternode is not registered in the network
154  errorMessage = "Masternode List doesn't include our Masternode, shutting down Masternode pinging service! " + vin.ToString();
156  notCapableReason = errorMessage;
157  return false;
158  }
159 }
160 
161 // when starting a Masternode, this can enable to run as a hot wallet with no funds
163 {
164  if (!fMasterNode) return false;
165 
167 
168  //The values below are needed for signing mnping messages going forward
169  vin = newVin;
170  service = newService;
171 
172  LogPrintf("CActiveMasternode::EnableHotColdMasterNode() - Enabled! You may shut down the cold daemon.\n");
173 
174  return true;
175 }
CTxIn
An input of a transaction.
Definition: transaction.h:83
CActiveMasternode::EnableHotColdMasterNode
bool EnableHotColdMasterNode(CTxIn &vin, CService &addr)
Enable cold wallet mode (run a Masternode with no funds)
Definition: activemasternode.cpp:162
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:133
CActiveMasternode::ResetStatus
void ResetStatus()
Definition: activemasternode.cpp:89
SplitHostPort
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
Definition: netbase.cpp:74
CMasternodeBroadcast::CheckDefaultPort
static bool CheckDefaultPort(CService service, std::string &strErrorRet, const std::string &strContext)
Definition: masternode.cpp:505
CActiveMasternode::notCapableReason
std::string notCapableReason
Definition: activemasternode.h:30
CMasternodeMan::Find
CMasternode * Find(const CScript &payee)
Find an entry.
Definition: masternodeman.cpp:440
CActiveMasternode::vin
CTxIn vin
Definition: activemasternode.h:44
CMasternodeSync::IsBlockchainSynced
bool IsBlockchainSynced()
Definition: masternode-sync.cpp:32
CMasternodePing::Sign
bool Sign(CKey &keyMasternode, CPubKey &pubKeyMasternode)
Definition: masternode.cpp:755
masternode-sync.h
CMasternodePing::sigTime
int64_t sigTime
Definition: masternode.h:44
NODE_NETWORK
@ NODE_NETWORK
Definition: protocol.h:302
masternodeman.h
CMasternode::vin
CTxIn vin
Definition: masternode.h:127
fMasterNode
bool fMasterNode
Definition: util.cpp:97
CTxIn::masternodeStealthAddress
std::vector< unsigned char > masternodeStealthAddress
Definition: transaction.h:99
masternodeSync
CMasternodeSync masternodeSync
Definition: masternode-sync.cpp:19
CActiveMasternode::pubKeyMasternode
CPubKey pubKeyMasternode
Definition: activemasternode.h:41
strMasterNodePrivKey
std::string strMasterNodePrivKey
Definition: util.cpp:98
ACTIVE_MASTERNODE_SYNC_IN_PROCESS
#define ACTIVE_MASTERNODE_SYNC_IN_PROCESS
Definition: activemasternode.h:18
protocol.h
mnodeman
CMasternodeMan mnodeman
Masternode manager.
Definition: masternodeman.cpp:22
CService::ToString
std::string ToString() const
Definition: netaddress.cpp:568
strMasterNodeAddr
std::string strMasterNodeAddr
Definition: util.cpp:99
GetLocal
bool GetLocal(CService &addr, const CNetAddr *paddrPeer)
Definition: net.cpp:119
CActiveMasternode::ManageStatus
void ManageStatus()
Manage status of main Masternode.
Definition: activemasternode.cpp:21
CActiveMasternode::GetStatusMessage
std::string GetStatusMessage() const
Definition: activemasternode.cpp:95
LogPrintf
#define LogPrintf(...)
Definition: logging.h:147
CMasternodeMan::mapSeenMasternodeBroadcast
std::map< uint256, CMasternodeBroadcast > mapSeenMasternodeBroadcast
Definition: masternodeman.h:76
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
LogPrint
#define LogPrint(category,...)
Definition: logging.h:162
ACTIVE_MASTERNODE_NOT_CAPABLE
#define ACTIVE_MASTERNODE_NOT_CAPABLE
Definition: activemasternode.h:19
CMessageSigner::GetKeysFromSecret
static bool GetKeysFromSecret(const std::string &strSecret, CKey &keyRet, CPubKey &pubkeyRet)
Set the private/public key values, returns true if successful.
Definition: messagesigner.cpp:13
activemasternode.h
CMasternode::addr
CService addr
Definition: masternode.h:128
CMasternodeBroadcast::GetHash
uint256 GetHash()
Definition: masternode.h:322
messagesigner.h
CMasternode
Definition: masternode.h:107
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
CAddress
A CService with information about it as peer.
Definition: protocol.h:323
CMasternodePing::Relay
void Relay()
Definition: masternode.cpp:872
CKey
An encapsulated private key.
Definition: key.h:39
CActiveMasternode::service
CService service
Definition: activemasternode.h:45
BCLog::MASTERNODE
@ MASTERNODE
Definition: logging.h:62
CTxIn::ToString
std::string ToString() const
Definition: transaction.cpp:50
CMasternodeMan::mapSeenMasternodePing
std::map< uint256, CMasternodePing > mapSeenMasternodePing
Definition: masternodeman.h:78
OpenNetworkConnection
bool OpenNetworkConnection(const CAddress &addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot, bool fFeeler)
Definition: net.cpp:1670
LookupNumeric
CService LookupNumeric(const char *pszName, int portDefault)
Definition: netbase.cpp:234
masternode.h
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:463
CMasternode::IsPingedWithin
bool IsPingedWithin(int seconds, int64_t now=-1)
Definition: masternode.h:229
ACTIVE_MASTERNODE_INITIAL
#define ACTIVE_MASTERNODE_INITIAL
Definition: activemasternode.h:17
MASTERNODE_PING_SECONDS
#define MASTERNODE_PING_SECONDS
Definition: masternode.h:21
addrman.h
CMasternode::IsEnabled
bool IsEnabled()
Definition: masternode.h:242
netbase.h
ACTIVE_MASTERNODE_STARTED
#define ACTIVE_MASTERNODE_STARTED
Definition: activemasternode.h:20
CActiveMasternode::status
int status
Definition: activemasternode.h:29
CActiveMasternode::SendMasternodePing
bool SendMasternodePing(std::string &errorMessage)
Ping Masternode.
Definition: activemasternode.cpp:111
CMasternodeBroadcast
Definition: masternode.h:292
CMasternode::protocolVersion
int protocolVersion
Definition: masternode.h:140
masternodeconfig.h
CMasternodePing
Definition: masternode.h:39
CMasternodePing::GetHash
uint256 GetHash()
Definition: masternode.h:67
CMasternode::Check
void Check(bool forceCheck=false)
Definition: masternode.cpp:200
CMasternode::lastPing
CMasternodePing lastPing
Definition: masternode.h:145