diff --git a/wire/tlvstream.c b/wire/tlvstream.c index 1a03625fc..0ee0771a8 100644 --- a/wire/tlvstream.c +++ b/wire/tlvstream.c @@ -1,5 +1,6 @@ #include "config.h" #include +#include #include #include #include @@ -8,11 +9,19 @@ #define SUPERVERBOSE(...) #endif -void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields) +static int tlv_field_cmp(const struct tlv_field *a, const struct tlv_field *b, + void *x) +{ + return a->numtype > b->numtype ? 1 : -1; +} + +void towire_tlvstream_raw(u8 **pptr, struct tlv_field *fields) { if (!fields) return; + asort(fields, tal_count(fields), tlv_field_cmp, NULL); + for (size_t i = 0; i < tal_count(fields); i++) { const struct tlv_field *field = &fields[i]; /* BOLT #1: diff --git a/wire/tlvstream.h b/wire/tlvstream.h index 964104a9f..0c378510f 100644 --- a/wire/tlvstream.h +++ b/wire/tlvstream.h @@ -28,7 +28,7 @@ struct tlv_field { }; /* Given any tlvstream serialize the raw fields (untyped ones). */ -void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields); +void towire_tlvstream_raw(u8 **pptr, struct tlv_field *fields); /* Given a tlv record with manually-set fields, populate ->fields */ #define tlv_make_fields(tlv, type) \