Skip to content

Commit aba9af9

Browse files
cdeckerrustyrussell
authored andcommitted
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.
1 parent 4560757 commit aba9af9

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

wire/tlvstream.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "config.h"
22
#include <assert.h>
3+
#include <ccan/asort/asort.h>
34
#include <common/bigsize.h>
45
#include <wire/tlvstream.h>
56
#include <wire/wire.h>
@@ -8,11 +9,19 @@
89
#define SUPERVERBOSE(...)
910
#endif
1011

11-
void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields)
12+
static int tlv_field_cmp(const struct tlv_field *a, const struct tlv_field *b,
13+
void *x)
14+
{
15+
return a->numtype > b->numtype ? 1 : -1;
16+
}
17+
18+
void towire_tlvstream_raw(u8 **pptr, struct tlv_field *fields)
1219
{
1320
if (!fields)
1421
return;
1522

23+
asort(fields, tal_count(fields), tlv_field_cmp, NULL);
24+
1625
for (size_t i = 0; i < tal_count(fields); i++) {
1726
const struct tlv_field *field = &fields[i];
1827
/* BOLT #1:

wire/tlvstream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct tlv_field {
2828
};
2929

3030
/* Given any tlvstream serialize the raw fields (untyped ones). */
31-
void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields);
31+
void towire_tlvstream_raw(u8 **pptr, struct tlv_field *fields);
3232

3333
/* Given a tlv record with manually-set fields, populate ->fields */
3434
#define tlv_make_fields(tlv, type) \

0 commit comments

Comments
 (0)