wire-gen: ensure that tlv messages are correctly ordered

Our TLV serializer relies on TLV outputs to be ordered by type
number. Prior to this commit we relied on 1) the ordering in the
RFC to be correct and 2) users to be using a version of Python that
respects stable ordering of dicts (i.e. Python 3.7+)

Instead of relying on these implicitly, we now explicitly sort messages
by type number when the TLV sets.

Resolves #2956.

Thanks-To: @ScottTre for the sort function
Reported-By: @ZmnSCPxj
This commit is contained in:
lisa neigut 2019-08-20 21:46:32 -05:00 committed by Rusty Russell
parent 4929034a40
commit e99720344e
2 changed files with 4 additions and 1 deletions

View File

@ -203,7 +203,7 @@ static void fromwire_${msg.struct_name()}(const u8 **cursor, size_t *plen, void
<% static = '' if tlv.name in options.expose_tlv_type else 'static ' <% static = '' if tlv.name in options.expose_tlv_type else 'static '
%>\ %>\
${static}const struct tlv_record_type tlvs_${tlv.name}[] = { ${static}const struct tlv_record_type tlvs_${tlv.name}[] = {
% for msg in tlv.messages.values(): % for msg in tlv.ordered_msgs():
{ ${msg.number}, towire_${msg.struct_name()}, fromwire_${msg.struct_name()} }, { ${msg.number}, towire_${msg.struct_name()}, fromwire_${msg.struct_name()} },
% endfor % endfor
}; };

View File

@ -377,6 +377,9 @@ class Tlv(object):
def find_message(self, name): def find_message(self, name):
return self.messages[name] return self.messages[name]
def ordered_msgs(self):
return sorted(self.messages.values(), key=lambda item: int(item.number))
class Master(object): class Master(object):
types = {} types = {}