Browse Source

wallet: Return change satoshis when selecting coins

We'd be computing them later most of the time anyway.
ppa-0.6.1
Christian Decker 8 years ago
committed by Rusty Russell
parent
commit
a775b52941
  1. 9
      lightningd/build_utxos.c
  2. 9
      wallet/wallet.c
  3. 3
      wallet/wallet.h

9
lightningd/build_utxos.c

@ -172,22 +172,17 @@ const struct utxo **build_utxos(const tal_t *ctx,
u32 feerate_per_kw, u64 dust_limit, u32 feerate_per_kw, u64 dust_limit,
u64 *change_satoshis, u32 *change_keyindex) u64 *change_satoshis, u32 *change_keyindex)
{ {
u64 satoshi_in = 0;
u64 fee_estimate = 0; u64 fee_estimate = 0;
u64 bip32_max_index = db_get_intvar(ld->wallet->db, "bip32_max_index", 0); u64 bip32_max_index = db_get_intvar(ld->wallet->db, "bip32_max_index", 0);
const struct utxo **utxos = 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 */ /* Oops, didn't have enough coins available */
if (!utxos) if (!utxos)
return NULL; return NULL;
/* How much are we actually claiming? */
for (size_t i=0; i<tal_count(utxos); i++)
satoshi_in += utxos[i]->amount;
/* Do we need a change output? */ /* Do we need a change output? */
*change_satoshis = satoshi_in - (fee_estimate + satoshi_out);
if (*change_satoshis < dust_limit) { if (*change_satoshis < dust_limit) {
*change_satoshis = 0; *change_satoshis = 0;
*change_keyindex = 0; *change_keyindex = 0;

9
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 struct utxo **wallet_select_coins(const tal_t *ctx, struct wallet *w,
const u64 value, const u64 value,
const u32 feerate_per_kw, const u32 feerate_per_kw,
u64 *fee_estimate) u64 *fee_estimate, u64 *changesatoshi)
{ {
size_t i = 0; size_t i = 0;
struct utxo **available; struct utxo **available;
const struct utxo **utxos = tal_arr(ctx, const struct utxo *, 0); const struct utxo **utxos = tal_arr(ctx, const struct utxo *, 0);
*fee_estimate = 0;
/* We assume two outputs for the weight. */ /* We assume two outputs for the weight. */
u64 satoshi_in = 0, weight = (4 + (8 + 22) * 2 + 4) * 4; u64 satoshi_in = 0, weight = (4 + (8 + 22) * 2 + 4) * 4;
tal_add_destructor2(utxos, destroy_utxos, w); 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); available = wallet_get_utxos(ctx, w, output_state_available);
for (i = 0; i < tal_count(available); i++) { 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 { } else {
/* Commit the db transaction to persist markings */ /* Commit the db transaction to persist markings */
db_commit_transaction(w->db); db_commit_transaction(w->db);
*changesatoshi = satoshi_in - value - *fee_estimate;
} }
return utxos; return utxos;
} }

3
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 struct utxo **wallet_select_coins(const tal_t *ctx, struct wallet *w,
const u64 value, const u64 value,
const u32 feerate_per_kw, 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. * wallet_confirm_utxos - Once we've spent a set of utxos, mark them confirmed.

Loading…
Cancel
Save