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

Field filtering: limit what fields will be returned from (almost) any JSON command. #5681

Merged
merged 11 commits into from
Nov 9, 2022

Conversation

rustyrussell
Copy link
Contributor

Sometimes you only care about a single field from a request: this especially makes this faster if you're running remotely (RTL, commando, etc). So we add a simple _filter field, which is a template of the same form as the request, but only fields mentioned in this template will be included.

In future, commands may examine the filter to avoid calculations altogether. And pyln may also be enhanced to support it (plugins which don't support it have it stripped, so no change for them!).

@rustyrussell rustyrussell added json-rpc plugin Optech Make Me Famous! Look! Look! Look! COOL NEW FEATURE! labels Oct 31, 2022
@rustyrussell rustyrussell added this to the v22.11 milestone Oct 31, 2022
@rustyrussell rustyrussell requested a review from cdecker as a code owner October 31, 2022 06:07
@jb55
Copy link
Collaborator

jb55 commented Oct 31, 2022

very nice

doc/PLUGINS.md Outdated Show resolved Hide resolved
Copy link
Member

@cdecker cdecker left a comment

Choose a reason for hiding this comment

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

Very cool idea. Apart from a minor suggestion to locate the result post-processing separately from the arguments to the method, I think this is good to go 👍

doc/PLUGINS.md Outdated Show resolved Hide resolved
Comment on lines +1 to +2
lightningd-rpc -- Lightning Daemon RPC Protocols
================================================
Copy link
Member

Choose a reason for hiding this comment

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

Good idea to add this to the docs 👍

@jb55
Copy link
Collaborator

jb55 commented Nov 1, 2022 via email

@rustyrussell
Copy link
Contributor Author

Hmm, good point re: a separate field being neater. However, that also means every layer above needs to know about it.

  1. lightning-cli needs a --filter argument
  2. pyln.client and grpc need updating either way since we already need to either wrap them all or have a "set_filter" function.
  3. commando needs to handle this somehow.
  4. c-lightning-rest needs updating.

I'd like @ShahanaFarooqui input on this!

@ShahanaFarooqui
Copy link
Collaborator

Keeping it as a separate field looks more logical and neat solution, even though it will require some top layer updates initially.

Regarding c-lightning-rest, this update should not take much time to implement.

Since the "struct command" is different from plugins and lightningd, we
need an accessor for this to work (the plugin one is a dummy for now!).

Signed-off-by: Rusty Russell <[email protected]>
@rustyrussell rustyrussell force-pushed the guilt/field-filter branch 2 times, most recently from 8632d98 to ebbc324 Compare November 4, 2022 04:08
@rustyrussell
Copy link
Contributor Author

OK, this reworks it to use a (non-standard) "filter" object in the request. This is simpler, though we now need to have an explicit lightning-cli --filter option, and a context manager for pyln to set it.

Copy link
Member

@cdecker cdecker left a comment

Choose a reason for hiding this comment

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

ACK 1ae3a47

Comment on lines +450 to +453
```python
with rpc.reply_filter({"transactions": [{"outputs": [{"amount_msat": true, "type": true}]}]}):
rpc.listtransactions()
```
Copy link
Member

Choose a reason for hiding this comment

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

This is very cool ^^

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 stole it from your context code I think!!

@cdecker cdecker force-pushed the guilt/field-filter branch from ebbc324 to 1ae3a47 Compare November 7, 2022 17:07
@cdecker
Copy link
Member

cdecker commented Nov 7, 2022

Source code checker choked on duplicate include. Fixed and force-pushed.

@cdecker
Copy link
Member

cdecker commented Nov 8, 2022

Hm, still fails, this time because with clang + fuzzing we have a reference to the json_filter infrastructure that doesn't get linked in. I tried adding it to the linker, but then we start slowly leaking out into the lightningd-specific code, which we likely don't want at all. To isolate the fuzz targets from lightningd may take some more time and a bit of rework.

Two options:

  • Disable the fuzzing targets that reference those symbols, and keep this PR in the release
  • Defer this PR until the next release.

What do you think @rustyrussell ?

No tests currently use it, and if they do we'll want to do some
per-test objects.  Otherwise, we are about it introduce a dependency
on common/json_filter.o, which is a can of worms.

Signed-off-by: Rusty Russell <[email protected]>
@rustyrussell
Copy link
Contributor Author

rustyrussell commented Nov 9, 2022

Found a trivial fix: remove the other JSON common objects from the fuzz makefile!

Ack 3bbdd35

Signed-off-by: Rusty Russell <[email protected]>
Changelog-Added: JSON-RPC: `filter` object allows reduction of JSON response to (most) commands.
We suppress schema reply checking when filter is set: we could just
remove all the `required` fields in the JSON schema.

Signed-off-by: Rusty Russell <[email protected]>
Signed-off-by: Rusty Russell <[email protected]>
Changelog-Added: pyln: LightningRpc has new `reply_filter` context manager for reducing output of RPC commands.
Signed-off-by: Rusty Russell <[email protected]>
This documents how to communicate with lightningd over RPC, including
use of the `filter` object.

Signed-off-by: Rusty Russell <[email protected]>
Changelog-Added: Documentation: `lightningd-rpc` manual page describes details of our JSON-RPC interface, including compatibility and filtering.
Signed-off-by: Rusty Russell <[email protected]>
Changelog-Added: cli: new `--filter` parameter to reduce JSON output.
@rustyrussell
Copy link
Contributor Author

Fuzz fix was trivial (stop including other JSON files).

Ack a6f38a2

@rustyrussell rustyrussell merged commit ae3550c into ElementsProject:master Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
json-rpc Optech Make Me Famous! Look! Look! Look! COOL NEW FEATURE! plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants