Skip to content

Commit

Permalink
tools/generate_wire.py: make functions allocate the TLV.
Browse files Browse the repository at this point in the history
Requiring the caller to allocate them is ugly, and differs from
other types.

This means we need a context arg if we don't have one already.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Mar 25, 2022
1 parent 2b42227 commit a770f51
Show file tree
Hide file tree
Showing 32 changed files with 199 additions and 199 deletions.
49 changes: 31 additions & 18 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,17 +664,21 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg)
enum channel_add_err add_err;
struct htlc *htlc;
#if EXPERIMENTAL_FEATURES
struct tlv_update_add_tlvs *tlvs = tlv_update_add_tlvs_new(msg);
struct tlv_update_add_tlvs *tlvs;
#endif
struct pubkey *blinding = NULL;

if (!fromwire_update_add_htlc(msg, &channel_id, &id, &amount,
&payment_hash, &cltv_expiry,
onion_routing_packet
if (!fromwire_update_add_htlc
#if EXPERIMENTAL_FEATURES
, tlvs
(msg, msg, &channel_id, &id, &amount,
&payment_hash, &cltv_expiry,
onion_routing_packet, &tlvs)
#else
(msg, &channel_id, &id, &amount,
&payment_hash, &cltv_expiry,
onion_routing_packet)
#endif
))
)
peer_failed_warn(peer->pps, &peer->channel_id,
"Bad peer_add_htlc %s", tal_hex(msg, msg));

Expand Down Expand Up @@ -1987,14 +1991,14 @@ static void handle_peer_shutdown(struct peer *peer, const u8 *shutdown)
{
struct channel_id channel_id;
u8 *scriptpubkey;
struct tlv_shutdown_tlvs *tlvs = tlv_shutdown_tlvs_new(tmpctx);
struct tlv_shutdown_tlvs *tlvs;
struct bitcoin_outpoint *wrong_funding;

/* Disable the channel. */
send_channel_update(peer, ROUTING_FLAGS_DISABLED);

if (!fromwire_shutdown(tmpctx, shutdown, &channel_id, &scriptpubkey,
tlvs))
&tlvs))
peer_failed_warn(peer->pps, &peer->channel_id,
"Bad shutdown %s", tal_hex(peer, shutdown));

Expand Down Expand Up @@ -2112,18 +2116,26 @@ static void handle_unexpected_reestablish(struct peer *peer, const u8 *msg)
struct secret your_last_per_commitment_secret;
struct pubkey my_current_per_commitment_point;
#if EXPERIMENTAL_FEATURES
struct tlv_channel_reestablish_tlvs *tlvs = tlv_channel_reestablish_tlvs_new(tmpctx);
struct tlv_channel_reestablish_tlvs *tlvs;
#endif

if (!fromwire_channel_reestablish(msg, &channel_id,
&next_commitment_number,
&next_revocation_number,
&your_last_per_commitment_secret,
&my_current_per_commitment_point

if (!fromwire_channel_reestablish
#if EXPERIMENTAL_FEATURES
, tlvs
(tmpctx, msg, &channel_id,
&next_commitment_number,
&next_revocation_number,
&your_last_per_commitment_secret,
&my_current_per_commitment_point,
&tlvs)
#else
(msg, &channel_id,
&next_commitment_number,
&next_revocation_number,
&your_last_per_commitment_secret,
&my_current_per_commitment_point)
#endif
))
)
peer_failed_warn(peer->pps, &peer->channel_id,
"Bad channel_reestablish %s", tal_hex(peer, msg));

Expand Down Expand Up @@ -2838,6 +2850,7 @@ static void peer_reconnect(struct peer *peer,
capture_premature_msg(&premature_msgs, msg));

#if EXPERIMENTAL_FEATURES
/* Initialize here in case we don't read it below! */
recv_tlvs = tlv_channel_reestablish_tlvs_new(tmpctx);

