Skip to content

Commit 4517646

Browse files
committed
Merge PR #965 - [RPC] Correct issues with budget commands
2 parents 9e2274a + 4b14327 commit 4517646

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/rpc/budget.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ void budgetToJSON(CBudgetProposal* pbudgetProposal, UniValue& bObj)
5151
void checkBudgetInputs(const UniValue& params, std::string &strProposalName, std::string &strURL,
5252
int &nPaymentCount, int &nBlockStart, CBitcoinAddress &address, CAmount &nAmount)
5353
{
54-
int nBlockMin = 0;
55-
CBlockIndex* pindexPrev = chainActive.Tip();
56-
54+
strProposalName = SanitizeString(params[0].get_str());
5755
if (strProposalName.size() > 20)
5856
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid proposal name, limit of 20 characters.");
5957

@@ -66,22 +64,20 @@ void checkBudgetInputs(const UniValue& params, std::string &strProposalName, std
6664
if (nPaymentCount < 1)
6765
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid payment count, must be more than zero.");
6866

69-
// Start must be in the next budget cycle
70-
if (pindexPrev != NULL) nBlockMin = pindexPrev->nHeight - pindexPrev->nHeight % Params().GetBudgetCycleBlocks() + Params().GetBudgetCycleBlocks();
71-
72-
nBlockStart = params[3].get_int();
73-
if (nBlockStart % Params().GetBudgetCycleBlocks() != 0) {
74-
int nNext = pindexPrev->nHeight - pindexPrev->nHeight % Params().GetBudgetCycleBlocks() + Params().GetBudgetCycleBlocks();
75-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nNext));
76-
}
67+
CBlockIndex* pindexPrev = chainActive.Tip();
68+
if (!pindexPrev)
69+
throw JSONRPCError(RPC_IN_WARMUP, "Try again after active chain is loaded");
7770

78-
int nBlockEnd = nBlockStart + (Params().GetBudgetCycleBlocks() * nPaymentCount); // End must be AFTER current cycle
71+
// Start must be in the next budget cycle or later
72+
const int budgetCycleBlocks = Params().GetBudgetCycleBlocks();
73+
int pHeight = pindexPrev->nHeight;
7974

80-
if (nBlockStart < nBlockMin)
81-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid block start, must be more than current height.");
75+
int nBlockMin = pHeight - (pHeight % budgetCycleBlocks) + budgetCycleBlocks;
8276

83-
if (nBlockEnd < pindexPrev->nHeight)
84-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid ending block, starting block + (payment_cycle*payments) must be more than current height.");
77+
nBlockStart = params[3].get_int();
78+
if ((nBlockStart < nBlockMin) || ((nBlockStart % budgetCycleBlocks) != 0)) {
79+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nBlockMin));
80+
}
8581

8682
address = params[4].get_str();
8783
if (!address.IsValid())
@@ -112,6 +108,10 @@ UniValue preparebudget(const UniValue& params, bool fHelp)
112108
HelpExampleCli("preparebudget", "\"test-proposal\" \"https://forum.pivx.org/t/test-proposal\" 2 820800 \"D9oc6C3dttUbv8zd7zGNq1qKBGf4ZQ1XEE\" 500") +
113109
HelpExampleRpc("preparebudget", "\"test-proposal\" \"https://forum.pivx.org/t/test-proposal\" 2 820800 \"D9oc6C3dttUbv8zd7zGNq1qKBGf4ZQ1XEE\" 500"));
114110

111+
if (!pwalletMain) {
112+
throw JSONRPCError(RPC_IN_WARMUP, "Try again after active chain is loaded");
113+
}
114+
115115
LOCK2(cs_main, pwalletMain->cs_wallet);
116116

117117
EnsureWalletIsUnlocked();

0 commit comments

Comments
 (0)