Skip to content

Commit

Permalink
db: always call db_update_our_closing in a transaction.
Browse files Browse the repository at this point in the history
It's not in a transaction in one caller, so wrap that.
This removes some more error handling code.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Sep 6, 2016
1 parent 5bcc904 commit fce9ee2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
14 changes: 6 additions & 8 deletions daemon/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion daemon/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 10 additions & 19 deletions daemon/peer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down Expand Up @@ -1283,34 +1285,26 @@ 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));

set_peer_state(peer, STATE_MUTUAL_CLOSING, __func__, true);

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. */
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit fce9ee2

Please sign in to comment.