mirror of
https://github.com/aljazceru/lightning.git
synced 2026-02-23 15:04:19 +01:00
channeld: use amount_sat/amount_msat.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -12,8 +12,8 @@ struct channel *new_initial_channel(const tal_t *ctx,
|
||||
const struct bitcoin_blkid *chain_hash,
|
||||
const struct bitcoin_txid *funding_txid,
|
||||
unsigned int funding_txout,
|
||||
u64 funding_satoshis,
|
||||
u64 local_msatoshi,
|
||||
struct amount_sat funding,
|
||||
struct amount_msat local_msatoshi,
|
||||
u32 feerate_per_kw,
|
||||
const struct channel_config *local,
|
||||
const struct channel_config *remote,
|
||||
@@ -24,14 +24,13 @@ struct channel *new_initial_channel(const tal_t *ctx,
|
||||
enum side funder)
|
||||
{
|
||||
struct channel *channel = tal(ctx, struct channel);
|
||||
struct amount_msat remote_msatoshi;
|
||||
|
||||
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)
|
||||
channel->funding = funding;
|
||||
if (!amount_sat_sub_msat(&remote_msatoshi,
|
||||
channel->funding, local_msatoshi))
|
||||
return tal_free(channel);
|
||||
|
||||
channel->funder = funder;
|
||||
@@ -47,12 +46,12 @@ struct channel *new_initial_channel(const tal_t *ctx,
|
||||
= channel->view[REMOTE].feerate_per_kw
|
||||
= feerate_per_kw;
|
||||
|
||||
channel->view[LOCAL].owed_msat[LOCAL]
|
||||
= channel->view[REMOTE].owed_msat[LOCAL]
|
||||
channel->view[LOCAL].owed[LOCAL]
|
||||
= channel->view[REMOTE].owed[LOCAL]
|
||||
= local_msatoshi;
|
||||
channel->view[REMOTE].owed_msat[REMOTE]
|
||||
= channel->view[LOCAL].owed_msat[REMOTE]
|
||||
= channel->funding_msat - local_msatoshi;
|
||||
channel->view[REMOTE].owed[REMOTE]
|
||||
= channel->view[LOCAL].owed[REMOTE]
|
||||
= remote_msatoshi;
|
||||
|
||||
channel->basepoints[LOCAL] = *local_basepoints;
|
||||
channel->basepoints[REMOTE] = *remote_basepoints;
|
||||
@@ -91,16 +90,16 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
|
||||
|
||||
return initial_commit_tx(ctx, &channel->funding_txid,
|
||||
channel->funding_txout,
|
||||
channel->funding_msat / 1000,
|
||||
channel->funding,
|
||||
channel->funder,
|
||||
/* They specify our to_self_delay and v.v. */
|
||||
channel->config[!side].to_self_delay,
|
||||
&keyset,
|
||||
channel->view[side].feerate_per_kw,
|
||||
channel->config[side].dust_limit.satoshis,
|
||||
channel->view[side].owed_msat[side],
|
||||
channel->view[side].owed_msat[!side],
|
||||
channel->config[!side].channel_reserve.satoshis * 1000,
|
||||
channel->config[side].dust_limit,
|
||||
channel->view[side].owed[side],
|
||||
channel->view[side].owed[!side],
|
||||
channel->config[!side].channel_reserve,
|
||||
0 ^ channel->commitment_number_obscurer,
|
||||
side);
|
||||
}
|
||||
@@ -108,21 +107,24 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
|
||||
static char *fmt_channel_view(const tal_t *ctx, const struct channel_view *view)
|
||||
{
|
||||
return tal_fmt(ctx, "{ feerate_per_kw=%"PRIu32","
|
||||
" owed_local=%"PRIu64","
|
||||
" owed_remote=%"PRIu64" }",
|
||||
" owed_local=%s,"
|
||||
" owed_remote=%s }",
|
||||
view->feerate_per_kw,
|
||||
view->owed_msat[LOCAL],
|
||||
view->owed_msat[REMOTE]);
|
||||
type_to_string(tmpctx, struct amount_msat,
|
||||
&view->owed[LOCAL]),
|
||||
type_to_string(tmpctx, struct amount_msat,
|
||||
&view->owed[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","
|
||||
return tal_fmt(ctx, "{ funding=%s,"
|
||||
" funder=%s,"
|
||||
" local=%s,"
|
||||
" remote=%s }",
|
||||
channel->funding_msat,
|
||||
type_to_string(tmpctx, struct amount_sat,
|
||||
&channel->funding),
|
||||
side_to_str(channel->funder),
|
||||
fmt_channel_view(ctx, &channel->view[LOCAL]),
|
||||
fmt_channel_view(ctx, &channel->view[REMOTE]));
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <bitcoin/shadouble.h>
|
||||
#include <ccan/short_types/short_types.h>
|
||||
#include <ccan/tal/tal.h>
|
||||
#include <common/amount.h>
|
||||
#include <common/channel_config.h>
|
||||
#include <common/derive_basepoints.h>
|
||||
#include <common/htlc.h>
|
||||
@@ -23,7 +24,7 @@ struct channel_view {
|
||||
u32 feerate_per_kw;
|
||||
|
||||
/* How much is owed to each side (includes pending changes) */
|
||||
u64 owed_msat[NUM_SIDES];
|
||||
struct amount_msat owed[NUM_SIDES];
|
||||
};
|
||||
|
||||
struct channel {
|
||||
@@ -34,8 +35,8 @@ struct channel {
|
||||
/* Keys used to spend funding tx. */
|
||||
struct pubkey funding_pubkey[NUM_SIDES];
|
||||
|
||||
/* Millisatoshis in from commitment tx */
|
||||
u64 funding_msat;
|
||||
/* satoshis in from commitment tx */
|
||||
struct amount_sat funding;
|
||||
|
||||
/* Who is paying fees. */
|
||||
enum side funder;
|
||||
@@ -86,8 +87,8 @@ struct channel *new_initial_channel(const tal_t *ctx,
|
||||
const struct bitcoin_blkid *chain_hash,
|
||||
const struct bitcoin_txid *funding_txid,
|
||||
unsigned int funding_txout,
|
||||
u64 funding_satoshis,
|
||||
u64 local_msatoshi,
|
||||
struct amount_sat funding,
|
||||
struct amount_msat local_msatoshi,
|
||||
u32 feerate_per_kw,
|
||||
const struct channel_config *local,
|
||||
const struct channel_config *remote,
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <common/keyset.h>
|
||||
#include <common/permute_tx.h>
|
||||
#include <common/status.h>
|
||||
#include <common/type_to_string.h>
|
||||
#include <common/utils.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
@@ -31,22 +32,22 @@ u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint,
|
||||
}
|
||||
|
||||
bool try_subtract_fee(enum side funder, enum side side,
|
||||
u64 base_fee_msat, u64 *self_msat, u64 *other_msat)
|
||||
struct amount_sat base_fee,
|
||||
struct amount_msat *self,
|
||||
struct amount_msat *other)
|
||||
{
|
||||
u64 *funder_msat;
|
||||
struct amount_msat *funder_amount;
|
||||
|
||||
if (funder == side)
|
||||
funder_msat = self_msat;
|
||||
funder_amount = self;
|
||||
else
|
||||
funder_msat = other_msat;
|
||||
funder_amount = other;
|
||||
|
||||
if (*funder_msat >= base_fee_msat) {
|
||||
*funder_msat -= base_fee_msat;
|
||||
if (amount_msat_sub_sat(funder_amount, *funder_amount, base_fee))
|
||||
return true;
|
||||
} else {
|
||||
*funder_msat = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
*funder_amount = AMOUNT_MSAT(0);
|
||||
return false;
|
||||
}
|
||||
|
||||
u8 *to_self_wscript(const tal_t *ctx,
|
||||
@@ -61,23 +62,26 @@ u8 *to_self_wscript(const tal_t *ctx,
|
||||
struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
const struct bitcoin_txid *funding_txid,
|
||||
unsigned int funding_txout,
|
||||
u64 funding_satoshis,
|
||||
struct amount_sat funding,
|
||||
enum side funder,
|
||||
u16 to_self_delay,
|
||||
const struct keyset *keyset,
|
||||
u32 feerate_per_kw,
|
||||
u64 dust_limit_satoshis,
|
||||
u64 self_pay_msat,
|
||||
u64 other_pay_msat,
|
||||
u64 self_reserve_msat,
|
||||
struct amount_sat dust_limit,
|
||||
struct amount_msat self_pay,
|
||||
struct amount_msat other_pay,
|
||||
struct amount_sat self_reserve,
|
||||
u64 obscured_commitment_number,
|
||||
enum side side)
|
||||
{
|
||||
u64 base_fee_msat;
|
||||
struct amount_sat base_fee;
|
||||
struct bitcoin_tx *tx;
|
||||
size_t n, untrimmed;
|
||||
struct amount_msat total_pay;
|
||||
|
||||
assert(self_pay_msat + other_pay_msat <= funding_satoshis * 1000);
|
||||
if (!amount_msat_add(&total_pay, self_pay, other_pay))
|
||||
abort();
|
||||
assert(!amount_msat_greater_sat(total_pay, funding));
|
||||
|
||||
/* BOLT #3:
|
||||
*
|
||||
@@ -91,15 +95,14 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
* 2. Calculate the base [commitment transaction
|
||||
* fee](#fee-calculation).
|
||||
*/
|
||||
base_fee_msat = commit_tx_base_fee_msat(feerate_per_kw, untrimmed);
|
||||
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed);
|
||||
|
||||
/* BOLT #3:
|
||||
*
|
||||
* 3. Subtract this base fee from the funder (either `to_local` or
|
||||
* `to_remote`), with a floor of 0 (see [Fee Payment](#fee-payment)).
|
||||
*/
|
||||
if (!try_subtract_fee(funder, side, base_fee_msat,
|
||||
&self_pay_msat, &other_pay_msat)) {
|
||||
if (!try_subtract_fee(funder, side, base_fee, &self_pay, &other_pay)) {
|
||||
/* BOLT #2:
|
||||
*
|
||||
* The receiving node MUST fail the channel if:
|
||||
@@ -120,15 +123,18 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
* commitment transaction are less than or equal to
|
||||
* `channel_reserve_satoshis`.
|
||||
*/
|
||||
if (self_pay_msat <= self_reserve_msat
|
||||
&& other_pay_msat <= self_reserve_msat) {
|
||||
status_unusual("Neither self amount %"PRIu64
|
||||
" nor other amount %"PRIu64
|
||||
" exceed reserve %"PRIu64
|
||||
if (!amount_msat_greater_sat(self_pay, self_reserve)
|
||||
&& !amount_msat_greater_sat(other_pay, self_reserve)) {
|
||||
status_unusual("Neither self amount %s"
|
||||
" nor other amount %s"
|
||||
" exceed reserve %s"
|
||||
" on initial commitment transaction",
|
||||
self_pay_msat / 1000,
|
||||
other_pay_msat / 1000,
|
||||
self_reserve_msat / 1000);
|
||||
type_to_string(tmpctx, struct amount_msat,
|
||||
&self_pay),
|
||||
type_to_string(tmpctx, struct amount_msat,
|
||||
&other_pay),
|
||||
type_to_string(tmpctx, struct amount_sat,
|
||||
&self_reserve));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -158,9 +164,9 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
* `dust_limit_satoshis`, add a [`to_local`
|
||||
* output](#to_local-output).
|
||||
*/
|
||||
if (self_pay_msat / 1000 >= dust_limit_satoshis) {
|
||||
if (amount_msat_greater_eq_sat(self_pay, dust_limit)) {
|
||||
u8 *wscript = to_self_wscript(tmpctx, to_self_delay,keyset);
|
||||
tx->output[n].amount = self_pay_msat / 1000;
|
||||
tx->output[n].amount = amount_msat_to_sat_round_down(self_pay).satoshis;
|
||||
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
|
||||
n++;
|
||||
}
|
||||
@@ -171,7 +177,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
* `dust_limit_satoshis`, add a [`to_remote`
|
||||
* output](#to_remote-output).
|
||||
*/
|
||||
if (other_pay_msat / 1000 >= dust_limit_satoshis) {
|
||||
if (amount_msat_greater_eq_sat(other_pay, dust_limit)) {
|
||||
/* BOLT #3:
|
||||
*
|
||||
* #### `to_remote` Output
|
||||
@@ -179,7 +185,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
* This output sends funds to the other peer and thus is a simple
|
||||
* P2WPKH to `remotepubkey`.
|
||||
*/
|
||||
tx->output[n].amount = other_pay_msat / 1000;
|
||||
tx->output[n].amount = amount_msat_to_sat_round_down(other_pay).satoshis;
|
||||
tx->output[n].script = scriptpubkey_p2wpkh(tx,
|
||||
&keyset->other_payment_key);
|
||||
n++;
|
||||
@@ -228,7 +234,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
= (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
|
||||
|
||||
/* Input amount needed for signature code. */
|
||||
tx->input[0].amount = tal_dup(tx->input, u64, &funding_satoshis);
|
||||
tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis);
|
||||
|
||||
return tx;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#define LIGHTNING_COMMON_INITIAL_COMMIT_TX_H
|
||||
#include "config.h"
|
||||
#include <bitcoin/pubkey.h>
|
||||
#include <common/amount.h>
|
||||
#include <common/htlc.h>
|
||||
|
||||
struct keyset;
|
||||
@@ -18,8 +19,8 @@ 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_sat(u32 feerate_per_kw,
|
||||
size_t num_untrimmed_htlcs)
|
||||
static inline struct amount_sat commit_tx_base_fee(u32 feerate_per_kw,
|
||||
size_t num_untrimmed_htlcs)
|
||||
{
|
||||
u64 weight;
|
||||
|
||||
@@ -44,26 +45,20 @@ static inline u64 commit_tx_base_fee_sat(u32 feerate_per_kw,
|
||||
* 3. Multiply `feerate_per_kw` by `weight`, divide by 1000 (rounding
|
||||
* down).
|
||||
*/
|
||||
return (feerate_per_kw * weight / 1000);
|
||||
}
|
||||
|
||||
static inline u64 commit_tx_base_fee_msat(u32 feerate_per_kw,
|
||||
size_t num_untrimmed_htlcs)
|
||||
{
|
||||
return commit_tx_base_fee_sat(feerate_per_kw, num_untrimmed_htlcs)
|
||||
* 1000;
|
||||
return amount_tx_fee(feerate_per_kw, weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @funding_txid, @funding_out, @funding: 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
|
||||
* @dust_limit: dust limit below which to trim outputs.
|
||||
* @self_pay: amount to pay directly to self
|
||||
* @other_pay: amount to pay directly to the other side
|
||||
* @self_reserve: reserve the other side insisted we have
|
||||
* @obscured_commitment_number: number to encode in commitment transaction
|
||||
* @side: side to generate commitment transaction for.
|
||||
*
|
||||
@@ -74,21 +69,23 @@ static inline u64 commit_tx_base_fee_msat(u32 feerate_per_kw,
|
||||
struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
const struct bitcoin_txid *funding_txid,
|
||||
unsigned int funding_txout,
|
||||
u64 funding_satoshis,
|
||||
struct amount_sat funding,
|
||||
enum side funder,
|
||||
u16 to_self_delay,
|
||||
const struct keyset *keyset,
|
||||
u32 feerate_per_kw,
|
||||
u64 dust_limit_satoshis,
|
||||
u64 self_pay_msat,
|
||||
u64 other_pay_msat,
|
||||
u64 self_reserve_msat,
|
||||
struct amount_sat dust_limit,
|
||||
struct amount_msat self_pay,
|
||||
struct amount_msat other_pay,
|
||||
struct amount_sat self_reserve,
|
||||
u64 obscured_commitment_number,
|
||||
enum side side);
|
||||
|
||||
/* try_subtract_fee - take away this fee from the funder (and return true), or all if insufficient (and return false). */
|
||||
bool try_subtract_fee(enum side funder, enum side side,
|
||||
u64 base_fee_msat, u64 *self_msat, u64 *other_msat);
|
||||
struct amount_sat base_fee,
|
||||
struct amount_msat *self,
|
||||
struct amount_msat *other);
|
||||
|
||||
/* Generate the witness script for the to-self output:
|
||||
* scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */
|
||||
|
||||
Reference in New Issue
Block a user