2017-07-20 20:49:45 +02:00
|
|
|
#ifndef LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H
|
|
|
|
#define LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H
|
|
|
|
#include "config.h"
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <ccan/compiler/compiler.h>
|
2017-07-20 20:49:45 +02:00
|
|
|
#include <ccan/short_types/short_types.h>
|
2018-07-04 07:30:02 +02:00
|
|
|
#include <ccan/structeq/structeq.h>
|
2017-07-20 20:49:45 +02:00
|
|
|
#include <ccan/tal/tal.h>
|
2019-09-22 04:08:43 +02:00
|
|
|
#include <common/gossip_constants.h>
|
2017-07-20 20:49:45 +02:00
|
|
|
|
|
|
|
/* Short Channel ID is composed of 3 bytes for the block height, 3
|
2018-07-04 07:30:02 +02:00
|
|
|
* bytes of tx index in block and 2 bytes of output index. */
|
2017-07-20 20:49:45 +02:00
|
|
|
struct short_channel_id {
|
2018-03-01 10:22:24 +01:00
|
|
|
u64 u64;
|
2017-07-20 20:49:45 +02:00
|
|
|
};
|
2018-07-04 07:30:02 +02:00
|
|
|
/* Define short_channel_id_eq (no padding) */
|
|
|
|
STRUCTEQ_DEF(short_channel_id, 0, u64);
|
2017-07-20 20:49:45 +02:00
|
|
|
|
2019-01-15 05:11:27 +01:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - MUST set `node_id_1` and `node_id_2` to the public keys of the two nodes
|
2020-03-31 00:38:58 +02:00
|
|
|
* operating the channel, such that `node_id_1` is the lexicographically-lesser of the
|
|
|
|
* two compressed keys sorted in ascending lexicographic order.
|
2019-01-15 05:11:27 +01:00
|
|
|
*...
|
|
|
|
* - if the origin node is `node_id_1` in the message:
|
|
|
|
* - MUST set the `direction` bit of `channel_flags` to 0.
|
|
|
|
* - otherwise:
|
|
|
|
* - MUST set the `direction` bit of `channel_flags` to 1.
|
|
|
|
*/
|
|
|
|
struct short_channel_id_dir {
|
|
|
|
struct short_channel_id scid;
|
|
|
|
/* 0 == from lesser id node, 1 == to lesser id node */
|
|
|
|
int dir;
|
|
|
|
};
|
|
|
|
|
2018-03-01 10:22:24 +01:00
|
|
|
static inline u32 short_channel_id_blocknum(const struct short_channel_id *scid)
|
|
|
|
{
|
|
|
|
return scid->u64 >> 40;
|
|
|
|
}
|
|
|
|
|
2022-06-24 03:14:11 +02:00
|
|
|
static inline bool is_stub_scid(const struct short_channel_id *scid)
|
|
|
|
{
|
|
|
|
return scid ? scid->u64 >> 40 == 1 &&
|
|
|
|
((scid->u64 >> 16) & 0x00FFFFFF) == 1 &&
|
|
|
|
(scid->u64 & 0xFFFF) == 1 : false;
|
|
|
|
}
|
|
|
|
|
2018-03-01 10:22:24 +01:00
|
|
|
static inline u32 short_channel_id_txnum(const struct short_channel_id *scid)
|
|
|
|
{
|
|
|
|
return (scid->u64 >> 16) & 0x00FFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u16 short_channel_id_outnum(const struct short_channel_id *scid)
|
|
|
|
{
|
|
|
|
return scid->u64 & 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2019-09-22 04:08:43 +02:00
|
|
|
/* Subtly, at block N, depth is 1, hence the -1 here. eg. 103x1x0 is announceable
|
|
|
|
* when height is 108. */
|
|
|
|
static inline bool
|
|
|
|
is_scid_depth_announceable(const struct short_channel_id *scid,
|
|
|
|
unsigned int height)
|
|
|
|
{
|
|
|
|
return short_channel_id_blocknum(scid) + ANNOUNCE_MIN_DEPTH - 1
|
|
|
|
<= height;
|
|
|
|
}
|
|
|
|
|
2019-01-21 01:57:43 +01:00
|
|
|
/* Returns false if blocknum, txnum or outnum require too many bits */
|
|
|
|
bool WARN_UNUSED_RESULT mk_short_channel_id(struct short_channel_id *scid,
|
|
|
|
u64 blocknum, u64 txnum, u64 outnum);
|
2018-03-01 10:22:24 +01:00
|
|
|
|
2019-01-20 06:43:35 +01:00
|
|
|
bool WARN_UNUSED_RESULT short_channel_id_from_str(const char *str, size_t strlen,
|
2019-09-06 08:41:41 +02:00
|
|
|
struct short_channel_id *dst);
|
2017-07-20 20:49:45 +02:00
|
|
|
|
2018-03-01 10:22:24 +01:00
|
|
|
char *short_channel_id_to_str(const tal_t *ctx, const struct short_channel_id *scid);
|
2018-01-30 20:11:27 +01:00
|
|
|
|
2019-01-20 06:43:35 +01:00
|
|
|
bool WARN_UNUSED_RESULT short_channel_id_dir_from_str(const char *str, size_t strlen,
|
2019-09-06 08:41:41 +02:00
|
|
|
struct short_channel_id_dir *scidd);
|
2019-01-15 05:11:27 +01:00
|
|
|
|
2020-05-15 12:32:43 +02:00
|
|
|
/* Marshal/unmarshal */
|
|
|
|
void towire_short_channel_id(u8 **pptr,
|
|
|
|
const struct short_channel_id *short_channel_id);
|
|
|
|
void fromwire_short_channel_id(const u8 **cursor, size_t *max,
|
|
|
|
struct short_channel_id *short_channel_id);
|
|
|
|
|
2017-07-20 20:49:45 +02:00
|
|
|
#endif /* LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H */
|