mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-17 19:03:42 +01:00
db: save error, return it when we commit transaction.
This saves a lot of error handling, and puts it in the place we care about. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
ea41386ed3
commit
5bcc9047b0
812
daemon/db.c
812
daemon/db.c
File diff suppressed because it is too large
Load Diff
40
daemon/db.h
40
daemon/db.h
@ -7,12 +7,11 @@
|
||||
void db_init(struct lightningd_state *dstate);
|
||||
|
||||
bool db_create_peer(struct peer *peer);
|
||||
bool db_set_anchor(struct peer *peer);
|
||||
bool db_set_visible_state(struct peer *peer);
|
||||
|
||||
bool db_start_transaction(struct peer *peer);
|
||||
void db_start_transaction(struct peer *peer);
|
||||
void db_abort_transaction(struct peer *peer);
|
||||
bool db_commit_transaction(struct peer *peer);
|
||||
const char *db_commit_transaction(struct peer *peer);
|
||||
|
||||
void db_add_wallet_privkey(struct lightningd_state *dstate,
|
||||
const struct privkey *privkey);
|
||||
@ -21,12 +20,9 @@ bool db_add_peer_address(struct lightningd_state *dstate,
|
||||
const struct peer_address *addr);
|
||||
|
||||
/* Must NOT be inside transaction. */
|
||||
bool db_htlc_fulfilled(struct peer *peer, const struct htlc *htlc);
|
||||
bool db_htlc_failed(struct peer *peer, const struct htlc *htlc);
|
||||
bool db_set_our_closing_script(struct peer *peer);
|
||||
bool db_set_their_closing_script(struct peer *peer);
|
||||
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,
|
||||
const struct sha256 *rhash,
|
||||
const struct pubkey *ids,
|
||||
@ -49,26 +45,30 @@ bool db_remove_invoice(struct lightningd_state *dstate,
|
||||
* which have to be inside transaction anyway. */
|
||||
|
||||
/* Must be inside transaction. */
|
||||
bool db_new_htlc(struct peer *peer, const struct htlc *htlc);
|
||||
bool db_new_feechange(struct peer *peer, const struct feechange *feechange);
|
||||
bool db_update_htlc_state(struct peer *peer, const struct htlc *htlc,
|
||||
void db_set_anchor(struct peer *peer);
|
||||
void db_new_htlc(struct peer *peer, const struct htlc *htlc);
|
||||
void db_new_feechange(struct peer *peer, const struct feechange *feechange);
|
||||
void db_htlc_fulfilled(struct peer *peer, const struct htlc *htlc);
|
||||
void db_htlc_failed(struct peer *peer, const struct htlc *htlc);
|
||||
void db_update_htlc_state(struct peer *peer, const struct htlc *htlc,
|
||||
enum htlc_state oldstate);
|
||||
bool db_complete_pay_command(struct lightningd_state *dstate,
|
||||
void db_complete_pay_command(struct lightningd_state *dstate,
|
||||
const struct htlc *htlc);
|
||||
bool db_resolve_invoice(struct lightningd_state *dstate,
|
||||
void db_resolve_invoice(struct lightningd_state *dstate,
|
||||
const char *label, u64 paid_num);
|
||||
bool db_update_feechange_state(struct peer *peer,
|
||||
void db_update_feechange_state(struct peer *peer,
|
||||
const struct feechange *f,
|
||||
enum htlc_state oldstate);
|
||||
bool db_new_commit_info(struct peer *peer, enum side side,
|
||||
void db_new_commit_info(struct peer *peer, enum side side,
|
||||
const struct sha256 *prev_rhash);
|
||||
bool db_remove_their_prev_revocation_hash(struct peer *peer);
|
||||
bool db_update_next_revocation_hash(struct peer *peer);
|
||||
bool db_save_shachain(struct peer *peer);
|
||||
bool db_update_state(struct peer *peer);
|
||||
bool db_begin_shutdown(struct peer *peer);
|
||||
void db_remove_their_prev_revocation_hash(struct peer *peer);
|
||||
void db_update_next_revocation_hash(struct peer *peer);
|
||||
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);
|
||||
|
||||
bool db_add_commit_map(struct peer *peer,
|
||||
void db_add_commit_map(struct peer *peer,
|
||||
const struct sha256_double *txid, u64 commit_num);
|
||||
|
||||
void db_forget_peer(struct peer *peer);
|
||||
|
@ -103,7 +103,7 @@ struct feechange *new_feechange(struct peer *peer,
|
||||
return f;
|
||||
}
|
||||
|
||||
bool feechange_changestate(struct peer *peer,
|
||||
void feechange_changestate(struct peer *peer,
|
||||
struct feechange *f,
|
||||
enum feechange_state oldstate,
|
||||
enum feechange_state newstate,
|
||||
@ -133,10 +133,9 @@ bool feechange_changestate(struct peer *peer,
|
||||
if (db_commit) {
|
||||
if (newstate == RCVD_FEECHANGE_COMMIT
|
||||
|| newstate == SENT_FEECHANGE_COMMIT)
|
||||
return db_new_feechange(peer, f);
|
||||
|
||||
return db_update_feechange_state(peer, f, oldstate);
|
||||
db_new_feechange(peer, f);
|
||||
else
|
||||
db_update_feechange_state(peer, f, oldstate);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ static inline enum side feechange_side(enum feechange_state state)
|
||||
}
|
||||
}
|
||||
|
||||
bool feechange_changestate(struct peer *peer,
|
||||
void feechange_changestate(struct peer *peer,
|
||||
struct feechange *feechange,
|
||||
enum feechange_state oldstate,
|
||||
enum feechange_state newstate,
|
||||
|
@ -151,7 +151,7 @@ int htlc_state_flags(enum htlc_state state)
|
||||
return per_state_bits[state];
|
||||
}
|
||||
|
||||
bool htlc_changestate(struct htlc *h,
|
||||
void htlc_changestate(struct htlc *h,
|
||||
enum htlc_state oldstate,
|
||||
enum htlc_state newstate,
|
||||
bool db_commit)
|
||||
@ -170,16 +170,17 @@ bool htlc_changestate(struct htlc *h,
|
||||
h->state = newstate;
|
||||
|
||||
if (db_commit) {
|
||||
if (newstate == RCVD_ADD_COMMIT || newstate == SENT_ADD_COMMIT)
|
||||
return db_new_htlc(h->peer, h);
|
||||
if (newstate == RCVD_ADD_COMMIT || newstate == SENT_ADD_COMMIT) {
|
||||
db_new_htlc(h->peer, h);
|
||||
return;
|
||||
}
|
||||
/* These never hit the database. */
|
||||
if (oldstate == RCVD_REMOVE_HTLC)
|
||||
oldstate = SENT_ADD_ACK_REVOCATION;
|
||||
else if (oldstate == SENT_REMOVE_HTLC)
|
||||
oldstate = RCVD_ADD_ACK_REVOCATION;
|
||||
return db_update_htlc_state(h->peer, h, oldstate);
|
||||
db_update_htlc_state(h->peer, h, oldstate);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void htlc_undostate(struct htlc *h,
|
||||
|
@ -69,7 +69,7 @@ struct htlc {
|
||||
|
||||
const char *htlc_state_name(enum htlc_state s);
|
||||
enum htlc_state htlc_state_from_name(const char *name);
|
||||
bool htlc_changestate(struct htlc *h,
|
||||
void htlc_changestate(struct htlc *h,
|
||||
enum htlc_state oldstate,
|
||||
enum htlc_state newstate,
|
||||
bool db_commit);
|
||||
|
@ -82,7 +82,7 @@ static void tell_waiter(struct command *cmd, const struct invoice *paid)
|
||||
command_success(cmd, response);
|
||||
}
|
||||
|
||||
bool resolve_invoice(struct lightningd_state *dstate,
|
||||
void resolve_invoice(struct lightningd_state *dstate,
|
||||
struct invoice *invoice)
|
||||
{
|
||||
struct invoice_waiter *w;
|
||||
@ -97,7 +97,7 @@ bool resolve_invoice(struct lightningd_state *dstate,
|
||||
list)) != NULL)
|
||||
tell_waiter(w->cmd, invoice);
|
||||
|
||||
return db_resolve_invoice(dstate, invoice->label, invoice->paid_num);
|
||||
db_resolve_invoice(dstate, invoice->label, invoice->paid_num);
|
||||
}
|
||||
|
||||
static void json_invoice(struct command *cmd,
|
||||
|
@ -23,7 +23,7 @@ void invoice_add(struct lightningd_state *dstate,
|
||||
const char *label,
|
||||
u64 complete);
|
||||
|
||||
bool resolve_invoice(struct lightningd_state *dstate,
|
||||
void resolve_invoice(struct lightningd_state *dstate,
|
||||
struct invoice *invoice);
|
||||
|
||||
struct invoice *find_unpaid(struct lightningd_state *dstate,
|
||||
|
151
daemon/peer.c
151
daemon/peer.c
@ -63,7 +63,7 @@ static bool command_htlc_fail(struct peer *peer, struct htlc *htlc);
|
||||
static bool command_htlc_fulfill(struct peer *peer, struct htlc *htlc);
|
||||
static void try_commit(struct peer *peer);
|
||||
|
||||
bool peer_add_their_commit(struct peer *peer,
|
||||
void peer_add_their_commit(struct peer *peer,
|
||||
const struct sha256_double *txid, u64 commit_num)
|
||||
{
|
||||
struct their_commit *tc = tal(peer, struct their_commit);
|
||||
@ -71,7 +71,7 @@ bool peer_add_their_commit(struct peer *peer,
|
||||
tc->commit_num = commit_num;
|
||||
list_add_tail(&peer->their_commits, &tc->list);
|
||||
|
||||
return db_add_commit_map(peer, txid, commit_num);
|
||||
db_add_commit_map(peer, txid, commit_num);
|
||||
}
|
||||
|
||||
/* Create a bitcoin close tx, using last signature they sent. */
|
||||
@ -287,7 +287,7 @@ void peer_open_complete(struct peer *peer, const char *problem)
|
||||
log_debug(peer->log, "peer open complete");
|
||||
}
|
||||
|
||||
static bool set_peer_state(struct peer *peer, enum state newstate,
|
||||
static void set_peer_state(struct peer *peer, enum state newstate,
|
||||
const char *caller, bool db_commit)
|
||||
{
|
||||
log_debug(peer->log, "%s: %s => %s", caller,
|
||||
@ -299,8 +299,7 @@ static bool set_peer_state(struct peer *peer, enum state newstate,
|
||||
peer->nc = tal_free(peer->nc);
|
||||
|
||||
if (db_commit)
|
||||
return db_update_state(peer);
|
||||
return true;
|
||||
db_update_state(peer);
|
||||
}
|
||||
|
||||
static void peer_breakdown(struct peer *peer)
|
||||
@ -564,13 +563,7 @@ static void their_htlc_added(struct peer *peer, struct htlc *htlc,
|
||||
log_info(peer->log, "Immediately resolving '%s' HTLC %"PRIu64,
|
||||
invoice->label, htlc->id);
|
||||
|
||||
if (!resolve_invoice(peer->dstate, invoice)) {
|
||||
command_htlc_set_fail(peer, htlc,
|
||||
INTERNAL_SERVER_ERROR_500,
|
||||
"database error");
|
||||
return;
|
||||
}
|
||||
|
||||
resolve_invoice(peer->dstate, invoice);
|
||||
set_htlc_rval(peer, htlc, &invoice->r);
|
||||
command_htlc_fulfill(peer, htlc);
|
||||
goto free_rest;
|
||||
@ -792,9 +785,8 @@ static const char *changestates(struct peer *peer,
|
||||
if (!adjust_cstates(peer, h,
|
||||
table[i].from, table[i].to))
|
||||
return "accounting error";
|
||||
if (!htlc_changestate(h, table[i].from,
|
||||
table[i].to, db_commit))
|
||||
return "database error";
|
||||
htlc_changestate(h, table[i].from,
|
||||
table[i].to, db_commit);
|
||||
check_both_committed(peer, h);
|
||||
changed = true;
|
||||
}
|
||||
@ -806,10 +798,9 @@ static const char *changestates(struct peer *peer,
|
||||
if (!f)
|
||||
continue;
|
||||
adjust_cstates_fee(peer, f, ftable[i].from, ftable[i].to);
|
||||
if (!feechange_changestate(peer, f,
|
||||
ftable[i].from, ftable[i].to,
|
||||
db_commit))
|
||||
return "database error";
|
||||
feechange_changestate(peer, f,
|
||||
ftable[i].from, ftable[i].to,
|
||||
db_commit);
|
||||
changed = true;
|
||||
}
|
||||
|
||||
@ -978,8 +969,7 @@ static Pkt *handle_pkt_commit(struct peer *peer, const Pkt *pkt)
|
||||
|
||||
ci = new_commit_info(peer, peer->local.commit->commit_num + 1);
|
||||
|
||||
if (!db_start_transaction(peer))
|
||||
return pkt_err(peer, "database error");
|
||||
db_start_transaction(peer);
|
||||
|
||||
/* BOLT #2:
|
||||
*
|
||||
@ -1048,10 +1038,7 @@ static Pkt *handle_pkt_commit(struct peer *peer, const Pkt *pkt)
|
||||
peer->local.commit = ci;
|
||||
peer->local.commit->order = peer->order_counter++;
|
||||
|
||||
if (!db_new_commit_info(peer, LOCAL, NULL)) {
|
||||
db_abort_transaction(peer);
|
||||
return pkt_err(peer, "Database error");
|
||||
}
|
||||
db_new_commit_info(peer, LOCAL, NULL);
|
||||
peer_get_revocation_hash(peer, ci->commit_num + 1,
|
||||
&peer->local.next_revocation_hash);
|
||||
peer->their_commitsigs++;
|
||||
@ -1080,7 +1067,7 @@ static Pkt *handle_pkt_commit(struct peer *peer, const Pkt *pkt)
|
||||
if (peer_uncommitted_changes(peer))
|
||||
remote_changes_pending(peer);
|
||||
|
||||
if (!db_commit_transaction(peer))
|
||||
if (db_commit_transaction(peer) != NULL)
|
||||
return pkt_err(peer, "Database error");
|
||||
|
||||
queue_pkt_revocation(peer, &preimage, &peer->local.next_revocation_hash);
|
||||
@ -1133,13 +1120,12 @@ static Pkt *handle_pkt_htlc_fail(struct peer *peer, const Pkt *pkt)
|
||||
htlc->fail = tal_free(htlc->fail);
|
||||
}
|
||||
|
||||
if (!db_start_transaction(peer))
|
||||
return pkt_err(peer, "database error");
|
||||
db_start_transaction(peer);
|
||||
|
||||
set_htlc_fail(peer, htlc, fail, tal_count(fail));
|
||||
tal_free(fail);
|
||||
|
||||
if (!db_commit_transaction(peer))
|
||||
if (db_commit_transaction(peer) != NULL)
|
||||
return pkt_err(peer, "database error");
|
||||
|
||||
cstate_fail_htlc(peer->local.staging_cstate, htlc);
|
||||
@ -1165,14 +1151,12 @@ static Pkt *handle_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt)
|
||||
|
||||
/* Reconnect may mean HTLC was already fulfilled. That's OK. */
|
||||
if (!htlc->r) {
|
||||
if (!db_start_transaction(peer))
|
||||
return pkt_err(peer, "database error");
|
||||
|
||||
db_start_transaction(peer);
|
||||
set_htlc_rval(peer, htlc, &r);
|
||||
|
||||
/* We can relay this upstream immediately. */
|
||||
our_htlc_fulfilled(peer, htlc);
|
||||
if (!db_commit_transaction(peer))
|
||||
if (db_commit_transaction(peer) != NULL)
|
||||
return pkt_err(peer, "database error");
|
||||
}
|
||||
|
||||
@ -1250,8 +1234,7 @@ static Pkt *handle_pkt_revocation(struct peer *peer, const Pkt *pkt,
|
||||
* The receiver of `update_revocation`... MUST add the remote
|
||||
* unacked changes to the set of local acked changes.
|
||||
*/
|
||||
if (!db_start_transaction(peer))
|
||||
return pkt_err(peer, "database error");
|
||||
db_start_transaction(peer);
|
||||
errmsg = changestates(peer, changes, ARRAY_SIZE(changes),
|
||||
feechanges, ARRAY_SIZE(feechanges), true);
|
||||
if (errmsg) {
|
||||
@ -1260,23 +1243,11 @@ static Pkt *handle_pkt_revocation(struct peer *peer, const Pkt *pkt,
|
||||
return pkt_err(peer, "failure accepting update_revocation: %s",
|
||||
errmsg);
|
||||
}
|
||||
if (!db_save_shachain(peer)) {
|
||||
db_abort_transaction(peer);
|
||||
return pkt_err(peer, "database error");
|
||||
}
|
||||
if (!db_update_next_revocation_hash(peer)) {
|
||||
db_abort_transaction(peer);
|
||||
return pkt_err(peer, "database error");
|
||||
}
|
||||
if (!set_peer_state(peer, next_state, __func__, true)) {
|
||||
db_abort_transaction(peer);
|
||||
return pkt_err(peer, "database error");
|
||||
}
|
||||
if (!db_remove_their_prev_revocation_hash(peer)) {
|
||||
db_abort_transaction(peer);
|
||||
return pkt_err(peer, "database error");
|
||||
}
|
||||
if (!db_commit_transaction(peer))
|
||||
db_save_shachain(peer);
|
||||
db_update_next_revocation_hash(peer);
|
||||
set_peer_state(peer, next_state, __func__, true);
|
||||
db_remove_their_prev_revocation_hash(peer);
|
||||
if (db_commit_transaction(peer) != NULL)
|
||||
return pkt_err(peer, "database error");
|
||||
|
||||
return NULL;
|
||||
@ -1316,8 +1287,7 @@ static bool start_closing_in_transaction(struct peer *peer)
|
||||
{
|
||||
assert(!committed_to_htlcs(peer));
|
||||
|
||||
if (!set_peer_state(peer, STATE_MUTUAL_CLOSING, __func__, true))
|
||||
return false;
|
||||
set_peer_state(peer, STATE_MUTUAL_CLOSING, __func__, true);
|
||||
|
||||
peer_calculate_close_fee(peer);
|
||||
peer->closing.closing_order = peer->order_counter++;
|
||||
@ -1329,15 +1299,13 @@ static bool start_closing_in_transaction(struct peer *peer)
|
||||
|
||||
static Pkt *start_closing(struct peer *peer)
|
||||
{
|
||||
if (!db_start_transaction(peer))
|
||||
goto fail;
|
||||
|
||||
db_start_transaction(peer);
|
||||
if (!start_closing_in_transaction(peer)) {
|
||||
db_abort_transaction(peer);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (!db_commit_transaction(peer))
|
||||
if (db_commit_transaction(peer) != NULL)
|
||||
goto fail;
|
||||
return NULL;
|
||||
|
||||
@ -1432,13 +1400,9 @@ static bool peer_start_shutdown(struct peer *peer)
|
||||
enum state newstate;
|
||||
u8 *redeemscript;
|
||||
|
||||
if (!db_start_transaction(peer))
|
||||
return false;
|
||||
db_start_transaction(peer);
|
||||
|
||||
if (!db_begin_shutdown(peer)) {
|
||||
db_abort_transaction(peer);
|
||||
return false;
|
||||
}
|
||||
db_begin_shutdown(peer);
|
||||
|
||||
/* If they started close, we might not have sent ours. */
|
||||
assert(!peer->closing.our_script);
|
||||
@ -1456,10 +1420,7 @@ static bool peer_start_shutdown(struct peer *peer)
|
||||
* not already) after receiving `close_shutdown`.
|
||||
*/
|
||||
peer->closing.shutdown_order = peer->order_counter++;
|
||||
if (!db_set_our_closing_script(peer)) {
|
||||
db_abort_transaction(peer);
|
||||
return false;
|
||||
}
|
||||
db_set_our_closing_script(peer);
|
||||
|
||||
queue_pkt_close_shutdown(peer);
|
||||
|
||||
@ -1469,10 +1430,7 @@ static bool peer_start_shutdown(struct peer *peer)
|
||||
assert(peer->state == STATE_NORMAL);
|
||||
newstate = STATE_SHUTDOWN;
|
||||
}
|
||||
if (!set_peer_state(peer, newstate, __func__, true)) {
|
||||
db_abort_transaction(peer);
|
||||
return false;
|
||||
}
|
||||
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)) {
|
||||
@ -1481,7 +1439,7 @@ static bool peer_start_shutdown(struct peer *peer)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return db_commit_transaction(peer);
|
||||
return db_commit_transaction(peer) == NULL;
|
||||
}
|
||||
|
||||
/* This is the io loop while we're in normal mode. */
|
||||
@ -1586,9 +1544,9 @@ static void state_single(struct peer *peer,
|
||||
io_wake(peer);
|
||||
} else if (!state_is_opening(peer->state)) {
|
||||
/* Now in STATE_NORMAL, so save. */
|
||||
if (!db_start_transaction(peer)
|
||||
|| !db_update_state(peer)
|
||||
|| !db_commit_transaction(peer)) {
|
||||
db_start_transaction(peer);
|
||||
db_update_state(peer);
|
||||
if (db_commit_transaction(peer) != NULL) {
|
||||
peer_fail(peer, __func__);
|
||||
|
||||
/* Start output if not running already; it will close conn. */
|
||||
@ -2234,8 +2192,7 @@ static void do_commit(struct peer *peer, struct command *jsoncmd)
|
||||
= tal_dup(peer, struct sha256,
|
||||
&peer->remote.commit->revocation_hash);
|
||||
|
||||
if (!db_start_transaction(peer))
|
||||
goto database_error;
|
||||
db_start_transaction(peer);
|
||||
|
||||
errmsg = changestates(peer, changes, ARRAY_SIZE(changes),
|
||||
feechanges, ARRAY_SIZE(feechanges), true);
|
||||
@ -2272,12 +2229,11 @@ static void do_commit(struct peer *peer, struct command *jsoncmd)
|
||||
tal_free(peer->remote.commit);
|
||||
peer->remote.commit = ci;
|
||||
peer->remote.commit->order = peer->order_counter++;
|
||||
if (!db_new_commit_info(peer, REMOTE, peer->their_prev_revocation_hash))
|
||||
goto database_error;
|
||||
db_new_commit_info(peer, REMOTE, peer->their_prev_revocation_hash);
|
||||
|
||||
/* We don't need to remember their commit if we don't give sig. */
|
||||
if (ci->sig && !peer_add_their_commit(peer, &ci->txid, ci->commit_num))
|
||||
goto database_error;
|
||||
if (ci->sig)
|
||||
peer_add_their_commit(peer, &ci->txid, ci->commit_num);
|
||||
|
||||
if (peer->state == STATE_SHUTDOWN) {
|
||||
set_peer_state(peer, STATE_SHUTDOWN_COMMITTING, __func__, true);
|
||||
@ -2285,7 +2241,7 @@ static void do_commit(struct peer *peer, struct command *jsoncmd)
|
||||
assert(peer->state == STATE_NORMAL);
|
||||
set_peer_state(peer, STATE_NORMAL_COMMITTING, __func__, true);
|
||||
}
|
||||
if (!db_commit_transaction(peer))
|
||||
if (db_commit_transaction(peer) != NULL)
|
||||
goto database_error;
|
||||
|
||||
queue_pkt_commit(peer, ci->sig);
|
||||
@ -2975,14 +2931,11 @@ static void check_htlc_expiry(struct peer *peer)
|
||||
if (height <= abs_locktime_to_blocks(&h->expiry))
|
||||
continue;
|
||||
|
||||
if (!db_start_transaction(peer)) {
|
||||
peer_fail(peer, __func__);
|
||||
return;
|
||||
}
|
||||
db_start_transaction(peer);
|
||||
/* This can fail only if we're in an error state. */
|
||||
command_htlc_set_fail(peer, h,
|
||||
REQUEST_TIMEOUT_408, "timed out");
|
||||
if (!db_commit_transaction(peer)) {
|
||||
if (db_commit_transaction(peer) != NULL) {
|
||||
peer_fail(peer, __func__);
|
||||
return;
|
||||
}
|
||||
@ -4530,17 +4483,16 @@ static void json_fulfillhtlc(struct command *cmd,
|
||||
/* This can happen if we're disconnected, and thus haven't sent
|
||||
* fulfill yet; we stored r in database immediately. */
|
||||
if (!htlc->r) {
|
||||
if (!db_start_transaction(peer)) {
|
||||
command_fail(cmd, "database error");
|
||||
return;
|
||||
}
|
||||
const char *db_err;
|
||||
|
||||
db_start_transaction(peer);
|
||||
set_htlc_rval(peer, htlc, &r);
|
||||
|
||||
/* We can relay this upstream immediately. */
|
||||
our_htlc_fulfilled(peer, htlc);
|
||||
if (!db_commit_transaction(peer)) {
|
||||
command_fail(cmd, "database error");
|
||||
db_err = db_commit_transaction(peer);
|
||||
if (db_err) {
|
||||
command_fail(cmd, "%s", db_err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -4567,6 +4519,7 @@ static void json_failhtlc(struct command *cmd,
|
||||
jsmntok_t *peeridtok, *idtok, *reasontok;
|
||||
u64 id;
|
||||
struct htlc *htlc;
|
||||
const char *db_err;
|
||||
|
||||
if (!json_get_params(buffer, params,
|
||||
"peerid", &peeridtok,
|
||||
@ -4612,16 +4565,14 @@ static void json_failhtlc(struct command *cmd,
|
||||
return;
|
||||
}
|
||||
|
||||
if (!db_start_transaction(peer)) {
|
||||
command_fail(cmd, "database error");
|
||||
return;
|
||||
}
|
||||
db_start_transaction(peer);
|
||||
|
||||
set_htlc_fail(peer, htlc, buffer + reasontok->start,
|
||||
reasontok->end - reasontok->start);
|
||||
|
||||
if (!db_commit_transaction(peer)) {
|
||||
command_fail(cmd, "database error");
|
||||
db_err = db_commit_transaction(peer);
|
||||
if (db_err) {
|
||||
command_fail(cmd, "%s", db_err);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -247,7 +247,7 @@ struct peer *new_peer(struct lightningd_state *dstate,
|
||||
bool setup_first_commit(struct peer *peer);
|
||||
|
||||
/* Whenever we send a signature, remember the txid -> commit_num mapping */
|
||||
bool peer_add_their_commit(struct peer *peer,
|
||||
void peer_add_their_commit(struct peer *peer,
|
||||
const struct sha256_double *txid, u64 commit_num);
|
||||
|
||||
/* Allocate a new commit_info struct. */
|
||||
|
@ -5,10 +5,10 @@
|
||||
|
||||
/* AUTOGENERATED MOCKS START */
|
||||
/* Generated stub for db_new_htlc */
|
||||
bool db_new_htlc(struct peer *peer UNNEEDED, const struct htlc *htlc UNNEEDED)
|
||||
void db_new_htlc(struct peer *peer UNNEEDED, const struct htlc *htlc UNNEEDED)
|
||||
{ fprintf(stderr, "db_new_htlc called!\n"); abort(); }
|
||||
/* Generated stub for db_update_htlc_state */
|
||||
bool db_update_htlc_state(struct peer *peer UNNEEDED, const struct htlc *htlc UNNEEDED,
|
||||
void db_update_htlc_state(struct peer *peer UNNEEDED, const struct htlc *htlc UNNEEDED,
|
||||
enum htlc_state oldstate UNNEEDED)
|
||||
{ fprintf(stderr, "db_update_htlc_state called!\n"); abort(); }
|
||||
/* Generated stub for log_ */
|
||||
|
68
state.c
68
state.c
@ -124,6 +124,8 @@ enum state state(struct peer *peer,
|
||||
break;
|
||||
case STATE_OPEN_WAIT_FOR_ANCHOR:
|
||||
if (input_is(input, PKT_OPEN_ANCHOR)) {
|
||||
const char *db_err;
|
||||
|
||||
err = accept_pkt_anchor(peer, pkt);
|
||||
if (err) {
|
||||
peer_open_complete(peer, err->error->problem);
|
||||
@ -150,34 +152,16 @@ enum state state(struct peer *peer,
|
||||
&peer->remote.commit->sig->sig);
|
||||
|
||||
peer->remote.commit->order = peer->order_counter++;
|
||||
if (!db_start_transaction(peer)) {
|
||||
db_start_transaction(peer);
|
||||
db_set_anchor(peer);
|
||||
db_new_commit_info(peer, REMOTE, NULL);
|
||||
peer_add_their_commit(peer,
|
||||
&peer->remote.commit->txid,
|
||||
peer->remote.commit->commit_num);
|
||||
db_err = db_commit_transaction(peer);
|
||||
if (db_err) {
|
||||
err = pkt_err(peer, "database error");
|
||||
peer_open_complete(peer, err->error->problem);
|
||||
goto err_breakdown;
|
||||
}
|
||||
if (!db_set_anchor(peer)) {
|
||||
err = pkt_err(peer, "database error");
|
||||
peer_open_complete(peer, err->error->problem);
|
||||
db_abort_transaction(peer);
|
||||
goto err_breakdown;
|
||||
}
|
||||
if (!db_new_commit_info(peer, REMOTE, NULL)) {
|
||||
err = pkt_err(peer, "database error");
|
||||
peer_open_complete(peer, err->error->problem);
|
||||
db_abort_transaction(peer);
|
||||
goto err_breakdown;
|
||||
}
|
||||
if (!peer_add_their_commit(peer,
|
||||
&peer->remote.commit->txid,
|
||||
peer->remote.commit->commit_num)) {
|
||||
err = pkt_err(peer, "database error");
|
||||
peer_open_complete(peer, err->error->problem);
|
||||
db_abort_transaction(peer);
|
||||
goto err_breakdown;
|
||||
}
|
||||
if (!db_commit_transaction(peer)) {
|
||||
err = pkt_err(peer, "database error");
|
||||
peer_open_complete(peer, err->error->problem);
|
||||
peer_open_complete(peer, db_err);
|
||||
goto err_breakdown;
|
||||
}
|
||||
|
||||
@ -195,6 +179,7 @@ enum state state(struct peer *peer,
|
||||
break;
|
||||
case STATE_OPEN_WAIT_FOR_COMMIT_SIG:
|
||||
if (input_is(input, PKT_OPEN_COMMIT_SIG)) {
|
||||
const char *db_err;
|
||||
err = accept_pkt_open_commit_sig(peer, pkt,
|
||||
&peer->local.commit->sig);
|
||||
if (!err &&
|
||||
@ -213,30 +198,15 @@ enum state state(struct peer *peer,
|
||||
}
|
||||
peer->their_commitsigs++;
|
||||
|
||||
if (!db_start_transaction(peer)) {
|
||||
db_start_transaction(peer);
|
||||
db_set_anchor(peer);
|
||||
db_new_commit_info(peer, LOCAL, NULL);
|
||||
db_err = db_commit_transaction(peer);
|
||||
|
||||
if (db_err) {
|
||||
bitcoin_release_anchor(peer, INPUT_NONE);
|
||||
err = pkt_err(peer, "database error");
|
||||
peer_open_complete(peer, err->error->problem);
|
||||
goto err_breakdown;
|
||||
}
|
||||
if (!db_set_anchor(peer)) {
|
||||
bitcoin_release_anchor(peer, INPUT_NONE);
|
||||
err = pkt_err(peer, "database error");
|
||||
peer_open_complete(peer, err->error->problem);
|
||||
db_abort_transaction(peer);
|
||||
goto err_breakdown;
|
||||
}
|
||||
if (!db_new_commit_info(peer, LOCAL, NULL)) {
|
||||
bitcoin_release_anchor(peer, INPUT_NONE);
|
||||
err = pkt_err(peer, "database error");
|
||||
peer_open_complete(peer, err->error->problem);
|
||||
db_abort_transaction(peer);
|
||||
goto err_breakdown;
|
||||
}
|
||||
if (!db_commit_transaction(peer)) {
|
||||
bitcoin_release_anchor(peer, INPUT_NONE);
|
||||
err = pkt_err(peer, "database error");
|
||||
peer_open_complete(peer, err->error->problem);
|
||||
peer_open_complete(peer, db_err);
|
||||
goto err_breakdown;
|
||||
}
|
||||
queue_tx_broadcast(broadcast, bitcoin_anchor(peer));
|
||||
|
Loading…
Reference in New Issue
Block a user