Dash Core  0.12.2.1
P2P Digital Currency
masternodeconfig.cpp
Go to the documentation of this file.
1 
2 #include "netbase.h"
3 #include "masternodeconfig.h"
4 #include "util.h"
5 #include "chainparams.h"
6 
7 #include <boost/filesystem.hpp>
8 #include <boost/filesystem/fstream.hpp>
9 
11 
12 void CMasternodeConfig::add(std::string alias, std::string ip, std::string privKey, std::string txHash, std::string outputIndex) {
13  CMasternodeEntry cme(alias, ip, privKey, txHash, outputIndex);
14  entries.push_back(cme);
15 }
16 
17 bool CMasternodeConfig::read(std::string& strErr) {
18  int linenumber = 1;
19  boost::filesystem::path pathMasternodeConfigFile = GetMasternodeConfigFile();
20  boost::filesystem::ifstream streamConfig(pathMasternodeConfigFile);
21 
22  if (!streamConfig.good()) {
23  FILE* configFile = fopen(pathMasternodeConfigFile.string().c_str(), "a");
24  if (configFile != NULL) {
25  std::string strHeader = "# Masternode config file\n"
26  "# Format: alias IP:port masternodeprivkey collateral_output_txid collateral_output_index\n"
27  "# Example: mn1 127.0.0.2:19999 93HaYBVUCYjEMeeH1Y4sBGLALQZE1Yc1K64xiqgX37tGBDQL8Xg 2bcd3c84c84f87eaa86e4e56834c92927a07f9e18718810b92e0d0324456a67c 0\n";
28  fwrite(strHeader.c_str(), std::strlen(strHeader.c_str()), 1, configFile);
29  fclose(configFile);
30  }
31  return true; // Nothing to read, so just return
32  }
33 
34  for(std::string line; std::getline(streamConfig, line); linenumber++)
35  {
36  if(line.empty()) continue;
37 
38  std::istringstream iss(line);
39  std::string comment, alias, ip, privKey, txHash, outputIndex;
40 
41  if (iss >> comment) {
42  if(comment.at(0) == '#') continue;
43  iss.str(line);
44  iss.clear();
45  }
46 
47  if (!(iss >> alias >> ip >> privKey >> txHash >> outputIndex)) {
48  iss.str(line);
49  iss.clear();
50  if (!(iss >> alias >> ip >> privKey >> txHash >> outputIndex)) {
51  strErr = _("Could not parse masternode.conf") + "\n" +
52  strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"";
53  streamConfig.close();
54  return false;
55  }
56  }
57 
58  int port = 0;
59  std::string hostname = "";
60  SplitHostPort(ip, port, hostname);
61  if(port == 0 || hostname == "") {
62  strErr = _("Failed to parse host:port string") + "\n"+
63  strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"";
64  streamConfig.close();
65  return false;
66  }
67  int mainnetDefaultPort = Params(CBaseChainParams::MAIN).GetDefaultPort();
68  if(Params().NetworkIDString() == CBaseChainParams::MAIN) {
69  if(port != mainnetDefaultPort) {
70  strErr = _("Invalid port detected in masternode.conf") + "\n" +
71  strprintf(_("Port: %d"), port) + "\n" +
72  strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"" + "\n" +
73  strprintf(_("(must be %d for mainnet)"), mainnetDefaultPort);
74  streamConfig.close();
75  return false;
76  }
77  } else if(port == mainnetDefaultPort) {
78  strErr = _("Invalid port detected in masternode.conf") + "\n" +
79  strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"" + "\n" +
80  strprintf(_("(%d could be used only on mainnet)"), mainnetDefaultPort);
81  streamConfig.close();
82  return false;
83  }
84 
85 
86  add(alias, ip, privKey, txHash, outputIndex);
87  }
88 
89  streamConfig.close();
90  return true;
91 }
CMasternodeConfig masternodeConfig
#define strprintf
Definition: tinyformat.h:1011
bool read(std::string &strErr)
std::vector< CMasternodeEntry > entries
int GetDefaultPort() const
Definition: chainparams.h:58
void add(std::string alias, std::string ip, std::string privKey, std::string txHash, std::string outputIndex)
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
Definition: netbase.cpp:61
const CChainParams & Params()
boost::filesystem::path GetMasternodeConfigFile()
Definition: util.cpp:620
static const std::string MAIN
int port
Definition: zmq_sub.py:8
std::string _(const char *psz)
Definition: util.h:84
FDELT_TYPE alias("__fdelt_warn")))