-
Notifications
You must be signed in to change notification settings - Fork 37.7k
cli: Handle arguments that can be either JSON or string #33230
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
base: master
Are you sure you want to change the base?
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33230. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsReviewers, 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. |
281802b
to
01039d3
Compare
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
01039d3
to
aabf1f6
Compare
Seems like a reasonable change. I wonder if an alternate solution might have the client treat hash_or_height as a string parameter instead of a JSON parameter, and update the server to accept height strings? But I guess that might not be backwards compatible if there are old scripts calling bitcoin-cli with extra quotes around the hash string. |
Even if there is, there are other use cases taking lists and strings as parameters. See: #32468 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approach ACK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK aabf1f6
This is quite practical, went thru these errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this basically works for the existing params, since a block hash is never valid JSON (which forbids leading zeros), but could be dangerous to use for new parameters that are ambiguous.
With that in mind, it might be worth instead having a new RPCResult::Type::BLOCK_HEIGHT_OR_HASH
type to sanity check this. Or at least a warning comment (but I don't know where you'd put it to ensure it's seen).
@@ -32,7 +32,7 @@ def process_mapping(fname): | |||
if line.startswith('};'): | |||
in_rpcs = False | |||
elif '{' in line and '"' in line: | |||
m = re.search(r'{ *("[^"]*"), *([0-9]+) *, *("[^"]*") *},', line) | |||
m = re.search(r'{ *("[^"]*"), *([0-9]+) *, *("[^"]*") *(, \/\*also_string=\*\/true *)?},', line) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be a good idea to actually test this is correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is here because the test fails without it.
Github-Pull: bitcoin#33230 Rebased-From: 009c972
I don't think we have any types where that would be the case. But this is also something that needs to be turned on for a parameter explicitly, so I don't expect this to be problematic.
Part of the impetus for this is #32468 where the parameter can be either is a JSON array or a string, so something that is more generic is better.
I think with it being default off makes that not really necessary. |
Concept ACK |
ACK aabf1f6 nit: It may be a good idea to also update the example command in the help message of getblockstats. Given that this PR eliminates the need for quotes around arguments, it would be good to update the example accordingly by using plain text instead of the quoted format. bitcoin/src/rpc/blockchain.cpp Line 1943 in 73220fc
|
ACK 111938c This change makes sense. I've definitely run into this issue, and being able to use a regular string is less confusing I checked that with the change to I was wondering if it makes sense to keep one test using |
{ "gettxoutsetinfo", 2, "use_index"}, | ||
{ "dumptxoutset", 2, "options" }, | ||
{ "dumptxoutset", 2, "rollback" }, | ||
{ "dumptxoutset", 2, "rollback", /*also_string=*/true }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this entry have an effect? In the positional map members
, the key ("dumptxoutset", 2)
is first set to false
. My understanding is that subsequent encounters of the same key are ignored by std::map::emplace
. Is this the desired behavior, or should the subsequent value overwrite the first?
{ "dumptxoutset", 2, "options", /*also_string=*/false }, // inserted
{ "dumptxoutset", 2, "rollback", /*also_string=*/true }, // ignored or used to overwrite?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK aabf1f6 — improves CLI ergonomics for mixed JSON/string RPC params
There are some RPCs where the argument can be either JSON that needs to be parsed, or a string that we can pass straight through. However,
bitcoin-cli
would always parse those arguments as JSON which makes for some cumbersome argument passing when using those RPCs. Notably,hash_or_height
ingetblockstats
andgettxoutsetinfo
do this, and results in a more cumbersome command ofbitcoin-cli getblockstats '"<hash>"'
. Otherwise, using a normal invocation ofbitcoin-cli getblockstats <hash>
results inerror: Error parsing JSON
. This PR marks those particular options as also being a string so that whenbitcoin-cli
fails to parse the argument as JSON, it will assume that the argument is a string and pass it straight through.