bkpr: prevent crash when updating same account all at once

If two events for the same (unlogged) account come in and get run
through the "lookup peer" code, we should anticipate that.

We do two things here:
	- one, if it's a duplicate "event" to create the channel open
	  we check for that and just exit early
	- two, we were using a copy of the account that was
	  fetched/pulled from before the RPC ran, so instead we
	  just re-pull the most up to date account info for the
	  close checks.

This fixes the crash I was getting in re-running these things
This commit is contained in:
niftynei
2022-07-19 17:04:40 +09:30
committed by Rusty Russell
parent 38eb95ffee
commit 67ce0ee3b2

View File

@@ -648,7 +648,9 @@ static bool new_missed_channel_account(struct command *cmd,
assert(ok); assert(ok);
chain_ev->credit = amt; chain_ev->credit = amt;
db_begin_transaction(db); db_begin_transaction(db);
log_chain_event(db, acct, chain_ev); if (!log_chain_event(db, acct, chain_ev))
goto done;
maybe_update_account(db, acct, chain_ev, maybe_update_account(db, acct, chain_ev,
tags, 0, &peer_id); tags, 0, &peer_id);
maybe_update_onchain_fees(cmd, db, &opt.txid); maybe_update_onchain_fees(cmd, db, &opt.txid);
@@ -679,6 +681,7 @@ static bool new_missed_channel_account(struct command *cmd,
log_channel_event(db, acct, chan_ev); log_channel_event(db, acct, chan_ev);
} }
done:
db_commit_transaction(db); db_commit_transaction(db);
return true; return true;
} }
@@ -874,15 +877,19 @@ static char *do_account_close_checks(const tal_t *ctx,
} else if (!is_channel_account(acct) && !e->spending_txid) } else if (!is_channel_account(acct) && !e->spending_txid)
closed_acct = find_close_account(ctx, db, &e->outpoint.txid); closed_acct = find_close_account(ctx, db, &e->outpoint.txid);
else else
closed_acct = acct; /* Get most up to date account entry */
closed_acct = find_account(ctx, db, acct->name);
if (closed_acct && closed_acct->closed_event_db_id) { if (closed_acct && closed_acct->closed_event_db_id) {
maybe_mark_account_onchain(db, closed_acct); maybe_mark_account_onchain(db, closed_acct);
if (closed_acct->onchain_resolved_block > 0) { if (closed_acct->onchain_resolved_block > 0) {
char *err; char *err;
err = update_channel_onchain_fees(ctx, db, closed_acct); err = update_channel_onchain_fees(ctx, db, closed_acct);
if (err) if (err) {
db_commit_transaction(db);
return err; return err;
}
} }
} }