From a775b52941e0dee7c74f2e919aea593c4f6114fc Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 20 Jun 2017 16:54:53 +0200 Subject: [PATCH] wallet: Return change satoshis when selecting coins We'd be computing them later most of the time anyway. --- lightningd/build_utxos.c | 9 ++------- wallet/wallet.c | 9 +++++++-- wallet/wallet.h | 3 ++- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lightningd/build_utxos.c b/lightningd/build_utxos.c index fb7ec09fd..76b88a8e4 100644 --- a/lightningd/build_utxos.c +++ b/lightningd/build_utxos.c @@ -172,22 +172,17 @@ const struct utxo **build_utxos(const tal_t *ctx, u32 feerate_per_kw, u64 dust_limit, u64 *change_satoshis, u32 *change_keyindex) { - u64 satoshi_in = 0; u64 fee_estimate = 0; u64 bip32_max_index = db_get_intvar(ld->wallet->db, "bip32_max_index", 0); const struct utxo **utxos = - wallet_select_coins(ctx, ld->wallet, satoshi_out, feerate_per_kw, &fee_estimate); + wallet_select_coins(ctx, ld->wallet, satoshi_out, feerate_per_kw, + &fee_estimate, change_satoshis); /* Oops, didn't have enough coins available */ if (!utxos) return NULL; - /* How much are we actually claiming? */ - for (size_t i=0; iamount; - /* Do we need a change output? */ - *change_satoshis = satoshi_in - (fee_estimate + satoshi_out); if (*change_satoshis < dust_limit) { *change_satoshis = 0; *change_keyindex = 0; diff --git a/wallet/wallet.c b/wallet/wallet.c index 7e25b4042..3e60ea4fa 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -129,17 +129,20 @@ 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 *fee_estimate, u64 *changesatoshi) { 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; tal_add_destructor2(utxos, destroy_utxos, w); - db_begin_transaction(w->db); + if (!db_begin_transaction(w->db)) { + fatal("Unable to begin transaction: %s", w->db->err); + } available = wallet_get_utxos(ctx, w, output_state_available); for (i = 0; i < tal_count(available); i++) { @@ -171,6 +174,8 @@ const struct utxo **wallet_select_coins(const tal_t *ctx, struct wallet *w, } else { /* Commit the db transaction to persist markings */ db_commit_transaction(w->db); + *changesatoshi = satoshi_in - value - *fee_estimate; + } return utxos; } diff --git a/wallet/wallet.h b/wallet/wallet.h index 9f21ea2f7..91e119a27 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -77,7 +77,8 @@ struct utxo **wallet_get_utxos(const tal_t *ctx, struct wallet *w, 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 *fee_estimate, + u64 *change_satoshi); /** * wallet_confirm_utxos - Once we've spent a set of utxos, mark them confirmed.