ElementsProject / lightning

Core Lightning — Lightning Network implementation focusing on spec compliance and performance
Other
2.84k stars 901 forks source link

rpc_command can't plug itself into Bitcoin backend commands #3709

Closed fiatjaf closed 4 years ago

fiatjaf commented 4 years ago

The rpc_command documentation page says that "The rpc_command hook allows a plugin to take over any RPC command.".

However it seems that plugins that implement Bitcoin backend methods are treated in a special way and can't be taken over: https://github.com/ElementsProject/lightning/blob/8d4abc1104bcfc27086b10162036127c575a5881/lightningd/bitcoind.c#L124-L134

Context: I was writing a plugin that would keep bcli in its place, but assume the task of fetching the raw block from a block explorer (and only that) so my home internet wasn't constantly drained by my lightningd nodes in the cloud.

fiatjaf commented 4 years ago

If that does not work I'll have to rewrite bcli entirely, or maybe manually comment out the getrawblockbyheight method from it. These could work, but it would more awesome if I could just take over one command from bcli without touching it.

niftynei commented 4 years ago

The bitcoin calls aren't RPC commands, so they don't hit the same rpc_command hook framework. You'd need to modify bcli or write your own backend for it; glightning has support for writing custom bitcoin backends, example here https://github.com/niftynei/glightning/blob/master/examples/plugin/pl_btc/btc.go (this is effectively a re-implementation of bcli in Go)

niftynei commented 4 years ago

note that the easiest way to deploy a plugin to replace bcli is to add --disable-plugin=bcli to your node config

darosior commented 4 years ago

In addition, note that you could have different backend plugins for each call.

fiatjaf commented 4 years ago

I was under the impression that they were RPC methods because the plugins would register Bitcoin backend methods as they would do with other RPC methods. After 3 days I've finally managed to convince myself now that they aren't. Thank you for your patience. @niftynei if you don't mind I'll now proceed to shamelessly steal parts of your code to accomplish my unfinished task.

darosior commented 4 years ago

@fiatjaf they are RPC commands. However the rpc_command hook only takes over user requests, i.e. anything that passes through the lightning-rpc socket.

For example, it would takeover a call to estimatefees done through the cli, as it's written to the `lightning-rpc``socket.