Skip to content

Commit

Permalink
bitcoin/short_channel_id: pass by copy everywhere.
Browse files Browse the repository at this point in the history
It's a u64, we should pass by copy.  This is a big sweeping change,
but mainly mechanical (change one, compile, fix breakage, repeat).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Mar 20, 2024
1 parent 98f491a commit 9450d46
Show file tree
Hide file tree
Showing 73 changed files with 355 additions and 319 deletions.
18 changes: 10 additions & 8 deletions bitcoin/short_channel_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ bool short_channel_id_from_str(const char *str, size_t strlen,
char *fmt_short_channel_id(const tal_t *ctx, struct short_channel_id scid)
{
return tal_fmt(ctx, "%dx%dx%d",
short_channel_id_blocknum(&scid),
short_channel_id_txnum(&scid),
short_channel_id_outnum(&scid));
short_channel_id_blocknum(scid),
short_channel_id_txnum(scid),
short_channel_id_outnum(scid));
}

bool short_channel_id_dir_from_str(const char *str, size_t strlen,
Expand Down Expand Up @@ -87,13 +87,15 @@ char *fmt_short_channel_id_dir(const tal_t *ctx,
}

void towire_short_channel_id(u8 **pptr,
const struct short_channel_id *short_channel_id)
struct short_channel_id short_channel_id)
{
towire_u64(pptr, short_channel_id->u64);
towire_u64(pptr, short_channel_id.u64);
}

void fromwire_short_channel_id(const u8 **cursor, size_t *max,
struct short_channel_id *short_channel_id)
struct short_channel_id fromwire_short_channel_id(const u8 **cursor, size_t *max)
{
short_channel_id->u64 = fromwire_u64(cursor, max);
struct short_channel_id scid;

scid.u64 = fromwire_u64(cursor, max);
return scid;
}
35 changes: 19 additions & 16 deletions bitcoin/short_channel_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
struct short_channel_id {
u64 u64;
};
/* Define short_channel_id_eq (no padding) */
STRUCTEQ_DEF(short_channel_id, 0, u64);

static inline bool short_channel_id_eq(struct short_channel_id a,
struct short_channel_id b)
{
return a.u64 == b.u64;
}

/* BOLT #7:
*
Expand All @@ -32,32 +36,32 @@ struct short_channel_id_dir {
int dir;
};

static inline u32 short_channel_id_blocknum(const struct short_channel_id *scid)
static inline u32 short_channel_id_blocknum(struct short_channel_id scid)
{
return scid->u64 >> 40;
return scid.u64 >> 40;
}

static inline bool is_stub_scid(const struct short_channel_id *scid)
static inline bool is_stub_scid(struct short_channel_id scid)
{
return scid ? scid->u64 >> 40 == 1 &&
((scid->u64 >> 16) & 0x00FFFFFF) == 1 &&
(scid->u64 & 0xFFFF) == 1 : false;
return scid.u64 >> 40 == 1 &&
((scid.u64 >> 16) & 0x00FFFFFF) == 1 &&
(scid.u64 & 0xFFFF) == 1;
}

static inline u32 short_channel_id_txnum(const struct short_channel_id *scid)
static inline u32 short_channel_id_txnum(struct short_channel_id scid)
{
return (scid->u64 >> 16) & 0x00FFFFFF;
return (scid.u64 >> 16) & 0x00FFFFFF;
}

static inline u16 short_channel_id_outnum(const struct short_channel_id *scid)
static inline u16 short_channel_id_outnum(struct short_channel_id scid)
{
return scid->u64 & 0xFFFF;
return scid.u64 & 0xFFFF;
}

/* 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,
is_scid_depth_announceable(struct short_channel_id scid,
unsigned int height)
{
return short_channel_id_blocknum(scid) + ANNOUNCE_MIN_DEPTH - 1
Expand All @@ -80,8 +84,7 @@ char *fmt_short_channel_id_dir(const tal_t *ctx,

/* 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);
struct short_channel_id short_channel_id);
struct short_channel_id fromwire_short_channel_id(const u8 **cursor, size_t *max);

#endif /* LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H */
6 changes: 3 additions & 3 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ static void check_mutual_splice_locked(struct peer *peer)
|| !peer->splice_state->locked_ready[REMOTE])
return;

if (short_channel_id_eq(&peer->short_channel_ids[LOCAL],
&peer->splice_state->short_channel_id))
if (short_channel_id_eq(peer->short_channel_ids[LOCAL],
peer->splice_state->short_channel_id))
peer_failed_warn(peer->pps, &peer->channel_id,
"Duplicate splice_locked events detected");

