Skip to content

Conversation

@random-zebra
Copy link

The JSON-RPC specification allows either passing parameters as an Array, for positional arguments, or as an Object, for named arguments. Currently PIVX Core only supports positional arguments.

This pull request adds support for named arguments, but preserves full backwards compatibility. APIs using by-name arguments are easier to extend - as arguments can be left out - and easier to use - no need to guess which argument goes where.

This is especially nice in languages such as Python, which have native support for named arguments. Examples from the test:

h = node.help(command='getinfo')
h = node.getblockhash(height=0)
h = node.getblock(hash=h)

Backported from:

@random-zebra random-zebra added this to the 6.0.0 milestone May 13, 2021
@random-zebra random-zebra self-assigned this May 13, 2021
@random-zebra random-zebra force-pushed the 202105_rpc_namedargs branch from 60f1a11 to 3de8df0 Compare May 17, 2021 23:38
laanwj and others added 11 commits May 25, 2021 14:30
The [JSON-RPC specification](http://www.jsonrpc.org/specification)
allows passing parameters as an Array, for by-position
arguments, or an Object, for by-name arguments.

This implements by-name arguments, but preserves full backwards
compatibility. API using by-name arguments are
easier to extend, and easier to use (no need to guess which argument
goes where).

Named are mapped to positions by a per-call structure, provided through
the RPC command table.

Missing arguments will be replaced by null, except if at the end, then
the argument is left out completely.

Currently calls fail (though not crash) on intermediate nulls, but this
should be improved on a per-call basis later.
This hidden call simply returns what is passed in.
>>> backports bitcoin/bitcoin@481f289

Usage e.g.:

    $ src/pivx-cli -testnet -named echo arg0="dfdf"
    [
    "dfdf"
    ]

Argument conversion also works, for arguments thus flagged in the table
in `src/rpc/client.cpp`.

    $ src/pivx-cli -testnet -named echojson arg0="[1,2,3]"
    [
      [
        1,
        2,
        3
      ]
    ]

Unknown parameter (detected server-side):

    $ src/pivx-cli -testnet -named getinfo arg0="dfdf"
    error code: -8
    error message:
    Unknown named parameter arg0
@furszy
Copy link

furszy commented May 25, 2021

needs rebase

@random-zebra random-zebra force-pushed the 202105_rpc_namedargs branch from 3de8df0 to 9ed1bdd Compare May 25, 2021 13:10
@random-zebra
Copy link
Author

needs rebase

also reviews :)

There were a couple conflicts with #2308, merged only few hours ago. Fixed.

@random-zebra random-zebra requested review from Fuzzbawls and furszy May 27, 2021 09:25
Copy link

@furszy furszy left a comment

Choose a reason for hiding this comment

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

cool feature, ACK 9ed1bdd

@random-zebra
Copy link
Author

It would be good to merge this PR before others that are introducing new RPC commands, or modifying existing ones (such as #2341, #2349, #2351, etc.).

Copy link
Collaborator

@Fuzzbawls Fuzzbawls left a comment

Choose a reason for hiding this comment

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

ACK 9ed1bdd

@furszy furszy merged commit 29650e0 into PIVX-Project:master Jun 2, 2021
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.

4 participants