Browse Source

bitcoin: use amount_sat/amount_msat.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
confirmed-only
Rusty Russell 6 years ago
parent
commit
948ca470ad
  1. 6
      bitcoin/pullpush.c
  2. 3
      bitcoin/pullpush.h
  3. 16
      bitcoin/tx.c
  4. 5
      bitcoin/tx.h
  5. 5
      channeld/channeld.c
  6. 34
      channeld/commit_tx.c
  7. 6
      cli/test/run-large-input.c
  8. 6
      common/close_tx.c
  9. 13
      common/funding_tx.c
  10. 9
      common/funding_tx.h
  11. 7
      common/htlc_tx.c
  12. 6
      common/initial_commit_tx.c
  13. 4
      common/permute_tx.c
  14. 37
      common/test/run-funding_tx.c
  15. 3
      common/utxo.c
  16. 11
      common/withdraw_tx.c
  17. 7
      common/withdraw_tx.h
  18. 20
      hsmd/hsmd.c
  19. 2
      lightningd/bitcoind.c
  20. 2
      lightningd/chaintopology.c
  21. 2
      lightningd/closing_control.c
  22. 2
      lightningd/gossip_control.c
  23. 2
      lightningd/onchain_control.c
  24. 4
      lightningd/opening_control.c
  25. 49
      onchaind/onchaind.c
  26. 4
      onchaind/test/run-grind_feerate.c
  27. 4
      openingd/openingd.c
  28. 2
      tests/test_misc.py
  29. 8
      wallet/wallet.c

6
bitcoin/pullpush.c

@ -26,6 +26,12 @@ void push_le64(u64 v,
push(&l, sizeof(l), pushp); push(&l, sizeof(l), pushp);
} }
void push_amount_sat(struct amount_sat v,
void (*push)(const void *, size_t, void *), void *pushp)
{
push_le64(v.satoshis, push, pushp);
}
void push_varint_blob(const tal_t *blob, void push_varint_blob(const tal_t *blob,
void (*push)(const void *, size_t, void *), void (*push)(const void *, size_t, void *),
void *pushp) void *pushp)

3
bitcoin/pullpush.h

@ -3,11 +3,14 @@
#include "config.h" #include "config.h"
#include "bitcoin/varint.h" #include "bitcoin/varint.h"
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <common/amount.h>
void push_varint(varint_t v, void push_varint(varint_t v,
void (*push)(const void *, size_t, void *), void *pushp); void (*push)(const void *, size_t, void *), void *pushp);
void push_le32(u32 v, void (*push)(const void *, size_t, void *), void *pushp); void push_le32(u32 v, void (*push)(const void *, size_t, void *), void *pushp);
void push_le64(u64 v, void (*push)(const void *, size_t, void *), void *pushp); void push_le64(u64 v, void (*push)(const void *, size_t, void *), void *pushp);
void push_amount_sat(struct amount_sat v,
void (*push)(const void *, size_t, void *), void *pushp);
void push_varint_blob(const tal_t *blob, void push_varint_blob(const tal_t *blob,
void (*push)(const void *, size_t, void *), void (*push)(const void *, size_t, void *),
void *pushp); void *pushp);

16
bitcoin/tx.c

