Browse Source

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 <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
85ff95e829
  1. 39
      Makefile
  2. 5
      bitcoin/Makefile
  3. 2
      bitcoin/base58.c
  4. 4
      bitcoin/locktime.c
  5. 4
      bitcoin/pubkey.c
  6. 2
      bitcoin/shadouble.c
  7. 4
      bitcoin/signature.c
  8. 8
      bitcoin/tx.c
  9. 26
      common/Makefile
  10. 0
      common/close_tx.c
  11. 0
      common/close_tx.h
  12. 4
      common/derive_basepoints.c
  13. 0
      common/derive_basepoints.h
  14. 2
      common/funding_tx.c
  15. 0
      common/funding_tx.h
  16. 26
      common/htlc_tx.c
  17. 39
      common/htlc_tx.h
  18. 123
      common/initial_channel.c
  19. 172
      common/initial_channel.h
  20. 200
      common/initial_commit_tx.c
  21. 96
      common/initial_commit_tx.h
  22. 0
      common/overflows.h
  23. 0
      common/permute_tx.c
  24. 0
      common/permute_tx.h
  25. 0
      common/type_to_string.c
  26. 0
      common/type_to_string.h
  27. 0
      common/utils.c
  28. 0
      common/utils.h
  29. 0
      common/version.c
  30. 0
      common/version.h
  31. 2
      common/withdraw_tx.c
  32. 0
      common/withdraw_tx.h
  33. 2
      daemon/bitcoind.c
  34. 2
      daemon/chaintopology.c
  35. 10
      daemon/htlc_state.c
  36. 2
      daemon/invoice.c
  37. 2
      daemon/jsonrpc.c
  38. 2
      daemon/log.c
  39. 2
      daemon/log.h
  40. 4
      daemon/netaddr.c
  41. 2
      daemon/options.c
  42. 2
      daemon/routing.c
  43. 2
      daemon/timeout.c
  44. 23
      lightningd/Makefile
  45. 2
      lightningd/build_utxos.c
  46. 19
      lightningd/channel/Makefile
  47. 16
      lightningd/channel/channel.c
  48. 136
      lightningd/channel/commit_tx.c
  49. 41
      lightningd/channel/commit_tx.h
  50. 161
      lightningd/channel/full_channel.c
  51. 135
      lightningd/channel/full_channel.h
  52. 11
      lightningd/closing/Makefile
  53. 10
      lightningd/closing/closing.c
  54. 2
      lightningd/crypto_sync.c
  55. 2
      lightningd/cryptomsg.c
  56. 2
      lightningd/dev_ping.c
  57. 8
      lightningd/gossip/Makefile
  58. 4
      lightningd/gossip/gossip.c
  59. 2
      lightningd/gossip_control.c
  60. 8
      lightningd/handshake/Makefile
  61. 4
      lightningd/handshake/handshake.c
  62. 12
      lightningd/hsm/Makefile
  63. 9
      lightningd/hsm/hsm.c
  64. 1
      lightningd/hsm_control.c
  65. 2
      lightningd/key_derive.c
  66. 4
      lightningd/lightningd.c
  67. 12
      lightningd/onchain/Makefile
  68. 12
      lightningd/onchain/onchain.c
  69. 13
      lightningd/opening/Makefile
  70. 104
      lightningd/opening/opening.c
  71. 8
      lightningd/peer_control.c
  72. 4
      lightningd/peer_htlcs.c
  73. 2
      lightningd/peer_htlcs.h
  74. 2
      lightningd/sphinx.c
  75. 6
      lightningd/status.c
  76. 2
      test/test_protocol.c
  77. 2
      test/test_sphinx.c
  78. 3
      wallet/walletrpc.c
  79. 4
      wire/fromwire.c
  80. 2
      wire/towire.c

39
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

5
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/%)

2
bitcoin/base58.c

@ -9,10 +9,10 @@
#include "privkey.h"
#include "pubkey.h"
#include "shadouble.h"
#include "utils.h"
#include <assert.h>
#include <ccan/build_assert/build_assert.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <secp256k1.h>
#include <string.h>

4
bitcoin/locktime.c

