commit_tx & htlc_tx: use amount_sat/amount_msat.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2019-02-21 14:15:55 +10:30
parent bb00deeea4
commit 3412c5d392
9 changed files with 162 additions and 145 deletions

View file

@ -12,10 +12,11 @@
#endif
static bool trim(const struct htlc *htlc,
u32 feerate_per_kw, u64 dust_limit_satoshis,
u32 feerate_per_kw,
struct amount_sat dust_limit,
enum side side)
{
u64 htlc_fee;
struct amount_sat htlc_fee, htlc_min;
/* BOLT #3:
*
@ -41,17 +42,21 @@ static bool trim(const struct htlc *htlc,
else
htlc_fee = htlc_success_fee(feerate_per_kw);
return htlc->msatoshi / 1000 < dust_limit_satoshis + htlc_fee;
/* If these overflow, it implies htlc must be less. */
if (!amount_sat_add(&htlc_min, dust_limit, htlc_fee))
return true;
return htlc->msatoshi / 1000 < htlc_min.satoshis;
}
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
u32 feerate_per_kw, u64 dust_limit_satoshis,
u32 feerate_per_kw,
struct amount_sat dust_limit,
enum side side)
{
size_t i, n;
for (i = n = 0; i < tal_count(htlcs); i++)
n += !trim(htlcs[i], feerate_per_kw, dust_limit_satoshis, side);
n += !trim(htlcs[i], feerate_per_kw, dust_limit, side);
return n;
}
@ -91,25 +96,28 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
struct bitcoin_tx *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,
struct amount_sat dust_limit,
struct amount_msat self_pay,
struct amount_msat other_pay,
const struct htlc **htlcs,
const struct htlc ***htlcmap,
u64 obscured_commitment_number,
enum side side)
{
struct amount_sat base_fee;
struct amount_msat total_pay;
struct bitcoin_tx *tx;
size_t i, n, untrimmed;
u32 *cltvs;
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:
*
@ -118,7 +126,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
*/
untrimmed = commit_tx_num_untrimmed(htlcs,
feerate_per_kw,
dust_limit_satoshis, side);
dust_limit, side);
/* BOLT #3:
*
@ -135,28 +143,22 @@ 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 0 (see [Fee Payment](#fee-payment)).
*/
struct amount_msat self_pay, other_pay;
self_pay.millisatoshis = self_pay_msat;
other_pay.millisatoshis = other_pay_msat;
try_subtract_fee(funder, side, base_fee, &self_pay, &other_pay);
self_pay_msat = self_pay.millisatoshis;
other_pay_msat = other_pay.millisatoshis;
#ifdef PRINT_ACTUAL_FEE
{
u64 satoshis_out = 0;
for (i = 0; i < tal_count(htlcs); i++) {
if (!trim(htlcs[i], feerate_per_kw, dust_limit_satoshis,
if (!trim(htlcs[i], feerate_per_kw, dust_limit,
side))
satoshis_out += htlcs[i]->msatoshi / 1000;
}
if (self_pay_msat / 1000 >= dust_limit_satoshis)
satoshis_out += self_pay_msat / 1000;
if (other_pay_msat / 1000 >= dust_limit_satoshis)
satoshis_out += other_pay_msat / 1000;
if (amount_msat_greater_sat(self_pay, dust_limit))
satoshis_out += self_pay.millisatoshis / 1000;
if (amount_msat_greater_sat(other_pay, dust_limit))
satoshis_out += other_pay.millisatoshis / 1000;
SUPERVERBOSE("# actual commitment transaction fee = %"PRIu64"\n",
funding_satoshis - satoshis_out);
funding.satoshis - satoshis_out);
}
#endif
@ -182,7 +184,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
for (i = 0; i < tal_count(htlcs); i++) {
if (htlc_owner(htlcs[i]) != side)
continue;
if (trim(htlcs[i], feerate_per_kw, dust_limit_satoshis, side))
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
continue;
add_offered_htlc_out(tx, n, htlcs[i], keyset);
(*htlcmap)[n] = htlcs[i];
@ -198,7 +200,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
for (i = 0; i < tal_count(htlcs); i++) {
if (htlc_owner(htlcs[i]) == side)
continue;
if (trim(htlcs[i], feerate_per_kw, dust_limit_satoshis, side))
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
continue;
add_received_htlc_out(tx, n, htlcs[i], keyset);
(*htlcmap)[n] = htlcs[i];
@ -212,9 +214,9 @@ struct bitcoin_tx *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 = self_pay.millisatoshis / 1000;
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
(*htlcmap)[n] = NULL;
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
@ -231,7 +233,7 @@ struct bitcoin_tx *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
@ -239,7 +241,7 @@ struct bitcoin_tx *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 = other_pay.millisatoshis / 1000;
tx->output[n].script = scriptpubkey_p2wpkh(tx,
&keyset->other_payment_key);
(*htlcmap)[n] = NULL;
@ -295,7 +297,7 @@ struct bitcoin_tx *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;
}

View file

@ -12,26 +12,27 @@ struct keyset;
* commit_tx_num_untrimmed: how many of these htlc outputs will commit tx have?
* @htlcs: tal_arr of HTLCs
* @feerate_per_kw: feerate to use
* @dust_limit_satoshis: dust limit below which to trim outputs.
* @dust_limit: dust limit below which to trim outputs.
* @side: from which side's point of view
*
* We need @side because HTLC fees are different for offered and
* received HTLCs.
*/
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
u32 feerate_per_kw, u64 dust_limit_satoshis,
u32 feerate_per_kw,
struct amount_sat dust_limit,
enum side side);
/**
* 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
* @htlcs: tal_arr of htlcs committed by transaction (some may be trimmed)
* @htlc_map: outputed map of outnum->HTLC (NULL for direct outputs).
* @obscured_commitment_number: number to encode in commitment transaction
@ -44,14 +45,14 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
struct bitcoin_tx *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,
struct amount_sat dust_limit,
struct amount_msat self_pay,
struct amount_msat other_pay,
const struct htlc **htlcs,
const struct htlc ***htlcmap,
u64 obscured_commitment_number,

View file

@ -211,13 +211,15 @@ static void add_htlcs(struct bitcoin_tx ***txs,
const struct htlc *htlc = htlcmap[i];
struct bitcoin_tx *tx;
u8 *wscript;
struct amount_msat htlc_amount;
if (!htlc)
continue;
htlc_amount.millisatoshis = htlc->msatoshi;
if (htlc_owner(htlc) == side) {
tx = htlc_timeout_tx(*txs, &txid, i,
htlc->msatoshi,
htlc_amount,
htlc->expiry.locktime,
channel->config[!side].to_self_delay,
feerate_per_kw,
@ -229,7 +231,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,
&keyset->self_revocation_key);
} else {
tx = htlc_success_tx(*txs, &txid, i,
htlc->msatoshi,
htlc_amount,
channel->config[!side].to_self_delay,
feerate_per_kw,
keyset);
@ -274,14 +276,14 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
txs = tal_arr(ctx, struct bitcoin_tx *, 1);
txs[0] = commit_tx(ctx, &channel->funding_txid,
channel->funding_txout,
channel->funding.satoshis,
channel->funding,
channel->funder,
channel->config[!side].to_self_delay,
&keyset,
channel->view[side].feerate_per_kw,
channel->config[side].dust_limit.satoshis,
channel->view[side].owed[side].millisatoshis,
channel->view[side].owed[!side].millisatoshis,
channel->config[side].dust_limit,
channel->view[side].owed[side],
channel->view[side].owed[!side],
committed,
htlcmap,
commitment_number ^ channel->commitment_number_obscurer,
@ -438,14 +440,14 @@ static enum channel_add_err add_htlc(struct channel *channel,
*/
if (channel->funder == htlc_owner(htlc)) {
u32 feerate = view->feerate_per_kw;
u64 dust = channel->config[recipient].dust_limit.satoshis;
struct amount_sat dust_limit = channel->config[recipient].dust_limit;
size_t untrimmed;
untrimmed = commit_tx_num_untrimmed(committed, feerate, dust,
untrimmed = commit_tx_num_untrimmed(committed, feerate, dust_limit,
recipient)
+ commit_tx_num_untrimmed(adding, feerate, dust,
+ commit_tx_num_untrimmed(adding, feerate, dust_limit,
recipient)
- commit_tx_num_untrimmed(removing, feerate, dust,
- commit_tx_num_untrimmed(removing, feerate, dust_limit,
recipient);
fee = commit_tx_base_fee(feerate, untrimmed);
@ -793,17 +795,17 @@ u32 approx_max_feerate(const struct channel *channel)
bool can_funder_afford_feerate(const struct channel *channel, u32 feerate_per_kw)
{
struct amount_sat needed, fee;
u64 dust = channel->config[!channel->funder].dust_limit.satoshis;
struct amount_sat dust_limit = channel->config[!channel->funder].dust_limit;
size_t untrimmed;
const struct htlc **committed, **adding, **removing;
gather_htlcs(tmpctx, channel, !channel->funder,
&committed, &removing, &adding);
untrimmed = commit_tx_num_untrimmed(committed, feerate_per_kw, dust,
untrimmed = commit_tx_num_untrimmed(committed, feerate_per_kw, dust_limit,
!channel->funder)
+ commit_tx_num_untrimmed(adding, feerate_per_kw, dust,
+ commit_tx_num_untrimmed(adding, feerate_per_kw, dust_limit,
!channel->funder)
- commit_tx_num_untrimmed(removing, feerate_per_kw, dust,
- commit_tx_num_untrimmed(removing, feerate_per_kw, dust_limit,
!channel->funder);
fee = commit_tx_base_fee(feerate_per_kw, untrimmed);

View file

@ -217,13 +217,15 @@ static void report_htlcs(const struct bitcoin_tx *tx,
for (i = 0; i < tal_count(htlc_map); i++) {
const struct htlc *htlc = htlc_map[i];
struct amount_msat htlc_amount;
if (!htlc)
continue;
htlc_amount.millisatoshis = htlc->msatoshi;
if (htlc_owner(htlc) == LOCAL) {
htlc_tx[i] = htlc_timeout_tx(htlc_tx, &txid, i,
htlc->msatoshi,
htlc_amount,
htlc->expiry.locktime,
to_self_delay,
feerate_per_kw,
@ -235,7 +237,7 @@ static void report_htlcs(const struct bitcoin_tx *tx,
remote_revocation_key);
} else {
htlc_tx[i] = htlc_success_tx(htlc_tx, &txid, i,
htlc->msatoshi,
htlc_amount,
to_self_delay,
feerate_per_kw,
&keyset);
@ -354,15 +356,17 @@ static void report(struct bitcoin_tx *tx,
}
#ifdef DEBUG
static u64 calc_fee(const struct bitcoin_tx *tx, u64 input_satoshi)
static struct amount_sat calc_fee(const struct bitcoin_tx *tx, struct amount_sat input)
{
size_t i;
u64 output_satoshi = 0;
struct amount_sat output = AMOUNT_SAT(0), fee;
for (i = 0; i < tal_count(tx->output); i++)
output_satoshi += tx->output[i].amount;
output.satoshis += tx->output[i].amount;
return input_satoshi - output_satoshi;
if (!amount_sub_sat(&fee, input, output))
abort();
return fee;
}
/* For debugging, we do brute-force increase to find thresholds */
@ -424,7 +428,7 @@ int main(void)
setup_locale();
struct bitcoin_txid funding_txid;
u64 funding_amount_satoshi, dust_limit_satoshi;
struct amount_sat funding_amount, dust_limit;
u32 feerate_per_kw;
u16 to_self_delay;
/* x_ prefix means internal vars we used to derive spec */
@ -450,7 +454,8 @@ int main(void)
struct keyset keyset;
u8 *wscript;
unsigned int funding_output_index;
u64 commitment_number, cn_obscurer, to_local_msat, to_remote_msat;
u64 commitment_number, cn_obscurer;
struct amount_msat to_local, to_remote;
const struct htlc **htlcs, **htlc_map, **htlc_map2, **inv_htlcs;
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
@ -487,10 +492,10 @@ int main(void)
*/
funding_txid = txid_from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be");
funding_output_index = 0;
funding_amount_satoshi = 10000000;
funding_amount.satoshis = 10000000;
commitment_number = 42;
to_self_delay = 144;
dust_limit_satoshi = 546;
dust_limit.satoshis = 546;
#ifdef DEBUG
print_superverbose = true;
@ -689,15 +694,15 @@ int main(void)
* to_remote_msat: 3000000000
* local_feerate_per_kw: 15000
*/
to_local_msat = 7000000000;
to_remote_msat = 3000000000;
to_local.millisatoshis = 7000000000;
to_remote.millisatoshis = 3000000000;
feerate_per_kw = 15000;
printf("\n"
"name: simple commitment tx with no HTLCs\n"
"to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
to_local_msat, to_remote_msat, feerate_per_kw);
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw);
keyset.self_revocation_key = remote_revocation_key;
keyset.self_delayed_payment_key = local_delayedkey;
@ -708,24 +713,24 @@ int main(void)
print_superverbose = true;
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount_satoshi,
funding_amount,
LOCAL, to_self_delay,
&keyset,
feerate_per_kw,
dust_limit_satoshi,
to_local_msat,
to_remote_msat,
dust_limit,
to_local,
to_remote,
NULL, &htlc_map, commitment_number ^ cn_obscurer,
LOCAL);
print_superverbose = false;
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount_satoshi,
funding_amount,
REMOTE, to_self_delay,
&keyset,
feerate_per_kw,
dust_limit_satoshi,
to_local_msat,
to_remote_msat,
dust_limit,
to_local,
to_remote,
NULL, &htlc_map2, commitment_number ^ cn_obscurer,
REMOTE);
tx_must_be_eq(tx, tx2);
@ -750,36 +755,36 @@ int main(void)
* to_remote_msat: 3000000000
* local_feerate_per_kw: 0
*/
to_local_msat = 6988000000;
to_remote_msat = 3000000000;
to_local.millisatoshis = 6988000000;
to_remote.millisatoshis = 3000000000;
feerate_per_kw = 0;
printf("\n"
"name: commitment tx with all 5 htlcs untrimmed (minimum feerate)\n"
"to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
to_local_msat, to_remote_msat, feerate_per_kw);
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw);
print_superverbose = true;
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount_satoshi,
funding_amount,
LOCAL, to_self_delay,
&keyset,
feerate_per_kw,
dust_limit_satoshi,
to_local_msat,
to_remote_msat,
dust_limit,
to_local,
to_remote,
htlcs, &htlc_map, commitment_number ^ cn_obscurer,
LOCAL);
print_superverbose = false;
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount_satoshi,
funding_amount,
REMOTE, to_self_delay,
&keyset,
feerate_per_kw,
dust_limit_satoshi,
to_local_msat,
to_remote_msat,
dust_limit,
to_local,
to_remote,
inv_htlcs, &htlc_map2,
commitment_number ^ cn_obscurer,
REMOTE);
@ -804,25 +809,25 @@ int main(void)
feerate_per_kw = increase(feerate_per_kw);
print_superverbose = false;
newtx = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount_satoshi,
funding_amount,
LOCAL, to_self_delay,
&keyset,
feerate_per_kw,
dust_limit_satoshi,
to_local_msat,
to_remote_msat,
dust_limit,
to_local,
to_remote,
htlcs, &htlc_map,
commitment_number ^ cn_obscurer,
LOCAL);
/* This is what it would look like for peer generating it! */
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount_satoshi,
funding_amount,
REMOTE, to_self_delay,
&keyset,
feerate_per_kw,
dust_limit_satoshi,
to_local_msat,
to_remote_msat,
dust_limit,
to_local,
to_remote,
inv_htlcs, &htlc_map2,
commitment_number ^ cn_obscurer,
REMOTE);
@ -830,7 +835,7 @@ int main(void)
#ifdef DEBUG
if (feerate_per_kw % 100000 == 0)
printf("feerate_per_kw = %u, fees = %"PRIu64"\n",
feerate_per_kw, calc_fee(newtx, funding_amount_satoshi));
feerate_per_kw, calc_fee(newtx, funding_amount));
if (tal_count(newtx->output) == tal_count(tx->output)) {
tal_free(newtx);
continue;
@ -843,17 +848,17 @@ int main(void)
"local_feerate_per_kw: %u\n",
tal_count(tx->output),
tal_count(tx->output) > 1 ? "s" : "",
to_local_msat, to_remote_msat, feerate_per_kw-1);
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw-1);
/* Recalc with verbosity on */
print_superverbose = true;
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount_satoshi,
funding_amount,
LOCAL, to_self_delay,
&keyset,
feerate_per_kw-1,
dust_limit_satoshi,
to_local_msat,
to_remote_msat,
dust_limit,
to_local,
to_remote,
htlcs, &htlc_map,
commitment_number ^ cn_obscurer,
LOCAL);
@ -879,17 +884,17 @@ int main(void)
"local_feerate_per_kw: %u\n",
tal_count(newtx->output),
tal_count(newtx->output) > 1 ? "s" : "",
to_local_msat, to_remote_msat, feerate_per_kw);
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw);
/* Recalc with verbosity on */
print_superverbose = true;
newtx = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount_satoshi,
funding_amount,
LOCAL, to_self_delay,
&keyset,
feerate_per_kw,
dust_limit_satoshi,
to_local_msat,
to_remote_msat,
dust_limit,
to_local,
to_remote,
htlcs, &htlc_map,
commitment_number ^ cn_obscurer,
LOCAL);
@ -917,11 +922,9 @@ int main(void)
/* Now make sure we cover case where funder can't afford the fee;
* its output cannot go negative! */
for (;;) {
struct amount_msat to_local;
struct amount_sat base_fee
= commit_tx_base_fee(feerate_per_kw, 0);
to_local.millisatoshis = to_local_msat;
if (amount_msat_greater_eq_sat(to_local, base_fee)) {
feerate_per_kw++;
continue;
@ -941,15 +944,15 @@ int main(void)
"to_local_msat: %"PRIu64"\n"
"to_remote_msat: %"PRIu64"\n"
"local_feerate_per_kw: %u\n",
to_local_msat, to_remote_msat, feerate_per_kw);
to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw);
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount_satoshi,
funding_amount,
LOCAL, to_self_delay,
&keyset,
feerate_per_kw,
dust_limit_satoshi,
to_local_msat,
to_remote_msat,
dust_limit,
to_local,
to_remote,
htlcs, &htlc_map,
commitment_number ^ cn_obscurer,
LOCAL);

