From ad8dfaca1c650309086bc295c601a3f2bc5dc1fc Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 8 Feb 2018 11:53:46 +1030 Subject: [PATCH] tools/generate_wire.py: make varlen structs self-allocate. If we tell it a struct is variable length, make fromwire() allocate and return it off ctx. Signed-off-by: Rusty Russell --- common/htlc_wire.c | 8 +++++--- common/htlc_wire.h | 4 ++-- common/utxo.c | 7 +++++-- common/utxo.h | 2 +- lightningd/gossip_msg.c | 15 ++++++++++----- lightningd/gossip_msg.h | 5 ++--- tools/generate-wire.py | 19 ++++++++++++------- 7 files changed, 37 insertions(+), 23 deletions(-) diff --git a/common/htlc_wire.c b/common/htlc_wire.c index 490f7c007..52d6dfaab 100644 --- a/common/htlc_wire.c +++ b/common/htlc_wire.c @@ -78,16 +78,18 @@ void fromwire_fulfilled_htlc(const u8 **cursor, size_t *max, fromwire_preimage(cursor, max, &fulfilled->payment_preimage); } -void fromwire_failed_htlc(const tal_t *ctx, const u8 **cursor, size_t *max, - struct failed_htlc *failed) +struct failed_htlc *fromwire_failed_htlc(const tal_t *ctx, const u8 **cursor, size_t *max) { u16 failreason_len; + struct failed_htlc *failed = tal(ctx, struct failed_htlc); failed->id = fromwire_u64(cursor, max); failed->malformed = fromwire_u16(cursor, max); failreason_len = fromwire_u16(cursor, max); - failed->failreason = tal_arr(ctx, u8, failreason_len); + failed->failreason = tal_arr(failed, u8, failreason_len); fromwire_u8_array(cursor, max, failed->failreason, failreason_len); + + return failed; } enum htlc_state fromwire_htlc_state(const u8 **cursor, size_t *max) diff --git a/common/htlc_wire.h b/common/htlc_wire.h index ad4642ad5..8be698d14 100644 --- a/common/htlc_wire.h +++ b/common/htlc_wire.h @@ -47,8 +47,8 @@ void fromwire_added_htlc(const u8 **cursor, size_t *max, struct added_htlc *added); void fromwire_fulfilled_htlc(const u8 **cursor, size_t *max, struct fulfilled_htlc *fulfilled); -void fromwire_failed_htlc(const tal_t *ctx, const u8 **cursor, size_t *max, - struct failed_htlc *failed); +struct failed_htlc *fromwire_failed_htlc(const tal_t *ctx, const u8 **cursor, + size_t *max); void fromwire_changed_htlc(const u8 **cursor, size_t *max, struct changed_htlc *changed); enum htlc_state fromwire_htlc_state(const u8 **cursor, size_t *max); diff --git a/common/utxo.c b/common/utxo.c index b3f7fb049..5114fe664 100644 --- a/common/utxo.c +++ b/common/utxo.c @@ -20,21 +20,24 @@ void towire_utxo(u8 **pptr, const struct utxo *utxo) } } -void fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max, struct utxo *utxo) +struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max) { + struct utxo *utxo = tal(ctx, struct utxo); + fromwire_bitcoin_txid(ptr, max, &utxo->txid); utxo->outnum = fromwire_u32(ptr, max); utxo->amount = fromwire_u64(ptr, max); utxo->keyindex = fromwire_u32(ptr, max); utxo->is_p2sh = fromwire_bool(ptr, max); if (fromwire_bool(ptr, max)) { - utxo->close_info = tal(ctx, struct unilateral_close_info); + utxo->close_info = tal(utxo, struct unilateral_close_info); utxo->close_info->channel_id = fromwire_u64(ptr, max); fromwire_pubkey(ptr, max, &utxo->close_info->peer_id); fromwire_pubkey(ptr, max, &utxo->close_info->commitment_point); } else { utxo->close_info = NULL; } + return utxo; } diff --git a/common/utxo.h b/common/utxo.h index 1f7e93b68..dc6a1b3a9 100644 --- a/common/utxo.h +++ b/common/utxo.h @@ -29,7 +29,7 @@ struct utxo { }; void towire_utxo(u8 **pptr, const struct utxo *utxo); -void fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max, struct utxo *utxo); +struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max); /* build_utxos/funding_tx use array of pointers, but marshall code * wants arr of structs */ diff --git a/lightningd/gossip_msg.c b/lightningd/gossip_msg.c index 0e6244711..e35bddb6c 100644 --- a/lightningd/gossip_msg.c +++ b/lightningd/gossip_msg.c @@ -2,31 +2,36 @@ #include #include -void fromwire_gossip_getnodes_entry(const tal_t *ctx, const u8 **pptr, size_t *max, struct gossip_getnodes_entry *entry) +struct gossip_getnodes_entry *fromwire_gossip_getnodes_entry(const tal_t *ctx, const u8 **pptr, size_t *max) { u8 numaddresses, i; + struct gossip_getnodes_entry *entry; + + entry = tal(ctx, struct gossip_getnodes_entry); fromwire_pubkey(pptr, max, &entry->nodeid); entry->last_timestamp = fromwire_u64(pptr, max); if (entry->last_timestamp < 0) { entry->addresses = NULL; entry->alias = NULL; - return; + return entry; } numaddresses = fromwire_u8(pptr, max); - entry->addresses = tal_arr(ctx, struct wireaddr, numaddresses); + entry->addresses = tal_arr(entry, struct wireaddr, numaddresses); for (i=0; iaddresses)) { fromwire_fail(pptr, max); - return; + return NULL; } } - entry->alias = tal_arr(ctx, u8, fromwire_u8(pptr, max)); + entry->alias = tal_arr(entry, u8, fromwire_u8(pptr, max)); fromwire(pptr, max, entry->alias, tal_len(entry->alias)); fromwire(pptr, max, entry->color, sizeof(entry->color)); + return entry; } + void towire_gossip_getnodes_entry(u8 **pptr, const struct gossip_getnodes_entry *entry) { u8 i, numaddresses = tal_count(entry->addresses); diff --git a/lightningd/gossip_msg.h b/lightningd/gossip_msg.h index 49ee8544d..f040eb543 100644 --- a/lightningd/gossip_msg.h +++ b/lightningd/gossip_msg.h @@ -25,9 +25,8 @@ struct gossip_getchannels_entry { u32 fee_per_millionth; }; -void fromwire_gossip_getnodes_entry(const tal_t *ctx, const u8 **pptr, - size_t *max, - struct gossip_getnodes_entry *entry); +struct gossip_getnodes_entry * +fromwire_gossip_getnodes_entry(const tal_t *ctx, const u8 **pptr, size_t *max); void towire_gossip_getnodes_entry(u8 **pptr, const struct gossip_getnodes_entry *entry); diff --git a/tools/generate-wire.py b/tools/generate-wire.py index 8e8a6add7..fcbaa2931 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -280,10 +280,12 @@ class Message(object): if f.fieldtype.is_assignable(): subcalls.append('\t\t({})[i] = fromwire_{}(&cursor, plen);' .format(name, basetype)) + elif basetype in varlen_structs: + subcalls.append('\t\t{}[i] = fromwire_{}(ctx, &cursor, plen);' + .format(f.name, basetype)) else: - ctx = "ctx, " if basetype in varlen_structs else "" - subcalls.append('\t\tfromwire_{}({}&cursor, plen, {} + i);' - .format(basetype, ctx, name)) + subcalls.append('\t\tfromwire_{}(&cursor, plen, {} + i);' + .format(basetype, name)) def print_fromwire(self,is_header): ctx_arg = 'const tal_t *ctx, ' if self.has_variable_fields else '' @@ -297,6 +299,8 @@ class Message(object): args.append(', {} {}[{}]'.format(f.fieldtype.name, f.name, f.num_elems)) elif f.is_variable_size(): args.append(', {} **{}'.format(f.fieldtype.name, f.name)) + elif f.basetype() in varlen_structs: + args.append(', {} **{}'.format(f.fieldtype.name, f.name)) else: args.append(', {} *{}'.format(f.fieldtype.name, f.name)) @@ -331,11 +335,12 @@ class Message(object): else: subcalls.append('\t*{} = fromwire_{}(&cursor, plen);' .format(f.name, basetype)) + elif basetype in varlen_structs: + subcalls.append('\t*{} = fromwire_{}(ctx, &cursor, plen);' + .format(f.name, basetype)) else: - subcalls.append("\t//4th case {name}".format(name=f.name)) - ctx = "ctx, " if basetype in varlen_structs else "" - subcalls.append('\tfromwire_{}({}&cursor, plen, {});' - .format(basetype, ctx, f.name)) + subcalls.append('\tfromwire_{}(&cursor, plen, {});' + .format(basetype, f.name)) return template.format( name=self.name,