@ -1,7 +1,7 @@
#include "bitcoin/locktime.h"
#include "type_to_string.h"
#include <assert.h>
#include <bitcoin/locktime.h>
#include <ccan/tal/str/str.h>
#include <common/type_to_string.h>
#define SECONDS_POINT 500000000

4
bitcoin/pubkey.c

@ -1,11 +1,11 @@
#include "privkey.h"
#include "pubkey.h"
#include "type_to_string.h"
#include "utils.h"
#include <assert.h>
#include <ccan/mem/mem.h>
#include <ccan/str/hex/hex.h>
#include <ccan/structeq/structeq.h>
#include <common/type_to_string.h>
#include <common/utils.h>
bool pubkey_from_der(const u8 *der, size_t len, struct pubkey *key)
{

2
bitcoin/shadouble.c

@ -1,6 +1,6 @@
#include "shadouble.h"
#include "type_to_string.h"
#include <ccan/mem/mem.h>
#include <common/type_to_string.h>
void sha256_double(struct sha256_double *shadouble, const void *p, size_t len)
{

4
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 <assert.h>
#include <ccan/cast/cast.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#undef DEBUG
#ifdef DEBUG

8
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 <assert.h>
#include <bitcoin/block.h>
#include <bitcoin/pullpush.h>
#include <bitcoin/tx.h>
#include <ccan/cast/cast.h>
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/endian/endian.h>
#include <ccan/mem/mem.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/str/hex/hex.h>
#include <common/type_to_string.h>
#include <stdio.h>
#define SEGREGATED_WITNESS_FLAG 0x1

26
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/%)

0
close_tx.c → common/close_tx.c

0
close_tx.h → common/close_tx.h

4
lightningd/derive_basepoints.c → common/derive_basepoints.c

@ -2,8 +2,8 @@
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/crypto/shachain/shachain.h>
#include <lightningd/derive_basepoints.h>
#include <utils.h>
#include <common/derive_basepoints.h>
#include <common/utils.h>
bool derive_basepoints(const struct privkey *seed,
struct pubkey *funding_pubkey,

0
lightningd/derive_basepoints.h → common/derive_basepoints.h

2
lightningd/funding_tx.c → common/funding_tx.c

@ -4,9 +4,9 @@
#include <bitcoin/script.h>
#include <bitcoin/tx.h>
#include <ccan/ptrint/ptrint.h>
#include <common/permute_tx.h>
#include <lightningd/key_derive.h>
#include <lightningd/utxo.h>
#include <permute_tx.h>
#ifndef SUPERVERBOSE
#define SUPERVERBOSE(...)

0
lightningd/funding_tx.h → common/funding_tx.h

26
lightningd/htlc_tx.c → common/htlc_tx.c

@ -1,8 +1,7 @@
#include <bitcoin/preimage.h>
#include <bitcoin/script.h>
#include <bitcoin/tx.h>
#include <lightningd/commit_tx.h>
#include <lightningd/htlc_tx.h>
#include <common/htlc_tx.h>
#include <lightningd/keyset.h>
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);
}

39
lightningd/htlc_tx.h → common/htlc_tx.h

@ -3,10 +3,35 @@
#include "config.h"
#include <daemon/htlc.h>
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 */

123
common/initial_channel.c

@ -0,0 +1,123 @@
#include <assert.h>
#include <bitcoin/script.h>
#include <ccan/tal/str/str.h>
#include <common/initial_channel.h>
#include <common/initial_commit_tx.h>
#include <common/type_to_string.h>
#include <inttypes.h>
#include <lightningd/keyset.h>
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);

172
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 <bitcoin/pubkey.h>
#include <bitcoin/shadouble.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <common/derive_basepoints.h>
#include <daemon/htlc.h>
#include <lightningd/channel_config.h>
#include <stdbool.h>
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 */

200
common/initial_commit_tx.c

@ -0,0 +1,200 @@
#include <bitcoin/script.h>
#include <bitcoin/tx.h>
#include <ccan/endian/endian.h>
#include <common/initial_commit_tx.h>
#include <common/permute_tx.h>
#include <common/utils.h>
#include <lightningd/keyset.h>
/* 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;
}

96
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 <bitcoin/pubkey.h>
#include <common/htlc.h>
#include <lightningd/channel/channeld_htlc.h>
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 */

