PIVX Core  5.6.99
P2P Digital Currency
masternodeconfig.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2015 The Dash developers
2 // Copyright (c) 2015-2021 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
5 #include "masternodeconfig.h"
6 
7 #include "fs.h"
8 #include "netbase.h"
9 #include "util/system.h"
10 #include "guiinterface.h"
11 #include <base58.h>
12 
14 
15 CMasternodeConfig::CMasternodeEntry* CMasternodeConfig::add(std::string alias, std::string ip, std::string privKey, std::string txHash, std::string outputIndex)
16 {
17  CMasternodeEntry cme(alias, ip, privKey, txHash, outputIndex);
18  entries.push_back(cme);
19  return &(entries[entries.size()-1]);
20 }
21 
22 void CMasternodeConfig::remove(std::string alias) {
24  int pos = -1;
25  for (int i = 0; i < ((int) entries.size()); ++i) {
27  if (e.getAlias() == alias) {
28  pos = i;
29  break;
30  }
31  }
32  entries.erase(entries.begin() + pos);
33 }
34 
35 bool CMasternodeConfig::read(std::string& strErr)
36 {
38  int linenumber = 1;
39  fs::path pathMasternodeConfigFile = GetMasternodeConfigFile();
40  fsbridge::ifstream streamConfig(pathMasternodeConfigFile);
41 
42  if (!streamConfig.good()) {
43  FILE* configFile = fsbridge::fopen(pathMasternodeConfigFile, "a");
44  if (configFile != nullptr) {
45  std::string strHeader = "# Masternode config file\n"
46  "# Format: alias IP:port masternodeprivkey collateral_output_txid collateral_output_index\n"
47  "# Example: mn1 127.0.0.2:51472 93HaYBVUCYjEMeeH1Y4sBGLALQZE1Yc1K64xiqgX37tGBDQL8Xg 2bcd3c84c84f87eaa86e4e56834c92927a07f9e18718810b92e0d0324456a67c 0"
48  "#\n";
49  fwrite(strHeader.c_str(), std::strlen(strHeader.c_str()), 1, configFile);
50  fclose(configFile);
51  }
52  return true; // Nothing to read, so just return
53  }
54 
55  for (std::string line; std::getline(streamConfig, line); linenumber++) {
56  if (line.empty()) continue;
57 
58  std::istringstream iss(line);
59  std::string comment, alias, ip, privKey, txHash, outputIndex;
60 
61  if (iss >> comment) {
62  if (comment.at(0) == '#') continue;
63  iss.str(line);
64  iss.clear();
65  }
66 
67  if (!(iss >> alias >> ip >> privKey >> txHash >> outputIndex)) {
68  iss.str(line);
69  iss.clear();
70  if (!(iss >> alias >> ip >> privKey >> txHash >> outputIndex)) {
71  strErr = _("Could not parse masternode.conf") + "\n" +
72  strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"";
73  streamConfig.close();
74  return false;
75  }
76  }
77 
78  int port = 0;
79  int nDefaultPort = Params().GetDefaultPort();
80  std::string hostname = "";
81  SplitHostPort(ip, port, hostname);
82  if(port == 0 || hostname == "") {
83  strErr = _("Failed to parse host:port string") + "\n"+
84  strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"";
85  streamConfig.close();
86  return false;
87  }
88 
89  if (port != nDefaultPort && !Params().IsRegTestNet()) {
90  strErr = strprintf(_("Invalid port %d detected in masternode.conf"), port) + "\n" +
91  strprintf(_("Line: %d"), linenumber) + "\n\"" + ip + "\"" + "\n" +
92  strprintf(_("(must be %d for %s-net)"), nDefaultPort, Params().NetworkIDString());
93  streamConfig.close();
94  return false;
95  }
96 
97 
98  add(alias, ip, privKey, txHash, outputIndex);
99  }
100 
101  streamConfig.close();
102  return true;
103 }
104 
106 {
107  try {
108  n = std::stoi(outputIndex);
109  } catch (const std::exception& e) {
110  LogPrintf("%s: %s on getOutputIndex\n", __func__, e.what());
111  return false;
112  }
113 
114  return true;
115 }
CService ip(uint32_t i)
Definition: DoS_tests.cpp:39
const CChainParams & Params()
Return the currently selected parameters.
int GetDefaultPort() const
Definition: chainparams.h:74
bool castOutputIndex(int &n) const
const std::string & getAlias() const
std::string outputIndex
bool read(std::string &strErr)
void remove(std::string alias)
CMasternodeConfig::CMasternodeEntry * add(std::string alias, std::string ip, std::string privKey, std::string txHash, std::string outputIndex)
std::vector< CMasternodeEntry > entries
@ LOCK
Definition: lockunlock.h:16
CMasternodeConfig masternodeConfig
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:23
fs::ifstream ifstream
Definition: fs.h:92
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
Definition: netbase.cpp:71
fs::path GetMasternodeConfigFile()
Definition: system.cpp:776
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a Optional result.
Definition: system.h:65
#define strprintf
Definition: tinyformat.h:1056