mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-03 18:57:06 +01:00
commit_tx & htlc_tx: use amount_sat/amount_msat.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
bb00deeea4
commit
3412c5d392
9 changed files with 162 additions and 145 deletions
|
@ -12,10 +12,11 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static bool trim(const struct htlc *htlc,
|
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)
|
enum side side)
|
||||||
{
|
{
|
||||||
u64 htlc_fee;
|
struct amount_sat htlc_fee, htlc_min;
|
||||||
|
|
||||||
/* BOLT #3:
|
/* BOLT #3:
|
||||||
*
|
*
|
||||||
|
@ -41,17 +42,21 @@ static bool trim(const struct htlc *htlc,
|
||||||
else
|
else
|
||||||
htlc_fee = htlc_success_fee(feerate_per_kw);
|
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,
|
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)
|
enum side side)
|
||||||
{
|
{
|
||||||
size_t i, n;
|
size_t i, n;
|
||||||
|
|
||||||
for (i = n = 0; i < tal_count(htlcs); i++)
|
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;
|
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,
|
struct bitcoin_tx *commit_tx(const tal_t *ctx,
|
||||||
const struct bitcoin_txid *funding_txid,
|
const struct bitcoin_txid *funding_txid,
|
||||||
unsigned int funding_txout,
|
unsigned int funding_txout,
|
||||||
u64 funding_satoshis,
|
struct amount_sat funding,
|
||||||
enum side funder,
|
enum side funder,
|
||||||
u16 to_self_delay,
|
u16 to_self_delay,
|
||||||
const struct keyset *keyset,
|
const struct keyset *keyset,
|
||||||
u32 feerate_per_kw,
|
u32 feerate_per_kw,
|
||||||
u64 dust_limit_satoshis,
|
struct amount_sat dust_limit,
|
||||||
u64 self_pay_msat,
|
struct amount_msat self_pay,
|
||||||
u64 other_pay_msat,
|
struct amount_msat other_pay,
|
||||||
const struct htlc **htlcs,
|
const struct htlc **htlcs,
|
||||||
const struct htlc ***htlcmap,
|
const struct htlc ***htlcmap,
|
||||||
u64 obscured_commitment_number,
|
u64 obscured_commitment_number,
|
||||||
enum side side)
|
enum side side)
|
||||||
{
|
{
|
||||||
struct amount_sat base_fee;
|
struct amount_sat base_fee;
|
||||||
|
struct amount_msat total_pay;
|
||||||
struct bitcoin_tx *tx;
|
struct bitcoin_tx *tx;
|
||||||
size_t i, n, untrimmed;
|
size_t i, n, untrimmed;
|
||||||
u32 *cltvs;
|
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:
|
/* BOLT #3:
|
||||||
*
|
*
|
||||||
|
@ -118,7 +126,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
|
||||||
*/
|
*/
|
||||||
untrimmed = commit_tx_num_untrimmed(htlcs,
|
untrimmed = commit_tx_num_untrimmed(htlcs,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
dust_limit_satoshis, side);
|
dust_limit, side);
|
||||||
|
|
||||||
/* BOLT #3:
|
/* 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
|
* 3. Subtract this base fee from the funder (either `to_local` or
|
||||||
* `to_remote`), with a floor of 0 (see [Fee Payment](#fee-payment)).
|
* `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);
|
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
|
#ifdef PRINT_ACTUAL_FEE
|
||||||
{
|
{
|
||||||
u64 satoshis_out = 0;
|
u64 satoshis_out = 0;
|
||||||
for (i = 0; i < tal_count(htlcs); i++) {
|
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))
|
side))
|
||||||
satoshis_out += htlcs[i]->msatoshi / 1000;
|
satoshis_out += htlcs[i]->msatoshi / 1000;
|
||||||
}
|
}
|
||||||
if (self_pay_msat / 1000 >= dust_limit_satoshis)
|
if (amount_msat_greater_sat(self_pay, dust_limit))
|
||||||
satoshis_out += self_pay_msat / 1000;
|
satoshis_out += self_pay.millisatoshis / 1000;
|
||||||
if (other_pay_msat / 1000 >= dust_limit_satoshis)
|
if (amount_msat_greater_sat(other_pay, dust_limit))
|
||||||
satoshis_out += other_pay_msat / 1000;
|
satoshis_out += other_pay.millisatoshis / 1000;
|
||||||
SUPERVERBOSE("# actual commitment transaction fee = %"PRIu64"\n",
|
SUPERVERBOSE("# actual commitment transaction fee = %"PRIu64"\n",
|
||||||
funding_satoshis - satoshis_out);
|
funding.satoshis - satoshis_out);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -182,7 +184,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
|
||||||
for (i = 0; i < tal_count(htlcs); i++) {
|
for (i = 0; i < tal_count(htlcs); i++) {
|
||||||
if (htlc_owner(htlcs[i]) != side)
|
if (htlc_owner(htlcs[i]) != side)
|
||||||
continue;
|
continue;
|
||||||
if (trim(htlcs[i], feerate_per_kw, dust_limit_satoshis, side))
|
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
|
||||||
continue;
|
continue;
|
||||||
add_offered_htlc_out(tx, n, htlcs[i], keyset);
|
add_offered_htlc_out(tx, n, htlcs[i], keyset);
|
||||||
(*htlcmap)[n] = htlcs[i];
|
(*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++) {
|
for (i = 0; i < tal_count(htlcs); i++) {
|
||||||
if (htlc_owner(htlcs[i]) == side)
|
if (htlc_owner(htlcs[i]) == side)
|
||||||
continue;
|
continue;
|
||||||
if (trim(htlcs[i], feerate_per_kw, dust_limit_satoshis, side))
|
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
|
||||||
continue;
|
continue;
|
||||||
add_received_htlc_out(tx, n, htlcs[i], keyset);
|
add_received_htlc_out(tx, n, htlcs[i], keyset);
|
||||||
(*htlcmap)[n] = htlcs[i];
|
(*htlcmap)[n] = htlcs[i];
|
||||||
|
@ -212,9 +214,9 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
|
||||||
* `dust_limit_satoshis`, add a [`to_local`
|
* `dust_limit_satoshis`, add a [`to_local`
|
||||||
* output](#to_local-output).
|
* 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);
|
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);
|
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
|
||||||
(*htlcmap)[n] = NULL;
|
(*htlcmap)[n] = NULL;
|
||||||
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
|
/* 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`
|
* `dust_limit_satoshis`, add a [`to_remote`
|
||||||
* output](#to_remote-output).
|
* output](#to_remote-output).
|
||||||
*/
|
*/
|
||||||
if (other_pay_msat / 1000 >= dust_limit_satoshis) {
|
if (amount_msat_greater_eq_sat(other_pay, dust_limit)) {
|
||||||
/* BOLT #3:
|
/* BOLT #3:
|
||||||
*
|
*
|
||||||
* #### `to_remote` Output
|
* #### `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
|
* This output sends funds to the other peer and thus is a simple
|
||||||
* P2WPKH to `remotepubkey`.
|
* 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,
|
tx->output[n].script = scriptpubkey_p2wpkh(tx,
|
||||||
&keyset->other_payment_key);
|
&keyset->other_payment_key);
|
||||||
(*htlcmap)[n] = NULL;
|
(*htlcmap)[n] = NULL;
|
||||||
|
@ -295,7 +297,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
|
||||||
= (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
|
= (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
|
||||||
|
|
||||||
/* Input amount needed for signature code. */
|
/* 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;
|
return tx;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,26 +12,27 @@ struct keyset;
|
||||||
* commit_tx_num_untrimmed: how many of these htlc outputs will commit tx have?
|
* commit_tx_num_untrimmed: how many of these htlc outputs will commit tx have?
|
||||||
* @htlcs: tal_arr of HTLCs
|
* @htlcs: tal_arr of HTLCs
|
||||||
* @feerate_per_kw: feerate to use
|
* @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
|
* @side: from which side's point of view
|
||||||
*
|
*
|
||||||
* We need @side because HTLC fees are different for offered and
|
* We need @side because HTLC fees are different for offered and
|
||||||
* received HTLCs.
|
* received HTLCs.
|
||||||
*/
|
*/
|
||||||
size_t commit_tx_num_untrimmed(const struct htlc **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);
|
enum side side);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* commit_tx: create (unsigned) commitment tx to spend the funding tx output
|
* commit_tx: create (unsigned) commitment tx to spend the funding tx output
|
||||||
* @ctx: context to allocate transaction and @htlc_map from.
|
* @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?
|
* @funder: is the LOCAL or REMOTE paying the fee?
|
||||||
* @keyset: keys derived for this commit tx.
|
* @keyset: keys derived for this commit tx.
|
||||||
* @feerate_per_kw: feerate to use
|
* @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.
|
||||||
* @self_pay_msat: amount to pay directly to self
|
* @self_pay: amount to pay directly to self
|
||||||
* @other_pay_msat: amount to pay directly to the other side
|
* @other_pay: amount to pay directly to the other side
|
||||||
* @htlcs: tal_arr of htlcs committed by transaction (some may be trimmed)
|
* @htlcs: tal_arr of htlcs committed by transaction (some may be trimmed)
|
||||||
* @htlc_map: outputed map of outnum->HTLC (NULL for direct outputs).
|
* @htlc_map: outputed map of outnum->HTLC (NULL for direct outputs).
|
||||||
* @obscured_commitment_number: number to encode in commitment transaction
|
* @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,
|
struct bitcoin_tx *commit_tx(const tal_t *ctx,
|
||||||
const struct bitcoin_txid *funding_txid,
|
const struct bitcoin_txid *funding_txid,
|
||||||
unsigned int funding_txout,
|
unsigned int funding_txout,
|
||||||
u64 funding_satoshis,
|
struct amount_sat funding,
|
||||||
enum side funder,
|
enum side funder,
|
||||||
u16 to_self_delay,
|
u16 to_self_delay,
|
||||||
const struct keyset *keyset,
|
const struct keyset *keyset,
|
||||||
u32 feerate_per_kw,
|
u32 feerate_per_kw,
|
||||||
u64 dust_limit_satoshis,
|
struct amount_sat dust_limit,
|
||||||
u64 self_pay_msat,
|
struct amount_msat self_pay,
|
||||||
u64 other_pay_msat,
|
struct amount_msat other_pay,
|
||||||
const struct htlc **htlcs,
|
const struct htlc **htlcs,
|
||||||
const struct htlc ***htlcmap,
|
const struct htlc ***htlcmap,
|
||||||
u64 obscured_commitment_number,
|
u64 obscured_commitment_number,
|
||||||
|
|
|
@ -211,13 +211,15 @@ static void add_htlcs(struct bitcoin_tx ***txs,
|
||||||
const struct htlc *htlc = htlcmap[i];
|
const struct htlc *htlc = htlcmap[i];
|
||||||
struct bitcoin_tx *tx;
|
struct bitcoin_tx *tx;
|
||||||
u8 *wscript;
|
u8 *wscript;
|
||||||
|
struct amount_msat htlc_amount;
|
||||||
|
|
||||||
if (!htlc)
|
if (!htlc)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
htlc_amount.millisatoshis = htlc->msatoshi;
|
||||||
if (htlc_owner(htlc) == side) {
|
if (htlc_owner(htlc) == side) {
|
||||||
tx = htlc_timeout_tx(*txs, &txid, i,
|
tx = htlc_timeout_tx(*txs, &txid, i,
|
||||||
htlc->msatoshi,
|
htlc_amount,
|
||||||
htlc->expiry.locktime,
|
htlc->expiry.locktime,
|
||||||
channel->config[!side].to_self_delay,
|
channel->config[!side].to_self_delay,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
|
@ -229,7 +231,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,
|
||||||
&keyset->self_revocation_key);
|
&keyset->self_revocation_key);
|
||||||
} else {
|
} else {
|
||||||
tx = htlc_success_tx(*txs, &txid, i,
|
tx = htlc_success_tx(*txs, &txid, i,
|
||||||
htlc->msatoshi,
|
htlc_amount,
|
||||||
channel->config[!side].to_self_delay,
|
channel->config[!side].to_self_delay,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
keyset);
|
keyset);
|
||||||
|
@ -274,14 +276,14 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
|
||||||
txs = tal_arr(ctx, struct bitcoin_tx *, 1);
|
txs = tal_arr(ctx, struct bitcoin_tx *, 1);
|
||||||
txs[0] = commit_tx(ctx, &channel->funding_txid,
|
txs[0] = commit_tx(ctx, &channel->funding_txid,
|
||||||
channel->funding_txout,
|
channel->funding_txout,
|
||||||
channel->funding.satoshis,
|
channel->funding,
|
||||||
channel->funder,
|
channel->funder,
|
||||||
channel->config[!side].to_self_delay,
|
channel->config[!side].to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
channel->view[side].feerate_per_kw,
|
channel->view[side].feerate_per_kw,
|
||||||
channel->config[side].dust_limit.satoshis,
|
channel->config[side].dust_limit,
|
||||||
channel->view[side].owed[side].millisatoshis,
|
channel->view[side].owed[side],
|
||||||
channel->view[side].owed[!side].millisatoshis,
|
channel->view[side].owed[!side],
|
||||||
committed,
|
committed,
|
||||||
htlcmap,
|
htlcmap,
|
||||||
commitment_number ^ channel->commitment_number_obscurer,
|
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)) {
|
if (channel->funder == htlc_owner(htlc)) {
|
||||||
u32 feerate = view->feerate_per_kw;
|
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;
|
size_t untrimmed;
|
||||||
|
|
||||||
untrimmed = commit_tx_num_untrimmed(committed, feerate, dust,
|
untrimmed = commit_tx_num_untrimmed(committed, feerate, dust_limit,
|
||||||
recipient)
|
recipient)
|
||||||
+ commit_tx_num_untrimmed(adding, feerate, dust,
|
+ commit_tx_num_untrimmed(adding, feerate, dust_limit,
|
||||||
recipient)
|
recipient)
|
||||||
- commit_tx_num_untrimmed(removing, feerate, dust,
|
- commit_tx_num_untrimmed(removing, feerate, dust_limit,
|
||||||
recipient);
|
recipient);
|
||||||
|
|
||||||
fee = commit_tx_base_fee(feerate, untrimmed);
|
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)
|
bool can_funder_afford_feerate(const struct channel *channel, u32 feerate_per_kw)
|
||||||
{
|
{
|
||||||
struct amount_sat needed, fee;
|
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;
|
size_t untrimmed;
|
||||||
const struct htlc **committed, **adding, **removing;
|
const struct htlc **committed, **adding, **removing;
|
||||||
gather_htlcs(tmpctx, channel, !channel->funder,
|
gather_htlcs(tmpctx, channel, !channel->funder,
|
||||||
&committed, &removing, &adding);
|
&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)
|
!channel->funder)
|
||||||
+ commit_tx_num_untrimmed(adding, feerate_per_kw, dust,
|
+ commit_tx_num_untrimmed(adding, feerate_per_kw, dust_limit,
|
||||||
!channel->funder)
|
!channel->funder)
|
||||||
- commit_tx_num_untrimmed(removing, feerate_per_kw, dust,
|
- commit_tx_num_untrimmed(removing, feerate_per_kw, dust_limit,
|
||||||
!channel->funder);
|
!channel->funder);
|
||||||
|
|
||||||
fee = commit_tx_base_fee(feerate_per_kw, untrimmed);
|
fee = commit_tx_base_fee(feerate_per_kw, untrimmed);
|
||||||
|
|
|
@ -217,13 +217,15 @@ static void report_htlcs(const struct bitcoin_tx *tx,
|
||||||
|
|
||||||
for (i = 0; i < tal_count(htlc_map); i++) {
|
for (i = 0; i < tal_count(htlc_map); i++) {
|
||||||
const struct htlc *htlc = htlc_map[i];
|
const struct htlc *htlc = htlc_map[i];
|
||||||
|
struct amount_msat htlc_amount;
|
||||||
|
|
||||||
if (!htlc)
|
if (!htlc)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
htlc_amount.millisatoshis = htlc->msatoshi;
|
||||||
if (htlc_owner(htlc) == LOCAL) {
|
if (htlc_owner(htlc) == LOCAL) {
|
||||||
htlc_tx[i] = htlc_timeout_tx(htlc_tx, &txid, i,
|
htlc_tx[i] = htlc_timeout_tx(htlc_tx, &txid, i,
|
||||||
htlc->msatoshi,
|
htlc_amount,
|
||||||
htlc->expiry.locktime,
|
htlc->expiry.locktime,
|
||||||
to_self_delay,
|
to_self_delay,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
|
@ -235,7 +237,7 @@ static void report_htlcs(const struct bitcoin_tx *tx,
|
||||||
remote_revocation_key);
|
remote_revocation_key);
|
||||||
} else {
|
} else {
|
||||||
htlc_tx[i] = htlc_success_tx(htlc_tx, &txid, i,
|
htlc_tx[i] = htlc_success_tx(htlc_tx, &txid, i,
|
||||||
htlc->msatoshi,
|
htlc_amount,
|
||||||
to_self_delay,
|
to_self_delay,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
&keyset);
|
&keyset);
|
||||||
|
@ -354,15 +356,17 @@ static void report(struct bitcoin_tx *tx,
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
#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;
|
size_t i;
|
||||||
u64 output_satoshi = 0;
|
struct amount_sat output = AMOUNT_SAT(0), fee;
|
||||||
|
|
||||||
for (i = 0; i < tal_count(tx->output); i++)
|
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 */
|
/* For debugging, we do brute-force increase to find thresholds */
|
||||||
|
@ -424,7 +428,7 @@ int main(void)
|
||||||
setup_locale();
|
setup_locale();
|
||||||
|
|
||||||
struct bitcoin_txid funding_txid;
|
struct bitcoin_txid funding_txid;
|
||||||
u64 funding_amount_satoshi, dust_limit_satoshi;
|
struct amount_sat funding_amount, dust_limit;
|
||||||
u32 feerate_per_kw;
|
u32 feerate_per_kw;
|
||||||
u16 to_self_delay;
|
u16 to_self_delay;
|
||||||
/* x_ prefix means internal vars we used to derive spec */
|
/* x_ prefix means internal vars we used to derive spec */
|
||||||
|
@ -450,7 +454,8 @@ int main(void)
|
||||||
struct keyset keyset;
|
struct keyset keyset;
|
||||||
u8 *wscript;
|
u8 *wscript;
|
||||||
unsigned int funding_output_index;
|
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;
|
const struct htlc **htlcs, **htlc_map, **htlc_map2, **inv_htlcs;
|
||||||
|
|
||||||
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
|
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
|
||||||
|
@ -487,10 +492,10 @@ int main(void)
|
||||||
*/
|
*/
|
||||||
funding_txid = txid_from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be");
|
funding_txid = txid_from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be");
|
||||||
funding_output_index = 0;
|
funding_output_index = 0;
|
||||||
funding_amount_satoshi = 10000000;
|
funding_amount.satoshis = 10000000;
|
||||||
commitment_number = 42;
|
commitment_number = 42;
|
||||||
to_self_delay = 144;
|
to_self_delay = 144;
|
||||||
dust_limit_satoshi = 546;
|
dust_limit.satoshis = 546;
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
print_superverbose = true;
|
print_superverbose = true;
|
||||||
|
@ -689,15 +694,15 @@ int main(void)
|
||||||
* to_remote_msat: 3000000000
|
* to_remote_msat: 3000000000
|
||||||
* local_feerate_per_kw: 15000
|
* local_feerate_per_kw: 15000
|
||||||
*/
|
*/
|
||||||
to_local_msat = 7000000000;
|
to_local.millisatoshis = 7000000000;
|
||||||
to_remote_msat = 3000000000;
|
to_remote.millisatoshis = 3000000000;
|
||||||
feerate_per_kw = 15000;
|
feerate_per_kw = 15000;
|
||||||
printf("\n"
|
printf("\n"
|
||||||
"name: simple commitment tx with no HTLCs\n"
|
"name: simple commitment tx with no HTLCs\n"
|
||||||
"to_local_msat: %"PRIu64"\n"
|
"to_local_msat: %"PRIu64"\n"
|
||||||
"to_remote_msat: %"PRIu64"\n"
|
"to_remote_msat: %"PRIu64"\n"
|
||||||
"local_feerate_per_kw: %u\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_revocation_key = remote_revocation_key;
|
||||||
keyset.self_delayed_payment_key = local_delayedkey;
|
keyset.self_delayed_payment_key = local_delayedkey;
|
||||||
|
@ -708,24 +713,24 @@ int main(void)
|
||||||
|
|
||||||
print_superverbose = true;
|
print_superverbose = true;
|
||||||
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
||||||
funding_amount_satoshi,
|
funding_amount,
|
||||||
LOCAL, to_self_delay,
|
LOCAL, to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
dust_limit_satoshi,
|
dust_limit,
|
||||||
to_local_msat,
|
to_local,
|
||||||
to_remote_msat,
|
to_remote,
|
||||||
NULL, &htlc_map, commitment_number ^ cn_obscurer,
|
NULL, &htlc_map, commitment_number ^ cn_obscurer,
|
||||||
LOCAL);
|
LOCAL);
|
||||||
print_superverbose = false;
|
print_superverbose = false;
|
||||||
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
||||||
funding_amount_satoshi,
|
funding_amount,
|
||||||
REMOTE, to_self_delay,
|
REMOTE, to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
dust_limit_satoshi,
|
dust_limit,
|
||||||
to_local_msat,
|
to_local,
|
||||||
to_remote_msat,
|
to_remote,
|
||||||
NULL, &htlc_map2, commitment_number ^ cn_obscurer,
|
NULL, &htlc_map2, commitment_number ^ cn_obscurer,
|
||||||
REMOTE);
|
REMOTE);
|
||||||
tx_must_be_eq(tx, tx2);
|
tx_must_be_eq(tx, tx2);
|
||||||
|
@ -750,36 +755,36 @@ int main(void)
|
||||||
* to_remote_msat: 3000000000
|
* to_remote_msat: 3000000000
|
||||||
* local_feerate_per_kw: 0
|
* local_feerate_per_kw: 0
|
||||||
*/
|
*/
|
||||||
to_local_msat = 6988000000;
|
to_local.millisatoshis = 6988000000;
|
||||||
to_remote_msat = 3000000000;
|
to_remote.millisatoshis = 3000000000;
|
||||||
feerate_per_kw = 0;
|
feerate_per_kw = 0;
|
||||||
printf("\n"
|
printf("\n"
|
||||||
"name: commitment tx with all 5 htlcs untrimmed (minimum feerate)\n"
|
"name: commitment tx with all 5 htlcs untrimmed (minimum feerate)\n"
|
||||||
"to_local_msat: %"PRIu64"\n"
|
"to_local_msat: %"PRIu64"\n"
|
||||||
"to_remote_msat: %"PRIu64"\n"
|
"to_remote_msat: %"PRIu64"\n"
|
||||||
"local_feerate_per_kw: %u\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;
|
print_superverbose = true;
|
||||||
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
||||||
funding_amount_satoshi,
|
funding_amount,
|
||||||
LOCAL, to_self_delay,
|
LOCAL, to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
dust_limit_satoshi,
|
dust_limit,
|
||||||
to_local_msat,
|
to_local,
|
||||||
to_remote_msat,
|
to_remote,
|
||||||
htlcs, &htlc_map, commitment_number ^ cn_obscurer,
|
htlcs, &htlc_map, commitment_number ^ cn_obscurer,
|
||||||
LOCAL);
|
LOCAL);
|
||||||
print_superverbose = false;
|
print_superverbose = false;
|
||||||
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
||||||
funding_amount_satoshi,
|
funding_amount,
|
||||||
REMOTE, to_self_delay,
|
REMOTE, to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
dust_limit_satoshi,
|
dust_limit,
|
||||||
to_local_msat,
|
to_local,
|
||||||
to_remote_msat,
|
to_remote,
|
||||||
inv_htlcs, &htlc_map2,
|
inv_htlcs, &htlc_map2,
|
||||||
commitment_number ^ cn_obscurer,
|
commitment_number ^ cn_obscurer,
|
||||||
REMOTE);
|
REMOTE);
|
||||||
|
@ -804,25 +809,25 @@ int main(void)
|
||||||
feerate_per_kw = increase(feerate_per_kw);
|
feerate_per_kw = increase(feerate_per_kw);
|
||||||
print_superverbose = false;
|
print_superverbose = false;
|
||||||
newtx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
newtx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
||||||
funding_amount_satoshi,
|
funding_amount,
|
||||||
LOCAL, to_self_delay,
|
LOCAL, to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
dust_limit_satoshi,
|
dust_limit,
|
||||||
to_local_msat,
|
to_local,
|
||||||
to_remote_msat,
|
to_remote,
|
||||||
htlcs, &htlc_map,
|
htlcs, &htlc_map,
|
||||||
commitment_number ^ cn_obscurer,
|
commitment_number ^ cn_obscurer,
|
||||||
LOCAL);
|
LOCAL);
|
||||||
/* This is what it would look like for peer generating it! */
|
/* This is what it would look like for peer generating it! */
|
||||||
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
||||||
funding_amount_satoshi,
|
funding_amount,
|
||||||
REMOTE, to_self_delay,
|
REMOTE, to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
dust_limit_satoshi,
|
dust_limit,
|
||||||
to_local_msat,
|
to_local,
|
||||||
to_remote_msat,
|
to_remote,
|
||||||
inv_htlcs, &htlc_map2,
|
inv_htlcs, &htlc_map2,
|
||||||
commitment_number ^ cn_obscurer,
|
commitment_number ^ cn_obscurer,
|
||||||
REMOTE);
|
REMOTE);
|
||||||
|
@ -830,7 +835,7 @@ int main(void)
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
if (feerate_per_kw % 100000 == 0)
|
if (feerate_per_kw % 100000 == 0)
|
||||||
printf("feerate_per_kw = %u, fees = %"PRIu64"\n",
|
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)) {
|
if (tal_count(newtx->output) == tal_count(tx->output)) {
|
||||||
tal_free(newtx);
|
tal_free(newtx);
|
||||||
continue;
|
continue;
|
||||||
|
@ -843,17 +848,17 @@ int main(void)
|
||||||
"local_feerate_per_kw: %u\n",
|
"local_feerate_per_kw: %u\n",
|
||||||
tal_count(tx->output),
|
tal_count(tx->output),
|
||||||
tal_count(tx->output) > 1 ? "s" : "",
|
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 */
|
/* Recalc with verbosity on */
|
||||||
print_superverbose = true;
|
print_superverbose = true;
|
||||||
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
||||||
funding_amount_satoshi,
|
funding_amount,
|
||||||
LOCAL, to_self_delay,
|
LOCAL, to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
feerate_per_kw-1,
|
feerate_per_kw-1,
|
||||||
dust_limit_satoshi,
|
dust_limit,
|
||||||
to_local_msat,
|
to_local,
|
||||||
to_remote_msat,
|
to_remote,
|
||||||
htlcs, &htlc_map,
|
htlcs, &htlc_map,
|
||||||
commitment_number ^ cn_obscurer,
|
commitment_number ^ cn_obscurer,
|
||||||
LOCAL);
|
LOCAL);
|
||||||
|
@ -879,17 +884,17 @@ int main(void)
|
||||||
"local_feerate_per_kw: %u\n",
|
"local_feerate_per_kw: %u\n",
|
||||||
tal_count(newtx->output),
|
tal_count(newtx->output),
|
||||||
tal_count(newtx->output) > 1 ? "s" : "",
|
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 */
|
/* Recalc with verbosity on */
|
||||||
print_superverbose = true;
|
print_superverbose = true;
|
||||||
newtx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
newtx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
||||||
funding_amount_satoshi,
|
funding_amount,
|
||||||
LOCAL, to_self_delay,
|
LOCAL, to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
dust_limit_satoshi,
|
dust_limit,
|
||||||
to_local_msat,
|
to_local,
|
||||||
to_remote_msat,
|
to_remote,
|
||||||
htlcs, &htlc_map,
|
htlcs, &htlc_map,
|
||||||
commitment_number ^ cn_obscurer,
|
commitment_number ^ cn_obscurer,
|
||||||
LOCAL);
|
LOCAL);
|
||||||
|
@ -917,11 +922,9 @@ int main(void)
|
||||||
/* Now make sure we cover case where funder can't afford the fee;
|
/* Now make sure we cover case where funder can't afford the fee;
|
||||||
* its output cannot go negative! */
|
* its output cannot go negative! */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
struct amount_msat to_local;
|
|
||||||
struct amount_sat base_fee
|
struct amount_sat base_fee
|
||||||
= commit_tx_base_fee(feerate_per_kw, 0);
|
= commit_tx_base_fee(feerate_per_kw, 0);
|
||||||
|
|
||||||
to_local.millisatoshis = to_local_msat;
|
|
||||||
if (amount_msat_greater_eq_sat(to_local, base_fee)) {
|
if (amount_msat_greater_eq_sat(to_local, base_fee)) {
|
||||||
feerate_per_kw++;
|
feerate_per_kw++;
|
||||||
continue;
|
continue;
|
||||||
|
@ -941,15 +944,15 @@ int main(void)
|
||||||
"to_local_msat: %"PRIu64"\n"
|
"to_local_msat: %"PRIu64"\n"
|
||||||
"to_remote_msat: %"PRIu64"\n"
|
"to_remote_msat: %"PRIu64"\n"
|
||||||
"local_feerate_per_kw: %u\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,
|
tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
||||||
funding_amount_satoshi,
|
funding_amount,
|
||||||
LOCAL, to_self_delay,
|
LOCAL, to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
dust_limit_satoshi,
|
dust_limit,
|
||||||
to_local_msat,
|
to_local,
|
||||||
to_remote_msat,
|
to_remote,
|
||||||
htlcs, &htlc_map,
|
htlcs, &htlc_map,
|
||||||
commitment_number ^ cn_obscurer,
|
commitment_number ^ cn_obscurer,
|
||||||
LOCAL);
|
LOCAL);
|
||||||
|
|
|
@ -338,7 +338,7 @@ int main(void)
|
||||||
struct pubkey *unknown;
|
struct pubkey *unknown;
|
||||||
struct bitcoin_tx *raw_tx, **txs, **txs2;
|
struct bitcoin_tx *raw_tx, **txs, **txs2;
|
||||||
struct channel_config *local_config, *remote_config;
|
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 struct htlc **htlc_map, **htlcs;
|
||||||
const u8 *funding_wscript, **wscripts;
|
const u8 *funding_wscript, **wscripts;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
@ -448,13 +448,13 @@ int main(void)
|
||||||
* local_feerate_per_kw: 15000
|
* local_feerate_per_kw: 15000
|
||||||
*/
|
*/
|
||||||
|
|
||||||
to_local_msat = AMOUNT_MSAT(7000000000);
|
to_local = AMOUNT_MSAT(7000000000);
|
||||||
to_remote_msat = AMOUNT_MSAT(3000000000);
|
to_remote = AMOUNT_MSAT(3000000000);
|
||||||
feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 15000;
|
feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 15000;
|
||||||
lchannel = new_full_channel(tmpctx,
|
lchannel = new_full_channel(tmpctx,
|
||||||
&chainparams->genesis_blockhash,
|
&chainparams->genesis_blockhash,
|
||||||
&funding_txid, funding_output_index,
|
&funding_txid, funding_output_index,
|
||||||
funding_amount, to_local_msat,
|
funding_amount, to_local,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
local_config,
|
local_config,
|
||||||
remote_config,
|
remote_config,
|
||||||
|
@ -465,7 +465,7 @@ int main(void)
|
||||||
rchannel = new_full_channel(tmpctx,
|
rchannel = new_full_channel(tmpctx,
|
||||||
&chainparams->genesis_blockhash,
|
&chainparams->genesis_blockhash,
|
||||||
&funding_txid, funding_output_index,
|
&funding_txid, funding_output_index,
|
||||||
funding_amount, to_remote_msat,
|
funding_amount, to_remote,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
remote_config,
|
remote_config,
|
||||||
local_config,
|
local_config,
|
||||||
|
@ -495,13 +495,13 @@ int main(void)
|
||||||
keyset.other_htlc_key = keyset.other_payment_key;
|
keyset.other_htlc_key = keyset.other_payment_key;
|
||||||
|
|
||||||
raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
||||||
funding_amount.satoshis,
|
funding_amount,
|
||||||
LOCAL, remote_config->to_self_delay,
|
LOCAL, remote_config->to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
feerate_per_kw[LOCAL],
|
feerate_per_kw[LOCAL],
|
||||||
local_config->dust_limit.satoshis,
|
local_config->dust_limit,
|
||||||
to_local_msat.millisatoshis,
|
to_local,
|
||||||
to_remote_msat.millisatoshis,
|
to_remote,
|
||||||
NULL, &htlc_map, 0x2bb038521914 ^ 42, LOCAL);
|
NULL, &htlc_map, 0x2bb038521914 ^ 42, LOCAL);
|
||||||
|
|
||||||
txs = channel_txs(tmpctx, &htlc_map, &wscripts,
|
txs = channel_txs(tmpctx, &htlc_map, &wscripts,
|
||||||
|
@ -523,8 +523,8 @@ int main(void)
|
||||||
* to_remote_msat: 3000000000
|
* to_remote_msat: 3000000000
|
||||||
* local_feerate_per_kw: 0
|
* local_feerate_per_kw: 0
|
||||||
*/
|
*/
|
||||||
to_local_msat = AMOUNT_MSAT(6988000000);
|
to_local = AMOUNT_MSAT(6988000000);
|
||||||
to_remote_msat = AMOUNT_MSAT(3000000000);
|
to_remote = AMOUNT_MSAT(3000000000);
|
||||||
feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 0;
|
feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 0;
|
||||||
|
|
||||||
/* Now, BOLT doesn't adjust owed amounts the same way we do
|
/* 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];
|
rchannel->view[REMOTE].feerate_per_kw = feerate_per_kw[REMOTE];
|
||||||
|
|
||||||
raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
|
||||||
funding_amount.satoshis,
|
funding_amount,
|
||||||
LOCAL, remote_config->to_self_delay,
|
LOCAL, remote_config->to_self_delay,
|
||||||
&keyset,
|
&keyset,
|
||||||
feerate_per_kw[LOCAL],
|
feerate_per_kw[LOCAL],
|
||||||
local_config->dust_limit.satoshis,
|
local_config->dust_limit,
|
||||||
to_local_msat.millisatoshis,
|
to_local,
|
||||||
to_remote_msat.millisatoshis,
|
to_remote,
|
||||||
htlcs, &htlc_map,
|
htlcs, &htlc_map,
|
||||||
0x2bb038521914 ^ 42, LOCAL);
|
0x2bb038521914 ^ 42, LOCAL);
|
||||||
|
|
||||||
|
|
|
@ -7,16 +7,16 @@
|
||||||
static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
|
static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
|
||||||
const struct bitcoin_txid *commit_txid,
|
const struct bitcoin_txid *commit_txid,
|
||||||
unsigned int commit_output_number,
|
unsigned int commit_output_number,
|
||||||
u64 msatoshi,
|
struct amount_msat msat,
|
||||||
u16 to_self_delay,
|
u16 to_self_delay,
|
||||||
const struct pubkey *revocation_pubkey,
|
const struct pubkey *revocation_pubkey,
|
||||||
const struct pubkey *local_delayedkey,
|
const struct pubkey *local_delayedkey,
|
||||||
u64 htlc_fee_satoshi,
|
struct amount_sat htlc_fee,
|
||||||
u32 locktime)
|
u32 locktime)
|
||||||
{
|
{
|
||||||
struct bitcoin_tx *tx = bitcoin_tx(ctx, 1, 1);
|
struct bitcoin_tx *tx = bitcoin_tx(ctx, 1, 1);
|
||||||
u8 *wscript;
|
u8 *wscript;
|
||||||
u64 amount;
|
struct amount_sat amount, out_amount;
|
||||||
|
|
||||||
/* BOLT #3:
|
/* BOLT #3:
|
||||||
*
|
*
|
||||||
|
@ -48,8 +48,8 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
|
||||||
tx->input[0].index = commit_output_number;
|
tx->input[0].index = commit_output_number;
|
||||||
|
|
||||||
/* We need amount for signing. */
|
/* We need amount for signing. */
|
||||||
amount = msatoshi / 1000;
|
amount = amount_msat_to_sat_round_down(msat);
|
||||||
tx->input[0].amount = tal_dup(tx, u64, &amount);
|
tx->input[0].amount = tal_dup(tx, u64, &amount.satoshis);
|
||||||
|
|
||||||
/* BOLT #3:
|
/* BOLT #3:
|
||||||
* * `txin[0]` sequence: `0`
|
* * `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
|
* * `txout[0]` script: version-0 P2WSH with witness script as shown
|
||||||
* below
|
* 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,
|
wscript = bitcoin_wscript_htlc_tx(tx, to_self_delay,
|
||||||
revocation_pubkey, local_delayedkey);
|
revocation_pubkey, local_delayedkey);
|
||||||
tx->output[0].script = scriptpubkey_p2wsh(tx, wscript);
|
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,
|
struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
|
||||||
const struct bitcoin_txid *commit_txid,
|
const struct bitcoin_txid *commit_txid,
|
||||||
unsigned int commit_output_number,
|
unsigned int commit_output_number,
|
||||||
u64 htlc_msatoshi,
|
struct amount_msat htlc_msatoshi,
|
||||||
u16 to_self_delay,
|
u16 to_self_delay,
|
||||||
u32 feerate_per_kw,
|
u32 feerate_per_kw,
|
||||||
const struct keyset *keyset)
|
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,
|
struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
|
||||||
const struct bitcoin_txid *commit_txid,
|
const struct bitcoin_txid *commit_txid,
|
||||||
unsigned int commit_output_number,
|
unsigned int commit_output_number,
|
||||||
u64 htlc_msatoshi,
|
struct amount_msat htlc_msatoshi,
|
||||||
u32 cltv_expiry,
|
u32 cltv_expiry,
|
||||||
u16 to_self_delay,
|
u16 to_self_delay,
|
||||||
u32 feerate_per_kw,
|
u32 feerate_per_kw,
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
#ifndef LIGHTNING_COMMON_HTLC_TX_H
|
#ifndef LIGHTNING_COMMON_HTLC_TX_H
|
||||||
#define LIGHTNING_COMMON_HTLC_TX_H
|
#define LIGHTNING_COMMON_HTLC_TX_H
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include <common/amount.h>
|
||||||
#include <common/htlc.h>
|
#include <common/htlc.h>
|
||||||
|
|
||||||
struct keyset;
|
struct keyset;
|
||||||
struct preimage;
|
struct preimage;
|
||||||
struct pubkey;
|
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:
|
/* 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
|
* 1. Multiply `feerate_per_kw` by 663 and divide by 1000 (rounding
|
||||||
* down).
|
* 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:
|
/* 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
|
* 1. Multiply `feerate_per_kw` by 703 and divide by 1000 (rounding
|
||||||
* down).
|
* 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
|
/* 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,
|
struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
|
||||||
const struct bitcoin_txid *commit_txid,
|
const struct bitcoin_txid *commit_txid,
|
||||||
unsigned int commit_output_number,
|
unsigned int commit_output_number,
|
||||||
u64 htlc_msatoshi,
|
struct amount_msat htlc_msatoshi,
|
||||||
u16 to_self_delay,
|
u16 to_self_delay,
|
||||||
u32 feerate_per_kw,
|
u32 feerate_per_kw,
|
||||||
const struct keyset *keyset);
|
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,
|
struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
|
||||||
const struct bitcoin_txid *commit_txid,
|
const struct bitcoin_txid *commit_txid,
|
||||||
unsigned int commit_output_number,
|
unsigned int commit_output_number,
|
||||||
u64 htlc_msatoshi,
|
struct amount_msat htlc_msatoshi,
|
||||||
u32 cltv_expiry,
|
u32 cltv_expiry,
|
||||||
u16 to_self_delay,
|
u16 to_self_delay,
|
||||||
u32 feerate_per_kw,
|
u32 feerate_per_kw,
|
||||||
|
|
|
@ -1150,9 +1150,11 @@ static void handle_preimage(struct tracked_output **outs,
|
||||||
* HTLC-success transaction.
|
* HTLC-success transaction.
|
||||||
*/
|
*/
|
||||||
if (outs[i]->remote_htlc_sig) {
|
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,
|
tx = htlc_success_tx(outs[i], &outs[i]->txid,
|
||||||
outs[i]->outnum,
|
outs[i]->outnum,
|
||||||
outs[i]->satoshi * 1000,
|
htlc_amount,
|
||||||
to_self_delay[LOCAL],
|
to_self_delay[LOCAL],
|
||||||
0,
|
0,
|
||||||
keyset);
|
keyset);
|
||||||
|
@ -1342,6 +1344,9 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out,
|
||||||
struct bitcoin_tx *tx = NULL;
|
struct bitcoin_tx *tx = NULL;
|
||||||
struct bitcoin_signature localsig;
|
struct bitcoin_signature localsig;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
struct amount_msat htlc_amount;
|
||||||
|
|
||||||
|
htlc_amount.millisatoshis = out->satoshi * 1000;
|
||||||
|
|
||||||
assert(tal_count(matches));
|
assert(tal_count(matches));
|
||||||
|
|
||||||
|
@ -1358,7 +1363,7 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out,
|
||||||
* HTLC-timeout transaction.
|
* HTLC-timeout transaction.
|
||||||
*/
|
*/
|
||||||
tx = htlc_timeout_tx(tmpctx, &out->txid, out->outnum,
|
tx = htlc_timeout_tx(tmpctx, &out->txid, out->outnum,
|
||||||
out->satoshi * 1000,
|
htlc_amount,
|
||||||
htlcs[matches[i]].cltv_expiry,
|
htlcs[matches[i]].cltv_expiry,
|
||||||
to_self_delay[LOCAL], 0, keyset);
|
to_self_delay[LOCAL], 0, keyset);
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ u8 *htlc_received_wscript(const tal_t *ctx UNNEEDED,
|
||||||
struct bitcoin_tx *htlc_success_tx(const tal_t *ctx UNNEEDED,
|
struct bitcoin_tx *htlc_success_tx(const tal_t *ctx UNNEEDED,
|
||||||
const struct bitcoin_txid *commit_txid UNNEEDED,
|
const struct bitcoin_txid *commit_txid UNNEEDED,
|
||||||
unsigned int commit_output_number UNNEEDED,
|
unsigned int commit_output_number UNNEEDED,
|
||||||
u64 htlc_msatoshi UNNEEDED,
|
struct amount_msat htlc_msatoshi UNNEEDED,
|
||||||
u16 to_self_delay UNNEEDED,
|
u16 to_self_delay UNNEEDED,
|
||||||
u32 feerate_per_kw UNNEEDED,
|
u32 feerate_per_kw UNNEEDED,
|
||||||
const struct keyset *keyset 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,
|
struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx UNNEEDED,
|
||||||
const struct bitcoin_txid *commit_txid UNNEEDED,
|
const struct bitcoin_txid *commit_txid UNNEEDED,
|
||||||
unsigned int commit_output_number UNNEEDED,
|
unsigned int commit_output_number UNNEEDED,
|
||||||
u64 htlc_msatoshi UNNEEDED,
|
struct amount_msat htlc_msatoshi UNNEEDED,
|
||||||
u32 cltv_expiry UNNEEDED,
|
u32 cltv_expiry UNNEEDED,
|
||||||
u16 to_self_delay UNNEEDED,
|
u16 to_self_delay UNNEEDED,
|
||||||
u32 feerate_per_kw UNNEEDED,
|
u32 feerate_per_kw UNNEEDED,
|
||||||
|
|
Loading…
Add table
Reference in a new issue