View file

@ -338,7 +338,7 @@ int main(void)
struct pubkey *unknown;
struct bitcoin_tx *raw_tx, **txs, **txs2;
struct channel_config *local_config, *remote_config;
struct amount_msat to_local_msat, to_remote_msat;
struct amount_msat to_local, to_remote;
const struct htlc **htlc_map, **htlcs;
const u8 *funding_wscript, **wscripts;
size_t i;
@ -448,13 +448,13 @@ int main(void)
* local_feerate_per_kw: 15000
*/
to_local_msat = AMOUNT_MSAT(7000000000);
to_remote_msat = AMOUNT_MSAT(3000000000);
to_local = AMOUNT_MSAT(7000000000);
to_remote = AMOUNT_MSAT(3000000000);
feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 15000;
lchannel = new_full_channel(tmpctx,
&chainparams->genesis_blockhash,
&funding_txid, funding_output_index,
funding_amount, to_local_msat,
funding_amount, to_local,
feerate_per_kw,
local_config,
remote_config,
@ -465,7 +465,7 @@ int main(void)
rchannel = new_full_channel(tmpctx,
&chainparams->genesis_blockhash,
&funding_txid, funding_output_index,
funding_amount, to_remote_msat,
funding_amount, to_remote,
feerate_per_kw,
remote_config,
local_config,
@ -495,13 +495,13 @@ int main(void)
keyset.other_htlc_key = keyset.other_payment_key;
raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount.satoshis,
funding_amount,
LOCAL, remote_config->to_self_delay,
&keyset,
feerate_per_kw[LOCAL],
local_config->dust_limit.satoshis,
to_local_msat.millisatoshis,
to_remote_msat.millisatoshis,
local_config->dust_limit,
to_local,
to_remote,
NULL, &htlc_map, 0x2bb038521914 ^ 42, LOCAL);
txs = channel_txs(tmpctx, &htlc_map, &wscripts,
@ -523,8 +523,8 @@ int main(void)
* to_remote_msat: 3000000000
* local_feerate_per_kw: 0
*/
to_local_msat = AMOUNT_MSAT(6988000000);
to_remote_msat = AMOUNT_MSAT(3000000000);
to_local = AMOUNT_MSAT(6988000000);
to_remote = AMOUNT_MSAT(3000000000);
feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 0;
/* Now, BOLT doesn't adjust owed amounts the same way we do
@ -613,13 +613,13 @@ int main(void)
rchannel->view[REMOTE].feerate_per_kw = feerate_per_kw[REMOTE];
raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount.satoshis,
funding_amount,
LOCAL, remote_config->to_self_delay,
&keyset,
feerate_per_kw[LOCAL],
local_config->dust_limit.satoshis,
to_local_msat.millisatoshis,
to_remote_msat.millisatoshis,
local_config->dust_limit,
to_local,
to_remote,
htlcs, &htlc_map,
0x2bb038521914 ^ 42, LOCAL);

View file

@ -7,16 +7,16 @@
static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number,
u64 msatoshi,
struct amount_msat msat,
u16 to_self_delay,
const struct pubkey *revocation_pubkey,
const struct pubkey *local_delayedkey,
u64 htlc_fee_satoshi,
struct amount_sat htlc_fee,
u32 locktime)
{
struct bitcoin_tx *tx = bitcoin_tx(ctx, 1, 1);
u8 *wscript;
u64 amount;
struct amount_sat amount, out_amount;
/* BOLT #3:
*
@ -48,8 +48,8 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
tx->input[0].index = commit_output_number;
/* We need amount for signing. */
amount = msatoshi / 1000;
tx->input[0].amount = tal_dup(tx, u64, &amount);
amount = amount_msat_to_sat_round_down(msat);
tx->input[0].amount = tal_dup(tx, u64, &amount.satoshis);
/* BOLT #3:
* * `txin[0]` sequence: `0`
@ -63,7 +63,10 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
* * `txout[0]` script: version-0 P2WSH with witness script as shown
* below
*/
tx->output[0].amount = amount - htlc_fee_satoshi;
if (!amount_sat_sub(&out_amount, amount, htlc_fee))
abort();
tx->output[0].amount = out_amount.satoshis;
wscript = bitcoin_wscript_htlc_tx(tx, to_self_delay,
revocation_pubkey, local_delayedkey);
tx->output[0].script = scriptpubkey_p2wsh(tx, wscript);
@ -75,7 +78,7 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number,
u64 htlc_msatoshi,
struct amount_msat htlc_msatoshi,
u16 to_self_delay,
u32 feerate_per_kw,
const struct keyset *keyset)
@ -120,7 +123,7 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success,
struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number,
u64 htlc_msatoshi,
struct amount_msat htlc_msatoshi,
u32 cltv_expiry,
u16 to_self_delay,
u32 feerate_per_kw,

View file

@ -1,13 +1,14 @@
#ifndef LIGHTNING_COMMON_HTLC_TX_H
#define LIGHTNING_COMMON_HTLC_TX_H
#include "config.h"
#include <common/amount.h>
#include <common/htlc.h>
struct keyset;
struct preimage;
struct pubkey;
static inline u64 htlc_timeout_fee(u32 feerate_per_kw)
static inline struct amount_sat htlc_timeout_fee(u32 feerate_per_kw)
{
/* BOLT #3:
*
@ -16,10 +17,10 @@ static inline u64 htlc_timeout_fee(u32 feerate_per_kw)
* 1. Multiply `feerate_per_kw` by 663 and divide by 1000 (rounding
* down).
*/
return feerate_per_kw * 663ULL / 1000;
return amount_tx_fee(663, feerate_per_kw);
}
static inline u64 htlc_success_fee(u32 feerate_per_kw)
static inline struct amount_sat htlc_success_fee(u32 feerate_per_kw)
{
/* BOLT #3:
*
@ -28,7 +29,7 @@ static inline u64 htlc_success_fee(u32 feerate_per_kw)
* 1. Multiply `feerate_per_kw` by 703 and divide by 1000 (rounding
* down).
*/
return feerate_per_kw * 703ULL / 1000;
return amount_tx_fee(703, feerate_per_kw);
}
/* Create HTLC-success tx to spend a received HTLC commitment tx
@ -36,7 +37,7 @@ static inline u64 htlc_success_fee(u32 feerate_per_kw)
struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number,
u64 htlc_msatoshi,
struct amount_msat htlc_msatoshi,
u16 to_self_delay,
u32 feerate_per_kw,
const struct keyset *keyset);
@ -56,7 +57,7 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success,
struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number,
u64 htlc_msatoshi,
struct amount_msat htlc_msatoshi,
u32 cltv_expiry,
u16 to_self_delay,
u32 feerate_per_kw,

View file

@ -1150,9 +1150,11 @@ static void handle_preimage(struct tracked_output **outs,
* HTLC-success transaction.
*/
if (outs[i]->remote_htlc_sig) {
struct amount_msat htlc_amount;
htlc_amount.millisatoshis = outs[i]->satoshi * 1000;
tx = htlc_success_tx(outs[i], &outs[i]->txid,
outs[i]->outnum,
outs[i]->satoshi * 1000,
htlc_amount,
to_self_delay[LOCAL],
0,
keyset);
@ -1342,6 +1344,9 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out,
struct bitcoin_tx *tx = NULL;
struct bitcoin_signature localsig;
size_t i;
struct amount_msat htlc_amount;
htlc_amount.millisatoshis = out->satoshi * 1000;
assert(tal_count(matches));
@ -1358,7 +1363,7 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out,
* HTLC-timeout transaction.
*/
tx = htlc_timeout_tx(tmpctx, &out->txid, out->outnum,
out->satoshi * 1000,
htlc_amount,
htlcs[matches[i]].cltv_expiry,
to_self_delay[LOCAL], 0, keyset);

View file

@ -63,7 +63,7 @@ u8 *htlc_received_wscript(const tal_t *ctx UNNEEDED,
struct bitcoin_tx *htlc_success_tx(const tal_t *ctx UNNEEDED,
const struct bitcoin_txid *commit_txid UNNEEDED,
unsigned int commit_output_number UNNEEDED,
u64 htlc_msatoshi UNNEEDED,
struct amount_msat htlc_msatoshi UNNEEDED,
u16 to_self_delay UNNEEDED,
u32 feerate_per_kw UNNEEDED,
const struct keyset *keyset UNNEEDED)
@ -72,7 +72,7 @@ struct bitcoin_tx *htlc_success_tx(const tal_t *ctx UNNEEDED,
struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx UNNEEDED,
const struct bitcoin_txid *commit_txid UNNEEDED,
unsigned int commit_output_number UNNEEDED,
u64 htlc_msatoshi UNNEEDED,
struct amount_msat htlc_msatoshi UNNEEDED,
u32 cltv_expiry UNNEEDED,
u16 to_self_delay UNNEEDED,
u32 feerate_per_kw UNNEEDED,