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.
This commit is contained in:
Christian Decker 2021-06-17 18:21:27 +02:00 committed by Rusty Russell
parent 45607577b9
commit aba9af9e12
2 changed files with 11 additions and 2 deletions

View file

@ -1,5 +1,6 @@
#include "config.h"
#include <assert.h>
#include <ccan/asort/asort.h>
#include <common/bigsize.h>
#include <wire/tlvstream.h>
#include <wire/wire.h>
@ -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:

View file

@ -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) \