Skip to content

Conversation

fjahr
Copy link
Contributor

@fjahr fjahr commented Jul 17, 2020

Mini refactoring for DRYer code and better readability. Replaces several instances of bool parsing in the RPC code with a rpc/util function. Also uses const when appropriate.

@fjahr
Copy link
Contributor Author

fjahr commented Jul 17, 2020

#19501 introduces more verbose flags, which is how I got the idea from reviewing it.

if (!request.params[0].isNull())
fVerbose = request.params[0].get_bool();

const bool fVerbose = GetBool(request.params[0], false);
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this change behavior when passed an int?

If you want to refactor this to a single line without changing behavior, it would read maybe so:

Suggested change
const bool fVerbose = GetBool(request.params[0], false);
const bool fVerbose{request.params[0].isNull() ? false : request.params[0].get_bool()};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, sorry, I forgot to comment on that it would allow us to use ints in more cases. I guess the int case is only used in rawtransaction.cpp for historical reasons and we rather want to error in other cases? I removed the int part from the function and removed its use from the one place where it's relevant. And I know this can be made a one-liner but I don't like reading ternary operators for the same base case because I still feel like I need to make sure there is not something less intuitive going on like with the is_witness stuff, a ! hiding somewhere etc. But if you don't see the same benefit it's fine if we close it, I think just using ternary operators isn't worth the change.

Copy link
Member

Choose a reason for hiding this comment

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

I like the idea of factoring this out to a function, but I don't personally think ints should be accepted where bools are expected in more cases. Being specific about input types helps catch, for example, bugs when the argument is shifted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, I changed that already so there is no behavior change anymore now. I just didn't make a clearer comment on it.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 18, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

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

Concept ACK.

@fjahr
Copy link
Contributor Author

fjahr commented Aug 2, 2020

Took suggestions by @promag

The helper function encapsulates the boolean parse logic in one place.
@promag
Copy link
Contributor

promag commented Aug 2, 2020

Need to update PR title and reword commit.

@fjahr
Copy link
Contributor Author

fjahr commented Aug 2, 2020

Need to update PR title and reword commit.

Done, also rebased on master to get access to the git commit message linter that failed in the CI but not locally on my one-liner commit message.

@fjahr fjahr changed the title refactor: Add GetBool to rpc/util refactor: Add ParseBool to rpc/util Aug 2, 2020
@practicalswift
Copy link
Contributor

ACK c2551d0 -- suggested version is easier to reason about and patch looks correct

@maflcko
Copy link
Member

maflcko commented Aug 27, 2020

Should a helper for every type be added?

It would probably be better to hide all parsing etc within RPCHelpMan, so that the logic in rpc methods itself doesn't need to worry about that

@fjahr
Copy link
Contributor Author

fjahr commented Aug 28, 2020

Should a helper for every type be added?

It would probably be better to hide all parsing etc within RPCHelpMan, so that the logic in rpc methods itself doesn't need to worry about that

Yeah, makes sense I think. Unless that is something you are already working on, I will explore it and make a suggestion.

@maflcko
Copy link
Member

maflcko commented Aug 28, 2020

I'd suspect this can only be done after #18531

@DrahtBot
Copy link
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

Want to unsubscribe from rebase notifications on this pull request? Just convert this pull request to a "draft".

@fanquake
Copy link
Member

fanquake commented Apr 8, 2021

#18531 is now merged. @fjahr are you still interested in working on this?

@maflcko
Copy link
Member

maflcko commented Apr 8, 2021

Related: #20017

@fjahr
Copy link
Contributor Author

fjahr commented Apr 11, 2021

#18531 is now merged. @fjahr are you still interested in working on this?

Yes but I guess #20017 took the same idea and is further along now, so closing.

@fjahr fjahr closed this Apr 11, 2021
@fjahr fjahr deleted the rpc_bool branch April 11, 2021 14:54
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants