Skip to content
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: Be able to access RPC parameters by name #27788

Closed
wants to merge 5 commits into from

Conversation

achow101
Copy link
Member

@achow101 achow101 commented May 30, 2023

Although we accept named RPC parameters, we still access the parameters by position within the RPC implementations. This PR makes it possible to access these parameters directly by name. This is achieved by holding the parameters in a separate JSONRPCParameters class which contains mappings from both name to value, and position to value. Two operator[] methods are implemented which can accept both strings and ints. This allows for backwards compatibility, while also allowing for future implementation to access the parameters by name.

The processing of named arguments ends up being entirely overhauled in order to be able to store the mappings to the parameters.

Based on #26485 to avoid conflicting with it.

@DrahtBot
Copy link
Contributor

DrahtBot commented May 30, 2023

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

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK jarolrod, stickies-v

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #29129 (wallet, rpc: add BIP44 account in createwallet by brunoerg)
  • #28560 (wallet, rpc: FundTransaction refactor by josibake)
  • #28201 (Silent Payments: sending by josibake)

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.

@jarolrod
Copy link
Member

jarolrod commented Jun 2, 2023

Concept ACK

@maflcko
Copy link
Member

maflcko commented Jun 15, 2023

Might be nice to at least update one method to use the new code to see that it compiles and passes the existing tests?

See also #20017

@achow101
Copy link
Member Author

Might be nice to at least update one method to use the new code to see that it compiles and passes the existing tests?

Added a commit to update the RPCs that have >= 6 args to use these named params.

@luke-jr
Copy link
Member

luke-jr commented Jun 24, 2023

#17356

@maflcko
Copy link
Member

maflcko commented Sep 3, 2023

The main risk I see, is that helper functions that currently expect a UniValue parameter (e.g. SetFeeEstimateMode) will need to be refactored to take the underlying type instead.

I don't think there is a need to? Arg<>() should be trivial to extend with a Arg<UniValue>() implementation, no?

@stickies-v
Copy link
Contributor

It's trivial, but do we want to? It doesn't really add significant benefit (besides accessing by name), and importantly it'd muddy the interface. For example, with MaybeArg, would we return a UniValue* or just UniValue? The first would be confusing to use imo, the last one would be changing the interface. Also, I think pushing the conversion of UniValue to the underlying type as closely to the edge as possible is a worthwhile effort so building an interface to do the opposite seems counterproductive.

In cases where eliminating UniValue usage is not really feasible (yet), I think just sticking to request.params for now makes more sense, even if it means no named parameters for those (hopefully few) occasions?

@maflcko
Copy link
Member

maflcko commented Oct 6, 2023

In cases where eliminating UniValue usage is not really feasible (yet), I think just sticking to request.params for now makes more sense, even if it means no named parameters for those (hopefully few) occasions?

sgtm

@achow101
Copy link
Member Author

@stickies-v feel free to open an alternative approach. I'm not particularly attached to any approach, I just want to be able to access params by name.

Instead of conditionally calling transformNamedArguments (renamed to
transformArguments), unconditionally call it and perform the isObject
check within that function.
In order to support positional and named parameter lookup, introduce a
JSONRPCParameters class that contains the parameters. For now, this is
simply a passthrough for the UniValue parameters.
JSONRPCParameters contains a map of named parameter name to the
parameter, and a vector of the positional parameters. These allow for
lookup by both parameter position and parameter name.
scanblocks, createwallet, listsinceblock, sendmany, and sendtoaddress
all have 6 or more parameters. To make these easier to follow, use param
names rather than positions.
@maflcko
Copy link
Member

maflcko commented Jan 18, 2024

Can be closed after #29277 ?

@achow101 achow101 closed this Jan 18, 2024
ryanofsky added a commit that referenced this pull request Apr 29, 2024
30a6c99 rpc: access some args by name (stickies-v)
bbb3126 rpc: add named arg helper (stickies-v)
13525e0 rpc: add arg helper unit test (stickies-v)

Pull request description:

  Adds string overloads for the `RPCHelpMan::Arg` and `RPCHelpMan::MaybeArg` helpers to be able to access RPC arguments by name instead of index number. Especially in RPCs with a large number of parameters, this can be quite helpful.

  Example usage:
  ```cpp
  const auto action{self.Arg<std::string>("action")};
  ```

  Most of the LoC is adding test coverage and documentation updates. No behaviour change.

  An alternative approach to #27788 with significantly less overhaul.

ACKs for top commit:
  fjahr:
    Code review ACK 30a6c99
  maflcko:
    ACK 30a6c99 🥑
  ryanofsky:
    Code review ACK 30a6c99. Nice change! Implementation is surprisingly simple and additional unit test coverage is welcome, too.

Tree-SHA512: 4904f5f914fe1d421d32f60edb7c5a028c8ea0f140a2f207a106b4752d441164e073066a6bf2e17693f859fe847815a96609d3cf521e0ac4178d8cd09362ea3d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants