Skip to content

Commit

Permalink
tools/generate-wire: no more lonely messages!
Browse files Browse the repository at this point in the history
When we have only a single member in a TLV (e.g. an optional u64),
wrapping it in a struct is awkward.  This changes it to directly
access those fields.

This is not only more elegant (60 fewer lines), it would also be
more cache friendly.  That's right: cache hot singles!

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and niftynei committed May 6, 2020
1 parent 8d4abc1 commit b0c9059
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 175 deletions.
52 changes: 17 additions & 35 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,7 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg)
"Bad peer_add_htlc %s", tal_hex(msg, msg));

#if EXPERIMENTAL_FEATURES
if (tlvs->blinding)
blinding = &tlvs->blinding->blinding;
blinding = tlvs->blinding;
#endif
add_err = channel_add_htlc(peer->channel, REMOTE, id, amount,
cltv_expiry, &payment_hash,
Expand Down Expand Up @@ -1661,8 +1660,6 @@ static void handle_onion_message(struct peer *peer, const u8 *msg)
const u8 *cursor;
size_t max, maxlen;
struct tlv_onionmsg_payload *om;
const struct short_channel_id *next_scid;
struct node_id *next_node;
struct tlv_onion_message_tlvs *tlvs = tlv_onion_message_tlvs_new(msg);

if (!fromwire_onion_message(msg, onion, tlvs))
Expand All @@ -1682,8 +1679,7 @@ static void handle_onion_message(struct peer *peer, const u8 *msg)
struct secret hmac;

/* E(i) */
blinding_in = tal(msg, struct pubkey);
*blinding_in = tlvs->blinding->blinding;
blinding_in = tal_dup(msg, struct pubkey, tlvs->blinding);
status_debug("blinding in = %s",
type_to_string(tmpctx, struct pubkey, blinding_in));
blinding_ss = tal(msg, struct secret);
Expand Down Expand Up @@ -1742,8 +1738,7 @@ static void handle_onion_message(struct peer *peer, const u8 *msg)
/* If we weren't given a blinding factor, tlv can provide one. */
if (om->blinding && !blinding_ss) {
/* E(i) */
blinding_in = tal(msg, struct pubkey);
*blinding_in = om->blinding->blinding;
blinding_in = tal_dup(msg, struct pubkey, om->blinding);
blinding_ss = tal(msg, struct secret);

ecdh(blinding_in, blinding_ss);
Expand All @@ -1764,19 +1759,19 @@ static void handle_onion_message(struct peer *peer, const u8 *msg)
subkey_from_hmac("rho", blinding_ss, &rho);

/* Overrides next_scid / next_node */
if (tal_bytelen(om->enctlv->enctlv)
if (tal_bytelen(om->enctlv)
< crypto_aead_chacha20poly1305_ietf_ABYTES) {
status_debug("enctlv too short for mac");
return;
}

dec = tal_arr(msg, u8,
tal_bytelen(om->enctlv->enctlv)
tal_bytelen(om->enctlv)
- crypto_aead_chacha20poly1305_ietf_ABYTES);
ret = crypto_aead_chacha20poly1305_ietf_decrypt(dec, NULL,
NULL,
om->enctlv->enctlv,
tal_bytelen(om->enctlv->enctlv),
om->enctlv,
tal_bytelen(om->enctlv),
NULL, 0,
npub,
rho.data);
Expand All @@ -1801,17 +1796,6 @@ static void handle_onion_message(struct peer *peer, const u8 *msg)
return;
}

if (om->next_short_channel_id)
next_scid = &om->next_short_channel_id->short_channel_id;
else
next_scid = NULL;

if (om->next_node_id) {
next_node = tal(msg, struct node_id);
node_id_from_pubkey(next_node, &om->next_node_id->node_id);
} else
next_node = NULL;

if (om->enctlv) {
status_broken("FIXME: Handle enctlv!");
return;
Expand All @@ -1835,14 +1819,16 @@ static void handle_onion_message(struct peer *peer, const u8 *msg)
path)));
} else {
struct pubkey *next_blinding;
struct node_id next_node;

/* This *MUST* have instructions on where to go next. */
if (!next_scid && !next_node) {
if (!om->next_short_channel_id && !om->next_node_id) {
status_debug("onion msg: no next field in %s",
tal_hex(tmpctx, rs->raw_payload));
return;
}

node_id_from_pubkey(&next_node, om->next_node_id);
if (blinding_ss) {
/* E(i-1) = H(E(i) || ss(i)) * E(i) */
struct sha256 h;
Expand All @@ -1854,8 +1840,8 @@ static void handle_onion_message(struct peer *peer, const u8 *msg)

wire_sync_write(MASTER_FD,
take(towire_got_onionmsg_forward(NULL,
next_scid,
next_node,
om->next_short_channel_id,
&next_node,
next_blinding,
serialize_onionpacket(tmpctx, rs->next))));
}
Expand All @@ -1871,11 +1857,8 @@ static void send_onionmsg(struct peer *peer, const u8 *msg)
if (!fromwire_send_onionmsg(msg, msg, onion_routing_packet, &blinding))
master_badmsg(WIRE_SEND_ONIONMSG, msg);

if (blinding) {
tlvs->blinding = tal(tlvs,
struct tlv_onion_message_tlvs_blinding);
tlvs->blinding->blinding = *blinding;
}
if (blinding)
tlvs->blinding = tal_dup(tlvs, struct pubkey, blinding);
sync_crypto_write(peer->pps,
take(towire_onion_message(NULL,
onion_routing_packet,
Expand Down Expand Up @@ -2087,8 +2070,8 @@ static void resend_commitment(struct peer *peer, const struct changed_htlc *last
struct tlv_update_add_tlvs *tlvs;
if (h->blinding) {
tlvs = tlv_update_add_tlvs_new(tmpctx);
tlvs->blinding = tal(tlvs, struct tlv_update_add_tlvs_blinding);
tlvs->blinding->blinding = *h->blinding;
tlvs->blinding = tal_dup(tlvs, struct pubkey,
h->blinding);
} else
tlvs = NULL;
#endif
Expand Down Expand Up @@ -2724,8 +2707,7 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
struct tlv_update_add_tlvs *tlvs;
if (blinding) {
tlvs = tlv_update_add_tlvs_new(tmpctx);
tlvs->blinding = tal(tlvs, struct tlv_update_add_tlvs_blinding);
tlvs->blinding->blinding = *blinding;
tlvs->blinding = tal_dup(tlvs, struct pubkey, blinding);
} else
tlvs = NULL;
#endif
Expand Down
61 changes: 17 additions & 44 deletions common/onion.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ u8 *onion_nonfinal_hop(const tal_t *ctx,
{
if (use_tlv) {
struct tlv_tlv_payload *tlv = tlv_tlv_payload_new(tmpctx);
struct tlv_tlv_payload_amt_to_forward tlv_amt;
struct tlv_tlv_payload_outgoing_cltv_value tlv_cltv;
struct tlv_tlv_payload_short_channel_id tlv_scid;

/* BOLT #4:
*
Expand All @@ -81,23 +78,13 @@ u8 *onion_nonfinal_hop(const tal_t *ctx,
* - MUST include `short_channel_id`
* - MUST NOT include `payment_data`
*/
tlv_amt.amt_to_forward = forward.millisatoshis; /* Raw: TLV convert */
tlv_cltv.outgoing_cltv_value = outgoing_cltv;
tlv_scid.short_channel_id = *scid;
tlv->amt_to_forward = &tlv_amt;
tlv->outgoing_cltv_value = &tlv_cltv;
tlv->short_channel_id = &tlv_scid;
tlv->amt_to_forward = &forward.millisatoshis; /* Raw: TLV convert */
tlv->outgoing_cltv_value = &outgoing_cltv;
tlv->short_channel_id = cast_const(struct short_channel_id *,
scid);
#if EXPERIMENTAL_FEATURES
struct tlv_tlv_payload_blinding_seed tlv_blinding;
struct tlv_tlv_payload_enctlv tlv_enctlv;
if (blinding) {
tlv_blinding.blinding_seed = *blinding;
tlv->blinding_seed = &tlv_blinding;
}
if (enctlv) {
tlv_enctlv.enctlv = cast_const(u8 *, enctlv);
tlv->enctlv = &tlv_enctlv;
}
tlv->blinding_seed = cast_const(struct pubkey *, blinding);
tlv->enctlv = cast_const(u8 *, enctlv);
#endif
return make_tlv_hop(ctx, tlv);
} else {
Expand All @@ -124,8 +111,6 @@ u8 *onion_final_hop(const tal_t *ctx,

if (use_tlv) {
struct tlv_tlv_payload *tlv = tlv_tlv_payload_new(tmpctx);
struct tlv_tlv_payload_amt_to_forward tlv_amt;
struct tlv_tlv_payload_outgoing_cltv_value tlv_cltv;
struct tlv_tlv_payload_payment_data tlv_pdata;

/* BOLT #4:
Expand All @@ -142,27 +127,17 @@ u8 *onion_final_hop(const tal_t *ctx,
* - MUST set `payment_secret` to the one provided
* - MUST set `total_msat` to the total amount it will send
*/
tlv_amt.amt_to_forward = forward.millisatoshis; /* Raw: TLV convert */
tlv_cltv.outgoing_cltv_value = outgoing_cltv;
tlv->amt_to_forward = &tlv_amt;
tlv->outgoing_cltv_value = &tlv_cltv;
tlv->amt_to_forward = &forward.millisatoshis; /* Raw: TLV convert */
tlv->outgoing_cltv_value = &outgoing_cltv;

if (payment_secret) {
tlv_pdata.payment_secret = *payment_secret;
tlv_pdata.total_msat = total_msat.millisatoshis; /* Raw: TLV convert */
tlv->payment_data = &tlv_pdata;
}
#if EXPERIMENTAL_FEATURES
struct tlv_tlv_payload_blinding_seed tlv_blinding;
struct tlv_tlv_payload_enctlv tlv_enctlv;
if (blinding) {
tlv_blinding.blinding_seed = *blinding;
tlv->blinding_seed = &tlv_blinding;
}
if (enctlv) {
tlv_enctlv.enctlv = cast_const(u8 *, enctlv);
tlv->enctlv = &tlv_enctlv;
}
tlv->blinding_seed = cast_const(struct pubkey *, blinding);
tlv->enctlv = cast_const(u8 *, enctlv);
#endif
return make_tlv_hop(ctx, tlv);
} else {
Expand Down Expand Up @@ -351,9 +326,8 @@ struct onion_payload *onion_decode(const tal_t *ctx,
if (!tlv->amt_to_forward || !tlv->outgoing_cltv_value)
goto fail;

amount_msat_from_u64(&p->amt_to_forward,
tlv->amt_to_forward->amt_to_forward);
p->outgoing_cltv = tlv->outgoing_cltv_value->outgoing_cltv_value;
amount_msat_from_u64(&p->amt_to_forward, *tlv->amt_to_forward);
p->outgoing_cltv = *tlv->outgoing_cltv_value;

/* BOLT #4:
*
Expand All @@ -365,9 +339,8 @@ struct onion_payload *onion_decode(const tal_t *ctx,
if (rs->nextcase == ONION_FORWARD) {
if (!tlv->short_channel_id)
goto fail;
p->forward_channel = tal(p, struct short_channel_id);
*p->forward_channel
= tlv->short_channel_id->short_channel_id;
p->forward_channel = tal_dup(p, struct short_channel_id,
tlv->short_channel_id);
p->total_msat = NULL;
} else {
p->forward_channel = NULL;
Expand All @@ -391,7 +364,7 @@ struct onion_payload *onion_decode(const tal_t *ctx,
if (tlv->blinding_seed) {
p->blinding =
tal_dup(p, struct pubkey,
&tlv->blinding_seed->blinding_seed);
tlv->blinding_seed);
ecdh(p->blinding, &p->blinding_ss);
}
} else
Expand All @@ -408,7 +381,7 @@ struct onion_payload *onion_decode(const tal_t *ctx,

ntlv = decrypt_tlv(tmpctx,
&p->blinding_ss,
tlv->enctlv->enctlv);
tlv->enctlv);
if (!ntlv)
goto fail;

Expand All @@ -417,7 +390,7 @@ struct onion_payload *onion_decode(const tal_t *ctx,
goto fail;

*p->forward_channel
= ntlv->short_channel_id->short_channel_id;
= *ntlv->short_channel_id;
}
}
#endif /* EXPERIMENTAL_FEATURES */
Expand Down
7 changes: 3 additions & 4 deletions connectd/peer_exchange_initmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static struct io_plan *peer_init_received(struct io_conn *conn,
* - MAY fail the connection.
*/
if (tlvs->networks) {
if (!contains_common_chain(tlvs->networks->chains)) {
if (!contains_common_chain(tlvs->networks)) {
status_peer_debug(&peer->id,
"No common chain with this peer '%s', closing",
tal_hex(tmpctx, msg));
Expand Down Expand Up @@ -160,9 +160,8 @@ struct io_plan *peer_exchange_initmsg(struct io_conn *conn,
* channels for.
*/
tlvs = tlv_init_tlvs_new(tmpctx);
tlvs->networks = tal(tlvs, struct tlv_init_tlvs_networks);
tlvs->networks->chains = tal_arr(tlvs->networks, struct bitcoin_blkid, 1);
tlvs->networks->chains[0] = chainparams->genesis_blockhash;
tlvs->networks = tal_dup_arr(tlvs, struct bitcoin_blkid,
&chainparams->genesis_blockhash, 1, 0);

/* Initially, there were two sets of feature bits: global and local.
* Local affected peer nodes only, global affected everyone. Both were
Expand Down
24 changes: 11 additions & 13 deletions devtools/blindedpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,19 @@ int main(int argc, char **argv)
/* Use scid if they provided one */
if (scids[i]) {
inner->next_short_channel_id
= tal(inner, struct tlv_onionmsg_payload_next_short_channel_id);
inner->next_short_channel_id->short_channel_id
= *scids[i];
= tal_dup(inner, struct short_channel_id,
scids[i]);
} else {
inner->next_node_id = tal(inner, struct tlv_onionmsg_payload_next_node_id);
inner->next_node_id->node_id = nodes[i+1];
inner->next_node_id
= tal_dup(inner, struct pubkey, &nodes[i+1]);
}
p = tal_arr(tmpctx, u8, 0);
towire_encmsg_tlvs(&p, inner);

outer = tlv_onionmsg_payload_new(tmpctx);
outer->enctlv = tal(outer, struct tlv_onionmsg_payload_enctlv);
outer->enctlv->enctlv = tal_arr(tmpctx, u8, tal_count(p)
outer->enctlv = tal_arr(outer, u8, tal_count(p)
+ crypto_aead_chacha20poly1305_ietf_ABYTES);
ret = crypto_aead_chacha20poly1305_ietf_encrypt(outer->enctlv->enctlv, NULL,
ret = crypto_aead_chacha20poly1305_ietf_encrypt(outer->enctlv, NULL,
p,
tal_bytelen(p),
NULL, 0,
Expand All @@ -188,7 +186,7 @@ int main(int argc, char **argv)
printf("%s\n%s\n",
type_to_string(tmpctx, struct pubkey,
&b[i]),
tal_hex(tmpctx, outer->enctlv->enctlv));
tal_hex(tmpctx, outer->enctlv));
} else {
/* devtools/onion wants length explicitly prepended */
printf("%s/%.*s%s ",
Expand Down Expand Up @@ -290,17 +288,17 @@ int main(int argc, char **argv)
if (!outer->enctlv)
errx(1, "No enctlv field");

if (tal_bytelen(outer->enctlv->enctlv)
if (tal_bytelen(outer->enctlv)
< crypto_aead_chacha20poly1305_ietf_ABYTES)
errx(1, "enctlv field too short");

dec = tal_arr(tmpctx, u8,
tal_bytelen(outer->enctlv->enctlv)
tal_bytelen(outer->enctlv)
- crypto_aead_chacha20poly1305_ietf_ABYTES);
ret = crypto_aead_chacha20poly1305_ietf_decrypt(dec, NULL,
NULL,
outer->enctlv->enctlv,
tal_bytelen(outer->enctlv->enctlv),
outer->enctlv,
tal_bytelen(outer->enctlv),
NULL, 0,
npub,
rho.data);
Expand Down
5 changes: 2 additions & 3 deletions devtools/gossipwith.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@ static struct io_plan *handshake_success(struct io_conn *conn,
struct tlv_init_tlvs *tlvs = NULL;
if (chainparams) {
tlvs = tlv_init_tlvs_new(NULL);
tlvs->networks = tal(tlvs, struct tlv_init_tlvs_networks);
tlvs->networks->chains = tal_arr(tlvs->networks, struct bitcoin_blkid, 1);
tlvs->networks->chains[0] = chainparams->genesis_blockhash;
tlvs->networks = tal_arr(tlvs, struct bitcoin_blkid, 1);
tlvs->networks[0] = chainparams->genesis_blockhash;
}
msg = towire_init(NULL, NULL, features, tlvs);

Expand Down
5 changes: 2 additions & 3 deletions devtools/mkquery.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ int main(int argc, char *argv[])
tlvs = NULL;
else if (argc == 6) {
tlvs = tlv_query_channel_range_tlvs_new(ctx);
tlvs->query_option = tal(tlvs, struct tlv_query_channel_range_tlvs_query_option);
tlvs->query_option->query_option_flags
= strtol(argv[5], NULL, 0);
tlvs->query_option = tal(tlvs, varint);
*tlvs->query_option = strtol(argv[5], NULL, 0);
} else
usage();
msg = towire_query_channel_range(ctx, &chainhash,
Expand Down
Loading

0 comments on commit b0c9059

Please sign in to comment.