From 8cd7ebb924e337ac3eccda439f4aad588e2a3af5 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 20 Dec 2017 06:22:45 +1030 Subject: [PATCH] wallet: wallet_select_all to select everything. Signed-off-by: Rusty Russell --- wallet/wallet.c | 66 +++++++++++++++++++++++++++++++++++++------------ wallet/wallet.h | 5 ++++ 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/wallet/wallet.c b/wallet/wallet.c index b6d9aff60..8ec34580c 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -131,20 +131,22 @@ void wallet_confirm_utxos(struct wallet *w, const struct utxo **utxos) } } -const struct utxo **wallet_select_coins(const tal_t *ctx, struct wallet *w, - const u64 value, - const u32 feerate_per_kw, - u64 *fee_estimate, u64 *changesatoshi) +static const struct utxo **wallet_select(const tal_t *ctx, struct wallet *w, + const u64 value, + const u32 feerate_per_kw, + u64 *satoshi_in, + u64 *fee_estimate) { size_t i = 0; struct utxo **available; const struct utxo **utxos = tal_arr(ctx, const struct utxo *, 0); - *fee_estimate = 0; - /* We assume two outputs for the weight. */ - u64 satoshi_in = 0, weight = (4 + (8 + 22) * 2 + 4) * 4; + u64 weight = (4 + (8 + 22) * 2 + 4) * 4; tal_add_destructor2(utxos, destroy_utxos, w); + *fee_estimate = 0; + *satoshi_in = 0; + available = wallet_get_utxos(ctx, w, output_state_available); for (i = 0; i < tal_count(available); i++) { @@ -163,22 +165,54 @@ const struct utxo **wallet_select_coins(const tal_t *ctx, struct wallet *w, /* Account for witness (1 byte count + sig + key */ weight += 1 + (1 + 73 + 1 + 33); *fee_estimate = weight * feerate_per_kw / 1000; - satoshi_in += utxos[i]->amount; - if (satoshi_in >= *fee_estimate + value) + *satoshi_in += utxos[i]->amount; + if (*satoshi_in >= *fee_estimate + value) break; } tal_free(available); - if (satoshi_in < *fee_estimate + value) { - /* Could not collect enough inputs, cleanup and bail */ - utxos = tal_free(utxos); - } else { - *changesatoshi = satoshi_in - value - *fee_estimate; - - } return utxos; } +const struct utxo **wallet_select_coins(const tal_t *ctx, struct wallet *w, + const u64 value, + const u32 feerate_per_kw, + u64 *fee_estimate, u64 *changesatoshi) +{ + u64 satoshi_in; + const struct utxo **utxo; + + utxo = wallet_select(ctx, w, value, feerate_per_kw, + &satoshi_in, fee_estimate); + + /* Couldn't afford it? */ + if (satoshi_in < *fee_estimate + value) + return tal_free(utxo); + + *changesatoshi = satoshi_in - value - *fee_estimate; + return utxo; +} + +const struct utxo **wallet_select_all(const tal_t *ctx, struct wallet *w, + const u32 feerate_per_kw, + u64 *value, + u64 *fee_estimate) +{ + u64 satoshi_in; + const struct utxo **utxo; + + /* Huge value, but won't overflow on addition */ + utxo = wallet_select(ctx, w, (1ULL << 56), feerate_per_kw, + &satoshi_in, fee_estimate); + + /* Can't afford fees? */ + if (*fee_estimate > satoshi_in) + return tal_free(utxo); + + *value = satoshi_in - *fee_estimate; + return utxo; +} + bool wallet_can_spend(struct wallet *w, const u8 *script, u32 *index, bool *output_is_p2sh) { diff --git a/wallet/wallet.h b/wallet/wallet.h index 1013ee432..236ca9707 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -137,6 +137,11 @@ const struct utxo **wallet_select_coins(const tal_t *ctx, struct wallet *w, u64 *fee_estimate, u64 *change_satoshi); +const struct utxo **wallet_select_all(const tal_t *ctx, struct wallet *w, + const u32 feerate_per_kw, + u64 *value, + u64 *fee_estimate); + /** * wallet_confirm_utxos - Once we've spent a set of utxos, mark them confirmed. *