core-lightning/common/wallet_tx.c
Mark Beckwith 7f437715d5 Added error code parameter to command_fail
Until now, `command_fail()` reported an error code of -1 for all uses.
This PR adds an `int code` parameter to `command_fail()`, requiring the
caller to explicitly include the error code.

This is part of #1464.

The majority of the calls are used during parameter validation and
their error code is now JSONRPC2_INVALID_PARAMS.

The rest of the calls report an error code of LIGHTNINGD, which I defined to
-1 in `jsonrpc_errors.h`.  The intention here is that as we improve our error
reporting, all occurenaces of LIGHTNINGD will go away and we can eventually
remove it.

I also converted calls to `command_fail_detailed()` that took a `NULL` `data`
parameter to use the new `command_fail()`.

The only difference from an end user perspecive is that bad input errors that
used to be -1 will now be -32602 (JSONRPC2_INVALID_PARAMS).
2018-05-26 12:17:36 +02:00

50 lines
1.2 KiB
C

#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, LIGHTNINGD,
"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, LIGHTNINGD,
"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;
}