Skip to content

Commit

Permalink
utxo: make reserved_til a u32 not a ptr, now it's compsulory.
Browse files Browse the repository at this point in the history
It's 0 for old dbs, which is the same as "available".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Aug 14, 2020
1 parent c2523b9 commit 012f0c8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 40 deletions.
2 changes: 1 addition & 1 deletion common/utxo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions wallet/reservation.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}

Expand All @@ -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],
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 8 additions & 27 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -434,22 +433,14 @@ 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));
}

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;
Expand All @@ -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);

Expand All @@ -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);
}
Expand Down
6 changes: 1 addition & 5 deletions wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down

0 comments on commit 012f0c8

Please sign in to comment.