wallet_tx: make wtx_select_utxos return command_result.

It can fail the command, so it should return accordingly.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-12-16 15:21:06 +10:30
parent bc41ab2cb9
commit 1ede7bc55b
4 changed files with 29 additions and 23 deletions

View File

@ -13,25 +13,26 @@ void wtx_init(struct command *cmd, struct wallet_tx * wtx)
wtx->all_funds = false;
}
static bool check_amount(const struct wallet_tx *tx, u64 amount)
static struct command_result *check_amount(const struct wallet_tx *tx,
u64 amount)
{
if (tal_count(tx->utxos) == 0) {
command_fail(tx->cmd, FUND_CANNOT_AFFORD,
"Cannot afford transaction");
return false;
return command_fail(tx->cmd, FUND_CANNOT_AFFORD,
"Cannot afford transaction");
}
if (amount < 546) {
command_fail(tx->cmd, FUND_OUTPUT_IS_DUST,
"Output %"PRIu64" satoshis would be dust",
amount);
return false;
return command_fail(tx->cmd, FUND_OUTPUT_IS_DUST,
"Output %"PRIu64" satoshis would be dust",
amount);
}
return true;
return NULL;
}
bool wtx_select_utxos(struct wallet_tx * tx, u32 fee_rate_per_kw,
size_t out_len)
struct command_result *wtx_select_utxos(struct wallet_tx *tx,
u32 fee_rate_per_kw,
size_t out_len)
{
struct command_result *res;
u64 fee_estimate;
if (tx->all_funds) {
u64 amount;
@ -39,13 +40,14 @@ bool wtx_select_utxos(struct wallet_tx * tx, u32 fee_rate_per_kw,
fee_rate_per_kw, out_len,
&amount,
&fee_estimate);
if (!check_amount(tx, amount))
return false;
res = check_amount(tx, amount);
if (res)
return res;
if (amount <= tx->amount) {
tx->change = 0;
tx->amount = amount;
return true;
return NULL;
}
/* Too much? Try again, but ask for limit instead. */
@ -57,8 +59,9 @@ bool wtx_select_utxos(struct wallet_tx * tx, u32 fee_rate_per_kw,
tx->amount,
fee_rate_per_kw, out_len,
&fee_estimate, &tx->change);
if (!check_amount(tx, tx->amount))
return false;
res = check_amount(tx, tx->amount);
if (res)
return res;
if (tx->change < 546) {
tx->change = 0;
@ -66,5 +69,5 @@ bool wtx_select_utxos(struct wallet_tx * tx, u32 fee_rate_per_kw,
} else {
tx->change_key_index = wallet_get_newindex(tx->cmd->ld);
}
return true;
return NULL;
}

View File

@ -20,6 +20,7 @@ struct wallet_tx {
};
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);
struct command_result *wtx_select_utxos(struct wallet_tx *tx,
u32 fee_rate_per_kw,
size_t out_len);
#endif /* LIGHTNING_COMMON_WALLET_TX_H */

View File

@ -839,8 +839,9 @@ static void json_fund_channel(struct command *cmd,
type_to_string(fc, struct pubkey, id));
}
if (!wtx_select_utxos(&fc->wtx, *feerate_per_kw,
BITCOIN_SCRIPTPUBKEY_P2WSH_LEN))
res = wtx_select_utxos(&fc->wtx, *feerate_per_kw,
BITCOIN_SCRIPTPUBKEY_P2WSH_LEN);
if (res)
return;
assert(fc->wtx.amount <= max_funding_satoshi);

View File

@ -138,8 +138,9 @@ static void json_withdraw(struct command *cmd,
return;
}
if (!wtx_select_utxos(&withdraw->wtx, *feerate_per_kw,
tal_count(withdraw->destination)))
res = wtx_select_utxos(&withdraw->wtx, *feerate_per_kw,
tal_count(withdraw->destination));
if (res)
return;
u8 *msg = towire_hsm_sign_withdrawal(cmd,