Browse Source

tlv: Allow passing a raw pointer and a length to tlvstream_set_raw

Allows us to do fewer allocations, since the argument doesn't have to be tal
allocated itself.

Suggested-by: Rusty Russell <@rustyrussell>
keysend
Christian Decker 5 years ago
parent
commit
2e2e1d16d2
  1. 6
      plugins/keysend.c
  2. 4
      plugins/libplugin-pay.c
  3. 10
      wire/tlvstream.c
  4. 2
      wire/tlvstream.h

6
plugins/keysend.c

@ -55,7 +55,6 @@ static void keysend_cb(struct keysend_data *d, struct payment *p) {
struct route_hop *last_hop; struct route_hop *last_hop;
struct createonion_hop *last_payload; struct createonion_hop *last_payload;
size_t hopcount; size_t hopcount;
u8 *raw_preimage;
if (p->step == PAYMENT_STEP_GOT_ROUTE) { if (p->step == PAYMENT_STEP_GOT_ROUTE) {
/* Force the last step to be a TLV, we might not have an /* Force the last step to be a TLV, we might not have an
@ -68,13 +67,10 @@ static void keysend_cb(struct keysend_data *d, struct payment *p) {
if (p->step != PAYMENT_STEP_ONION_PAYLOAD) if (p->step != PAYMENT_STEP_ONION_PAYLOAD)
return payment_continue(p); return payment_continue(p);
raw_preimage = tal_dup_arr(p->createonion_request, u8, d->preimage.r,
sizeof(d->preimage), 0);
hopcount = tal_count(p->createonion_request->hops); hopcount = tal_count(p->createonion_request->hops);
last_payload = &p->createonion_request->hops[hopcount - 1]; last_payload = &p->createonion_request->hops[hopcount - 1];
tlvstream_set_raw(&last_payload->tlv_payload->fields, PREIMAGE_TLV_TYPE, tlvstream_set_raw(&last_payload->tlv_payload->fields, PREIMAGE_TLV_TYPE,
take(raw_preimage)); &d->preimage, sizeof(struct preimage));
return payment_continue(p); return payment_continue(p);
} }

4
plugins/libplugin-pay.c

@ -815,8 +815,10 @@ static void tlvstream_set_tlv_payload_data(struct tlv_field **stream,
u8 *ser = tal_arr(NULL, u8, 0); u8 *ser = tal_arr(NULL, u8, 0);
towire_secret(&ser, payment_secret); towire_secret(&ser, payment_secret);
towire_tu64(&ser, total_msat); towire_tu64(&ser, total_msat);
tlvstream_set_raw(stream, TLV_TLV_PAYLOAD_PAYMENT_DATA, take(ser)); tlvstream_set_raw(stream, TLV_TLV_PAYLOAD_PAYMENT_DATA, ser, tal_bytelen(ser));
tal_free(ser);
} }
static void payment_add_hop_onion_payload(struct payment *p, static void payment_add_hop_onion_payload(struct payment *p,
struct createonion_hop *dst, struct createonion_hop *dst,
struct route_hop *node, struct route_hop *node,

10
wire/tlvstream.c

@ -21,10 +21,10 @@ void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields)
} }
} }
void tlvstream_set_raw(struct tlv_field **stream, u64 type, u8 *value TAKES) void tlvstream_set_raw(struct tlv_field **stream, u64 type, void *value, size_t valuelen)
{ {
struct tlv_field f; struct tlv_field f;
f.length = tal_bytelen(value); f.length = valuelen;
f.numtype = type; f.numtype = type;
f.value = tal_dup_arr(*stream, u8, value, f.length, 0); f.value = tal_dup_arr(*stream, u8, value, f.length, 0);
tal_arr_expand(stream, f); tal_arr_expand(stream, f);
@ -35,21 +35,21 @@ void tlvstream_set_short_channel_id(struct tlv_field **stream, u64 type,
{ {
u8 *ser = tal_arr(NULL, u8, 0); u8 *ser = tal_arr(NULL, u8, 0);
towire_short_channel_id(&ser, value); towire_short_channel_id(&ser, value);
tlvstream_set_raw(stream, type, take(ser)); tlvstream_set_raw(stream, type, ser, tal_bytelen(ser));
} }
void tlvstream_set_tu64(struct tlv_field **stream, u64 type, u64 value) void tlvstream_set_tu64(struct tlv_field **stream, u64 type, u64 value)
{ {
u8 *ser = tal_arr(NULL, u8, 0); u8 *ser = tal_arr(NULL, u8, 0);
towire_tu64(&ser, value); towire_tu64(&ser, value);
tlvstream_set_raw(stream, type, take(ser)); tlvstream_set_raw(stream, type, ser, tal_bytelen(ser));
} }
void tlvstream_set_tu32(struct tlv_field **stream, u64 type, u32 value) void tlvstream_set_tu32(struct tlv_field **stream, u64 type, u32 value)
{ {
u8 *ser = tal_arr(NULL, u8, 0); u8 *ser = tal_arr(NULL, u8, 0);
towire_tu64(&ser, value); towire_tu64(&ser, value);
tlvstream_set_raw(stream, type, take(ser)); tlvstream_set_raw(stream, type, ser, tal_bytelen(ser));
} }
static struct tlv_field *tlvstream_get_raw(struct tlv_field *stream, u64 type) static struct tlv_field *tlvstream_get_raw(struct tlv_field *stream, u64 type)

2
wire/tlvstream.h

@ -39,7 +39,7 @@ void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields);
/* Generic primitive setters for tlvstreams. */ /* Generic primitive setters for tlvstreams. */
void tlvstream_set_raw(struct tlv_field **stream, u64 type, u8 *value TAKES); void tlvstream_set_raw(struct tlv_field **stream, u64 type, void *value, size_t valuelen);
void tlvstream_set_short_channel_id(struct tlv_field **stream, u64 type, void tlvstream_set_short_channel_id(struct tlv_field **stream, u64 type,
struct short_channel_id *value); struct short_channel_id *value);
void tlvstream_set_tu64(struct tlv_field **stream, u64 type, u64 value); void tlvstream_set_tu64(struct tlv_field **stream, u64 type, u64 value);

Loading…
Cancel
Save