message: [RPC] Correct issues with budget commands
diff: @@ -51,9 +51,6 @@ void budgetToJSON(CBudgetProposal* pbudgetProposal, UniValue& bObj)
  void checkBudgetInputs(const UniValue& params, std::string &strProposalName, std::string &strURL,
  int &nPaymentCount, int &nBlockStart, CBitcoinAddress &address, CAmount &nAmount)
  {
  - int nBlockMin = 0;
  - CBlockIndex* pindexPrev = chainActive.Tip();
  -
  strProposalName = SanitizeString(params[0].get_str());
  if (strProposalName.size() > 20)
  throw std::runtime_error("Invalid proposal name, limit of 20 characters.");
  @@ -66,22 +63,20 @@ void checkBudgetInputs(const UniValue& params, std::string &strProposalName, std
  if (nPaymentCount < 1)
  throw std::runtime_error("Invalid payment count, must be more than zero.");
  - // Start must be in the next budget cycle
  - if (pindexPrev != NULL) nBlockMin = pindexPrev->nHeight - pindexPrev->nHeight % Params().GetBudgetCycleBlocks() + Params().GetBudgetCycleBlocks();
  -
  - nBlockStart = params[3].get_int();
  - if (nBlockStart % Params().GetBudgetCycleBlocks() != 0) {
  - int nNext = pindexPrev->nHeight - pindexPrev->nHeight % Params().GetBudgetCycleBlocks() + Params().GetBudgetCycleBlocks();
  - throw std::runtime_error(strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nNext));
  - }
  + CBlockIndex* pindexPrev = chainActive.Tip();
  + if (pindexPrev == NULL)
  + throw std::runtime_error("Try again after active chain is loaded");
  - int nBlockEnd = nBlockStart + (Params().GetBudgetCycleBlocks() * nPaymentCount); // End must be AFTER current cycle
  + // Start must be in the next budget cycle or later
  + const int budgetCycleBlocks = Params().GetBudgetCycleBlocks();
  + int pHeight = pindexPrev->nHeight;
  - if (nBlockStart < nBlockMin)
  - throw std::runtime_error("Invalid block start, must be more than current height.");
  + int nBlockMin = pHeight - (pHeight % budgetCycleBlocks) + budgetCycleBlocks;
  - if (nBlockEnd < pindexPrev->nHeight)
  - throw std::runtime_error("Invalid ending block, starting block + (payment_cycle*payments) must be more than current height.");
  + nBlockStart = params[3].get_int();
  + if ((nBlockStart < nBlockMin) || ((nBlockStart % budgetCycleBlocks) != 0)) {
  + throw std::runtime_error(strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nBlockMin));
  + }
  address = params[4].get_str();
  if (!address.IsValid())
  @@ -112,6 +107,10 @@ UniValue preparebudget(const UniValue& params, bool fHelp)
  HelpExampleCli("preparebudget", "\"test-proposal\" \"https://forum.pivx.org/t/test-proposal\" 2 820800 \"D9oc6C3dttUbv8zd7zGNq1qKBGf4ZQ1XEE\" 500") +
  HelpExampleRpc("preparebudget", "\"test-proposal\" \"https://forum.pivx.org/t/test-proposal\" 2 820800 \"D9oc6C3dttUbv8zd7zGNq1qKBGf4ZQ1XEE\" 500"));
  + if (!pwalletMain) {
  + throw std::runtime_error("Try again after wallet is fully started");
  + }
  +
  LOCK2(cs_main, pwalletMain->cs_wallet);
  EnsureWalletIsUnlocked();
  