@ -28,7 +28,7 @@ static void push_tx_input(const struct bitcoin_tx_input *input,
static void push_tx_output(const struct bitcoin_tx_output *output, static void push_tx_output(const struct bitcoin_tx_output *output,
void (*push)(const void *, size_t, void *), void *pushp) void (*push)(const void *, size_t, void *), void *pushp)
{ {
push_le64(output->amount, push, pushp); push_amount_sat(output->amount, push, pushp);
push_varint_blob(output->script, push, pushp); push_varint_blob(output->script, push, pushp);
} }
@ -204,7 +204,7 @@ static void hash_outputs(struct sha256_double *h, const struct bitcoin_tx *tx,
if (sighash_single(sighash_type) && i != input_num) if (sighash_single(sighash_type) && i != input_num)
continue; continue;
push_le64(tx->output[i].amount, push_sha, &ctx); push_amount_sat(tx->output[i].amount, push_sha, &ctx);
push_varint_blob(tx->output[i].script, push_sha, &ctx); push_varint_blob(tx->output[i].script, push_sha, &ctx);
} }
@ -243,7 +243,7 @@ static void hash_for_segwit(struct sha256_ctx *ctx,
push_varint_blob(witness_script, push_sha, ctx); push_varint_blob(witness_script, push_sha, ctx);
/* 6. value of the output spent by this input (8-byte little end) */ /* 6. value of the output spent by this input (8-byte little end) */
push_le64(*tx->input[input_num].amount, push_sha, ctx); push_amount_sat(*tx->input[input_num].amount, push_sha, ctx);
/* 7. nSequence of the input (4-byte little endian) */ /* 7. nSequence of the input (4-byte little endian) */
push_le32(tx->input[input_num].sequence_number, push_sha, ctx); push_le32(tx->input[input_num].sequence_number, push_sha, ctx);
@ -362,6 +362,14 @@ static u64 pull_value(const u8 **cursor, size_t *max)
return amount; return amount;
} }
static struct amount_sat pull_amount_sat(const u8 **cursor, size_t *max)
{
struct amount_sat sat;
sat.satoshis = pull_value(cursor, max);
return sat;
}
/* Pulls a varint which specifies n items of mult size: ensures basic /* Pulls a varint which specifies n items of mult size: ensures basic
* sanity to avoid trivial OOM */ * sanity to avoid trivial OOM */
static u64 pull_length(const u8 **cursor, size_t *max, size_t mult) static u64 pull_length(const u8 **cursor, size_t *max, size_t mult)
@ -393,7 +401,7 @@ static void pull_input(const tal_t *ctx, const u8 **cursor, size_t *max,
static void pull_output(const tal_t *ctx, const u8 **cursor, size_t *max, static void pull_output(const tal_t *ctx, const u8 **cursor, size_t *max,
struct bitcoin_tx_output *output) struct bitcoin_tx_output *output)
{ {
output->amount = pull_value(cursor, max); output->amount = pull_amount_sat(cursor, max);
output->script = tal_arr(ctx, u8, pull_length(cursor, max, 1)); output->script = tal_arr(ctx, u8, pull_length(cursor, max, 1));
pull(cursor, max, output->script, tal_count(output->script)); pull(cursor, max, output->script, tal_count(output->script));
} }

5
bitcoin/tx.h

@ -7,6 +7,7 @@
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/structeq/structeq.h> #include <ccan/structeq/structeq.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <common/amount.h>
struct bitcoin_txid { struct bitcoin_txid {
struct sha256_double shad; struct sha256_double shad;
@ -22,7 +23,7 @@ struct bitcoin_tx {
}; };
struct bitcoin_tx_output { struct bitcoin_tx_output {
u64 amount; struct amount_sat amount;
u8 *script; u8 *script;
}; };
@ -33,7 +34,7 @@ struct bitcoin_tx_input {
u32 sequence_number; u32 sequence_number;
/* Value of the output we're spending (NULL if unknown). */ /* Value of the output we're spending (NULL if unknown). */
u64 *amount; struct amount_sat *amount;
/* Only if BIP141 used. */ /* Only if BIP141 used. */
u8 **witness; u8 **witness;

5
channeld/channeld.c

@ -942,7 +942,7 @@ static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx,
msg = towire_hsm_sign_remote_commitment_tx(NULL, txs[0], msg = towire_hsm_sign_remote_commitment_tx(NULL, txs[0],
&peer->channel->funding_pubkey[REMOTE], &peer->channel->funding_pubkey[REMOTE],
(struct amount_sat){*txs[0]->input[0].amount}); *txs[0]->input[0].amount);
msg = hsm_req(tmpctx, take(msg)); msg = hsm_req(tmpctx, take(msg));
if (!fromwire_hsm_sign_tx_reply(msg, commit_sig)) if (!fromwire_hsm_sign_tx_reply(msg, commit_sig))
@ -980,8 +980,7 @@ static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx,
struct bitcoin_signature sig; struct bitcoin_signature sig;
msg = towire_hsm_sign_remote_htlc_tx(NULL, txs[i + 1], msg = towire_hsm_sign_remote_htlc_tx(NULL, txs[i + 1],
wscripts[i + 1], wscripts[i + 1],
(struct amount_sat) *txs[i+1]->input[0].amount,
{ *txs[i+1]->input[0].amount },
&peer->remote_per_commit); &peer->remote_per_commit);
msg = hsm_req(tmpctx, take(msg)); msg = hsm_req(tmpctx, take(msg));

34
channeld/commit_tx.c

@ -70,10 +70,13 @@ static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8)); ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
wscript = htlc_offered_wscript(tx->output, &ripemd, keyset); wscript = htlc_offered_wscript(tx->output, &ripemd, keyset);
tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount).satoshis; tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount);
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript); tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
SUPERVERBOSE("# HTLC %"PRIu64" offered amount %"PRIu64" wscript %s\n", SUPERVERBOSE("# HTLC %"PRIu64" offered %s wscript %s\n",
htlc->id, tx->output[n].amount, tal_hex(wscript, wscript)); htlc->id,
type_to_string(tmpctx, struct amount_sat,
&tx->output[n].amount),
tal_hex(wscript, wscript));
tal_free(wscript); tal_free(wscript);
} }
@ -86,10 +89,13 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8)); ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset); wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset);
tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount).satoshis; tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount);
tx->output[n].script = scriptpubkey_p2wsh(tx->output, wscript); tx->output[n].script = scriptpubkey_p2wsh(tx->output, wscript);
SUPERVERBOSE("# HTLC %"PRIu64" received amount %"PRIu64" wscript %s\n", SUPERVERBOSE("# HTLC %"PRIu64" received %s wscript %s\n",
htlc->id, tx->output[n].amount, tal_hex(wscript, wscript)); htlc->id,
type_to_string(tmpctx, struct amount_sat,
&tx->output[n].amount),
tal_hex(wscript, wscript));
tal_free(wscript); tal_free(wscript);
} }
@ -217,13 +223,14 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
*/ */
if (amount_msat_greater_eq_sat(self_pay, dust_limit)) { 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.millisatoshis / 1000; tx->output[n].amount = amount_msat_to_sat_round_down(self_pay);
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.
* However, valgrind will warn us something wierd is happening */ * However, valgrind will warn us something wierd is happening */
SUPERVERBOSE("# to-local amount %"PRIu64" wscript %s\n", SUPERVERBOSE("# to-local amount %s wscript %s\n",
tx->output[n].amount, type_to_string(tmpctx, struct amount_sat,
&tx->output[n].amount),
tal_hex(tmpctx, wscript)); tal_hex(tmpctx, wscript));
n++; n++;
} }
@ -242,14 +249,15 @@ 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.millisatoshis / 1000; tx->output[n].amount = amount_msat_to_sat_round_down(other_pay);
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;
/* 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.
* However, valgrind will warn us something wierd is happening */ * However, valgrind will warn us something wierd is happening */
SUPERVERBOSE("# to-remote amount %"PRIu64" P2WPKH(%s)\n", SUPERVERBOSE("# to-remote amount %s P2WPKH(%s)\n",
tx->output[n].amount, type_to_string(tmpctx, struct amount_sat,
&tx->output[n].amount),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
&keyset->other_payment_key)); &keyset->other_payment_key));
n++; n++;
@ -298,7 +306,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, struct amount_sat, &funding);
return tx; return tx;
} }

6
cli/test/run-large-input.c

