Browse Source

common: absorb remaining files from daemon/

Also, we split the more sophisticated json_add helpers to avoid pulling in
everything into lightning-cli, and unify the routines to print struct
short_channel_id (it's ':',  not '/' too).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
8375857116
  1. 2
      .gitignore
  2. 7
      Makefile
  3. 16
      README.md
  4. 2
      bitcoin/short_channel_id.c
  5. 2
      bitcoin/short_channel_id.h
  6. 37
      cli/Makefile
  7. 6
      cli/lightning-cli.c
  8. 17
      common/Makefile
  9. 1
      common/configdir.c
  10. 0
      common/configdir.h
  11. 1
      common/htlc.h
  12. 4
      common/htlc_state.c
  13. 0
      common/htlc_state.h
  14. 2
      common/htlc_tx.h
  15. 2
      common/initial_channel.h
  16. 2
      common/initial_commit_tx.h
  17. 38
      common/json.c
  18. 19
      common/json.h
  19. 0
      common/pseudorand.c
  20. 0
      common/pseudorand.h
  21. 1
      common/timeout.c
  22. 0
      common/timeout.h
  23. 8
      common/type_to_string.c
  24. 1
      common/type_to_string.h
  25. 4
      contrib/lightning-open-channel
  26. 4
      contrib/lightning-pay
  27. 21
      daemon/Makefile
  28. 135
      daemon/lightningd.h
  29. 73
      lightningd/Makefile
  30. 2
      lightningd/bitcoind.c
  31. 0
      lightningd/bitcoind.h
  32. 2
      lightningd/build_utxos.c
  33. 2
      lightningd/chaintopology.c
  34. 2
      lightningd/chaintopology.h
  35. 3
      lightningd/channel/Makefile
  36. 4
      lightningd/channel/channel.c
  37. 1
      lightningd/channel/channeld_htlc.h
  38. 2
      lightningd/channel/commit_tx.h
  39. 2
      lightningd/channel/full_channel.c
  40. 2
      lightningd/closing/closing.c
  41. 4
      lightningd/dev_ping.c
  42. 0
      lightningd/dns.c
  43. 0
      lightningd/dns.h
  44. 10
      lightningd/gossip/Makefile
  45. 2
      lightningd/gossip/broadcast.c
  46. 0
      lightningd/gossip/broadcast.h
  47. 7
      lightningd/gossip/gossip.c
  48. 6
      lightningd/gossip/routing.c
  49. 6
      lightningd/gossip/routing.h
  50. 4
      lightningd/gossip_control.c
  51. 2
      lightningd/gossip_msg.h
  52. 2
      lightningd/hsm_control.c
  53. 6
      lightningd/htlc_end.c
  54. 2
      lightningd/htlc_end.h
  55. 2
      lightningd/htlc_wire.h
  56. 1
      lightningd/invoice.c
  57. 0
      lightningd/invoice.h
  58. 48
      lightningd/jsonrpc.c
  59. 17
      lightningd/jsonrpc.h
  60. 15
      lightningd/lightningd.c
  61. 128
      lightningd/lightningd.h
  62. 2
      lightningd/log.c
  63. 0
      lightningd/log.h
  64. 0
      lightningd/netaddr.c
  65. 0
      lightningd/netaddr.h
  66. 4
      lightningd/new_connection.c
  67. 2
      lightningd/onchain/onchain_wire.h
  68. 1
      lightningd/opening/Makefile
  69. 1
      lightningd/opening/opening.c
  70. 0
      lightningd/opt_time.c
  71. 0
      lightningd/opt_time.h
  72. 22
      lightningd/options.c
  73. 0
      lightningd/options.h
  74. 8
      lightningd/pay.c
  75. 10
      lightningd/peer_control.c
  76. 6
      lightningd/peer_control.h
  77. 6
      lightningd/peer_htlcs.c
  78. 2
      lightningd/subd.c
  79. 4
      lightningd/watch.c
  80. 0
      lightningd/watch.h
  81. 5
      wallet/db.c
  82. 3
      wallet/db.h
  83. 1
      wallet/wallet.c
  84. 1
      wallet/wallet.h
  85. 7
      wallet/walletrpc.c
  86. 7
      wire/fromwire.c

2
.gitignore

@ -16,7 +16,7 @@ libsodium.la
libwallycore.a
libwallycore.la
gen_*
daemon/lightning-cli
lightning-cli/lightning-cli
tools/check-bolt
coverage
ccan/config.h

7
Makefile

@ -189,6 +189,7 @@ include bitcoin/Makefile
include wire/Makefile
include wallet/Makefile
include lightningd/Makefile
include cli/Makefile
# Git doesn't maintain timestamps, so we only regen if git says we should.
CHANGED_FROM_GIT = [ x"`git log $@ | head -n1`" != x"`git log $< | head -n1`" -o x"`git diff $<`" != x"" ]
@ -205,7 +206,7 @@ test-protocol: test/test_protocol
check: test-protocol
$(MAKE) pytest
pytest: daemon/lightning-cli lightningd-all
pytest: cli/lightning-cli lightningd-all
PYTHONPATH=contrib/pylightning python3 tests/test_lightningd.py -f
# Keep includes in alpha order.
@ -319,7 +320,7 @@ maintainer-clean: distclean
@echo 'This command is intended for maintainers to use; it'
@echo 'deletes files that may need special tools to rebuild.'
clean: daemon-clean wire-clean
clean: wire-clean
$(MAKE) -C secp256k1/ clean || true
$(RM) libsecp256k1.{a,la}
$(RM) libsodium.{a,la}
@ -331,8 +332,6 @@ clean: daemon-clean wire-clean
find . -name '*gcda' -delete
find . -name '*gcno' -delete
include daemon/Makefile
update-mocks/%: %
@set -e; BASE=/tmp/mocktmp.$$$$.`echo $* | tr / _`; trap "rm -f $$BASE.*" EXIT; \
START=`fgrep -n '/* AUTOGENERATED MOCKS START */' $< | cut -d: -f1`;\

16
README.md

@ -60,7 +60,7 @@ First you need to transfer some funds to `lightningd` so that it can open a chan
```
# Returns an address <address>
daemon/lightning-cli newaddr
cli/lightning-cli newaddr
# Returns a transaction id <txid>
bitcoin-cli -testnet sendtoaddress <address> <amount>
@ -79,12 +79,12 @@ Once `lightningd` has funds, we can connect to a node and open a channel.
Let's assume the remote node is accepting connections at `<ip>:<port>` and has the node ID `<node_id>`:
```
daemon/lightning-cli connect <ip> <port> <node_id>
daemon/lightning-cli fundchannel <node_id> <amount>
cli/lightning-cli connect <ip> <port> <node_id>
cli/lightning-cli fundchannel <node_id> <amount>
```
This opens a connection and, on top of that connection, then opens a channel.
You can check the status of the channel using `daemon/lightning-cli getpeers`.
You can check the status of the channel using `cli/lightning-cli getpeers`.
The funding transaction needs to confirm in order for the channel to be usable, so wait a few minutes, and once that is complete it `getpeers` should say that the status is in _Normal operation_.
### Receiving and receiving payments
@ -93,7 +93,7 @@ Payments in Lightning are invoice based.
The recipient creates an invoice with the expected `<amount>` in millisatoshi and a `<label>`:
```
daemon/lightning-cli invoice <amount> <label>
cli/lightning-cli invoice <amount> <label>
```
This returns a random value called `rhash` that is part of the invoice.
@ -103,8 +103,8 @@ The sender needs to compute a route to the recipient, and use that route to actu
The route contains the path that the payment will take throught the Lightning Network and the respective funds that each node will forward.
```
route=$(daemon/lightning-cli getroute <recipient_id> <amount> 1 | jq --raw-output .route -)
daemon/lightning-cli sendpay $route <rhash>
route=$(cli/lightning-cli getroute <recipient_id> <amount> 1 | jq --raw-output .route -)
cli/lightning-cli sendpay $route <rhash>
```
Notice that in the first step we stored the route in a variable and reused it in the second step.
@ -123,4 +123,4 @@ JSON-RPC interface is documented in the following manual pages:
* [getroute](doc/lightning-getroute.7.txt)
* [sendpay](doc/lightning-sendpay.7.txt)
For simple access to the JSON-RPC interface you can use the `daemon/lightning-cli` tool, or the [python API client](contrib/pylightning).
For simple access to the JSON-RPC interface you can use the `cli/lightning-cli` tool, or the [python API client](contrib/pylightning).

2
bitcoin/short_channel_id.c

@ -21,7 +21,7 @@ bool short_channel_id_from_str(const char *str, size_t strlen,
return matches == 3;
}
char *short_channel_id_to_str(tal_t *ctx, const struct short_channel_id *scid)
char *short_channel_id_to_str(const tal_t *ctx, const struct short_channel_id *scid)
{
return tal_fmt(ctx, "%d:%d:%d", scid->blocknum, scid->txnum, scid->outnum);
}

2
bitcoin/short_channel_id.h

@ -23,6 +23,6 @@ bool short_channel_id_from_str(const char *str, size_t strlen,
bool short_channel_id_eq(const struct short_channel_id *a,
const struct short_channel_id *b);
char *short_channel_id_to_str(tal_t *ctx, const struct short_channel_id *scid);
char *short_channel_id_to_str(const tal_t *ctx, const struct short_channel_id *scid);
#endif /* LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H */

37
cli/Makefile

@ -0,0 +1,37 @@
LIGHTNING_CLI_SRC := cli/lightning-cli.c
LIGHTNING_CLI_OBJS := $(LIGHTNING_CLI_SRC:.c=.o)
JSMN_OBJS := daemon/jsmn.o
JSMN_HEADERS := daemon/jsmn/jsmn.h
LIGHTNING_CLI_COMMON_OBJS := \
common/configdir.o \
common/json.o \
common/version.o
lightning-cli-all: cli/lightning-cli
$(LIGHTNINGD_OPENING_OBJS): $(LIGHTNINGD_HEADERS)
# Git submodules are seriously broken.
daemon/jsmn/jsmn.h:
git submodule update daemon/jsmn/
[ -f $@ ] || git submodule update --init daemon/jsmn/
# If we tell Make that the above builds both, it runs it twice in
# parallel. So we lie :(
daemon/jsmn/jsmn.c: daemon/jsmn/jsmn.h
[ -f $@ ]
daemon/jsmn.o: daemon/jsmn/jsmn.c
$(COMPILE.c) -DJSMN_STRICT=1 $(OUTPUT_OPTION) $<
$(LIGHTNING_CLI_OBJS) $(JSMN_OBJS): $(JSMN_HEADERS) $(COMMON_HEADERS) $(CCAN_HEADERS)
cli/lightning-cli: $(LIGHTNING_CLI_OBJS) $(LIGHTNING_CLI_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS)
clean: lightning-cli-clean
lightning-cli-clean:
$(RM) $(LIGHTNING-CLI_LIB_OBJS) $(DAEMON_JSMN_OBJS)

6
daemon/lightning-cli.c → cli/lightning-cli.c

@ -1,14 +1,14 @@
/*
* Helper to submit via JSON-RPC and get back response.
*/
#include "configdir.h"
#include "json.h"
#include "version.h"
#include <ccan/err/err.h>
#include <ccan/opt/opt.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/str/str.h>
#include <ccan/tal/str/str.h>
#include <common/configdir.h>
#include <common/json.h>
#include <common/version.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>

17
common/Makefile

@ -1,26 +1,37 @@
COMMON_SRC := \
common/close_tx.c \
common/configdir.c \
common/derive_basepoints.c \
common/funding_tx.c \
common/htlc_tx.c \
common/htlc_state.c \
common/initial_channel.c \
common/initial_commit_tx.c \
common/json.c \
common/permute_tx.c \
common/pseudorand.c \
common/timeout.c \
common/type_to_string.c \
common/utils.c \
common/version.c \
common/withdraw_tx.c
COMMON_HEADERS := $(COMMON_SRC:.c=.h) common/overflows.h
COMMON_HEADERS_NOGEN := $(COMMON_SRC:.c=.h) common/overflows.h common/htlc.h
COMMON_HEADERS_GEN := common/gen_htlc_state_names.h
COMMON_HEADERS := $(COMMON_HEADERS_GEN) $(COMMON_HEADERS_NOGEN)
COMMON_OBJS := $(COMMON_SRC:.c=.o)
common/gen_htlc_state_names.h: common/htlc_state.h ccan/ccan/cdump/tools/cdump-enumstr
ccan/ccan/cdump/tools/cdump-enumstr common/htlc_state.h > $@
check-makefile: check-common-makefile
check-common-makefile:
@if [ x"`LC_ALL=C ls common/*.h | grep -v ^gen_`" != x"`echo $(COMMON_HEADERS) | tr ' ' '\n' | LC_ALL=C sort`" ]; then echo COMMON_HEADERS incorrect; exit 1; fi
@if [ x"`LC_ALL=C ls common/*.h | grep -v ^common/gen_`" != x"`echo $(COMMON_HEADERS_NOGEN) | tr ' ' '\n' | LC_ALL=C sort`" ]; then echo COMMON_HEADERS_NOGEN incorrect; exit 1; fi
check-source-bolt: $(COMMON_SRC:%=bolt-check/%) $(COMMON_HEADERS:%=bolt-check/%)
check-whitespace: $(COMMON_SRC:%=check-whitespace/%) $(COMMON_HEADERS:%=check-whitespace/%)
check-source: $(COMMON_SRC:%=check-src-include-order/%) \
$(COMMON_HEADERS:%=check-hdr-include-order/%)
$(COMMON_HEADERS_NOGEN:%=check-hdr-include-order/%)

1
daemon/configdir.c → common/configdir.c

@ -1,5 +1,4 @@
#include "configdir.h"
#include "log.h"
#include <ccan/opt/opt.h>
#include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h>

0
daemon/configdir.h → common/configdir.h

1
daemon/htlc.h → common/htlc.h

@ -3,7 +3,6 @@
#include "config.h"
#include "bitcoin/locktime.h"
#include "htlc_state.h"
#include "pseudorand.h"
#include <assert.h>
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/crypto/siphash24/siphash24.h>

4
daemon/htlc_state.c → common/htlc_state.c

@ -1,6 +1,6 @@
#include "htlc.h"
#include "gen_htlc_state_names.h"
#include <ccan/array_size/array_size.h>
#include <common/htlc.h>
#include "gen_htlc_state_names.h"
const char *htlc_state_name(enum htlc_state s)
{

0
daemon/htlc_state.h → common/htlc_state.h

2
common/htlc_tx.h

@ -1,7 +1,7 @@
#ifndef LIGHTNING_LIGHTNINGD_HTLC_TX_H
#define LIGHTNING_LIGHTNINGD_HTLC_TX_H
#include "config.h"
#include <daemon/htlc.h>
#include <common/htlc.h>
struct keyset;
struct preimage;

2
common/initial_channel.h

@ -8,7 +8,7 @@
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <common/derive_basepoints.h>
#include <daemon/htlc.h>
#include <common/htlc.h>
#include <lightningd/channel_config.h>
#include <stdbool.h>

2
common/initial_commit_tx.h

@ -3,7 +3,7 @@
#define LIGHTNING_COMMON_INITIAL_COMMIT_TX_H
#include "config.h"
#include <bitcoin/pubkey.h>
#include <daemon/htlc.h>
#include <common/htlc.h>
#include <lightningd/channel/channeld_htlc.h>
struct keyset;

38
daemon/json.c → common/json.c

@ -1,6 +1,5 @@
/* JSON core and helpers */
#include "json.h"
#include <arpa/inet.h>
#include <assert.h>
#include <ccan/build_assert/build_assert.h>
#include <ccan/str/hex/hex.h>
@ -436,43 +435,6 @@ void json_add_hex(struct json_result *result, const char *fieldname,
json_add_string(result, fieldname, hex);
}
void json_add_pubkey(struct json_result *response,
const char *fieldname,
const struct pubkey *key)
{
u8 der[PUBKEY_DER_LEN];
pubkey_to_der(der, key);
json_add_hex(response, fieldname, der, sizeof(der));
}
void json_add_short_channel_id(struct json_result *response,
const char *fieldname,
const struct short_channel_id *id)
{
char *str = tal_fmt(response, "%d:%d:%d", id->blocknum, id->txnum, id->outnum);
json_add_string(response, fieldname, str);
}
void json_add_address(struct json_result *response, const char *fieldname,
const struct ipaddr *addr)
{
json_object_start(response, fieldname);
char *addrstr = tal_arr(response, char, INET6_ADDRSTRLEN);
if (addr->type == ADDR_TYPE_IPV4) {
inet_ntop(AF_INET, addr->addr, addrstr, INET_ADDRSTRLEN);
json_add_string(response, "type", "ipv4");
json_add_string(response, "address", addrstr);
json_add_num(response, "port", addr->port);
} else if (addr->type == ADDR_TYPE_IPV6) {
inet_ntop(AF_INET6, addr->addr, addrstr, INET6_ADDRSTRLEN);
json_add_string(response, "type", "ipv6");
json_add_string(response, "address", addrstr);
json_add_num(response, "port", addr->port);
}
json_object_end(response);
}
void json_add_object(struct json_result *result, ...)
{
va_list ap;

19
daemon/json.h → common/json.h

@ -3,15 +3,16 @@
#include "config.h"
#include <bitcoin/pubkey.h>
#include <ccan/tal/tal.h>
#include <daemon/routing.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define JSMN_STRICT 1
# include "jsmn/jsmn.h"
# include "daemon/jsmn/jsmn.h"
struct ipaddr;
struct json_result;
struct short_channel_id;
/* Include " if it's a string. */
const char *json_tok_contents(const char *buffer, const jsmntok_t *t);
@ -102,20 +103,6 @@ void json_add_null(struct json_result *result, const char *fieldname);
/* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */
void json_add_hex(struct json_result *result, const char *fieldname,
const void *data, size_t len);
/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */
void json_add_pubkey(struct json_result *response,
const char *fieldname,
const struct pubkey *key);
/* '"fieldname" : "1234/5/6"' */
void json_add_short_channel_id(struct json_result *response,
const char *fieldname,
const struct short_channel_id *id);
/* JSON serialize a network address for a node */
void json_add_address(struct json_result *response, const char *fieldname,
const struct ipaddr *addr);
void json_add_object(struct json_result *result, ...);
const char *json_result_string(const struct json_result *result);

0
daemon/pseudorand.c → common/pseudorand.c

0
daemon/pseudorand.h → common/pseudorand.h

1
daemon/timeout.c → common/timeout.c

@ -1,4 +1,3 @@
#include "lightningd.h"
#include "timeout.h"
#include <common/utils.h>

0
daemon/timeout.h → common/timeout.h

8
common/type_to_string.c

@ -1,10 +1,6 @@
#include "bitcoin/locktime.h"
#include "bitcoin/pubkey.h"
#include "bitcoin/tx.h"
#include "daemon/htlc.h"
#include "type_to_string.h"
#include "utils.h"
#include <ccan/tal/str/str.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <inttypes.h>
/* We need at least one, and this is in CCAN so register it here. */

1
common/type_to_string.h

@ -3,6 +3,7 @@
#include "config.h"
#include "utils.h"
#include <ccan/autodata/autodata.h>
#include <ccan/crypto/sha256/sha256.h>
#include <secp256k1.h>
/* This must match the type_to_string_ cases. */

4
contrib/lightning-open-channel

@ -9,10 +9,10 @@ HOST="$1"
PORT="$2"
AMOUNT="$3"
NEWADDR=`daemon/lightning-cli newaddr | sed -n 's/{ "address" : "\(.*\)" }/\1/p'`
NEWADDR=`cli/lightning-cli newaddr | sed -n 's/{ "address" : "\(.*\)" }/\1/p'`
TXID=`bitcoin-cli -testnet sendtoaddress $NEWADDR $AMOUNT`
RAWTX=`bitcoin-cli -testnet getrawtransaction $TXID`
echo "Connecting to $HOST port $PORT with $AMOUNT (minus fee) in address $NEWADDR tx $TXID"
echo "(Note: this will block until we get sufficient confirmations!)"
exec daemon/lightning-cli connect "$HOST" "$PORT" $RAWTX
exec cli/lightning-cli connect "$HOST" "$PORT" $RAWTX

4
contrib/lightning-pay

@ -9,7 +9,7 @@ DEST="$1"
AMOUNT="$2"
PHASH="$3"
if ROUTE=`daemon/lightning-cli getroute $DEST $AMOUNT 1`; then
if ROUTE=`cli/lightning-cli getroute $DEST $AMOUNT 1`; then
# Strip down to raw array.
ROUTE=`echo $ROUTE | sed 's/^{ "route" : \(.*\) }$/\1/'`
# Get first amount.
@ -18,7 +18,7 @@ if ROUTE=`daemon/lightning-cli getroute $DEST $AMOUNT 1`; then
read REPLY
case $REPLY in
""|y*|Y)
daemon/lightning-cli sendpay "$ROUTE" "$PHASH"
cli/lightning-cli sendpay "$ROUTE" "$PHASH"
exit
;;
*)

