Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Combine parameter checking of budget commands #964

Merged
merged 1 commit into from
Aug 3, 2019

Conversation

CaveSpectre11
Copy link

@CaveSpectre11 CaveSpectre11 commented Jul 26, 2019

Release notes

  • [Refactor] Move common budget parameter checking code into a function

Details

The code in both preparebudget and submitbudget used the same parameter checking. This pulls that parameter checking and loading into a common routine used by both; for ease of improvements and adjustments to the parameter checking... so the risk of not adjusting them consistently in both routines is removed.

Note

The early discussion below is based on a combined PR that addressed this, plus #950 and a PR to be named later. The decision was made to split and focus; so the discussions can be independent of each other and refactor as needed.

Copy link

@random-zebra random-zebra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, thanks for your continuing efforts to improve PIVX code.
The quality of your work is definitely appreciated.

I like the proposed changes (I have a question after code inspection, will test it asap).

One minor thing, I think it would be slightly better (from a styling and performance point of view) to include the following variables (either as references or pointers) directly as arguments in checkBudgetInputs and parse them there from UniValue& params:

std::string strProposalName,
std::string strURL,
int nPaymentCount,
int nBlockStart,
CBitcoinAddress address,
CAmount nAmount

src/rpc/budget.cpp Outdated Show resolved Hide resolved
@CaveSpectre11
Copy link
Author

One minor thing, I think it would be slightly better (from a styling and performance point of view) to include the following variables (either as references or pointers) directly as arguments in checkBudgetInputs and parse them there from UniValue& params:

It was one thing I was considering; since I don't like pulling them out in the routine, and then pulling them out again. Now that I ponder it more; since the routine is pretty specific to exactly the parameters that are required; it probably does make sense to do that.

src/rpc/budget.cpp Outdated Show resolved Hide resolved
@Fuzzbawls
Copy link
Collaborator

Some thoughts on this particular PR:

  • Its doing too much (optimizing existing code and adding new functionality at the same time)
  • As convenient as Boost may be, we've set a precedent on trying to reduce it's usage, not add.
  • Do we really want to try to account for all these kinds of letter casing situations, or do we just want to be strict about it and say that (for our purposes) only lowercase http:// and https:// is considered "valid"?

Personally, i'm ok with being uber strict on the URL scheme character casing. Saves from having to rely on boost or a locale dependent function, as well as many lines of code. I'm pretty sure that most browsers and URL shortening websites end up with the standard lowercase scheme in a users clipboard when copying, so someone would actually have to go out of their way to end up with all upper or mixed casing in she URL scheme.

@random-zebra
Copy link

I think it makes sense to have both refactoring and new functionality in the same place, here, given the narrow focus of the PR. Maybe just split the commits.

But, after pondering some more, I agree on the other points.
Accounting for anything else other than plain lowercase is probably overkill here.

@Fuzzbawls
Copy link
Collaborator

@random-zebra I brought up the refactor + new functionality issue because I think everyone can agree that refactoring out the duplicate code is a good thing, but the new added functionality here is clearly still controversial and no consensus has been reached on the best way to proceed with it.

For this particular situation, it shouldn't be too big of a deal as @CaveSpectre11 is very much active and open to the idea of multiple change requests, but it is good practice (and actually stated in our contributing guidelines) in general to not mix refactorings with functional changes.

@CaveSpectre11
Copy link
Author

Some thoughts on this particular PR:

  • Do we really want to try to account for all these kinds of letter casing situations, or do we just want to be strict about it and say that (for our purposes) only lowercase http:// and https:// is considered "valid"?

I'm thinking including Http and Https might be beneficial; since a lot of phones make it a hassle to get your first character to not be an uppercase.

e..g. boil down to:

std::string validateURL(std::string strURL) {

    // Check URL size
    if (strURL.size() > 64)
        return std::string("Invalid url, limit of 64 characters.");

    std::vector<std::string> reqPre;

    // Required initial strings; URL must contain one
    reqPre.push_back("http://");
    reqPre.push_back("https://");
    reqPre.push_back("Http://");
    reqPre.push_back("Https://");

    // check fronts
    bool found = false;
    for (int i=0; i < reqPre.size() && !found; i++) {
        if (strURL.find(reqPre[i]) == 0) found = true;
    }
    if ((!found) && (reqPre.size() > 0))
        return std::string("Invalid URL, check scheme (e.g. https://)");

    return std::string("");
}

And expand on it at a later date if more checking is required.

@Fuzzbawls
Copy link
Collaborator

I think that if someone is advanced enough to manage to interact with a core wallet's RPC interface from their phone, they can ensure the URL scheme is in standard lowercase themselves :P

Also, even phone browsers end up re-writing the URL scheme to standard lowercase when resolving/loading a URL, so copying to a phone's clipboard would still satisfy the all lowercase requirement.

@CaveSpectre11
Copy link
Author

The code has been following the conversation; so it's pretty much good. What I'll do is push this one for the refactoring; and push the other one for the validateURL (after finding where best to move it), then just do the rebase when the time comes. The remaining question falls on the crash fixes... keep them in here, move them to the other one, or have them as a 3rd PR?

@Fuzzbawls
Copy link
Collaborator

Yeah I think that since the issue of how best to handle the URL scheme is still in debate, this PR can be changed to include just a code refactor of what currently exists in master (and renamed appropriately).

