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>
This commit is contained in:
Christian Decker 2020-07-09 10:39:22 +02:00
parent 7978a13cae
commit 514c4044c8
4 changed files with 10 additions and 12 deletions

View File

@ -55,7 +55,6 @@ static void keysend_cb(struct keysend_data *d, struct payment *p) {
struct route_hop *last_hop;
struct createonion_hop *last_payload;
size_t hopcount;
u8 *raw_preimage;
if (p->step == PAYMENT_STEP_GOT_ROUTE) {
/* 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)
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);
last_payload = &p->createonion_request->hops[hopcount - 1];
tlvstream_set_raw(&last_payload->tlv_payload->fields, PREIMAGE_TLV_TYPE,
take(raw_preimage));
&d->preimage, sizeof(struct preimage));
return payment_continue(p);
}

View File

@ -815,8 +815,10 @@ static void tlvstream_set_tlv_payload_data(struct tlv_field **stream,
u8 *ser = tal_arr(NULL, u8, 0);
towire_secret(&ser, payment_secret);
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,
struct createonion_hop *dst,
struct route_hop *node,

View File

@ -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;
f.length = tal_bytelen(value);
f.length = valuelen;
f.numtype = type;
f.value = tal_dup_arr(*stream, u8, value, f.length, 0);
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);
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)
{
u8 *ser = tal_arr(NULL, u8, 0);
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)
{
u8 *ser = tal_arr(NULL, u8, 0);
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)

View File

@ -39,7 +39,7 @@ void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields);
/* 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,
struct short_channel_id *value);
void tlvstream_set_tu64(struct tlv_field **stream, u64 type, u64 value);