21
daemon/Makefile

@ -34,11 +34,8 @@ DAEMON_SRC := \
DAEMON_OBJS := $(DAEMON_SRC:.c=.o)
DAEMON_CLI_SRC := daemon/lightning-cli.c
DAEMON_CLI_OBJS := $(DAEMON_CLI_SRC:.c=.o)
DAEMON_JSMN_OBJS := daemon/jsmn.o
DAEMON_JSMN_HEADERS := daemon/jsmn/jsmn.h
DAEMON_JSMN_HEADERS := daemon/jsmn/jsmn.hb
DAEMON_GEN_HEADERS := \
daemon/gen_htlc_state_names.h
@ -64,9 +61,6 @@ DAEMON_HEADERS := \
daemon/timeout.h \
daemon/watch.h
daemon/gen_htlc_state_names.h: daemon/htlc_state.h ccan/ccan/cdump/tools/cdump-enumstr
ccan/ccan/cdump/tools/cdump-enumstr daemon/htlc_state.h > $@
daemon/gen_feechange_state_names.h: daemon/feechange_state.h ccan/ccan/cdump/tools/cdump-enumstr
ccan/ccan/cdump/tools/cdump-enumstr daemon/feechange_state.h > $@
@ -91,19 +85,6 @@ check-source-bolt: $(DAEMON_SRC:%=bolt-check/%) $(DAEMON_HEADERS:%=bolt-check/%)
check-whitespace: $(DAEMON_SRC:%=check-whitespace/%) $(DAEMON_HEADERS:%=check-whitespace/%) check-whitespace/daemon/Makefile
# Git submodules are seriously broken.
daemon/jsmn/jsmn.h:
git submodule update daemon/jsmn/
[ -f $@ ] || git submodule update --init daemon/jsmn/
# If we tell Make that the above builds both, it runs it twice in
# parallel. So we lie :(
daemon/jsmn/jsmn.c: daemon/jsmn/jsmn.h
[ -f $@ ]
daemon/jsmn.o: daemon/jsmn/jsmn.c
$(COMPILE.c) -DJSMN_STRICT=1 $(OUTPUT_OPTION) $<
daemon/lightning-cli: $(DAEMON_CLI_OBJS) $(DAEMON_LIB_OBJS) $(DAEMON_JSMN_OBJS) $(CORE_OBJS) $(BITCOIN_OBJS) $(LIBBASE58_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) libsecp256k1.a libsodium.a
daemon-clean:

