Skip to content

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

Merged
merged 4 commits into from
Dec 6, 2018
Merged

Conversation

wythe
Copy link
Contributor

@wythe wythe commented Nov 29, 2018

The check command will check the parameters of a command without running it; e.g.:

#cli/lightning-cli check invoice ant hello description
{ "code" : -32602, "message" : "'msatoshi' should be a positive number or 'any', not 'ant'" }

# cli/lightning-cli check invoice any hello description
{
  "command": "invoice", 
  "parameters": "ok"
}

And yes, you can check the check command:

# cli/lightning-cli check check
{ "code" : -32602, "message" : "missing required parameter: 'command_to_check'" }

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.

Copy link
Contributor

@rustyrussell rustyrussell left a 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);
Copy link
Contributor

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;
}
Copy link
Contributor

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);

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 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).

Copy link
Contributor

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!

Copy link
Contributor Author

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++;
}
}
Copy link
Contributor

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),
Copy link
Contributor

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).

Copy link
Contributor

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'?

Copy link
Contributor

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.

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 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.

Copy link
Contributor

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",
Copy link
Contributor

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'" }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops.

@wythe wythe force-pushed the rpc-check branch 2 times, most recently from f62d406 to 83fe4d4 Compare December 4, 2018 17:36
@wythe
Copy link
Contributor Author

wythe commented Dec 4, 2018

I incorporated your suggested changes and rebased. This feature was oddly way more involved than I originally anticipated! But we did get a cool json_tok_print function out of it.

Copy link
Contributor

@rustyrussell rustyrussell left a 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;
}
Copy link
Contributor

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!

@rustyrussell
Copy link
Contributor

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!

@wythe
Copy link
Contributor Author

wythe commented Dec 5, 2018

Your additions look great. I will make a new PR for them (unless you want to). I also started on the man page.

wythe added 4 commits December 5, 2018 23:38
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>
@cdecker
Copy link
Member

cdecker commented Dec 5, 2018

Rebased on top of master, checked locally.

Will merge once CI is happy.

ACK 8a361b4

@rustyrussell
Copy link
Contributor

Your additions look great. I will make a new PR for them (unless you want to). I also started on the man page.

You steal them :)

Thanks!

@rustyrussell rustyrussell merged commit 164c764 into ElementsProject:master Dec 6, 2018
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