0
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

2
lightningd/withdraw_tx.c → common/withdraw_tx.c

@ -2,9 +2,9 @@
#include <bitcoin/pubkey.h>
#include <bitcoin/script.h>
#include <ccan/ptrint/ptrint.h>
#include <common/permute_tx.h>
#include <lightningd/key_derive.h>
#include <lightningd/utxo.h>
#include <permute_tx.h>
#include <wally_bip32.h>
struct bitcoin_tx *withdraw_tx(const tal_t *ctx,

0
lightningd/withdraw_tx.h → common/withdraw_tx.h

2
daemon/bitcoind.c

@ -7,7 +7,6 @@
#include "json.h"
#include "lightningd.h"
#include "log.h"
#include "utils.h"
#include <ccan/cast/cast.h>
#include <ccan/io/io.h>
#include <ccan/pipecmd/pipecmd.h>
@ -17,6 +16,7 @@
#include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
#include <common/utils.h>
#include <errno.h>
#include <inttypes.h>

2
daemon/chaintopology.c

@ -6,13 +6,13 @@
#include "lightningd.h"
#include "log.h"
#include "timeout.h"
#include "utils.h"
#include "watch.h"
#include <ccan/array_size/array_size.h>
#include <ccan/asort/asort.h>
#include <ccan/io/io.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <inttypes.h>
static void start_poll_chaintip(struct chain_topology *topo);

10
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

2
daemon/invoice.c

@ -2,10 +2,10 @@
#include "invoice.h"
#include "jsonrpc.h"
#include "lightningd.h"
#include "utils.h"
#include <ccan/str/hex/hex.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <sodium/randombytes.h>
struct invoice_waiter {

2
daemon/jsonrpc.c

@ -5,12 +5,12 @@
#include "jsonrpc.h"
#include "lightningd.h"
#include "log.h"
#include "version.h"
#include <ccan/array_size/array_size.h>
#include <ccan/err/err.h>
#include <ccan/io/io.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <common/version.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>

2
daemon/log.c

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

2
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 <ccan/tal/tal.h>
#include <ccan/typesafe_cb/typesafe_cb.h>
#include <common/type_to_string.h>
#include <stdarg.h>
struct timerel;

4
daemon/netaddr.c

@ -1,7 +1,5 @@
#include "bitcoin/pullpush.h"
#include "netaddr.h"
#include "type_to_string.h"
#include "utils.h"
#include <arpa/inet.h>
#include <assert.h>
#include <ccan/cast/cast.h>
@ -9,6 +7,8 @@
#include <ccan/short_types/short_types.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>

2
daemon/options.c

@ -8,13 +8,13 @@
#include "daemon/options.h"
#include "daemon/routing.h"
#include "lightningd/lightningd.h"
#include "version.h"
#include <arpa/inet.h>
#include <ccan/err/err.h>
#include <ccan/opt/opt.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/str/str.h>
#include <common/version.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>

2
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 <ccan/endian/endian.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/str/str.h>
#include <common/overflows.h>
#include <inttypes.h>
/* 365.25 * 24 * 60 / 10 */

2
daemon/timeout.c

@ -1,6 +1,6 @@
#include "lightningd.h"
#include "timeout.h"
#include "utils.h"
#include <common/utils.h>
struct oneshot {
struct timers *timers;

23
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

2
lightningd/build_utxos.c

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

19
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/%)

16
lightningd/channel/channel.c

@ -11,20 +11,22 @@
#include <ccan/take/take.h>
#include <ccan/tal/str/str.h>
#include <ccan/time/time.h>
#include <common/derive_basepoints.h>
#include <common/htlc_tx.h>
#include <common/type_to_string.h>
#include <common/version.h>
#include <daemon/routing.h>
#include <daemon/timeout.h>
#include <errno.h>
#include <inttypes.h>
#include <lightningd/channel.h>
#include <lightningd/channel/commit_tx.h>
#include <lightningd/channel/full_channel.h>
#include <lightningd/channel/gen_channel_wire.h>
#include <lightningd/commit_tx.h>
#include <lightningd/crypto_sync.h>
#include <lightningd/cryptomsg.h>
#include <lightningd/daemon_conn.h>
#include <lightningd/debug.h>
#include <lightningd/derive_basepoints.h>
#include <lightningd/hsm/gen_hsm_client_wire.h>
#include <lightningd/htlc_tx.h>
#include <lightningd/key_derive.h>
#include <lightningd/msg_queue.h>
#include <lightningd/peer_failed.h>
@ -34,8 +36,6 @@
#include <secp256k1.h>
#include <signal.h>
#include <stdio.h>
#include <type_to_string.h>
#include <version.h>
#include <wire/gen_onion_wire.h>
#include <wire/peer_wire.h>
#include <wire/wire.h>
@ -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);
}

