From 1ede7bc55b2f080f28dcd500879cc5525075d380 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 16 Dec 2018 15:21:06 +1030 Subject: [PATCH] wallet_tx: make wtx_select_utxos return command_result. It can fail the command, so it should return accordingly. Signed-off-by: Rusty Russell --- common/wallet_tx.c | 37 +++++++++++++++++++----------------- common/wallet_tx.h | 5 +++-- lightningd/opening_control.c | 5 +++-- wallet/walletrpc.c | 5 +++-- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/common/wallet_tx.c b/common/wallet_tx.c index e627f0c7b..54e2b3b47 100644 --- a/common/wallet_tx.c +++ b/common/wallet_tx.c @@ -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; } diff --git a/common/wallet_tx.h b/common/wallet_tx.h index 97027005c..536096bbd 100644 --- a/common/wallet_tx.h +++ b/common/wallet_tx.h @@ -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 */ diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index f46c274ab..27923e57c 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -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); diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 63f9199b9..66835d1f9 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -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,