Skip to content

Commit

Permalink
short_channel_id: don't use bitfields.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Mar 1, 2018
1 parent 6f14736 commit 042d5d1
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 45 deletions.
30 changes: 13 additions & 17 deletions bitcoin/short_channel_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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));
}
38 changes: 30 additions & 8 deletions bitcoin/short_channel_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

char *short_channel_id_to_str(const tal_t *ctx, const struct short_channel_id *scid);
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 */
u64 short_channel_id_to_uint(const struct short_channel_id *scid);
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);

#endif /* LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H */
4 changes: 3 additions & 1 deletion lightningd/gossip_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ static void get_txout(struct subd *gossip, const u8 *msg)
/* FIXME: Block less than 6 deep? */

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);
}

Expand Down
2 changes: 1 addition & 1 deletion lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ remote_routing_failure(const tal_t *ctx,
const struct pubkey *erring_node;
const struct short_channel_id *route_channels;
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;
bool retry_plausible;
bool report_to_gossipd;
Expand Down
6 changes: 3 additions & 3 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 (!channel->scid) {
channel->scid = tal(channel, struct short_channel_id);
channel->scid->blocknum = loc->blkheight;
channel->scid->txnum = loc->index;
channel->scid->outnum = channel->funding_outnum;
mk_short_channel_id(channel->scid,
loc->blkheight, loc->index,
channel->funding_outnum);
}
tal_free(loc);

Expand Down
10 changes: 1 addition & 9 deletions wire/fromwire.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
struct short_channel_id *short_channel_id)
{
be32 txnum = 0, blocknum = 0;

/* 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);
short_channel_id->u64 = fromwire_u64(cursor, max);
}

void fromwire_sha256(const u8 **cursor, size_t *max, struct sha256 *sha256)
Expand Down
7 changes: 1 addition & 6 deletions wire/towire.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,7 @@ void towire_channel_id(u8 **pptr, const struct channel_id *channel_id)
void towire_short_channel_id(u8 **pptr,
const struct short_channel_id *short_channel_id)
{
be32 txnum = cpu_to_be32(short_channel_id->txnum);
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);
towire_u64(pptr, short_channel_id->u64);
}

void towire_sha256(u8 **pptr, const struct sha256 *sha256)
Expand Down

0 comments on commit 042d5d1

Please sign in to comment.