Skip to content

Conversation

@CaveSpectre11
Copy link

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.

random-zebra
random-zebra previously approved these changes Jul 27, 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 55408b8
Ivoking preparebudget before the wallet has fully started, correctly returns the error.
Successfully prepared and submitted budget on testnet.

@CaveSpectre11
Copy link
Author

Rebased on top of PR #964 (refactoring) for ease of merging later. Given the complications/complexity of the merge; I'm retesting.

@furszy furszy added this to the 4.0.0 milestone Jul 30, 2019
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 0041fd2

@CaveSpectre11
Copy link
Author

Added in the change from runtime_error to JSONRPCError. It should have been done here instead of #950; and was causing confusion on the merge into #950.

@Fuzzbawls
Copy link
Collaborator

needs rebase now that #964 is merged

@CaveSpectre11
Copy link
Author

needs rebase now that #964 is merged

Rebase done

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 7261134

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 7261134

@random-zebra
Copy link

Merging...

@random-zebra random-zebra merged commit 7261134 into PIVX-Project:master Aug 8, 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 RPCBudgetCrash 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