Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fundchannel connect #3314

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ static void try_connect_peer(struct daemon *daemon,
* to retry; an address may get gossiped or appear on the DNS seed. */
if (tal_count(addrs) == 0) {
connect_failed(daemon, id, seconds_waited, addrhint,
"No address known");
"Unable to connect, no address known for peer");
return;
}

Expand Down
55 changes: 38 additions & 17 deletions plugins/fundchannel.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ static struct command_result *fundchannel_start_done(struct command *cmd,
}

static struct command_result *fundchannel_start(struct command *cmd,
struct funding_req *fr)
struct funding_req *fr)
{
struct json_out *ret = json_out_new(NULL);

Expand All @@ -326,18 +326,15 @@ static struct command_result *fundchannel_start(struct command *cmd,
json_out_end(ret, '}');
json_out_finished(ret);

/* FIXME: as a nice feature, we should check that the peer
* you want to connect to is connected first. if not, we should
* connect and then call fundchannel start! */
return send_outreq(cmd, "fundchannel_start",
fundchannel_start_done, tx_abort,
fr, take(ret));
}

static struct command_result *tx_prepare_dryrun(struct command *cmd,
const char *buf,
const jsmntok_t *result,
struct funding_req *fr)
static struct command_result *post_dryrun(struct command *cmd,
const char *buf,
const jsmntok_t *result,
struct funding_req *fr)
{
struct bitcoin_tx *tx;
const char *hex;
Expand Down Expand Up @@ -385,6 +382,38 @@ static struct command_result *tx_prepare_dryrun(struct command *cmd,
return fundchannel_start(cmd, fr);
}

static struct command_result *exec_dryrun(struct command *cmd,
const char *buf,
const jsmntok_t *result,
struct funding_req *fr)
{
struct json_out *ret;

/* Now that we've tried connecting, we do a 'dry-run' of txprepare,
* so we can get an accurate idea of the funding amount */
ret = txprepare(cmd, fr, placeholder_funding_addr);

return send_outreq(cmd, "txprepare",
post_dryrun, forward_error,
fr, take(ret));

}

static struct command_result *connect_to_peer(struct command *cmd,
struct funding_req *fr)
{
struct json_out *ret = json_out_new(NULL);

json_out_start(ret, NULL, '{');
json_out_addstr(ret, "id", node_id_to_hexstr(tmpctx, fr->id));
json_out_end(ret, '}');
json_out_finished(ret);

return send_outreq(cmd, "connect",
exec_dryrun, forward_error,
fr, take(ret));
}

/* We will use 'id' and 'amount' to build a output: {id: amount}.
* For array type, if we miss 'amount', next parameter will be
* mistaken for 'amount'.
Expand All @@ -411,7 +440,6 @@ static struct command_result *json_fundchannel(struct command *cmd,
const jsmntok_t *params)
{
struct funding_req *fr = tal(cmd, struct funding_req);
struct json_out *ret;

/* For generating help, give new-style. */
if (!params || !deprecated_apis || params->type == JSMN_ARRAY) {
Expand Down Expand Up @@ -448,14 +476,7 @@ static struct command_result *json_fundchannel(struct command *cmd,

fr->funding_all = streq(fr->funding_str, "all");

/* First we do a 'dry-run' of txprepare, so we can get
* an accurate idea of the funding amount */
ret = txprepare(cmd, fr, placeholder_funding_addr);

return send_outreq(cmd, "txprepare",
tx_prepare_dryrun, forward_error,
fr, take(ret));

return connect_to_peer(cmd, fr);
}

static void init(struct plugin_conn *rpc,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_connect(node_factory):
assert len(l2.rpc.listpeers()) == 1

# Should get reasonable error if unknown addr for peer.
with pytest.raises(RpcError, match=r'No address known'):
with pytest.raises(RpcError, match=r'Unable to connect, no address known'):
l1.rpc.connect('032cf15d1ad9c4a08d26eab1918f732d8ef8fdc6abb9640bf3db174372c491304e')

# Should get reasonable error if connection refuse.
Expand Down