df-open: use channel_id for openchannel_update and openchannel_signed

Be as specific as possible is a good rule for things, I think
This commit is contained in:
niftynei
2020-09-17 15:28:46 -05:00
committed by Rusty Russell
parent 085c590a51
commit b696ec89a5
11 changed files with 89 additions and 52 deletions

View File

@@ -20,6 +20,7 @@
#include <lightningd/log.h>
#include <lightningd/notification.h>
#include <lightningd/opening_control.h>
#include <lightningd/opening_common.h>
#include <lightningd/peer_control.h>
#include <lightningd/subd.h>
#include <wire/wire_sync.h>
@@ -376,6 +377,35 @@ struct channel *channel_by_dbid(struct lightningd *ld, const u64 dbid)
return NULL;
}
struct channel *channel_by_cid(struct lightningd *ld,
const struct channel_id *cid,
struct uncommitted_channel **uc)
{
struct peer *p;
struct channel *channel;
list_for_each(&ld->peers, p, list) {
if (p->uncommitted_channel) {
if (channel_id_eq(&p->uncommitted_channel->cid, cid)) {
if (uc)
*uc = p->uncommitted_channel;
return NULL;
}
}
list_for_each(&p->channels, channel, list) {
if (channel_id_eq(&channel->cid, cid)) {
if (uc)
*uc = p->uncommitted_channel;
return channel;
}
}
}
if (uc)
*uc = NULL;
return NULL;
}
void channel_set_last_tx(struct channel *channel,
struct bitcoin_tx *tx,
const struct bitcoin_signature *sig,

View File

@@ -253,6 +253,11 @@ struct channel *channel_by_dbid(struct lightningd *ld, const u64 dbid);
struct channel *active_channel_by_scid(struct lightningd *ld,
const struct short_channel_id *scid);
/* Get channel by channel_id, optionally returning uncommitted_channel. */
struct channel *channel_by_cid(struct lightningd *ld,
const struct channel_id *cid,
struct uncommitted_channel **uc);
void channel_set_last_tx(struct channel *channel,
struct bitcoin_tx *tx,
const struct bitcoin_signature *sig,

View File

@@ -699,6 +699,7 @@ static void opener_psbt_changed(struct subd *dualopend,
json_add_psbt(response, "psbt", psbt);
json_add_bool(response, "commitments_secured", false);
uc->cid = cid;
uc->fc->inflight = true;
uc->fc->cmd = NULL;
was_pending(command_success(cmd, response));
@@ -1009,32 +1010,25 @@ static struct command_result *json_open_channel_signed(struct command *cmd,
const jsmntok_t *params)
{
struct wally_psbt *psbt;
struct node_id *id;
struct peer *peer;
struct channel_id *cid;
struct channel *channel;
struct bitcoin_txid txid;
if (!param(cmd, buffer, params,
p_req("id", param_node_id, &id),
p_req("channel_id", param_channel_id, &cid),
p_req("signed_psbt", param_psbt, &psbt),
NULL))
return command_param_failed();
peer = peer_by_id(cmd->ld, id);
if (!peer)
return command_fail(cmd, FUNDING_UNKNOWN_PEER, "Unknown peer");
channel = peer_active_channel(peer);
channel = channel_by_cid(cmd->ld, cid, NULL);
if (!channel)
return command_fail(cmd, LIGHTNINGD,
"Peer has no active channel");
return command_fail(cmd, FUNDING_UNKNOWN_CHANNEL,
"Unknown channel");
if (!channel->pps)
return command_fail(cmd, LIGHTNINGD,
"Missing per-peer-state for channel, "
"are you in the right state to call "
"this method?");
if (channel->psbt)
return command_fail(cmd, LIGHTNINGD,
"Already have a finalized PSBT for "
@@ -1053,7 +1047,6 @@ static struct command_result *json_open_channel_signed(struct command *cmd,
type_to_string(tmpctx, struct bitcoin_txid,
&txid));
/* Go ahead and try to finalize things, or what we can */
psbt_finalize(psbt);
@@ -1082,47 +1075,48 @@ static struct command_result *json_open_channel_update(struct command *cmd,
const jsmntok_t *params)
{
struct wally_psbt *psbt;
struct node_id *id;
struct peer *peer;
struct channel_id *cid;
struct channel *channel;
struct uncommitted_channel *uc;
u8 *msg;
if (!param(cmd, buffer, params,
p_req("id", param_node_id, &id),
p_req("channel_id", param_channel_id, &cid),
p_req("psbt", param_psbt, &psbt),
NULL))
return command_param_failed();
peer = peer_by_id(cmd->ld, id);
if (!peer)
return command_fail(cmd, FUNDING_UNKNOWN_PEER, "Unknown peer");
channel = peer_active_channel(peer);
/* We expect this to return NULL, as the channel hasn't been
* created yet. Instead, the uncommitted channel is populated */
channel = channel_by_cid(cmd->ld, cid, &uc);
if (channel)
return command_fail(cmd, LIGHTNINGD, "Peer already %s",
return command_fail(cmd, LIGHTNINGD, "Channel already %s",
channel_state_name(channel));
if (!peer->uncommitted_channel)
return command_fail(cmd, FUNDING_PEER_NOT_CONNECTED,
"Peer not connected");
if (!uc)
return command_fail(cmd, FUNDING_UNKNOWN_CHANNEL,
"Unknown channel");
if (!peer->uncommitted_channel->fc || !peer->uncommitted_channel->fc->inflight)
return command_fail(cmd, LIGHTNINGD, "No channel funding in progress");
if (!uc->fc || !uc->fc->inflight)
return command_fail(cmd, LIGHTNINGD,
"No channel funding in progress");
if (peer->uncommitted_channel->fc->cmd)
return command_fail(cmd, LIGHTNINGD, "Channel funding in progress");
if (uc->fc->cmd)
return command_fail(cmd, LIGHTNINGD,
"Channel funding in progress");
/* Add serials to PSBT */
psbt_add_serials(psbt, TX_INITIATOR);
if (!psbt_has_required_fields(psbt))
return command_fail(cmd, FUNDING_PSBT_INVALID,
"PSBT is missing required fields %s",
type_to_string(tmpctx, struct wally_psbt, psbt));
type_to_string(tmpctx, struct wally_psbt,
psbt));
peer->uncommitted_channel->fc->cmd = cmd;
uc->fc->cmd = cmd;
msg = towire_dual_open_psbt_updated(NULL, psbt);
subd_send_msg(peer->uncommitted_channel->open_daemon, take(msg));
subd_send_msg(uc->open_daemon, take(msg));
return command_still_pending(cmd);
}

View File

@@ -47,6 +47,8 @@ new_uncommitted_channel(struct peer *peer)
uc->fc = NULL;
uc->our_config.id = 0;
memset(&uc->cid, 0xFF, sizeof(uc->cid));
get_channel_basepoints(ld, &uc->peer->id, uc->dbid,
&uc->local_basepoints, &uc->local_funding_pubkey);

View File

@@ -4,6 +4,7 @@
#include <bitcoin/pubkey.h>
#include <common/amount.h>
#include <common/channel_config.h>
#include <common/channel_id.h>
#include <common/derive_basepoints.h>
#include <common/status_levels.h>
@@ -27,6 +28,9 @@ struct uncommitted_channel {
/* Reserved dbid for if we become a real struct channel */
u64 dbid;
/* Channel id, v2 opens only */
struct channel_id cid;
/* For logging */
struct log *log;