Guest User

Untitled

a guest
Oct 22nd, 2017
487
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. From 7ba0e884a16bca4c56a544b8aa59bcefb976412e Mon Sep 17 00:00:00 2001
  2. From: UdjinM6 <UdjinM6@dash.org>
  3. Date: Mon, 19 Sep 2016 21:19:09 +0300
  4. Subject: [PATCH] generalize governance vote string/enum conversion/usage for
  5.  (probable) future expansion
  6.  
  7. use weighten vote count
  8. ---
  9. src/governance-vote.cpp | 304 +++++++++++++++++++++---------------------------
  10.  src/governance-vote.h   | 122 ++++++++++---------
  11.  src/governance.cpp      |  12 +-
  12.  src/rpcgovernance.cpp   |  81 +++++++------
  13.  4 files changed, 258 insertions(+), 261 deletions(-)
  14.  
  15. diff --git a/src/governance-vote.cpp b/src/governance-vote.cpp
  16. index 7f5d710..5065b69 100644
  17. --- a/src/governance-vote.cpp
  18. +++ b/src/governance-vote.cpp
  19. @@ -2,213 +2,173 @@
  20.  // Distributed under the MIT/X11 software license, see the accompanying
  21.  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  22.  
  23. -#include "core_io.h"
  24. -#include "main.h"
  25. -#include "init.h"
  26. -
  27. -#include "flat-database.h"
  28. -#include "governance.h"
  29. -#include "masternode.h"
  30. -#include "governance.h"
  31.  #include "darksend.h"
  32. +#include "governance.h"
  33. +#include "governance-vote.h"
  34.  #include "masternodeman.h"
  35. -#include "masternode-sync.h"
  36.  #include "util.h"
  37. -#include "addrman.h"
  38. +
  39.  #include <boost/lexical_cast.hpp>
  40. +#include <boost/algorithm/string.hpp>
  41.  
  42. +// Note: keep this list sorted same way as vote_outcome_enum_t
  43. +const std::string CGovernanceVoting::vote_outcome_strings[] = {
  44. +    "NONE", // the first item in this array must always indicate error
  45. +    // "STRONG_NO",
  46. +    // "WEAK_NO",
  47. +    "NO",
  48. +    "ABSTAIN",
  49. +    "YES",
  50. +    // "WEAK_YES",
  51. +    // "STRONG_YES",
  52. +};
  53.  
  54. +const std::string CGovernanceVoting::vote_signal_strings[] = {
  55. +    "NONE", // the first item in this array must always indicate error
  56. +    "FUNDING",
  57. +    "VALID",
  58. +    "DELETE",
  59. +    "ENDORSED",
  60. +    "NOOP1",
  61. +    "NOOP2",
  62. +    "NOOP3",
  63. +    "NOOP4",
  64. +    "NOOP5",
  65. +    "NOOP6",
  66. +    "NOOP7",
  67. +    "NOOP8",
  68. +    "NOOP9",
  69. +    "NOOP10",
  70. +    "NOOP11",
  71. +    "CUSTOM1",
  72. +    "CUSTOM2",
  73. +    "CUSTOM3",
  74. +    "CUSTOM4",
  75. +    "CUSTOM5",
  76. +    "CUSTOM6",
  77. +    "CUSTOM7",
  78. +    "CUSTOM8",
  79. +    "CUSTOM9",
  80. +    "CUSTOM10",
  81. +    "CUSTOM11",
  82. +    "CUSTOM12",
  83. +    "CUSTOM13",
  84. +    "CUSTOM14",
  85. +    "CUSTOM15",
  86. +    "CUSTOM16",
  87. +    "CUSTOM17",
  88. +    "CUSTOM18",
  89. +    "CUSTOM19",
  90. +    "CUSTOM20",
  91. +};
  92.  
  93. -std::string CGovernanceVoting::ConvertOutcomeToString(vote_outcome_enum_t nOutcome)
  94. +// Note: only valid outcomes here (no NONE)
  95. +const std::map<std::string, vote_outcome_enum_t> CGovernanceVoting::CreateVoteOutomeMap()
  96.  {
  97. -    switch(nOutcome)
  98. -    {
  99. -        case VOTE_OUTCOME_NONE:
  100. -            return "NONE"; break;
  101. -        case VOTE_OUTCOME_YES:
  102. -            return "YES"; break;
  103. -        case VOTE_OUTCOME_NO:
  104. -            return "NO"; break;
  105. -        case VOTE_OUTCOME_ABSTAIN:
  106. -            return "ABSTAIN"; break;
  107. +    std::map<std::string, vote_outcome_enum_t> map_temp;
  108. +    // VOTE_OUTCOME_MIN is basically a negative shift from "ABSTAIN" in vote_outcome_strings list
  109. +    // so by using "- VOTE_OUTCOME_MIN" we are going back to "ABSTAIN"
  110. +    // and nOutcome acts as a shift from there ("+ 1" is used to skip "NONE")
  111. +    for (int i = VOTE_OUTCOME_MIN; i <= VOTE_OUTCOME_MAX; ++i) {
  112. +        map_temp[vote_outcome_strings[i - VOTE_OUTCOME_MIN + 1]] = vote_outcome_enum_t(i);
  113.      }
  114. -    return "error";
  115. +    return map_temp;
  116. +};
  117. +const std::map<std::string, vote_outcome_enum_t> CGovernanceVoting::mapVoteOutcome_t = CGovernanceVoting::CreateVoteOutomeMap();
  118. +
  119. +
  120. +// Note: only valid explicitly defined signals here (no NONE, NOOP or CUSTOM)
  121. +const std::map<std::string, vote_signal_enum_t> CGovernanceVoting::CreateDefinedVoteSignalMap()
  122. +{
  123. +    std::map<std::string, vote_signal_enum_t> map_temp;
  124. +    for (int i = VOTE_SIGNAL_MIN; i <= VOTE_SIGNAL_MAX_DEFINED; ++i) {
  125. +        map_temp[vote_signal_strings[i]] = vote_signal_enum_t(i);
  126. +    }
  127. +    return map_temp;
  128. +};
  129. +const std::map<std::string, vote_signal_enum_t> CGovernanceVoting::mapDefinedVoteSignal_t = CGovernanceVoting::CreateDefinedVoteSignalMap();
  130. +
  131. +
  132. +std::string CGovernanceVoting::ConvertOutcomeToString(vote_outcome_enum_t nOutcome)
  133. +{
  134. +    if(nOutcome < VOTE_OUTCOME_MIN || nOutcome > VOTE_OUTCOME_MAX)
  135. +        return vote_outcome_strings[0];
  136. +
  137. +    return vote_outcome_strings[nOutcome - VOTE_OUTCOME_MIN + 1];
  138.  }
  139.  
  140.  std::string CGovernanceVoting::ConvertSignalToString(vote_signal_enum_t nSignal)
  141.  {
  142. -    string strReturn = "NONE";
  143. -    switch(nSignal)
  144. -    {
  145. -        case VOTE_SIGNAL_NONE:
  146. -            strReturn = "NONE";
  147. -            break;
  148. -        case VOTE_SIGNAL_FUNDING:
  149. -            strReturn = "FUNDING";
  150. -            break;
  151. -        case VOTE_SIGNAL_VALID:
  152. -            strReturn = "VALID";
  153. -            break;
  154. -        case VOTE_SIGNAL_DELETE:
  155. -            strReturn = "DELETE";
  156. -            break;
  157. -        case VOTE_SIGNAL_ENDORSED:
  158. -            strReturn = "ENDORSED";
  159. -            break;
  160. -        case VOTE_SIGNAL_NOOP1:
  161. -            strReturn = "NOOP1";
  162. -            break;
  163. -        case VOTE_SIGNAL_NOOP2:
  164. -            strReturn = "NOOP2";
  165. -            break;
  166. -        case VOTE_SIGNAL_NOOP3:
  167. -            strReturn = "NOOP3";
  168. -            break;
  169. -        case VOTE_SIGNAL_NOOP4:
  170. -            strReturn = "NOOP4";
  171. -            break;
  172. -        case VOTE_SIGNAL_NOOP5:
  173. -            strReturn = "NOOP5";
  174. -            break;
  175. -        case VOTE_SIGNAL_NOOP6:
  176. -            strReturn = "NOOP6";
  177. -            break;
  178. -        case VOTE_SIGNAL_NOOP7:
  179. -            strReturn = "NOOP7";
  180. -            break;
  181. -        case VOTE_SIGNAL_NOOP8:
  182. -            strReturn = "NOOP8";
  183. -            break;
  184. -        case VOTE_SIGNAL_NOOP9:
  185. -            strReturn = "NOOP9";
  186. -            break;
  187. -        case VOTE_SIGNAL_NOOP10:
  188. -            strReturn = "NOOP10";
  189. -            break;
  190. -        case VOTE_SIGNAL_NOOP11:
  191. -            strReturn = "NOOP11";
  192. -            break;
  193. -        case VOTE_SIGNAL_CUSTOM1:
  194. -            strReturn = "CUSTOM1";
  195. -            break;
  196. -        case VOTE_SIGNAL_CUSTOM2:
  197. -            strReturn = "CUSTOM2";
  198. -            break;
  199. -        case VOTE_SIGNAL_CUSTOM3:
  200. -            strReturn = "CUSTOM3";
  201. -            break;
  202. -        case VOTE_SIGNAL_CUSTOM4:
  203. -            strReturn = "CUSTOM4";
  204. -            break;
  205. -        case VOTE_SIGNAL_CUSTOM5:
  206. -            strReturn = "CUSTOM5";
  207. -            break;
  208. -        case VOTE_SIGNAL_CUSTOM6:
  209. -            strReturn = "CUSTOM6";
  210. -            break;
  211. -        case VOTE_SIGNAL_CUSTOM7:
  212. -            strReturn = "CUSTOM7";
  213. -            break;
  214. -        case VOTE_SIGNAL_CUSTOM8:
  215. -            strReturn = "CUSTOM8";
  216. -            break;
  217. -        case VOTE_SIGNAL_CUSTOM9:
  218. -            strReturn = "CUSTOM9";
  219. -            break;
  220. -        case VOTE_SIGNAL_CUSTOM10:
  221. -            strReturn = "CUSTOM10";
  222. -            break;
  223. -        case VOTE_SIGNAL_CUSTOM11:
  224. -            strReturn = "CUSTOM11";
  225. -            break;
  226. -        case VOTE_SIGNAL_CUSTOM12:
  227. -            strReturn = "CUSTOM12";
  228. -            break;
  229. -        case VOTE_SIGNAL_CUSTOM13:
  230. -            strReturn = "CUSTOM13";
  231. -            break;
  232. -        case VOTE_SIGNAL_CUSTOM14:
  233. -            strReturn = "CUSTOM14";
  234. -            break;
  235. -        case VOTE_SIGNAL_CUSTOM15:
  236. -            strReturn = "CUSTOM15";
  237. -            break;
  238. -        case VOTE_SIGNAL_CUSTOM16:
  239. -            strReturn = "CUSTOM16";
  240. -            break;
  241. -        case VOTE_SIGNAL_CUSTOM17:
  242. -            strReturn = "CUSTOM17";
  243. -            break;
  244. -        case VOTE_SIGNAL_CUSTOM18:
  245. -            strReturn = "CUSTOM18";
  246. -            break;
  247. -        case VOTE_SIGNAL_CUSTOM19:
  248. -            strReturn = "CUSTOM19";
  249. -            break;
  250. -        case VOTE_SIGNAL_CUSTOM20:
  251. -            strReturn = "CUSTOM20";
  252. -            break;
  253. -    }
  254. +    if(nSignal < VOTE_SIGNAL_MIN || nSignal > VOTE_SIGNAL_MAX)
  255. +        return vote_signal_strings[0];
  256.  
  257. -    return strReturn;
  258. +    return vote_signal_strings[nSignal];
  259.  }
  260.  
  261.  
  262. -vote_outcome_enum_t CGovernanceVoting::ConvertVoteOutcome(std::string strVoteOutcome)
  263. +bool CGovernanceVoting::ConvertVoteOutcome(std::string strVoteOutcome, vote_outcome_enum_t &eVoteRet)
  264.  {
  265. -    vote_outcome_enum_t eVote = VOTE_OUTCOME_NONE;
  266. -    if(strVoteOutcome == "yes") {
  267. -        eVote = VOTE_OUTCOME_YES;
  268. -    }
  269. -    else if(strVoteOutcome == "no") {
  270. -        eVote = VOTE_OUTCOME_NO;
  271. -    }
  272. -    else if(strVoteOutcome == "abstain") {
  273. -        eVote = VOTE_OUTCOME_ABSTAIN;
  274. +    boost::algorithm::to_upper(strVoteOutcome);
  275. +    std::map<std::string, vote_outcome_enum_t>::const_iterator it = mapVoteOutcome_t.find(strVoteOutcome);
  276. +    if(it == mapVoteOutcome_t.end()) {
  277. +        eVoteRet = VOTE_OUTCOME_NONE;
  278. +        return false;
  279.      }
  280. -    return eVote;
  281. +
  282. +    eVoteRet = it->second;
  283. +    return true;
  284.  }
  285.  
  286. -vote_signal_enum_t CGovernanceVoting::ConvertVoteSignal(std::string strVoteSignal)
  287. +bool CGovernanceVoting::ConvertVoteSignal(std::string strVoteSignal, vote_signal_enum_t &eSignalRet)
  288.  {
  289. -    vote_signal_enum_t eSignal = VOTE_SIGNAL_NONE;
  290. -    if(strVoteSignal == "funding") {
  291. -        eSignal = VOTE_SIGNAL_FUNDING;
  292. -    }
  293. -    else if(strVoteSignal == "valid") {
  294. -        eSignal = VOTE_SIGNAL_VALID;
  295. -    }
  296. -    if(strVoteSignal == "delete") {
  297. -        eSignal = VOTE_SIGNAL_DELETE;
  298. -    }
  299. -    if(strVoteSignal == "endorsed") {
  300. -        eSignal = VOTE_SIGNAL_ENDORSED;
  301. -    }
  302. +    eSignalRet = VOTE_SIGNAL_NONE;
  303.  
  304. -    if(eSignal != VOTE_SIGNAL_NONE)  {
  305. -        return eSignal;
  306. +    boost::algorithm::to_upper(strVoteSignal);
  307. +    std::map<std::string, vote_signal_enum_t>::const_iterator it = mapDefinedVoteSignal_t.find(strVoteSignal);
  308. +    if(it != mapDefinedVoteSignal_t.end()) {
  309. +        eSignalRet = it->second;
  310. +        return true;
  311.      }
  312.  
  313.      // ID FIVE THROUGH CUSTOM_START ARE TO BE USED BY GOVERNANCE ENGINE / TRIGGER SYSTEM
  314.  
  315. -    // convert custom sentinel outcomes to integer and store
  316. +    // convert custom sentinel signals to integer and store
  317.      try {
  318.          int i = boost::lexical_cast<int>(strVoteSignal);
  319.          if(i < VOTE_SIGNAL_CUSTOM1 || i > VOTE_SIGNAL_CUSTOM20) {
  320. -            eSignal = VOTE_SIGNAL_NONE;
  321. -        }
  322. -        else  {
  323. -            eSignal = vote_signal_enum_t(i);
  324. +            return false;
  325.          }
  326. +        eSignalRet = vote_signal_enum_t(i);
  327.      }
  328.      catch(std::exception const & e)
  329.      {
  330.          std::ostringstream ostr;
  331.          ostr << "CGovernanceVote::ConvertVoteSignal: error : " << e.what() << std::endl;
  332.          LogPrintf(ostr.str().c_str());
  333. +        return false;
  334.      }
  335.  
  336. -    return eSignal;
  337. +    return true;
  338. +}
  339. +
  340. +const std::string CGovernanceVoting::GetValidVoteOutcomeStrings()
  341. +{
  342. +    std::string strRet = "";
  343. +    for (int i = VOTE_OUTCOME_MIN; i <= VOTE_OUTCOME_MAX; ++i) {
  344. +        strRet += ConvertOutcomeToString(vote_outcome_enum_t(i)) + (i < VOTE_OUTCOME_MAX ? "|" : "");
  345. +    }
  346. +    boost::algorithm::to_lower(strRet);
  347. +    return strRet;
  348. +}
  349. +
  350. +const std::string CGovernanceVoting::GetValidVoteSignalStrings()
  351. +{
  352. +    std::string strRet = "";
  353. +    for (int i = VOTE_SIGNAL_MIN; i <= VOTE_SIGNAL_MAX_DEFINED; ++i) {
  354. +        strRet += ConvertSignalToString(vote_signal_enum_t(i)) + (i < VOTE_SIGNAL_MAX_DEFINED ? "|" : "");
  355. +    }
  356. +    boost::algorithm::to_lower(strRet);
  357. +    return strRet;
  358.  }
  359.  
  360.  CGovernanceVote::CGovernanceVote()
  361. @@ -269,15 +229,15 @@ bool CGovernanceVote::IsValid(bool fSignatureCheck)
  362.          return false;
  363.      }
  364.  
  365. -    // support up to 50 actions (implemented in sentinel)
  366. -    if(nVoteSignal > 50)
  367. +    // Should be in range of valid vote_signal_enum_t values (actuall actions are implemented in sentinel).
  368. +    if(nVoteSignal < VOTE_SIGNAL_MIN || (nVoteSignal > VOTE_SIGNAL_MAX_DEFINED && nVoteSignal < VOTE_SIGNAL_CUSTOM1) || nVoteSignal > VOTE_SIGNAL_MAX)
  369.      {
  370.          LogPrint("gobject", "CGovernanceVote::IsValid -- Client attempted to vote on invalid signal(%d) - %s\n", nVoteSignal, GetHash().ToString());
  371.          return false;
  372.      }
  373.  
  374. -    // 0=none, 1=yes, 2=no, 3=abstain. Beyond that reject votes
  375. -    if(nVoteOutcome > 3)
  376. +    // Should be in range of valid vote_outcome_enum_t values. Beyond that reject votes.
  377. +    if(nVoteOutcome < VOTE_OUTCOME_MIN || nVoteOutcome > VOTE_OUTCOME_MAX)
  378.      {
  379.          LogPrint("gobject", "CGovernanceVote::IsValid -- Client attempted to vote on invalid outcome(%d) - %s\n", nVoteSignal, GetHash().ToString());
  380.          return false;
  381. diff --git a/src/governance-vote.h b/src/governance-vote.h
  382. index 57aba35..fd2230e 100644
  383. --- a/src/governance-vote.h
  384. +++ b/src/governance-vote.h
  385. @@ -1,71 +1,73 @@
  386.  // Copyright (c) 2014-2016 The Dash Core developers
  387. -
  388.  // Distributed under the MIT/X11 software license, see the accompanying
  389.  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  390. +
  391.  #ifndef GOVERANCE_VOTE_H
  392.  #define GOVERANCE_VOTE_H
  393.  
  394. -#include "main.h"
  395. -#include "sync.h"
  396. -#include "net.h"
  397.  #include "key.h"
  398. -#include "util.h"
  399. -#include "base58.h"
  400. -#include "masternode.h"
  401. -#include <boost/lexical_cast.hpp>
  402. -#include "init.h"
  403. -
  404. -using namespace std;
  405. +#include "primitives/transaction.h"
  406.  
  407.  class CGovernanceVote;
  408.  
  409.  // INTENTION OF MASTERNODES REGARDING ITEM
  410. +// Note: keep votes values in sequence from min to max (no void places)
  411.  enum vote_outcome_enum_t  {
  412. -    VOTE_OUTCOME_NONE      = 0,
  413. -    VOTE_OUTCOME_YES       = 1,
  414. -    VOTE_OUTCOME_NO        = 2,
  415. -    VOTE_OUTCOME_ABSTAIN   = 3
  416. +    VOTE_OUTCOME_NONE           = -9999,
  417. +    // VOTE_OUTCOME_NO_STRONG      = -2,
  418. +    // VOTE_OUTCOME_NO_WEAK        = -1,
  419. +    VOTE_OUTCOME_NO             = -1,
  420. +    VOTE_OUTCOME_ABSTAIN        = 0,
  421. +    VOTE_OUTCOME_YES            = 1,
  422. +    // VOTE_OUTCOME_YES_WEAK       = 1,
  423. +    // VOTE_OUTCOME_YES_STRONG     = 2,
  424. +    // VOTE_OUTCOME_MIN            = VOTE_OUTCOME_NO_STRONG,
  425. +    // VOTE_OUTCOME_MAX            = VOTE_OUTCOME_YES_STRONG,
  426. +    VOTE_OUTCOME_MIN            = VOTE_OUTCOME_NO, // adjust this if NO range is extended
  427. +    VOTE_OUTCOME_MAX            = VOTE_OUTCOME_YES, // adjust this if YES range is extended
  428.  };
  429.  
  430. -
  431.  // SIGNAL VARIOUS THINGS TO HAPPEN:
  432.  enum vote_signal_enum_t  {
  433. -    VOTE_SIGNAL_NONE       = 0,
  434. -    VOTE_SIGNAL_FUNDING    = 1, //   -- fund this object for it's stated amount
  435. -    VOTE_SIGNAL_VALID      = 2, //   -- this object checks out in sentinel engine
  436. -    VOTE_SIGNAL_DELETE     = 3, //   -- this object should be deleted from memory entirely
  437. -    VOTE_SIGNAL_ENDORSED   = 4, //   -- officially endorsed by the network somehow (delegation)
  438. -    VOTE_SIGNAL_NOOP1      = 5, // FOR FURTHER EXPANSION
  439. -    VOTE_SIGNAL_NOOP2      = 6,
  440. -    VOTE_SIGNAL_NOOP3      = 7,
  441. -    VOTE_SIGNAL_NOOP4      = 8,
  442. -    VOTE_SIGNAL_NOOP5      = 9,
  443. -    VOTE_SIGNAL_NOOP6      = 10,
  444. -    VOTE_SIGNAL_NOOP7      = 11,
  445. -    VOTE_SIGNAL_NOOP8      = 12,
  446. -    VOTE_SIGNAL_NOOP9      = 13,
  447. -    VOTE_SIGNAL_NOOP10     = 14,
  448. -    VOTE_SIGNAL_NOOP11     = 15,
  449. -    VOTE_SIGNAL_CUSTOM1    = 16,  // SENTINEL CUSTOM ACTIONS
  450. -    VOTE_SIGNAL_CUSTOM2    = 17,  //        16-35
  451. -    VOTE_SIGNAL_CUSTOM3    = 18,
  452. -    VOTE_SIGNAL_CUSTOM4    = 19,
  453. -    VOTE_SIGNAL_CUSTOM5    = 20,
  454. -    VOTE_SIGNAL_CUSTOM6    = 21,
  455. -    VOTE_SIGNAL_CUSTOM7    = 22,
  456. -    VOTE_SIGNAL_CUSTOM8    = 23,
  457. -    VOTE_SIGNAL_CUSTOM9    = 24,
  458. -    VOTE_SIGNAL_CUSTOM10   = 25,
  459. -    VOTE_SIGNAL_CUSTOM11   = 26,
  460. -    VOTE_SIGNAL_CUSTOM12   = 27,
  461. -    VOTE_SIGNAL_CUSTOM13   = 28,
  462. -    VOTE_SIGNAL_CUSTOM14   = 29,
  463. -    VOTE_SIGNAL_CUSTOM15   = 30,
  464. -    VOTE_SIGNAL_CUSTOM16   = 31,
  465. -    VOTE_SIGNAL_CUSTOM17   = 32,
  466. -    VOTE_SIGNAL_CUSTOM18   = 33,
  467. -    VOTE_SIGNAL_CUSTOM19   = 34,
  468. -    VOTE_SIGNAL_CUSTOM20   = 35
  469. +    VOTE_SIGNAL_NONE        = -9999,
  470. +    VOTE_SIGNAL_FUNDING     = 1, //   -- fund this object for it's stated amount
  471. +    VOTE_SIGNAL_VALID       = 2, //   -- this object checks out in sentinel engine
  472. +    VOTE_SIGNAL_DELETE      = 3, //   -- this object should be deleted from memory entirely
  473. +    VOTE_SIGNAL_ENDORSED    = 4, //   -- officially endorsed by the network somehow (delegation)
  474. +    VOTE_SIGNAL_NOOP1       = 5, // FOR FURTHER EXPANSION
  475. +    VOTE_SIGNAL_NOOP2       = 6,
  476. +    VOTE_SIGNAL_NOOP3       = 7,
  477. +    VOTE_SIGNAL_NOOP4       = 8,
  478. +    VOTE_SIGNAL_NOOP5       = 9,
  479. +    VOTE_SIGNAL_NOOP6       = 10,
  480. +    VOTE_SIGNAL_NOOP7       = 11,
  481. +    VOTE_SIGNAL_NOOP8       = 12,
  482. +    VOTE_SIGNAL_NOOP9       = 13,
  483. +    VOTE_SIGNAL_NOOP10      = 14,
  484. +    VOTE_SIGNAL_NOOP11      = 15,
  485. +    VOTE_SIGNAL_CUSTOM1     = 16,  // SENTINEL CUSTOM ACTIONS
  486. +    VOTE_SIGNAL_CUSTOM2     = 17,  //        16-35
  487. +    VOTE_SIGNAL_CUSTOM3     = 18,
  488. +    VOTE_SIGNAL_CUSTOM4     = 19,
  489. +    VOTE_SIGNAL_CUSTOM5     = 20,
  490. +    VOTE_SIGNAL_CUSTOM6     = 21,
  491. +    VOTE_SIGNAL_CUSTOM7     = 22,
  492. +    VOTE_SIGNAL_CUSTOM8     = 23,
  493. +    VOTE_SIGNAL_CUSTOM9     = 24,
  494. +    VOTE_SIGNAL_CUSTOM10    = 25,
  495. +    VOTE_SIGNAL_CUSTOM11    = 26,
  496. +    VOTE_SIGNAL_CUSTOM12    = 27,
  497. +    VOTE_SIGNAL_CUSTOM13    = 28,
  498. +    VOTE_SIGNAL_CUSTOM14    = 29,
  499. +    VOTE_SIGNAL_CUSTOM15    = 30,
  500. +    VOTE_SIGNAL_CUSTOM16    = 31,
  501. +    VOTE_SIGNAL_CUSTOM17    = 32,
  502. +    VOTE_SIGNAL_CUSTOM18    = 33,
  503. +    VOTE_SIGNAL_CUSTOM19    = 34,
  504. +    VOTE_SIGNAL_CUSTOM20    = 35,
  505. +    VOTE_SIGNAL_MIN         = VOTE_SIGNAL_FUNDING,
  506. +    VOTE_SIGNAL_MAX_DEFINED = VOTE_SIGNAL_ENDORSED, // adjust this if more signals are defined i.e. next NOOP became smth meaningful
  507. +    VOTE_SIGNAL_MAX         = VOTE_SIGNAL_CUSTOM20, // adjust this if more custom signals are added
  508.  };
  509.  
  510.  /**
  511. @@ -76,11 +78,23 @@ enum vote_signal_enum_t  {
  512.  
  513.  class CGovernanceVoting
  514.  {
  515. +private:
  516. +    const static std::string vote_outcome_strings[];
  517. +    const static std::map<std::string, vote_outcome_enum_t> CreateVoteOutomeMap();
  518. +    const static std::map<std::string, vote_outcome_enum_t> mapVoteOutcome_t;
  519. +
  520. +    const static std::string vote_signal_strings[];
  521. +    const static std::map<std::string, vote_signal_enum_t> CreateDefinedVoteSignalMap();
  522. +    const static std::map<std::string, vote_signal_enum_t> mapDefinedVoteSignal_t;
  523. +
  524.  public:
  525. -    static vote_outcome_enum_t ConvertVoteOutcome(std::string strVoteOutcome);
  526. -    static vote_signal_enum_t ConvertVoteSignal(std::string strVoteSignal);
  527. +    static bool ConvertVoteOutcome(std::string strVoteOutcome, vote_outcome_enum_t &eVoteRet);
  528. +    static bool ConvertVoteSignal(std::string strVoteSignal, vote_signal_enum_t &eSignalRet);
  529.      static std::string ConvertOutcomeToString(vote_outcome_enum_t nOutcome);
  530.      static std::string ConvertSignalToString(vote_signal_enum_t nSignal);
  531. +
  532. +    static const std::string GetValidVoteOutcomeStrings();
  533. +    static const std::string GetValidVoteSignalStrings();
  534.  };
  535.  
  536.  //
  537. diff --git a/src/governance.cpp b/src/governance.cpp
  538. index 8aa516e..46ed94c 100644
  539. --- a/src/governance.cpp
  540. +++ b/src/governance.cpp
  541. @@ -1060,12 +1060,20 @@ int CGovernanceObject::GetAbsoluteNoCount(vote_signal_enum_t eVoteSignalIn)
  542.  
  543.  int CGovernanceObject::GetYesCount(vote_signal_enum_t eVoteSignalIn)
  544.  {
  545. -    return governance.CountMatchingVotes((*this), eVoteSignalIn, VOTE_OUTCOME_YES);
  546. +    int nCount = 0;
  547. +    for (int i = VOTE_OUTCOME_MAX; i > VOTE_OUTCOME_ABSTAIN ; --i) {
  548. +        nCount += governance.CountMatchingVotes((*this), eVoteSignalIn, vote_outcome_enum_t(i)) * i;
  549. +    }
  550. +    return nCount/VOTE_OUTCOME_MAX; // need to keep votes normalized
  551.  }
  552.  
  553.  int CGovernanceObject::GetNoCount(vote_signal_enum_t eVoteSignalIn)
  554.  {
  555. -    return governance.CountMatchingVotes((*this), eVoteSignalIn, VOTE_OUTCOME_NO);
  556. +    int nCount = 0;
  557. +    for (int i = VOTE_OUTCOME_MIN; i < VOTE_OUTCOME_ABSTAIN ; ++i) {
  558. +        nCount += governance.CountMatchingVotes((*this), eVoteSignalIn, vote_outcome_enum_t(i)) * i;
  559. +    }
  560. +    return nCount/VOTE_OUTCOME_MIN; // need to keep votes normalized
  561.  }
  562.  
  563.  int CGovernanceObject::GetAbstainCount(vote_signal_enum_t eVoteSignalIn)
  564. diff --git a/src/rpcgovernance.cpp b/src/rpcgovernance.cpp
  565. index 1271701..1a674d6 100644
  566. --- a/src/rpcgovernance.cpp
  567. +++ b/src/rpcgovernance.cpp
  568. @@ -218,7 +218,9 @@ UniValue gobject(const UniValue& params, bool fHelp)
  569.      if(strCommand == "vote-conf")
  570.      {
  571.          if(params.size() != 4)
  572. -            throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject vote-conf <governance-hash> [funding|valid|delete] [yes|no|abstain]'");
  573. +            throw JSONRPCError(RPC_INVALID_PARAMETER,
  574. +                strprintf("Correct usage is 'gobject vote-conf <governance-hash> [%s] [%s]'",
  575. +                    CGovernanceVoting::GetValidVoteSignalStrings(), CGovernanceVoting::GetValidVoteOutcomeStrings()));
  576.  
  577.          uint256 hash;
  578.          std::string strVote;
  579. @@ -227,16 +229,18 @@ UniValue gobject(const UniValue& params, bool fHelp)
  580.          std::string strVoteSignal = params[2].get_str();
  581.          std::string strVoteOutcome = params[3].get_str();
  582.  
  583. -        vote_signal_enum_t eVoteSignal = CGovernanceVoting::ConvertVoteSignal(strVoteSignal);
  584. -        if(eVoteSignal == VOTE_SIGNAL_NONE) {
  585. +        vote_signal_enum_t eVoteSignal;
  586. +        if(!CGovernanceVoting::ConvertVoteSignal(strVoteSignal, eVoteSignal)) {
  587.              throw JSONRPCError(RPC_INVALID_PARAMETER,
  588. -                               "Invalid vote signal. Please using one of the following: "
  589. -                               "(funding|valid|delete|endorsed) OR `custom sentinel code` ");
  590. +                               strprintf("Invalid vote signal. Please using one of the following: "
  591. +                                   "(%s) OR `custom sentinel code`", CGovernanceVoting::GetValidVoteSignalStrings()));
  592.          }
  593.  
  594. -        vote_outcome_enum_t eVoteOutcome = CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome);
  595. -        if(eVoteOutcome == VOTE_OUTCOME_NONE) {
  596. -            throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'");
  597. +        vote_outcome_enum_t eVoteOutcome;
  598. +        if(!CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome, eVoteOutcome)) {
  599. +            throw JSONRPCError(RPC_INVALID_PARAMETER,
  600. +                                strprintf("Invalid vote outcome. Please use one of the following: [%s]",
  601. +                                    CGovernanceVoting::GetValidVoteOutcomeStrings()));
  602.          }
  603.  
  604.          int success = 0;
  605. @@ -297,7 +301,9 @@ UniValue gobject(const UniValue& params, bool fHelp)
  606.      if(strCommand == "vote-many")
  607.      {
  608.          if(params.size() != 4)
  609. -            throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject vote-many <governance-hash> [funding|valid|delete] [yes|no|abstain]'");
  610. +            throw JSONRPCError(RPC_INVALID_PARAMETER,
  611. +                strprintf("Correct usage is 'gobject vote-many <governance-hash> [%s] [%s]'",
  612. +                    CGovernanceVoting::GetValidVoteSignalStrings(), CGovernanceVoting::GetValidVoteOutcomeStrings()));
  613.  
  614.          uint256 hash;
  615.          std::string strVote;
  616. @@ -307,16 +313,18 @@ UniValue gobject(const UniValue& params, bool fHelp)
  617.          std::string strVoteOutcome = params[3].get_str();
  618.  
  619.  
  620. -        vote_signal_enum_t eVoteSignal = CGovernanceVoting::ConvertVoteSignal(strVoteSignal);
  621. -        if(eVoteSignal == VOTE_SIGNAL_NONE) {
  622. +        vote_signal_enum_t eVoteSignal;
  623. +        if(!CGovernanceVoting::ConvertVoteSignal(strVoteSignal, eVoteSignal)) {
  624.              throw JSONRPCError(RPC_INVALID_PARAMETER,
  625. -                               "Invalid vote signal. Please using one of the following: "
  626. -                               "(funding|valid|delete|endorsed) OR `custom sentinel code` ");
  627. +                               strprintf("Invalid vote signal. Please using one of the following: "
  628. +                                   "(%s) OR `custom sentinel code`", CGovernanceVoting::GetValidVoteSignalStrings()));
  629.          }
  630.  
  631. -        vote_outcome_enum_t eVoteOutcome = CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome);
  632. -        if(eVoteOutcome == VOTE_OUTCOME_NONE) {
  633. -            throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'");
  634. +        vote_outcome_enum_t eVoteOutcome;
  635. +        if(!CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome, eVoteOutcome)) {
  636. +            throw JSONRPCError(RPC_INVALID_PARAMETER,
  637. +                                strprintf("Invalid vote outcome. Please use one of the following: [%s]",
  638. +                                    CGovernanceVoting::GetValidVoteOutcomeStrings()));
  639.          }
  640.  
  641.          int success = 0;
  642. @@ -403,7 +411,9 @@ UniValue gobject(const UniValue& params, bool fHelp)
  643.      if(strCommand == "vote-alias")
  644.      {
  645.          if(params.size() != 5)
  646. -            throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject vote-alias <governance-hash> [funding|valid|delete] [yes|no|abstain] <alias-name>'");
  647. +            throw JSONRPCError(RPC_INVALID_PARAMETER,
  648. +                strprintf("Correct usage is 'gobject vote-alias <governance-hash> [f%s] [%s] <alias-name>'",
  649. +                    CGovernanceVoting::GetValidVoteSignalStrings(), CGovernanceVoting::GetValidVoteOutcomeStrings()));
  650.  
  651.          uint256 hash;
  652.          std::string strVote;
  653. @@ -417,16 +427,18 @@ UniValue gobject(const UniValue& params, bool fHelp)
  654.  
  655.          // CONVERT NAMED SIGNAL/ACTION AND CONVERT
  656.  
  657. -        vote_signal_enum_t eVoteSignal = CGovernanceVoting::ConvertVoteSignal(strVoteSignal);
  658. -        if(eVoteSignal == VOTE_SIGNAL_NONE) {
  659. +        vote_signal_enum_t eVoteSignal;
  660. +        if(!CGovernanceVoting::ConvertVoteSignal(strVoteSignal, eVoteSignal)) {
  661.              throw JSONRPCError(RPC_INVALID_PARAMETER,
  662. -                               "Invalid vote signal. Please using one of the following: "
  663. -                               "(funding|valid|delete|endorsed) OR `custom sentinel code` ");
  664. +                               strprintf("Invalid vote signal. Please using one of the following: "
  665. +                                   "(%s) OR `custom sentinel code`", CGovernanceVoting::GetValidVoteSignalStrings()));
  666.          }
  667.  
  668. -        vote_outcome_enum_t eVoteOutcome = CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome);
  669. -        if(eVoteOutcome == VOTE_OUTCOME_NONE) {
  670. -            throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'");
  671. +        vote_outcome_enum_t eVoteOutcome;
  672. +        if(!CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome, eVoteOutcome)) {
  673. +            throw JSONRPCError(RPC_INVALID_PARAMETER,
  674. +                                strprintf("Invalid vote outcome. Please use one of the following: [%s]",
  675. +                                    CGovernanceVoting::GetValidVoteOutcomeStrings()));
  676.          }
  677.  
  678.          // EXECUTE VOTE FOR EACH MASTERNODE, COUNT SUCCESSES VS FAILURES
  679. @@ -701,8 +713,9 @@ UniValue voteraw(const UniValue& params, bool fHelp)
  680.  {
  681.      if (fHelp || params.size() != 6)
  682.          throw std::runtime_error(
  683. -                "voteraw <masternode-tx-hash> <masternode-tx-index> <governance-hash> <vote-signal> [yes|no|abstain] <time> <vote-sig>\n"
  684. -                "Compile and relay a governance vote with provided external signature instead of signing vote internally\n"
  685. +                strprintf("voteraw <masternode-tx-hash> <masternode-tx-index> <governance-hash> [%s] [%s] <time> <vote-sig>\n"
  686. +                    "Compile and relay a governance vote with provided external signature instead of signing vote internally\n",
  687. +                    CGovernanceVoting::GetValidVoteSignalStrings(), CGovernanceVoting::GetValidVoteOutcomeStrings())
  688.                  );
  689.  
  690.      uint256 hashMnTx = ParseHashV(params[0], "mn tx hash");
  691. @@ -713,16 +726,18 @@ UniValue voteraw(const UniValue& params, bool fHelp)
  692.      std::string strVoteSignal = params[3].get_str();
  693.      std::string strVoteOutcome = params[4].get_str();
  694.  
  695. -    vote_signal_enum_t eVoteSignal = CGovernanceVoting::ConvertVoteSignal(strVoteSignal);
  696. -    if(eVoteSignal == VOTE_SIGNAL_NONE)  {
  697. +    vote_signal_enum_t eVoteSignal;
  698. +    if(!CGovernanceVoting::ConvertVoteSignal(strVoteSignal, eVoteSignal)) {
  699.          throw JSONRPCError(RPC_INVALID_PARAMETER,
  700. -                           "Invalid vote signal. Please using one of the following: "
  701. -                           "(funding|valid|delete|endorsed) OR `custom sentinel code` ");
  702. +                           strprintf("Invalid vote signal. Please using one of the following: "
  703. +                               "(%s) OR `custom sentinel code`", CGovernanceVoting::GetValidVoteSignalStrings()));
  704.      }
  705.  
  706. -    vote_outcome_enum_t eVoteOutcome = CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome);
  707. -    if(eVoteOutcome == VOTE_OUTCOME_NONE) {
  708. -        throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'");
  709. +    vote_outcome_enum_t eVoteOutcome;
  710. +    if(!CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome, eVoteOutcome)) {
  711. +        throw JSONRPCError(RPC_INVALID_PARAMETER,
  712. +                            strprintf("Invalid vote outcome. Please use one of the following: [%s]",
  713. +                                CGovernanceVoting::GetValidVoteOutcomeStrings()));
  714.      }
  715.  
  716.      int64_t nTime = params[5].get_int64();
  717. --
  718. 2.9.0
RAW Paste Data