diff --git a/connectd/connectd.c b/connectd/connectd.c index a1bbbaf39a44..35f81ebb2a92 100644 --- a/connectd/connectd.c +++ b/connectd/connectd.c @@ -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; } diff --git a/plugins/fundchannel.c b/plugins/fundchannel.c index 2de8948cd437..23f9340fb633 100644 --- a/plugins/fundchannel.c +++ b/plugins/fundchannel.c @@ -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); @@ -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; @@ -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'. @@ -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) { @@ -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, diff --git a/tests/test_connection.py b/tests/test_connection.py index c5af38e5a2e5..277e49cdeddb 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -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.