Skip to content

Commit 73e0518

Browse files
committed
tlv: adapt to work with new output format
Updated to match what the CSV generator in the RFC repo actually outputs, see lightning/bolts#597
1 parent a889c6a commit 73e0518

File tree

1 file changed

+16
-73
lines changed

1 file changed

+16
-73
lines changed

tools/generate-wire.py

Lines changed: 16 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,10 @@ def __init__(self, message, name, size, comments, prevname, includes):
136136
self.optional = False
137137
self.is_tlv = False
138138

139-
# field name appended with '+' means this field contains a tlv
140-
if name.endswith('+'):
139+
if name.endswith('_tlv'):
141140
self.is_tlv = True
142-
self.name = name[:-1]
143141
if self.name not in tlv_fields:
144-
tlv_includes, tlv_messages = parse_tlv_file(self.name)
145-
includes += tlv_includes
146-
tlv_fields[self.name] = tlv_messages
142+
tlv_fields[self.name] = []
147143

148144
# ? means optional field (not supported for arrays)
149145
if size.startswith('?'):
@@ -977,68 +973,6 @@ def find_message_with_option(messages, optional_messages, name, option):
977973
return m
978974

979975

980-
def get_directory_prefix():
981-
# FIXME: use prefix of filename
982-
return "wire/"
983-
984-
985-
def get_tlv_filename(field_name):
986-
return 'gen_{}_csv'.format(field_name)
987-
988-
989-
def parse_tlv_file(tlv_field_name):
990-
tlv_includes = []
991-
tlv_messages = []
992-
tlv_comments = []
993-
tlv_prevfield = None
994-
with open(get_directory_prefix() + get_tlv_filename(tlv_field_name)) as f:
995-
for line in f:
996-
# #include gets inserted into header
997-
if line.startswith('#include '):
998-
tlv_includes.append(line)
999-
continue
1000-
1001-
by_comments = line.rstrip().split('#')
1002-
1003-
# Emit a comment if they included one
1004-
if by_comments[1:]:
1005-
tlv_comments.append(' '.join(by_comments[1:]))
1006-
1007-
parts = by_comments[0].split(',')
1008-
if parts == ['']:
1009-
continue
1010-
1011-
if len(parts) == 2:
1012-
# eg commit_sig,132
1013-
tlv_msg = Message(parts[0], Enumtype("TLV_" + parts[0].upper(), parts[1]), tlv_comments, True)
1014-
tlv_messages.append(tlv_msg)
1015-
1016-
tlv_comments = []
1017-
tlv_prevfield = None
1018-
else:
1019-
if len(parts) == 4:
1020-
# eg commit_sig,0,channel-id,8 OR
1021-
# commit_sig,0,channel-id,u64
1022-
m = find_message(tlv_messages, parts[0])
1023-
if m is None:
1024-
raise ValueError('Unknown message {}'.format(parts[0]))
1025-
elif len(parts) == 5:
1026-
# eg.
1027-
# channel_reestablish,48,your_last_per_commitment_secret,32,option209
1028-
m = find_message_with_option(tlv_messages, messages_with_option, parts[0], parts[4])
1029-
else:
1030-
raise ValueError('Line {} malformed'.format(line.rstrip()))
1031-
1032-
f = Field(m.name, parts[2], parts[3], tlv_comments, tlv_prevfield, includes)
1033-
m.addField(f)
1034-
# If it used prevfield as lenvar, keep that for next
1035-
# time (multiple fields can use the same lenvar).
1036-
if not f.lenvar:
1037-
tlv_prevfield = parts[2]
1038-
tlv_comments = []
1039-
return tlv_includes, tlv_messages
1040-
1041-
1042976
parser = argparse.ArgumentParser(description='Generate C from CSV')
1043977
parser.add_argument('--header', action='store_true', help="Create wire header")
1044978
parser.add_argument('--bolt', action='store_true', help="Generate wire-format for BOLT")
@@ -1073,9 +1007,18 @@ def parse_tlv_file(tlv_field_name):
10731007
if parts == ['']:
10741008
continue
10751009

1076-
if len(parts) == 2:
1077-
# eg commit_sig,132
1078-
messages.append(Message(parts[0], Enumtype("WIRE_" + parts[0].upper(), parts[1]), comments, False))
1010+
is_tlv_msg = len(parts) == 3
1011+
if len(parts) == 2 or is_tlv_msg:
1012+
# eg: commit_sig,132,(_tlv)
1013+
message = Message(parts[0],
1014+
Enumtype("WIRE_" + parts[0].upper(), parts[1]),
1015+
comments,
1016+
is_tlv_msg)
1017+
1018+
messages.append(message)
1019+
if is_tlv_msg:
1020+
tlv_fields[parts[2]].append(message)
1021+
10791022
comments = []
10801023
prevfield = None
10811024
else:
@@ -1265,8 +1208,8 @@ def build_tlv_structs(tlv_fields):
12651208
towire_decls += build_tlv_towires(tlv_fields)
12661209
fromwire_decls += build_tlv_fromwires(tlv_fields)
12671210

1268-
towire_decls += [m.print_towire(options.header, '') for m in messages + messages_with_option]
1269-
fromwire_decls += [m.print_fromwire(options.header, '') for m in messages + messages_with_option]
1211+
towire_decls += [m.print_towire(options.header, '') for m in toplevel_messages + messages_with_option]
1212+
fromwire_decls += [m.print_fromwire(options.header, '') for m in toplevel_messages + messages_with_option]
12701213
decls = fromwire_decls + towire_decls
12711214

12721215
print(template.format(

0 commit comments

Comments
 (0)