@ -25,6 +25,12 @@ int test_printf(const char *format, ...);
#undef main #undef main
/* AUTOGENERATED MOCKS START */ /* AUTOGENERATED MOCKS START */
/* Generated stub for amount_sat_eq */
bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); }
/* Generated stub for amount_sat_less */
bool amount_sat_less(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_sat_less called!\n"); abort(); }
/* Generated stub for version_and_exit */ /* Generated stub for version_and_exit */
char *version_and_exit(const void *unused UNNEEDED) char *version_and_exit(const void *unused UNNEEDED)
{ fprintf(stderr, "version_and_exit called!\n"); abort(); } { fprintf(stderr, "version_and_exit called!\n"); abort(); }

6
common/close_tx.c

@ -37,11 +37,11 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
/* Our input spends the anchor tx output. */ /* Our input spends the anchor tx output. */
tx->input[0].txid = *anchor_txid; tx->input[0].txid = *anchor_txid;
tx->input[0].index = anchor_index; tx->input[0].index = anchor_index;
tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis); tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &funding);
if (amount_sat_greater_eq(to_us, dust_limit)) { if (amount_sat_greater_eq(to_us, dust_limit)) {
/* One output is to us. */ /* One output is to us. */
tx->output[num_outputs].amount = to_us.satoshis; tx->output[num_outputs].amount = to_us;
tx->output[num_outputs].script = tal_dup_arr(tx, u8, tx->output[num_outputs].script = tal_dup_arr(tx, u8,
our_script, tal_count(our_script), 0); our_script, tal_count(our_script), 0);
num_outputs++; num_outputs++;
@ -49,7 +49,7 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
if (amount_sat_greater_eq(to_them, dust_limit)) { if (amount_sat_greater_eq(to_them, dust_limit)) {
/* Other output is to them. */ /* Other output is to them. */
tx->output[num_outputs].amount = to_them.satoshis; tx->output[num_outputs].amount = to_them;
tx->output[num_outputs].script = tal_dup_arr(tx, u8, tx->output[num_outputs].script = tal_dup_arr(tx, u8,
their_script, tal_count(their_script), their_script, tal_count(their_script),
0); 0);

13
common/funding_tx.c

@ -13,31 +13,32 @@
struct bitcoin_tx *funding_tx(const tal_t *ctx, struct bitcoin_tx *funding_tx(const tal_t *ctx,
u16 *outnum, u16 *outnum,
const struct utxo **utxomap, const struct utxo **utxomap,
u64 funding_satoshis, struct amount_sat funding,
const struct pubkey *local_fundingkey, const struct pubkey *local_fundingkey,
const struct pubkey *remote_fundingkey, const struct pubkey *remote_fundingkey,
u64 change_satoshis, struct amount_sat change,
const struct pubkey *changekey, const struct pubkey *changekey,
const struct ext_key *bip32_base) const struct ext_key *bip32_base)
{ {
u8 *wscript; u8 *wscript;
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
tx = tx_spending_utxos(ctx, utxomap, bip32_base, change_satoshis != 0); tx = tx_spending_utxos(ctx, utxomap, bip32_base,
!amount_sat_eq(change, AMOUNT_SAT(0)));
tx->output[0].amount = funding_satoshis; tx->output[0].amount = funding;
wscript = bitcoin_redeem_2of2(tx, local_fundingkey, remote_fundingkey); wscript = bitcoin_redeem_2of2(tx, local_fundingkey, remote_fundingkey);
SUPERVERBOSE("# funding witness script = %s\n", SUPERVERBOSE("# funding witness script = %s\n",
tal_hex(wscript, wscript)); tal_hex(wscript, wscript));
tx->output[0].script = scriptpubkey_p2wsh(tx, wscript); tx->output[0].script = scriptpubkey_p2wsh(tx, wscript);
tal_free(wscript); tal_free(wscript);
if (change_satoshis != 0) { if (!amount_sat_eq(change, AMOUNT_SAT(0))) {
const void *map[2]; const void *map[2];
map[0] = int2ptr(0); map[0] = int2ptr(0);
map[1] = int2ptr(1); map[1] = int2ptr(1);
tx->output[1].script = scriptpubkey_p2wpkh(tx, changekey); tx->output[1].script = scriptpubkey_p2wpkh(tx, changekey);
tx->output[1].amount = change_satoshis; tx->output[1].amount = change;
permute_outputs(tx->output, NULL, map); permute_outputs(tx->output, NULL, map);
*outnum = (map[0] == int2ptr(0) ? 0 : 1); *outnum = (map[0] == int2ptr(0) ? 0 : 1);
} else { } else {

9
common/funding_tx.h

@ -3,6 +3,7 @@
#include "config.h" #include "config.h"
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <common/amount.h>
struct bitcoin_tx; struct bitcoin_tx;
struct ext_key; struct ext_key;
@ -15,10 +16,10 @@ struct utxo;
* @ctx: context to tal from. * @ctx: context to tal from.
* @outnum: (out) txout (0 or 1) which is the funding output. * @outnum: (out) txout (0 or 1) which is the funding output.
* @utxomap: (in/out) tal_arr of UTXO pointers to spend (permuted to match) * @utxomap: (in/out) tal_arr of UTXO pointers to spend (permuted to match)
* @funding_satoshis: (in) satoshis to output. * @funding: (in) satoshis to output.
* @local_fundingkey: (in) local key for 2of2 funding output. * @local_fundingkey: (in) local key for 2of2 funding output.
* @remote_fundingkey: (in) remote key for 2of2 funding output. * @remote_fundingkey: (in) remote key for 2of2 funding output.
* @change_satoshis: (in) amount to send as change. * @change: (in) amount to send as change.
* @changekey: (in) key to send change to (only used if change_satoshis != 0). * @changekey: (in) key to send change to (only used if change_satoshis != 0).
* @bip32_base: (in) bip32 base for key derivation, or NULL. * @bip32_base: (in) bip32 base for key derivation, or NULL.
* *
@ -34,10 +35,10 @@ struct utxo;
struct bitcoin_tx *funding_tx(const tal_t *ctx, struct bitcoin_tx *funding_tx(const tal_t *ctx,
u16 *outnum, u16 *outnum,
const struct utxo **utxomap, const struct utxo **utxomap,
u64 funding_satoshis, struct amount_sat funding,
const struct pubkey *local_fundingkey, const struct pubkey *local_fundingkey,
const struct pubkey *remote_fundingkey, const struct pubkey *remote_fundingkey,
u64 change_satoshis, struct amount_sat change,
const struct pubkey *changekey, const struct pubkey *changekey,
const struct ext_key *bip32_base); const struct ext_key *bip32_base);
#endif /* LIGHTNING_COMMON_FUNDING_TX_H */ #endif /* LIGHTNING_COMMON_FUNDING_TX_H */

7
common/htlc_tx.c

@ -16,7 +16,7 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
{ {
struct bitcoin_tx *tx = bitcoin_tx(ctx, 1, 1); struct bitcoin_tx *tx = bitcoin_tx(ctx, 1, 1);
u8 *wscript; u8 *wscript;
struct amount_sat amount, out_amount; struct amount_sat amount;
/* BOLT #3: /* BOLT #3:
* *
@ -49,7 +49,7 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
/* We need amount for signing. */ /* We need amount for signing. */
amount = amount_msat_to_sat_round_down(msat); amount = amount_msat_to_sat_round_down(msat);
tx->input[0].amount = tal_dup(tx, u64, &amount.satoshis); tx->input[0].amount = tal_dup(tx, struct amount_sat, &amount);
/* BOLT #3: /* BOLT #3:
* * `txin[0]` sequence: `0` * * `txin[0]` sequence: `0`
@ -63,10 +63,9 @@ 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
*/ */
if (!amount_sat_sub(&out_amount, amount, htlc_fee)) if (!amount_sat_sub(&tx->output[0].amount, amount, htlc_fee))
abort(); 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);

6
common/initial_commit_tx.c

@ -166,7 +166,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
*/ */
if (amount_msat_greater_eq_sat(self_pay, dust_limit)) { 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 = amount_msat_to_sat_round_down(self_pay).satoshis; tx->output[n].amount = amount_msat_to_sat_round_down(self_pay);
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript); tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
n++; n++;
} }
@ -185,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 * This output sends funds to the other peer and thus is a simple
* P2WPKH to `remotepubkey`. * P2WPKH to `remotepubkey`.
*/ */
tx->output[n].amount = amount_msat_to_sat_round_down(other_pay).satoshis; tx->output[n].amount = amount_msat_to_sat_round_down(other_pay);
tx->output[n].script = scriptpubkey_p2wpkh(tx, tx->output[n].script = scriptpubkey_p2wpkh(tx,
&keyset->other_payment_key); &keyset->other_payment_key);
n++; n++;
@ -234,7 +234,7 @@ struct bitcoin_tx *initial_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, struct amount_sat, &funding);
return tx; return tx;
} }

4
common/permute_tx.c

@ -107,8 +107,8 @@ static bool output_better(const struct bitcoin_tx_output *a,
size_t len, lena, lenb; size_t len, lena, lenb;
int ret; int ret;
if (a->amount != b->amount) if (!amount_sat_eq(a->amount, b->amount))
return a->amount < b->amount; return amount_sat_less(a->amount, b->amount);
/* Lexicographical sort. */ /* Lexicographical sort. */
lena = tal_count(a->script); lena = tal_count(a->script);

37
common/test/run-funding_tx.c

@ -8,13 +8,14 @@
#include <common/utils.h> #include <common/utils.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include "../amount.c"
#define SUPERVERBOSE printf #define SUPERVERBOSE printf
#include "../funding_tx.c" #include "../funding_tx.c"
#undef SUPERVERBOSE #undef SUPERVERBOSE
#include "../key_derive.c" #include "../key_derive.c"
#include "../type_to_string.c" #include "../type_to_string.c"
#include "../permute_tx.c" #include "../permute_tx.c"
#include "../utxo.c" #include "../utxo.c"
/* AUTOGENERATED MOCKS START */ /* AUTOGENERATED MOCKS START */
/* Generated stub for fromwire_amount_sat */ /* Generated stub for fromwire_amount_sat */
@ -87,14 +88,14 @@ int main(void)
setup_locale(); setup_locale();
struct bitcoin_tx *input, *funding; struct bitcoin_tx *input, *funding;
u64 fee; struct amount_sat fee, change;
struct pubkey local_funding_pubkey, remote_funding_pubkey; struct pubkey local_funding_pubkey, remote_funding_pubkey;
struct privkey input_privkey; struct privkey input_privkey;
struct pubkey inputkey; struct pubkey inputkey;
bool testnet; bool testnet;
struct utxo utxo; struct utxo utxo;
const struct utxo **utxomap; const struct utxo **utxomap;
u64 funding_satoshis; struct amount_sat funding_sat;
u16 funding_outnum; u16 funding_outnum;
u8 *subscript; u8 *subscript;
struct bitcoin_signature sig; struct bitcoin_signature sig;
@ -145,27 +146,33 @@ int main(void)
utxo.amount = AMOUNT_SAT(5000000000); utxo.amount = AMOUNT_SAT(5000000000);
utxo.is_p2sh = false; utxo.is_p2sh = false;
utxo.close_info = NULL; utxo.close_info = NULL;
funding_satoshis = 10000000; funding_sat = AMOUNT_SAT(10000000);
fee = 13920; fee = AMOUNT_SAT(13920);
printf("input[0] txid: %s\n", printf("input[0] txid: %s\n",
tal_hexstr(tmpctx, &utxo.txid, sizeof(utxo.txid))); tal_hexstr(tmpctx, &utxo.txid, sizeof(utxo.txid)));
printf("input[0] input: %u\n", utxo.outnum); printf("input[0] input: %u\n", utxo.outnum);
printf("input[0] satoshis: %s\n", printf("input[0] satoshis: %s\n",
type_to_string(tmpctx, struct amount_sat, &utxo.amount)); type_to_string(tmpctx, struct amount_sat, &utxo.amount));
printf("funding satoshis: %"PRIu64"\n", funding_satoshis); printf("funding: %s\n",
type_to_string(tmpctx, struct amount_sat, &funding_sat));
utxomap = tal_arr(tmpctx, const struct utxo *, 1); utxomap = tal_arr(tmpctx, const struct utxo *, 1);
utxomap[0] = &utxo; utxomap[0] = &utxo;
if (!amount_sat_sub(&change, utxo.amount, funding_sat)
|| !amount_sat_sub(&change, change, fee))
abort();
funding = funding_tx(tmpctx, &funding_outnum, utxomap, funding = funding_tx(tmpctx, &funding_outnum, utxomap,
funding_satoshis, funding_sat,
&local_funding_pubkey, &local_funding_pubkey,
&remote_funding_pubkey, &remote_funding_pubkey,
utxo.amount.satoshis - fee - funding_satoshis, change,
&inputkey, NULL); &inputkey, NULL);
printf("# fee: %"PRIu64"\n", fee); printf("# fee: %s\n",
printf("change satoshis: %"PRIu64"\n", type_to_string(tmpctx, struct amount_sat, &fee));
funding->output[!funding_outnum].amount); printf("change: %s\n",
type_to_string(tmpctx, struct amount_sat,
&funding->output[!funding_outnum].amount));
printf("funding output: %u\n", funding_outnum); printf("funding output: %u\n", funding_outnum);

3
common/utxo.c

@ -53,7 +53,8 @@ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx,
for (size_t i = 0; i < tal_count(utxos); i++) { for (size_t i = 0; i < tal_count(utxos); i++) {
tx->input[i].txid = utxos[i]->txid; tx->input[i].txid = utxos[i]->txid;
tx->input[i].index = utxos[i]->outnum; tx->input[i].index = utxos[i]->outnum;
tx->input[i].amount = tal_dup(tx, u64, &utxos[i]->amount.satoshis); tx->input[i].amount = tal_dup(tx, struct amount_sat,
&utxos[i]->amount);
if (utxos[i]->is_p2sh && bip32_base) { if (utxos[i]->is_p2sh && bip32_base) {
struct pubkey key; struct pubkey key;
bip32_pubkey(bip32_base, &key, utxos[i]->keyindex); bip32_pubkey(bip32_base, &key, utxos[i]->keyindex);

11
common/withdraw_tx.c

@ -10,24 +10,25 @@
struct bitcoin_tx *withdraw_tx(const tal_t *ctx, struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
const struct utxo **utxos, const struct utxo **utxos,
u8 *destination, u8 *destination,
const u64 withdraw_amount, struct amount_sat withdraw_amount,
const struct pubkey *changekey, const struct pubkey *changekey,
const u64 changesat, struct amount_sat change,
const struct ext_key *bip32_base) const struct ext_key *bip32_base)
{ {
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
tx = tx_spending_utxos(ctx, utxos, bip32_base, changesat != 0); tx = tx_spending_utxos(ctx, utxos, bip32_base,
!amount_sat_eq(change, AMOUNT_SAT(0)));
tx->output[0].amount = withdraw_amount; tx->output[0].amount = withdraw_amount;
tx->output[0].script = destination; tx->output[0].script = destination;
if (changesat != 0) { if (!amount_sat_eq(change, AMOUNT_SAT(0))) {
const void *map[2]; const void *map[2];
map[0] = int2ptr(0); map[0] = int2ptr(0);
map[1] = int2ptr(1); map[1] = int2ptr(1);
tx->output[1].script = scriptpubkey_p2wpkh(tx, changekey); tx->output[1].script = scriptpubkey_p2wpkh(tx, changekey);
tx->output[1].amount = changesat; tx->output[1].amount = change;
permute_outputs(tx->output, NULL, map); permute_outputs(tx->output, NULL, map);
} }
permute_inputs(tx->input, (const void **)utxos); permute_inputs(tx->input, (const void **)utxos);

7
common/withdraw_tx.h

@ -3,6 +3,7 @@
#include "config.h" #include "config.h"
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <common/amount.h>
struct bitcoin_tx; struct bitcoin_tx;
struct ext_key; struct ext_key;
@ -19,15 +20,15 @@ struct utxo;
* @destination: (in) tal_arr of u8, scriptPubKey to send to. * @destination: (in) tal_arr of u8, scriptPubKey to send to.
* @amount: (in) satoshis to send to the destination * @amount: (in) satoshis to send to the destination
* @changekey: (in) key to send change to (only used if change_satoshis != 0). * @changekey: (in) key to send change to (only used if change_satoshis != 0).
* @changesat: (in) amount to send as change. * @change: (in) amount to send as change.
* @bip32_base: (in) bip32 base for key derivation, or NULL. * @bip32_base: (in) bip32 base for key derivation, or NULL.
*/ */
struct bitcoin_tx *withdraw_tx(const tal_t *ctx, struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
const struct utxo **utxos, const struct utxo **utxos,
u8 *destination, u8 *destination,
const u64 withdraw_amount, struct amount_sat withdraw_amount,
const struct pubkey *changekey, const struct pubkey *changekey,
const u64 changesat, struct amount_sat change,
const struct ext_key *bip32_base); const struct ext_key *bip32_base);
#endif /* LIGHTNING_COMMON_WITHDRAW_TX_H */ #endif /* LIGHTNING_COMMON_WITHDRAW_TX_H */

20
hsmd/hsmd.c

@ -759,7 +759,7 @@ static struct io_plan *handle_sign_commitment_tx(struct io_conn *conn,
* pointer, as we don't always know it (and zero is a valid amount, so * pointer, as we don't always know it (and zero is a valid amount, so
* NULL is better to mean 'unknown' and has the nice property that * NULL is better to mean 'unknown' and has the nice property that
* you'll crash if you assume it's there and you're wrong. */ * you'll crash if you assume it's there and you're wrong. */
tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis); tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &funding);
sign_tx_input(tx, 0, NULL, funding_wscript, sign_tx_input(tx, 0, NULL, funding_wscript,
&secrets.funding_privkey, &secrets.funding_privkey,
&local_funding_pubkey, &local_funding_pubkey,
@ -804,7 +804,7 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
&local_funding_pubkey, &local_funding_pubkey,
&remote_funding_pubkey); &remote_funding_pubkey);
/* Need input amount for signing */ /* Need input amount for signing */
tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis); tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &funding);
sign_tx_input(tx, 0, NULL, funding_wscript, sign_tx_input(tx, 0, NULL, funding_wscript,
&secrets.funding_privkey, &secrets.funding_privkey,
&local_funding_pubkey, &local_funding_pubkey,
@ -853,7 +853,7 @@ static struct io_plan *handle_sign_remote_htlc_tx(struct io_conn *conn,
"Failed deriving htlc pubkey"); "Failed deriving htlc pubkey");
/* Need input amount for signing */ /* Need input amount for signing */
tx->input[0].amount = tal_dup(tx->input, u64, &amount.satoshis); tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &amount);
sign_tx_input(tx, 0, NULL, wscript, &htlc_privkey, &htlc_pubkey, sign_tx_input(tx, 0, NULL, wscript, &htlc_privkey, &htlc_pubkey,
SIGHASH_ALL, &sig); SIGHASH_ALL, &sig);
@ -880,7 +880,7 @@ static struct io_plan *handle_sign_to_us_tx(struct io_conn *conn,
if (tal_count(tx->input) != 1) if (tal_count(tx->input) != 1)
return bad_req_fmt(conn, c, msg_in, "bad txinput count"); return bad_req_fmt(conn, c, msg_in, "bad txinput count");
tx->input[0].amount = tal_dup(tx->input, u64, &input_sat.satoshis); tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &input_sat);
sign_tx_input(tx, 0, NULL, wscript, privkey, &pubkey, SIGHASH_ALL, &sig); sign_tx_input(tx, 0, NULL, wscript, privkey, &pubkey, SIGHASH_ALL, &sig);
return req_reply(conn, c, take(towire_hsm_sign_tx_reply(NULL, &sig))); return req_reply(conn, c, take(towire_hsm_sign_tx_reply(NULL, &sig)));
@ -1079,7 +1079,7 @@ static struct io_plan *handle_sign_local_htlc_tx(struct io_conn *conn,
return bad_req_fmt(conn, c, msg_in, "bad txinput count"); return bad_req_fmt(conn, c, msg_in, "bad txinput count");
/* FIXME: Check that output script is correct! */ /* FIXME: Check that output script is correct! */
tx->input[0].amount = tal_dup(tx->input, u64, &input_sat.satoshis); tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &input_sat);
sign_tx_input(tx, 0, NULL, wscript, &htlc_privkey, &htlc_pubkey, sign_tx_input(tx, 0, NULL, wscript, &htlc_privkey, &htlc_pubkey,
SIGHASH_ALL, &sig); SIGHASH_ALL, &sig);
@ -1194,7 +1194,7 @@ static struct io_plan *handle_sign_mutual_close_tx(struct io_conn *conn,
&local_funding_pubkey, &local_funding_pubkey,
&remote_funding_pubkey); &remote_funding_pubkey);
/* Need input amount for signing */ /* Need input amount for signing */
tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis); tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &funding);
sign_tx_input(tx, 0, NULL, funding_wscript, sign_tx_input(tx, 0, NULL, funding_wscript,
&secrets.funding_privkey, &secrets.funding_privkey,
&local_funding_pubkey, &local_funding_pubkey,
@ -1394,8 +1394,8 @@ static struct io_plan *handle_sign_funding_tx(struct io_conn *conn,
* ccan/cast which ensures the type is correct and * ccan/cast which ensures the type is correct and
* we're not casting something random */ * we're not casting something random */
cast_const2(const struct utxo **, utxos), cast_const2(const struct utxo **, utxos),
satoshi_out.satoshis, &local_pubkey, &remote_pubkey, satoshi_out, &local_pubkey, &remote_pubkey,
change_out.satoshis, changekey, change_out, changekey,
NULL); NULL);
sign_all_inputs(tx, utxos); sign_all_inputs(tx, utxos);
@ -1428,8 +1428,8 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn,
pubkey_from_der(ext.pub_key, sizeof(ext.pub_key), &changekey); pubkey_from_der(ext.pub_key, sizeof(ext.pub_key), &changekey);
tx = withdraw_tx(tmpctx, cast_const2(const struct utxo **, utxos), tx = withdraw_tx(tmpctx, cast_const2(const struct utxo **, utxos),
scriptpubkey, satoshi_out.satoshis, scriptpubkey, satoshi_out,
&changekey, change_out.satoshis, NULL); &changekey, change_out, NULL);
sign_all_inputs(tx, utxos); sign_all_inputs(tx, utxos);

2
lightningd/bitcoind.c

@ -558,7 +558,7 @@ static bool process_gettxout(struct bitcoin_cli *bcli)
bcli_args(tmpctx, bcli), bcli_args(tmpctx, bcli),
(int)bcli->output_bytes, bcli->output); (int)bcli->output_bytes, bcli->output);
if (!json_to_bitcoin_amount(bcli->output, valuetok, &out.amount)) if (!json_to_bitcoin_amount(bcli->output, valuetok, &out.amount.satoshis))
fatal("%s: had bad value (%.*s)?", fatal("%s: had bad value (%.*s)?",
bcli_args(tmpctx, bcli), bcli_args(tmpctx, bcli),
(int)bcli->output_bytes, bcli->output); (int)bcli->output_bytes, bcli->output);

2
lightningd/chaintopology.c

@ -593,7 +593,7 @@ static void topo_add_utxos(struct chain_topology *topo, struct block *b)
if (is_p2wsh(output->script, NULL)) { if (is_p2wsh(output->script, NULL)) {
wallet_utxoset_add(topo->ld->wallet, tx, j, wallet_utxoset_add(topo->ld->wallet, tx, j,
b->height, i, output->script, b->height, i, output->script,
(struct amount_sat){ output->amount }); output->amount);
} }
} }
} }

