mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-17 19:03:42 +01:00
db: always call db_update_our_closing in a transaction.
It's not in a transaction in one caller, so wrap that. This removes some more error handling code. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
5bcc9047b0
commit
fce9ee29e3
14
daemon/db.c
14
daemon/db.c
@ -1733,21 +1733,19 @@ bool db_set_their_closing_script(struct peer *peer)
|
||||
/* For first time, we are in transaction to make it atomic with peer->state
|
||||
* update. Later calls are not. */
|
||||
/* FIXME: make caller wrap in transaction. */
|
||||
bool db_update_our_closing(struct peer *peer)
|
||||
void db_update_our_closing(struct peer *peer)
|
||||
{
|
||||
const char *ctx = tal(peer, char);
|
||||
bool ok;
|
||||
const char *peerid = pubkey_to_hexstr(ctx, peer->dstate->secpctx, peer->id);
|
||||
|
||||
log_debug(peer->log, "%s(%s)", __func__, peerid);
|
||||
|
||||
ok = db_exec(__func__, peer->dstate,
|
||||
"UPDATE closing SET our_fee=%"PRIu64", closing_order=%"PRIi64" WHERE peer=x'%s';",
|
||||
peer->closing.our_fee,
|
||||
peer->closing.closing_order,
|
||||
peerid);
|
||||
db_exec(__func__, peer->dstate,
|
||||
"UPDATE closing SET our_fee=%"PRIu64", closing_order=%"PRIi64" WHERE peer=x'%s';",
|
||||
peer->closing.our_fee,
|
||||
peer->closing.closing_order,
|
||||
peerid);
|
||||
tal_free(ctx);
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool db_update_their_closing(struct peer *peer)
|
||||
|
@ -20,7 +20,6 @@ bool db_add_peer_address(struct lightningd_state *dstate,
|
||||
const struct peer_address *addr);
|
||||
|
||||
/* Must NOT be inside transaction. */
|
||||
bool db_update_our_closing(struct peer *peer);
|
||||
bool db_update_their_closing(struct peer *peer);
|
||||
bool db_set_their_closing_script(struct peer *peer);
|
||||
bool db_new_pay_command(struct lightningd_state *dstate,
|
||||
@ -67,6 +66,7 @@ void db_save_shachain(struct peer *peer);
|
||||
void db_update_state(struct peer *peer);
|
||||
void db_begin_shutdown(struct peer *peer);
|
||||
void db_set_our_closing_script(struct peer *peer);
|
||||
void db_update_our_closing(struct peer *peer);
|
||||
|
||||
void db_add_commit_map(struct peer *peer,
|
||||
const struct sha256_double *txid, u64 commit_num);
|
||||
|
@ -914,7 +914,9 @@ static bool closing_pkt_in(struct peer *peer, const Pkt *pkt)
|
||||
|
||||
peer->closing.closing_order = peer->order_counter++;
|
||||
|
||||
if (!db_update_our_closing(peer)) {
|
||||
db_start_transaction(peer);
|
||||
db_update_our_closing(peer);
|
||||
if (db_commit_transaction(peer) != NULL) {
|
||||
return peer_comms_err(peer,
|
||||
pkt_err(peer, "Database error"));
|
||||
}
|
||||
@ -1283,7 +1285,7 @@ static void peer_calculate_close_fee(struct peer *peer)
|
||||
assert(!(peer->closing.our_fee & 1));
|
||||
}
|
||||
|
||||
static bool start_closing_in_transaction(struct peer *peer)
|
||||
static void start_closing_in_transaction(struct peer *peer)
|
||||
{
|
||||
assert(!committed_to_htlcs(peer));
|
||||
|
||||
@ -1291,26 +1293,18 @@ static bool start_closing_in_transaction(struct peer *peer)
|
||||
|
||||
peer_calculate_close_fee(peer);
|
||||
peer->closing.closing_order = peer->order_counter++;
|
||||
if (!db_update_our_closing(peer))
|
||||
return false;
|
||||
db_update_our_closing(peer);
|
||||
queue_pkt_close_signature(peer);
|
||||
return true;
|
||||
}
|
||||
|
||||
static Pkt *start_closing(struct peer *peer)
|
||||
{
|
||||
db_start_transaction(peer);
|
||||
if (!start_closing_in_transaction(peer)) {
|
||||
db_abort_transaction(peer);
|
||||
goto fail;
|
||||
}
|
||||
start_closing_in_transaction(peer);
|
||||
|
||||
if (db_commit_transaction(peer) != NULL)
|
||||
goto fail;
|
||||
return pkt_err(peer, "database error");
|
||||
return NULL;
|
||||
|
||||
fail:
|
||||
return pkt_err(peer, "database error");
|
||||
}
|
||||
|
||||
/* This is the io loop while we're doing shutdown. */
|
||||
@ -1433,12 +1427,9 @@ static bool peer_start_shutdown(struct peer *peer)
|
||||
set_peer_state(peer, newstate, __func__, true);
|
||||
|
||||
/* Catch case where we've exchanged and had no HTLCs anyway. */
|
||||
if (peer->closing.their_script && !committed_to_htlcs(peer)) {
|
||||
if (!start_closing_in_transaction(peer)) {
|
||||
db_abort_transaction(peer);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (peer->closing.their_script && !committed_to_htlcs(peer))
|
||||
start_closing_in_transaction(peer);
|
||||
|
||||
return db_commit_transaction(peer) == NULL;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user