wallet: Add utxo_is_immature helper

This commit is contained in:
Christian Decker
2022-11-08 16:06:55 +01:00
parent adf14151fa
commit eb122827f6
5 changed files with 34 additions and 14 deletions

View File

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

View File

@@ -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 */

View File

@@ -26,6 +26,7 @@ WALLET_TEST_COMMON_OBJS := \
common/setup.o \
common/timeout.o \
common/utils.o \
common/utxo.o \
common/wireaddr.o \
common/version.o \
wallet/db_sqlite3_sqlgen.o \

View File

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

View File

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