135
daemon/lightningd.h

@ -1,135 +0,0 @@
#ifndef LIGHTNING_DAEMON_LIGHTNING_H
#define LIGHTNING_DAEMON_LIGHTNING_H
#include "config.h"
#include "bitcoin/pubkey.h"
#include "watch.h"
#include <ccan/list/list.h>
#include <ccan/short_types/short_types.h>
#include <ccan/timer/timer.h>
#include <stdio.h>
#include <wire/wire.h>
/* Various adjustable things. */
struct config {
/* How long do we want them to lock up their funds? (blocks) */
u32 locktime_blocks;
/* How long do we let them lock up our funds? (blocks) */
u32 locktime_max;
/* How many blocks before we expect to see anchor?. */
u32 anchor_onchain_wait;
/* How many confirms until we consider an anchor "settled". */
u32 anchor_confirms;
/* How long will we accept them waiting? */
u32 anchor_confirms_max;
/* How many blocks until we stop watching a close commit? */
u32 forever_confirms;
/* Maximum percent of fee rate we'll accept. */
u32 commitment_fee_max_percent;
/* Minimum percent of fee rate we'll accept. */
u32 commitment_fee_min_percent;
/* Percent of fee rate we'll use. */
u32 commitment_fee_percent;
/* Minimum/maximum time for an expiring HTLC (blocks). */
u32 min_htlc_expiry, max_htlc_expiry;
/* How many blocks before upstream HTLC expiry do we panic and dump? */
u32 deadline_blocks;
/* Fee rates. */
u32 fee_base;
s32 fee_per_satoshi;
/* How long between polling bitcoind. */
struct timerel poll_time;
/* How long between changing commit and sending COMMIT message. */
struct timerel commit_time;
/* Whether to enable IRC peer discovery. */
bool use_irc;
/* Whether to ignore database version. */
bool db_version_ignore;
/* IPv4 or IPv6 address to announce to the network */
struct ipaddr ipaddr;
};
/* Here's where the global variables hide! */
struct lightningd_state {
/* Where all our logging goes. */
struct log_book *log_book;
struct log *base_log;
FILE *logf;
/* Our config dir, and rpc file */
char *config_dir;
char *rpc_filename;
/* Port we're listening on */
u16 portnum;
/* We're on testnet. */
bool testnet;
/* Configuration settings. */
struct config config;
/* The database where we keep our stuff. */
struct db *db;
/* Any pending timers. */
struct timers timers;
/* Cached block topology. */
struct chain_topology *topology;
/* Our peers. */
struct list_head peers;
/* Addresses to contact peers. */
struct list_head addresses;
/* Any outstanding "pay" commands. */
struct list_head pay_commands;
/* Our private key */
struct privkey *privkey;
/* This is us. */
struct pubkey id;
/* Our tame bitcoind. */
struct bitcoind *bitcoind;
/* Wallet addresses we maintain. */
struct list_head wallet;
/* Maintained by invoices.c */
struct invoices *invoices;
/* Routing information */
struct routing_state *rstate;
/* For testing: don't fail if we can't route. */
bool dev_never_routefail;
/* Re-exec hack for testing. */
char **reexec;
/* IP/hostname to be announced for incoming connections */
char *external_ip;
/* Announce timer. */
struct oneshot *announce;
};
#endif /* LIGHTNING_DAEMON_LIGHTNING_H */