136
lightningd/commit_tx.c → lightningd/channel/commit_tx.c

@ -1,78 +1,16 @@
#include <bitcoin/script.h>
#include <bitcoin/tx.h>
#include <ccan/endian/endian.h>
#include <lightningd/commit_tx.h>
#include <common/htlc_tx.h>
#include <common/permute_tx.h>
#include <common/utils.h>
#include <lightningd/channel/commit_tx.h>
#include <lightningd/keyset.h>
#include <permute_tx.h>
#include <utils.h>
#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
{

41
lightningd/commit_tx.h → lightningd/channel/commit_tx.h

@ -2,26 +2,13 @@
#define LIGHTNING_LIGHTNINGD_COMMIT_TX_H
#include "config.h"
#include <bitcoin/pubkey.h>
#include <common/initial_commit_tx.h>
#include <daemon/htlc.h>
#include <lightningd/channel/channeld_htlc.h>
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 */

161
lightningd/channel.c → lightningd/channel/full_channel.c

@ -1,6 +1,3 @@
#include "channel.h"
#include "commit_tx.h"
#include "type_to_string.h"
#include <assert.h>
#include <bitcoin/preimage.h>
#include <bitcoin/script.h>
@ -9,16 +6,52 @@
#include <ccan/mem/mem.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/str/str.h>
#include <common/htlc_tx.h>
#include <common/type_to_string.h>
#include <daemon/htlc.h>
#include <inttypes.h>
#include <lightningd/channel/commit_tx.h>
#include <lightningd/channel/full_channel.h>
#include <lightningd/channel_config.h>
#include <lightningd/htlc_tx.h>
#include <lightningd/htlc_wire.h>
#include <lightningd/key_derive.h>
#include <lightningd/keyset.h>
#include <lightningd/status.h>
#include <string.h>
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);

135
lightningd/channel.h → 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 <bitcoin/pubkey.h>
#include <bitcoin/shadouble.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <daemon/htlc.h>
#include <common/initial_channel.h>
#include <lightningd/channel/channeld_htlc.h>
#include <lightningd/channel_config.h>
#include <lightningd/derive_basepoints.h>
#include <lightningd/sphinx.h>
#include <stdbool.h>
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

11
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/%)

10
lightningd/closing/closing.c

@ -1,19 +1,19 @@
#include <bitcoin/script.h>
#include <close_tx.h>
#include <common/close_tx.h>
#include <common/derive_basepoints.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <common/version.h>
#include <daemon/htlc.h>
#include <errno.h>
#include <inttypes.h>
#include <lightningd/closing/gen_closing_wire.h>
#include <lightningd/crypto_sync.h>
#include <lightningd/debug.h>
#include <lightningd/derive_basepoints.h>
#include <lightningd/status.h>
#include <lightningd/subd.h>
#include <signal.h>
#include <stdio.h>
#include <type_to_string.h>
#include <utils.h>
#include <version.h>
#include <wire/peer_wire.h>
#include <wire/wire_sync.h>

2
lightningd/crypto_sync.c

@ -1,11 +1,11 @@
#include <ccan/read_write_all/read_write_all.h>
#include <common/utils.h>
#include <errno.h>
#include <inttypes.h>
#include <lightningd/crypto_sync.h>
#include <lightningd/cryptomsg.h>
#include <lightningd/dev_disconnect.h>
#include <lightningd/status.h>
#include <utils.h>
#include <wire/wire.h>
#include <wire/wire_sync.h>

