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

View file

@ -12,20 +12,42 @@
* just memset them and not have to take care about the extra byte for * just memset them and not have to take care about the extra byte for
* u32 */ * u32 */
struct short_channel_id { struct short_channel_id {
u32 blocknum : 24; u64 u64;
u32 txnum : 24;
u16 outnum;
}; };
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, bool short_channel_id_from_str(const char *str, size_t strlen,
struct short_channel_id *dst); struct short_channel_id *dst);
bool short_channel_id_eq(const struct short_channel_id *a, static inline bool short_channel_id_eq(const struct short_channel_id *a,
const struct short_channel_id *b); 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); 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 */ #endif /* LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H */

View file

@ -89,7 +89,9 @@ static void get_txout(struct subd *gossip, const u8 *msg)
/* FIXME: Block less than 6 deep? */ /* FIXME: Block less than 6 deep? */
bitcoind_getoutput(gossip->ld->topology->bitcoind, bitcoind_getoutput(gossip->ld->topology->bitcoind,
scid->blocknum, scid->txnum, scid->outnum, short_channel_id_blocknum(scid),
short_channel_id_txnum(scid),
short_channel_id_outnum(scid),
got_txout, scid); got_txout, scid);
} }

View file

@ -256,7 +256,7 @@ remote_routing_failure(const tal_t *ctx,
const struct pubkey *erring_node; const struct pubkey *erring_node;
const struct short_channel_id *route_channels; const struct short_channel_id *route_channels;
const struct short_channel_id *erring_channel; const struct short_channel_id *erring_channel;
static const struct short_channel_id dummy_channel = { 0, 0, 0 }; static const struct short_channel_id dummy_channel = { 0 };
int origin_index; int origin_index;
bool retry_plausible; bool retry_plausible;
bool report_to_gossipd; bool report_to_gossipd;

View file

@ -521,9 +521,9 @@ static enum watch_result funding_lockin_cb(struct channel *channel,
/* If we restart, we could already have peer->scid from database */ /* If we restart, we could already have peer->scid from database */
if (!channel->scid) { if (!channel->scid) {
channel->scid = tal(channel, struct short_channel_id); channel->scid = tal(channel, struct short_channel_id);
channel->scid->blocknum = loc->blkheight; mk_short_channel_id(channel->scid,
channel->scid->txnum = loc->index; loc->blkheight, loc->index,
channel->scid->outnum = channel->funding_outnum; channel->funding_outnum);
} }
tal_free(loc); tal_free(loc);

View file

@ -158,15 +158,7 @@ void fromwire_channel_id(const u8 **cursor, size_t *max,
void fromwire_short_channel_id(const u8 **cursor, size_t *max, void fromwire_short_channel_id(const u8 **cursor, size_t *max,
struct short_channel_id *short_channel_id) struct short_channel_id *short_channel_id)
{ {
be32 txnum = 0, blocknum = 0; short_channel_id->u64 = fromwire_u64(cursor, max);
/* Pulling 3 bytes off wire is tricky; they're big-endian. */
fromwire(cursor, max, (char *)&blocknum + 1, 3);
short_channel_id->blocknum = be32_to_cpu(blocknum);
fromwire(cursor, max, (char *)&txnum + 1, 3);
short_channel_id->txnum = be32_to_cpu(txnum);
short_channel_id->outnum = fromwire_u16 (cursor, max);
} }
void fromwire_sha256(const u8 **cursor, size_t *max, struct sha256 *sha256) void fromwire_sha256(const u8 **cursor, size_t *max, struct sha256 *sha256)

View file

@ -105,12 +105,7 @@ void towire_channel_id(u8 **pptr, const struct channel_id *channel_id)
void towire_short_channel_id(u8 **pptr, void towire_short_channel_id(u8 **pptr,
const struct short_channel_id *short_channel_id) const struct short_channel_id *short_channel_id)
{ {
be32 txnum = cpu_to_be32(short_channel_id->txnum); towire_u64(pptr, short_channel_id->u64);
be32 blocknum = cpu_to_be32(short_channel_id->blocknum);
towire(pptr, (char *)&blocknum + 1, 3);
towire(pptr, (char *)&txnum + 1, 3);
towire_u16(pptr, short_channel_id->outnum);
} }
void towire_sha256(u8 **pptr, const struct sha256 *sha256) void towire_sha256(u8 **pptr, const struct sha256 *sha256)