73
lightningd/Makefile

@ -10,37 +10,16 @@ lightningd-all: $(LIGHTNINGD_BINS)
default: lightningd-all
LIGHTNINGD_OLD_SRC := \
daemon/bitcoind.c \
daemon/broadcast.c \
daemon/chaintopology.c \
daemon/configdir.c \
daemon/dns.c \
daemon/invoice.c \
daemon/json.c \
daemon/jsonrpc.c \
daemon/log.c \
daemon/netaddr.c \
daemon/options.c \
daemon/opt_time.c \
daemon/pseudorand.c \
daemon/routing.c \
daemon/watch.c
LIGHTNINGD_OLD_OBJS := $(LIGHTNINGD_OLD_SRC:.c=.o)
LIGHTNINGD_OLD_HEADERS := $(LIGHTNINGD_OLD_SRC:.c=.h)
LIGHTNINGD_OLD_LIB_SRC := \
daemon/htlc_state.c \
daemon/pseudorand.c \
daemon/timeout.c
LIGHTNINGD_OLD_LIB_OBJS := $(LIGHTNINGD_OLD_LIB_SRC:.c=.o)
LIGHTNINGD_OLD_LIB_HEADERS := $(LIGHTNINGD_OLD_LIB_SRC:.c=.h)
# Common source we use.
LIGHTNINGD_COMMON_OBJS := \
common/configdir.o \
common/derive_basepoints.o \
common/funding_tx.o \
common/htlc_state.o \
common/json.o \
common/permute_tx.o \
common/pseudorand.o \
common/timeout.o \
common/type_to_string.o \
common/utils.o \
common/version.o \
@ -70,47 +49,51 @@ LIGHTNINGD_LIB_OBJS := $(LIGHTNINGD_LIB_SRC:.c=.o)
LIGHTNINGD_LIB_HEADERS := $(LIGHTNINGD_LIB_SRC:.c=.h)
LIGHTNINGD_SRC := \
lightningd/bitcoind.c \
lightningd/build_utxos.c \
lightningd/dev_ping.c \
lightningd/chaintopology.c \
lightningd/dns.c \
lightningd/gossip_control.c \
lightningd/htlc_end.c \
lightningd/hsm_control.c \
lightningd/htlc_end.c \
lightningd/invoice.c \
lightningd/jsonrpc.c \
lightningd/lightningd.c \
lightningd/log.c \
lightningd/netaddr.c \
lightningd/new_connection.c \
lightningd/opt_time.c \
lightningd/options.c \
lightningd/pay.c \
lightningd/peer_control.c \
lightningd/peer_htlcs.c \
lightningd/subd.c
lightningd/subd.c \
lightningd/watch.c
LIGHTNINGD_OBJS := $(LIGHTNINGD_SRC:.c=.o)
# Source files without corresponding headers
LIGHTNINGD_SRC_NOHDR := \
lightningd/dev_ping.c \
LIGHTNINGD_OBJS := $(LIGHTNINGD_SRC:.c=.o) $(LIGHTNINGD_SRC_NOHDR:.c=.o)
LIGHTNINGD_JSMN_OBJS := daemon/jsmn.o
LIGHTNINGD_JSMN_HEADERS := daemon/jsmn/jsmn.h
# We accumulate all lightningd/ headers in these three:
LIGHTNINGD_HEADERS_NOGEN = \
lightningd/build_utxos.h \
lightningd/gossip_control.h \
lightningd/hsm_control.h \
lightningd/htlc_end.h \
lightningd/lightningd.h \
lightningd/new_connection.h \
lightningd/pay.h \
lightningd/peer_control.h \
lightningd/peer_htlcs.h \
$(LIGHTNINGD_SRC:.c=.h) \
lightningd/peer_state.h \
lightningd/subd.h \
$(LIGHTNINGD_OLD_LIB_HEADERS) \
$(LIGHTNINGD_LIB_HEADERS) \
$(WIRE_HEADERS) \
$(BITCOIN_HEADERS) \
$(COMMON_HEADERS) \
$(DAEMON_HEADERS) \
$(COMMON_HEADERS_NOGEN) \
$(WALLET_LIB_HEADERS)
# Generated headers
LIGHTNINGD_HEADERS_GEN = \
lightningd/gen_peer_state_names.h \
lightningd/gen_peer_state_names.h \
$(COMMON_HEADERS_GEN) \
$(WIRE_GEN_HEADERS) \
$(GEN_HEADERS)
@ -139,7 +122,7 @@ $(LIGHTNINGD_OBJS) $(LIGHTNINGD_LIB_OBJS): $(LIGHTNINGD_HEADERS)
lightningd/gen_peer_state_names.h: lightningd/peer_state.h ccan/ccan/cdump/tools/cdump-enumstr
ccan/ccan/cdump/tools/cdump-enumstr lightningd/peer_state.h > $@
check-source: $(LIGHTNINGD_SRC:%=check-src-include-order/%)
check-source: $(LIGHTNINGD_SRC:%=check-src-include-order/%) $(LIGHTNINGD_SRC_NOHDR:%=check-src-include-order/%)
check-source: $(LIGHTNINGD_LIB_SRC:%=check-src-include-order/%)
check-source: $(LIGHTNINGD_CLI_SRC:%=check-src-include-order/%)
check-source: $(LIGHTNINGD_HEADERS_NOGEN:%=check-hdr-include-order/%)
@ -151,7 +134,7 @@ check-makefile: check-lightningd-makefile
check-lightningd-makefile:
@for f in lightningd/*.h lightningd/*/*.h; do if ! echo $(LIGHTNINGD_HEADERS_NOGEN) $(LIGHTNINGD_HEADERS_GEN) "" | grep -q "$$f "; then echo $$f not mentioned in LIGHTNINGD_HEADERS_NOGEN or LIGHTNINGD_HEADERS_GEN >&2; exit 1; fi; done
lightningd/lightningd: $(LIGHTNINGD_OBJS) $(LIGHTNINGD_OLD_OBJS) $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_COMMON_OBJS) $(LIGHTNINGD_JSMN_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(WIRE_ONION_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CONTROL_OBJS) $(LIGHTNINGD_HANDSHAKE_CONTROL_OBJS) $(LIGHTNINGD_GOSSIP_CONTROL_OBJS) $(LIBBASE58_OBJS) $(LIGHTNINGD_OPENING_CONTROL_OBJS) $(LIGHTNINGD_CHANNEL_CONTROL_OBJS) $(LIGHTNINGD_CLOSING_CONTROL_OBJS) $(LIGHTNINGD_ONCHAIN_CONTROL_OBJS) $(WALLET_LIB_OBJS) libsecp256k1.a libsodium.a libwallycore.a
lightningd/lightningd: $(LIGHTNINGD_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_COMMON_OBJS) $(LIGHTNINGD_JSMN_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(WIRE_ONION_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CONTROL_OBJS) $(LIGHTNINGD_HANDSHAKE_CONTROL_OBJS) $(LIGHTNINGD_GOSSIP_CONTROL_OBJS) $(LIBBASE58_OBJS) $(LIGHTNINGD_OPENING_CONTROL_OBJS) $(LIGHTNINGD_CHANNEL_CONTROL_OBJS) $(LIGHTNINGD_CLOSING_CONTROL_OBJS) $(LIGHTNINGD_ONCHAIN_CONTROL_OBJS) $(WALLET_LIB_OBJS) libsecp256k1.a libsodium.a libwallycore.a
clean: lightningd-clean

2
daemon/bitcoind.c → lightningd/bitcoind.c

@ -4,7 +4,6 @@
#include "bitcoin/shadouble.h"
#include "bitcoin/tx.h"
#include "bitcoind.h"
#include "json.h"
#include "lightningd.h"
#include "log.h"
#include <ccan/cast/cast.h>
@ -16,6 +15,7 @@
#include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
#include <common/json.h>
#include <common/utils.h>
#include <errno.h>
#include <inttypes.h>

0
daemon/bitcoind.h → lightningd/bitcoind.h

2
lightningd/build_utxos.c

@ -2,8 +2,8 @@
#include <bitcoin/script.h>
#include <ccan/structeq/structeq.h>
#include <common/utils.h>
#include <daemon/jsonrpc.h>
#include <lightningd/build_utxos.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <wally_bip32.h>

2
daemon/chaintopology.c → lightningd/chaintopology.c

@ -5,13 +5,13 @@
#include "jsonrpc.h"
#include "lightningd.h"
#include "log.h"
#include "timeout.h"
#include "watch.h"
#include <ccan/array_size/array_size.h>
#include <ccan/asort/asort.h>
#include <ccan/io/io.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/str/str.h>
#include <common/timeout.h>
#include <common/utils.h>
#include <inttypes.h>

2
daemon/chaintopology.h → lightningd/chaintopology.h

@ -8,7 +8,7 @@
#include <ccan/structeq/structeq.h>
#include <ccan/time/time.h>
#include <daemon/jsmn/jsmn.h>
#include <daemon/watch.h>
#include <lightningd/watch.h>
#include <stddef.h>
struct bitcoin_tx;

3
lightningd/channel/Makefile

@ -28,10 +28,13 @@ LIGHTNINGD_CHANNEL_OBJS := $(LIGHTNINGD_CHANNEL_SRC:.c=.o)
# Common source we use.
CHANNELD_COMMON_OBJS := \
common/derive_basepoints.o \
common/htlc_state.o \
common/htlc_tx.o \
common/initial_channel.o \
common/initial_commit_tx.o \
common/permute_tx.o \
common/pseudorand.o \
common/timeout.o \
common/type_to_string.o \
common/utils.o \
common/version.o

4
lightningd/channel/channel.c

@ -13,10 +13,9 @@
#include <ccan/time/time.h>
#include <common/derive_basepoints.h>
#include <common/htlc_tx.h>
#include <common/timeout.h>
#include <common/type_to_string.h>
#include <common/version.h>
#include <daemon/routing.h>
#include <daemon/timeout.h>
#include <errno.h>
#include <inttypes.h>
#include <lightningd/channel/commit_tx.h>
@ -26,6 +25,7 @@
#include <lightningd/cryptomsg.h>
#include <lightningd/daemon_conn.h>
#include <lightningd/debug.h>
#include <lightningd/gossip/routing.h>
#include <lightningd/hsm/gen_hsm_client_wire.h>
#include <lightningd/key_derive.h>
#include <lightningd/msg_queue.h>

1
lightningd/channel/channeld_htlc.h

@ -2,6 +2,7 @@
#define LIGHTNINGD_CHANNELD_HTLC_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <common/pseudorand.h>
#include <wire/gen_onion_wire.h>
struct htlc {

2
lightningd/channel/commit_tx.h

@ -2,8 +2,8 @@
#define LIGHTNING_LIGHTNINGD_COMMIT_TX_H
#include "config.h"
#include <bitcoin/pubkey.h>
#include <common/htlc.h>
#include <common/initial_commit_tx.h>
#include <daemon/htlc.h>
#include <lightningd/channel/channeld_htlc.h>
struct keyset;

2
lightningd/channel/full_channel.c

@ -6,9 +6,9 @@
#include <ccan/mem/mem.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/str/str.h>
#include <common/htlc.h>
#include <common/htlc_tx.h>
#include <common/type_to_string.h>
#include <daemon/htlc.h>
#include <inttypes.h>
#include <lightningd/channel/commit_tx.h>
#include <lightningd/channel/full_channel.h>

2
lightningd/closing/closing.c

@ -1,10 +1,10 @@
#include <bitcoin/script.h>
#include <common/close_tx.h>
#include <common/derive_basepoints.h>
#include <common/htlc.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <common/version.h>
#include <daemon/htlc.h>
#include <errno.h>
#include <inttypes.h>
#include <lightningd/closing/gen_closing_wire.h>

4
lightningd/dev_ping.c

@ -1,10 +1,10 @@
#include <common/utils.h>
#include <daemon/jsonrpc.h>
#include <daemon/log.h>
#include <lightningd/channel/gen_channel_wire.h>
#include <lightningd/gossip/gen_gossip_wire.h>
#include <lightningd/htlc_end.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/peer_control.h>
#include <lightningd/sphinx.h>
#include <lightningd/subd.h>

0
daemon/dns.c → lightningd/dns.c

0
daemon/dns.h → lightningd/dns.h

10
lightningd/gossip/Makefile

@ -11,14 +11,10 @@ LIGHTNINGD_GOSSIP_CONTROL_HEADERS := lightningd/gossip/gen_gossip_wire.h
LIGHTNINGD_GOSSIP_CONTROL_SRC := lightningd/gossip/gen_gossip_wire.c
LIGHTNINGD_GOSSIP_CONTROL_OBJS := $(LIGHTNINGD_GOSSIP_CONTROL_SRC:.c=.o)
# These should eventually be migrated to the lightningd directory, after
# deprecating the legacy daemons
LIGHTNINGD_GOSSIP_LEGACY_HEADERS := daemon/routing.h daemon/broadcast.h \
daemon/log.h
# lightningd/gossip needs these:
LIGHTNINGD_GOSSIP_HEADERS := lightningd/gossip/gen_gossip_wire.h \
$(LIGHTNINGD_GOSSIP_LEGACY_HEADERS)
lightningd/gossip/routing.h \
lightningd/gossip/broadcast.h
LIGHTNINGD_GOSSIP_SRC := lightningd/gossip/gossip.c \
$(LIGHTNINGD_GOSSIP_HEADERS:.h=.c)
LIGHTNINGD_GOSSIP_OBJS := $(LIGHTNINGD_GOSSIP_SRC:.c=.o)
@ -32,6 +28,8 @@ LIGHTNINGD_HEADERS_GEN += $(LIGHTNINGD_GOSSIP_HEADERS)
# Common source we use.
GOSSIPD_COMMON_OBJS := \
common/pseudorand.o \
common/timeout.o \
common/type_to_string.o \
common/utils.o \
common/version.o

2
daemon/broadcast.c → lightningd/gossip/broadcast.c

@ -1,4 +1,4 @@
#include "daemon/broadcast.h"
#include <lightningd/gossip/broadcast.h>
struct broadcast_state *new_broadcast_state(tal_t *ctx)
{

0
daemon/broadcast.h → lightningd/gossip/broadcast.h

7
lightningd/gossip/gossip.c

@ -9,19 +9,20 @@
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/take/take.h>
#include <ccan/tal/str/str.h>
#include <ccan/timer/timer.h>
#include <common/timeout.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <common/version.h>
#include <daemon/broadcast.h>
#include <daemon/routing.h>
#include <daemon/timeout.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <lightningd/cryptomsg.h>
#include <lightningd/daemon_conn.h>
#include <lightningd/debug.h>
#include <lightningd/gossip/broadcast.h>
#include <lightningd/gossip/gen_gossip_wire.h>
#include <lightningd/gossip/routing.h>
#include <lightningd/gossip_msg.h>
#include <lightningd/ping.h>
#include <lightningd/status.h>

6
daemon/routing.c → lightningd/gossip/routing.c

@ -1,7 +1,4 @@
#include "lightningd.h"
#include "pseudorand.h"
#include "routing.h"
#include "wire/gen_peer_wire.h"
#include <arpa/inet.h>
#include <bitcoin/block.h>
#include <ccan/array_size/array_size.h>
@ -10,9 +7,12 @@
#include <ccan/structeq/structeq.h>
#include <ccan/tal/str/str.h>
#include <common/overflows.h>
#include <common/pseudorand.h>
#include <common/type_to_string.h>
#include <inttypes.h>
#include <lightningd/lightningd.h>
#include <lightningd/status.h>
#include <wire/gen_peer_wire.h>
/* 365.25 * 24 * 60 / 10 */
#define BLOCKS_PER_YEAR 52596

6
daemon/routing.h → lightningd/gossip/routing.h

@ -1,10 +1,10 @@
#ifndef LIGHTNING_DAEMON_ROUTING_H
#define LIGHTNING_DAEMON_ROUTING_H
#include "config.h"
#include "bitcoin/pubkey.h"
#include "daemon/broadcast.h"
#include "wire/wire.h"
#include <bitcoin/pubkey.h>
#include <ccan/htable/htable_type.h>
#include <lightningd/gossip/broadcast.h>
#include <wire/wire.h>
#define ROUTING_MAX_HOPS 20
#define ROUTING_FLAGS_DISABLED 2

4
lightningd/gossip_control.c

@ -7,12 +7,12 @@
#include <ccan/tal/str/str.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <daemon/jsonrpc.h>
#include <daemon/log.h>
#include <inttypes.h>
#include <lightningd/cryptomsg.h>
#include <lightningd/gossip/gen_gossip_wire.h>
#include <lightningd/gossip_msg.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/log.h>
#include <wire/gen_peer_wire.h>
static void gossip_finished(struct subd *gossip, int status)

2
lightningd/gossip_msg.h

@ -2,7 +2,7 @@
#define LIGHTNING_LIGHTNINGD_GOSSIP_MSG_H
#include "config.h"
#include <bitcoin/pubkey.h>
#include <daemon/routing.h>
#include <lightningd/gossip/routing.h>
struct gossip_getnodes_entry {
struct pubkey nodeid;

2
lightningd/hsm_control.c

@ -6,10 +6,10 @@
#include <ccan/io/io.h>
#include <ccan/take/take.h>
#include <common/utils.h>
#include <daemon/log.h>
#include <errno.h>
#include <inttypes.h>
#include <lightningd/hsm/gen_hsm_wire.h>
#include <lightningd/log.h>
#include <lightningd/status.h>
#include <string.h>
#include <wally_bip32.h>

6
lightningd/htlc_end.c

@ -2,10 +2,10 @@
#include <ccan/crypto/siphash24/siphash24.h>
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
#include <daemon/htlc.h>
#include <daemon/log.h>
#include <daemon/pseudorand.h>
#include <common/htlc.h>
#include <common/pseudorand.h>
#include <lightningd/htlc_end.h>
#include <lightningd/log.h>
#include <stdio.h>
size_t hash_htlc_key(const struct htlc_key *k)

2
lightningd/htlc_end.h

@ -3,7 +3,7 @@
#include "config.h"
#include <ccan/htable/htable_type.h>
#include <ccan/short_types/short_types.h>
#include <daemon/htlc_state.h>
#include <common/htlc_state.h>
#include <lightningd/sphinx.h>
#include <wire/gen_onion_wire.h>

2
lightningd/htlc_wire.h

@ -3,7 +3,7 @@
#include "config.h"
#include <bitcoin/preimage.h>
#include <ccan/short_types/short_types.h>
#include <daemon/htlc.h>
#include <common/htlc.h>
#include <lightningd/sphinx.h>
#include <wire/gen_onion_wire.h>

1
daemon/invoice.c → lightningd/invoice.c

@ -1,4 +1,3 @@
#include "db.h"
#include "invoice.h"
#include "jsonrpc.h"
#include "lightningd.h"

0
daemon/invoice.h → lightningd/invoice.h

48
daemon/jsonrpc.c → lightningd/jsonrpc.c

@ -1,18 +1,19 @@
/* Code for JSON_RPC API */
/* eg: { "method" : "dev-echo", "params" : [ "hello", "Arabella!" ], "id" : "1" } */
#include "chaintopology.h"
#include "json.h"
#include "jsonrpc.h"
#include "lightningd.h"
#include "log.h"
#include <arpa/inet.h>
#include <ccan/array_size/array_size.h>
#include <ccan/err/err.h>
#include <ccan/io/io.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <common/json.h>
#include <common/version.h>
#include <errno.h>
#include <fcntl.h>
#include <lightningd/chaintopology.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/stat.h>
@ -368,6 +369,43 @@ struct json_result *null_response(const tal_t *ctx)
return response;
}
void json_add_pubkey(struct json_result *response,
const char *fieldname,
const struct pubkey *key)
{
u8 der[PUBKEY_DER_LEN];
pubkey_to_der(der, key);
json_add_hex(response, fieldname, der, sizeof(der));
}
void json_add_short_channel_id(struct json_result *response,
const char *fieldname,
const struct short_channel_id *id)
{
json_add_string(response, fieldname,
type_to_string(response, struct short_channel_id, id));
}
void json_add_address(struct json_result *response, const char *fieldname,
const struct ipaddr *addr)
{
json_object_start(response, fieldname);
char *addrstr = tal_arr(response, char, INET6_ADDRSTRLEN);
if (addr->type == ADDR_TYPE_IPV4) {
inet_ntop(AF_INET, addr->addr, addrstr, INET_ADDRSTRLEN);
json_add_string(response, "type", "ipv4");
json_add_string(response, "address", addrstr);
json_add_num(response, "port", addr->port);
} else if (addr->type == ADDR_TYPE_IPV6) {
inet_ntop(AF_INET6, addr->addr, addrstr, INET6_ADDRSTRLEN);
json_add_string(response, "type", "ipv6");
json_add_string(response, "address", addrstr);
json_add_num(response, "port", addr->port);
}
json_object_end(response);
}
void command_success(struct command *cmd, struct json_result *result)
{
struct json_connection *jcon = cmd->jcon;

17
daemon/jsonrpc.h → lightningd/jsonrpc.h

@ -1,9 +1,9 @@
#ifndef LIGHTNING_DAEMON_JSONRPC_H
#define LIGHTNING_DAEMON_JSONRPC_H
#include "config.h"
#include "json.h"
#include <ccan/autodata/autodata.h>
#include <ccan/list/list.h>
#include <common/json.h>
/* Context for a command (from JSON, but might outlive the connection!)
* You can allocate off this for temporary objects. */
@ -54,6 +54,21 @@ struct json_result *null_response(const tal_t *ctx);
void command_success(struct command *cmd, struct json_result *response);
void PRINTF_FMT(2, 3) command_fail(struct command *cmd, const char *fmt, ...);
/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */
void json_add_pubkey(struct json_result *response,
const char *fieldname,
const struct pubkey *key);
/* '"fieldname" : "1234:5:6"' */
void json_add_short_channel_id(struct json_result *response,
const char *fieldname,
const struct short_channel_id *id);
/* JSON serialize a network address for a node */
void json_add_address(struct json_result *response, const char *fieldname,
const struct ipaddr *addr);
/* For initialization */
void setup_jsonrpc(struct lightningd_state *dstate, const char *rpc_filename);

15
lightningd/lightningd.c

@ -13,17 +13,16 @@
#include <ccan/take/take.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
#include <common/timeout.h>
#include <common/utils.h>
#include <common/version.h>
#include <daemon/bitcoind.h>
#include <daemon/chaintopology.h>
#include <daemon/invoice.h>
#include <daemon/jsonrpc.h>
#include <daemon/log.h>
#include <daemon/options.h>
#include <daemon/routing.h>
#include <daemon/timeout.h>
#include <lightningd/bitcoind.h>
#include <lightningd/chaintopology.h>
#include <lightningd/invoice.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/log.h>
#include <lightningd/onchain/onchain_wire.h>
#include <lightningd/options.h>
#include <sys/types.h>
#include <unistd.h>

128
lightningd/lightningd.h

@ -4,8 +4,10 @@
#include <bitcoin/chainparams.h>
#include <bitcoin/privkey.h>
#include <ccan/container_of/container_of.h>
#include <daemon/lightningd.h>
#include <ccan/time/time.h>
#include <ccan/timer/timer.h>
#include <lightningd/htlc_end.h>
#include <stdio.h>
#include <wallet/wallet.h>
/* BOLT #1:
@ -15,6 +17,130 @@
*/
#define DEFAULT_PORT 0x2607
/* Various adjustable things. */
struct config {
/* How long do we want them to lock up their funds? (blocks) */
u32 locktime_blocks;
/* How long do we let them lock up our funds? (blocks) */
u32 locktime_max;
/* How many blocks before we expect to see anchor?. */
u32 anchor_onchain_wait;
/* How many confirms until we consider an anchor "settled". */
u32 anchor_confirms;
/* How long will we accept them waiting? */
u32 anchor_confirms_max;
/* How many blocks until we stop watching a close commit? */
u32 forever_confirms;
/* Maximum percent of fee rate we'll accept. */
u32 commitment_fee_max_percent;
/* Minimum percent of fee rate we'll accept. */
u32 commitment_fee_min_percent;
/* Percent of fee rate we'll use. */
u32 commitment_fee_percent;
/* Minimum/maximum time for an expiring HTLC (blocks). */
u32 min_htlc_expiry, max_htlc_expiry;
/* How many blocks before upstream HTLC expiry do we panic and dump? */
u32 deadline_blocks;
/* Fee rates. */
u32 fee_base;
s32 fee_per_satoshi;
/* How long between polling bitcoind. */
struct timerel poll_time;
/* How long between changing commit and sending COMMIT message. */
struct timerel commit_time;
/* Whether to enable IRC peer discovery. */
bool use_irc;
/* Whether to ignore database version. */
bool db_version_ignore;
/* IPv4 or IPv6 address to announce to the network */
struct ipaddr ipaddr;
};
/* Here's where the global variables hide! */
struct lightningd_state {
/* Where all our logging goes. */
struct log_book *log_book;
struct log *base_log;
FILE *logf;
/* Our config dir, and rpc file */
char *config_dir;
char *rpc_filename;
/* Port we're listening on */
u16 portnum;
/* We're on testnet. */
bool testnet;
/* Configuration settings. */
struct config config;
/* The database where we keep our stuff. */
struct db *db;
/* Any pending timers. */
struct timers timers;
/* Cached block topology. */
struct chain_topology *topology;
/* Our peers. */
struct list_head peers;
/* Addresses to contact peers. */
struct list_head addresses;
/* Any outstanding "pay" commands. */
struct list_head pay_commands;
/* Our private key */
struct privkey *privkey;
/* This is us. */
struct pubkey id;
/* Our tame bitcoind. */
struct bitcoind *bitcoind;
/* Wallet addresses we maintain. */
struct list_head wallet;
/* Maintained by invoices.c */
struct invoices *invoices;
/* Routing information */
struct routing_state *rstate;
/* For testing: don't fail if we can't route. */
bool dev_never_routefail;
/* Re-exec hack for testing. */
char **reexec;
/* IP/hostname to be announced for incoming connections */
char *external_ip;
/* Announce timer. */
struct oneshot *announce;
};
/* FIXME: This is two structures, during the migration from old setup to new */
struct lightningd {
/* Must be first, since things assume we can tal() off it */

2
daemon/log.c → lightningd/log.c

@ -1,5 +1,4 @@
#include "log.h"
#include "pseudorand.h"
#include <ccan/array_size/array_size.h>
#include <ccan/list/list.h>
#include <ccan/opt/opt.h>
@ -7,6 +6,7 @@
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <ccan/time/time.h>
#include <common/pseudorand.h>
#include <common/utils.h>
#include <errno.h>
#include <fcntl.h>

0
daemon/log.h → lightningd/log.h

0
daemon/netaddr.c → lightningd/netaddr.c

0
daemon/netaddr.h → lightningd/netaddr.h

4
lightningd/new_connection.c

@ -1,15 +1,15 @@
#include <ccan/array_size/array_size.h>
#include <ccan/fdpass/fdpass.h>
#include <ccan/tal/str/str.h>
#include <daemon/jsonrpc.h>
#include <daemon/log.h>
#include <errno.h>
#include <fcntl.h>
#include <lightningd/cryptomsg.h>
#include <lightningd/handshake/gen_handshake_wire.h>
#include <lightningd/hsm/gen_hsm_wire.h>
#include <lightningd/hsm_control.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/new_connection.h>
#include <lightningd/peer_control.h>
#include <lightningd/status.h>

2
lightningd/onchain/onchain_wire.h

@ -3,7 +3,7 @@
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <daemon/htlc.h>
#include <common/htlc.h>
/* The minimal info about an htlc. */
struct htlc_stub {

1
lightningd/opening/Makefile

@ -40,6 +40,7 @@ OPENINGD_COMMON_OBJS := \
common/initial_channel.o \
common/initial_commit_tx.o \
common/permute_tx.o \
common/pseudorand.o \
common/type_to_string.o \
common/utils.o \
common/version.o

1
lightningd/opening/opening.c

@ -8,6 +8,7 @@
#include <common/derive_basepoints.h>
#include <common/funding_tx.h>
#include <common/initial_channel.h>
#include <common/pseudorand.h>
#include <common/type_to_string.h>
#include <common/version.h>
#include <errno.h>

0
daemon/opt_time.c → lightningd/opt_time.c

0
daemon/opt_time.h → lightningd/opt_time.h

22
daemon/options.c → lightningd/options.c

@ -1,22 +1,20 @@
#include "bitcoin/chainparams.h"
#include "daemon/bitcoind.h"
#include "daemon/chaintopology.h"
#include "daemon/configdir.h"
#include "daemon/lightningd.h"
#include "daemon/log.h"
#include "daemon/opt_time.h"
#include "daemon/options.h"
#include "daemon/routing.h"
#include "lightningd/lightningd.h"
#include <arpa/inet.h>
#include <bitcoin/chainparams.h>
#include <ccan/err/err.h>
#include <ccan/opt/opt.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/str/str.h>
#include <common/configdir.h>
#include <common/version.h>
#include <errno.h>
#include <inttypes.h>
#include <lightningd/bitcoind.h>
#include <lightningd/chaintopology.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/opt_time.h>
#include <lightningd/options.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
@ -228,10 +226,6 @@ static void config_register_opts(struct lightningd_state *dstate)
opt_register_arg("--fee-per-satoshi", opt_set_s32, opt_show_s32,
&dstate->config.fee_per_satoshi,
"Microsatoshi fee for every satoshi in HTLC");
opt_register_arg("--add-route", opt_add_route, NULL,
dstate,
"Add route of form srcid/dstid/base/var/delay/minblocks"
"(base in millisatoshi, var in millionths of satoshi per satoshi)");
opt_register_noarg("--disable-irc", opt_set_invbool,
&dstate->config.use_irc,
"Disable IRC peer discovery for routing");

0
daemon/options.h → lightningd/options.h

8
lightningd/pay.c

@ -2,12 +2,12 @@
#include <bitcoin/preimage.h>
#include <ccan/str/hex/hex.h>
#include <ccan/structeq/structeq.h>
#include <daemon/chaintopology.h>
#include <daemon/jsonrpc.h>
#include <daemon/log.h>
#include <inttypes.h>
#include <lightningd/chaintopology.h>
#include <lightningd/channel/gen_channel_wire.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/peer_control.h>
#include <lightningd/peer_htlcs.h>
#include <lightningd/sphinx.h>
@ -231,7 +231,7 @@ static void json_sendpay(struct command *cmd,
if (!short_channel_id_from_str(buffer + chantok->start,
chantok->end - chantok->start,
&hop_data[n_hops].channel_id)) {
command_fail(cmd, "route %zu invalid id", n_hops);
command_fail(cmd, "route %zu invalid channel_id", n_hops);
return;
}
if (!pubkey_from_hexstr(buffer + idtok->start,

10
lightningd/peer_control.c

@ -11,23 +11,23 @@
#include <common/close_tx.h>
#include <common/funding_tx.h>
#include <common/initial_commit_tx.h>
#include <daemon/chaintopology.h>
#include <daemon/dns.h>
#include <daemon/jsonrpc.h>
#include <daemon/log.h>
#include <daemon/timeout.h>
#include <common/timeout.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <lightningd/build_utxos.h>
#include <lightningd/chaintopology.h>
#include <lightningd/channel/gen_channel_wire.h>
#include <lightningd/closing/gen_closing_wire.h>
#include <lightningd/dev_disconnect.h>
#include <lightningd/dns.h>
#include <lightningd/gen_peer_state_names.h>
#include <lightningd/gossip/gen_gossip_wire.h>
#include <lightningd/hsm/gen_hsm_wire.h>
#include <lightningd/hsm_control.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/key_derive.h>
#include <lightningd/log.h>
#include <lightningd/new_connection.h>
#include <lightningd/onchain/gen_onchain_wire.h>
#include <lightningd/onchain/onchain_wire.h>

6
lightningd/peer_control.h

@ -4,10 +4,10 @@
#include <ccan/compiler/compiler.h>
#include <ccan/crypto/shachain/shachain.h>
#include <ccan/list/list.h>
#include <daemon/htlc.h>
#include <daemon/json.h>
#include <daemon/netaddr.h>
#include <common/htlc.h>
#include <common/json.h>
#include <lightningd/channel_config.h>
#include <lightningd/netaddr.h>
#include <lightningd/peer_state.h>
#include <stdbool.h>
#include <wallet/wallet.h>

6
lightningd/peer_htlcs.c

@ -5,14 +5,14 @@
#include <ccan/tal/str/str.h>
#include <common/derive_basepoints.h>
#include <common/overflows.h>
#include <daemon/chaintopology.h>
#include <daemon/invoice.h>
#include <daemon/log.h>
#include <lightningd/chaintopology.h>
#include <lightningd/channel/gen_channel_wire.h>
#include <lightningd/gossip/gen_gossip_wire.h>
#include <lightningd/htlc_end.h>
#include <lightningd/htlc_wire.h>
#include <lightningd/invoice.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/onchain/onchain_wire.h>
#include <lightningd/pay.h>
#include <lightningd/peer_control.h>

2
lightningd/subd.c

@ -5,10 +5,10 @@
#include <ccan/take/take.h>
#include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h>
#include <daemon/log.h>
#include <errno.h>
#include <fcntl.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/status.h>
#include <lightningd/subd.h>
#include <stdarg.h>

4
daemon/watch.c → lightningd/watch.c

@ -32,12 +32,12 @@
#include "chaintopology.h"
#include "lightningd.h"
#include "log.h"
#include "pseudorand.h"
#include "timeout.h"
#include "watch.h"
#include <ccan/crypto/siphash24/siphash24.h>
#include <ccan/ptrint/ptrint.h>
#include <ccan/structeq/structeq.h>
#include <common/pseudorand.h>
#include <common/timeout.h>
const struct txwatch_output *txowatch_keyof(const struct txowatch *w)
{

0
daemon/watch.h → lightningd/watch.h

5
wallet/db.c

@ -1,12 +1,11 @@
#include "db.h"
#include "daemon/log.h"
#include "lightningd/lightningd.h"
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
#include <inttypes.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#define DB_FILE "lightningd.sqlite3"

3
wallet/db.h

@ -2,7 +2,8 @@
#define WALLET_DB_H
#include "config.h"
#include "daemon/log.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <sqlite3.h>
#include <stdbool.h>

1
wallet/wallet.c

@ -5,6 +5,7 @@
#include <ccan/tal/str/str.h>
#include <inttypes.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/peer_control.h>
#include <lightningd/peer_htlcs.h>

1
wallet/wallet.h

@ -11,6 +11,7 @@
#include <wally_bip32.h>
struct lightningd;
struct pubkey;
struct wallet {
struct db *db;

7
wallet/walletrpc.c

@ -3,14 +3,15 @@
#include <bitcoin/script.h>
#include <ccan/tal/str/str.h>
#include <common/withdraw_tx.h>
#include <daemon/bitcoind.h>
#include <daemon/chaintopology.h>
#include <daemon/jsonrpc.h>
#include <errno.h>
#include <lightningd/bitcoind.h>
#include <lightningd/chaintopology.h>
#include <lightningd/hsm/gen_hsm_wire.h>
#include <lightningd/hsm_control.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/key_derive.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/status.h>
#include <lightningd/subd.h>
#include <lightningd/utxo.h>

7
wire/fromwire.c

@ -204,12 +204,7 @@ void fromwire_pad(const u8 **cursor, size_t *max, size_t num)
fromwire(cursor, max, NULL, num);
}
static char *fmt_short_channel_id(const tal_t *ctx,
const struct short_channel_id *id)
{
return tal_fmt(ctx, "%u/%u/%u", id->blocknum, id->txnum, id->outnum);
}
REGISTER_TYPE_TO_STRING(short_channel_id, fmt_short_channel_id);
REGISTER_TYPE_TO_STRING(short_channel_id, short_channel_id_to_str);
REGISTER_TYPE_TO_HEXSTR(channel_id);
/* BOLT #2:

Loading…
Cancel
Save