Browse Source
No new functionality, just a continuation of my work toward completing #665. I removed the common members of `struct withdrawal` and `struct fund_channel` and placed them in a new `struct wallet_tx`. Then it was fairly straightforward to reimplement the existing code in terms of `wallet_tx`. Since I made some structural changes I wanted to get this approved before I go any farther. Added 'all' to fundchannel help message.ppa-0.6.1
Mark Beckwith
7 years ago
committed by
Christian Decker
11 changed files with 155 additions and 189 deletions
@ -0,0 +1,48 @@ |
|||
#include <common/wallet_tx.h> |
|||
#include <inttypes.h> |
|||
#include <lightningd/jsonrpc_errors.h> |
|||
#include <wallet/wallet.h> |
|||
|
|||
void wtx_init(struct command *cmd, struct wallet_tx * wtx) |
|||
{ |
|||
wtx->cmd = cmd; |
|||
wtx->amount = 0; |
|||
wtx->change_key_index = 0; |
|||
wtx->utxos = NULL; |
|||
wtx->all_funds = false; |
|||
} |
|||
|
|||
bool wtx_select_utxos(struct wallet_tx * tx, u32 fee_rate_per_kw, |
|||
size_t out_len) |
|||
{ |
|||
u64 fee_estimate; |
|||
if (tx->all_funds) { |
|||
tx->utxos = wallet_select_all(tx->cmd, tx->cmd->ld->wallet, |
|||
fee_rate_per_kw, out_len, |
|||
&tx->amount, |
|||
&fee_estimate); |
|||
if (!tx->utxos || tx->amount < 546) { |
|||
command_fail(tx->cmd, "Cannot afford fee %"PRIu64, |
|||
fee_estimate); |
|||
return false; |
|||
} |
|||
tx->change = 0; |
|||
} else { |
|||
tx->utxos = wallet_select_coins(tx->cmd, tx->cmd->ld->wallet, |
|||
tx->amount, |
|||
fee_rate_per_kw, out_len, |
|||
&fee_estimate, &tx->change); |
|||
if (!tx->utxos || tx->amount < 546) { |
|||
command_fail(tx->cmd, |
|||
"Cannot afford funding transaction"); |
|||
return false; |
|||
} |
|||
if (tx->change < 546) { |
|||
tx->change = 0; |
|||
tx->change_key_index = 0; |
|||
} else { |
|||
tx->change_key_index = wallet_get_newindex(tx->cmd->ld); |
|||
} |
|||
} |
|||
return true; |
|||
} |
@ -0,0 +1,25 @@ |
|||
#ifndef LIGHTNING_COMMON_WALLET_TX_H |
|||
#define LIGHTNING_COMMON_WALLET_TX_H |
|||
#include "config.h" |
|||
#include <ccan/short_types/short_types.h> |
|||
#include <lightningd/json.h> |
|||
#include <lightningd/jsonrpc.h> |
|||
#include <lightningd/lightningd.h> |
|||
|
|||
/* A specification of funds in the wallet used for funding channels and
|
|||
* withdrawal. |
|||
*/ |
|||
struct wallet_tx { |
|||
struct command *cmd; |
|||
u64 amount; |
|||
u64 change; |
|||
u32 change_key_index; |
|||
const struct utxo **utxos; |
|||
|
|||
bool all_funds; |
|||
}; |
|||
|
|||
void wtx_init(struct command *cmd, struct wallet_tx *wtx); |
|||
bool wtx_select_utxos(struct wallet_tx * tx, u32 fee_rate_per_kw, |
|||
size_t out_len); |
|||
#endif /* LIGHTNING_COMMON_WALLET_TX_H */ |
@ -1,34 +0,0 @@ |
|||
#include <bitcoin/base58.h> |
|||
#include <bitcoin/script.h> |
|||
#include <ccan/structeq/structeq.h> |
|||
#include <common/utils.h> |
|||
#include <lightningd/build_utxos.h> |
|||
#include <lightningd/jsonrpc.h> |
|||
#include <wally_bip32.h> |
|||
|
|||
|
|||
const struct utxo **build_utxos(const tal_t *ctx, |
|||
struct lightningd *ld, u64 satoshi_out, |
|||
u32 feerate_per_kw, u64 dust_limit, |
|||
size_t outputscriptlen, |
|||
u64 *change_satoshis, u32 *change_keyindex) |
|||
{ |
|||
u64 fee_estimate = 0; |
|||
const struct utxo **utxos = |
|||
wallet_select_coins(ctx, ld->wallet, satoshi_out, feerate_per_kw, |
|||
outputscriptlen, |
|||
&fee_estimate, change_satoshis); |
|||
|
|||
/* Oops, didn't have enough coins available */ |
|||
if (!utxos) |
|||
return NULL; |
|||
|
|||
/* Do we need a change output? */ |
|||
if (*change_satoshis < dust_limit) { |
|||
*change_satoshis = 0; |
|||
*change_keyindex = 0; |
|||
} else { |
|||
*change_keyindex = wallet_get_newindex(ld); |
|||
} |
|||
return utxos; |
|||
} |
@ -1,15 +0,0 @@ |
|||
#ifndef LIGHTNING_LIGHTNINGD_BUILD_UTXOS_H |
|||
#define LIGHTNING_LIGHTNINGD_BUILD_UTXOS_H |
|||
#include "config.h" |
|||
#include <common/utxo.h> |
|||
#include <lightningd/lightningd.h> |
|||
|
|||
/* Reserves UTXOs to build tx which pays this amount; returns NULL if
|
|||
* impossible. */ |
|||
const struct utxo **build_utxos(const tal_t *ctx, |
|||
struct lightningd *ld, u64 satoshi_out, |
|||
u32 feerate_per_kw, u64 dust_limit, |
|||
size_t outputscriptlen, |
|||
u64 *change_satoshis, u32 *change_keyindex); |
|||
|
|||
#endif /* LIGHTNING_LIGHTNINGD_BUILD_UTXOS_H */ |
Loading…
Reference in new issue