Expand Down Expand Up @@ -599,7 +599,7 @@ static void handle_peer_announcement_signatures(struct peer *peer, const u8 *msg

wire_sync_write(MASTER_FD,
take(towire_channeld_got_announcement(NULL,
&remote_scid,
remote_scid,
&remote_node_sig,
&remote_bitcoin_sig)));
}
Expand Down
4 changes: 2 additions & 2 deletions common/bolt11.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ static bool fromwire_route_info(const u8 **cursor, size_t *max,
struct route_info *route_info)
{
fromwire_node_id(cursor, max, &route_info->pubkey);
fromwire_short_channel_id(cursor, max, &route_info->short_channel_id);
route_info->short_channel_id = fromwire_short_channel_id(cursor, max);
route_info->fee_base_msat = fromwire_u32(cursor, max);
route_info->fee_proportional_millionths = fromwire_u32(cursor, max);
route_info->cltv_expiry_delta = fromwire_u16(cursor, max);
Expand All @@ -460,7 +460,7 @@ static bool fromwire_route_info(const u8 **cursor, size_t *max,
static void towire_route_info(u8 **pptr, const struct route_info *route_info)
{
towire_node_id(pptr, &route_info->pubkey);
towire_short_channel_id(pptr, &route_info->short_channel_id);
towire_short_channel_id(pptr, route_info->short_channel_id);
towire_u32(pptr, route_info->fee_base_msat);
towire_u32(pptr, route_info->fee_proportional_millionths);
towire_u16(pptr, route_info->cltv_expiry_delta);
Expand Down
2 changes: 1 addition & 1 deletion common/bolt11_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void json_add_bolt11(struct json_stream *response,
&b11->routes[i][n].pubkey);
json_add_short_channel_id(response,
"short_channel_id",
&b11->routes[i][n]
b11->routes[i][n]
.short_channel_id);
json_add_u64(response, "fee_base_msat",
b11->routes[i][n].fee_base_msat);
Expand Down
2 changes: 1 addition & 1 deletion common/decode_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct short_channel_id *decode_short_ids(const tal_t *ctx, const u8 *encoded)
scids = tal_arr(ctx, struct short_channel_id, 0);
while (max) {
struct short_channel_id scid;
fromwire_short_channel_id(&encoded, &max, &scid);
scid = fromwire_short_channel_id(&encoded, &max);
tal_arr_expand(&scids, scid);
}

Expand Down
16 changes: 8 additions & 8 deletions common/gossmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static bool chanidx_eq_id(const ptrint_t *pidx,
struct short_channel_id scid)
{
struct short_channel_id pidxid = chanidx_id(pidx);
return short_channel_id_eq(&pidxid, &scid);
return short_channel_id_eq(pidxid, scid);
}
static size_t scid_hash(const struct short_channel_id scid)
{
Expand Down Expand Up @@ -785,18 +785,18 @@ static size_t insert_local_space(struct gossmap_localmods *localmods,
}

static struct localmod *find_localmod(struct gossmap_localmods *localmods,
const struct short_channel_id *scid)
struct short_channel_id scid)
{
for (size_t i = 0; i < tal_count(localmods->mods); i++)
if (short_channel_id_eq(&localmods->mods[i].scid, scid))
if (short_channel_id_eq(localmods->mods[i].scid, scid))
return &localmods->mods[i];
return NULL;
}

bool gossmap_local_addchan(struct gossmap_localmods *localmods,
const struct node_id *n1,
const struct node_id *n2,
const struct short_channel_id *scid,
struct short_channel_id scid,
const u8 *features)
{
be16 be16;
Expand All @@ -818,7 +818,7 @@ bool gossmap_local_addchan(struct gossmap_localmods *localmods,
if (node_id_cmp(n1, n2) > 0)
return gossmap_local_addchan(localmods, n2, n1, scid, features);

mod.scid = *scid;
mod.scid = scid;
mod.updates_set[0] = mod.updates_set[1] = false;

/* We create fake local channel_announcement. */
Expand Down Expand Up @@ -848,7 +848,7 @@ bool gossmap_local_addchan(struct gossmap_localmods *localmods,
off += 32;

/* Set scid */
be64 = be64_to_cpu(scid->u64);
be64 = be64_to_cpu(scid.u64);
memcpy(localmods->local + off, &be64, sizeof(be64));
off += sizeof(be64);

Expand All @@ -866,7 +866,7 @@ bool gossmap_local_addchan(struct gossmap_localmods *localmods,

/* Insert a local-only channel_update. */
bool gossmap_local_updatechan(struct gossmap_localmods *localmods,
const struct short_channel_id *scid,
struct short_channel_id scid,
struct amount_msat htlc_min,
struct amount_msat htlc_max,
u32 base_fee,
Expand All @@ -884,7 +884,7 @@ bool gossmap_local_updatechan(struct gossmap_localmods *localmods,

tal_resize(&localmods->mods, nmods + 1);
mod = &localmods->mods[nmods];
mod->scid = *scid;
mod->scid = scid;
mod->updates_set[0] = mod->updates_set[1] = false;
mod->local_off = 0xFFFFFFFF;
}
Expand Down
6 changes: 3 additions & 3 deletions common/gossmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ struct gossmap_localmods *gossmap_localmods_new(const tal_t *ctx);
bool gossmap_local_addchan(struct gossmap_localmods *localmods,
const struct node_id *n1,
const struct node_id *n2,
const struct short_channel_id *scid,
struct short_channel_id scid,
const u8 *features)
NON_NULL_ARGS(1,2,3,4);
NON_NULL_ARGS(1,2,3);

/* Create a local-only channel_update: can apply to lcoal-only or
* normal channels. Returns false if amounts don't fit in our
* internal representation (implies channel unusable anyway). */
bool gossmap_local_updatechan(struct gossmap_localmods *localmods,
const struct short_channel_id *scid,
struct short_channel_id scid,
struct amount_msat htlc_min,
struct amount_msat htlc_max,
u32 base_fee,
Expand Down
4 changes: 2 additions & 2 deletions common/gossmods_listpeerchannels.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ void gossmod_add_localchan(struct gossmap_localmods *mods,
void *cbarg UNUSED)
{
/* FIXME: features? */
gossmap_local_addchan(mods, self, peer, &scidd->scid, NULL);
gossmap_local_addchan(mods, self, peer, scidd->scid, NULL);

gossmap_local_updatechan(mods, &scidd->scid, min, max,
gossmap_local_updatechan(mods, scidd->scid, min, max,
fee_base.millisatoshis, /* Raw: gossmap */
fee_proportional,
cltv_delta,
Expand Down
2 changes: 1 addition & 1 deletion common/json_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ void json_add_outpoint(struct json_stream *result, const char *fieldname,

void json_add_short_channel_id(struct json_stream *response,
const char *fieldname,
const struct short_channel_id *scid)
struct short_channel_id scid)
{
json_add_str_fmt(response, fieldname, "%dx%dx%d",
short_channel_id_blocknum(scid),
Expand Down
2 changes: 1 addition & 1 deletion common/json_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ void json_add_outpoint(struct json_stream *result, const char *fieldname,
/* '"fieldname" : "1234:5:6"' */
void json_add_short_channel_id(struct json_stream *response,
const char *fieldname,
const struct short_channel_id *id);
struct short_channel_id id);

/* JSON serialize a network address for a node */
void json_add_address(struct json_stream *response, const char *fieldname,
Expand Down
4 changes: 2 additions & 2 deletions common/test/run-bolt11.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ static void test_b11(const char *b11str,
*er = &expect_b11->routes[i][j];
assert(node_id_eq(&er->pubkey, &r->pubkey));
assert(er->cltv_expiry_delta == r->cltv_expiry_delta);
assert(short_channel_id_eq(&er->short_channel_id,
&r->short_channel_id));
assert(short_channel_id_eq(er->short_channel_id,
r->short_channel_id));
assert(er->fee_base_msat == r->fee_base_msat);
assert(er->fee_proportional_millionths
== r->fee_proportional_millionths);
Expand Down
2 changes: 1 addition & 1 deletion common/test/run-bolt12_merkle.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ int main(int argc, char *argv[])
v = tal_arr(tmpctx, u8, 0);
if (!mk_short_channel_id(&scid, 1, 2, 3))
abort();
towire_short_channel_id(&v, &scid);
towire_short_channel_id(&v, scid);
tlv2 = tlv(2, v, tal_bytelen(v));

node_id_from_hexstr("0266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c03518", strlen("0266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c03518"), &nid);
Expand Down
6 changes: 3 additions & 3 deletions common/test/run-gossmap_canned.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ static u8 canned_map[] = {
};

static void check_cannounce(const u8 *cannounce,
const struct short_channel_id *scid,
struct short_channel_id scid,
const struct node_id *n1,
const struct node_id *n2)
{
Expand All @@ -293,7 +293,7 @@ static void check_cannounce(const u8 *cannounce,
&actual_n1,
&actual_n2,
&k, &k));
assert(short_channel_id_eq(&actual_scid, scid));
assert(short_channel_id_eq(actual_scid, scid));
if (node_id_cmp(n1, n2) < 0) {
assert(node_id_eq(&actual_n1, n1));
assert(node_id_eq(&actual_n2, n2));
Expand Down Expand Up @@ -376,6 +376,6 @@ int main(int argc, char *argv[])

cann = gossmap_chan_get_announce(tmpctx, map,
gossmap_find_chan(map, &scid12));
check_cannounce(cann, &scid12, &l1, &l2);
check_cannounce(cann, scid12, &l1, &l2);
common_shutdown();
}
16 changes: 8 additions & 8 deletions common/test/run-gossmap_local.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ static u8 canned_map[] = {
};

static void check_cannounce(const u8 *cannounce,
const struct short_channel_id *scid,
struct short_channel_id scid,
const struct node_id *n1,
const struct node_id *n2)
{
Expand All @@ -293,7 +293,7 @@ static void check_cannounce(const u8 *cannounce,
&actual_n1,
&actual_n2,
&k, &k));
assert(short_channel_id_eq(&actual_scid, scid));
assert(short_channel_id_eq(actual_scid, scid));
if (node_id_cmp(n1, n2) < 0) {
assert(node_id_eq(&actual_n1, n1));
assert(node_id_eq(&actual_n2, n2));
Expand Down Expand Up @@ -443,10 +443,10 @@ int main(int argc, char *argv[])

cann = gossmap_chan_get_announce(tmpctx, map,
gossmap_find_chan(map, &scid12));
check_cannounce(cann, &scid12, &l1, &l2);
check_cannounce(cann, scid12, &l1, &l2);
cann = gossmap_chan_get_announce(tmpctx, map,
gossmap_find_chan(map, &scid23));
check_cannounce(cann, &scid23, &l2, &l3);
check_cannounce(cann, scid23, &l2, &l3);

nann = gossmap_node_get_announce(tmpctx, map,
gossmap_find_node(map, &l1));
Expand All @@ -463,7 +463,7 @@ int main(int argc, char *argv[])
assert(node_id_from_hexstr("0382ce59ebf18be7d84677c2e35f23294b9992ceca95491fcf8a56c6cb2d9de199", 66, &l4));
assert(short_channel_id_from_str("111x1x1", 7, &scid_local));

assert(gossmap_local_addchan(mods, &l1, &l4, &scid_local, NULL));
assert(gossmap_local_addchan(mods, &l1, &l4, scid_local, NULL));

/* Apply changes, check they work. */
gossmap_apply_localmods(map, mods);
Expand All @@ -481,15 +481,15 @@ int main(int argc, char *argv[])
assert(!gossmap_find_node(map, &l4));

/* Now update it both local, and an existing one. */
gossmap_local_updatechan(mods, &scid_local,
gossmap_local_updatechan(mods, scid_local,
AMOUNT_MSAT(1),
AMOUNT_MSAT(100000),
2, 3, 4, true, 0);

/* Adding an existing channel is a noop. */
assert(gossmap_local_addchan(mods, &l2, &l3, &scid23, NULL));
assert(gossmap_local_addchan(mods, &l2, &l3, scid23, NULL));

gossmap_local_updatechan(mods, &scid23,
gossmap_local_updatechan(mods, scid23,
AMOUNT_MSAT(99),
AMOUNT_MSAT(100),
101, 102, 103, true, 0);
Expand Down
2 changes: 1 addition & 1 deletion common/test/run-route-infloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ int main(int argc, char *argv[])
if (!c->half[dir].enabled)
continue;
scid = gossmap_chan_scid(gossmap, c);
assert(gossmap_local_updatechan(localmods, &scid,
assert(gossmap_local_updatechan(localmods, scid,
amount_msat(fp16_to_u64(c->half[dir].htlc_min)),
amount_msat(fp16_to_u64(c->half[dir].htlc_max)),
0, 0, 0, true, dir));
Expand Down
Loading

0 comments on commit 9450d46

Please sign in to comment.