From aba9af9e12488b31b0c4d4266a299d616403a175 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 17 Jun 2021 18:21:27 +0200 Subject: [PATCH] tlv: Add sorting of the tlvstream So far we didn't need this since we were adding them in the correct order. Now we have multiple possible origins so we better sort them. --- wire/tlvstream.c | 11 ++++++++++- wire/tlvstream.h | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) 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) \