Browse Source

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 <rusty@rustcorp.com.au>
plugin-6
Rusty Russell 6 years ago
committed by Christian Decker
parent
commit
23540fe956
  1. 1
      closingd/Makefile
  2. 18
      common/funding_tx.c
  3. 35
      common/test/run-funding_tx.c
  4. 25
      common/utxo.c
  5. 9
      common/utxo.h
  6. 18
      common/withdraw_tx.c
  7. 1
      connectd/Makefile
  8. 1
      gossipd/Makefile
  9. 1
      lightningd/test/Makefile

1
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 \

18
common/funding_tx.c

@ -3,7 +3,6 @@
#include <bitcoin/script.h>
#include <bitcoin/tx.h>
#include <ccan/ptrint/ptrint.h>
#include <common/key_derive.h>
#include <common/permute_tx.h>
#include <common/utxo.h>
@ -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);

35
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)

25
common/utxo.c

@ -1,3 +1,5 @@
#include <bitcoin/script.h>
#include <common/key_derive.h>
#include <common/utxo.h>
#include <wire/wire.h>
@ -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;
}

9
common/utxo.h

@ -8,6 +8,8 @@
#include <ccan/tal/tal.h>
#include <stdbool.h>
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 */

18
common/withdraw_tx.c

@ -2,7 +2,6 @@
#include <bitcoin/pubkey.h>
#include <bitcoin/script.h>
#include <ccan/ptrint/ptrint.h>
#include <common/key_derive.h>
#include <common/permute_tx.h>
#include <common/utxo.h>
#include <string.h>
@ -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;

1
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 \

1
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 \

1
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

Loading…
Cancel
Save