2
lightningd/cryptomsg.c

@ -6,11 +6,11 @@
#include <ccan/mem/mem.h>
#include <ccan/short_types/short_types.h>
#include <ccan/take/take.h>
#include <common/utils.h>
#include <lightningd/cryptomsg.h>
#include <lightningd/dev_disconnect.h>
#include <lightningd/status.h>
#include <sodium/crypto_aead_chacha20poly1305.h>
#include <utils.h>
#include <wire/peer_wire.h>
#include <wire/wire.h>
#include <wire/wire_io.h>

2
lightningd/dev_ping.c

@ -1,3 +1,4 @@
#include <common/utils.h>
#include <daemon/jsonrpc.h>
#include <daemon/log.h>
#include <lightningd/channel/gen_channel_wire.h>
@ -7,7 +8,6 @@
#include <lightningd/peer_control.h>
#include <lightningd/sphinx.h>
#include <lightningd/subd.h>
#include <utils.h>
static bool ping_reply(struct subd *subd, const u8 *msg, const int *fds,
struct command *cmd)

8
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

4
lightningd/gossip/gossip.c

@ -9,6 +9,8 @@
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/take/take.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <common/version.h>
#include <daemon/broadcast.h>
#include <daemon/log.h>
#include <daemon/routing.h>
@ -29,8 +31,6 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <utils.h>
#include <version.h>
#include <wire/gen_peer_wire.h>
#include <wire/wire_io.h>

2
lightningd/gossip_control.c

@ -5,6 +5,8 @@
#include <ccan/err/err.h>
#include <ccan/take/take.h>
#include <ccan/tal/str/str.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <daemon/jsonrpc.h>
#include <daemon/log.h>
#include <inttypes.h>

8
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/%)

4
lightningd/handshake/handshake.c

@ -1,4 +1,3 @@
#include "type_to_string.h"
#include <assert.h>
#include <bitcoin/privkey.h>
#include <ccan/build_assert/build_assert.h>
@ -8,6 +7,8 @@
#include <ccan/mem/mem.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/short_types/short_types.h>
#include <common/type_to_string.h>
#include <common/version.h>
#include <errno.h>
#include <lightningd/crypto_sync.h>
#include <lightningd/debug.h>
@ -20,7 +21,6 @@
#include <sodium/randombytes.h>
#include <stdio.h>
#include <unistd.h>
#include <version.h>
#include <wire/peer_wire.h>
#include <wire/wire.h>
#include <wire/wire_sync.h>

12
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

9
lightningd/hsm/hsm.c

@ -14,26 +14,25 @@
#include <ccan/ptrint/ptrint.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/take/take.h>
#include <common/funding_tx.h>
#include <common/utils.h>
#include <common/version.h>
#include <common/withdraw_tx.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <lightningd/build_utxos.h>
#include <lightningd/daemon_conn.h>
#include <lightningd/funding_tx.h>
#include <lightningd/hsm/client.h>
#include <lightningd/hsm/gen_hsm_client_wire.h>
#include <lightningd/hsm/gen_hsm_wire.h>
#include <lightningd/status.h>
#include <lightningd/withdraw_tx.h>
#include <permute_tx.h>
#include <secp256k1_ecdh.h>
#include <sodium/randombytes.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <utils.h>
#include <version.h>
#include <wally_bip32.h>
#include <wire/gen_peer_wire.h>
#include <wire/wire_io.h>

1
lightningd/hsm_control.c

@ -5,6 +5,7 @@
#include <ccan/err/err.h>
#include <ccan/io/io.h>
#include <ccan/take/take.h>
#include <common/utils.h>
#include <daemon/log.h>
#include <errno.h>
#include <inttypes.h>

2
lightningd/key_derive.c

@ -1,8 +1,8 @@
#include <bitcoin/privkey.h>
#include <bitcoin/pubkey.h>
#include <ccan/crypto/sha256/sha256.h>
#include <common/utils.h>
#include <lightningd/key_derive.h>
#include <utils.h>
#include <wally_bip32.h>
/* BOLT #3:

4
lightningd/lightningd.c

@ -13,6 +13,8 @@
#include <ccan/take/take.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
#include <common/utils.h>
#include <common/version.h>
#include <daemon/bitcoind.h>
#include <daemon/chaintopology.h>
#include <daemon/invoice.h>
@ -24,8 +26,6 @@
#include <lightningd/onchain/onchain_wire.h>
#include <sys/types.h>
#include <unistd.h>
#include <utils.h>
#include <version.h>
char *bitcoin_datadir;

12
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/%)

12
lightningd/onchain/onchain.c

@ -3,12 +3,15 @@
#include <ccan/mem/mem.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/str/str.h>
#include <common/derive_basepoints.h>
#include <common/htlc_tx.h>
#include <common/initial_commit_tx.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <common/version.h>
#include <errno.h>
#include <inttypes.h>
#include <lightningd/commit_tx.h>
#include <lightningd/debug.h>
#include <lightningd/derive_basepoints.h>
#include <lightningd/htlc_tx.h>
#include <lightningd/key_derive.h>
#include <lightningd/keyset.h>
#include <lightningd/onchain/gen_onchain_wire.h>
@ -18,9 +21,6 @@
#include <lightningd/subd.h>
#include <signal.h>
#include <stdio.h>
#include <type_to_string.h>
#include <utils.h>
#include <version.h>
#include <wire/wire_sync.h>
#include "gen_onchain_types_names.h"

13
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/%)

104
lightningd/opening/opening.c

@ -5,14 +5,15 @@
#include <ccan/breakpoint/breakpoint.h>
#include <ccan/fdpass/fdpass.h>
#include <ccan/structeq/structeq.h>
#include <common/derive_basepoints.h>
#include <common/funding_tx.h>
#include <common/initial_channel.h>
#include <common/type_to_string.h>
#include <common/version.h>
#include <errno.h>
#include <inttypes.h>
#include <lightningd/channel.h>
#include <lightningd/commit_tx.h>
#include <lightningd/crypto_sync.h>
#include <lightningd/debug.h>
#include <lightningd/derive_basepoints.h>
#include <lightningd/funding_tx.h>
#include <lightningd/key_derive.h>
#include <lightningd/opening/gen_opening_wire.h>
#include <lightningd/peer_failed.h>
@ -21,8 +22,6 @@
#include <secp256k1.h>
#include <signal.h>
#include <stdio.h>
#include <type_to_string.h>
#include <version.h>
#include <wally_bip32.h>
#include <wire/gen_peer_wire.h>
#include <wire/peer_wire.h>
@ -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,

8
lightningd/peer_control.c

@ -8,7 +8,9 @@
#include <ccan/noerr/noerr.h>
#include <ccan/take/take.h>
#include <ccan/tal/str/str.h>
#include <close_tx.h>
#include <common/close_tx.h>
#include <common/funding_tx.h>
#include <common/initial_commit_tx.h>
#include <daemon/chaintopology.h>
#include <daemon/dns.h>
#include <daemon/jsonrpc.h>
@ -18,12 +20,9 @@
#include <fcntl.h>
#include <inttypes.h>
#include <lightningd/build_utxos.h>
#include <lightningd/channel.h>
#include <lightningd/channel/gen_channel_wire.h>
#include <lightningd/closing/gen_closing_wire.h>
#include <lightningd/commit_tx.h>
#include <lightningd/dev_disconnect.h>
#include <lightningd/funding_tx.h>
#include <lightningd/gen_peer_state_names.h>
#include <lightningd/gossip/gen_gossip_wire.h>
#include <lightningd/hsm/gen_hsm_wire.h>
@ -36,7 +35,6 @@
#include <lightningd/peer_htlcs.h>
#include <lightningd/status.h>
#include <netinet/in.h>
#include <overflows.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

4
lightningd/peer_htlcs.c

@ -3,11 +3,12 @@
#include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <common/derive_basepoints.h>
#include <common/overflows.h>
#include <daemon/chaintopology.h>
#include <daemon/invoice.h>
#include <daemon/log.h>
#include <lightningd/channel/gen_channel_wire.h>
#include <lightningd/derive_basepoints.h>
#include <lightningd/gossip/gen_gossip_wire.h>
#include <lightningd/htlc_end.h>
#include <lightningd/htlc_wire.h>
@ -18,7 +19,6 @@
#include <lightningd/peer_htlcs.h>
#include <lightningd/sphinx.h>
#include <lightningd/subd.h>
#include <overflows.h>
#include <wire/gen_onion_wire.h>
static bool state_update_ok(struct peer *peer,

2
lightningd/peer_htlcs.h

@ -3,7 +3,7 @@
#define LIGHTNING_LIGHTNINGD_PEER_HTLCS_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <lightningd/derive_basepoints.h>
#include <common/derive_basepoints.h>
#include <lightningd/htlc_wire.h>
/* FIXME: Define serialization primitive for this? */

2
lightningd/sphinx.c

@ -1,10 +1,10 @@
#include "lightningd/sphinx.h"
#include "utils.h"
#include <assert.h>
#include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/mem/mem.h>
#include <common/utils.h>
#include <err.h>

6
lightningd/status.c

@ -1,6 +1,3 @@
#include "utils.h"
#include "wire/wire.h"
#include "wire/wire_sync.h"
#include <assert.h>
#include <ccan/breakpoint/breakpoint.h>
#include <ccan/endian/endian.h>
@ -9,9 +6,12 @@
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/take/take.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <lightningd/daemon_conn.h>
#include <lightningd/status.h>
#include <stdarg.h>
#include <wire/wire.h>
#include <wire/wire_sync.h>
static int status_fd = -1;
static struct daemon_conn *status_conn;

2
test/test_protocol.c

@ -1,6 +1,5 @@
/* Simple simulator for protocol. */
#include "config.h"
#include "utils.h"
#include <assert.h>
#include <ccan/array_size/array_size.h>
#include <ccan/err/err.h>
@ -11,6 +10,7 @@
#include <ccan/structeq/structeq.h>
#include <ccan/tal/tal.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <inttypes.h>
#include <signal.h>
#include <stdbool.h>

2
test/test_sphinx.c

@ -4,13 +4,13 @@
#include <string.h>
#include <ccan/str/hex/hex.h>
#include <ccan/read_write_all/read_write_all.h>
#include <common/utils.h>
#include <err.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include "lightningd/sphinx.h"
#include "utils.h"
secp256k1_context *secp256k1_ctx;

3
wallet/walletrpc.c

@ -2,6 +2,7 @@
#include <bitcoin/base58.h>
#include <bitcoin/script.h>
#include <ccan/tal/str/str.h>
#include <common/withdraw_tx.h>
#include <daemon/bitcoind.h>
#include <daemon/chaintopology.h>
#include <daemon/jsonrpc.h>
@ -13,8 +14,6 @@
#include <lightningd/status.h>
#include <lightningd/subd.h>
#include <lightningd/utxo.h>
#include <lightningd/withdraw_tx.h>
#include <permute_tx.h>
#include <wally_bip32.h>
#include <wire/wire_sync.h>

4
wire/fromwire.c

@ -1,4 +1,3 @@
#include "utils.h"
#include "wire.h"
#include <bitcoin/preimage.h>
#include <bitcoin/pubkey.h>
@ -7,7 +6,8 @@
#include <ccan/endian/endian.h>
#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <type_to_string.h>
#include <common/type_to_string.h>
#include <common/utils.h>
/* Sets *cursor to NULL and returns NULL when extraction fails. */
const void *fromwire_fail(const u8 **cursor, size_t *max)

2
wire/towire.c

@ -1,4 +1,3 @@
#include "utils.h"
#include "wire.h"
#include <bitcoin/preimage.h>
#include <bitcoin/shadouble.h>
@ -6,6 +5,7 @@
#include <ccan/endian/endian.h>
#include <ccan/mem/mem.h>
#include <ccan/tal/tal.h>
#include <common/utils.h>
void towire(u8 **pptr, const void *data, size_t len)
{

Loading…
Cancel
Save