utxo: make reserved_til a u32 not a ptr, now it's compsulory.

It's 0 for old dbs, which is the same as "available".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2020-08-28 13:26:34 +09:30
parent ea819107eb
commit 438953b8f0
4 changed files with 17 additions and 40 deletions

View File

@ -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;

View File

@ -12,13 +12,13 @@
#include <wallet/walletrpc.h>
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,

View File

@ -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, &current_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);
}

View File

@ -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;
}