Skip to content

Commit

Permalink
common: absorb remaining files from daemon/
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Aug 29, 2017
1 parent a3c5116 commit 8375857
Show file tree
Hide file tree
Showing 86 changed files with 387 additions and 394 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"" ]
Expand All @@ -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.
Expand Down Expand Up @@ -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}
Expand All @@ -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`;\
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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 changes: 1 addition & 1 deletion bitcoin/short_channel_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/short_channel_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 37 additions & 0 deletions cli/Makefile
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 3 deletions daemon/lightning-cli.c → cli/lightning-cli.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
17 changes: 14 additions & 3 deletions common/Makefile
Original file line number Diff line number Diff line change
@@ -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 change: 0 additions & 1 deletion daemon/configdir.c → common/configdir.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion daemon/htlc.h → common/htlc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions daemon/htlc_state.c → common/htlc_state.c
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion common/htlc_tx.h
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion common/initial_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
2 changes: 1 addition & 1 deletion common/initial_commit_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 0 additions & 38 deletions daemon/json.c → common/json.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 3 additions & 16 deletions daemon/json.h → common/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion daemon/timeout.c → common/timeout.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "lightningd.h"
#include "timeout.h"
#include <common/utils.h>

Expand Down
File renamed without changes.
8 changes: 2 additions & 6 deletions common/type_to_string.c
Original file line number Diff line number Diff line change
@@ -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. */
Expand Down
1 change: 1 addition & 0 deletions common/type_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
4 changes: 2 additions & 2 deletions contrib/lightning-open-channel
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 8375857

Please sign in to comment.