Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Funding channel with "all"
* Added test for fund all
  • Loading branch information
wythe authored and cdecker committed Mar 20, 2018
1 parent 5737853 commit 489d8d0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
65 changes: 45 additions & 20 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,51 +816,76 @@ static void gossip_peer_released(struct subd *gossip,
fds[0], fds[1]);
}

/**
* json_fund_channel - Entrypoint for funding a channel
*/
static void json_fund_channel(struct command *cmd,
const char *buffer, const jsmntok_t *params)
{
jsmntok_t *peertok, *satoshitok;
struct funding_channel *fc = tal(cmd, struct funding_channel);
jsmntok_t *desttok, *sattok;
bool all_funds = false;
struct funding_channel * fc;
u32 feerate_per_kw = get_feerate(cmd->ld->topology, FEERATE_NORMAL);
u64 fee_estimate;
u8 *msg;

if (!json_get_params(cmd, buffer, params,
"id", &peertok,
"satoshi", &satoshitok,
"id", &desttok,
"satoshi", &sattok,
NULL)) {
return;
}

fc = tal(cmd, struct funding_channel);
fc->cmd = cmd;
fc->change_keyindex = 0;
fc->funding_satoshi = 0;

if (!pubkey_from_hexstr(buffer + peertok->start,
peertok->end - peertok->start, &fc->peerid)) {
command_fail(cmd, "Could not parse id");
return;
}
if (json_tok_streq(buffer, sattok, "all")) {
all_funds = true;

if (!json_tok_u64(buffer, satoshitok, &fc->funding_satoshi)) {
} else if (!json_tok_u64(buffer, sattok, &fc->funding_satoshi)) {
command_fail(cmd, "Invalid satoshis");
return;
}

if (fc->funding_satoshi > MAX_FUNDING_SATOSHI) {
command_fail(cmd, "Funding satoshi must be <= %d",
MAX_FUNDING_SATOSHI);
if (!pubkey_from_hexstr(buffer + desttok->start,
desttok->end - desttok->start, &fc->peerid)) {
command_fail(cmd, "Could not parse id");
return;
}

/* FIXME: Support push_msat? */
fc->push_msat = 0;
fc->channel_flags = OUR_CHANNEL_FLAGS;

/* Try to do this now, so we know if insufficient funds. */
/* FIXME: dustlimit */
fc->utxomap = build_utxos(fc, cmd->ld, fc->funding_satoshi,
get_feerate(cmd->ld->topology, FEERATE_NORMAL),
600, BITCOIN_SCRIPTPUBKEY_P2WSH_LEN,
&fc->change, &fc->change_keyindex);
if (!fc->utxomap) {
command_fail(cmd, "Cannot afford funding transaction");
if (all_funds) {
fc->utxomap = wallet_select_all(cmd, cmd->ld->wallet,
feerate_per_kw,
BITCOIN_SCRIPTPUBKEY_P2WSH_LEN,
&fc->funding_satoshi,
&fee_estimate);
if (!fc->utxomap || fc->funding_satoshi < 546) {
command_fail(cmd, "Cannot afford fee %"PRIu64,
fee_estimate);
return;
}
fc->change = 0;
} else {
fc->utxomap = build_utxos(fc, cmd->ld, fc->funding_satoshi,
feerate_per_kw,
600, BITCOIN_SCRIPTPUBKEY_P2WSH_LEN,
&fc->change, &fc->change_keyindex);
if (!fc->utxomap) {
command_fail(cmd, "Cannot afford funding transaction");
return;
}
}

if (fc->funding_satoshi > MAX_FUNDING_SATOSHI) {
command_fail(cmd, "Funding satoshi must be <= %d",
MAX_FUNDING_SATOSHI);
return;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/test_lightningd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3121,6 +3121,21 @@ def test_funding_change(self):
assert outputs[0] > 8990000
assert outputs[2] == 10000000

def test_funding_all(self):
"""Add some funds, fund a channel using all funds, make sure no funds remain
"""
l1, l2 = self.connect()

self.give_funds(l1, 0.1 * 10**8)

outputs = l1.db_query('SELECT value FROM outputs WHERE status=0;')
assert len(outputs) == 1 and outputs[0]['value'] == 10000000

l1.rpc.fundchannel(l2.info['id'], "all")

outputs = l1.db_query('SELECT value FROM outputs WHERE status=0;')
assert len(outputs) == 0

def test_funding_fail(self):
"""Add some funds, fund a channel without enough funds"""
# Previous runs with same bitcoind can leave funds!
Expand Down
6 changes: 3 additions & 3 deletions wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static void json_withdraw(struct command *cmd,
u32 feerate_per_kw = get_feerate(cmd->ld->topology, FEERATE_NORMAL);
u64 fee_estimate;
struct bitcoin_tx *tx;
bool withdraw_all = false;
bool all_funds = false;
enum address_parse_result addr_parse;

if (!json_get_params(cmd, buffer, params,
Expand All @@ -105,7 +105,7 @@ static void json_withdraw(struct command *cmd,
withdraw->cmd = cmd;

if (json_tok_streq(buffer, sattok, "all"))
withdraw_all = true;
all_funds = true;
else if (!json_tok_u64(buffer, sattok, &withdraw->amount)) {
command_fail(cmd, "Invalid satoshis");
return;
Expand All @@ -132,7 +132,7 @@ static void json_withdraw(struct command *cmd,
}

/* Select the coins */
if (withdraw_all) {
if (all_funds) {
withdraw->utxos = wallet_select_all(cmd, cmd->ld->wallet,
feerate_per_kw,
tal_len(withdraw->destination),
Expand Down

0 comments on commit 489d8d0

Please sign in to comment.