short_channel_id: don't use bitfields.

I leave all the now-unnecessary accessors in place to avoid churn, but
the use of bitfields has been more pain than help.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-03-01 19:52:24 +10:30
committed by Christian Decker
parent 6f14736803
commit 042d5d13f5
7 changed files with 52 additions and 45 deletions

View File

@@ -3,6 +3,14 @@
#include <stdio.h>
#include <string.h>
void mk_short_channel_id(struct short_channel_id *scid,
u32 blocknum, u32 txnum, u16 outnum)
{
scid->u64 = (((u64)blocknum & 0xFFFFFF) << 40 |
((u64)txnum & 0xFFFFFF) << 16 |
(outnum & 0xFFFF));
}
bool short_channel_id_from_str(const char *str, size_t strlen,
struct short_channel_id *dst)
{
@@ -15,26 +23,14 @@ bool short_channel_id_from_str(const char *str, size_t strlen,
buf[strlen] = 0;
matches = sscanf(buf, "%u:%u:%hu", &blocknum, &txnum, &outnum);
dst->blocknum = blocknum;
dst->txnum = txnum;
dst->outnum = outnum;
mk_short_channel_id(dst, blocknum, txnum, outnum);
return matches == 3;
}
char *short_channel_id_to_str(const tal_t *ctx, const struct short_channel_id *scid)
{
return tal_fmt(ctx, "%d:%d:%d", scid->blocknum, scid->txnum, scid->outnum);
}
bool short_channel_id_eq(const struct short_channel_id *a,
const struct short_channel_id *b)
{
return a->blocknum == b->blocknum && a->txnum == b->txnum &&
a->outnum == b->outnum;
}
u64 short_channel_id_to_uint(const struct short_channel_id *scid)
{
return ((u64)scid->blocknum & 0xFFFFFF) << 40 |
((u64)scid->txnum & 0xFFFFFF) << 16 | (scid->outnum & 0xFFFF);
return tal_fmt(ctx, "%d:%d:%d",
short_channel_id_blocknum(scid),
short_channel_id_txnum(scid),
short_channel_id_outnum(scid));
}

View File

@@ -12,20 +12,42 @@
* just memset them and not have to take care about the extra byte for
* u32 */
struct short_channel_id {
u32 blocknum : 24;
u32 txnum : 24;
u16 outnum;
u64 u64;
};
static inline u32 short_channel_id_blocknum(const struct short_channel_id *scid)
{
return scid->u64 >> 40;
}
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;
}
void mk_short_channel_id(struct short_channel_id *scid,
u32 blocknum, u32 txnum, u16 outnum);
bool short_channel_id_from_str(const char *str, size_t strlen,
struct short_channel_id *dst);
bool short_channel_id_eq(const struct short_channel_id *a,
const struct short_channel_id *b);
static inline bool short_channel_id_eq(const struct short_channel_id *a,
const struct short_channel_id *b)
{
return a->u64 == b->u64;
}
/* Fast, platform dependent, way to convert from a short_channel_id to u64 */
static inline u64 short_channel_id_to_uint(const struct short_channel_id *scid)
{
return scid->u64;
}
char *short_channel_id_to_str(const tal_t *ctx, const struct short_channel_id *scid);
/* Fast, platform dependent, way to convert from a short_channel_id to u64 */
u64 short_channel_id_to_uint(const struct short_channel_id *scid);
#endif /* LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H */