LCOV - code coverage report
Current view: top level - src - masternodeconfig.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 38 66 57.6 %
Date: 2025-02-23 09:33:43 Functions: 3 4 75.0 %

          Line data    Source code
       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             : 
      13             : CMasternodeConfig masternodeConfig;
      14             : 
      15          20 : CMasternodeConfig::CMasternodeEntry* CMasternodeConfig::add(std::string alias, std::string ip, std::string privKey, std::string txHash, std::string outputIndex)
      16             : {
      17          40 :     CMasternodeEntry cme(alias, ip, privKey, txHash, outputIndex);
      18          20 :     entries.push_back(cme);
      19          20 :     return &(entries[entries.size()-1]);
      20             : }
      21             : 
      22           0 : void CMasternodeConfig::remove(std::string alias) {
      23           0 :     LOCK(cs_entries);
      24           0 :     int pos = -1;
      25           0 :     for (int i = 0; i < ((int) entries.size()); ++i) {
      26           0 :         CMasternodeEntry e = entries[i];
      27           0 :         if (e.getAlias() == alias) {
      28           0 :             pos = i;
      29           0 :             break;
      30             :         }
      31             :     }
      32           0 :     entries.erase(entries.begin() + pos);
      33           0 : }
      34             : 
      35         368 : bool CMasternodeConfig::read(std::string& strErr)
      36             : {
      37         736 :     LOCK(cs_entries);
      38         368 :     int linenumber = 1;
      39         736 :     fs::path pathMasternodeConfigFile = GetMasternodeConfigFile();
      40         736 :     fsbridge::ifstream streamConfig(pathMasternodeConfigFile);
      41             : 
      42         368 :     if (!streamConfig.good()) {
      43         269 :         FILE* configFile = fsbridge::fopen(pathMasternodeConfigFile, "a");
      44         269 :         if (configFile != nullptr) {
      45         269 :             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         538 :                                     "#\n";
      49         269 :             fwrite(strHeader.c_str(), std::strlen(strHeader.c_str()), 1, configFile);
      50         269 :             fclose(configFile);
      51             :         }
      52         269 :         return true; // Nothing to read, so just return
      53             :     }
      54             : 
      55         430 :     for (std::string line; std::getline(streamConfig, line); linenumber++) {
      56         628 :         if (line.empty()) continue;
      57             : 
      58         317 :         std::istringstream iss(line);
      59         377 :         std::string comment, alias, ip, privKey, txHash, outputIndex;
      60             : 
      61         317 :         if (iss >> comment) {
      62         317 :             if (comment.at(0) == '#') continue;
      63          20 :             iss.str(line);
      64          20 :             iss.clear();
      65             :         }
      66             : 
      67          20 :         if (!(iss >> alias >> ip >> privKey >> txHash >> outputIndex)) {
      68           0 :             iss.str(line);
      69           0 :             iss.clear();
      70           0 :             if (!(iss >> alias >> ip >> privKey >> txHash >> outputIndex)) {
      71           0 :                 strErr = _("Could not parse masternode.conf") + "\n" +
      72           0 :                          strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"";
      73           0 :                 streamConfig.close();
      74           0 :                 return false;
      75             :             }
      76             :         }
      77             : 
      78          20 :         int port = 0;
      79          20 :         int nDefaultPort = Params().GetDefaultPort();
      80          40 :         std::string hostname = "";
      81          40 :         SplitHostPort(ip, port, hostname);
      82          40 :         if(port == 0 || hostname == "") {
      83           0 :             strErr = _("Failed to parse host:port string") + "\n"+
      84           0 :                      strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"";
      85           0 :             streamConfig.close();
      86           0 :             return false;
      87             :         }
      88             : 
      89          20 :         if (port != nDefaultPort && !Params().IsRegTestNet()) {
      90           0 :             strErr = strprintf(_("Invalid port %d detected in masternode.conf"), port) + "\n" +
      91           0 :                      strprintf(_("Line: %d"), linenumber) + "\n\"" + ip + "\"" + "\n" +
      92           0 :                      strprintf(_("(must be %d for %s-net)"), nDefaultPort, Params().NetworkIDString());
      93           0 :             streamConfig.close();
      94             :             return false;
      95             :         }
      96             : 
      97             : 
      98         180 :         add(alias, ip, privKey, txHash, outputIndex);
      99             :     }
     100             : 
     101          99 :     streamConfig.close();
     102             :     return true;
     103             : }
     104             : 
     105          14 : bool CMasternodeConfig::CMasternodeEntry::castOutputIndex(int &n) const
     106             : {
     107          14 :     try {
     108          14 :         n = std::stoi(outputIndex);
     109           0 :     } catch (const std::exception& e) {
     110           0 :         LogPrintf("%s: %s on getOutputIndex\n", __func__, e.what());
     111           0 :         return false;
     112             :     }
     113             : 
     114          14 :     return true;
     115             : }

Generated by: LCOV version 1.14