lightningd: clean up channel_tell_depth.

It's a mess right now.

Try to express it as a switch() statement over the states we can be in.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-10-02 09:29:49 +10:30
parent fe0959fd31
commit 58d27c4bdc

View file

@ -1515,39 +1515,26 @@ bool peer_start_channeld(struct channel *channel,
return true;
}
bool channel_tell_depth(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *txid,
u32 depth)
static bool channel_splice_set_scid(struct channel *channel,
const struct bitcoin_txid *txid)
{
const char *txidstr;
struct txlocator *loc;
u32 outnum;
txidstr = type_to_string(tmpctx, struct bitcoin_txid, txid);
channel->depth = depth;
if (!channel->owner) {
log_debug(channel->log,
"Funding tx %s confirmed, but peer disconnected",
txidstr);
return false;
}
if (channel->state == CHANNELD_AWAITING_SPLICE
&& depth >= channel->minimum_depth) {
if (!get_inflight_outpoint_index(channel, txid, &outnum)) {
log_debug(channel->log, "Can't locate splice inflight"
" txid %s", txidstr);
" txid %s",
type_to_string(tmpctx, struct bitcoin_txid, txid));
return false;
}
loc = wallet_transaction_locate(tmpctx, ld->wallet, txid);
loc = wallet_transaction_locate(tmpctx, channel->peer->ld->wallet, txid);
if (!loc) {
channel_fail_permanent(channel,
REASON_LOCAL,
"Can't locate splice transaction"
" in wallet txid %s", txidstr);
" in wallet txid %s",
type_to_string(tmpctx, struct bitcoin_txid, txid));
return false;
}
@ -1561,32 +1548,14 @@ bool channel_tell_depth(struct lightningd *ld,
channel->funding.n);
return false;
}
}
if (streq(channel->owner->name, "dualopend")) {
if (channel->state != DUALOPEND_AWAITING_LOCKIN) {
log_debug(channel->log,
"Funding tx %s confirmed, but peer in"
" state %s",
txidstr, channel_state_name(channel));
return true;
}
log_debug(channel->log,
"Funding tx %s confirmed, telling peer", txidstr);
dualopen_tell_depth(channel->owner, channel,
txid, depth);
return true;
} else if (channel->state != CHANNELD_AWAITING_LOCKIN
&& !channel_state_normalish(channel)) {
/* If not awaiting lockin/announce, it doesn't
* care any more */
log_debug(channel->log,
"Funding tx %s confirmed, but peer in state %s",
txidstr, channel_state_name(channel));
return true;
}
}
/* Actually send the depth message to channeld */
static void channeld_tell_depth(struct channel *channel,
const struct bitcoin_txid *txid,
u32 depth)
{
log_debug(channel->log,
"Sending towire_channeld_funding_depth with channel state %s",
channel_state_str(channel->state));
@ -1595,18 +1564,59 @@ bool channel_tell_depth(struct lightningd *ld,
take(towire_channeld_funding_depth(
NULL, channel->scid, channel->alias[LOCAL], depth,
channel->state == CHANNELD_AWAITING_SPLICE, txid)));
}
if (channel->remote_channel_ready &&
channel->state == CHANNELD_AWAITING_LOCKIN &&
depth >= channel->minimum_depth) {
bool channel_tell_depth(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *txid,
u32 depth)
{
const char *txidstr;
txidstr = type_to_string(tmpctx, struct bitcoin_txid, txid);
channel->depth = depth;
if (!channel->owner) {
log_debug(channel->log,
"Funding tx %s confirmed, but peer disconnected",
txidstr);
return false;
}
switch (channel->state) {
case CHANNELD_AWAITING_SPLICE:
if (depth >= channel->minimum_depth) {
if (!channel_splice_set_scid(channel, txid))
return false;
}
channeld_tell_depth(channel, txid, depth);
/* We're done, don't track depth any more. */
return true;
case DUALOPEND_AWAITING_LOCKIN:
log_debug(channel->log,
"Funding tx %s confirmed, telling peer", txidstr);
dualopen_tell_depth(channel->owner, channel,
txid, depth);
/* We're done, don't track depth any more. */
return true;
case CHANNELD_AWAITING_LOCKIN:
channeld_tell_depth(channel, txid, depth);
if (channel->remote_channel_ready
&& depth >= channel->minimum_depth) {
lockin_complete(channel, CHANNELD_AWAITING_LOCKIN);
} else if (depth == 1 && channel->minimum_depth == 0) {
return true;
}
return false;
case CHANNELD_NORMAL:
/* If we have a zeroconf channel, i.e., no scid yet
* but have exchange `channel_ready` messages, then we
* need to fire a second time, in order to trigger the
* `coin_movement` event. This is a subset of the
* `lockin_complete` function below. */
* `lockin_complete` function above. */
if (depth == 1 && channel->minimum_depth == 0) {
assert(channel->scid != NULL);
/* Fees might have changed (and we use IMMEDIATE once we're
* funded), so update now. */
@ -1621,7 +1631,25 @@ bool channel_tell_depth(struct lightningd *ld,
short_channel_id_blocknum(channel->scid),
false);
}
/* Still might want to know, for announcement reasons */
channeld_tell_depth(channel, txid, depth);
return false;
case AWAITING_UNILATERAL:
case CHANNELD_SHUTTING_DOWN:
case CLOSINGD_SIGEXCHANGE:
case CLOSINGD_COMPLETE:
case FUNDING_SPEND_SEEN:
case ONCHAIN:
case CLOSED:
case DUALOPEND_OPEN_INIT:
/* If not awaiting lockin/announce, it doesn't care any more */
log_debug(channel->log,
"Funding tx %s confirmed, but peer in state %s",
txidstr, channel_state_name(channel));
return true;
}
abort();
}
/* Check if we are the fundee of this channel, the channel