2017-08-29 01:36:01 +09:30
|
|
|
#ifndef LIGHTNING_COMMON_UTXO_H
|
|
|
|
#define LIGHTNING_COMMON_UTXO_H
|
2017-02-21 15:15:29 +10:30
|
|
|
#include "config.h"
|
2021-07-02 14:56:35 -05:00
|
|
|
#include <assert.h>
|
2017-12-18 17:11:52 +10:30
|
|
|
#include <bitcoin/tx.h>
|
2019-04-08 19:28:32 +09:30
|
|
|
#include <common/node_id.h>
|
2017-02-21 15:15:29 +10:30
|
|
|
|
2018-12-03 09:32:11 +10:30
|
|
|
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 19:28:32 +09:30
|
|
|
struct node_id peer_id;
|
2023-06-26 08:48:21 +09:30
|
|
|
bool option_anchors;
|
2019-09-10 11:54:27 +09:30
|
|
|
/* NULL if this is an option_static_remotekey commitment */
|
|
|
|
struct pubkey *commitment_point;
|
2021-06-16 12:56:36 -05:00
|
|
|
u32 csv;
|
2017-12-20 12:44:00 +01:00
|
|
|
};
|
|
|
|
|
2020-08-28 13:26:34 +09:30
|
|
|
/* 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 15:15:29 +10:30
|
|
|
struct utxo {
|
2021-10-13 14:15:36 +10:30
|
|
|
struct bitcoin_outpoint outpoint;
|
2019-02-21 13:09:51 +10:30
|
|
|
struct amount_sat amount;
|
2017-02-21 15:15:29 +10:30
|
|
|
u32 keyindex;
|
|
|
|
bool is_p2sh;
|
2020-08-28 13:26:34 +09:30
|
|
|
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-15 04:59:26 +09:30
|
|
|
/* Block this utxo becomes unreserved, if applicable */
|
2020-08-28 13:26:34 +09:30
|
|
|
u32 reserved_til;
|
2020-07-15 04:59:26 +09:30
|
|
|
|
2019-02-22 15:47:30 +01:00
|
|
|
/* The scriptPubkey if it is known */
|
|
|
|
u8 *scriptPubkey;
|
2022-10-17 14:59:57 -05:00
|
|
|
|
|
|
|
/* Is this utxo a coinbase output */
|
|
|
|
bool is_in_coinbase;
|
2017-02-21 15:15:29 +10:30
|
|
|
};
|
|
|
|
|
2020-08-28 13:26:34 +09:30
|
|
|
/* 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 14:56:35 -05:00
|
|
|
static inline bool utxo_is_csv_locked(const struct utxo *utxo, u32 current_height)
|
|
|
|
{
|
|
|
|
if (!utxo->close_info)
|
|
|
|
return false;
|
2023-06-26 08:48:21 +09:30
|
|
|
/* 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 14:56:35 -05:00
|
|
|
return true;
|
|
|
|
assert(*utxo->blockheight + utxo->close_info->csv > *utxo->blockheight);
|
|
|
|
return *utxo->blockheight + utxo->close_info->csv > current_height;
|
|
|
|
}
|
|
|
|
|
2017-02-21 15:15:29 +10:30
|
|
|
void towire_utxo(u8 **pptr, const struct utxo *utxo);
|
2018-02-08 11:53:46 +10:30
|
|
|
struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max);
|
2018-12-03 09:32:11 +10:30
|
|
|
|
2020-07-06 14:56:14 +09:30
|
|
|
/* Estimate of (signed) UTXO weight in transaction */
|
2020-11-17 19:45:23 -06: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-29 01:36:01 +09:30
|
|
|
#endif /* LIGHTNING_COMMON_UTXO_H */
|