-
Notifications
You must be signed in to change notification settings - Fork 951
rpc: add check command #2123
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
rpc: add check command #2123
Conversation
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 an awesome idea! Some minor comments, and please add a CHANGELOG.md entry under Added:
- JSON API: New command
check
checks the validity of a JSON API call without running it.
common/json.c
Outdated
{ | ||
const jsmntok_t *first = params; | ||
const jsmntok_t *last = json_next(params); | ||
printf("size: %d, count: %ld\n", params->size, last - first); |
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.
"count: %td" (ptrdiff_t) BTW.
*dest++ = *first++; | ||
|
||
return arr; | ||
} |
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 think this is almost a 1-liner?
if (!tok)
return NULL;
return tal_dup_arr(ctx, jsmntok_t, tok, json_next(tok) - tok, 0);
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 took this approach at first, but it turns out the tok
is not a tal allocated array. It's an offset into a tal allocated array. Its setup in parse_request
(jsonrpc.c:506).
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 think it should still work... I'll try it!
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.
Ok you're right. I tried a getting the tal_length of tok and it didn't work, so I bailed out. My bad.
*first = '"'; | ||
first++; | ||
} | ||
} |
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.
Cute trick :)
} | ||
|
||
if (!param(cmd, buffer, mod_params, | ||
p_req("command_to_check", json_tok_command, &name_tok), |
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 would remove "_to_check" here, since it makes the JSON API neater (the context is pretty clear).
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.
... or just call it 'check'?
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.
command
seems better to me.
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 discussed my reason for choosing such an awkward name in the commit message for e45dd7f. Basically, it has to be unique to all other parameter names for all commands. But I'm fine with changing it to whatever you are comfortable with. I can also add a developer mode check to make sure it is not used again. Note @ZmnSCPxj, we already are using command
for the help command.
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.
Note that you can't make it unique, since 'check check' is supposed to work :) But clashing with some other command would be painful, so happy to stop bikeshedding now :)
@@ -79,7 +79,7 @@ static bool parse_by_position(struct command *cmd, | |||
} | |||
|
|||
/* check for unexpected trailing params */ | |||
if (tok != end) { | |||
if (!cmd->allow_unused && tok != end) { | |||
command_fail(cmd, JSONRPC2_INVALID_PARAMS, | |||
"too many parameters:" | |||
" got %u, expected %zu", |
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.
Can we modify parse_by_name similarly?
Currently:
$ ./cli/lightning-cli check command_to_check=listpeers id=xxx
{ "code" : -32602, "message" : "unknown parameter: 'id'" }
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.
oops.
f62d406
to
83fe4d4
Compare
I incorporated your suggested changes and rebased. This feature was oddly way more involved than I originally anticipated! But we did get a cool |
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 83fe4d4
*dest++ = *first++; | ||
|
||
return arr; | ||
} |
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 think it should still work... I'll try it!
OK, so I've got a few of additions if you want them in https://github.com/rustyrussell/lightning/commits/pr-2123 but they can be a separate PR and I don't want to hold this one up any longer! |
Your additions look great. I will make a new PR for them (unless you want to). I also started on the man page. |
Needed for check command. I left the print function in since it was so convenient for debugging purposes. Signed-off-by: Mark Beckwith <wythe@intrig.com>
We can now set a flag to have param() ignore unexpected parameters. Normally unexpected parameters are considered errors. Needed by the check command. Signed-off-by: Mark Beckwith <wythe@intrig.com>
The check command allows us to check the parameters of a command without running it. Example: lightning-cli check invoice 234 foo desc We do this by removing the "command_to_check" parameter and then using the remaining parameters as-is. I chose the parameter name "command_to_check" instead of just "command" because it must be unique to all other parameter names for all other commands. Why? Because it may be ambiguous in the case of a json object, where the parameters are not necessary ordered. We don't know which one is the command to check and which one is a parameter. Signed-off-by: Mark Beckwith <wythe@intrig.com>
Signed-off-by: Mark Beckwith <wythe@intrig.com>
Rebased on top of Will merge once CI is happy. ACK 8a361b4 |
You steal them :) Thanks! |
The
check
command will check the parameters of a command without running it; e.g.:And yes, you can check the
check
command:The first commit might be a bit overkill but it made for easy implementation.
While playing with this I thought it would be nice if the error case would present the usage string for
the command also. But that can be saved for later.