diff --git a/common/wallet_tx.c b/common/wallet_tx.c index 104f265bd..89ff3aa2c 100644 --- a/common/wallet_tx.c +++ b/common/wallet_tx.c @@ -12,6 +12,21 @@ void wtx_init(struct command *cmd, struct wallet_tx * wtx) wtx->all_funds = false; } +static bool check_amount(const struct wallet_tx *tx) +{ + if (!tx->utxos) { + command_fail(tx->cmd, FUND_CANNOT_AFFORD, + "Cannot afford funding transaction"); + return false; + } + if (tx->amount < 546) { + command_fail(tx->cmd, FUND_DUST_LIMIT_UNMET, + "Dust limit unmet"); + return false; + } + return true; +} + bool wtx_select_utxos(struct wallet_tx * tx, u32 fee_rate_per_kw, size_t out_len) { @@ -21,23 +36,18 @@ bool wtx_select_utxos(struct wallet_tx * tx, u32 fee_rate_per_kw, 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); + if (!check_amount(tx)) 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"); + if (!check_amount(tx)) return false; - } + if (tx->change < 546) { tx->change = 0; tx->change_key_index = 0; diff --git a/doc/lightning-fundchannel.7.txt b/doc/lightning-fundchannel.7.txt index 5b143487f..a337e72a5 100644 --- a/doc/lightning-fundchannel.7.txt +++ b/doc/lightning-fundchannel.7.txt @@ -24,7 +24,7 @@ for the channel. 'satoshi' is the amount in satoshis taken from the internal wallet to fund the channel. The string 'all' can be used to specify all available funds. -This value must be greater than the dust limit, currently set to 546. +This value cannot be less than the dust limit, currently set to 546. And it must be less than 1<<24 (approximately 0.16778 BTC). RETURN VALUE @@ -35,14 +35,11 @@ On failure, an error is reported and the channel is not funded. The following error codes may occur: -* -1. Catchall nonspecific arror. - -The above error may include a descriptive message indicating: - -* The 'id' is invalid. -* There are not enough funds in the internal wallet to create the transaction. -* The maximum allowed funding amount is exceeded. -* 'satoshi' is less than the dust limit. +* -1. Catchall nonspecific error. +* 300. The maximum allowed funding amount is exceeded. +* 301. There are not enough funds in the internal wallet (including fees) to + create the transaction. +* 302. The dust limit is not met. Failure may also occur if *lightningd* and the peer cannot agree on channel parameters (funding limits, channel reserves, fees, etc.). diff --git a/doc/lightning-withdraw.7.txt b/doc/lightning-withdraw.7.txt index 8907d8c45..f58550228 100644 --- a/doc/lightning-withdraw.7.txt +++ b/doc/lightning-withdraw.7.txt @@ -33,11 +33,14 @@ be returned. 'tx' represents the raw bitcoin, fully signed, transaction and 'txid' represent the bitcoin transaction id. -ERRORS ------- -If an incorrect address is supplied or the 'satoshi' -parameter exceeds the amount in the internal wallet -an error message will be returned. +On failure, an error is reported and the channel is not funded. + +The following error codes may occur: + +* -1. Catchall nonspecific error. +* 301. There are not enough funds in the internal wallet (including fees) to + create the transaction. +* 302. The dust limit is not met. AUTHOR ------ diff --git a/lightningd/jsonrpc_errors.h b/lightningd/jsonrpc_errors.h index 7bf3fbb77..c7cc4ab20 100644 --- a/lightningd/jsonrpc_errors.h +++ b/lightningd/jsonrpc_errors.h @@ -29,6 +29,11 @@ #define PAY_UNSPECIFIED_ERROR 209 #define PAY_STOPPED_RETRYING 210 +/* `fundchannel` or `withdraw` errors */ +#define FUND_MAX_EXCEEDED 300 +#define FUND_CANNOT_AFFORD 301 +#define FUND_DUST_LIMIT_UNMET 302 + /* Errors from `invoice` command */ #define INVOICE_LABEL_ALREADY_EXISTS 900 #define INVOICE_PREIMAGE_ALREADY_EXISTS 901 diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 9dd35bac4..82e5700b8 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -922,7 +922,7 @@ static void json_fund_channel(struct command *cmd, return; if (fc->wtx.amount > MAX_FUNDING_SATOSHI) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, + command_fail(cmd, FUND_MAX_EXCEEDED, "Funding satoshi must be <= %d", MAX_FUNDING_SATOSHI); return; diff --git a/tests/test_lightningd.py b/tests/test_lightningd.py index 8c917fdf5..118db2de2 100644 --- a/tests/test_lightningd.py +++ b/tests/test_lightningd.py @@ -3915,7 +3915,7 @@ class LightningDTests(BaseLightningDTests): # This should fail, can't even afford fee. self.assertRaises(ValueError, l1.rpc.withdraw, waddr, 'all') - l1.daemon.wait_for_log('Cannot afford fee') + l1.daemon.wait_for_log('Cannot afford funding transaction') def test_funding_change(self): """Add some funds, fund a channel, and make sure we remember the change