2
lightningd/closing_control.c

@ -22,7 +22,7 @@ static struct amount_sat calc_tx_fee(struct amount_sat sat_in,
{ {
struct amount_sat fee = sat_in; struct amount_sat fee = sat_in;
for (size_t i = 0; i < tal_count(tx->output); i++) { for (size_t i = 0; i < tal_count(tx->output); i++) {
if (!amount_sat_sub(&fee, fee, (struct amount_sat){tx->output[i].amount})) if (!amount_sat_sub(&fee, fee, tx->output[i].amount))
fatal("Tx spends more than input %s? %s", fatal("Tx spends more than input %s? %s",
type_to_string(tmpctx, struct amount_sat, &sat_in), type_to_string(tmpctx, struct amount_sat, &sat_in),
type_to_string(tmpctx, struct bitcoin_tx, tx)); type_to_string(tmpctx, struct bitcoin_tx, tx));

2
lightningd/gossip_control.c

@ -47,7 +47,7 @@ static void got_txout(struct bitcoind *bitcoind,
/* output will be NULL if it wasn't found */ /* output will be NULL if it wasn't found */
if (output) { if (output) {
script = output->script; script = output->script;
sat = (struct amount_sat){ output->amount}; sat = output->amount;
} else { } else {
script = NULL; script = NULL;
sat = AMOUNT_SAT(0); sat = AMOUNT_SAT(0);

2
lightningd/onchain_control.c

@ -452,7 +452,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
struct amount_sat fee = channel->funding; struct amount_sat fee = channel->funding;
for (size_t i = 0; i < tal_count(channel->last_tx->output); i++) for (size_t i = 0; i < tal_count(channel->last_tx->output); i++)
if (!amount_sat_sub(&fee, fee, if (!amount_sat_sub(&fee, fee,
(struct amount_sat) {channel->last_tx->output[i].amount})) { channel->last_tx->output[i].amount)) {
log_broken(channel->log, "Could not get fee" log_broken(channel->log, "Could not get fee"
" funding %s tx %s", " funding %s tx %s",
type_to_string(tmpctx, type_to_string(tmpctx,

4
lightningd/opening_control.c

@ -334,10 +334,10 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp,
fatal("Error deriving change key %u", fc->wtx.change_key_index); fatal("Error deriving change key %u", fc->wtx.change_key_index);
fundingtx = funding_tx(tmpctx, &funding_outnum, fundingtx = funding_tx(tmpctx, &funding_outnum,
fc->wtx.utxos, fc->wtx.amount.satoshis, fc->wtx.utxos, fc->wtx.amount,
&fc->uc->local_funding_pubkey, &fc->uc->local_funding_pubkey,
&channel_info.remote_fundingkey, &channel_info.remote_fundingkey,
fc->wtx.change.satoshis, &changekey, fc->wtx.change, &changekey,
ld->wallet->bip32_base); ld->wallet->bip32_base);
log_debug(fc->uc->log, "Funding tx has %zi inputs, %zu outputs:", log_debug(fc->uc->log, "Funding tx has %zi inputs, %zu outputs:",

49
onchaind/onchaind.c

@ -114,7 +114,6 @@ static bool grind_htlc_tx_fee(struct amount_sat *fee,
u64 weight) u64 weight)
{ {
struct amount_sat prev_fee = AMOUNT_SAT(UINT64_MAX); struct amount_sat prev_fee = AMOUNT_SAT(UINT64_MAX);
struct amount_sat input_amount = (struct amount_sat){*tx->input[0].amount};
for (u64 i = min_possible_feerate; i <= max_possible_feerate; i++) { for (u64 i = min_possible_feerate; i <= max_possible_feerate; i++) {
/* BOLT #3: /* BOLT #3:
@ -138,10 +137,10 @@ static bool grind_htlc_tx_fee(struct amount_sat *fee,
continue; continue;
prev_fee = *fee; prev_fee = *fee;
if (!amount_sat_sub(&out, input_amount, *fee)) if (!amount_sat_sub(&out, *tx->input[0].amount, *fee))
break; break;
tx->output[0].amount = out.satoshis; tx->output[0].amount = out;
if (!check_tx_sig(tx, 0, NULL, wscript, if (!check_tx_sig(tx, 0, NULL, wscript,
&keyset->other_htlc_key, remotesig)) &keyset->other_htlc_key, remotesig))
continue; continue;
@ -158,7 +157,6 @@ static bool set_htlc_timeout_fee(struct bitcoin_tx *tx,
const u8 *wscript) const u8 *wscript)
{ {
static struct amount_sat fee = AMOUNT_SAT(UINT64_MAX); static struct amount_sat fee = AMOUNT_SAT(UINT64_MAX);
struct amount_sat out;
/* BOLT #3: /* BOLT #3:
* *
@ -170,13 +168,11 @@ static bool set_htlc_timeout_fee(struct bitcoin_tx *tx,
if (amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX))) if (amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX)))
return grind_htlc_tx_fee(&fee, tx, remotesig, wscript, 663); return grind_htlc_tx_fee(&fee, tx, remotesig, wscript, 663);
out.satoshis = tx->output[0].amount; if (!amount_sat_sub(&tx->output[0].amount, tx->output[0].amount, fee))
if (!amount_sat_sub(&out, out, fee))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Cannot deduct htlc-timeout fee %s from tx %s", "Cannot deduct htlc-timeout fee %s from tx %s",
type_to_string(tmpctx, struct amount_sat, &fee), type_to_string(tmpctx, struct amount_sat, &fee),
type_to_string(tmpctx, struct bitcoin_tx, tx)); type_to_string(tmpctx, struct bitcoin_tx, tx));
tx->output[0].amount = out.satoshis;
return check_tx_sig(tx, 0, NULL, wscript, return check_tx_sig(tx, 0, NULL, wscript,
&keyset->other_htlc_key, remotesig); &keyset->other_htlc_key, remotesig);
} }
@ -186,7 +182,6 @@ static void set_htlc_success_fee(struct bitcoin_tx *tx,
const u8 *wscript) const u8 *wscript)
{ {
static struct amount_sat fee = AMOUNT_SAT(UINT64_MAX); static struct amount_sat fee = AMOUNT_SAT(UINT64_MAX);
struct amount_sat out;
/* BOLT #3: /* BOLT #3:
* *
@ -209,13 +204,11 @@ static void set_htlc_success_fee(struct bitcoin_tx *tx,
return; return;
} }
out.satoshis = tx->output[0].amount; if (!amount_sat_sub(&tx->output[0].amount, tx->output[0].amount, fee))
if (!amount_sat_sub(&out, out, fee))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Cannot deduct htlc-success fee %s from tx %s", "Cannot deduct htlc-success fee %s from tx %s",
type_to_string(tmpctx, struct amount_sat, &fee), type_to_string(tmpctx, struct amount_sat, &fee),
type_to_string(tmpctx, struct bitcoin_tx, tx)); type_to_string(tmpctx, struct bitcoin_tx, tx));
tx->output[0].amount = out.satoshis;
if (check_tx_sig(tx, 0, NULL, wscript, if (check_tx_sig(tx, 0, NULL, wscript,
&keyset->other_htlc_key, remotesig)) &keyset->other_htlc_key, remotesig))
@ -256,7 +249,7 @@ static u8 *delayed_payment_to_us(const tal_t *ctx,
{ {
return towire_hsm_sign_delayed_payment_to_us(ctx, commit_num, return towire_hsm_sign_delayed_payment_to_us(ctx, commit_num,
tx, wscript, tx, wscript,
(struct amount_sat){*tx->input[0].amount}); *tx->input[0].amount);
} }
static u8 *remote_htlc_to_us(const tal_t *ctx, static u8 *remote_htlc_to_us(const tal_t *ctx,
@ -266,7 +259,7 @@ static u8 *remote_htlc_to_us(const tal_t *ctx,
return towire_hsm_sign_remote_htlc_to_us(ctx, return towire_hsm_sign_remote_htlc_to_us(ctx,
remote_per_commitment_point, remote_per_commitment_point,
tx, wscript, tx, wscript,
(struct amount_sat){*tx->input[0].amount}); *tx->input[0].amount);
} }
static u8 *penalty_to_us(const tal_t *ctx, static u8 *penalty_to_us(const tal_t *ctx,
@ -274,7 +267,7 @@ static u8 *penalty_to_us(const tal_t *ctx,
const u8 *wscript) const u8 *wscript)
{ {
return towire_hsm_sign_penalty_to_us(ctx, remote_per_commitment_secret, return towire_hsm_sign_penalty_to_us(ctx, remote_per_commitment_secret,
tx, wscript, (struct amount_sat){*tx->input[0].amount}); tx, wscript, *tx->input[0].amount);
} }
/* /*
@ -299,7 +292,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx,
enum tx_type *tx_type) enum tx_type *tx_type)
{ {
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
struct amount_sat fee, min_out, outsat; struct amount_sat fee, min_out;
struct bitcoin_signature sig; struct bitcoin_signature sig;
size_t weight; size_t weight;
u8 *msg; u8 *msg;
@ -309,9 +302,9 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx,
tx->input[0].sequence_number = to_self_delay; tx->input[0].sequence_number = to_self_delay;
tx->input[0].txid = out->txid; tx->input[0].txid = out->txid;
tx->input[0].index = out->outnum; tx->input[0].index = out->outnum;
tx->input[0].amount = tal_dup(tx->input, u64, &out->sat.satoshis); tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &out->sat);
tx->output[0].amount = out->sat.satoshis; tx->output[0].amount = out->sat;
tx->output[0].script = scriptpubkey_p2wpkh(tx->output, tx->output[0].script = scriptpubkey_p2wpkh(tx->output,
&our_wallet_pubkey); &our_wallet_pubkey);
@ -340,14 +333,14 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx,
/* This can only happen if feerate_floor() is still too high; shouldn't /* This can only happen if feerate_floor() is still too high; shouldn't
* happen! */ * happen! */
if (!amount_sat_sub(&outsat, out->sat, fee)) { if (!amount_sat_sub(&tx->output[0].amount, out->sat, fee)) {
outsat = dust_limit; tx->output[0].amount = dust_limit;
status_broken("TX %s can't afford minimal feerate" status_broken("TX %s can't afford minimal feerate"
"; setting output to %s", "; setting output to %s",
tx_type_name(*tx_type), tx_type_name(*tx_type),
type_to_string(tmpctx, struct amount_sat, &outsat)); type_to_string(tmpctx, struct amount_sat,
&tx->output[0].amount));
} }
tx->output[0].amount = outsat.satoshis;
if (!wire_sync_write(HSM_FD, take(hsm_sign_msg(NULL, tx, wscript)))) if (!wire_sync_write(HSM_FD, take(hsm_sign_msg(NULL, tx, wscript))))
status_failed(STATUS_FAIL_HSM_IO, "Writing sign request to hsm"); status_failed(STATUS_FAIL_HSM_IO, "Writing sign request to hsm");
@ -371,7 +364,7 @@ static void hsm_sign_local_htlc_tx(struct bitcoin_tx *tx,
{ {
u8 *msg = towire_hsm_sign_local_htlc_tx(NULL, commit_num, u8 *msg = towire_hsm_sign_local_htlc_tx(NULL, commit_num,
tx, wscript, tx, wscript,
(struct amount_sat){*tx->input[0].amount}); *tx->input[0].amount);
if (!wire_sync_write(HSM_FD, take(msg))) if (!wire_sync_write(HSM_FD, take(msg)))
status_failed(STATUS_FAIL_HSM_IO, status_failed(STATUS_FAIL_HSM_IO,
@ -406,16 +399,14 @@ static struct tracked_output *
u32 tx_blockheight, u32 tx_blockheight,
enum tx_type tx_type, enum tx_type tx_type,
u32 outnum, u32 outnum,
u64 satoshi, struct amount_sat sat,
enum output_type output_type, enum output_type output_type,
const struct htlc_stub *htlc, const struct htlc_stub *htlc,
const u8 *wscript, const u8 *wscript,
const secp256k1_ecdsa_signature *remote_htlc_sig) const secp256k1_ecdsa_signature *remote_htlc_sig)
{ {
struct tracked_output *out = tal(*outs, struct tracked_output); struct tracked_output *out = tal(*outs, struct tracked_output);
struct amount_sat sat;
sat.satoshis = satoshi;
status_trace("Tracking output %u of %s: %s/%s", status_trace("Tracking output %u of %s: %s/%s",
outnum, outnum,
type_to_string(tmpctx, struct bitcoin_txid, txid), type_to_string(tmpctx, struct bitcoin_txid, txid),
@ -2030,7 +2021,7 @@ static void handle_their_cheat(const struct bitcoin_tx *tx,
wire_sync_write(REQ_FD, towire_onchain_add_utxo( wire_sync_write(REQ_FD, towire_onchain_add_utxo(
tmpctx, txid, i, tmpctx, txid, i,
remote_per_commitment_point, remote_per_commitment_point,
(struct amount_sat){tx->output[i].amount}, tx->output[i].amount,
tx_blockheight)); tx_blockheight));
continue; continue;
} }
@ -2242,7 +2233,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
wire_sync_write(REQ_FD, towire_onchain_add_utxo( wire_sync_write(REQ_FD, towire_onchain_add_utxo(
tmpctx, txid, i, tmpctx, txid, i,
remote_per_commitment_point, remote_per_commitment_point,
(struct amount_sat){tx->output[i].amount}, tx->output[i].amount,
tx_blockheight)); tx_blockheight));
continue; continue;
} }
@ -2371,7 +2362,7 @@ static void handle_unknown_commitment(const struct bitcoin_tx *tx,
wire_sync_write(REQ_FD, towire_onchain_add_utxo( wire_sync_write(REQ_FD, towire_onchain_add_utxo(
tmpctx, txid, i, tmpctx, txid, i,
possible_remote_per_commitment_point, possible_remote_per_commitment_point,
(struct amount_sat){tx->output[i].amount}, tx->output[i].amount,
tx_blockheight)); tx_blockheight));
to_us_output = i; to_us_output = i;
} }
@ -2485,7 +2476,7 @@ int main(int argc, char *argv[])
0, /* We don't care about funding blockheight */ 0, /* We don't care about funding blockheight */
FUNDING_TRANSACTION, FUNDING_TRANSACTION,
tx->input[0].index, tx->input[0].index,
funding.satoshis, funding,
FUNDING_OUTPUT, NULL, NULL, NULL); FUNDING_OUTPUT, NULL, NULL, NULL);
status_trace("Remote per-commit point: %s", status_trace("Remote per-commit point: %s",

4
onchaind/test/run-grind_feerate.c

@ -197,8 +197,8 @@ int main(int argc, char *argv[])
setup_tmpctx(); setup_tmpctx();
tx = bitcoin_tx_from_hex(tmpctx, "0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000", tx = bitcoin_tx_from_hex(tmpctx, "0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000",
strlen("0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000")); strlen("0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000"));
tx->input[0].amount = tal(tx, u64); tx->input[0].amount = tal(tx, struct amount_sat);
*tx->input[0].amount = 700000; *tx->input[0].amount = AMOUNT_SAT(700000);
der = tal_hexdata(tmpctx, "30450221009b2e0eef267b94c3899fb0dc7375012e2cee4c10348a068fe78d1b82b4b14036022077c3fad3adac2ddf33f415e45f0daf6658b7a0b09647de4443938ae2dbafe2b9" "01", der = tal_hexdata(tmpctx, "30450221009b2e0eef267b94c3899fb0dc7375012e2cee4c10348a068fe78d1b82b4b14036022077c3fad3adac2ddf33f415e45f0daf6658b7a0b09647de4443938ae2dbafe2b9" "01",
strlen("30450221009b2e0eef267b94c3899fb0dc7375012e2cee4c10348a068fe78d1b82b4b14036022077c3fad3adac2ddf33f415e45f0daf6658b7a0b09647de4443938ae2dbafe2b9" "01")); strlen("30450221009b2e0eef267b94c3899fb0dc7375012e2cee4c10348a068fe78d1b82b4b14036022077c3fad3adac2ddf33f415e45f0daf6658b7a0b09647de4443938ae2dbafe2b9" "01"));
if (!signature_from_der(der, tal_count(der), &sig)) if (!signature_from_der(der, tal_count(der), &sig))

4
openingd/openingd.c

@ -621,10 +621,10 @@ static u8 *funder_channel(struct state *state,
*/ */
funding = funding_tx(state, &state->funding_txout, funding = funding_tx(state, &state->funding_txout,
cast_const2(const struct utxo **, utxos), cast_const2(const struct utxo **, utxos),
state->funding.satoshis, state->funding,
&state->our_funding_pubkey, &state->our_funding_pubkey,
&their_funding_pubkey, &their_funding_pubkey,
change.satoshis, changekey, change, changekey,
bip32_base); bip32_base);
bitcoin_txid(funding, &state->funding_txid); bitcoin_txid(funding, &state->funding_txid);

2
tests/test_misc.py

@ -198,7 +198,7 @@ def test_htlc_sig_persistence(node_factory, bitcoind, executor):
time.sleep(3) time.sleep(3)
bitcoind.generate_block(1) bitcoind.generate_block(1)
l1.daemon.wait_for_logs([ l1.daemon.wait_for_logs([
r'Owning output . (\d+) .SEGWIT. txid', r'Owning output . (\d+)sat .SEGWIT. txid',
]) ])
# We should now have a) the change from funding, b) the # We should now have a) the change from funding, b) the

8
wallet/wallet.c

@ -1144,7 +1144,7 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx,
utxo = tal(w, struct utxo); utxo = tal(w, struct utxo);
utxo->keyindex = index; utxo->keyindex = index;
utxo->is_p2sh = is_p2sh; utxo->is_p2sh = is_p2sh;
utxo->amount.satoshis = tx->output[output].amount; utxo->amount = tx->output[output].amount;
utxo->status = output_state_available; utxo->status = output_state_available;
bitcoin_txid(tx, &utxo->txid); bitcoin_txid(tx, &utxo->txid);
utxo->outnum = output; utxo->outnum = output;
@ -1153,8 +1153,10 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx,
utxo->blockheight = blockheight ? blockheight : NULL; utxo->blockheight = blockheight ? blockheight : NULL;
utxo->spendheight = NULL; utxo->spendheight = NULL;
log_debug(w->log, "Owning output %zu %"PRIu64" (%s) txid %s%s", log_debug(w->log, "Owning output %zu %s (%s) txid %s%s",
output, tx->output[output].amount, output,
type_to_string(tmpctx, struct amount_sat,
&tx->output[output].amount),
is_p2sh ? "P2SH" : "SEGWIT", is_p2sh ? "P2SH" : "SEGWIT",
type_to_string(tmpctx, struct bitcoin_txid, type_to_string(tmpctx, struct bitcoin_txid,
&utxo->txid), blockheight ? " CONFIRMED" : ""); &utxo->txid), blockheight ? " CONFIRMED" : "");

Loading…
Cancel
Save