Skip to content

Commit

Permalink
wallet: Add utxo_is_immature helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cdecker committed Nov 8, 2022
1 parent 01b6cd5 commit 70d7cff
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
18 changes: 18 additions & 0 deletions common/utxo.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,21 @@ size_t utxo_spend_weight(const struct utxo *utxo, size_t min_witness_weight)

return bitcoin_tx_input_weight(utxo->is_p2sh, wit_weight);
}

u32 utxo_is_immature(const struct utxo *utxo, u32 blockheight)
{
if (utxo->is_in_coinbase) {
/* We got this from a block, it must have a known
* blockheight. */
assert(utxo->blockheight);

if (blockheight < *utxo->blockheight + 100)
return *utxo->blockheight + 99 - blockheight;

else
return 0;
} else {
/* Non-coinbase outputs are always mature. */
return 0;
}
}
7 changes: 7 additions & 0 deletions common/utxo.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max);

/* Estimate of (signed) UTXO weight in transaction */
size_t utxo_spend_weight(const struct utxo *utxo, size_t min_witness_weight);

/**
* Determine how many blocks until a UTXO becomes mature.
*
* Returns 0 for non-coinbase outputs or the number of blocks to mature.
*/
u32 utxo_is_immature(const struct utxo *utxo, u32 blockheight);
#endif /* LIGHTNING_COMMON_UTXO_H */
3 changes: 3 additions & 0 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,9 @@ void try_reconnect(const tal_t *ctx UNNEEDED,
struct peer *peer UNNEEDED,
const struct wireaddr_internal *addrhint UNNEEDED)
{ fprintf(stderr, "try_reconnect called!\n"); abort(); }
/* Generated stub for utxo_is_immature */
u32 utxo_is_immature(const struct utxo *utxo UNNEEDED, u32 blockheight UNNEEDED)
{ fprintf(stderr, "utxo_is_immature called!\n"); abort(); }
/* Generated stub for watch_txid */
struct txwatch *watch_txid(const tal_t *ctx UNNEEDED,
struct chain_topology *topo UNNEEDED,
Expand Down
12 changes: 3 additions & 9 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,9 @@ static bool deep_enough(u32 maxheight, const struct utxo *utxo,
return false;
}

/* If the utxo is a coinbase, we over-write the maxheight
* to the coinbase maxheight (current - 99) */
if (utxo->is_in_coinbase) {
/* Nothing is spendable the first 100 blocks */
if (current_blockheight < 100)
return false;
if (maxheight > current_blockheight - 99)
maxheight = current_blockheight - 99;
}
bool immature = utxo_is_immature(utxo, current_blockheight);
if (immature)
return false;

/* If we require confirmations check that we have a
* confirmation height and that it is below the required
Expand Down
10 changes: 5 additions & 5 deletions wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ static void json_add_utxo(struct json_stream *response,
if (utxo->spendheight)
json_add_string(response, "status", "spent");
else if (utxo->blockheight) {
if (utxo->is_in_coinbase
&& *utxo->blockheight + 99 > current_height) {
json_add_string(response, "status", "immature");
} else
json_add_string(response, "status", "confirmed");
json_add_string(response, "status",
utxo_is_immature(utxo, current_height)
? "immature"
: "confirmed");

json_add_num(response, "blockheight", *utxo->blockheight);
} else
json_add_string(response, "status", "unconfirmed");
Expand Down

0 comments on commit 70d7cff

Please sign in to comment.