mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 15:44:21 +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:
@@ -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
|
/* For first time, we are in transaction to make it atomic with peer->state
|
||||||
* update. Later calls are not. */
|
* update. Later calls are not. */
|
||||||
/* FIXME: make caller wrap in transaction. */
|
/* 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);
|
const char *ctx = tal(peer, char);
|
||||||
bool ok;
|
|
||||||
const char *peerid = pubkey_to_hexstr(ctx, peer->dstate->secpctx, peer->id);
|
const char *peerid = pubkey_to_hexstr(ctx, peer->dstate->secpctx, peer->id);
|
||||||
|
|
||||||
log_debug(peer->log, "%s(%s)", __func__, peerid);
|
log_debug(peer->log, "%s(%s)", __func__, peerid);
|
||||||
|
|
||||||
ok = db_exec(__func__, peer->dstate,
|
db_exec(__func__, peer->dstate,
|
||||||
"UPDATE closing SET our_fee=%"PRIu64", closing_order=%"PRIi64" WHERE peer=x'%s';",
|
"UPDATE closing SET our_fee=%"PRIu64", closing_order=%"PRIi64" WHERE peer=x'%s';",
|
||||||
peer->closing.our_fee,
|
peer->closing.our_fee,
|
||||||
peer->closing.closing_order,
|
peer->closing.closing_order,
|
||||||
peerid);
|
peerid);
|
||||||
tal_free(ctx);
|
tal_free(ctx);
|
||||||
return ok;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool db_update_their_closing(struct peer *peer)
|
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);
|
const struct peer_address *addr);
|
||||||
|
|
||||||
/* Must NOT be inside transaction. */
|
/* Must NOT be inside transaction. */
|
||||||
bool db_update_our_closing(struct peer *peer);
|
|
||||||
bool db_update_their_closing(struct peer *peer);
|
bool db_update_their_closing(struct peer *peer);
|
||||||
bool db_set_their_closing_script(struct peer *peer);
|
bool db_set_their_closing_script(struct peer *peer);
|
||||||
bool db_new_pay_command(struct lightningd_state *dstate,
|
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_update_state(struct peer *peer);
|
||||||
void db_begin_shutdown(struct peer *peer);
|
void db_begin_shutdown(struct peer *peer);
|
||||||
void db_set_our_closing_script(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,
|
void db_add_commit_map(struct peer *peer,
|
||||||
const struct sha256_double *txid, u64 commit_num);
|
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++;
|
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,
|
return peer_comms_err(peer,
|
||||||
pkt_err(peer, "Database error"));
|
pkt_err(peer, "Database error"));
|
||||||
}
|
}
|
||||||
@@ -1283,7 +1285,7 @@ static void peer_calculate_close_fee(struct peer *peer)
|
|||||||
assert(!(peer->closing.our_fee & 1));
|
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));
|
assert(!committed_to_htlcs(peer));
|
||||||
|
|
||||||
@@ -1291,26 +1293,18 @@ static bool start_closing_in_transaction(struct peer *peer)
|
|||||||
|
|
||||||
peer_calculate_close_fee(peer);
|
peer_calculate_close_fee(peer);
|
||||||
peer->closing.closing_order = peer->order_counter++;
|
peer->closing.closing_order = peer->order_counter++;
|
||||||
if (!db_update_our_closing(peer))
|
db_update_our_closing(peer);
|
||||||
return false;
|
|
||||||
queue_pkt_close_signature(peer);
|
queue_pkt_close_signature(peer);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Pkt *start_closing(struct peer *peer)
|
static Pkt *start_closing(struct peer *peer)
|
||||||
{
|
{
|
||||||
db_start_transaction(peer);
|
db_start_transaction(peer);
|
||||||
if (!start_closing_in_transaction(peer)) {
|
start_closing_in_transaction(peer);
|
||||||
db_abort_transaction(peer);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (db_commit_transaction(peer) != NULL)
|
if (db_commit_transaction(peer) != NULL)
|
||||||
goto fail;
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
return pkt_err(peer, "database error");
|
return pkt_err(peer, "database error");
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This is the io loop while we're doing shutdown. */
|
/* 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);
|
set_peer_state(peer, newstate, __func__, true);
|
||||||
|
|
||||||
/* Catch case where we've exchanged and had no HTLCs anyway. */
|
/* Catch case where we've exchanged and had no HTLCs anyway. */
|
||||||
if (peer->closing.their_script && !committed_to_htlcs(peer)) {
|
if (peer->closing.their_script && !committed_to_htlcs(peer))
|
||||||
if (!start_closing_in_transaction(peer)) {
|
start_closing_in_transaction(peer);
|
||||||
db_abort_transaction(peer);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return db_commit_transaction(peer) == NULL;
|
return db_commit_transaction(peer) == NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user