From there #950 can be retained for the purpose of enforcing whatever method of URL scheme restriction is eventually decided on.

Also, to keep things as atomic as possible, yes a new PR can be opened which is specifically to address the crash fix only.

@CaveSpectre11 CaveSpectre11 changed the title [RPC] Rework parameter checking of budget commands [Refactor] Combine parameter checking of budget commands Jul 27, 2019
@CaveSpectre11
Copy link
Author

PR is refactored to be just the refactor ;)

random-zebra
random-zebra previously approved these changes Jul 31, 2019
Copy link

@random-zebra random-zebra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 7834c5e and tested

getbudgetinfo
[
...
   {
      "Name": "PR964_testing",
      ...
      "fValid": true
   }
]

@Fuzzbawls
Copy link
Collaborator

needs a quick rebase, looks like #951 may have caused a small conflict

@CaveSpectre11
Copy link
Author

needs a quick rebase, looks like #951 may have caused a small conflict

done

Copy link
Collaborator

@Fuzzbawls Fuzzbawls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 983d97a

@Fuzzbawls Fuzzbawls requested a review from Warrows August 3, 2019 02:04
Copy link

@random-zebra random-zebra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 983d97a

@Fuzzbawls Fuzzbawls merged commit 983d97a into PIVX-Project:master Aug 3, 2019
Fuzzbawls added a commit that referenced this pull request Aug 3, 2019
983d97a [Refactor] Combine parameter checking of budget commands (Cave Spectre)

Pull request description:

  ### **Release notes**
  - [Refactor] Move common budget parameter checking code into a function

  ### **Details**
  The code in both `preparebudget` and `submitbudget` used the same parameter checking.  This pulls that parameter checking and loading into a common routine used by both; for ease of improvements and adjustments to the parameter checking... so the risk of not adjusting them consistently in both routines is removed.

  ### **Note**
  The early discussion below is based on a combined PR that addressed this, plus #950 and a PR to be named later.  The decision was made to split and focus; so the discussions can be independent of each other and refactor as needed.

ACKs for top commit:
  Fuzzbawls:
    ACK 983d97a
  random-zebra:
    ACK 983d97a

Tree-SHA512: a88eb41e06d1936db77f1cd9c4f04360b446c48d27652d57dfd6e4b119140d56e24b24e00e0ac48890cc78d4130f8f688a711f74899f9c9301d1aba22dff5315
@random-zebra random-zebra added this to the 4.0.0 milestone Aug 6, 2019
random-zebra added a commit that referenced this pull request Aug 8, 2019
7261134 [Review] Convert runtime_error to JSONRPCError (Cave Spectre)
f7ac53b [RPC] Correct issues with budget commands (Cave Spectre)

Pull request description:

  ### **Release notes**
  - [RPC] Fix potential wallet crashes in budget commands
  - [RPC] Remove unnecessary conditionals in parameter checking of budget commands

  ### **Details**
  **wallet segfault**
  `preparebudget`, invoked before the wallet has fully started, caused a segfault and crashed.  This was due to the lack of checking for `pwalletMain` before referencing cs_wallet.  This is solved with a throw error to tell the user to try again after the wallet has started.

  Furthermore; both `preparebudget` and `submitbudget` has a check for `pindexPrev` to conditionalize the assignment of nBlockMin (which references `pindexPrev->nHeight`); however later checks, `pindexPrev->nHeight` is referenced even if the pindexPrev check failed; which would have caused a crash if pindexPrev was NULL.  This would occur if `chainActive->Tip()` returns null.  Given the logic of `getnextsuperblock`, the check added to preparebudget and submitbudget will generate a throw error if there is no active chain tip.

  **Remove unnecessary conditionals and code**
  `pindexPrev` is loaded from `chainActive.Tip()` pindexPrev->nHeight is checked multiple times in the parameter checking.  For ease of reading, pindexPrev->nHeight is now saved in a variable as it is used multiple times in the execution of the RPC commands in question.  Likewise, two function calls are used to determine the constant length of a budget cycle `Params().GetBudgetCycleBlocks()`; and is used several times.  Storing this information in a constant is more efficient then several calls for the information.

  nBlockMin is determined (the next superblock).  However later nNext was defined to be the exact same thing.  It's not necessary to build them both.

  By nature of validating that the chosen budget cycle block is greater than the current block, and validating the number of cycles is greater than zero; the need to check the end of the budget cycle is unnecessary.

  Having a separate throw message for choosing the wrong block (not a budget block), and specifying a block that has passed is unnecessary.  These have been combined so that the user, putting in the wrong block, is informed, in both cases, what the next budget block is.

  ### **Note**
  This PR was originally part of #964, but split out to distinguish between these changes and the other's refactoring.

ACKs for top commit:
  random-zebra:
    ACK 7261134
  Fuzzbawls:
    ACK 7261134

Tree-SHA512: b0ccabae52d02767971104353b48d4efb76926017d7099b52b341d525710aa3a70d343c92c260048bb1c288990a914730a4dd0832578f07e96abb384d628cd4e
@CaveSpectre11 CaveSpectre11 deleted the budgetParamChecks branch August 9, 2019 01:20
@random-zebra random-zebra modified the milestones: 4.0.0, 3.4.0 Aug 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants