Browse Source
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
80 changed files with 971 additions and 609 deletions
@ -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,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); |
@ -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 */ |
@ -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; |
||||
|
} |
@ -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 */ |
Loading…
Reference in new issue