From fce9ee29e3c37b4291ebb050e6a687cfaa7df95a Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 6 Sep 2016 16:47:49 +0930 Subject: [PATCH] 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 --- daemon/db.c | 14 ++++++-------- daemon/db.h | 2 +- daemon/peer.c | 29 ++++++++++------------------- 3 files changed, 17 insertions(+), 28 deletions(-) diff --git a/daemon/db.c b/daemon/db.c index d541cd251..a80dc7a24 100644 --- a/daemon/db.c +++ b/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) diff --git a/daemon/db.h b/daemon/db.h index 284061b53..ce85c1435 100644 --- a/daemon/db.h +++ b/daemon/db.h @@ -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); diff --git a/daemon/peer.c b/daemon/peer.c index 94f40a951..0cde78326 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -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; }