From f7ac53b2d5de29951240718680443b62acfa6db2 Mon Sep 17 00:00:00 2001 From: Cave Spectre Date: Sat, 27 Jul 2019 13:36:55 -0400 Subject: [PATCH 1/2] [RPC] Correct issues with budget commands --- src/rpc/budget.cpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/rpc/budget.cpp b/src/rpc/budget.cpp index 8b91c03ddb2d..5ca3db4eed7a 100644 --- a/src/rpc/budget.cpp +++ b/src/rpc/budget.cpp @@ -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(); From 72611349f10c94de54162ab1a226a81dd9af35fd Mon Sep 17 00:00:00 2001 From: Cave Spectre Date: Thu, 1 Aug 2019 21:52:28 -0400 Subject: [PATCH 2/2] [Review] Convert runtime_error to JSONRPCError --- src/rpc/budget.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/rpc/budget.cpp b/src/rpc/budget.cpp index 5ca3db4eed7a..e05b61ff9b27 100644 --- a/src/rpc/budget.cpp +++ b/src/rpc/budget.cpp @@ -53,19 +53,19 @@ void checkBudgetInputs(const UniValue& params, std::string &strProposalName, std { strProposalName = SanitizeString(params[0].get_str()); if (strProposalName.size() > 20) - throw std::runtime_error("Invalid proposal name, limit of 20 characters."); + throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid proposal name, limit of 20 characters."); strURL = SanitizeString(params[1].get_str()); if (strURL.size() > 64) - throw std::runtime_error("Invalid url, limit of 64 characters."); + throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid url, limit of 64 characters."); nPaymentCount = params[2].get_int(); if (nPaymentCount < 1) - throw std::runtime_error("Invalid payment count, must be more than zero."); + throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid payment count, must be more than zero."); CBlockIndex* pindexPrev = chainActive.Tip(); - if (pindexPrev == NULL) - throw std::runtime_error("Try again after active chain is loaded"); + if (!pindexPrev) + throw JSONRPCError(RPC_IN_WARMUP, "Try again after active chain is loaded"); // Start must be in the next budget cycle or later const int budgetCycleBlocks = Params().GetBudgetCycleBlocks(); @@ -75,7 +75,7 @@ void checkBudgetInputs(const UniValue& params, std::string &strProposalName, std 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)); + throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nBlockMin)); } address = params[4].get_str(); @@ -108,7 +108,7 @@ UniValue preparebudget(const UniValue& params, bool fHelp) 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"); + throw JSONRPCError(RPC_IN_WARMUP, "Try again after active chain is loaded"); } LOCK2(cs_main, pwalletMain->cs_wallet);