From 85ff95e82953f45c2e3ca83e31279361537492ab Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 29 Aug 2017 01:32:01 +0930 Subject: [PATCH] common: new directory for any shared objects. To avoid everything pulling in HTLCs stuff to the opening daemon, we split the channel and commit_tx routines into initial_channel and initial_commit_tx (no HTLC support) and move full HTLC supporting versions into channeld. Signed-off-by: Rusty Russell --- Makefile | 39 +--- bitcoin/Makefile | 5 + bitcoin/base58.c | 2 +- bitcoin/locktime.c | 4 +- bitcoin/pubkey.c | 4 +- bitcoin/shadouble.c | 2 +- bitcoin/signature.c | 4 +- bitcoin/tx.c | 8 +- common/Makefile | 26 +++ close_tx.c => common/close_tx.c | 0 close_tx.h => common/close_tx.h | 0 {lightningd => common}/derive_basepoints.c | 4 +- {lightningd => common}/derive_basepoints.h | 0 {lightningd => common}/funding_tx.c | 2 +- {lightningd => common}/funding_tx.h | 0 {lightningd => common}/htlc_tx.c | 26 ++- {lightningd => common}/htlc_tx.h | 39 ++++ common/initial_channel.c | 123 +++++++++++ common/initial_channel.h | 172 +++++++++++++++ common/initial_commit_tx.c | 200 ++++++++++++++++++ common/initial_commit_tx.h | 96 +++++++++ overflows.h => common/overflows.h | 0 permute_tx.c => common/permute_tx.c | 0 permute_tx.h => common/permute_tx.h | 0 type_to_string.c => common/type_to_string.c | 0 type_to_string.h => common/type_to_string.h | 0 utils.c => common/utils.c | 0 utils.h => common/utils.h | 0 version.c => common/version.c | 0 version.h => common/version.h | 0 {lightningd => common}/withdraw_tx.c | 2 +- {lightningd => common}/withdraw_tx.h | 0 daemon/bitcoind.c | 2 +- daemon/chaintopology.c | 2 +- daemon/htlc_state.c | 10 - daemon/invoice.c | 2 +- daemon/jsonrpc.c | 2 +- daemon/log.c | 2 +- daemon/log.h | 2 +- daemon/netaddr.c | 4 +- daemon/options.c | 2 +- daemon/routing.c | 2 +- daemon/timeout.c | 2 +- lightningd/Makefile | 23 +- lightningd/build_utxos.c | 2 +- lightningd/channel/Makefile | 19 +- lightningd/channel/channel.c | 16 +- lightningd/{ => channel}/commit_tx.c | 136 +----------- lightningd/{ => channel}/commit_tx.h | 41 +--- .../{channel.c => channel/full_channel.c} | 161 +++++--------- .../{channel.h => channel/full_channel.h} | 135 +----------- lightningd/closing/Makefile | 11 +- lightningd/closing/closing.c | 10 +- lightningd/crypto_sync.c | 2 +- lightningd/cryptomsg.c | 2 +- lightningd/dev_ping.c | 2 +- lightningd/gossip/Makefile | 8 +- lightningd/gossip/gossip.c | 4 +- lightningd/gossip_control.c | 2 + lightningd/handshake/Makefile | 8 +- lightningd/handshake/handshake.c | 4 +- lightningd/hsm/Makefile | 12 +- lightningd/hsm/hsm.c | 9 +- lightningd/hsm_control.c | 1 + lightningd/key_derive.c | 2 +- lightningd/lightningd.c | 4 +- lightningd/onchain/Makefile | 12 +- lightningd/onchain/onchain.c | 12 +- lightningd/opening/Makefile | 13 +- lightningd/opening/opening.c | 104 +++++---- lightningd/peer_control.c | 8 +- lightningd/peer_htlcs.c | 4 +- lightningd/peer_htlcs.h | 2 +- lightningd/sphinx.c | 2 +- lightningd/status.c | 6 +- test/test_protocol.c | 2 +- test/test_sphinx.c | 2 +- wallet/walletrpc.c | 3 +- wire/fromwire.c | 4 +- wire/towire.c | 2 +- 80 files changed, 971 insertions(+), 609 deletions(-) create mode 100644 common/Makefile rename close_tx.c => common/close_tx.c (100%) rename close_tx.h => common/close_tx.h (100%) rename {lightningd => common}/derive_basepoints.c (97%) rename {lightningd => common}/derive_basepoints.h (100%) rename {lightningd => common}/funding_tx.c (98%) rename {lightningd => common}/funding_tx.h (100%) rename {lightningd => common}/htlc_tx.c (87%) rename {lightningd => common}/htlc_tx.h (62%) create mode 100644 common/initial_channel.c create mode 100644 common/initial_channel.h create mode 100644 common/initial_commit_tx.c create mode 100644 common/initial_commit_tx.h rename overflows.h => common/overflows.h (100%) rename permute_tx.c => common/permute_tx.c (100%) rename permute_tx.h => common/permute_tx.h (100%) rename type_to_string.c => common/type_to_string.c (100%) rename type_to_string.h => common/type_to_string.h (100%) rename utils.c => common/utils.c (100%) rename utils.h => common/utils.h (100%) rename version.c => common/version.c (100%) rename version.h => common/version.h (100%) rename {lightningd => common}/withdraw_tx.c (97%) rename {lightningd => common}/withdraw_tx.h (100%) rename lightningd/{ => channel}/commit_tx.c (72%) rename lightningd/{ => channel}/commit_tx.h (60%) rename lightningd/{channel.c => channel/full_channel.c} (89%) rename lightningd/{channel.h => channel/full_channel.h} (71%) diff --git a/Makefile b/Makefile index f7f481096..9e71d9ac1 100644 --- a/Makefile +++ b/Makefile @@ -43,19 +43,6 @@ BITCOIN_SRC := \ BITCOIN_OBJS := $(BITCOIN_SRC:.c=.o) -CORE_SRC := \ - type_to_string.c \ - utils.c \ - version.c - -CORE_OBJS := $(CORE_SRC:.c=.o) - -CORE_TX_SRC := \ - close_tx.c \ - permute_tx.c - -CORE_TX_OBJS := $(CORE_TX_SRC:.c=.o) - CCAN_OBJS := \ ccan-asort.o \ ccan-autodata.o \ @@ -172,15 +159,6 @@ BITCOIN_HEADERS := bitcoin/address.h \ bitcoin/tx.h \ bitcoin/varint.h -CORE_TX_HEADERS := close_tx.h \ - permute_tx.h - -CORE_HEADERS := \ - overflows.h \ - type_to_string.h \ - utils.h \ - version.h - GEN_HEADERS := gen_version.h LIBSODIUM_HEADERS := libsodium/src/libsodium/include/sodium.h @@ -205,6 +183,7 @@ $(PROGRAMS): CFLAGS+=-I. default: $(PROGRAMS) doc-all +include common/Makefile include doc/Makefile include bitcoin/Makefile include wire/Makefile @@ -217,8 +196,8 @@ CHANGED_FROM_GIT = [ x"`git log $@ | head -n1`" != x"`git log $< | head -n1`" -o # Everything depends on the CCAN headers. $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(CDUMP_OBJS) $(HELPER_OBJS) $(BITCOIN_OBJS) $(TEST_PROGRAMS:=.o) ccan/ccan/cdump/tools/cdump-enumstr.o: $(CCAN_HEADERS) -# Except for CCAN, everything depends on bitcoin/ and core headers. -$(HELPER_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(BITCOIN_OBJS) $(LIBBASE58_OBJS) $(WIRE_OBJS) $(WALLET_LIB_OBJS) $(TEST_PROGRAMS:=.o): $(BITCOIN_HEADERS) $(CORE_HEADERS) $(CCAN_HEADERS) $(GEN_HEADERS) $(LIBBASE58_HEADERS) $(LIBSODIUM_HEADERS) $(LIBWALLY_HEADERS) +# Except for CCAN, everything depends on bitcoin, ccan, library and common headers. +$(HELPER_OBJS) $(COMMON_OBJS) $(BITCOIN_OBJS) $(LIBBASE58_OBJS) $(WIRE_OBJS) $(WALLET_LIB_OBJS) $(TEST_PROGRAMS:=.o): $(BITCOIN_HEADERS) $(COMMON_HEADERS) $(CCAN_HEADERS) $(GEN_HEADERS) $(LIBBASE58_HEADERS) $(LIBSODIUM_HEADERS) $(LIBWALLY_HEADERS) test-protocol: test/test_protocol set -e; TMP=`mktemp`; for f in test/commits/*.script; do if ! $(VALGRIND) test/test_protocol < $$f > $$TMP; then echo "test/test_protocol < $$f FAILED" >&2; exit 1; fi; diff -u $$TMP $$f.expected; done; rm $$TMP @@ -240,8 +219,6 @@ check-hdr-include-order/%: % # Make sure Makefile includes all headers. check-makefile: - @if [ "`echo bitcoin/*.h`" != "$(BITCOIN_HEADERS)" ]; then echo BITCOIN_HEADERS incorrect; exit 1; fi - @if [ x"`ls *.h | grep -v ^gen_`" != x"`echo $(CORE_HEADERS) $(CORE_TX_HEADERS) | tr ' ' '\n' | LC_ALL=C sort`" ]; then echo CORE_HEADERS incorrect; exit 1; fi @if [ x"$(CCANDIR)/config.h `find $(CCANDIR)/ccan -name '*.h' | grep -v /test/ | LC_ALL=C sort | tr '\n' ' '`" != x"$(CCAN_HEADERS) " ]; then echo CCAN_HEADERS incorrect; exit 1; fi # Any mention of BOLT# must be followed by an exact quote, modulo whitepace. @@ -251,7 +228,7 @@ bolt-check/%: % bolt-precheck tools/check-bolt bolt-precheck: @rm -rf .tmp.lightningrfc; if [ ! -d $(BOLTDIR) ]; then echo Not checking BOLT references: BOLTDIR $(BOLTDIR) does not exist >&2; exit 0; fi; set -e; if [ -n "$(BOLTVERSION)" ]; then git clone -q -b $(BOLTVERSION) $(BOLTDIR) .tmp.lightningrfc; else cp -a $(BOLTDIR) .tmp.lightningrfc; fi -check-source-bolt: $(CORE_SRC:%=bolt-check/%) $(CORE_TX_SRC:%=bolt-check/%) $(CORE_HEADERS:%=bolt-check/%) $(TEST_PROGRAMS:%=bolt-check/%.c) +check-source-bolt: $(TEST_PROGRAMS:%=bolt-check/%.c) tools/check-bolt: tools/check-bolt.o $(CCAN_OBJS) @@ -260,14 +237,10 @@ tools/check-bolt.o: $(CCAN_HEADERS) check-whitespace/%: % @if grep -Hn '[ ]$$' $<; then echo Extraneous whitespace found >&2; exit 1; fi -check-whitespace: check-whitespace/Makefile check-whitespace/tools/check-bolt.c $(CORE_SRC:%=check-whitespace/%) $(CORE_TX_SRC:%=check-whitespace/%) $(CORE_HEADERS:%=check-whitespace/%) +check-whitespace: check-whitespace/Makefile check-whitespace/tools/check-bolt.c check-source: check-makefile check-source-bolt check-whitespace \ - $(CORE_SRC:%=check-src-include-order/%) \ - $(CORE_TX_SRC:%=check-src-include-order/%) \ $(BITCOIN_SRC:%=check-src-include-order/%) \ - $(CORE_HEADERS:%=check-hdr-include-order/%) \ - $(CORE_TX_HEADERS:%=check-hdr-include-order/%) \ $(BITCOIN_HEADERS:%=check-hdr-include-order/%) full-check: check $(TEST_PROGRAMS) check-source @@ -305,7 +278,7 @@ libsecp256k1.% libwallycore.%: libwally-core/src/secp256k1/libsecp256k1.la libwa libwally-core/src/libwallycore.% libwally-core/src/secp256k1/libsecp256k1.%: $(LIBWALLY_HEADERS) $(LIBSECP_HEADERS) cd libwally-core && ./tools/autogen.sh && ./configure CC="$(CC)" --enable-static=yes --enable-shared=no --libdir=`pwd`/.. && $(MAKE) -$(TEST_PROGRAMS): % : %.o $(BITCOIN_OBJS) $(LIBBASE58_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) lightningd/sphinx.o utils.o version.o libwallycore.a libsecp256k1.a libsodium.a +$(TEST_PROGRAMS): % : %.o $(BITCOIN_OBJS) $(LIBBASE58_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) lightningd/sphinx.o common/utils.o libwallycore.a libsecp256k1.a libsodium.a ccan/config.h: ccan/tools/configurator/configurator if $< > $@.new; then mv $@.new $@; else rm $@.new; exit 1; fi diff --git a/bitcoin/Makefile b/bitcoin/Makefile index cb6874302..b8ed36583 100644 --- a/bitcoin/Makefile +++ b/bitcoin/Makefile @@ -15,6 +15,11 @@ check: bitcoin-tests check-source-bolt: $(BITCOIN_SRC:%=bolt-check/%) $(BITCOIN_HEADERS:%=bolt-check/%) +check-makefile: check-bitcoin-makefile + +check-bitcoin-makefile: + @if [ "`echo bitcoin/*.h`" != "$(BITCOIN_HEADERS)" ]; then echo BITCOIN_HEADERS incorrect; exit 1; fi + check-whitespace: $(BITCOIN_SRC:%=check-whitespace/%) $(BITCOIN_HEADERS:%=check-whitespace/%) check-whitespace/bitcoin/Makefile bitcoin-tests: $(BITCOIN_TEST_PROGRAMS:%=unittest/%) diff --git a/bitcoin/base58.c b/bitcoin/base58.c index a10b3f2be..605c6882f 100644 --- a/bitcoin/base58.c +++ b/bitcoin/base58.c @@ -9,10 +9,10 @@ #include "privkey.h" #include "pubkey.h" #include "shadouble.h" -#include "utils.h" #include #include #include +#include #include #include diff --git a/bitcoin/locktime.c b/bitcoin/locktime.c index 3ebaacf0f..9b74a948c 100644 --- a/bitcoin/locktime.c +++ b/bitcoin/locktime.c @@ -1,7 +1,7 @@ -#include "bitcoin/locktime.h" -#include "type_to_string.h" #include +#include #include +#include #define SECONDS_POINT 500000000 diff --git a/bitcoin/pubkey.c b/bitcoin/pubkey.c index 122e6bca5..e1e75177e 100644 --- a/bitcoin/pubkey.c +++ b/bitcoin/pubkey.c @@ -1,11 +1,11 @@ #include "privkey.h" #include "pubkey.h" -#include "type_to_string.h" -#include "utils.h" #include #include #include #include +#include +#include bool pubkey_from_der(const u8 *der, size_t len, struct pubkey *key) { diff --git a/bitcoin/shadouble.c b/bitcoin/shadouble.c index a21e96ad5..4e819d51b 100644 --- a/bitcoin/shadouble.c +++ b/bitcoin/shadouble.c @@ -1,6 +1,6 @@ #include "shadouble.h" -#include "type_to_string.h" #include +#include void sha256_double(struct sha256_double *shadouble, const void *p, size_t len) { diff --git a/bitcoin/signature.c b/bitcoin/signature.c index 9b823025e..4464796a5 100644 --- a/bitcoin/signature.c +++ b/bitcoin/signature.c @@ -4,10 +4,10 @@ #include "shadouble.h" #include "signature.h" #include "tx.h" -#include "type_to_string.h" -#include "utils.h" #include #include +#include +#include #undef DEBUG #ifdef DEBUG diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 21d4aad2e..1f7c806ce 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -1,14 +1,14 @@ -#include "bitcoin/block.h" -#include "bitcoin/pullpush.h" -#include "bitcoin/tx.h" -#include "type_to_string.h" #include +#include +#include +#include #include #include #include #include #include #include +#include #include #define SEGREGATED_WITNESS_FLAG 0x1 diff --git a/common/Makefile b/common/Makefile new file mode 100644 index 000000000..7d0b27634 --- /dev/null +++ b/common/Makefile @@ -0,0 +1,26 @@ +COMMON_SRC := \ + common/close_tx.c \ + common/derive_basepoints.c \ + common/funding_tx.c \ + common/htlc_tx.c \ + common/initial_channel.c \ + common/initial_commit_tx.c \ + common/permute_tx.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_OBJS := $(COMMON_SRC:.c=.o) + +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 + +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/%) diff --git a/close_tx.c b/common/close_tx.c similarity index 100% rename from close_tx.c rename to common/close_tx.c diff --git a/close_tx.h b/common/close_tx.h similarity index 100% rename from close_tx.h rename to common/close_tx.h diff --git a/lightningd/derive_basepoints.c b/common/derive_basepoints.c similarity index 97% rename from lightningd/derive_basepoints.c rename to common/derive_basepoints.c index 802c634b7..75041e4c9 100644 --- a/lightningd/derive_basepoints.c +++ b/common/derive_basepoints.c @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include bool derive_basepoints(const struct privkey *seed, struct pubkey *funding_pubkey, diff --git a/lightningd/derive_basepoints.h b/common/derive_basepoints.h similarity index 100% rename from lightningd/derive_basepoints.h rename to common/derive_basepoints.h diff --git a/lightningd/funding_tx.c b/common/funding_tx.c similarity index 98% rename from lightningd/funding_tx.c rename to common/funding_tx.c index 37549b0da..77bdd1cae 100644 --- a/lightningd/funding_tx.c +++ b/common/funding_tx.c @@ -4,9 +4,9 @@ #include #include #include +#include #include #include -#include #ifndef SUPERVERBOSE #define SUPERVERBOSE(...) diff --git a/lightningd/funding_tx.h b/common/funding_tx.h similarity index 100% rename from lightningd/funding_tx.h rename to common/funding_tx.h diff --git a/lightningd/htlc_tx.c b/common/htlc_tx.c similarity index 87% rename from lightningd/htlc_tx.c rename to common/htlc_tx.c index 0525a2fbe..64d712350 100644 --- a/lightningd/htlc_tx.c +++ b/common/htlc_tx.c @@ -1,8 +1,7 @@ #include #include #include -#include -#include +#include #include static struct bitcoin_tx *htlc_tx(const tal_t *ctx, @@ -157,3 +156,26 @@ void htlc_timeout_tx_add_witness(struct bitcoin_tx *htlc_timeout, tal_free(wscript); } +u8 *htlc_offered_wscript(const tal_t *ctx, + const struct ripemd160 *ripemd, + const struct keyset *keyset) +{ + return bitcoin_wscript_htlc_offer_ripemd160(ctx, + &keyset->self_payment_key, + &keyset->other_payment_key, + ripemd, + &keyset->self_revocation_key); +} + +u8 *htlc_received_wscript(const tal_t *ctx, + const struct ripemd160 *ripemd, + const struct abs_locktime *expiry, + const struct keyset *keyset) +{ + return bitcoin_wscript_htlc_receive_ripemd(ctx, + expiry, + &keyset->self_payment_key, + &keyset->other_payment_key, + ripemd, + &keyset->self_revocation_key); +} diff --git a/lightningd/htlc_tx.h b/common/htlc_tx.h similarity index 62% rename from lightningd/htlc_tx.h rename to common/htlc_tx.h index bc28892cc..3766dfa38 100644 --- a/lightningd/htlc_tx.h +++ b/common/htlc_tx.h @@ -3,10 +3,35 @@ #include "config.h" #include +struct keyset; struct preimage; struct pubkey; struct sha256_double; +static inline u64 htlc_timeout_fee(u64 feerate_per_kw) +{ + /* BOLT #3: + * + * The fee for an HTLC-timeout transaction MUST BE calculated to match: + * + * 1. Multiply `feerate_per_kw` by 663 and divide by 1000 (rounding + * down). + */ + return feerate_per_kw * 663 / 1000; +} + +static inline u64 htlc_success_fee(u64 feerate_per_kw) +{ + /* BOLT #3: + * + * The fee for an HTLC-success transaction MUST BE calculated to match: + * + * 1. Multiply `feerate_per_kw` by 703 and divide by 1000 (rounding + * down). + */ + return feerate_per_kw * 703 / 1000; +} + /* Create HTLC-success tx to spend a received HTLC commitment tx * output; doesn't fill in input witness. */ struct bitcoin_tx *htlc_success_tx(const tal_t *ctx, @@ -47,4 +72,18 @@ void htlc_timeout_tx_add_witness(struct bitcoin_tx *htlc_timeout, const secp256k1_ecdsa_signature *localsig, const secp256k1_ecdsa_signature *remotesig); + +/* Generate the witness script for an HTLC the other side offered: + * scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */ +u8 *htlc_received_wscript(const tal_t *ctx, + const struct ripemd160 *ripemd, + const struct abs_locktime *expiry, + const struct keyset *keyset); + +/* Generate the witness script for an HTLC this side offered: + * scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */ +u8 *htlc_offered_wscript(const tal_t *ctx, + const struct ripemd160 *ripemd, + const struct keyset *keyset); + #endif /* LIGHTNING_LIGHTNINGD_HTLC_TX_H */ diff --git a/common/initial_channel.c b/common/initial_channel.c new file mode 100644 index 000000000..03e7308c3 --- /dev/null +++ b/common/initial_channel.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +struct channel *new_initial_channel(const tal_t *ctx, + const struct sha256_double *funding_txid, + unsigned int funding_txout, + u64 funding_satoshis, + u64 local_msatoshi, + u32 feerate_per_kw, + const struct channel_config *local, + const struct channel_config *remote, + const struct basepoints *local_basepoints, + const struct basepoints *remote_basepoints, + const struct pubkey *local_funding_pubkey, + const struct pubkey *remote_funding_pubkey, + enum side funder) +{ + struct channel *channel = tal(ctx, struct channel); + + channel->funding_txid = *funding_txid; + channel->funding_txout = funding_txout; + if (funding_satoshis > UINT64_MAX / 1000) + return tal_free(channel); + + channel->funding_msat = funding_satoshis * 1000; + if (local_msatoshi > channel->funding_msat) + return tal_free(channel); + + channel->funder = funder; + channel->config[LOCAL] = local; + channel->config[REMOTE] = remote; + channel->funding_pubkey[LOCAL] = *local_funding_pubkey; + channel->funding_pubkey[REMOTE] = *remote_funding_pubkey; + channel->htlcs = NULL; + + channel->view[LOCAL].feerate_per_kw + = channel->view[REMOTE].feerate_per_kw + = feerate_per_kw; + + channel->view[LOCAL].owed_msat[LOCAL] + = channel->view[REMOTE].owed_msat[LOCAL] + = local_msatoshi; + channel->view[REMOTE].owed_msat[REMOTE] + = channel->view[LOCAL].owed_msat[REMOTE] + = channel->funding_msat - local_msatoshi; + + channel->basepoints[LOCAL] = *local_basepoints; + channel->basepoints[REMOTE] = *remote_basepoints; + + channel->commitment_number_obscurer + = commit_number_obscurer(&channel->basepoints[funder].payment, + &channel->basepoints[!funder].payment); + + return channel; +} + +/* FIXME: We could cache this. */ +struct bitcoin_tx *initial_channel_tx(const tal_t *ctx, + const u8 **wscript, + const struct channel *channel, + const struct pubkey *per_commitment_point, + enum side side) +{ + struct keyset keyset; + + /* This assumes no HTLCs! */ + assert(!channel->htlcs); + + if (!derive_keyset(per_commitment_point, + &channel->basepoints[side].payment, + &channel->basepoints[!side].payment, + &channel->basepoints[side].delayed_payment, + &channel->basepoints[!side].revocation, + &keyset)) + return NULL; + + *wscript = bitcoin_redeem_2of2(ctx, + &channel->funding_pubkey[side], + &channel->funding_pubkey[!side]); + + return initial_commit_tx(ctx, &channel->funding_txid, + channel->funding_txout, + channel->funding_msat / 1000, + channel->funder, + to_self_delay(channel, side), + &keyset, + channel->view[side].feerate_per_kw, + dust_limit_satoshis(channel, side), + channel->view[side].owed_msat[side], + channel->view[side].owed_msat[!side], + 0 ^ channel->commitment_number_obscurer, + side); +} + +static char *fmt_channel_view(const tal_t *ctx, const struct channel_view *view) +{ + return tal_fmt(ctx, "{ feerate_per_kw=%"PRIu64"," + " owed_local=%"PRIu64"," + " owed_remote=%"PRIu64" }", + view->feerate_per_kw, + view->owed_msat[LOCAL], + view->owed_msat[REMOTE]); +} + +/* FIXME: This should reference HTLCs somehow. */ +static char *fmt_channel(const tal_t *ctx, const struct channel *channel) +{ + return tal_fmt(ctx, "{ funding_msat=%"PRIu64"," + " funder=%s," + " local=%s," + " remote=%s }", + channel->funding_msat, + side_to_str(channel->funder), + fmt_channel_view(ctx, &channel->view[LOCAL]), + fmt_channel_view(ctx, &channel->view[REMOTE])); +} +REGISTER_TYPE_TO_STRING(channel, fmt_channel); diff --git a/common/initial_channel.h b/common/initial_channel.h new file mode 100644 index 000000000..886058a86 --- /dev/null +++ b/common/initial_channel.h @@ -0,0 +1,172 @@ +/* This represents a channel with no HTLCs: all that's required for openingd. */ +#ifndef LIGHTNING_COMMON_INITIAL_CHANNEL_H +#define LIGHTNING_COMMON_INITIAL_CHANNEL_H +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +struct signature; +struct added_htlc; +struct failed_htlc; +struct fulfilled_htlc; + +/* View from each side */ +struct channel_view { + /* Current feerate in satoshis per 1000 weight. */ + u64 feerate_per_kw; + + /* How much is owed to each side (includes pending changes) */ + u64 owed_msat[NUM_SIDES]; +}; + +struct channel { + /* Funding txid and output. */ + struct sha256_double funding_txid; + unsigned int funding_txout; + + /* Keys used to spend funding tx. */ + struct pubkey funding_pubkey[NUM_SIDES]; + + /* Millisatoshis in from commitment tx */ + u64 funding_msat; + + /* Who is paying fees. */ + enum side funder; + + /* Limits and settings on this channel. */ + const struct channel_config *config[NUM_SIDES]; + + /* Basepoints for deriving keys. */ + struct basepoints basepoints[NUM_SIDES]; + + /* Mask for obscuring the encoding of the commitment number. */ + u64 commitment_number_obscurer; + + /* All live HTLCs for this channel */ + struct htlc_map *htlcs; + + /* What it looks like to each side. */ + struct channel_view view[NUM_SIDES]; +}; + +/* Some requirements are self-specified (eg. my dust limit), others + * are force upon the other side (eg. minimum htlc you can add). + * + * These values are also universally in msatsoshi. These avoid + * confusion: use them! */ + +/* BOLT #2: + * + * `dust_limit_satoshis` is the threshold below which output should be + * generated for this node's commitment or HTLC transaction */ +static inline u64 dust_limit_satoshis(const struct channel *channel, + enum side side) +{ + return channel->config[side]->dust_limit_satoshis; +} +/* BOLT #2: + * + * `max_htlc_value_in_flight_msat` is a cap on total value of + * outstanding HTLCs, which allows a node to limit its exposure to + * HTLCs */ +static inline u64 max_htlc_value_in_flight_msat(const struct channel *channel, + enum side recipient) +{ + return channel->config[recipient]->max_htlc_value_in_flight_msat; +} +/* BOLT #2: + * + * similarly `max_accepted_htlcs` limits the number of outstanding + * HTLCs the other node can offer. */ +static inline u16 max_accepted_htlcs(const struct channel *channel, + enum side recipient) +{ + return channel->config[recipient]->max_accepted_htlcs; +} +/* BOLT #2: + * + * `channel_reserve_satoshis` is the minimum amount that the other + * node is to keep as a direct payment. */ +static inline u64 channel_reserve_msat(const struct channel *channel, + enum side side) +{ + return channel->config[!side]->channel_reserve_satoshis * 1000; +} +/* BOLT #2: + * + * `htlc_minimum_msat` indicates the smallest value HTLC this node will accept. + */ +static inline u32 htlc_minimum_msat(const struct channel *channel, + enum side recipient) +{ + return channel->config[recipient]->htlc_minimum_msat; +} +/* BOLT #2: + * + * `to_self_delay` is the number of blocks that the other nodes + * to-self outputs must be delayed, using `OP_CHECKSEQUENCEVERIFY` + * delays */ +static inline u16 to_self_delay(const struct channel *channel, enum side side) +{ + return channel->config[!side]->to_self_delay; +} + + +/** + * new_initial_channel: Given initial fees and funding, what is initial state? + * @ctx: tal context to allocate return value from. + * @funding_txid: The commitment transaction id. + * @funding_txout: The commitment transaction output number. + * @funding_satoshis: The commitment transaction amount. + * @local_msatoshi: The amount for the local side (remainder goes to remote) + * @feerate_per_kw: feerate per kiloweight (satoshis) for the commitment + * transaction and HTLCS + * @local: local channel configuration + * @remote: remote channel configuration + * @local_basepoints: local basepoints. + * @remote_basepoints: remote basepoints. + * @local_fundingkey: local funding key + * @remote_fundingkey: remote funding key + * @funder: which side initiated it. + * + * Returns channel, or NULL if malformed. + */ +struct channel *new_initial_channel(const tal_t *ctx, + const struct sha256_double *funding_txid, + unsigned int funding_txout, + u64 funding_satoshis, + u64 local_msatoshi, + u32 feerate_per_kw, + const struct channel_config *local, + const struct channel_config *remote, + const struct basepoints *local_basepoints, + const struct basepoints *remote_basepoints, + const struct pubkey *local_funding_pubkey, + const struct pubkey *remote_funding_pubkey, + enum side funder); + + +/** + * initial_channel_tx: Get the current commitment tx for the *empty* channel. + * @ctx: tal context to allocate return value from. + * @wscript: wscripts for the commitment tx. + * @channel: The channel to evaluate + * @per_commitment_point: Per-commitment point to determine keys + * @side: which side to get the commitment transaction for + * + * Returns the unsigned initial commitment transaction for @side. + */ +struct bitcoin_tx *initial_channel_tx(const tal_t *ctx, + const u8 **wscript, + const struct channel *channel, + const struct pubkey *per_commitment_point, + enum side side); + +#endif /* LIGHTNING_COMMON_INITIAL_CHANNEL_H */ diff --git a/common/initial_commit_tx.c b/common/initial_commit_tx.c new file mode 100644 index 000000000..5c923fcac --- /dev/null +++ b/common/initial_commit_tx.c @@ -0,0 +1,200 @@ +#include +#include +#include +#include +#include +#include +#include + +/* BOLT #3: + * + * The 48-bit commitment transaction number is obscured by `XOR` with + * the lower 48 bits of: + * + * SHA256(payment_basepoint from open_channel || payment_basepoint from accept_channel) + */ +u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint, + const struct pubkey *accepter_payment_basepoint) +{ + u8 ders[PUBKEY_DER_LEN * 2]; + struct sha256 sha; + be64 obscurer = 0; + + pubkey_to_der(ders, opener_payment_basepoint); + pubkey_to_der(ders + PUBKEY_DER_LEN, accepter_payment_basepoint); + + sha256(&sha, ders, sizeof(ders)); + /* Lower 48 bits */ + memcpy((u8 *)&obscurer + 2, sha.u.u8 + sizeof(sha.u.u8) - 6, 6); + return be64_to_cpu(obscurer); +} + +void try_subtract_fee(enum side funder, enum side side, + u64 base_fee_msat, u64 *self_msat, u64 *other_msat) +{ + u64 *funder_msat; + + if (funder == side) + funder_msat = self_msat; + else + funder_msat = other_msat; + + if (*funder_msat >= base_fee_msat) + *funder_msat -= base_fee_msat; + else + *funder_msat = 0; +} + +u8 *to_self_wscript(const tal_t *ctx, + u16 to_self_delay, + const struct keyset *keyset) +{ + return bitcoin_wscript_to_local(ctx, to_self_delay, + &keyset->self_revocation_key, + &keyset->self_delayed_payment_key); +} + +struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, + const struct sha256_double *funding_txid, + unsigned int funding_txout, + u64 funding_satoshis, + enum side funder, + u16 to_self_delay, + const struct keyset *keyset, + u64 feerate_per_kw, + u64 dust_limit_satoshis, + u64 self_pay_msat, + u64 other_pay_msat, + u64 obscured_commitment_number, + enum side side) +{ + const tal_t *tmpctx = tal_tmpctx(ctx); + u64 base_fee_msat; + struct bitcoin_tx *tx; + size_t n, untrimmed; + + assert(self_pay_msat + other_pay_msat <= funding_satoshis * 1000); + + /* BOLT #3: + * + * 1. Calculate which committed HTLCs need to be trimmed (see + * [Trimmed Outputs](#trimmed-outputs)). + */ + untrimmed = 0; + + /* BOLT #3: + * + * 2. Calculate the base [commitment transaction + * fee](#fee-calculation). + */ + base_fee_msat = commit_tx_base_fee(feerate_per_kw, untrimmed) * 1000; + + /* BOLT #3: + * + * 3. Subtract this base fee from the funder (either `to_local` or + * `to_remote`), with a floor of zero (see [Fee Payment](#fee-payment)). + */ + try_subtract_fee(funder, side, base_fee_msat, + &self_pay_msat, &other_pay_msat); + + /* Worst-case sizing: both to-local and to-remote outputs. */ + tx = bitcoin_tx(ctx, 1, untrimmed + 2); + + /* This could be done in a single loop, but we follow the BOLT + * literally to make comments in test vectors clearer. */ + + n = 0; + /* BOLT #3: + * + * 3. For every offered HTLC, if it is not trimmed, add an + * [offered HTLC output](#offered-htlc-outputs). + */ + + /* BOLT #3: + * + * 4. For every received HTLC, if it is not trimmed, add an + * [received HTLC output](#received-htlc-outputs). + */ + + /* BOLT #3: + * + * 5. If the `to_local` amount is greater or equal to + * `dust_limit_satoshis`, add a [`to_local` + * Output](#to-local-output). + */ + if (self_pay_msat / 1000 >= dust_limit_satoshis) { + u8 *wscript = to_self_wscript(tmpctx, to_self_delay,keyset); + tx->output[n].amount = self_pay_msat / 1000; + tx->output[n].script = scriptpubkey_p2wsh(tx, wscript); + n++; + } + + /* BOLT #3: + * + * 6. If the `to_remote` amount is greater or equal to + * `dust_limit_satoshis`, add a [`to_remote` + * Output](#to-remote-output). + */ + if (other_pay_msat / 1000 >= dust_limit_satoshis) { + /* BOLT #3: + * + * #### `to_remote` Output + * + * This output sends funds to the other peer, thus is a simple + * P2WPKH to `remotekey`. + */ + tx->output[n].amount = other_pay_msat / 1000; + tx->output[n].script = scriptpubkey_p2wpkh(tx, + &keyset->other_payment_key); + n++; + } + + assert(n <= tal_count(tx->output)); + tal_resize(&tx->output, n); + + /* BOLT #3: + * + * 7. Sort the outputs into [BIP 69 + * order](#transaction-input-and-output-ordering) + */ + permute_outputs(tx->output, tal_count(tx->output), NULL); + + /* BOLT #3: + * + * ## Commitment Transaction + * + * * version: 2 + */ + assert(tx->version == 2); + + /* BOLT #3: + * + * * locktime: upper 8 bits are 0x20, lower 24 bits are the lower + * 24 bits of the obscured commitment transaction number. + */ + tx->lock_time + = (0x20000000 | (obscured_commitment_number & 0xFFFFFF)); + + /* BOLT #3: + * + * * txin count: 1 + * * `txin[0]` outpoint: `txid` and `output_index` from + * `funding_created` message + */ + tx->input[0].txid = *funding_txid; + tx->input[0].index = funding_txout; + + /* BOLT #3: + * + * * `txin[0]` sequence: upper 8 bits are 0x80, lower 24 bits are + * upper 24 bits of the obscured commitment transaction number. + */ + tx->input[0].sequence_number + = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF)); + + /* Input amount needed for signature code. */ + tx->input[0].amount = tal_dup(tx->input, u64, &funding_satoshis); + + tal_free(tmpctx); + return tx; +} diff --git a/common/initial_commit_tx.h b/common/initial_commit_tx.h new file mode 100644 index 000000000..982a3d5aa --- /dev/null +++ b/common/initial_commit_tx.h @@ -0,0 +1,96 @@ +/* Commit tx without HTLC support; needed for openingd. */ +#ifndef LIGHTNING_COMMON_INITIAL_COMMIT_TX_H +#define LIGHTNING_COMMON_INITIAL_COMMIT_TX_H +#include "config.h" +#include +#include +#include + +struct keyset; +struct sha256_double; + +/* BOLT #3: + * + * This obscures the number of commitments made on the channel in the + * case of unilateral close, yet still provides a useful index for + * both nodes (who know the `payment_basepoint`s) to quickly find a + * revoked commitment transaction. + */ +u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint, + const struct pubkey *accepter_payment_basepoint); + +/* Helper to calculate the base fee if we have this many htlc outputs */ +static inline u64 commit_tx_base_fee(u64 feerate_per_kw, + size_t num_untrimmed_htlcs) +{ + u64 weight; + + /* BOLT #3: + * + * The base fee for a commitment transaction MUST BE + * calculated to match: + * + * 1. Start with `weight` = 724. + */ + weight = 724; + + /* BOLT #3: + * + * 2. For each committed HTLC, if that output is not trimmed + * as specified in [Trimmed Outputs](#trimmed-outputs), add + * 172 to `weight`. + */ + weight += 172 * num_untrimmed_htlcs; + + /* BOLT #3: + * + * 3. Multiply `feerate_per_kw` by `weight`, divide by 1000 + * (rounding down). + */ + return feerate_per_kw * weight / 1000; +} + +/** + * initial_commit_tx: create (unsigned) commitment tx to spend the funding tx output + * @ctx: context to allocate transaction and @htlc_map from. + * @funding_txid, @funding_out, @funding_satoshis: funding outpoint. + * @funder: is the LOCAL or REMOTE paying the fee? + * @keyset: keys derived for this commit tx. + * @feerate_per_kw: feerate to use + * @dust_limit_satoshis: dust limit below which to trim outputs. + * @self_pay_msat: amount to pay directly to self + * @other_pay_msat: amount to pay directly to the other side + * @obscured_commitment_number: number to encode in commitment transaction + * @side: side to generate commitment transaction for. + * + * We need to be able to generate the remote side's tx to create signatures, + * but the BOLT is expressed in terms of generating our local commitment + * transaction, so we carefully use the terms "self" and "other" here. + */ +struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, + const struct sha256_double *funding_txid, + unsigned int funding_txout, + u64 funding_satoshis, + enum side funder, + u16 to_self_delay, + const struct keyset *keyset, + u64 feerate_per_kw, + u64 dust_limit_satoshis, + u64 self_pay_msat, + u64 other_pay_msat, + u64 obscured_commitment_number, + enum side side); + +/* try_subtract_fee - take away this fee from the funder, or all if insufficient. */ +void try_subtract_fee(enum side funder, enum side side, + u64 base_fee_msat, u64 *self_msat, u64 *other_msat); + +/* Generate the witness script for the to-self output: + * scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */ +u8 *to_self_wscript(const tal_t *ctx, + u16 to_self_delay, + const struct keyset *keyset); + +/* To-other is simply: scriptpubkey_p2wpkh(tx, keyset->other_payment_key) */ + +#endif /* LIGHTNING_COMMON_INITIAL_COMMIT_TX_H */ diff --git a/overflows.h b/common/overflows.h similarity index 100% rename from overflows.h rename to common/overflows.h diff --git a/permute_tx.c b/common/permute_tx.c similarity index 100% rename from permute_tx.c rename to common/permute_tx.c diff --git a/permute_tx.h b/common/permute_tx.h similarity index 100% rename from permute_tx.h rename to common/permute_tx.h diff --git a/type_to_string.c b/common/type_to_string.c similarity index 100% rename from type_to_string.c rename to common/type_to_string.c diff --git a/type_to_string.h b/common/type_to_string.h similarity index 100% rename from type_to_string.h rename to common/type_to_string.h diff --git a/utils.c b/common/utils.c similarity index 100% rename from utils.c rename to common/utils.c diff --git a/utils.h b/common/utils.h similarity index 100% rename from utils.h rename to common/utils.h diff --git a/version.c b/common/version.c similarity index 100% rename from version.c rename to common/version.c diff --git a/version.h b/common/version.h similarity index 100% rename from version.h rename to common/version.h diff --git a/lightningd/withdraw_tx.c b/common/withdraw_tx.c similarity index 97% rename from lightningd/withdraw_tx.c rename to common/withdraw_tx.c index 36bf86503..6411d05db 100644 --- a/lightningd/withdraw_tx.c +++ b/common/withdraw_tx.c @@ -2,9 +2,9 @@ #include #include #include +#include #include #include -#include #include struct bitcoin_tx *withdraw_tx(const tal_t *ctx, diff --git a/lightningd/withdraw_tx.h b/common/withdraw_tx.h similarity index 100% rename from lightningd/withdraw_tx.h rename to common/withdraw_tx.h diff --git a/daemon/bitcoind.c b/daemon/bitcoind.c index 2b081f0e0..e1ad3b668 100644 --- a/daemon/bitcoind.c +++ b/daemon/bitcoind.c @@ -7,7 +7,6 @@ #include "json.h" #include "lightningd.h" #include "log.h" -#include "utils.h" #include #include #include @@ -17,6 +16,7 @@ #include #include #include +#include #include #include diff --git a/daemon/chaintopology.c b/daemon/chaintopology.c index f848aa99e..7b5e36199 100644 --- a/daemon/chaintopology.c +++ b/daemon/chaintopology.c @@ -6,13 +6,13 @@ #include "lightningd.h" #include "log.h" #include "timeout.h" -#include "utils.h" #include "watch.h" #include #include #include #include #include +#include #include static void start_poll_chaintip(struct chain_topology *topo); diff --git a/daemon/htlc_state.c b/daemon/htlc_state.c index 0219dec45..42d3bfbef 100644 --- a/daemon/htlc_state.c +++ b/daemon/htlc_state.c @@ -12,16 +12,6 @@ const char *htlc_state_name(enum htlc_state s) return "unknown"; } -enum htlc_state htlc_state_from_name(const char *name) -{ - size_t i; - - for (i = 0; enum_htlc_state_names[i].name; i++) - if (streq(enum_htlc_state_names[i].name, name)) - return enum_htlc_state_names[i].v; - return HTLC_STATE_INVALID; -} - /* This is the flags for each state. */ static const int per_state_bits[] = { [SENT_ADD_HTLC] = HTLC_ADDING + HTLC_LOCAL_F_OWNER diff --git a/daemon/invoice.c b/daemon/invoice.c index db66257ae..1d8620917 100644 --- a/daemon/invoice.c +++ b/daemon/invoice.c @@ -2,10 +2,10 @@ #include "invoice.h" #include "jsonrpc.h" #include "lightningd.h" -#include "utils.h" #include #include #include +#include #include struct invoice_waiter { diff --git a/daemon/jsonrpc.c b/daemon/jsonrpc.c index a32f644e3..63a7bba28 100644 --- a/daemon/jsonrpc.c +++ b/daemon/jsonrpc.c @@ -5,12 +5,12 @@ #include "jsonrpc.h" #include "lightningd.h" #include "log.h" -#include "version.h" #include #include #include #include #include +#include #include #include #include diff --git a/daemon/log.c b/daemon/log.c index a8f0fe910..9331c0a6b 100644 --- a/daemon/log.c +++ b/daemon/log.c @@ -1,6 +1,5 @@ #include "log.h" #include "pseudorand.h" -#include "utils.h" #include #include #include @@ -8,6 +7,7 @@ #include #include #include +#include #include #include #include diff --git a/daemon/log.h b/daemon/log.h index 1b36f358d..9fa5d36d9 100644 --- a/daemon/log.h +++ b/daemon/log.h @@ -1,9 +1,9 @@ #ifndef LIGHTNING_DAEMON_LOG_H #define LIGHTNING_DAEMON_LOG_H #include "config.h" -#include "type_to_string.h" #include #include +#include #include struct timerel; diff --git a/daemon/netaddr.c b/daemon/netaddr.c index 3eb50714f..9ce429dfd 100644 --- a/daemon/netaddr.c +++ b/daemon/netaddr.c @@ -1,7 +1,5 @@ #include "bitcoin/pullpush.h" #include "netaddr.h" -#include "type_to_string.h" -#include "utils.h" #include #include #include @@ -9,6 +7,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/daemon/options.c b/daemon/options.c index 99bd8b4c6..bcbdbc10b 100644 --- a/daemon/options.c +++ b/daemon/options.c @@ -8,13 +8,13 @@ #include "daemon/options.h" #include "daemon/routing.h" #include "lightningd/lightningd.h" -#include "version.h" #include #include #include #include #include #include +#include #include #include #include diff --git a/daemon/routing.c b/daemon/routing.c index ffd4ff0e9..26b55c529 100644 --- a/daemon/routing.c +++ b/daemon/routing.c @@ -1,6 +1,5 @@ #include "lightningd.h" #include "log.h" -#include "overflows.h" #include "pseudorand.h" #include "routing.h" #include "wire/gen_peer_wire.h" @@ -11,6 +10,7 @@ #include #include #include +#include #include /* 365.25 * 24 * 60 / 10 */ diff --git a/daemon/timeout.c b/daemon/timeout.c index 0ea87a713..b9e438aee 100644 --- a/daemon/timeout.c +++ b/daemon/timeout.c @@ -1,6 +1,6 @@ #include "lightningd.h" #include "timeout.h" -#include "utils.h" +#include struct oneshot { struct timers *timers; diff --git a/lightningd/Makefile b/lightningd/Makefile index 09b040953..413dd0fd4 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -36,21 +36,26 @@ LIGHTNINGD_OLD_LIB_SRC := \ 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/derive_basepoints.o \ + common/funding_tx.o \ + common/permute_tx.o \ + common/type_to_string.o \ + common/utils.o \ + common/version.o \ + common/withdraw_tx.o + # FIXME: put in a library so we don't link all of them. LIGHTNINGD_LIB_SRC := \ lightningd/bip32.c \ - lightningd/channel.c \ lightningd/channel_config.c \ - lightningd/commit_tx.c \ lightningd/cryptomsg.c \ lightningd/crypto_sync.c \ lightningd/debug.c \ lightningd/daemon_conn.c \ - lightningd/derive_basepoints.c \ lightningd/dev_disconnect.c \ - lightningd/funding_tx.c \ lightningd/gossip_msg.c \ - lightningd/htlc_tx.c \ lightningd/htlc_wire.c \ lightningd/key_derive.c \ lightningd/keyset.c \ @@ -59,8 +64,7 @@ LIGHTNINGD_LIB_SRC := \ lightningd/ping.c \ lightningd/sphinx.c \ lightningd/status.c \ - lightningd/utxo.c \ - lightningd/withdraw_tx.c + lightningd/utxo.c LIGHTNINGD_LIB_OBJS := $(LIGHTNINGD_LIB_SRC:.c=.o) LIGHTNINGD_LIB_HEADERS := $(LIGHTNINGD_LIB_SRC:.c=.h) @@ -100,8 +104,7 @@ LIGHTNINGD_HEADERS_NOGEN = \ $(LIGHTNINGD_LIB_HEADERS) \ $(WIRE_HEADERS) \ $(BITCOIN_HEADERS) \ - $(CORE_HEADERS) \ - $(CORE_TX_HEADERS) \ + $(COMMON_HEADERS) \ $(DAEMON_HEADERS) \ $(WALLET_LIB_HEADERS) @@ -148,7 +151,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_JSMN_OBJS) $(CORE_OBJS) $(CORE_TX_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_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 clean: lightningd-clean diff --git a/lightningd/build_utxos.c b/lightningd/build_utxos.c index cdc14ca23..134f81025 100644 --- a/lightningd/build_utxos.c +++ b/lightningd/build_utxos.c @@ -1,10 +1,10 @@ #include #include #include +#include #include #include #include -#include #include diff --git a/lightningd/channel/Makefile b/lightningd/channel/Makefile index b859b470d..4f7fe8081 100644 --- a/lightningd/channel/Makefile +++ b/lightningd/channel/Makefile @@ -13,14 +13,29 @@ LIGHTNINGD_CHANNEL_HEADERS_GEN := \ lightningd/channel/gen_channel_wire.h LIGHTNINGD_CHANNEL_HEADERS_NOGEN := \ - lightningd/channel/channeld_htlc.h + lightningd/channel/channeld_htlc.h \ + lightningd/channel/commit_tx.h \ + lightningd/channel/full_channel.h LIGHTNINGD_CHANNEL_HEADERS := $(LIGHTNINGD_CHANNEL_HEADERS_GEN) $(LIGHTNINGD_CHANNEL_HEADERS_NOGEN) LIGHTNINGD_CHANNEL_SRC := lightningd/channel/channel.c \ + lightningd/channel/commit_tx.c \ + lightningd/channel/full_channel.c \ $(LIGHTNINGD_CHANNEL_HEADERS_GEN:.h=.c) LIGHTNINGD_CHANNEL_OBJS := $(LIGHTNINGD_CHANNEL_SRC:.c=.o) +# Common source we use. +CHANNELD_COMMON_OBJS := \ + common/derive_basepoints.o \ + common/htlc_tx.o \ + common/initial_channel.o \ + common/initial_commit_tx.o \ + common/permute_tx.o \ + common/type_to_string.o \ + common/utils.o \ + common/version.o + # Control daemon uses this: LIGHTNINGD_CHANNEL_CONTROL_HEADERS := $(LIGHTNINGD_CHANNEL_HEADERS_GEN) LIGHTNINGD_CHANNEL_CONTROL_SRC := $(LIGHTNINGD_CHANNEL_HEADERS_GEN:.h=.c) @@ -44,7 +59,7 @@ lightningd/channel/gen_channel_wire.c: $(WIRE_GEN) lightningd/channel/channel_wi LIGHTNINGD_CHANNEL_OBJS := $(LIGHTNINGD_CHANNEL_SRC:.c=.o) $(LIGHTNINGD_CHANNEL_GEN_SRC:.c=.o) -lightningd/lightningd_channel: $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_CHANNEL_OBJS) $(WIRE_ONION_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CLIENT_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a +lightningd/lightningd_channel: $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_CHANNEL_OBJS) $(WIRE_ONION_OBJS) $(CHANNELD_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CLIENT_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) check-source: $(LIGHTNINGD_CHANNEL_SRC_NOGEN:%=check-src-include-order/%) diff --git a/lightningd/channel/channel.c b/lightningd/channel/channel.c index 4ee71a18b..a7348576a 100644 --- a/lightningd/channel/channel.c +++ b/lightningd/channel/channel.c @@ -11,20 +11,22 @@ #include #include #include +#include +#include +#include +#include #include #include #include #include -#include +#include +#include #include -#include #include #include #include #include -#include #include -#include #include #include #include @@ -34,8 +36,6 @@ #include #include #include -#include -#include #include #include #include @@ -1632,9 +1632,9 @@ again: start_commit_timer(peer); /* Now, re-send any that we're supposed to be failing. */ - for (htlc = htlc_map_first(&peer->channel->htlcs, &it); + for (htlc = htlc_map_first(peer->channel->htlcs, &it); htlc; - htlc = htlc_map_next(&peer->channel->htlcs, &it)) { + htlc = htlc_map_next(peer->channel->htlcs, &it)) { if (htlc->state == SENT_REMOVE_HTLC) send_fail_or_fulfill(peer, htlc); } diff --git a/lightningd/commit_tx.c b/lightningd/channel/commit_tx.c similarity index 72% rename from lightningd/commit_tx.c rename to lightningd/channel/commit_tx.c index 0941249b4..d70b23ac2 100644 --- a/lightningd/commit_tx.c +++ b/lightningd/channel/commit_tx.c @@ -1,78 +1,16 @@ #include #include #include -#include +#include +#include +#include +#include #include -#include -#include #ifndef SUPERVERBOSE #define SUPERVERBOSE(...) #endif -/* BOLT #3: - * - * The 48-bit commitment transaction number is obscured by `XOR` with - * the lower 48 bits of: - * - * SHA256(payment_basepoint from open_channel || payment_basepoint from accept_channel) - */ -u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint, - const struct pubkey *accepter_payment_basepoint) -{ - u8 ders[PUBKEY_DER_LEN * 2]; - struct sha256 sha; - be64 obscurer = 0; - - pubkey_to_der(ders, opener_payment_basepoint); - pubkey_to_der(ders + PUBKEY_DER_LEN, accepter_payment_basepoint); - - sha256(&sha, ders, sizeof(ders)); - /* Lower 48 bits */ - memcpy((u8 *)&obscurer + 2, sha.u.u8 + sizeof(sha.u.u8) - 6, 6); - return be64_to_cpu(obscurer); -} - -static void subtract_fee(enum side funder, enum side side, - u64 base_fee_msat, u64 *self_msat, u64 *other_msat) -{ - u64 *funder_msat; - - if (funder == side) - funder_msat = self_msat; - else - funder_msat = other_msat; - - if (*funder_msat >= base_fee_msat) - *funder_msat -= base_fee_msat; - else - *funder_msat = 0; -} - -u64 htlc_timeout_fee(u64 feerate_per_kw) -{ - /* BOLT #3: - * - * The fee for an HTLC-timeout transaction MUST BE calculated to match: - * - * 1. Multiply `feerate_per_kw` by 663 and divide by 1000 (rounding - * down). - */ - return feerate_per_kw * 663 / 1000; -} - -u64 htlc_success_fee(u64 feerate_per_kw) -{ - /* BOLT #3: - * - * The fee for an HTLC-success transaction MUST BE calculated to match: - * - * 1. Multiply `feerate_per_kw` by 703 and divide by 1000 (rounding - * down). - */ - return feerate_per_kw * 703 / 1000; -} - static bool trim(const struct htlc *htlc, u64 feerate_per_kw, u64 dust_limit_satoshis, enum side side) @@ -115,46 +53,6 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs, return n; } -u64 commit_tx_base_fee(u64 feerate_per_kw, size_t num_untrimmed_htlcs) -{ - u64 weight; - - /* BOLT #3: - * - * The base fee for a commitment transaction MUST BE - * calculated to match: - * - * 1. Start with `weight` = 724. - */ - weight = 724; - - /* BOLT #3: - * - * 2. For each committed HTLC, if that output is not trimmed - * as specified in [Trimmed Outputs](#trimmed-outputs), add - * 172 to `weight`. - */ - weight += 172 * num_untrimmed_htlcs; - - /* BOLT #3: - * - * 3. Multiply `feerate_per_kw` by `weight`, divide by 1000 - * (rounding down). - */ - return feerate_per_kw * weight / 1000; -} - -u8 *htlc_offered_wscript(const tal_t *ctx, - const struct ripemd160 *ripemd, - const struct keyset *keyset) -{ - return bitcoin_wscript_htlc_offer_ripemd160(ctx, - &keyset->self_payment_key, - &keyset->other_payment_key, - ripemd, - &keyset->self_revocation_key); -} - static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n, const struct htlc *htlc, const struct keyset *keyset) @@ -171,19 +69,6 @@ static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n, tal_free(wscript); } -u8 *htlc_received_wscript(const tal_t *ctx, - const struct ripemd160 *ripemd, - const struct abs_locktime *expiry, - const struct keyset *keyset) -{ - return bitcoin_wscript_htlc_receive_ripemd(ctx, - expiry, - &keyset->self_payment_key, - &keyset->other_payment_key, - ripemd, - &keyset->self_revocation_key); -} - static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n, const struct htlc *htlc, const struct keyset *keyset) @@ -200,15 +85,6 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n, tal_free(wscript); } -u8 *to_self_wscript(const tal_t *ctx, - u16 to_self_delay, - const struct keyset *keyset) -{ - return bitcoin_wscript_to_local(ctx, to_self_delay, - &keyset->self_revocation_key, - &keyset->self_delayed_payment_key); -} - struct bitcoin_tx *commit_tx(const tal_t *ctx, const struct sha256_double *funding_txid, unsigned int funding_txout, @@ -256,8 +132,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, * 3. Subtract this base fee from the funder (either `to_local` or * `to_remote`), with a floor of zero (see [Fee Payment](#fee-payment)). */ - subtract_fee(funder, side, base_fee_msat, - &self_pay_msat, &other_pay_msat); + try_subtract_fee(funder, side, base_fee_msat, + &self_pay_msat, &other_pay_msat); #ifdef PRINT_ACTUAL_FEE { diff --git a/lightningd/commit_tx.h b/lightningd/channel/commit_tx.h similarity index 60% rename from lightningd/commit_tx.h rename to lightningd/channel/commit_tx.h index 16c3c5510..467d2f54d 100644 --- a/lightningd/commit_tx.h +++ b/lightningd/channel/commit_tx.h @@ -2,26 +2,13 @@ #define LIGHTNING_LIGHTNINGD_COMMIT_TX_H #include "config.h" #include +#include #include #include struct keyset; struct sha256_double; -/* BOLT #3: - * - * This obscures the number of commitments made on the channel in the - * case of unilateral close, yet still provides a useful index for - * both nodes (who know the `payment_basepoint`s) to quickly find a - * revoked commitment transaction. - */ -u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint, - const struct pubkey *accepter_payment_basepoint); - -/* commit_tx needs to know these so it knows what outputs to trim */ -u64 htlc_success_fee(u64 feerate_per_kw); -u64 htlc_timeout_fee(u64 feerate_per_kw); - /** * commit_tx_num_untrimmed: how many of these htlc outputs will commit tx have? * @htlcs: tal_arr of HTLCs @@ -36,9 +23,6 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs, u64 feerate_per_kw, u64 dust_limit_satoshis, enum side side); -/* Helper to calculate the base fee if we add this extra htlc */ -u64 commit_tx_base_fee(u64 feerate_per_kw, size_t num_untrimmed_htlcs); - /** * commit_tx: create (unsigned) commitment tx to spend the funding tx output * @ctx: context to allocate transaction and @htlc_map from. @@ -74,27 +58,4 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, u64 obscured_commitment_number, enum side side); - -/* Generate the witness script for an HTLC the other side offered: - * scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */ -u8 *htlc_received_wscript(const tal_t *ctx, - const struct ripemd160 *ripemd, - const struct abs_locktime *expiry, - const struct keyset *keyset); - -/* Generate the witness script for an HTLC this side offered: - * scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */ -u8 *htlc_offered_wscript(const tal_t *ctx, - const struct ripemd160 *ripemd, - const struct keyset *keyset); - - -/* Generate the witness script for the to-self output: - * scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */ -u8 *to_self_wscript(const tal_t *ctx, - u16 to_self_delay, - const struct keyset *keyset); - -/* To-other is simply: scriptpubkey_p2wpkh(tx, keyset->other_payment_key) */ - #endif /* LIGHTNING_LIGHTNINGD_COMMIT_TX_H */ diff --git a/lightningd/channel.c b/lightningd/channel/full_channel.c similarity index 89% rename from lightningd/channel.c rename to lightningd/channel/full_channel.c index b00dff307..14245f82d 100644 --- a/lightningd/channel.c +++ b/lightningd/channel/full_channel.c @@ -1,6 +1,3 @@ -#include "channel.h" -#include "commit_tx.h" -#include "type_to_string.h" #include #include #include @@ -9,16 +6,52 @@ #include #include #include +#include +#include #include #include +#include +#include #include -#include #include #include #include #include #include +struct channel *new_channel(const tal_t *ctx, + const struct sha256_double *funding_txid, + unsigned int funding_txout, + u64 funding_satoshis, + u64 local_msatoshi, + u32 feerate_per_kw, + const struct channel_config *local, + const struct channel_config *remote, + const struct basepoints *local_basepoints, + const struct basepoints *remote_basepoints, + const struct pubkey *local_funding_pubkey, + const struct pubkey *remote_funding_pubkey, + enum side funder) +{ + struct channel *channel = new_initial_channel(ctx, funding_txid, + funding_txout, + funding_satoshis, + local_msatoshi, + feerate_per_kw, + local, remote, + local_basepoints, + remote_basepoints, + local_funding_pubkey, + remote_funding_pubkey, + funder); + if (channel) { + channel->htlcs = tal(channel, struct htlc_map); + htlc_map_init(channel->htlcs); + tal_add_destructor(channel->htlcs, htlc_map_clear); + } + return channel; +} + static void htlc_arr_append(const struct htlc ***arr, const struct htlc *htlc) { size_t n; @@ -77,9 +110,9 @@ void dump_htlcs(const struct channel *channel, const char *prefix) struct htlc_map_iter it; const struct htlc *htlc; - for (htlc = htlc_map_first(&channel->htlcs, &it); + for (htlc = htlc_map_first(channel->htlcs, &it); htlc; - htlc = htlc_map_next(&channel->htlcs, &it)) { + htlc = htlc_map_next(channel->htlcs, &it)) { dump_htlc(htlc, prefix); } } @@ -107,9 +140,12 @@ static void gather_htlcs(const tal_t *ctx, if (pending_addition) *pending_addition = tal_arr(ctx, const struct htlc *, 0); - for (htlc = htlc_map_first(&channel->htlcs, &it); + if (!channel->htlcs) + return; + + for (htlc = htlc_map_first(channel->htlcs, &it); htlc; - htlc = htlc_map_next(&channel->htlcs, &it)) { + htlc = htlc_map_next(channel->htlcs, &it)) { if (htlc_has(htlc, committed_flag)) { htlc_arr_append(committed, htlc); if (htlc_has(htlc, pending_flag)) @@ -131,65 +167,6 @@ static u64 total_offered_msatoshis(const struct htlc **htlcs, enum side side) return total; } -static void destroy_htlc_map(struct channel *channel) -{ - htlc_map_clear(&channel->htlcs); -} - -struct channel *new_channel(const tal_t *ctx, - const struct sha256_double *funding_txid, - unsigned int funding_txout, - u64 funding_satoshis, - u64 local_msatoshi, - u32 feerate_per_kw, - const struct channel_config *local, - const struct channel_config *remote, - const struct basepoints *local_basepoints, - const struct basepoints *remote_basepoints, - const struct pubkey *local_funding_pubkey, - const struct pubkey *remote_funding_pubkey, - enum side funder) -{ - struct channel *channel = tal(ctx, struct channel); - - channel->funding_txid = *funding_txid; - channel->funding_txout = funding_txout; - if (funding_satoshis > UINT64_MAX / 1000) - return tal_free(channel); - - channel->funding_msat = funding_satoshis * 1000; - if (local_msatoshi > channel->funding_msat) - return tal_free(channel); - - channel->funder = funder; - channel->config[LOCAL] = local; - channel->config[REMOTE] = remote; - channel->funding_pubkey[LOCAL] = *local_funding_pubkey; - channel->funding_pubkey[REMOTE] = *remote_funding_pubkey; - htlc_map_init(&channel->htlcs); - - channel->view[LOCAL].feerate_per_kw - = channel->view[REMOTE].feerate_per_kw - = feerate_per_kw; - - channel->view[LOCAL].owed_msat[LOCAL] - = channel->view[REMOTE].owed_msat[LOCAL] - = local_msatoshi; - channel->view[REMOTE].owed_msat[REMOTE] - = channel->view[LOCAL].owed_msat[REMOTE] - = channel->funding_msat - local_msatoshi; - - channel->basepoints[LOCAL] = *local_basepoints; - channel->basepoints[REMOTE] = *remote_basepoints; - - channel->commitment_number_obscurer - = commit_number_obscurer(&channel->basepoints[funder].payment, - &channel->basepoints[!funder].payment); - - tal_add_destructor(channel, destroy_htlc_map); - return channel; -} - static void add_htlcs(struct bitcoin_tx ***txs, const u8 ***wscripts, const struct htlc **htlcmap, @@ -305,13 +282,6 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx, return txs; } -struct channel *copy_channel(const tal_t *ctx, const struct channel *old) -{ - struct channel *new = tal_dup(ctx, struct channel, old); - htlc_map_copy(&new->htlcs, &old->htlcs); - return new; -} - static enum channel_add_err add_htlc(struct channel *channel, enum side sender, u64 id, u64 msatoshi, u32 cltv_expiry, @@ -331,8 +301,6 @@ static enum channel_add_err add_htlc(struct channel *channel, htlc = tal(tmpctx, struct htlc); - /* FIXME: Don't need fields: peer, deadline, src. */ - if (sender == LOCAL) htlc->state = SENT_ADD_HTLC; else @@ -358,7 +326,7 @@ static enum channel_add_err add_htlc(struct channel *channel, htlc->r = NULL; htlc->routing = tal_dup_arr(htlc, u8, routing, TOTAL_PACKET_SIZE, 0); - old = htlc_get(&channel->htlcs, htlc->id, htlc_owner(htlc)); + old = htlc_get(channel->htlcs, htlc->id, htlc_owner(htlc)); if (old) { if (old->state != htlc->state || old->msatoshi != htlc->msatoshi @@ -479,7 +447,7 @@ static enum channel_add_err add_htlc(struct channel *channel, } dump_htlc(htlc, "NEW:"); - htlc_map_add(&channel->htlcs, tal_steal(channel, htlc)); + htlc_map_add(channel->htlcs, tal_steal(channel, htlc)); e = CHANNEL_ERR_ADD_OK; if (htlcp) *htlcp = htlc; @@ -504,7 +472,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel, struct htlc *channel_get_htlc(struct channel *channel, enum side sender, u64 id) { - return htlc_get(&channel->htlcs, id, sender); + return htlc_get(channel->htlcs, id, sender); } enum channel_remove_err channel_fulfill_htlc(struct channel *channel, @@ -670,9 +638,9 @@ static int change_htlcs(struct channel *channel, int cflags = 0; size_t i; - for (h = htlc_map_first(&channel->htlcs, &it); + for (h = htlc_map_first(channel->htlcs, &it); h; - h = htlc_map_next(&channel->htlcs, &it)) { + h = htlc_map_next(channel->htlcs, &it)) { for (i = 0; i < n_hstates; i++) { if (h->state == htlc_states[i]) { htlc_incstate(channel, h, sidechanged); @@ -756,9 +724,9 @@ bool channel_awaiting_revoke_and_ack(const struct channel *channel) struct htlc *h; size_t i; - for (h = htlc_map_first(&channel->htlcs, &it); + for (h = htlc_map_first(channel->htlcs, &it); h; - h = htlc_map_next(&channel->htlcs, &it)) { + h = htlc_map_next(channel->htlcs, &it)) { for (i = 0; i < ARRAY_SIZE(states); i++) if (h->state == states[i]) return true; @@ -771,9 +739,9 @@ bool channel_has_htlcs(const struct channel *channel) struct htlc_map_iter it; const struct htlc *htlc; - for (htlc = htlc_map_first(&channel->htlcs, &it); + for (htlc = htlc_map_first(channel->htlcs, &it); htlc; - htlc = htlc_map_next(&channel->htlcs, &it)) { + htlc = htlc_map_next(channel->htlcs, &it)) { /* FIXME: Clean these out! */ if (!htlc_is_dead(htlc)) return true; @@ -962,26 +930,3 @@ bool channel_force_htlcs(struct channel *channel, return true; } - -static char *fmt_channel_view(const tal_t *ctx, const struct channel_view *view) -{ - return tal_fmt(ctx, "{ feerate_per_kw=%"PRIu64"," - " owed_local=%"PRIu64"," - " owed_remote=%"PRIu64" }", - view->feerate_per_kw, - view->owed_msat[LOCAL], - view->owed_msat[REMOTE]); -} - -static char *fmt_channel(const tal_t *ctx, const struct channel *channel) -{ - return tal_fmt(ctx, "{ funding_msat=%"PRIu64"," - " funder=%s," - " local=%s," - " remote=%s }", - channel->funding_msat, - side_to_str(channel->funder), - fmt_channel_view(ctx, &channel->view[LOCAL]), - fmt_channel_view(ctx, &channel->view[REMOTE])); -} -REGISTER_TYPE_TO_STRING(channel, fmt_channel); diff --git a/lightningd/channel.h b/lightningd/channel/full_channel.h similarity index 71% rename from lightningd/channel.h rename to lightningd/channel/full_channel.h index 7d5347186..518a8c8b1 100644 --- a/lightningd/channel.h +++ b/lightningd/channel/full_channel.h @@ -1,123 +1,10 @@ +/* This is the full channel routines, with HTLC support. */ #ifndef LIGHTNING_LIGHTNINGD_CHANNEL_H #define LIGHTNING_LIGHTNINGD_CHANNEL_H #include "config.h" -#include -#include -#include -#include -#include +#include #include -#include -#include #include -#include - -struct signature; -struct added_htlc; -struct failed_htlc; -struct fulfilled_htlc; - -/* View from each side */ -struct channel_view { - /* Current feerate in satoshis per 1000 weight. */ - u64 feerate_per_kw; - - /* How much is owed to each side (includes pending changes) */ - u64 owed_msat[NUM_SIDES]; -}; - -struct channel { - /* Funding txid and output. */ - struct sha256_double funding_txid; - unsigned int funding_txout; - - /* Keys used to spend funding tx. */ - struct pubkey funding_pubkey[NUM_SIDES]; - - /* Millisatoshis in from commitment tx */ - u64 funding_msat; - - /* Who is paying fees. */ - enum side funder; - - /* Limits and settings on this channel. */ - const struct channel_config *config[NUM_SIDES]; - - /* Basepoints for deriving keys. */ - struct basepoints basepoints[NUM_SIDES]; - - /* Mask for obscuring the encoding of the commitment number. */ - u64 commitment_number_obscurer; - - /* All live HTLCs for this channel */ - struct htlc_map htlcs; - - /* What it looks like to each side. */ - struct channel_view view[NUM_SIDES]; -}; - -/* Some requirements are self-specified (eg. my dust limit), others - * are force upon the other side (eg. minimum htlc you can add). - * - * These values are also universally in msatsoshi. These avoid - * confusion: use them! */ - -/* BOLT #2: - * - * `dust_limit_satoshis` is the threshold below which output should be - * generated for this node's commitment or HTLC transaction */ -static inline u64 dust_limit_satoshis(const struct channel *channel, - enum side side) -{ - return channel->config[side]->dust_limit_satoshis; -} -/* BOLT #2: - * - * `max_htlc_value_in_flight_msat` is a cap on total value of - * outstanding HTLCs, which allows a node to limit its exposure to - * HTLCs */ -static inline u64 max_htlc_value_in_flight_msat(const struct channel *channel, - enum side recipient) -{ - return channel->config[recipient]->max_htlc_value_in_flight_msat; -} -/* BOLT #2: - * - * similarly `max_accepted_htlcs` limits the number of outstanding - * HTLCs the other node can offer. */ -static inline u16 max_accepted_htlcs(const struct channel *channel, - enum side recipient) -{ - return channel->config[recipient]->max_accepted_htlcs; -} -/* BOLT #2: - * - * `channel_reserve_satoshis` is the minimum amount that the other - * node is to keep as a direct payment. */ -static inline u64 channel_reserve_msat(const struct channel *channel, - enum side side) -{ - return channel->config[!side]->channel_reserve_satoshis * 1000; -} -/* BOLT #2: - * - * `htlc_minimum_msat` indicates the smallest value HTLC this node will accept. - */ -static inline u32 htlc_minimum_msat(const struct channel *channel, - enum side recipient) -{ - return channel->config[recipient]->htlc_minimum_msat; -} -/* BOLT #2: - * - * `to_self_delay` is the number of blocks that the other nodes - * to-self outputs must be delayed, using `OP_CHECKSEQUENCEVERIFY` - * delays */ -static inline u16 to_self_delay(const struct channel *channel, enum side side) -{ - return channel->config[!side]->to_self_delay; -} - /** * new_channel: Given initial fees and funding, what is initial state? @@ -191,13 +78,6 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx, uint32_t actual_feerate(const struct channel *channel, const struct signature *theirsig); -/** - * copy_channel: Make a deep copy of channel - * @ctx: tal context to allocate return value from. - * @channel: channel to copy. - */ -struct channel *copy_channel(const tal_t *ctx, const struct channel *channel); - enum channel_add_err { /* All OK! */ CHANNEL_ERR_ADD_OK, @@ -313,17 +193,6 @@ bool can_afford_feerate(const struct channel *channel, u64 feerate_per_kw); */ void adjust_fee(struct channel *channel, u64 feerate_per_kw, enum side side); -/** - * force_fee: Change fees to a specific value. - * @channel: The channel state - * @fee: fee in satoshi. - * - * This is used for the close transaction, which specifies an exact fee. - * If the fee cannot be paid in full, this return false (but cstate will - * still be altered). - */ -bool force_fee(struct channel *channel, u64 fee); - /** * channel_sending_commit: commit all remote outstanding changes. * @channel: the channel diff --git a/lightningd/closing/Makefile b/lightningd/closing/Makefile index 15caa218e..f41816c57 100644 --- a/lightningd/closing/Makefile +++ b/lightningd/closing/Makefile @@ -35,6 +35,15 @@ LIGHTNINGD_HEADERS_NOGEN += $(LIGHTNINGD_CLOSING_HEADERS_NOGEN) $(LIGHTNINGD_CLOSING_OBJS): $(LIGHTNINGD_HEADERS) +# Common source we use. +CLOSINGD_COMMON_OBJS := \ + common/close_tx.o \ + common/derive_basepoints.o \ + common/permute_tx.o \ + common/type_to_string.o \ + common/utils.o \ + common/version.o + lightningd/closing/gen_closing_wire.h: $(WIRE_GEN) lightningd/closing/closing_wire.csv $(WIRE_GEN) --header $@ closing_wire_type < lightningd/closing/closing_wire.csv > $@ @@ -43,7 +52,7 @@ lightningd/closing/gen_closing_wire.c: $(WIRE_GEN) lightningd/closing/closing_wi LIGHTNINGD_CLOSING_OBJS := $(LIGHTNINGD_CLOSING_SRC:.c=.o) $(LIGHTNINGD_CLOSING_GEN_SRC:.c=.o) -lightningd/lightningd_closing: $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_CLOSING_OBJS) $(WIRE_ONION_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CLIENT_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a +lightningd/lightningd_closing: $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_CLOSING_OBJS) $(WIRE_ONION_OBJS) $(CLOSINGD_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CLIENT_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) check-source: $(LIGHTNINGD_CLOSING_SRC_NOGEN:%=check-src-include-order/%) diff --git a/lightningd/closing/closing.c b/lightningd/closing/closing.c index 7a0d8597b..e7a2351f0 100644 --- a/lightningd/closing/closing.c +++ b/lightningd/closing/closing.c @@ -1,19 +1,19 @@ #include -#include +#include +#include +#include +#include +#include #include #include #include #include #include #include -#include #include #include #include #include -#include -#include -#include #include #include diff --git a/lightningd/crypto_sync.c b/lightningd/crypto_sync.c index 9f1ee67d8..b3abcfd09 100644 --- a/lightningd/crypto_sync.c +++ b/lightningd/crypto_sync.c @@ -1,11 +1,11 @@ #include +#include #include #include #include #include #include #include -#include #include #include diff --git a/lightningd/cryptomsg.c b/lightningd/cryptomsg.c index ef3ac9da2..d9b386d79 100644 --- a/lightningd/cryptomsg.c +++ b/lightningd/cryptomsg.c @@ -6,11 +6,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include #include diff --git a/lightningd/dev_ping.c b/lightningd/dev_ping.c index 77ac981a1..8e6078975 100644 --- a/lightningd/dev_ping.c +++ b/lightningd/dev_ping.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,7 +8,6 @@ #include #include #include -#include static bool ping_reply(struct subd *subd, const u8 *msg, const int *fds, struct command *cmd) diff --git a/lightningd/gossip/Makefile b/lightningd/gossip/Makefile index 5990d6a58..9b3188b3a 100644 --- a/lightningd/gossip/Makefile +++ b/lightningd/gossip/Makefile @@ -30,13 +30,19 @@ LIGHTNINGD_GOSSIP_ALLHEADERS_NOGEN := $(filter-out lightningd/gossip/gen_%, $(LI # Add to headers which any object might need. LIGHTNINGD_HEADERS_GEN += $(LIGHTNINGD_GOSSIP_HEADERS) +# Common source we use. +GOSSIPD_COMMON_OBJS := \ + common/type_to_string.o \ + common/utils.o \ + common/version.o + $(LIGHTNINGD_GOSSIP_OBJS) $(LIGHTNINGD_GOSSIP_CLIENT_OBJS): $(LIGHTNINGD_HEADERS) $(LIGHTNINGD_GOSSIP_CONTROL_OBJS) : $(LIGHTNINGD_GOSSIP_CONTROL_HEADERS) lightningd/gossip-all: lightningd/lightningd_gossip $(LIGHTNINGD_GOSSIP_CLIENT_OBJS) -lightningd/lightningd_gossip: $(LIGHTNINGD_GOSSIP_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a +lightningd/lightningd_gossip: $(LIGHTNINGD_GOSSIP_OBJS) $(GOSSIPD_COMMON_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) lightningd/gossip/gen_gossip_wire.h: $(WIRE_GEN) lightningd/gossip/gossip_wire.csv diff --git a/lightningd/gossip/gossip.c b/lightningd/gossip/gossip.c index 89d3d23c7..00e8db8b7 100644 --- a/lightningd/gossip/gossip.c +++ b/lightningd/gossip/gossip.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include #include @@ -29,8 +31,6 @@ #include #include #include -#include -#include #include #include diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 166d29230..7f79268c8 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/lightningd/handshake/Makefile b/lightningd/handshake/Makefile index 75e268a4f..6e4c9f67b 100644 --- a/lightningd/handshake/Makefile +++ b/lightningd/handshake/Makefile @@ -29,6 +29,12 @@ LIGHTNINGD_HEADERS_GEN += $(LIGHTNINGD_HANDSHAKE_HEADERS) $(LIGHTNINGD_HANDSHAKE_OBJS): $(LIGHTNINGD_HEADERS) +# Common source we use. +HANDSHAKED_COMMON_OBJS := \ + common/type_to_string.o \ + common/utils.o \ + common/version.o + lightningd/handshake/gen_handshake_wire.h: $(WIRE_GEN) lightningd/handshake/handshake_wire.csv $(WIRE_GEN) --header $@ handshake_wire_type < lightningd/handshake/handshake_wire.csv > $@ @@ -37,7 +43,7 @@ lightningd/handshake/gen_handshake_wire.c: $(WIRE_GEN) lightningd/handshake/hand LIGHTNINGD_HANDSHAKE_OBJS := $(LIGHTNINGD_HANDSHAKE_SRC:.c=.o) $(LIGHTNINGD_HANDSHAKE_GEN_SRC:.c=.o) -lightningd/lightningd_handshake: $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_HANDSHAKE_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CLIENT_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a +lightningd/lightningd_handshake: $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_HANDSHAKE_OBJS) $(HANDSHAKED_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CLIENT_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) check-source: $(LIGHTNINGD_HANDSHAKE_SRC_NOGEN:%=check-src-include-order/%) diff --git a/lightningd/handshake/handshake.c b/lightningd/handshake/handshake.c index b474df781..830f7e97f 100644 --- a/lightningd/handshake/handshake.c +++ b/lightningd/handshake/handshake.c @@ -1,4 +1,3 @@ -#include "type_to_string.h" #include #include #include @@ -8,6 +7,8 @@ #include #include #include +#include +#include #include #include #include @@ -20,7 +21,6 @@ #include #include #include -#include #include #include #include diff --git a/lightningd/hsm/Makefile b/lightningd/hsm/Makefile index 323c3b638..aa1a5f1a9 100644 --- a/lightningd/hsm/Makefile +++ b/lightningd/hsm/Makefile @@ -23,6 +23,14 @@ LIGHTNINGD_HSM_SRC := lightningd/hsm/hsm.c \ $(LIGHTNINGD_HSM_HEADERS:.h=.c) LIGHTNINGD_HSM_OBJS := $(LIGHTNINGD_HSM_SRC:.c=.o) +# Common source we use. +HSMD_COMMON_OBJS := \ + common/funding_tx.o \ + common/permute_tx.o \ + common/utils.o \ + common/version.o \ + common/withdraw_tx.o + # For checking LIGHTNINGD_HSM_ALLSRC_NOGEN := $(filter-out lightningd/hsm/gen_%, $(LIGHTNINGD_HSM_CLIENT_SRC) $(LIGHTNINGD_HSM_SRC)) LIGHTNINGD_HSM_ALLHEADERS_NOGEN := $(filter-out lightningd/hsm/gen_%, $(LIGHTNINGD_HSM_CLIENT_HEADERS) $(LIGHTNINGD_HSM_HEADERS)) @@ -34,11 +42,11 @@ $(LIGHTNINGD_HSM_OBJS) $(LIGHTNINGD_HSM_CLIENT_OBJS): $(LIGHTNINGD_HEADERS) $(LIGHTNINGD_HSM_CONTROL_OBJS) : $(LIGHTNINGD_HSM_CONTROL_HEADERS) -$(LIGHTNINGD_HSM_OBJS): $(CORE_TX_HEADERS) +$(LIGHTNINGD_HSM_OBJS): $(COMMON_HEADERS) lightningd/hsm-all: lightningd/lightningd_hsm $(LIGHTNINGD_HSM_CLIENT_OBJS) -lightningd/lightningd_hsm: $(LIGHTNINGD_HSM_OBJS) $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIBBASE58_OBJS) lightningd/utxo.o libsecp256k1.a libsodium.a libwallycore.a +lightningd/lightningd_hsm: $(LIGHTNINGD_HSM_OBJS) $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(HSMD_COMMON_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIBBASE58_OBJS) lightningd/utxo.o libsecp256k1.a libsodium.a libwallycore.a $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) lightningd/hsm/gen_hsm_client_wire.h: $(WIRE_GEN) lightningd/hsm/hsm_client_wire_csv diff --git a/lightningd/hsm/hsm.c b/lightningd/hsm/hsm.c index 71669ef68..bc8235aed 100644 --- a/lightningd/hsm/hsm.c +++ b/lightningd/hsm/hsm.c @@ -14,26 +14,25 @@ #include #include #include +#include +#include +#include +#include #include #include #include #include #include -#include #include #include #include #include -#include -#include #include #include #include #include #include #include -#include -#include #include #include #include diff --git a/lightningd/hsm_control.c b/lightningd/hsm_control.c index afef1ff6b..bec7204fd 100644 --- a/lightningd/hsm_control.c +++ b/lightningd/hsm_control.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/key_derive.c b/lightningd/key_derive.c index 6b0b7409e..7668c6e2c 100644 --- a/lightningd/key_derive.c +++ b/lightningd/key_derive.c @@ -1,8 +1,8 @@ #include #include #include +#include #include -#include #include /* BOLT #3: diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index 06c92557a..30334f75b 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include #include #include @@ -24,8 +26,6 @@ #include #include #include -#include -#include char *bitcoin_datadir; diff --git a/lightningd/onchain/Makefile b/lightningd/onchain/Makefile index 1f4f6e757..20747d21b 100644 --- a/lightningd/onchain/Makefile +++ b/lightningd/onchain/Makefile @@ -45,6 +45,16 @@ LIGHTNINGD_HEADERS_NOGEN += $(LIGHTNINGD_ONCHAIN_HEADERS_NOGEN) $(LIGHTNINGD_ONCHAIN_OBJS): $(LIGHTNINGD_HEADERS) +# Common source we use. +ONCHAIND_COMMON_OBJS := \ + common/derive_basepoints.o \ + common/initial_commit_tx.o \ + common/htlc_tx.o \ + common/permute_tx.o \ + common/type_to_string.o \ + common/utils.o \ + common/version.o + lightningd/onchain/gen_onchain_wire.h: $(WIRE_GEN) lightningd/onchain/onchain_wire.csv $(WIRE_GEN) --header $@ onchain_wire_type < lightningd/onchain/onchain_wire.csv > $@ @@ -53,7 +63,7 @@ lightningd/onchain/gen_onchain_wire.c: $(WIRE_GEN) lightningd/onchain/onchain_wi LIGHTNINGD_ONCHAIN_OBJS := $(LIGHTNINGD_ONCHAIN_SRC:.c=.o) $(LIGHTNINGD_ONCHAIN_GEN_SRC:.c=.o) -lightningd/lightningd_onchain: $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_ONCHAIN_OBJS) $(WIRE_ONION_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CLIENT_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a +lightningd/lightningd_onchain: $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_ONCHAIN_OBJS) $(WIRE_ONION_OBJS) $(ONCHAIND_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CLIENT_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) check-source: $(LIGHTNINGD_ONCHAIN_SRC_NOGEN:%=check-src-include-order/%) diff --git a/lightningd/onchain/onchain.c b/lightningd/onchain/onchain.c index cd7bcbbde..e469d9388 100644 --- a/lightningd/onchain/onchain.c +++ b/lightningd/onchain/onchain.c @@ -3,12 +3,15 @@ #include #include #include +#include +#include +#include +#include +#include +#include #include #include -#include #include -#include -#include #include #include #include @@ -18,9 +21,6 @@ #include #include #include -#include -#include -#include #include #include "gen_onchain_types_names.h" diff --git a/lightningd/opening/Makefile b/lightningd/opening/Makefile index 44b28b38c..3e1f12325 100644 --- a/lightningd/opening/Makefile +++ b/lightningd/opening/Makefile @@ -33,6 +33,17 @@ LIGHTNINGD_OPENING_SRC_NOGEN := $(filter-out lightningd/opening/gen_%, $(LIGHTNI LIGHTNINGD_HEADERS_GEN += $(LIGHTNINGD_OPENING_HEADERS_GEN) LIGHTNINGD_HEADERS_NOGEN += $(LIGHTNINGD_OPENING_HEADERS_NOGEN) +# Common source we use. +OPENINGD_COMMON_OBJS := \ + common/derive_basepoints.o \ + common/funding_tx.o \ + common/initial_channel.o \ + common/initial_commit_tx.o \ + common/permute_tx.o \ + common/type_to_string.o \ + common/utils.o \ + common/version.o + $(LIGHTNINGD_OPENING_OBJS): $(LIGHTNINGD_HEADERS) lightningd/opening/gen_opening_wire.h: $(WIRE_GEN) lightningd/opening/opening_wire.csv @@ -43,7 +54,7 @@ lightningd/opening/gen_opening_wire.c: $(WIRE_GEN) lightningd/opening/opening_wi LIGHTNINGD_OPENING_OBJS := $(LIGHTNINGD_OPENING_SRC:.c=.o) $(LIGHTNINGD_OPENING_GEN_SRC:.c=.o) -lightningd/lightningd_opening: $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_OPENING_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CLIENT_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a +lightningd/lightningd_opening: $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_OPENING_OBJS) $(OPENINGD_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS) $(CCAN_SHACHAIN48_OBJ) $(LIGHTNINGD_HSM_CLIENT_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) check-source: $(LIGHTNINGD_OPENING_SRC_NOGEN:%=check-src-include-order/%) diff --git a/lightningd/opening/opening.c b/lightningd/opening/opening.c index 09ddc8626..9c7904963 100644 --- a/lightningd/opening/opening.c +++ b/lightningd/opening/opening.c @@ -5,14 +5,15 @@ #include #include #include +#include +#include +#include +#include +#include #include #include -#include -#include #include #include -#include -#include #include #include #include @@ -21,8 +22,6 @@ #include #include #include -#include -#include #include #include #include @@ -211,12 +210,12 @@ static u8 *funder_channel(struct state *state, { struct channel_id channel_id, id_in; u8 *msg; - struct bitcoin_tx **txs; + struct bitcoin_tx *tx; struct basepoints theirs; struct pubkey their_funding_pubkey, changekey; secp256k1_ecdsa_signature sig; u32 minimum_depth; - const u8 **wscripts; + const u8 *wscript; struct bitcoin_tx *funding; const struct utxo **utxomap; @@ -335,19 +334,19 @@ static u8 *funder_channel(struct state *state, bip32_base); bitcoin_txid(funding, &state->funding_txid); - state->channel = new_channel(state, - &state->funding_txid, - state->funding_txout, - state->funding_satoshis, - state->funding_satoshis * 1000 - - state->push_msat, - state->feerate_per_kw, - &state->localconf, - state->remoteconf, - ours, &theirs, - our_funding_pubkey, - &their_funding_pubkey, - LOCAL); + state->channel = new_initial_channel(state, + &state->funding_txid, + state->funding_txout, + state->funding_satoshis, + state->funding_satoshis * 1000 + - state->push_msat, + state->feerate_per_kw, + &state->localconf, + state->remoteconf, + ours, &theirs, + our_funding_pubkey, + &their_funding_pubkey, + LOCAL); if (!state->channel) peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_BAD_PARAM, "could not create channel with given config"); @@ -360,15 +359,15 @@ static u8 *funder_channel(struct state *state, * for the initial commitment transactions. After receiving the * peer's signature, it will broadcast the funding transaction. */ - txs = channel_txs(state, NULL, &wscripts, state->channel, - &state->next_per_commit[REMOTE], 0, REMOTE); + tx = initial_channel_tx(state, &wscript, state->channel, + &state->next_per_commit[REMOTE], REMOTE); - sign_tx_input(txs[0], 0, NULL, wscripts[0], + sign_tx_input(tx, 0, NULL, wscript, &state->our_secrets.funding_privkey, our_funding_pubkey, &sig); status_trace("signature %s on tx %s using key %s", type_to_string(trc, secp256k1_ecdsa_signature, &sig), - type_to_string(trc, struct bitcoin_tx, txs[0]), + type_to_string(trc, struct bitcoin_tx, tx), type_to_string(trc, struct pubkey, our_funding_pubkey)); msg = towire_funding_created(state, &channel_id, @@ -418,16 +417,15 @@ static u8 *funder_channel(struct state *state, * * The recipient MUST fail the channel if `signature` is incorrect. */ - txs = channel_txs(state, NULL, &wscripts, state->channel, - &state->next_per_commit[LOCAL], 0, LOCAL); + tx = initial_channel_tx(state, &wscript, state->channel, + &state->next_per_commit[LOCAL], LOCAL); - if (!check_tx_sig(txs[0], 0, NULL, wscripts[0], &their_funding_pubkey, - &sig)) { + if (!check_tx_sig(tx, 0, NULL, wscript, &their_funding_pubkey, &sig)) { peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_READ_FAILED, "Bad signature %s on tx %s using key %s", type_to_string(trc, secp256k1_ecdsa_signature, &sig), - type_to_string(trc, struct bitcoin_tx, txs[0]), + type_to_string(trc, struct bitcoin_tx, tx), type_to_string(trc, struct pubkey, &their_funding_pubkey)); } @@ -439,7 +437,7 @@ static u8 *funder_channel(struct state *state, */ return towire_opening_funder_reply(state, state->remoteconf, - txs[0], + tx, &sig, &state->cs, &theirs.revocation, @@ -464,10 +462,10 @@ static u8 *fundee_channel(struct state *state, struct basepoints theirs; struct pubkey their_funding_pubkey; secp256k1_ecdsa_signature theirsig, sig; - struct bitcoin_tx **txs; + struct bitcoin_tx *tx; struct sha256_double chain_hash; u8 *msg; - const u8 **wscripts; + const u8 *wscript; u8 channel_flags; state->remoteconf = tal(state, struct channel_config); @@ -592,18 +590,18 @@ static u8 *fundee_channel(struct state *state, type_to_string(msg, struct channel_id, &channel_id), type_to_string(msg, struct channel_id, &id_in)); - state->channel = new_channel(state, - &state->funding_txid, - state->funding_txout, - state->funding_satoshis, - state->push_msat, - state->feerate_per_kw, - &state->localconf, - state->remoteconf, - ours, &theirs, - our_funding_pubkey, - &their_funding_pubkey, - REMOTE); + state->channel = new_initial_channel(state, + &state->funding_txid, + state->funding_txout, + state->funding_satoshis, + state->push_msat, + state->feerate_per_kw, + &state->localconf, + state->remoteconf, + ours, &theirs, + our_funding_pubkey, + &their_funding_pubkey, + REMOTE); if (!state->channel) peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_BAD_PARAM, "could not create channel with given config"); @@ -612,16 +610,16 @@ static u8 *fundee_channel(struct state *state, * * The recipient MUST fail the channel if `signature` is incorrect. */ - txs = channel_txs(state, NULL, &wscripts, state->channel, - &state->next_per_commit[LOCAL], 0, LOCAL); + tx = initial_channel_tx(state, &wscript, state->channel, + &state->next_per_commit[LOCAL], LOCAL); - if (!check_tx_sig(txs[0], 0, NULL, wscripts[0], &their_funding_pubkey, + if (!check_tx_sig(tx, 0, NULL, wscript, &their_funding_pubkey, &theirsig)) { peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_READ_FAILED, "Bad signature %s on tx %s using key %s", type_to_string(trc, secp256k1_ecdsa_signature, &theirsig), - type_to_string(trc, struct bitcoin_tx, txs[0]), + type_to_string(trc, struct bitcoin_tx, tx), type_to_string(trc, struct pubkey, &their_funding_pubkey)); } @@ -645,9 +643,9 @@ static u8 *fundee_channel(struct state *state, * commitment transaction, so they can broadcast it knowing they can * redeem their funds if they need to. */ - txs = channel_txs(state, NULL, &wscripts, state->channel, - &state->next_per_commit[REMOTE], 0, REMOTE); - sign_tx_input(txs[0], 0, NULL, wscripts[0], + tx = initial_channel_tx(state, &wscript, state->channel, + &state->next_per_commit[REMOTE], REMOTE); + sign_tx_input(tx, 0, NULL, wscript, &state->our_secrets.funding_privkey, our_funding_pubkey, &sig); @@ -657,7 +655,7 @@ static u8 *fundee_channel(struct state *state, return towire_opening_fundee_reply(state, state->remoteconf, - txs[0], + tx, &theirsig, &state->cs, &theirs.revocation, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index ea55c707d..1a5ff05f0 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -8,7 +8,9 @@ #include #include #include -#include +#include +#include +#include #include #include #include @@ -18,12 +20,9 @@ #include #include #include -#include #include #include -#include #include -#include #include #include #include @@ -36,7 +35,6 @@ #include #include #include -#include #include #include #include diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 84fd5beb9..964a8712c 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -3,11 +3,12 @@ #include #include #include +#include +#include #include #include #include #include -#include #include #include #include @@ -18,7 +19,6 @@ #include #include #include -#include #include static bool state_update_ok(struct peer *peer, diff --git a/lightningd/peer_htlcs.h b/lightningd/peer_htlcs.h index 6116d1bb4..83d527234 100644 --- a/lightningd/peer_htlcs.h +++ b/lightningd/peer_htlcs.h @@ -3,7 +3,7 @@ #define LIGHTNING_LIGHTNINGD_PEER_HTLCS_H #include "config.h" #include -#include +#include #include /* FIXME: Define serialization primitive for this? */ diff --git a/lightningd/sphinx.c b/lightningd/sphinx.c index 087f013dc..66dbdabb4 100644 --- a/lightningd/sphinx.c +++ b/lightningd/sphinx.c @@ -1,10 +1,10 @@ #include "lightningd/sphinx.h" -#include "utils.h" #include #include #include #include +#include #include diff --git a/lightningd/status.c b/lightningd/status.c index a4ccbe446..81017dade 100644 --- a/lightningd/status.c +++ b/lightningd/status.c @@ -1,6 +1,3 @@ -#include "utils.h" -#include "wire/wire.h" -#include "wire/wire_sync.h" #include #include #include @@ -9,9 +6,12 @@ #include #include #include +#include #include #include #include +#include +#include static int status_fd = -1; static struct daemon_conn *status_conn; diff --git a/test/test_protocol.c b/test/test_protocol.c index 02ab4568d..5c302e362 100644 --- a/test/test_protocol.c +++ b/test/test_protocol.c @@ -1,6 +1,5 @@ /* Simple simulator for protocol. */ #include "config.h" -#include "utils.h" #include #include #include @@ -11,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/test/test_sphinx.c b/test/test_sphinx.c index 6460d8ebf..a07ef8b46 100644 --- a/test/test_sphinx.c +++ b/test/test_sphinx.c @@ -4,13 +4,13 @@ #include #include #include +#include #include #include #include #include #include "lightningd/sphinx.h" -#include "utils.h" secp256k1_context *secp256k1_ctx; diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 4b0d9dbd9..f3be91cb8 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -13,8 +14,6 @@ #include #include #include -#include -#include #include #include diff --git a/wire/fromwire.c b/wire/fromwire.c index 854ab8fd4..d376bd3ef 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -1,4 +1,3 @@ -#include "utils.h" #include "wire.h" #include #include @@ -7,7 +6,8 @@ #include #include #include -#include +#include +#include /* Sets *cursor to NULL and returns NULL when extraction fails. */ const void *fromwire_fail(const u8 **cursor, size_t *max) diff --git a/wire/towire.c b/wire/towire.c index 830bda3d5..eec8fbae2 100644 --- a/wire/towire.c +++ b/wire/towire.c @@ -1,4 +1,3 @@ -#include "utils.h" #include "wire.h" #include #include @@ -6,6 +5,7 @@ #include #include #include +#include void towire(u8 **pptr, const void *data, size_t len) {