From 948ca470ad99cc6470d08a84e0db9f041e30b481 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 21 Feb 2019 14:15:55 +1030 Subject: [PATCH] bitcoin: use amount_sat/amount_msat. Signed-off-by: Rusty Russell --- bitcoin/pullpush.c | 6 ++++ bitcoin/pullpush.h | 3 ++ bitcoin/tx.c | 16 +++++++--- bitcoin/tx.h | 5 ++-- channeld/channeld.c | 5 ++-- channeld/commit_tx.c | 34 +++++++++++++-------- cli/test/run-large-input.c | 6 ++++ common/close_tx.c | 6 ++-- common/funding_tx.c | 13 ++++---- common/funding_tx.h | 9 +++--- common/htlc_tx.c | 7 ++--- common/initial_commit_tx.c | 6 ++-- common/permute_tx.c | 4 +-- common/test/run-funding_tx.c | 37 +++++++++++++---------- common/utxo.c | 3 +- common/withdraw_tx.c | 11 +++---- common/withdraw_tx.h | 7 +++-- hsmd/hsmd.c | 20 ++++++------- lightningd/bitcoind.c | 2 +- lightningd/chaintopology.c | 2 +- lightningd/closing_control.c | 2 +- lightningd/gossip_control.c | 2 +- lightningd/onchain_control.c | 2 +- lightningd/opening_control.c | 4 +-- onchaind/onchaind.c | 49 +++++++++++++------------------ onchaind/test/run-grind_feerate.c | 4 +-- openingd/openingd.c | 4 +-- tests/test_misc.py | 2 +- wallet/wallet.c | 8 +++-- 29 files changed, 157 insertions(+), 122 deletions(-) diff --git a/bitcoin/pullpush.c b/bitcoin/pullpush.c index c5e652ae757e..1831a4641b88 100644 --- a/bitcoin/pullpush.c +++ b/bitcoin/pullpush.c @@ -26,6 +26,12 @@ void push_le64(u64 v, 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)(const void *, size_t, void *), void *pushp) diff --git a/bitcoin/pullpush.h b/bitcoin/pullpush.h index 9e9fa22a2a7f..268125abac71 100644 --- a/bitcoin/pullpush.h +++ b/bitcoin/pullpush.h @@ -3,11 +3,14 @@ #include "config.h" #include "bitcoin/varint.h" #include +#include void push_varint(varint_t 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_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)(const void *, size_t, void *), void *pushp); diff --git a/bitcoin/tx.c b/bitcoin/tx.c index bfcbe96df2d5..5a6c0492a6ce 100644 --- a/bitcoin/tx.c +++ b/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, 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); } @@ -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) 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); } @@ -243,7 +243,7 @@ static void hash_for_segwit(struct sha256_ctx *ctx, push_varint_blob(witness_script, push_sha, ctx); /* 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) */ 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; } +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 * sanity to avoid trivial OOM */ 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, 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)); pull(cursor, max, output->script, tal_count(output->script)); } diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 9298ab531589..2e8045f18d9f 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -7,6 +7,7 @@ #include #include #include +#include struct bitcoin_txid { struct sha256_double shad; @@ -22,7 +23,7 @@ struct bitcoin_tx { }; struct bitcoin_tx_output { - u64 amount; + struct amount_sat amount; u8 *script; }; @@ -33,7 +34,7 @@ struct bitcoin_tx_input { u32 sequence_number; /* Value of the output we're spending (NULL if unknown). */ - u64 *amount; + struct amount_sat *amount; /* Only if BIP141 used. */ u8 **witness; diff --git a/channeld/channeld.c b/channeld/channeld.c index 9d429e4b4906..6a16d1322cab 100644 --- a/channeld/channeld.c +++ b/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], &peer->channel->funding_pubkey[REMOTE], - (struct amount_sat){*txs[0]->input[0].amount}); + *txs[0]->input[0].amount); msg = hsm_req(tmpctx, take(msg)); 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; msg = towire_hsm_sign_remote_htlc_tx(NULL, txs[i + 1], wscripts[i + 1], - (struct amount_sat) - { *txs[i+1]->input[0].amount }, + *txs[i+1]->input[0].amount, &peer->remote_per_commit); msg = hsm_req(tmpctx, take(msg)); diff --git a/channeld/commit_tx.c b/channeld/commit_tx.c index b48b17538098..d6e0ec614e45 100644 --- a/channeld/commit_tx.c +++ b/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)); 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); - SUPERVERBOSE("# HTLC %"PRIu64" offered amount %"PRIu64" wscript %s\n", - htlc->id, tx->output[n].amount, tal_hex(wscript, wscript)); + SUPERVERBOSE("# HTLC %"PRIu64" offered %s wscript %s\n", + htlc->id, + type_to_string(tmpctx, struct amount_sat, + &tx->output[n].amount), + tal_hex(wscript, 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)); 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); - SUPERVERBOSE("# HTLC %"PRIu64" received amount %"PRIu64" wscript %s\n", - htlc->id, tx->output[n].amount, tal_hex(wscript, wscript)); + SUPERVERBOSE("# HTLC %"PRIu64" received %s wscript %s\n", + htlc->id, + type_to_string(tmpctx, struct amount_sat, + &tx->output[n].amount), + tal_hex(wscript, 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)) { 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); (*htlcmap)[n] = NULL; /* We don't assign cltvs[n]: if we use it, order doesn't matter. * However, valgrind will warn us something wierd is happening */ - SUPERVERBOSE("# to-local amount %"PRIu64" wscript %s\n", - tx->output[n].amount, + SUPERVERBOSE("# to-local amount %s wscript %s\n", + type_to_string(tmpctx, struct amount_sat, + &tx->output[n].amount), tal_hex(tmpctx, wscript)); 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 * 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, &keyset->other_payment_key); (*htlcmap)[n] = NULL; /* We don't assign cltvs[n]: if we use it, order doesn't matter. * However, valgrind will warn us something wierd is happening */ - SUPERVERBOSE("# to-remote amount %"PRIu64" P2WPKH(%s)\n", - tx->output[n].amount, + SUPERVERBOSE("# to-remote amount %s P2WPKH(%s)\n", + type_to_string(tmpctx, struct amount_sat, + &tx->output[n].amount), type_to_string(tmpctx, struct pubkey, &keyset->other_payment_key)); n++; @@ -298,7 +306,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF)); /* Input amount needed for signature code. */ - tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis); + tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &funding); return tx; } diff --git a/cli/test/run-large-input.c b/cli/test/run-large-input.c index bedf3d5374fd..594644223d07 100644 --- a/cli/test/run-large-input.c +++ b/cli/test/run-large-input.c @@ -25,6 +25,12 @@ int test_printf(const char *format, ...); #undef main /* 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 */ char *version_and_exit(const void *unused UNNEEDED) { fprintf(stderr, "version_and_exit called!\n"); abort(); } diff --git a/common/close_tx.c b/common/close_tx.c index 56e3e081c322..a4318059ac64 100644 --- a/common/close_tx.c +++ b/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. */ tx->input[0].txid = *anchor_txid; 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)) { /* 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, our_script, tal_count(our_script), 0); 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)) { /* 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, their_script, tal_count(their_script), 0); diff --git a/common/funding_tx.c b/common/funding_tx.c index d46b5cab74ba..f4385aff4791 100644 --- a/common/funding_tx.c +++ b/common/funding_tx.c @@ -13,31 +13,32 @@ struct bitcoin_tx *funding_tx(const tal_t *ctx, u16 *outnum, const struct utxo **utxomap, - u64 funding_satoshis, + struct amount_sat funding, const struct pubkey *local_fundingkey, const struct pubkey *remote_fundingkey, - u64 change_satoshis, + struct amount_sat change, const struct pubkey *changekey, const struct ext_key *bip32_base) { u8 *wscript; 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); SUPERVERBOSE("# funding witness script = %s\n", tal_hex(wscript, wscript)); tx->output[0].script = scriptpubkey_p2wsh(tx, wscript); tal_free(wscript); - if (change_satoshis != 0) { + if (!amount_sat_eq(change, AMOUNT_SAT(0))) { const void *map[2]; map[0] = int2ptr(0); map[1] = int2ptr(1); 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); *outnum = (map[0] == int2ptr(0) ? 0 : 1); } else { diff --git a/common/funding_tx.h b/common/funding_tx.h index 7fd89616b72c..22e48cecb5b7 100644 --- a/common/funding_tx.h +++ b/common/funding_tx.h @@ -3,6 +3,7 @@ #include "config.h" #include #include +#include struct bitcoin_tx; struct ext_key; @@ -15,10 +16,10 @@ struct utxo; * @ctx: context to tal from. * @outnum: (out) txout (0 or 1) which is the funding output. * @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. * @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). * @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, u16 *outnum, const struct utxo **utxomap, - u64 funding_satoshis, + struct amount_sat funding, const struct pubkey *local_fundingkey, const struct pubkey *remote_fundingkey, - u64 change_satoshis, + struct amount_sat change, const struct pubkey *changekey, const struct ext_key *bip32_base); #endif /* LIGHTNING_COMMON_FUNDING_TX_H */ diff --git a/common/htlc_tx.c b/common/htlc_tx.c index af018672062d..2689b38a726e 100644 --- a/common/htlc_tx.c +++ b/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); u8 *wscript; - struct amount_sat amount, out_amount; + struct amount_sat amount; /* BOLT #3: * @@ -49,7 +49,7 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx, /* We need amount for signing. */ 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: * * `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 * below */ - if (!amount_sat_sub(&out_amount, amount, htlc_fee)) + if (!amount_sat_sub(&tx->output[0].amount, amount, htlc_fee)) abort(); - tx->output[0].amount = out_amount.satoshis; wscript = bitcoin_wscript_htlc_tx(tx, to_self_delay, revocation_pubkey, local_delayedkey); tx->output[0].script = scriptpubkey_p2wsh(tx, wscript); diff --git a/common/initial_commit_tx.c b/common/initial_commit_tx.c index 372f30603f2a..592ecff48f60 100644 --- a/common/initial_commit_tx.c +++ b/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)) { 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); 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 * 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, &keyset->other_payment_key); n++; @@ -234,7 +234,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF)); /* Input amount needed for signature code. */ - tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis); + tx->input[0].amount = tal_dup(tx->input, struct amount_sat, &funding); return tx; } diff --git a/common/permute_tx.c b/common/permute_tx.c index e2e8138e96a2..909ffdf1804f 100644 --- a/common/permute_tx.c +++ b/common/permute_tx.c @@ -107,8 +107,8 @@ static bool output_better(const struct bitcoin_tx_output *a, size_t len, lena, lenb; int ret; - if (a->amount != b->amount) - return a->amount < b->amount; + if (!amount_sat_eq(a->amount, b->amount)) + return amount_sat_less(a->amount, b->amount); /* Lexicographical sort. */ lena = tal_count(a->script); diff --git a/common/test/run-funding_tx.c b/common/test/run-funding_tx.c index 76bdac98c748..275fd11a8b70 100644 --- a/common/test/run-funding_tx.c +++ b/common/test/run-funding_tx.c @@ -8,13 +8,14 @@ #include #include #include +#include "../amount.c" #define SUPERVERBOSE printf - #include "../funding_tx.c" +#include "../funding_tx.c" #undef SUPERVERBOSE - #include "../key_derive.c" - #include "../type_to_string.c" - #include "../permute_tx.c" - #include "../utxo.c" +#include "../key_derive.c" +#include "../type_to_string.c" +#include "../permute_tx.c" +#include "../utxo.c" /* AUTOGENERATED MOCKS START */ /* Generated stub for fromwire_amount_sat */ @@ -87,14 +88,14 @@ int main(void) setup_locale(); struct bitcoin_tx *input, *funding; - u64 fee; + struct amount_sat fee, change; struct pubkey local_funding_pubkey, remote_funding_pubkey; struct privkey input_privkey; struct pubkey inputkey; bool testnet; struct utxo utxo; const struct utxo **utxomap; - u64 funding_satoshis; + struct amount_sat funding_sat; u16 funding_outnum; u8 *subscript; struct bitcoin_signature sig; @@ -145,27 +146,33 @@ int main(void) utxo.amount = AMOUNT_SAT(5000000000); utxo.is_p2sh = false; utxo.close_info = NULL; - funding_satoshis = 10000000; - fee = 13920; + funding_sat = AMOUNT_SAT(10000000); + fee = AMOUNT_SAT(13920); printf("input[0] txid: %s\n", tal_hexstr(tmpctx, &utxo.txid, sizeof(utxo.txid))); printf("input[0] input: %u\n", utxo.outnum); printf("input[0] satoshis: %s\n", 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[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_satoshis, + funding_sat, &local_funding_pubkey, &remote_funding_pubkey, - utxo.amount.satoshis - fee - funding_satoshis, + change, &inputkey, NULL); - printf("# fee: %"PRIu64"\n", fee); - printf("change satoshis: %"PRIu64"\n", - funding->output[!funding_outnum].amount); + printf("# fee: %s\n", + type_to_string(tmpctx, struct amount_sat, &fee)); + printf("change: %s\n", + type_to_string(tmpctx, struct amount_sat, + &funding->output[!funding_outnum].amount)); printf("funding output: %u\n", funding_outnum); diff --git a/common/utxo.c b/common/utxo.c index 7cb609a3cfaf..dcbccbbf01a6 100644 --- a/common/utxo.c +++ b/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++) { tx->input[i].txid = utxos[i]->txid; 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) { struct pubkey key; bip32_pubkey(bip32_base, &key, utxos[i]->keyindex); diff --git a/common/withdraw_tx.c b/common/withdraw_tx.c index e697b69d0a2b..8cd9d52bd559 100644 --- a/common/withdraw_tx.c +++ b/common/withdraw_tx.c @@ -10,24 +10,25 @@ struct bitcoin_tx *withdraw_tx(const tal_t *ctx, const struct utxo **utxos, u8 *destination, - const u64 withdraw_amount, + struct amount_sat withdraw_amount, const struct pubkey *changekey, - const u64 changesat, + struct amount_sat change, const struct ext_key *bip32_base) { 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].script = destination; - if (changesat != 0) { + if (!amount_sat_eq(change, AMOUNT_SAT(0))) { const void *map[2]; map[0] = int2ptr(0); map[1] = int2ptr(1); 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_inputs(tx->input, (const void **)utxos); diff --git a/common/withdraw_tx.h b/common/withdraw_tx.h index 5569fafe3563..608e68b14af0 100644 --- a/common/withdraw_tx.h +++ b/common/withdraw_tx.h @@ -3,6 +3,7 @@ #include "config.h" #include #include +#include struct bitcoin_tx; struct ext_key; @@ -19,15 +20,15 @@ struct utxo; * @destination: (in) tal_arr of u8, scriptPubKey to send to. * @amount: (in) satoshis to send to the destination * @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. */ struct bitcoin_tx *withdraw_tx(const tal_t *ctx, const struct utxo **utxos, u8 *destination, - const u64 withdraw_amount, + struct amount_sat withdraw_amount, const struct pubkey *changekey, - const u64 changesat, + struct amount_sat change, const struct ext_key *bip32_base); #endif /* LIGHTNING_COMMON_WITHDRAW_TX_H */ diff --git a/hsmd/hsmd.c b/hsmd/hsmd.c index e08a8411bc12..ede8e728f558 100644 --- a/hsmd/hsmd.c +++ b/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 * 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. */ - 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, &secrets.funding_privkey, &local_funding_pubkey, @@ -804,7 +804,7 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn, &local_funding_pubkey, &remote_funding_pubkey); /* 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, &secrets.funding_privkey, &local_funding_pubkey, @@ -853,7 +853,7 @@ static struct io_plan *handle_sign_remote_htlc_tx(struct io_conn *conn, "Failed deriving htlc pubkey"); /* 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, 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) 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); 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"); /* 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, SIGHASH_ALL, &sig); @@ -1194,7 +1194,7 @@ static struct io_plan *handle_sign_mutual_close_tx(struct io_conn *conn, &local_funding_pubkey, &remote_funding_pubkey); /* 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, &secrets.funding_privkey, &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 * we're not casting something random */ cast_const2(const struct utxo **, utxos), - satoshi_out.satoshis, &local_pubkey, &remote_pubkey, - change_out.satoshis, changekey, + satoshi_out, &local_pubkey, &remote_pubkey, + change_out, changekey, NULL); 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); tx = withdraw_tx(tmpctx, cast_const2(const struct utxo **, utxos), - scriptpubkey, satoshi_out.satoshis, - &changekey, change_out.satoshis, NULL); + scriptpubkey, satoshi_out, + &changekey, change_out, NULL); sign_all_inputs(tx, utxos); diff --git a/lightningd/bitcoind.c b/lightningd/bitcoind.c index bbf7057294a0..5292e0c25aca 100644 --- a/lightningd/bitcoind.c +++ b/lightningd/bitcoind.c @@ -558,7 +558,7 @@ static bool process_gettxout(struct bitcoin_cli *bcli) bcli_args(tmpctx, bcli), (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)?", bcli_args(tmpctx, bcli), (int)bcli->output_bytes, bcli->output); diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index a824f6948986..67299c6ef278 100644 --- a/lightningd/chaintopology.c +++ b/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)) { wallet_utxoset_add(topo->ld->wallet, tx, j, b->height, i, output->script, - (struct amount_sat){ output->amount }); + output->amount); } } } diff --git a/lightningd/closing_control.c b/lightningd/closing_control.c index 2504d46b0598..54a23c342dce 100644 --- a/lightningd/closing_control.c +++ b/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; 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", type_to_string(tmpctx, struct amount_sat, &sat_in), type_to_string(tmpctx, struct bitcoin_tx, tx)); diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 19cd9dab885d..8e7cd5f5430f 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -47,7 +47,7 @@ static void got_txout(struct bitcoind *bitcoind, /* output will be NULL if it wasn't found */ if (output) { script = output->script; - sat = (struct amount_sat){ output->amount}; + sat = output->amount; } else { script = NULL; sat = AMOUNT_SAT(0); diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index e3f6522972f7..bd715a27942a 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -452,7 +452,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel, struct amount_sat fee = channel->funding; for (size_t i = 0; i < tal_count(channel->last_tx->output); i++) 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" " funding %s tx %s", type_to_string(tmpctx, diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 8aa6ce5c261a..8322a0973006 100644 --- a/lightningd/opening_control.c +++ b/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); fundingtx = funding_tx(tmpctx, &funding_outnum, - fc->wtx.utxos, fc->wtx.amount.satoshis, + fc->wtx.utxos, fc->wtx.amount, &fc->uc->local_funding_pubkey, &channel_info.remote_fundingkey, - fc->wtx.change.satoshis, &changekey, + fc->wtx.change, &changekey, ld->wallet->bip32_base); log_debug(fc->uc->log, "Funding tx has %zi inputs, %zu outputs:", diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index eb071120a988..515879bce618 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -114,7 +114,6 @@ static bool grind_htlc_tx_fee(struct amount_sat *fee, u64 weight) { 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++) { /* BOLT #3: @@ -138,10 +137,10 @@ static bool grind_htlc_tx_fee(struct amount_sat *fee, continue; prev_fee = *fee; - if (!amount_sat_sub(&out, input_amount, *fee)) + if (!amount_sat_sub(&out, *tx->input[0].amount, *fee)) break; - tx->output[0].amount = out.satoshis; + tx->output[0].amount = out; if (!check_tx_sig(tx, 0, NULL, wscript, &keyset->other_htlc_key, remotesig)) continue; @@ -158,7 +157,6 @@ static bool set_htlc_timeout_fee(struct bitcoin_tx *tx, const u8 *wscript) { static struct amount_sat fee = AMOUNT_SAT(UINT64_MAX); - struct amount_sat out; /* 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))) return grind_htlc_tx_fee(&fee, tx, remotesig, wscript, 663); - out.satoshis = tx->output[0].amount; - if (!amount_sat_sub(&out, out, fee)) + if (!amount_sat_sub(&tx->output[0].amount, tx->output[0].amount, fee)) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Cannot deduct htlc-timeout fee %s from tx %s", type_to_string(tmpctx, struct amount_sat, &fee), type_to_string(tmpctx, struct bitcoin_tx, tx)); - tx->output[0].amount = out.satoshis; return check_tx_sig(tx, 0, NULL, wscript, &keyset->other_htlc_key, remotesig); } @@ -186,7 +182,6 @@ static void set_htlc_success_fee(struct bitcoin_tx *tx, const u8 *wscript) { static struct amount_sat fee = AMOUNT_SAT(UINT64_MAX); - struct amount_sat out; /* BOLT #3: * @@ -209,13 +204,11 @@ static void set_htlc_success_fee(struct bitcoin_tx *tx, return; } - out.satoshis = tx->output[0].amount; - if (!amount_sat_sub(&out, out, fee)) + if (!amount_sat_sub(&tx->output[0].amount, tx->output[0].amount, fee)) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Cannot deduct htlc-success fee %s from tx %s", type_to_string(tmpctx, struct amount_sat, &fee), type_to_string(tmpctx, struct bitcoin_tx, tx)); - tx->output[0].amount = out.satoshis; if (check_tx_sig(tx, 0, NULL, wscript, &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, tx, wscript, - (struct amount_sat){*tx->input[0].amount}); + *tx->input[0].amount); } 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, remote_per_commitment_point, tx, wscript, - (struct amount_sat){*tx->input[0].amount}); + *tx->input[0].amount); } 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) { 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) { struct bitcoin_tx *tx; - struct amount_sat fee, min_out, outsat; + struct amount_sat fee, min_out; struct bitcoin_signature sig; size_t weight; 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].txid = out->txid; 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, &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 * happen! */ - if (!amount_sat_sub(&outsat, out->sat, fee)) { - outsat = dust_limit; + if (!amount_sat_sub(&tx->output[0].amount, out->sat, fee)) { + tx->output[0].amount = dust_limit; status_broken("TX %s can't afford minimal feerate" "; setting output to %s", 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)))) 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, tx, wscript, - (struct amount_sat){*tx->input[0].amount}); + *tx->input[0].amount); if (!wire_sync_write(HSM_FD, take(msg))) status_failed(STATUS_FAIL_HSM_IO, @@ -406,16 +399,14 @@ static struct tracked_output * u32 tx_blockheight, enum tx_type tx_type, u32 outnum, - u64 satoshi, + struct amount_sat sat, enum output_type output_type, const struct htlc_stub *htlc, const u8 *wscript, const secp256k1_ecdsa_signature *remote_htlc_sig) { 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", outnum, 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( tmpctx, txid, i, remote_per_commitment_point, - (struct amount_sat){tx->output[i].amount}, + tx->output[i].amount, tx_blockheight)); continue; } @@ -2242,7 +2233,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, wire_sync_write(REQ_FD, towire_onchain_add_utxo( tmpctx, txid, i, remote_per_commitment_point, - (struct amount_sat){tx->output[i].amount}, + tx->output[i].amount, tx_blockheight)); continue; } @@ -2371,7 +2362,7 @@ static void handle_unknown_commitment(const struct bitcoin_tx *tx, wire_sync_write(REQ_FD, towire_onchain_add_utxo( tmpctx, txid, i, possible_remote_per_commitment_point, - (struct amount_sat){tx->output[i].amount}, + tx->output[i].amount, tx_blockheight)); to_us_output = i; } @@ -2485,7 +2476,7 @@ int main(int argc, char *argv[]) 0, /* We don't care about funding blockheight */ FUNDING_TRANSACTION, tx->input[0].index, - funding.satoshis, + funding, FUNDING_OUTPUT, NULL, NULL, NULL); status_trace("Remote per-commit point: %s", diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index 5d28ac6edc14..555a1728f263 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -197,8 +197,8 @@ int main(int argc, char *argv[]) setup_tmpctx(); tx = bitcoin_tx_from_hex(tmpctx, "0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000", strlen("0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000")); - tx->input[0].amount = tal(tx, u64); - *tx->input[0].amount = 700000; + tx->input[0].amount = tal(tx, struct amount_sat); + *tx->input[0].amount = AMOUNT_SAT(700000); der = tal_hexdata(tmpctx, "30450221009b2e0eef267b94c3899fb0dc7375012e2cee4c10348a068fe78d1b82b4b14036022077c3fad3adac2ddf33f415e45f0daf6658b7a0b09647de4443938ae2dbafe2b9" "01", strlen("30450221009b2e0eef267b94c3899fb0dc7375012e2cee4c10348a068fe78d1b82b4b14036022077c3fad3adac2ddf33f415e45f0daf6658b7a0b09647de4443938ae2dbafe2b9" "01")); if (!signature_from_der(der, tal_count(der), &sig)) diff --git a/openingd/openingd.c b/openingd/openingd.c index e50251b5b12f..8107fc849c60 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -621,10 +621,10 @@ static u8 *funder_channel(struct state *state, */ funding = funding_tx(state, &state->funding_txout, cast_const2(const struct utxo **, utxos), - state->funding.satoshis, + state->funding, &state->our_funding_pubkey, &their_funding_pubkey, - change.satoshis, changekey, + change, changekey, bip32_base); bitcoin_txid(funding, &state->funding_txid); diff --git a/tests/test_misc.py b/tests/test_misc.py index a4283c8f3dae..42509e7b007c 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -198,7 +198,7 @@ def test_htlc_sig_persistence(node_factory, bitcoind, executor): time.sleep(3) bitcoind.generate_block(1) 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 diff --git a/wallet/wallet.c b/wallet/wallet.c index e29bc60567bb..9bbb62d16ccb 100644 --- a/wallet/wallet.c +++ b/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->keyindex = index; utxo->is_p2sh = is_p2sh; - utxo->amount.satoshis = tx->output[output].amount; + utxo->amount = tx->output[output].amount; utxo->status = output_state_available; bitcoin_txid(tx, &utxo->txid); 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->spendheight = NULL; - log_debug(w->log, "Owning output %zu %"PRIu64" (%s) txid %s%s", - output, tx->output[output].amount, + log_debug(w->log, "Owning output %zu %s (%s) txid %s%s", + output, + type_to_string(tmpctx, struct amount_sat, + &tx->output[output].amount), is_p2sh ? "P2SH" : "SEGWIT", type_to_string(tmpctx, struct bitcoin_txid, &utxo->txid), blockheight ? " CONFIRMED" : "");