2017-08-28 18:06:01 +02:00
|
|
|
#ifndef LIGHTNING_COMMON_UTXO_H
|
|
|
|
#define LIGHTNING_COMMON_UTXO_H
|
2017-02-21 05:45:29 +01:00
|
|
|
#include "config.h"
|
2021-07-02 21:56:35 +02:00
|
|
|
#include <assert.h>
|
2017-12-18 07:41:52 +01:00
|
|
|
#include <bitcoin/tx.h>
|
2019-04-08 11:58:32 +02:00
|
|
|
#include <common/node_id.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-12-03 00:02:11 +01:00
|
|
|
struct ext_key;
|
|
|
|
|
2017-12-20 12:44:00 +01:00
|
|
|
/* Information needed for their_unilateral/to-us outputs */
|
|
|
|
struct unilateral_close_info {
|
|
|
|
u64 channel_id;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id peer_id;
|
2023-06-26 01:18:21 +02:00
|
|
|
bool option_anchors;
|
2019-09-10 04:24:27 +02:00
|
|
|
/* NULL if this is an option_static_remotekey commitment */
|
|
|
|
struct pubkey *commitment_point;
|
2021-06-16 19:56:36 +02:00
|
|
|
u32 csv;
|
2017-12-20 12:44:00 +01:00
|
|
|
};
|
|
|
|
|
2020-08-28 05:56:34 +02:00
|
|
|
/* Possible states for tracked outputs in the database. Not sure yet
|
|
|
|
* whether we really want to have reservations reflected in the
|
|
|
|
* database, it would simplify queries at the cost of some IO ops */
|
|
|
|
/* /!\ This is a DB ENUM, please do not change the numbering of any
|
|
|
|
* already defined elements (adding is ok) /!\ */
|
|
|
|
enum output_status {
|
|
|
|
OUTPUT_STATE_AVAILABLE = 0,
|
|
|
|
OUTPUT_STATE_RESERVED = 1,
|
|
|
|
OUTPUT_STATE_SPENT = 2,
|
|
|
|
/* Special status used to express that we don't care in
|
|
|
|
* queries */
|
|
|
|
OUTPUT_STATE_ANY = 255
|
|
|
|
};
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
struct utxo {
|
2021-10-13 05:45:36 +02:00
|
|
|
struct bitcoin_outpoint outpoint;
|
2019-02-21 03:39:51 +01:00
|
|
|
struct amount_sat amount;
|
2017-02-21 05:45:29 +01:00
|
|
|
u32 keyindex;
|
|
|
|
bool is_p2sh;
|
2020-08-28 05:56:34 +02:00
|
|
|
enum output_status status;
|
2017-12-20 12:44:00 +01:00
|
|
|
|
|
|
|
/* Optional unilateral close information, NULL if this is just
|
|
|
|
* a HD key */
|
|
|
|
struct unilateral_close_info *close_info;
|
2018-02-26 11:36:48 +01:00
|
|
|
|
|
|
|
/* NULL if we haven't seen it in a block, otherwise the block it's in */
|
2018-03-22 00:13:16 +01:00
|
|
|
const u32 *blockheight;
|
2018-02-26 11:36:48 +01:00
|
|
|
|
|
|
|
/* NULL if not spent yet, otherwise, the block the spending transaction is in */
|
2018-03-22 00:13:16 +01:00
|
|
|
const u32 *spendheight;
|
2019-02-22 15:47:30 +01:00
|
|
|
|
2020-07-14 21:29:26 +02:00
|
|
|
/* Block this utxo becomes unreserved, if applicable */
|
2020-08-28 05:56:34 +02:00
|
|
|
u32 reserved_til;
|
2020-07-14 21:29:26 +02:00
|
|
|
|
2019-02-22 15:47:30 +01:00
|
|
|
/* The scriptPubkey if it is known */
|
|
|
|
u8 *scriptPubkey;
|
2022-10-17 21:59:57 +02:00
|
|
|
|
|
|
|
/* Is this utxo a coinbase output */
|
|
|
|
bool is_in_coinbase;
|
2017-02-21 05:45:29 +01:00
|
|
|
};
|
|
|
|
|
2020-08-28 05:56:34 +02:00
|
|
|
/* We lazy-evaluate whether a utxo is really still reserved. */
|
|
|
|
static inline bool utxo_is_reserved(const struct utxo *utxo, u32 current_height)
|
|
|
|
{
|
|
|
|
if (utxo->status != OUTPUT_STATE_RESERVED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return utxo->reserved_til > current_height;
|
|
|
|
}
|
|
|
|
|
2021-07-02 21:56:35 +02:00
|
|
|
static inline bool utxo_is_csv_locked(const struct utxo *utxo, u32 current_height)
|
|
|
|
{
|
|
|
|
if (!utxo->close_info)
|
|
|
|
return false;
|
2023-06-26 01:18:21 +02:00
|
|
|
/* BOLT #3:
|
|
|
|
* If `option_anchors` applies to the commitment transaction, the
|
|
|
|
* `to_remote` output is encumbered by a one block csv lock.
|
|
|
|
*/
|
|
|
|
if (!utxo->blockheight && utxo->close_info->option_anchors)
|
2021-07-02 21:56:35 +02:00
|
|
|
return true;
|
|
|
|
assert(*utxo->blockheight + utxo->close_info->csv > *utxo->blockheight);
|
|
|
|
return *utxo->blockheight + utxo->close_info->csv > current_height;
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
void towire_utxo(u8 **pptr, const struct utxo *utxo);
|
2018-02-08 02:23:46 +01:00
|
|
|
struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max);
|
2018-12-03 00:02:11 +01:00
|
|
|
|
2020-07-06 07:26:14 +02:00
|
|
|
/* Estimate of (signed) UTXO weight in transaction */
|
2020-11-18 02:45:23 +01:00
|
|
|
size_t utxo_spend_weight(const struct utxo *utxo, size_t min_witness_weight);
|
2022-11-08 16:06:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_COMMON_UTXO_H */
|