diff --git a/common/utxo.h b/common/utxo.h index 4dd1c1642..8ee7fac25 100644 --- a/common/utxo.h +++ b/common/utxo.h @@ -41,7 +41,7 @@ struct utxo { const u32 *spendheight; /* Block this utxo becomes unreserved, if applicable */ - u32 *reserved_til; + u32 reserved_til; /* The scriptPubkey if it is known */ u8 *scriptPubkey; diff --git a/wallet/reservation.c b/wallet/reservation.c index aa20177bf..7e489a6b1 100644 --- a/wallet/reservation.c +++ b/wallet/reservation.c @@ -12,13 +12,13 @@ #include static bool was_reserved(enum output_status oldstatus, - const u32 *reserved_til, + u32 reserved_til, u32 current_height) { if (oldstatus != output_state_reserved) return false; - return *reserved_til > current_height; + return reserved_til > current_height; } static void json_add_reservestatus(struct json_stream *response, @@ -31,12 +31,12 @@ static void json_add_reservestatus(struct json_stream *response, json_add_txid(response, "txid", &utxo->txid); json_add_u32(response, "vout", utxo->outnum); json_add_bool(response, "was_reserved", - was_reserved(oldstatus, &old_res, current_height)); + was_reserved(oldstatus, old_res, current_height)); json_add_bool(response, "reserved", is_reserved(utxo, current_height)); - if (utxo->reserved_til) + if (is_reserved(utxo, current_height)) json_add_u32(response, "reserved_to_block", - *utxo->reserved_til); + utxo->reserved_til); json_object_end(response); } @@ -52,7 +52,7 @@ static void reserve_and_report(struct json_stream *response, u32 old_res; oldstatus = utxos[i]->status; - old_res = utxos[i]->reserved_til ? *utxos[i]->reserved_til : 0; + old_res = utxos[i]->reserved_til; if (!wallet_reserve_utxo(wallet, utxos[i], @@ -156,7 +156,7 @@ static struct command_result *json_unreserveinputs(struct command *cmd, continue; oldstatus = utxo->status; - old_res = *utxo->reserved_til; + old_res = utxo->reserved_til; wallet_unreserve_utxo(cmd->ld->wallet, utxo, diff --git a/wallet/wallet.c b/wallet/wallet.c index 85b74a64f..013105780 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -205,8 +205,7 @@ static struct utxo *wallet_stmt2output(const tal_t *ctx, struct db_stmt *stmt) } /* This column can be null if 0.9.1 db or below. */ - utxo->reserved_til = tal(utxo, u32); - *utxo->reserved_til = db_column_int_or_default(stmt, 13, 0); + utxo->reserved_til = db_column_int_or_default(stmt, 13, 0); return utxo; } @@ -434,10 +433,7 @@ static void db_set_utxo(struct db *db, const struct utxo *utxo) db, SQL("UPDATE outputs SET status=?, reserved_til=?" "WHERE prev_out_tx=? AND prev_out_index=?")); db_bind_int(stmt, 0, output_status_in_db(utxo->status)); - if (utxo->reserved_til) - db_bind_int(stmt, 1, *utxo->reserved_til); - else - db_bind_null(stmt, 1); + db_bind_int(stmt, 1, utxo->reserved_til); db_bind_txid(stmt, 2, &utxo->txid); db_bind_int(stmt, 3, utxo->outnum); db_exec_prepared_v2(take(stmt)); @@ -445,11 +441,6 @@ static void db_set_utxo(struct db *db, const struct utxo *utxo) bool wallet_reserve_utxo(struct wallet *w, struct utxo *utxo, u32 current_height) { - u32 reservation_height; - - if (utxo->status == output_state_reserved) - assert(utxo->reserved_til); - switch (utxo->status) { case output_state_spent: return false; @@ -461,15 +452,12 @@ bool wallet_reserve_utxo(struct wallet *w, struct utxo *utxo, u32 current_height } /* We simple increase existing reservations, which DTRT if we unreserve */ - if (utxo->reserved_til - && *utxo->reserved_til >= current_height) - reservation_height = *utxo->reserved_til + RESERVATION_INC; + if (utxo->reserved_til >= current_height) + utxo->reserved_til += RESERVATION_INC; else - reservation_height = current_height + RESERVATION_INC; + utxo->reserved_til = current_height + RESERVATION_INC; utxo->status = output_state_reserved; - tal_free(utxo->reserved_til); - utxo->reserved_til = tal_dup(utxo, u32, &reservation_height); db_set_utxo(w->db, utxo); @@ -478,23 +466,16 @@ bool wallet_reserve_utxo(struct wallet *w, struct utxo *utxo, u32 current_height void wallet_unreserve_utxo(struct wallet *w, struct utxo *utxo, u32 current_height) { - if (utxo->status == output_state_reserved) { - /* FIXME: old code didn't set reserved_til, so fake it here */ - if (!utxo->reserved_til) - utxo->reserved_til = tal_dup(utxo, u32, ¤t_height); - assert(utxo->reserved_til); - } - if (utxo->status != output_state_reserved) fatal("UTXO %s:%u is not reserved", type_to_string(tmpctx, struct bitcoin_txid, &utxo->txid), utxo->outnum); - if (*utxo->reserved_til <= current_height + RESERVATION_INC) { + if (utxo->reserved_til <= current_height + RESERVATION_INC) { utxo->status = output_state_available; - utxo->reserved_til = tal_free(utxo->reserved_til); + utxo->reserved_til = 0; } else - *utxo->reserved_til -= RESERVATION_INC; + utxo->reserved_til -= RESERVATION_INC; db_set_utxo(w->db, utxo); } diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index c1c69c59b..ce5f3aa94 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -249,11 +249,7 @@ bool is_reserved(const struct utxo *utxo, u32 current_height) if (utxo->status != output_state_reserved) return false; - /* FIXME: Eventually this will always be set! */ - if (!utxo->reserved_til) - return true; - - return *utxo->reserved_til > current_height; + return utxo->reserved_til > current_height; }