From 23540fe956a8eae9a5138d8286f7f9b91af3bb7c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 3 Dec 2018 09:32:11 +1030 Subject: [PATCH] common: make funding_tx and withdraw_tx share UTXO code. They both do the same thing: convert utxos into tx inputs. Share code. Signed-off-by: Rusty Russell --- closingd/Makefile | 1 + common/funding_tx.c | 18 ++---------------- common/test/run-funding_tx.c | 35 +++++++++++++++++++++++++++++++++++ common/utxo.c | 25 +++++++++++++++++++++++++ common/utxo.h | 9 +++++++++ common/withdraw_tx.c | 18 ++++-------------- connectd/Makefile | 1 + gossipd/Makefile | 1 + lightningd/test/Makefile | 1 + 9 files changed, 79 insertions(+), 30 deletions(-) diff --git a/closingd/Makefile b/closingd/Makefile index 982c0fef7..639225f8a 100644 --- a/closingd/Makefile +++ b/closingd/Makefile @@ -55,6 +55,7 @@ CLOSINGD_COMMON_OBJS := \ common/gen_peer_status_wire.o \ common/gen_status_wire.o \ common/htlc_wire.o \ + common/key_derive.o \ common/memleak.o \ common/msg_queue.o \ common/peer_billboard.o \ diff --git a/common/funding_tx.c b/common/funding_tx.c index 28a5376e5..d46b5cab7 100644 --- a/common/funding_tx.c +++ b/common/funding_tx.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -21,23 +20,10 @@ struct bitcoin_tx *funding_tx(const tal_t *ctx, const struct pubkey *changekey, const struct ext_key *bip32_base) { - struct bitcoin_tx *tx = bitcoin_tx(ctx, tal_count(utxomap), - change_satoshis ? 2 : 1); u8 *wscript; - size_t i; + struct bitcoin_tx *tx; - for (i = 0; i < tal_count(utxomap); i++) { - tx->input[i].txid = utxomap[i]->txid; - tx->input[i].index = utxomap[i]->outnum; - tx->input[i].amount = tal_dup(tx, u64, &utxomap[i]->amount); - if (utxomap[i]->is_p2sh && bip32_base) { - struct pubkey key; - - bip32_pubkey(bip32_base, &key, utxomap[i]->keyindex); - tx->input[i].script - = bitcoin_scriptsig_p2sh_p2wpkh(tx, &key); - } - } + tx = tx_spending_utxos(ctx, utxomap, bip32_base, change_satoshis != 0); tx->output[0].amount = funding_satoshis; wscript = bitcoin_redeem_2of2(tx, local_fundingkey, remote_fundingkey); diff --git a/common/test/run-funding_tx.c b/common/test/run-funding_tx.c index b39e6ef19..c56b052cf 100644 --- a/common/test/run-funding_tx.c +++ b/common/test/run-funding_tx.c @@ -14,6 +14,41 @@ #include "../key_derive.c" #include "../type_to_string.c" #include "../permute_tx.c" + #include "../utxo.c" + +/* AUTOGENERATED MOCKS START */ +/* Generated stub for fromwire_bitcoin_txid */ +void fromwire_bitcoin_txid(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, + struct bitcoin_txid *txid UNNEEDED) +{ fprintf(stderr, "fromwire_bitcoin_txid called!\n"); abort(); } +/* Generated stub for fromwire_bool */ +bool fromwire_bool(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_bool called!\n"); abort(); } +/* Generated stub for fromwire_pubkey */ +void fromwire_pubkey(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct pubkey *pubkey UNNEEDED) +{ fprintf(stderr, "fromwire_pubkey called!\n"); abort(); } +/* Generated stub for fromwire_u32 */ +u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_u32 called!\n"); abort(); } +/* Generated stub for fromwire_u64 */ +u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_u64 called!\n"); abort(); } +/* Generated stub for towire_bitcoin_txid */ +void towire_bitcoin_txid(u8 **pptr UNNEEDED, const struct bitcoin_txid *txid UNNEEDED) +{ fprintf(stderr, "towire_bitcoin_txid called!\n"); abort(); } +/* Generated stub for towire_bool */ +void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED) +{ fprintf(stderr, "towire_bool called!\n"); abort(); } +/* Generated stub for towire_pubkey */ +void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED) +{ fprintf(stderr, "towire_pubkey called!\n"); abort(); } +/* Generated stub for towire_u32 */ +void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED) +{ fprintf(stderr, "towire_u32 called!\n"); abort(); } +/* Generated stub for towire_u64 */ +void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED) +{ fprintf(stderr, "towire_u64 called!\n"); abort(); } +/* AUTOGENERATED MOCKS END */ #if 0 static struct sha256 sha256_from_hex(const char *hex) diff --git a/common/utxo.c b/common/utxo.c index d1821f155..0132bd727 100644 --- a/common/utxo.c +++ b/common/utxo.c @@ -1,3 +1,5 @@ +#include +#include #include #include @@ -39,3 +41,26 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max) } return utxo; } + +struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx, + const struct utxo **utxos, + const struct ext_key *bip32_base, + bool add_change_output) +{ + struct bitcoin_tx *tx = + bitcoin_tx(ctx, tal_count(utxos), add_change_output ? 2 : 1); + + 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); + if (utxos[i]->is_p2sh && bip32_base) { + struct pubkey key; + bip32_pubkey(bip32_base, &key, utxos[i]->keyindex); + tx->input[i].script = + bitcoin_scriptsig_p2sh_p2wpkh(tx, &key); + } + } + + return tx; +} diff --git a/common/utxo.h b/common/utxo.h index 7f996ffb1..58a82d454 100644 --- a/common/utxo.h +++ b/common/utxo.h @@ -8,6 +8,8 @@ #include #include +struct ext_key; + /* Information needed for their_unilateral/to-us outputs */ struct unilateral_close_info { u64 channel_id; @@ -36,4 +38,11 @@ struct utxo { void towire_utxo(u8 **pptr, const struct utxo *utxo); struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max); + +/* Create a tx, and populate inputs from utxos */ +struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx, + const struct utxo **utxos, + const struct ext_key *bip32_base, + bool add_change_output); + #endif /* LIGHTNING_COMMON_UTXO_H */ diff --git a/common/withdraw_tx.c b/common/withdraw_tx.c index 05432c426..e697b69d0 100644 --- a/common/withdraw_tx.c +++ b/common/withdraw_tx.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -16,19 +15,10 @@ struct bitcoin_tx *withdraw_tx(const tal_t *ctx, const u64 changesat, const struct ext_key *bip32_base) { - struct bitcoin_tx *tx = - bitcoin_tx(ctx, tal_count(utxos), changesat ? 2 : 1); - 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); - if (utxos[i]->is_p2sh && bip32_base) { - struct pubkey key; - bip32_pubkey(bip32_base, &key, utxos[i]->keyindex); - tx->input[i].script = - bitcoin_scriptsig_p2sh_p2wpkh(tx, &key); - } - } + struct bitcoin_tx *tx; + + tx = tx_spending_utxos(ctx, utxos, bip32_base, changesat != 0); + tx->output[0].amount = withdraw_amount; tx->output[0].script = destination; diff --git a/connectd/Makefile b/connectd/Makefile index f624b441d..3da35dfa2 100644 --- a/connectd/Makefile +++ b/connectd/Makefile @@ -49,6 +49,7 @@ CONNECTD_COMMON_OBJS := \ common/dev_disconnect.o \ common/features.o \ common/gen_status_wire.o \ + common/key_derive.o \ common/memleak.o \ common/msg_queue.o \ common/pseudorand.o \ diff --git a/gossipd/Makefile b/gossipd/Makefile index 6f03f5121..b8c96bcab 100644 --- a/gossipd/Makefile +++ b/gossipd/Makefile @@ -49,6 +49,7 @@ GOSSIPD_COMMON_OBJS := \ common/dev_disconnect.o \ common/features.o \ common/gen_status_wire.o \ + common/key_derive.o \ common/memleak.o \ common/msg_queue.o \ common/ping.o \ diff --git a/lightningd/test/Makefile b/lightningd/test/Makefile index 10653ff2c..294106508 100644 --- a/lightningd/test/Makefile +++ b/lightningd/test/Makefile @@ -18,6 +18,7 @@ LIGHTNINGD_TEST_COMMON_OBJS := \ common/memleak.o \ common/msg_queue.o \ common/utils.o \ + common/utxo.o \ common/type_to_string.o \ common/permute_tx.o