Browse Source

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 <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
ad8dfaca1c
  1. 8
      common/htlc_wire.c
  2. 4
      common/htlc_wire.h
  3. 7
      common/utxo.c
  4. 2
      common/utxo.h
  5. 15
      lightningd/gossip_msg.c
  6. 5
      lightningd/gossip_msg.h
  7. 19
      tools/generate-wire.py

8
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)

4
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);

7
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;
}

2
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 */

15
lightningd/gossip_msg.c

@ -2,31 +2,36 @@
#include <lightningd/gossip_msg.h>
#include <wire/wire.h>
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; i<numaddresses; i++) {
/* Gossipd doesn't hand us addresses we can't understand. */
if (!fromwire_wireaddr(pptr, max, entry->addresses)) {
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);

5
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);

19
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,

Loading…
Cancel
Save