/* FIXME: v0.10.1 would send a different tlv set, due to older spec.
Expand All @@ -2856,13 +2869,13 @@ static void peer_reconnect(struct peer *peer,
"bad reestablish msg: %s %s",
peer_wire_name(fromwire_peektype(msg)),
tal_hex(msg, msg));
} else if (!fromwire_channel_reestablish(msg,
} else if (!fromwire_channel_reestablish(tmpctx, msg,
&channel_id,
&next_commitment_number,
&next_revocation_number,
&last_local_per_commitment_secret,
&remote_current_per_commitment_point,
recv_tlvs)) {
&recv_tlvs)) {
peer_failed_warn(peer->pps,
&peer->channel_id,
"bad reestablish msg: %s %s",
Expand Down
5 changes: 2 additions & 3 deletions closingd/closingd.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,9 @@ receive_offer(struct per_peer_state *pps,
} while (!msg);

their_sig.sighash_type = SIGHASH_ALL;
close_tlvs = tlv_closing_signed_tlvs_new(msg);
if (!fromwire_closing_signed(msg, &their_channel_id,
if (!fromwire_closing_signed(msg, msg, &their_channel_id,
&received_fee, &their_sig.s,
close_tlvs))
&close_tlvs))
peer_failed_warn(pps, channel_id,
"Expected closing_signed: %s",
tal_hex(tmpctx, msg));
Expand Down
4 changes: 2 additions & 2 deletions common/blindedpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ static struct tlv_encrypted_data_tlv *decrypt_encmsg(const tal_t *ctx,
* - if the `enctlv` is not a valid TLV...
* - MUST drop the message.
*/
encmsg = tlv_encrypted_data_tlv_new(ctx);
if (!fromwire_tlv_encrypted_data_tlv(&cursor, &maxlen, encmsg)
encmsg = fromwire_tlv_encrypted_data_tlv(ctx, &cursor, &maxlen);
if (!encmsg
|| !tlv_fields_valid(encmsg->fields, NULL, NULL))
return tal_free(encmsg);

Expand Down
27 changes: 15 additions & 12 deletions common/bolt12.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,18 @@ struct tlv_offer *offer_decode(const tal_t *ctx,
const struct chainparams *must_be_chain,
char **fail)
{
struct tlv_offer *offer = tlv_offer_new(ctx);
struct tlv_offer *offer;
const u8 *data;
size_t dlen;

data = string_to_data(tmpctx, b12, b12len, "lno", &dlen, fail);
if (!data)
return tal_free(offer);
return NULL;;

if (!fromwire_tlv_offer(&data, &dlen, offer)) {
offer = fromwire_tlv_offer(ctx, &data, &dlen);
if (!offer) {
*fail = tal_fmt(ctx, "invalid offer data");
return tal_free(offer);
return NULL;
}

*fail = check_features_and_chain(ctx,
Expand Down Expand Up @@ -219,17 +220,18 @@ struct tlv_invoice_request *invrequest_decode(const tal_t *ctx,
const struct chainparams *must_be_chain,
char **fail)
{
struct tlv_invoice_request *invrequest = tlv_invoice_request_new(ctx);
struct tlv_invoice_request *invrequest;
const u8 *data;
size_t dlen;

data = string_to_data(tmpctx, b12, b12len, "lnr", &dlen, fail);
if (!data)
return tal_free(invrequest);
return NULL;

if (!fromwire_tlv_invoice_request(&data, &dlen, invrequest)) {
invrequest = fromwire_tlv_invoice_request(ctx, &data, &dlen);
if (!invrequest) {
*fail = tal_fmt(ctx, "invalid invoice_request data");
return tal_free(invrequest);
return NULL;
}

*fail = check_features_and_chain(ctx,
Expand Down Expand Up @@ -258,17 +260,18 @@ struct tlv_invoice *invoice_decode_nosig(const tal_t *ctx,
const struct chainparams *must_be_chain,
char **fail)
{
struct tlv_invoice *invoice = tlv_invoice_new(ctx);
struct tlv_invoice *invoice;
const u8 *data;
size_t dlen;

data = string_to_data(tmpctx, b12, b12len, "lni", &dlen, fail);
if (!data)
return tal_free(invoice);
return NULL;

if (!fromwire_tlv_invoice(&data, &dlen, invoice)) {
invoice = fromwire_tlv_invoice(ctx, &data, &dlen);
if (!invoice) {
*fail = tal_fmt(ctx, "invalid invoice data");
return tal_free(invoice);
return NULL;
}

*fail = check_features_and_chain(ctx,
Expand Down
11 changes: 3 additions & 8 deletions common/onion.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ static struct tlv_tlv_payload *decrypt_tlv(const tal_t *ctx,
const u8 *cursor;
size_t max;
int ret;
struct tlv_tlv_payload *tlv;

subkey_from_hmac("rho", blinding_ss, &rho);
if (tal_bytelen(enc) < crypto_aead_chacha20poly1305_ietf_ABYTES)
Expand All @@ -192,13 +191,9 @@ static struct tlv_tlv_payload *decrypt_tlv(const tal_t *ctx,
if (ret != 0)
return NULL;

tlv = tlv_tlv_payload_new(ctx);
cursor = dec;
max = tal_bytelen(dec);
if (!fromwire_tlv_tlv_payload(&cursor, &max, tlv))
return tal_free(tlv);

return tlv;
return fromwire_tlv_tlv_payload(ctx, &cursor, &max);
}
#endif /* EXPERIMENTAL_FEATURES */

Expand All @@ -219,8 +214,8 @@ struct onion_payload *onion_decode(const tal_t *ctx,
if (!pull_payload_length(&cursor, &max, true, &len))
goto general_fail;

tlv = tlv_tlv_payload_new(p);
if (!fromwire_tlv_tlv_payload(&cursor, &max, tlv)) {
tlv = fromwire_tlv_tlv_payload(p, &cursor, &max);
if (!tlv) {
/* FIXME: Fill in correct thing here! */
goto general_fail;
}
Expand Down
3 changes: 1 addition & 2 deletions common/test/run-blindedpath_onion.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ static u8 *next_onion(const tal_t *ctx, u8 *omsg,
cursor = rs->raw_payload;
max = tal_bytelen(rs->raw_payload);
maxlen = fromwire_bigsize(&cursor, &max);
om = tlv_onionmsg_payload_new(tmpctx);
assert(fromwire_tlv_onionmsg_payload(&cursor, &maxlen, om));
om = fromwire_tlv_onionmsg_payload(tmpctx, &cursor, &maxlen);

if (rs->nextcase == ONION_END)
return NULL;
Expand Down
21 changes: 6 additions & 15 deletions common/test/run-bolt12_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED,
const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED)
{ fprintf(stderr, "fromwire_tal_arrn called!\n"); abort(); }
/* Generated stub for fromwire_tlv_invoice */
bool fromwire_tlv_invoice(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct tlv_invoice * record UNNEEDED)
struct tlv_invoice *fromwire_tlv_invoice(const tal_t *ctx UNNEEDED,
const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_tlv_invoice called!\n"); abort(); }
/* Generated stub for fromwire_tlv_invoice_request */
bool fromwire_tlv_invoice_request(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct tlv_invoice_request * record UNNEEDED)
struct tlv_invoice_request *fromwire_tlv_invoice_request(const tal_t *ctx UNNEEDED,
const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_tlv_invoice_request called!\n"); abort(); }
/* Generated stub for fromwire_tlv_offer */
bool fromwire_tlv_offer(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct tlv_offer * record UNNEEDED)
struct tlv_offer *fromwire_tlv_offer(const tal_t *ctx UNNEEDED,
const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_tlv_offer called!\n"); abort(); }
/* Generated stub for fromwire_u32 */
u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
Expand Down Expand Up @@ -110,15 +110,6 @@ void sighash_from_merkle(const char *messagename UNNEEDED,
const struct sha256 *merkle UNNEEDED,
struct sha256 *sighash UNNEEDED)
{ fprintf(stderr, "sighash_from_merkle called!\n"); abort(); }
/* Generated stub for tlv_invoice_new */
struct tlv_invoice *tlv_invoice_new(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "tlv_invoice_new called!\n"); abort(); }
/* Generated stub for tlv_invoice_request_new */
struct tlv_invoice_request *tlv_invoice_request_new(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "tlv_invoice_request_new called!\n"); abort(); }
/* Generated stub for tlv_offer_new */
struct tlv_offer *tlv_offer_new(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "tlv_offer_new called!\n"); abort(); }
/* Generated stub for towire */
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "towire called!\n"); abort(); }
Expand Down
10 changes: 6 additions & 4 deletions common/test/run-bolt12_merkle-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,18 @@ int main(int argc, char *argv[])

/* Now do it via type-specific fromwire. */
if (streq(tlvtype, "n1")) {
struct tlv_n1 *n1 = tlv_n1_new(tmpctx);
struct tlv_n1 *n1;
size_t len = tal_bytelen(tlv);
assert(fromwire_tlv_n1(&tlv, &len, n1));
n1 = fromwire_tlv_n1(tmpctx, &tlv, &len);
assert(n1);
assert(len == 0);
merkle_tlv(n1->fields, &merkle);
assert(sha256_eq(&merkle, &expected_merkle));
} else if (streq(tlvtype, "offer")) {
struct tlv_offer *offer = tlv_offer_new(tmpctx);
struct tlv_offer *offer;
size_t len = tal_bytelen(tlv);
assert(fromwire_tlv_offer(&tlv, &len, offer));
offer = fromwire_tlv_offer(tmpctx, &tlv, &len);
assert(offer);
assert(len == 0);
merkle_tlv(offer->fields, &merkle);
assert(sha256_eq(&merkle, &expected_merkle));
Expand Down
4 changes: 1 addition & 3 deletions common/test/run-bolt12_merkle.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ static void merkle_n1(const struct tlv_n1 *n1, struct sha256 *test_m)
towire_tlv_n1(&v, n1);

len = tal_bytelen(v);
tmp = tlv_n1_new(tmpctx);
if (!fromwire_tlv_n1(cast_const2(const u8 **, &v), &len, tmp))
abort();
tmp = fromwire_tlv_n1(tmpctx, cast_const2(const u8 **, &v), &len);
assert(len == 0);

merkle_tlv(tmp->fields, test_m);
Expand Down
21 changes: 6 additions & 15 deletions common/test/run-bolt12_period.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED,
const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED)
{ fprintf(stderr, "fromwire_tal_arrn called!\n"); abort(); }
/* Generated stub for fromwire_tlv_invoice */
bool fromwire_tlv_invoice(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct tlv_invoice * record UNNEEDED)
struct tlv_invoice *fromwire_tlv_invoice(const tal_t *ctx UNNEEDED,
const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_tlv_invoice called!\n"); abort(); }
/* Generated stub for fromwire_tlv_invoice_request */
bool fromwire_tlv_invoice_request(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct tlv_invoice_request * record UNNEEDED)
struct tlv_invoice_request *fromwire_tlv_invoice_request(const tal_t *ctx UNNEEDED,
const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_tlv_invoice_request called!\n"); abort(); }
/* Generated stub for fromwire_tlv_offer */
bool fromwire_tlv_offer(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct tlv_offer * record UNNEEDED)
struct tlv_offer *fromwire_tlv_offer(const tal_t *ctx UNNEEDED,
const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_tlv_offer called!\n"); abort(); }
/* Generated stub for fromwire_u32 */
u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
Expand Down Expand Up @@ -113,15 +113,6 @@ void sighash_from_merkle(const char *messagename UNNEEDED,
const struct sha256 *merkle UNNEEDED,
struct sha256 *sighash UNNEEDED)
{ fprintf(stderr, "sighash_from_merkle called!\n"); abort(); }
/* Generated stub for tlv_invoice_new */
struct tlv_invoice *tlv_invoice_new(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "tlv_invoice_new called!\n"); abort(); }
/* Generated stub for tlv_invoice_request_new */
struct tlv_invoice_request *tlv_invoice_request_new(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "tlv_invoice_request_new called!\n"); abort(); }
/* Generated stub for tlv_offer_new */
struct tlv_offer *tlv_offer_new(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "tlv_offer_new called!\n"); abort(); }
/* Generated stub for to_bech32_charset */
char *to_bech32_charset(const tal_t *ctx UNNEEDED,
const char *hrp UNNEEDED, const u8 *data UNNEEDED)
Expand Down
5 changes: 2 additions & 3 deletions common/test/run-gossmap_local.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,15 @@ static void check_nannounce(const u8 *nannounce,
u32 timestamp;
u8 rgb_color[3], alias[32];
struct node_id node_id;
struct tlv_node_ann_tlvs *na_tlvs
= tlv_node_ann_tlvs_new(tmpctx);
struct tlv_node_ann_tlvs *na_tlvs;
assert(fromwire_node_announcement(nannounce, nannounce,
&sig, &features,
&timestamp,
&node_id,
rgb_color,
alias,
&addresses,
na_tlvs));
&na_tlvs));
assert(node_id_eq(&node_id, n));
}

Expand Down
4 changes: 2 additions & 2 deletions connectd/onion_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ void handle_onion_message(struct daemon *daemon,
return;
}

om = tlv_onionmsg_payload_new(msg);
if (!fromwire_tlv_onionmsg_payload(&cursor, &maxlen, om)) {
om = fromwire_tlv_onionmsg_payload(msg, &cursor, &maxlen);
if (!om) {
status_peer_debug(&peer->id, "onion msg: invalid onionmsg_payload %s",
tal_hex(tmpctx, rs->raw_payload));
return;
Expand Down
Loading

0 comments on commit a770f51

Please sign in to comment.