Commit Graph

143 Commits

Author SHA1 Message Date
Michael Schmoock d3ece69a1b fix: adds bcli plugin to check-source targets
Changelog-None
2020-03-04 14:04:51 +10:30
Rusty Russell 2aad3ffcf8 common: tal_dup_talarr() helper.
This is a common thing to do, so create a macro.

Unfortunately, it still needs the type arg, because the paramter may
be const, and the return cannot be, and C doesn't have a general
"(-const)" cast.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-02-27 14:16:16 +10:30
Vasil Dimov 89ceb273f5 wire: remove towire_double()
Before this patch we used to send `double`s over the wire by just
copying them. This is not portable because the internal represenation
of a `double` is implementation specific.

Instead of this, multiply any floating-point numbers that come from
the outside (e.g. JSONs) by 1 million and round them to integers when
handling them.

* Introduce a new param_millionths() that expects a floating-point
  number and returns it multipled by 1000000 as an integer.

* Replace param_double() and param_percent() with param_millionths()

* Previously the riskfactor would be allowed to be negative, which must
  have been unintentional. This patch changes that to require a
  non-negative number.

Changelog-None
2020-02-27 09:07:04 +10:30
Vasil Dimov a754e4c6aa gitignore: extend with recently added build products
Changelog-None
2020-02-21 09:44:41 +01:00
darosior e7b0c24db2 libplugin: use a typesafe_cb for plugin_timer 2020-02-18 10:21:48 +10:30
Rusty Russell 5e2053feea pay: fix crash when we paystatus too soon.
If the attempt hasn't started yet, we can have an empty 'attempts' array
(while it's in listpeers, for example):

```
pay: plugins/pay.c:1457: json_paystatus: Assertion `tal_count(ps->attempts)' failed.
pay: FATAL SIGNAL 6 (version v0.8.1rc1-1-g946af3f)
0x55e895110543 send_backtrace
	common/daemon.c:39
0x55e8951105e9 crashdump
	common/daemon.c:52
0x7f92b0928f1f ???
	???:0
0x7f92b0928e97 ???
	???:0
0x7f92b092a800 ???
	???:0
0x7f92b091a399 ???
	???:0
0x7f92b091a411 ???
	???:0
0x55e895100012 json_paystatus
	plugins/pay.c:1457
0x55e895103aaa ld_command_handle
	plugins/libplugin.c:968
0x55e895103bd4 ld_read_json_one
	plugins/libplugin.c:1011
0x55e895103cd2 ld_read_json
	plugins/libplugin.c:1030
0x55e895124cbd next_plan
	ccan/ccan/io/io.c:59
0x55e89512583a do_plan
	ccan/ccan/io/io.c:407
0x55e895125878 io_ready
	ccan/ccan/io/io.c:417
0x55e895127a3e io_loop
	ccan/ccan/io/poll.c:445
0x55e895104593 plugin_main
	plugins/libplugin.c:1194
0x55e895100cbc main
	plugins/pay.c:1697
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-02-13 14:20:50 +10:30
Rusty Russell 7a178ebb7a plugins: libplugin don't insist on 'jsonrpc' field from lightningd.
Spark does this, for example:

{"method":"pay","params":["lnbc..."],"id":22}

Which doesn't have a jsonrpc field.  The result is that the command
doesn't terminate, there is nothing in the logs, stderr contains
"pay: JSON-RPC message does not contain "jsonrpc" field", and
from then on "Unknown command 'pay'".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-02-13 12:34:31 +10:30
darosior 5840e90ceb plugins/libplugin: don't crash if 'lightning-rpc' doesnt exist (yet)
We are going to initialize a plugin before its creation, so log as
UNUSUAL instead.

Also, `pay` and `fundchannel` inits are using rpc_delve(), so we need to
io_new_conn() (which sets the socket as non blocking) after calling the
plugin's init.
2020-02-12 11:45:07 +10:30
darosior c1009635ed plugins/bcli: register Bitcoin-related options 2020-02-12 11:45:07 +10:30
darosior f81cc9f552 plugins/bcli: wait for bitcoind to be warmed up at init
This is also taken and adapted from lightningd/bitcoind.

The call to 'getblockchaininfo' is replaced by 'echo' as we don't
make use of the result and the former can sometimes be slow (e.g. on
IBD).
2020-02-12 11:45:07 +10:30
darosior 70a79e3998 plugins/bcli: a new plugin for gathering Bitcoin data
Most is taken from lightningd/bitcoind and adapted. This currently
exposes 5 commands:
- `getchaininfo`, currently called at startup to check the network and
  whether we are on IBD.
- `getrawblockbyheight`, which basically does the `getblockhash` +
  `getblock` trick.
- `getfeerate`
- `sendrawtransaction`
- `getutxout`, used to gather infos about an output and currently used by
  `getfilteredblock` in `lightningd/bitcoind`.
2020-02-12 11:45:07 +10:30
darosior 3eb0f56f87 libplugin: generalize the plugin_timer callback type
We don't take the callback result into account, so it can better be void.
Having a general callback parameter is handy, because for bcli we want
to pass it the struct bcli.
2020-02-12 11:45:07 +10:30
darosior b0b55d36ef libplugin: add a 'still_pending' helper 2020-02-12 11:45:07 +10:30
darosior ceeb5503cc libplugin: fix 'dynamic' field in getmanifest
As a separated commit because it was pre-existent (changelog + xfail test).

This also fix a logical problem in lightningd/plugin_control: we were
assuming a plugin started with 'plugin start' but which did not comport
a 'dynamic' entry in its manifest to be dynamic, though it should have
been treated as static.

Changelog-fixed: plugins: Dynamic C plugins can now be managed when lightningd is up
2020-02-10 09:49:15 +10:30
darosior b91433cb42 libplugin: use json_stream for all plugins' commands 2020-02-10 09:49:15 +10:30
darosior 2bff80e3de libplugin: use json_stream helpers for RPC calls
This adds helpers to start and send a jsonrpc request using json_stream
in order to benefit from the helpers.

This then simplifies existing plugins RPC requests by using json_stream
helpers.
2020-02-10 09:49:15 +10:30
darosior 0546728819 libplugin: use json_stream helpers for handle_getmanifest 2020-02-10 09:49:15 +10:30
darosior 45e9f53c6b libplugin: expose helpers to start and end a jsonrpc response
The usual json_stream starters and command_result enders.
2020-02-10 09:49:15 +10:30
darosior 4772025b5d libplugin don't expose the plugin struct 2020-02-05 17:05:56 +10:30
darosior b6b2e6727e libplugin: make deprecated_apis a global 2020-02-05 17:05:56 +10:30
darosior 02fe34ed95 libplugin: don't expose the rpc_conn struct 2020-02-04 13:24:32 +10:30
darosior b31e3b1541 libplugin: pass a pointer to plugin to send_outreq
autoclean needs to send outreqs from a timer cb, hence with cmd == NULL.
2020-02-04 13:24:32 +10:30
darosior c765a223f1 libplugin: use ccan/io for async Rpc requests 2020-02-04 13:24:32 +10:30
darosior 765626875e libplugin: use ccan/io for notifications 2020-02-04 13:24:32 +10:30
darosior 9ebfdf0b8c libplugin: add remaining globals to the global state
But not the outreqs helpers, which will be moved when passing
send_outreq_ to using ccan/io.
2020-02-04 13:24:32 +10:30
darosior 499dce1c38 libplugin: include the rpc conn into the global state
And rename the struct name, as it's now only used for RPC.
2020-02-04 13:24:32 +10:30
darosior 98e8aac75f libplugin: use ccan/io for response to plugin commands
This pass to json_stream helpers for commands outputs, but keeps
compatibility with existing plugins which use jout as of now, by not
starting/closing the "result"/"error" objects.
2020-02-04 13:24:32 +10:30
darosior 42cd23092c libplugin: use ccan/io for lightningd connections
Now we have streams and a global object, we can use them for io_plans.
This makes the logic closer from lightningd/plugin (it has been mostly
stolen from here), and simplifies the main.
This also allows plugins to use io_plans (thanks to the io_loop) for
asynchronous connections.

This commit only handle incoming commands.
2020-02-04 13:24:32 +10:30
darosior ddf14613d1 libplugin: introduce a top-level object
Having a global object is very handy to pass to `io_plan`s, which would
have otherwise required a whole bunch of new globals.
2020-02-04 13:24:32 +10:30
darosior 3510c29e5d common: move json_stream helpers to common/json
Now that we have json_stream in common/, we can move all the related
helpers from lightningd/json to common/json. This way everyone can
benefit of them (including libplugin, the plugins themselves,
potentially lightning-cli), not lightningd alone!

Note that the Makefile of the common/test/ had to be modified, because
the new helpers make use of common/wireaddr... Which turns out to
\#include <lightingd/lightningd.h> ! So we couldnt just include the .c
and add mocks if we redefined some structs (hello run-param).
2020-02-04 13:24:32 +10:30
Wladimir J. van der Laan e4c6fd89b7 Add missing `extern` qualifiers for gcc 10
GCC 10 defaults to `-fno-common`. no longer automatically sharing
global variable definitions, which makes it important to define
them in only one place (otherwise there will be duplicate definition
errors). Add `extern` qualifiers where (I think) is the best place for
them.
2020-02-02 12:59:17 +10:30
Vasil Dimov 55173a56b7 Use dedicated type for error codes
Before this patch we used `int` for error codes. The problem with
`int` is that we try to pass it to/from wire and the size of `int` is
not defined by the standard. So a sender with 4-byte `int` would write
4 bytes to the wire and a receiver with 2-byte `int` (for example) would
read just 2 bytes from the wire.

To resolve this:

* Introduce an error code type with a known size:
  `typedef s32 errcode_t`.

* Change all error code macros to constants of type `errcode_t`.
  Constants also play better with gdb - it would visualize the name of
  the constant instead of the numeric value.

* Change all functions that take error codes to take the new type
  `errcode_t` instead of `int`.

* Introduce towire / fromwire functions to send / receive the newly added
  type `errcode_t` and use it instead of `towire_int()`.

In addition:

* Remove the now unneeded `towire_int()`.

* Replace a hardcoded error code `-2` with a new constant
  `INVOICE_EXPIRED_DURING_WAIT` (903).

Changelog-Changed: The waitinvoice command would now return error code 903 to designate that the invoice expired during wait, instead of the previous -2
2020-01-31 06:02:47 +00:00
ZmnSCPxj a9f0f05eea pay: Implement retry in case of final CLTV being too soon for receiver.
Changelog-Fixed: Detect a previously non-permanent error (`final_cltv_too_soon`) that has been merged into a permanent error (`incorrect_or_unknown_payment_details`), and retry that failure case in `pay`.
2020-01-21 22:23:21 +01:00
ZmnSCPxj 6c0d7ca737 pay: Implement tracking of blockheight for starting payment attempts. 2020-01-21 22:23:21 +01:00
ZmnSCPxj e9aa690d44 pay: Factor out execution of `getroute` from starting of payment attempt. 2020-01-21 22:23:21 +01:00
Vasil Dimov 2ea91f834c Add the missing space between "if" and "("
Changelog-None
2020-01-06 12:57:59 +01:00
Vasil Dimov 76e5ed45a6 doc: fix wording in plugins/README.md
Changelog-None
2020-01-06 12:57:59 +01:00
lisa neigut 689dd28ddd funding: enable push_msat
it's that time of year (merry xmas!)

enables the ability to push_msat on fundchannel

Changelog-Added: RPC: `fundchannel` and `fundchannel_start` can now accept an optional parameter, `push_msat`, which will gift that amount of satoshis to the peer at channel open.
2019-12-24 12:04:01 -06:00
Rusty Russell 207689c274 plugins: listpays will now consolidate multi-part payments.
This won't usually be visible to the end-user, since the pay plugin doesn't
do multi-part yet (and mpp requires EXPERIMENTAL_FEATURES), but we're ready
once it does.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-12-12 15:03:53 +01:00
Rusty Russell f1bc0b21f1 plugins: listpays ignores pre-0.7.0 or manual sendpay payments w/ no bolt11.
The pay plugin has been supplying the bolt11 string since 0.7.0, so only
ancient "pay" commands would be omitted by this change.

You can create a no-bolt11 "sendpay" manually, but then you'll find it
in 'listsendpays'.

Changelog-Removed: JSON: `listpays` won't shown payments made via sendpay without a bolt11 string, or before 0.7.0.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-12-12 15:03:53 +01:00
darosior fcbd11f0c5 plugins/libplugin: hook support
Changelog-Added: plugins: libplugin now supports writing plugins which register to hooks
2019-12-09 16:18:28 +01:00
darosior 3371f0cf78 plugins/libplugin: notifications support
Changelog-Added: plugins: libplugin now supports writing plugin which registers to notifications
2019-12-09 16:18:28 +01:00
lisa neigut a30ee2b7cd fundchannel: attempt to connect before funding channel
Attempts to connect to peer before funding the channel
2019-12-04 15:32:31 -06:00
darosior ff412a6c9a plugins/fundchannel: try to connect to peer before funding
Changelog-Added: RPC: The 'fundchannel' command now tries to connect to the peer before funding the channel, no need to 'connect' before 'fundchannel' anymore !
2019-12-04 15:32:31 -06:00
Rusty Russell 18e9144675 plugins/pay: hand payment_secret from bolt11 through to sendpay.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-11-24 23:33:17 +00:00
lisa neigut 963a1da958 addr: handle P2SH/P2PKH in scriptpubkey encoding
Previously, returned null if a scriptpubkey was not Segwit; now
handles encoding to Base58 for other types.
2019-11-13 03:31:20 +00:00
darosior f3d304901b pay: add a dev-only parameter to deactivate shadow routing
Had to make a special pylightning function to avoid rewriting all calls
to 'pay()' with 'rpc.call()' in the next commit..
2019-11-08 03:27:58 +00:00
darosior 16f5af00c7 common/json: add a helper for json to u16
As Rusty pointed out to me, the gossip protocol restricts cltvs to u16
so at least we'll use this helper for them.
2019-11-08 03:27:58 +00:00
darosior 5766231d98 pay: restore payment value randomization through shadow routing
When doing the random walk through the channel, we now add the fees
(both the base and the proportional one) for that channel in addition to
the cltv delta.

Changelog-Added: Payment amount fuzzing is restored, but through shadow routing.
2019-11-08 03:27:58 +00:00
Rusty Russell fe17acf07b TAGS: reformat to fix when PRINTF_FMT() used.
I was wondering why TAGS was missing some functions, and finally
tracked it down: PRINTF_FMT() confuses etags if it's at the start
of a function, and it ignores the rest of the file.

So we put PRINTF_FMT at the end, but that doesn't work for
*definitions*, only *declarations*.  So we remove it from definitions
and add gratuitous declarations in the few static places.1

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-11-01 17:27:20 -05:00