Browse Source

generate-wire.py: don't generate structures, hand in all values.

This is a bit more awkward for large structures, but avoids
indirection for the simpler ones (I copied the structures for the test
code, however).  We also remove explicit padding.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
c864b28068
  1. 332
      daemon/p2p_announce.c
  2. 6
      daemon/p2p_announce.h
  3. 9
      daemon/peer.c
  4. 109
      tools/generate-wire.py
  5. 4
      wire/fromwire.c
  6. 698
      wire/test/run-peer-wire.c
  7. 2
      wire/towire.c
  8. 5
      wire/wire.h

332
daemon/p2p_announce.c

@ -6,6 +6,7 @@
#include "daemon/routing.h"
#include "daemon/secrets.h"
#include "daemon/timeout.h"
#include "utils.h"
#include <arpa/inet.h>
#include <ccan/tal/str/str.h>
@ -132,255 +133,348 @@ static bool add_channel_direction(struct lightningd_state *dstate,
void handle_channel_announcement(
struct peer *peer,
const struct msg_channel_announcement *msg)
const u8 *announce, size_t len)
{
u8 *serialized;
bool forward = false;
if (!msg)
struct signature node_signature_1;
struct signature node_signature_2;
struct channel_id channel_id;
struct signature bitcoin_signature_1;
struct signature bitcoin_signature_2;
struct pubkey node_id_1;
struct pubkey node_id_2;
struct pubkey bitcoin_key_1;
struct pubkey bitcoin_key_2;
const tal_t *tmpctx = tal_tmpctx(peer);
serialized = tal_dup_arr(tmpctx, u8, announce, len, 0);
/* FIXME: use NULL arg to mean tal_count() here! */
if (!fromwire_channel_announcement(serialized, &len,
&node_signature_1, &node_signature_2,
&channel_id,
&bitcoin_signature_1,
&bitcoin_signature_2,
&node_id_1, &node_id_2,
&bitcoin_key_1, &bitcoin_key_2)) {
tal_free(tmpctx);
return;
}
//FIXME(cdecker) Check signatures, when the spec is settled
//FIXME(cdecker) Check chain topology for the anchor TX
serialized = towire_channel_announcement(msg, msg);
log_debug(peer->log,
"Received channel_announcement for channel %d:%d:%d",
msg->channel_id.blocknum,
msg->channel_id.txnum,
msg->channel_id.outnum
channel_id.blocknum,
channel_id.txnum,
channel_id.outnum
);
forward |= add_channel_direction(peer->dstate, &msg->node_id_1,
&msg->node_id_2, 0, &msg->channel_id,
forward |= add_channel_direction(peer->dstate, &node_id_1,
&node_id_2, 0, &channel_id,
serialized);
forward |= add_channel_direction(peer->dstate, &msg->node_id_2,
&msg->node_id_1, 1, &msg->channel_id,
forward |= add_channel_direction(peer->dstate, &node_id_2,
&node_id_1, 1, &channel_id,
serialized);
if (!forward){
log_debug(peer->log, "Not forwarding channel_announcement");
tal_free(tmpctx);
return;
}
u8 *tag = tal_arr(msg, u8, 0);
towire_channel_id(&tag, &msg->channel_id);
u8 *tag = tal_arr(tmpctx, u8, 0);
towire_channel_id(&tag, &channel_id);
queue_broadcast(peer->dstate, WIRE_CHANNEL_ANNOUNCEMENT,
0, /* `channel_announcement`s do not have a timestamp */
tag, serialized);
tal_free(msg);
tal_free(tmpctx);
}
void handle_channel_update(struct peer *peer, const struct msg_channel_update *msg)
void handle_channel_update(struct peer *peer, const u8 *update, size_t len)
{
if (!msg)
return;
u8 *serialized;
struct node_connection *c;
struct signature signature;
struct channel_id channel_id;
u32 timestamp;
u16 flags;
u16 expiry;
u32 htlc_minimum_msat;
u32 fee_base_msat;
u32 fee_proportional_millionths;
const tal_t *tmpctx = tal_tmpctx(peer);
serialized = tal_dup_arr(tmpctx, u8, update, len, 0);
/* FIXME: use NULL arg to mean tal_count() here! */
if (!fromwire_channel_update(serialized, &len, &signature, &channel_id,
&timestamp, &flags, &expiry,
&htlc_minimum_msat, &fee_base_msat,
&fee_proportional_millionths)) {
tal_free(tmpctx);
return;
}
log_debug(peer->log, "Received channel_update for channel %d:%d:%d(%d)",
msg->channel_id.blocknum,
msg->channel_id.txnum,
msg->channel_id.outnum,
msg->flags & 0x01
channel_id.blocknum,
channel_id.txnum,
channel_id.outnum,
flags & 0x01
);
c = get_connection_by_cid(peer->dstate, &msg->channel_id, msg->flags & 0x1);
c = get_connection_by_cid(peer->dstate, &channel_id, flags & 0x1);
if (!c) {
log_debug(peer->log, "Ignoring update for unknown channel %d:%d:%d",
msg->channel_id.blocknum,
msg->channel_id.txnum,
msg->channel_id.outnum
channel_id.blocknum,
channel_id.txnum,
channel_id.outnum
);
tal_free(tmpctx);
return;
} else if (c->last_timestamp >= msg->timestamp) {
} else if (c->last_timestamp >= timestamp) {
log_debug(peer->log, "Ignoring outdated update.");
tal_free(tmpctx);
return;
}
//FIXME(cdecker) Check signatures
serialized = towire_channel_update(msg, msg);
c->last_timestamp = msg->timestamp;
c->delay = msg->expiry;
c->htlc_minimum_msat = msg->htlc_minimum_msat;
c->base_fee = msg->fee_base_msat;
c->proportional_fee = msg->fee_proportional_millionths;
c->last_timestamp = timestamp;
c->delay = expiry;
c->htlc_minimum_msat = htlc_minimum_msat;
c->base_fee = fee_base_msat;
c->proportional_fee = fee_proportional_millionths;
c->active = true;
log_debug(peer->log, "Channel %d:%d:%d(%d) was updated.",
msg->channel_id.blocknum,
msg->channel_id.txnum,
msg->channel_id.outnum,
msg->flags
channel_id.blocknum,
channel_id.txnum,
channel_id.outnum,
flags
);
u8 *tag = tal_arr(msg, u8, 0);
towire_channel_id(&tag, &msg->channel_id);
u8 *tag = tal_arr(tmpctx, u8, 0);
towire_channel_id(&tag, &channel_id);
queue_broadcast(peer->dstate,
WIRE_CHANNEL_UPDATE,
msg->timestamp,
timestamp,
tag,
serialized);
tal_free(c->channel_update);
c->channel_update = tal_dup_arr(c, u8, serialized, tal_count(serialized), 0);
tal_free(msg);
c->channel_update = tal_steal(c, serialized);
tal_free(tmpctx);
}
void handle_node_announcement(
struct peer *peer, const struct msg_node_announcement *msg)
struct peer *peer, const u8 *node_ann, size_t len)
{
u8 *serialized;
struct sha256_double hash;
struct node *node;
if (!msg)
struct signature signature;
u32 timestamp;
struct ipv6 ipv6;
u16 port;
struct pubkey node_id;
u8 rgb_color[3];
u8 alias[32];
const tal_t *tmpctx = tal_tmpctx(peer);
serialized = tal_dup_arr(tmpctx, u8, node_ann, len, 0);
/* FIXME: use NULL arg to mean tal_count() here! */
if (!fromwire_node_announcement(serialized, &len,
&signature, &timestamp, &ipv6, &port,
&node_id, rgb_color, alias)) {
tal_free(tmpctx);
return;
}
log_debug_struct(peer->log,
"Received node_announcement for node %s",
struct pubkey, &msg->node_id);
struct pubkey, &node_id);
serialized = towire_node_announcement(msg, msg);
sha256_double(&hash, serialized + 64, tal_count(serialized) - 64);
if (!check_signed_hash(&hash, &msg->signature, &msg->node_id)) {
if (!check_signed_hash(&hash, &signature, &node_id)) {
log_debug(peer->dstate->base_log,
"Ignoring node announcement, signature verification failed.");
tal_free(tmpctx);
return;
}
node = get_node(peer->dstate, &msg->node_id);
node = get_node(peer->dstate, &node_id);
if (!node) {
log_debug(peer->dstate->base_log,
"Node not found, was the node_announcement preceeded by at least channel_announcement?");
tal_free(tmpctx);
return;
} else if (node->last_timestamp >= msg->timestamp) {
} else if (node->last_timestamp >= timestamp) {
log_debug(peer->dstate->base_log,
"Ignoring node announcement, it's outdated.");
tal_free(tmpctx);
return;
}
node->last_timestamp = msg->timestamp;
node->last_timestamp = timestamp;
node->hostname = tal_free(node->hostname);
node->hostname = read_ip(node, &msg->ipv6);
node->port = msg->port;
memcpy(node->rgb_color, msg->rgb_color, 3);
node->hostname = read_ip(node, &ipv6);
node->port = port;
memcpy(node->rgb_color, rgb_color, 3);
u8 *tag = tal_arr(msg, u8, 0);
towire_pubkey(&tag, &msg->node_id);
u8 *tag = tal_arr(tmpctx, u8, 0);
towire_pubkey(&tag, &node_id);
queue_broadcast(peer->dstate,
WIRE_NODE_ANNOUNCEMENT,
msg->timestamp,
timestamp,
tag,
serialized);
tal_free(node->node_announcement);
node->node_announcement = tal_dup_arr(node, u8, serialized, tal_count(serialized), 0);
tal_free(msg);
node->node_announcement = tal_steal(node, serialized);
tal_free(tmpctx);
}
static void broadcast_channel_update(struct lightningd_state *dstate, struct peer *peer)
{
struct msg_channel_update *msg;
struct txlocator *loc;
u8 *serialized;
struct signature signature;
struct channel_id channel_id;
u32 timestamp = time_now().ts.tv_sec;
const tal_t *tmpctx = tal_tmpctx(dstate);
msg = tal(peer, struct msg_channel_update);
loc = locate_tx(msg, dstate, &peer->anchor.txid);
msg->timestamp = timeabs_to_timeval(time_now()).tv_sec;
msg->channel_id.blocknum = loc->blkheight;
msg->channel_id.txnum = loc->index;
msg->channel_id.outnum = peer->anchor.index;
msg->flags = pubkey_cmp(&dstate->id, peer->id) > 0;
msg->expiry = dstate->config.min_htlc_expiry;
//FIXME(cdecker) Make the minimum HTLC configurable
msg->htlc_minimum_msat = 1;
msg->fee_base_msat = dstate->config.fee_base;
msg->fee_proportional_millionths = dstate->config.fee_per_satoshi;
loc = locate_tx(tmpctx, dstate, &peer->anchor.txid);
channel_id.blocknum = loc->blkheight;
channel_id.txnum = loc->index;
channel_id.outnum = peer->anchor.index;
/* Avoid triggering memcheck */
memset(&msg->signature, 0, sizeof(msg->signature));
serialized = towire_channel_update(msg, msg);
privkey_sign(dstate, serialized + 64, tal_count(serialized) - 64, &msg->signature);
serialized = towire_channel_update(msg, msg);
memset(&signature, 0, sizeof(signature));
serialized = towire_channel_update(tmpctx, &signature, &channel_id,
timestamp,
pubkey_cmp(&dstate->id, peer->id) > 0,
dstate->config.min_htlc_expiry,
//FIXME(cdecker) Make the minimum HTLC configurable
1,
dstate->config.fee_base,
dstate->config.fee_per_satoshi);
privkey_sign(dstate, serialized + 64, tal_count(serialized) - 64,
&signature);
serialized = towire_channel_update(tmpctx, &signature, &channel_id,
timestamp,
pubkey_cmp(&dstate->id, peer->id) > 0,
dstate->config.min_htlc_expiry,
1,
dstate->config.fee_base,
dstate->config.fee_per_satoshi);
broadcast(dstate, WIRE_CHANNEL_UPDATE, serialized, NULL);
tal_free(msg);
tal_free(tmpctx);
}
static void broadcast_node_announcement(struct lightningd_state *dstate)
{
u8 *serialized;
/* Are we listeing for incoming connections at all? */
if (!dstate->external_ip || !dstate->portnum)
struct signature signature;
struct ipv6 ipv6;
static const u8 rgb_color[3];
static const u8 alias[32];
u32 timestamp = time_now().ts.tv_sec;
const tal_t *tmpctx = tal_tmpctx(dstate);
/* Are we listening for incoming connections at all? */
if (!dstate->external_ip || !dstate->portnum) {
tal_free(tmpctx);
return;
}
struct msg_node_announcement *msg = tal(dstate, struct msg_node_announcement);
msg->timestamp = timeabs_to_timeval(time_now()).tv_sec;
msg->node_id = dstate->id;
write_ip(&msg->ipv6, dstate->external_ip);
msg->port = dstate->portnum;
memset(&msg->rgb_color, 0x00, 3);
serialized = towire_node_announcement(msg, msg);
privkey_sign(dstate, serialized + 64, tal_count(serialized) - 64, &msg->signature);
serialized = towire_node_announcement(msg, msg);
/* Avoid triggering memcheck */
memset(&signature, 0, sizeof(signature));
write_ip(&ipv6, dstate->external_ip);
serialized = towire_node_announcement(tmpctx, &signature,
timestamp,
&ipv6, dstate->portnum,
&dstate->id, rgb_color, alias);
privkey_sign(dstate, serialized + 64, tal_count(serialized) - 64,
&signature);
serialized = towire_node_announcement(tmpctx, &signature,
timestamp,
&ipv6, dstate->portnum,
&dstate->id, rgb_color, alias);
broadcast(dstate, WIRE_NODE_ANNOUNCEMENT, serialized, NULL);
tal_free(msg);
tal_free(tmpctx);
}
static void broadcast_channel_announcement(struct lightningd_state *dstate, struct peer *peer)
{
struct msg_channel_announcement *msg = tal(peer, struct msg_channel_announcement);
struct txlocator *loc;
struct channel_id channel_id;
struct signature node_signature[2];
struct signature bitcoin_signature[2];
const struct pubkey *node_id[2];
const struct pubkey *bitcoin_key[2];
struct signature *my_node_signature;
struct signature *my_bitcoin_signature;
u8 *serialized;
const tal_t *tmpctx = tal_tmpctx(dstate);
loc = locate_tx(msg, dstate, &peer->anchor.txid);
msg->channel_id.blocknum = loc->blkheight;
msg->channel_id.txnum = loc->index;
msg->channel_id.outnum = peer->anchor.index;
loc = locate_tx(tmpctx, dstate, &peer->anchor.txid);
channel_id.blocknum = loc->blkheight;
channel_id.txnum = loc->index;
channel_id.outnum = peer->anchor.index;
/* Set all sigs to zero */
memset(&msg->node_signature_1, 0, sizeof(msg->node_signature_1));
memset(&msg->bitcoin_signature_1, 0, sizeof(msg->bitcoin_signature_1));
memset(&msg->node_signature_2, 0, sizeof(msg->node_signature_2));
memset(&msg->bitcoin_signature_2, 0, sizeof(msg->bitcoin_signature_2));
memset(node_signature, 0, sizeof(node_signature));
memset(bitcoin_signature, 0, sizeof(bitcoin_signature));
//FIXME(cdecker) Copy remote stored signatures into place
if (pubkey_cmp(&dstate->id, peer->id) > 0) {
msg->node_id_1 = *peer->id;
msg->node_id_2 = dstate->id;
msg->bitcoin_key_1 = *peer->id;
msg->bitcoin_key_2 = dstate->id;
my_node_signature = &msg->node_signature_2;
my_bitcoin_signature = &msg->bitcoin_signature_2;
node_id[0] = peer->id;
node_id[1] = &dstate->id;
bitcoin_key[0] = peer->id;
bitcoin_key[1] = &dstate->id;
my_node_signature = &node_signature[1];
my_bitcoin_signature = &bitcoin_signature[1];
} else {
msg->node_id_2 = *peer->id;
msg->node_id_1 = dstate->id;
msg->bitcoin_key_2 = *peer->id;
msg->bitcoin_key_1 = dstate->id;
my_node_signature = &msg->node_signature_1;
my_bitcoin_signature = &msg->bitcoin_signature_1;
node_id[1] = peer->id;
node_id[0] = &dstate->id;
bitcoin_key[1] = peer->id;
bitcoin_key[0] = &dstate->id;
my_node_signature = &node_signature[0];
my_bitcoin_signature = &bitcoin_signature[0];
}
/* Sign the node_id with the bitcoin_key, proves delegation */
serialized = tal_arr(msg, u8, 0);
serialized = tal_arr(tmpctx, u8, 0);
towire_pubkey(&serialized, &dstate->id);
privkey_sign(dstate, serialized, tal_count(serialized), my_bitcoin_signature);
/* Sign the entire packet with `node_id`, proves integrity and origin */
serialized = towire_channel_announcement(msg, msg);
serialized = towire_channel_announcement(tmpctx, &node_signature[0],
&node_signature[1],
&channel_id,
&bitcoin_signature[0],
&bitcoin_signature[1],
node_id[0],
node_id[1],
bitcoin_key[0],
bitcoin_key[1]);
privkey_sign(dstate, serialized + 128, tal_count(serialized) - 128, my_node_signature);
serialized = towire_channel_announcement(msg, msg);
serialized = towire_channel_announcement(tmpctx, &node_signature[0],
&node_signature[1],
&channel_id,
&bitcoin_signature[0],
&bitcoin_signature[1],
node_id[0],
node_id[1],
bitcoin_key[0],
bitcoin_key[1]);
broadcast(dstate, WIRE_CHANNEL_ANNOUNCEMENT, serialized, NULL);
tal_free(msg);
tal_free(tmpctx);
}
static void announce(struct lightningd_state *dstate)

6
daemon/p2p_announce.h

@ -7,9 +7,9 @@
void setup_p2p_announce(struct lightningd_state *dstate);
/* Handlers for incoming messages */
void handle_channel_announcement(struct peer *peer, const struct msg_channel_announcement *announce);
void handle_channel_update(struct peer *peer, const struct msg_channel_update *update);
void handle_node_announcement(struct peer *peer, const struct msg_node_announcement *node);
void handle_channel_announcement(struct peer *peer, const u8 *announce, size_t len);
void handle_channel_update(struct peer *peer, const u8 *update, size_t len);
void handle_node_announcement(struct peer *peer, const u8 *node, size_t len);
/* Used to announce the existence of a channel and the endpoints */
void announce_channel(struct lightningd_state *dstate, struct peer *peer);

9
daemon/peer.c

@ -1866,16 +1866,13 @@ static bool nested_pkt_in(struct peer *peer, const u32 type,
{
switch (type) {
case WIRE_CHANNEL_ANNOUNCEMENT:
handle_channel_announcement(
peer, fromwire_channel_announcement(peer, innerpkt, &innerpktlen));
handle_channel_announcement(peer, innerpkt, innerpktlen);
break;
case WIRE_CHANNEL_UPDATE:
handle_channel_update(
peer, fromwire_channel_update(peer, innerpkt, &innerpktlen));
handle_channel_update(peer, innerpkt, innerpktlen);
break;
case WIRE_NODE_ANNOUNCEMENT:
handle_node_announcement(
peer, fromwire_node_announcement(peer, innerpkt, &innerpktlen));
handle_node_announcement(peer, innerpkt, innerpktlen);
break;
default:
/* BOLT01: Unknown even typed packets MUST kill the

109
tools/generate-wire.py

@ -12,6 +12,7 @@ class Field(object):
def __init__(self,message,name,size):
self.message = message
self.name = name.replace('-', '_')
self.is_len_var = False
(self.typename, self.basesize) = Field._guess_type(message,self.name,size)
try:
@ -108,6 +109,7 @@ class Message(object):
self.name = name
self.enum = enum
self.fields = []
self.has_variable_fields = False
def checkLenField(self,field):
for f in self.fields:
@ -119,6 +121,7 @@ class Message(object):
if f.is_array() or f.is_variable_size():
raise ValueError('Field {} has non-simple length variable {}'
.format(field.name, field.lenvar))
f.is_len_var = True;
return
raise ValueError('Field {} unknown length variable {}'
.format(field.name, field.lenvar))
@ -128,82 +131,104 @@ class Message(object):
# massive allocations.
if field.is_variable_size():
self.checkLenField(field)
self.has_variable_fields = True
self.fields.append(field)
def print_structure(self):
def print_fromwire(self,is_header):
if not self.fields:
return
print('struct msg_{} {{'.format(self.name));
if self.has_variable_fields:
ctx_arg = 'const tal_t *ctx, '
else:
ctx_arg = ''
print('bool fromwire_{}({}const void *p, size_t *plen'
.format(self.name, ctx_arg), end='')
for f in self.fields:
# If size isn't known, it's a pointer.
if f.is_len_var:
continue
if f.is_padding():
continue
if f.is_array():
print('\t{} {}[{}];'.format(f.typename, f.name, f.num_elems))
print(', {} {}[{}]'.format(f.typename, f.name, f.num_elems), end='')
elif f.is_variable_size():
print('\t{} *{};'.format(f.typename, f.name))
print(', {} **{}'.format(f.typename, f.name), end='')
else:
print('\t{} {};'.format(f.typename, f.name))
print('};')
print(', {} *{}'.format(f.typename, f.name), end='')
def print_fromwire(self,is_header):
if not self.fields:
if is_header:
print(');')
return
print('struct msg_{0} *fromwire_{0}(const tal_t *ctx, const void *p, size_t *len)'.format(self.name), end='')
print(')\n'
'{\n')
if is_header:
print(';')
return
for f in self.fields:
if f.is_len_var:
print('\t{} {};\n'.format(f.typename, f.name));
print('\n'
'{{\n'
'\tconst u8 *cursor = p;\n'
'\tstruct msg_{} *in = tal(ctx, struct msg_{});\n'
''.format(self.name, self.name));
print('\tconst u8 *cursor = p;\n'
'')
for f in self.fields:
basetype=f.typename
if f.typename.startswith('struct '):
basetype=f.typename[7:]
if f.is_array():
if f.is_padding():
print('\tfromwire_pad(&cursor, plen, {});'
.format(f.num_elems))
elif f.is_array():
print("\t//1th case", f.name)
print('\tfromwire_{}_array(&cursor, len, in->{}, {});'
print('\tfromwire_{}_array(&cursor, plen, {}, {});'
.format(basetype, f.name, f.num_elems))
elif f.is_variable_size():
print("\t//2th case", f.name)
print('\tin->{} = tal_arr(in, {}, in->{});'
print('\t*{} = tal_arr(ctx, {}, {});'
.format(f.name, f.typename, f.lenvar))
print('\tfromwire_{}_array(&cursor, len, in->{}, in->{});'
print('\tfromwire_{}_array(&cursor, plen, *{}, {});'
.format(basetype, f.name, f.lenvar))
elif f.is_assignable():
print("\t//3th case", f.name)
print('\tin->{} = fromwire_{}(&cursor, len);'
.format(f.name, basetype))
if f.is_len_var:
print('\t{} = fromwire_{}(&cursor, plen);'
.format(f.name, basetype))
else:
print('\t*{} = fromwire_{}(&cursor, plen);'
.format(f.name, basetype))
else:
print("\t//4th case", f.name)
print('\tfromwire_{}(&cursor, len, &in->{});'
print('\tfromwire_{}(&cursor, plen, {});'
.format(basetype, f.name))
print('\n'
'\tif (!cursor)\n'
'\t\treturn tal_free(in);\n'
'\treturn in;\n'
'\treturn cursor != NULL;\n'
'}\n')
def print_towire(self,is_header):
if not self.fields:
return
print('u8 *towire_{0}(const tal_t *ctx, const struct msg_{0} *out)'.format(self.name), end='')
print('u8 *towire_{}(const tal_t *ctx'
.format(self.name), end='')
for f in self.fields:
if f.is_padding():
continue
if f.is_array():
print(', const {} {}[{}]'.format(f.typename, f.name, f.num_elems), end='')
elif f.is_assignable():
print(', {} {}'.format(f.typename, f.name), end='')
else:
print(', const {} *{}'.format(f.typename, f.name), end='')
if is_header:
print(';')
print(');')
return
print('\n'
print(')\n'
'{\n'
'\tu8 *p = tal_arr(ctx, u8, 0);\n'
'')
@ -213,17 +238,17 @@ class Message(object):
if f.typename.startswith('struct '):
basetype=f.typename[7:]
if f.is_array():
print('\ttowire_{}_array(&p, out->{}, {});'
if f.is_padding():
print('\ttowire_pad(&p, {});'
.format(f.num_elems))
elif f.is_array():
print('\ttowire_{}_array(&p, {}, {});'
.format(basetype, f.name, f.num_elems))
elif f.is_variable_size():
print('\ttowire_{}_array(&p, out->{}, out->{});'
print('\ttowire_{}_array(&p, {}, {});'
.format(basetype, f.name, f.lenvar))
elif f.is_assignable():
print('\ttowire_{}(&p, out->{});'
.format(basetype, f.name))
else:
print('\ttowire_{}(&p, &out->{});'
print('\ttowire_{}(&p, {});'
.format(basetype, f.name))
print('\n'
@ -246,8 +271,6 @@ if options.output_header:
'#define LIGHTNING_{0}\n'
'#include <ccan/tal/tal.h>\n'
'#include <wire/wire.h>\n'
'\n'
'typedef u8 pad;\n'
''.format(idem))
else:
print('#include <{}>\n'
@ -276,10 +299,6 @@ if options.output_header:
print('\t{} = {},'.format(m.enum.name, m.enum.value))
print('};')
# Dump out structure definitions.
for m in messages.values():
m.print_structure()
for m in messages.values():
m.print_fromwire(options.output_header)

4
wire/fromwire.c

@ -118,9 +118,9 @@ void fromwire_u8_array(const u8 **cursor, size_t *max, u8 *arr, size_t num)
fromwire(cursor, max, arr, num);
}
void fromwire_pad_array(const u8 **cursor, size_t *max, u8 *arr, size_t num)
void fromwire_pad(const u8 **cursor, size_t *max, size_t num)
{
fromwire(cursor, max, arr, num);
fromwire(cursor, max, NULL, num);
}
void fromwire_signature_array(const u8 **cursor, size_t *max,

698
wire/test/run-peer-wire.c

@ -1,11 +1,15 @@
#include "../gen_peer_wire.c"
void towire_pad_array_orig(u8 **pptr, const u8 *arr, size_t num);
#define towire_pad_array towire_pad_array_orig
void towire_pad_orig(u8 **pptr, size_t num);
#define towire_pad towire_pad_orig
void fromwire_pad_orig(const u8 **cursor, size_t *max, size_t num);
#define towire_pad towire_pad_orig
#define fromwire_pad fromwire_pad_orig
#include "../towire.c"
#undef towire_pad_array
#include "../fromwire.c"
#undef towire_pad
#undef fromwire_pad
#include <ccan/structeq/structeq.h>
#include <assert.h>
#include <stdio.h>
@ -13,9 +17,16 @@ void towire_pad_array_orig(u8 **pptr, const u8 *arr, size_t num);
secp256k1_context *secp256k1_ctx;
/* We allow non-zero padding for testing. */
void towire_pad_array(u8 **pptr, const u8 *arr, size_t num)
static const void *towire_pad_arr;
void towire_pad(u8 **pptr, size_t num)
{
towire_u8_array(pptr, towire_pad_arr, num);
}
static void *fromwire_pad_arr;
void fromwire_pad(const u8 **cursor, size_t *max, size_t num)
{
towire_u8_array(pptr, arr, num);
fromwire(cursor, max, fromwire_pad_arr, num);
}
/* memsetting pubkeys doesn't work */
@ -67,6 +78,602 @@ static inline bool eq_skip_(const void *p1, const void *p2,
return memcmp(p1, p2, total - (off + skip)) == 0;
}
/* Convenience structs for everyone! */
struct msg_error {
struct channel_id channel_id;
u16 len;
u8 *data;
};
struct msg_closing_signed {
struct channel_id channel_id;
u64 fee_satoshis;
struct signature signature;
};
struct msg_funding_created {
struct channel_id temporary_channel_id;
struct sha256 txid;
u8 output_index;
struct signature signature;
};
struct msg_accept_channel {
struct channel_id temporary_channel_id;
u64 dust_limit_satoshis;
u64 max_htlc_value_in_flight_msat;
u64 channel_reserve_satoshis;
u32 minimum_depth;
u32 htlc_minimum_msat;
u16 to_self_delay;
u16 max_accepted_htlcs;
struct pubkey funding_pubkey;
struct pubkey revocation_basepoint;
struct pubkey payment_basepoint;
struct pubkey delayed_payment_basepoint;
struct pubkey first_per_commitment_point;
};
struct msg_update_fulfill_htlc {
struct channel_id channel_id;
u64 id;
struct sha256 payment_preimage;
};
struct msg_shutdown {
struct channel_id channel_id;
u16 len;
u8 *scriptpubkey;
};
struct msg_funding_signed {
struct channel_id temporary_channel_id;
struct signature signature;
};
struct msg_revoke_and_ack {
struct channel_id channel_id;
struct sha256 per_commitment_secret;
struct pubkey next_per_commitment_point;
u8 padding[1];
u16 num_htlc_timeouts;
struct signature *htlc_timeout_signature;
};
struct msg_channel_update {
struct signature signature;
struct channel_id channel_id;
u32 timestamp;
u16 flags;
u16 expiry;
u32 htlc_minimum_msat;
u32 fee_base_msat;
u32 fee_proportional_millionths;
};
struct msg_funding_locked {
struct channel_id temporary_channel_id;
struct channel_id channel_id;
struct signature announcement_node_signature;
struct signature announcement_bitcoin_signature;
struct pubkey next_per_commitment_point;
};
struct msg_commit_sig {
struct channel_id channel_id;
struct signature signature;
u16 num_htlcs;
struct signature *htlc_signature;
};
struct msg_node_announcement {
struct signature signature;
u32 timestamp;
struct ipv6 ipv6;
u16 port;
struct pubkey node_id;
u8 rgb_color[3];
u8 padding[2];
u8 alias[32];
};
struct msg_open_channel {
struct channel_id temporary_channel_id;
u64 funding_satoshis;
u64 push_msat;
u64 dust_limit_satoshis;
u64 max_htlc_value_in_flight_msat;
u64 channel_reserve_satoshis;
u32 htlc_minimum_msat;
u32 feerate_per_kw;
u16 to_self_delay;
u16 max_accepted_htlcs;
struct pubkey funding_pubkey;
struct pubkey revocation_basepoint;
struct pubkey payment_basepoint;
struct pubkey delayed_payment_basepoint;
struct pubkey first_per_commitment_point;
};
struct msg_update_fail_htlc {
struct channel_id channel_id;
u64 id;
u8 reason[154];
};
struct msg_channel_announcement {
struct signature node_signature_1;
struct signature node_signature_2;
struct channel_id channel_id;
struct signature bitcoin_signature_1;
struct signature bitcoin_signature_2;
struct pubkey node_id_1;
struct pubkey node_id_2;
struct pubkey bitcoin_key_1;
struct pubkey bitcoin_key_2;
};
struct msg_init {
u16 gflen;
u8 *globalfeatures;
u16 lflen;
u8 *localfeatures;
};
struct msg_update_add_htlc {
struct channel_id channel_id;
u64 id;
u32 amount_msat;
u32 expiry;
struct sha256 payment_hash;
u8 onion_routing_packet[1254];
};
struct msg_update_fee {
struct channel_id channel_id;
u32 feerate_per_kw;
};
static void *towire_struct_channel_announcement(const tal_t *ctx,
const struct msg_channel_announcement *s)
{
return towire_channel_announcement(ctx,
&s->node_signature_1,
&s->node_signature_2,
&s->channel_id,
&s->bitcoin_signature_1,
&s->bitcoin_signature_2,
&s->node_id_1,
&s->node_id_2,
&s->bitcoin_key_1,
&s->bitcoin_key_2);
}
static struct msg_channel_announcement *fromwire_struct_channel_announcement(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_channel_announcement *s = tal(ctx, struct msg_channel_announcement);
if (!fromwire_channel_announcement(p, plen,
&s->node_signature_1,
&s->node_signature_2,
&s->channel_id,
&s->bitcoin_signature_1,
&s->bitcoin_signature_2,
&s->node_id_1,
&s->node_id_2,
&s->bitcoin_key_1,
&s->bitcoin_key_2))
return tal_free(s);
return s;
}
static void *towire_struct_open_channel(const tal_t *ctx,
const struct msg_open_channel *s)
{
return towire_open_channel(ctx,
&s->temporary_channel_id,
s->funding_satoshis,
s->push_msat,
s->dust_limit_satoshis,
s->max_htlc_value_in_flight_msat,
s->channel_reserve_satoshis,
s->htlc_minimum_msat,
s->feerate_per_kw,
s->to_self_delay,
s->max_accepted_htlcs,
&s->funding_pubkey,
&s->revocation_basepoint,
&s->payment_basepoint,
&s->delayed_payment_basepoint,
&s->first_per_commitment_point);
}
static struct msg_open_channel *fromwire_struct_open_channel(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_open_channel *s = tal(ctx, struct msg_open_channel);
if (fromwire_open_channel(p, plen,
&s->temporary_channel_id,
&s->funding_satoshis,
&s->push_msat,
&s->dust_limit_satoshis,
&s->max_htlc_value_in_flight_msat,
&s->channel_reserve_satoshis,
&s->htlc_minimum_msat,
&s->feerate_per_kw,
&s->to_self_delay,
&s->max_accepted_htlcs,
&s->funding_pubkey,
&s->revocation_basepoint,
&s->payment_basepoint,
&s->delayed_payment_basepoint,
&s->first_per_commitment_point))
return s;
return tal_free(s);
}
static void *towire_struct_accept_channel(const tal_t *ctx,
const struct msg_accept_channel *s)
{
return towire_accept_channel(ctx,
&s->temporary_channel_id,
s->dust_limit_satoshis,
s->max_htlc_value_in_flight_msat,
s->channel_reserve_satoshis,
s->minimum_depth,
s->htlc_minimum_msat,
s->to_self_delay,
s->max_accepted_htlcs,
&s->funding_pubkey,
&s->revocation_basepoint,
&s->payment_basepoint,
&s->delayed_payment_basepoint,
&s->first_per_commitment_point);
}
static struct msg_accept_channel *fromwire_struct_accept_channel(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_accept_channel *s = tal(ctx, struct msg_accept_channel);
if (fromwire_accept_channel(p, plen,
&s->temporary_channel_id,
&s->dust_limit_satoshis,
&s->max_htlc_value_in_flight_msat,
&s->channel_reserve_satoshis,
&s->minimum_depth,
&s->htlc_minimum_msat,
&s->to_self_delay,
&s->max_accepted_htlcs,
&s->funding_pubkey,
&s->revocation_basepoint,
&s->payment_basepoint,
&s->delayed_payment_basepoint,
&s->first_per_commitment_point))
return s;
return tal_free(s);
}
static void *towire_struct_node_announcement(const tal_t *ctx,
const struct msg_node_announcement *s)
{
towire_pad_arr = s->padding;
return towire_node_announcement(ctx,
&s->signature,
s->timestamp,
&s->ipv6,
s->port,
&s->node_id,
s->rgb_color,
s->alias);
}
static struct msg_node_announcement *fromwire_struct_node_announcement(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_node_announcement *s = tal(ctx, struct msg_node_announcement);
fromwire_pad_arr = s->padding;
if (!fromwire_node_announcement(p, plen,
&s->signature,
&s->timestamp,
&s->ipv6,
&s->port,
&s->node_id,
s->rgb_color,
s->alias))
return tal_free(s);
return s;
}
static void *towire_struct_channel_update(const tal_t *ctx,
const struct msg_channel_update *s)
{
return towire_channel_update(ctx,
&s->signature,
&s->channel_id,
s->timestamp,
s->flags,
s->expiry,
s->htlc_minimum_msat,
s->fee_base_msat,
s->fee_proportional_millionths);
}
static struct msg_channel_update *fromwire_struct_channel_update(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_channel_update *s = tal(ctx, struct msg_channel_update);
if (fromwire_channel_update(p, plen,
&s->signature,
&s->channel_id,
&s->timestamp,
&s->flags,
&s->expiry,
&s->htlc_minimum_msat,
&s->fee_base_msat,
&s->fee_proportional_millionths))
return s;
return tal_free(s);
}
static void *towire_struct_funding_locked(const tal_t *ctx,
const struct msg_funding_locked *s)
{
return towire_funding_locked(ctx,
&s->temporary_channel_id,
&s->channel_id,
&s->announcement_node_signature,
&s->announcement_bitcoin_signature,
&s->next_per_commitment_point);
}
static struct msg_funding_locked *fromwire_struct_funding_locked(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_funding_locked *s = tal(ctx, struct msg_funding_locked);
if (fromwire_funding_locked(p, plen,
&s->temporary_channel_id,
&s->channel_id,
&s->announcement_node_signature,
&s->announcement_bitcoin_signature,
&s->next_per_commitment_point))
return s;
return tal_free(s);
}
static void *towire_struct_update_fail_htlc(const tal_t *ctx,
const struct msg_update_fail_htlc *s)
{
return towire_update_fail_htlc(ctx,
&s->channel_id,
s->id,
s->reason);
}
static struct msg_update_fail_htlc *fromwire_struct_update_fail_htlc(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_update_fail_htlc *s = tal(ctx, struct msg_update_fail_htlc);
if (fromwire_update_fail_htlc(p, plen,
&s->channel_id,
&s->id,
s->reason))
return s;
return tal_free(s);
}
static void *towire_struct_update_fulfill_htlc(const tal_t *ctx,
const struct msg_update_fulfill_htlc *s)
{
return towire_update_fulfill_htlc(ctx,
&s->channel_id,
s->id,
&s->payment_preimage);
}
static struct msg_update_fulfill_htlc *fromwire_struct_update_fulfill_htlc(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_update_fulfill_htlc *s = tal(ctx, struct msg_update_fulfill_htlc);
if (fromwire_update_fulfill_htlc(p, plen,
&s->channel_id,
&s->id,
&s->payment_preimage))
return s;
return tal_free(s);
}
static void *towire_struct_commit_sig(const tal_t *ctx,
const struct msg_commit_sig *s)
{
return towire_commit_sig(ctx,
&s->channel_id,
&s->signature,
s->num_htlcs,
s->htlc_signature);
}
static struct msg_commit_sig *fromwire_struct_commit_sig(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_commit_sig *s = tal(ctx, struct msg_commit_sig);
if (!fromwire_commit_sig(s, p, plen,
&s->channel_id,
&s->signature,
&s->htlc_signature))
return tal_free(s);
s->num_htlcs = tal_count(s->htlc_signature);
return s;
}
static void *towire_struct_revoke_and_ack(const tal_t *ctx,
const struct msg_revoke_and_ack *s)
{
towire_pad_arr = s->padding;
return towire_revoke_and_ack(ctx,
&s->channel_id,
&s->per_commitment_secret,
&s->next_per_commitment_point,
s->num_htlc_timeouts,
s->htlc_timeout_signature);
}
static struct msg_revoke_and_ack *fromwire_struct_revoke_and_ack(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_revoke_and_ack *s = tal(ctx, struct msg_revoke_and_ack);
fromwire_pad_arr = s->padding;
if (!fromwire_revoke_and_ack(s, p, plen,
&s->channel_id,
&s->per_commitment_secret,
&s->next_per_commitment_point,
&s->htlc_timeout_signature))
return tal_free(s);
s->num_htlc_timeouts = tal_count(s->htlc_timeout_signature);
return s;
}
static void *towire_struct_funding_signed(const tal_t *ctx,
const struct msg_funding_signed *s)
{
return towire_funding_signed(ctx,
&s->temporary_channel_id,
&s->signature);
}
static struct msg_funding_signed *fromwire_struct_funding_signed(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_funding_signed *s = tal(ctx, struct msg_funding_signed);
if (fromwire_funding_signed(p, plen,
&s->temporary_channel_id,
&s->signature))
return s;
return tal_free(s);
}
static void *towire_struct_closing_signed(const tal_t *ctx,
const struct msg_closing_signed *s)
{
return towire_closing_signed(ctx,
&s->channel_id,
s->fee_satoshis,
&s->signature);
}
static struct msg_closing_signed *fromwire_struct_closing_signed(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_closing_signed *s = tal(ctx, struct msg_closing_signed);
if (fromwire_closing_signed(p, plen,
&s->channel_id,
&s->fee_satoshis,
&s->signature))
return s;
return tal_free(s);
}
static void *towire_struct_shutdown(const tal_t *ctx,
const struct msg_shutdown *s)
{
return towire_shutdown(ctx,
&s->channel_id,
s->len,
s->scriptpubkey);
}
static struct msg_shutdown *fromwire_struct_shutdown(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_shutdown *s = tal(ctx, struct msg_shutdown);
if (!fromwire_shutdown(s, p, plen,
&s->channel_id,
&s->scriptpubkey))
return tal_free(s);
s->len = tal_count(s->scriptpubkey);
return s;
}
static void *towire_struct_funding_created(const tal_t *ctx,
const struct msg_funding_created *s)
{
return towire_funding_created(ctx,
&s->temporary_channel_id,
&s->txid,
s->output_index,
&s->signature);
}
static struct msg_funding_created *fromwire_struct_funding_created(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_funding_created *s = tal(ctx, struct msg_funding_created);
if (fromwire_funding_created(p, plen,
&s->temporary_channel_id,
&s->txid,
&s->output_index,
&s->signature))
return s;
return tal_free(s);
}
static void *towire_struct_error(const tal_t *ctx,
const struct msg_error *s)
{
return towire_error(ctx,
&s->channel_id,
s->len,
s->data);
}
static struct msg_error *fromwire_struct_error(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_error *s = tal(ctx, struct msg_error);
if (!fromwire_error(s, p, plen,
&s->channel_id,
&s->data))
return tal_free(s);
s->len = tal_count(s->data);
return s;
}
static void *towire_struct_update_add_htlc(const tal_t *ctx,
const struct msg_update_add_htlc *s)
{
return towire_update_add_htlc(ctx,
&s->channel_id,
s->id,
s->amount_msat,
s->expiry,
&s->payment_hash,
s->onion_routing_packet);
}
static struct msg_update_add_htlc *fromwire_struct_update_add_htlc(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_update_add_htlc *s = tal(ctx, struct msg_update_add_htlc);
if (fromwire_update_add_htlc(p, plen,
&s->channel_id,
&s->id,
&s->amount_msat,
&s->expiry,
&s->payment_hash,
s->onion_routing_packet))
return s;
return tal_free(s);
}
static void *towire_struct_update_fee(const tal_t *ctx,
const struct msg_update_fee *s)
{
return towire_update_fee(ctx,
&s->channel_id,
s->feerate_per_kw);
}
static struct msg_update_fee *fromwire_struct_update_fee(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_update_fee *s = tal(ctx, struct msg_update_fee);
if (fromwire_update_fee(p, plen,
&s->channel_id,
&s->feerate_per_kw))
return s;
return tal_free(s);
}
static void *towire_struct_init(const tal_t *ctx,
const struct msg_init *s)
{
return towire_init(ctx,
s->gflen,
s->globalfeatures,
s->lflen,
s->localfeatures);
}
static struct msg_init *fromwire_struct_init(const tal_t *ctx, const void *p, size_t *plen)
{
struct msg_init *s = tal(ctx, struct msg_init);
if (!fromwire_init(s, p, plen,
&s->globalfeatures,
&s->localfeatures))
return tal_free(s);
s->gflen = tal_count(s->globalfeatures);
s->lflen = tal_count(s->localfeatures);
return s;
}
static bool channel_announcement_eq(const struct msg_channel_announcement *a,
const struct msg_channel_announcement *b)
{
@ -184,8 +791,7 @@ static bool node_announcement_eq(const struct msg_node_announcement *a,
const struct msg_node_announcement *b)
{
return eq_with(a, b, port)
&& eq_between(a, b, node_id, pad)
&& eq_field(a, b, alias);
&& eq_between(a, b, node_id, alias);
}
/* Try flipping each bit, try running short. */
@ -193,13 +799,13 @@ static bool node_announcement_eq(const struct msg_node_announcement *a,
for (i = 0; i < tal_count(msg) * 8; i++) { \
len = tal_count(msg); \
msg[i / 8] ^= (1 << (i%8)); \
b = fromwire_##type(ctx, msg, &len); \
b = fromwire_struct_##type(ctx, msg, &len); \
assert(!b || !type##_eq(a, b)); \
msg[i / 8] ^= (1 << (i%8)); \
} \
for (i = 0; i < tal_count(msg); i++) { \
len = i; \
b = fromwire_##type(ctx, msg, &len); \
b = fromwire_struct_##type(ctx, msg, &len); \
assert(!b); \
}
@ -236,9 +842,9 @@ int main(void)
set_pubkey(&ca.bitcoin_key_1);
set_pubkey(&ca.bitcoin_key_2);
msg = towire_channel_announcement(ctx, &ca);
msg = towire_struct_channel_announcement(ctx, &ca);
len = tal_count(msg);
ca2 = fromwire_channel_announcement(ctx, msg, &len);
ca2 = fromwire_struct_channel_announcement(ctx, msg, &len);
assert(len == 0);
assert(channel_announcement_eq(&ca, ca2));
test_corruption(&ca, ca2, channel_announcement);
@ -246,18 +852,18 @@ int main(void)
memset(&fl, 2, sizeof(fl));
set_pubkey(&fl.next_per_commitment_point);
msg = towire_funding_locked(ctx, &fl);
msg = towire_struct_funding_locked(ctx, &fl);
len = tal_count(msg);
fl2 = fromwire_funding_locked(ctx, msg, &len);
fl2 = fromwire_struct_funding_locked(ctx, msg, &len);
assert(len == 0);
assert(funding_locked_eq(&fl, fl2));
test_corruption(&fl, fl2, funding_locked);
memset(&ufh, 2, sizeof(ufh));
msg = towire_update_fail_htlc(ctx, &ufh);
msg = towire_struct_update_fail_htlc(ctx, &ufh);
len = tal_count(msg);
ufh2 = fromwire_update_fail_htlc(ctx, msg, &len);
ufh2 = fromwire_struct_update_fail_htlc(ctx, msg, &len);
assert(len == 0);
assert(update_fail_htlc_eq(&ufh, ufh2));
test_corruption(&ufh, ufh2, update_fail_htlc);
@ -267,36 +873,36 @@ int main(void)
cs.htlc_signature = tal_arr(ctx, struct signature, 2);
memset(cs.htlc_signature, 2, sizeof(struct signature)*2);
msg = towire_commit_sig(ctx, &cs);
msg = towire_struct_commit_sig(ctx, &cs);
len = tal_count(msg);
cs2 = fromwire_commit_sig(ctx, msg, &len);
cs2 = fromwire_struct_commit_sig(ctx, msg, &len);
assert(len == 0);
assert(commit_sig_eq(&cs, cs2));
test_corruption(&cs, cs2, commit_sig);
memset(&fs, 2, sizeof(fs));
msg = towire_funding_signed(ctx, &fs);
msg = towire_struct_funding_signed(ctx, &fs);
len = tal_count(msg);
fs2 = fromwire_funding_signed(ctx, msg, &len);
fs2 = fromwire_struct_funding_signed(ctx, msg, &len);
assert(len == 0);
assert(funding_signed_eq(&fs, fs2));
test_corruption(&fs, fs2, funding_signed);
memset(&cls, 2, sizeof(cls));
msg = towire_closing_signed(ctx, &cls);
msg = towire_struct_closing_signed(ctx, &cls);
len = tal_count(msg);
cls2 = fromwire_closing_signed(ctx, msg, &len);
cls2 = fromwire_struct_closing_signed(ctx, msg, &len);
assert(len == 0);
assert(closing_signed_eq(&cls, cls2));
test_corruption(&cls, cls2, closing_signed);
memset(&uflh, 2, sizeof(uflh));
msg = towire_update_fulfill_htlc(ctx, &uflh);
msg = towire_struct_update_fulfill_htlc(ctx, &uflh);
len = tal_count(msg);
uflh2 = fromwire_update_fulfill_htlc(ctx, msg, &len);
uflh2 = fromwire_struct_update_fulfill_htlc(ctx, msg, &len);
assert(len == 0);
assert(update_fulfill_htlc_eq(&uflh, uflh2));
test_corruption(&uflh, uflh2, update_fulfill_htlc);
@ -306,9 +912,9 @@ int main(void)
e.data = tal_arr(ctx, u8, 2);
memset(e.data, 2, 2);
msg = towire_error(ctx, &e);
msg = towire_struct_error(ctx, &e);
len = tal_count(msg);
e2 = fromwire_error(ctx, msg, &len);
e2 = fromwire_struct_error(ctx, msg, &len);
assert(len == 0);
assert(error_eq(&e, e2));
test_corruption(&e, e2, error);
@ -321,18 +927,18 @@ int main(void)
init.localfeatures = tal_arr(ctx, u8, 2);
memset(init.localfeatures, 2, 2);
msg = towire_init(ctx, &init);
msg = towire_struct_init(ctx, &init);
len = tal_count(msg);
init2 = fromwire_init(ctx, msg, &len);
init2 = fromwire_struct_init(ctx, msg, &len);
assert(len == 0);
assert(init_eq(&init, init2));
test_corruption(&init, init2, init);
memset(&uf, 2, sizeof(uf));
msg = towire_update_fee(ctx, &uf);
msg = towire_struct_update_fee(ctx, &uf);
len = tal_count(msg);
uf2 = fromwire_update_fee(ctx, msg, &len);
uf2 = fromwire_struct_update_fee(ctx, msg, &len);
assert(len == 0);
assert(update_fee_eq(&uf, uf2));
test_corruption(&uf, uf2, update_fee);
@ -342,18 +948,18 @@ int main(void)
shutdown.scriptpubkey = tal_arr(ctx, u8, 2);
memset(shutdown.scriptpubkey, 2, 2);
msg = towire_shutdown(ctx, &shutdown);
msg = towire_struct_shutdown(ctx, &shutdown);
len = tal_count(msg);
shutdown2 = fromwire_shutdown(ctx, msg, &len);
shutdown2 = fromwire_struct_shutdown(ctx, msg, &len);
assert(len == 0);
assert(shutdown_eq(&shutdown, shutdown2));
test_corruption(&shutdown, shutdown2, shutdown);
memset(&fc, 2, sizeof(fc));
msg = towire_funding_created(ctx, &fc);
msg = towire_struct_funding_created(ctx, &fc);
len = tal_count(msg);
fc2 = fromwire_funding_created(ctx, msg, &len);
fc2 = fromwire_struct_funding_created(ctx, msg, &len);
assert(len == 0);
assert(funding_created_eq(&fc, fc2));
test_corruption(&fc, fc2, funding_created);
@ -364,9 +970,9 @@ int main(void)
raa.htlc_timeout_signature = tal_arr(ctx, struct signature, 2);
memset(raa.htlc_timeout_signature, 2, sizeof(struct signature) * 2);
msg = towire_revoke_and_ack(ctx, &raa);
msg = towire_struct_revoke_and_ack(ctx, &raa);
len = tal_count(msg);
raa2 = fromwire_revoke_and_ack(ctx, msg, &len);
raa2 = fromwire_struct_revoke_and_ack(ctx, msg, &len);
assert(len == 0);
assert(revoke_and_ack_eq(&raa, raa2));
test_corruption(&raa, raa2, revoke_and_ack);
@ -378,18 +984,18 @@ int main(void)
set_pubkey(&oc.delayed_payment_basepoint);
set_pubkey(&oc.first_per_commitment_point);
msg = towire_open_channel(ctx, &oc);
msg = towire_struct_open_channel(ctx, &oc);
len = tal_count(msg);
oc2 = fromwire_open_channel(ctx, msg, &len);
oc2 = fromwire_struct_open_channel(ctx, msg, &len);
assert(len == 0);
assert(open_channel_eq(&oc, oc2));
test_corruption(&oc, oc2, open_channel);
memset(&cu, 2, sizeof(cu));
msg = towire_channel_update(ctx, &cu);
msg = towire_struct_channel_update(ctx, &cu);
len = tal_count(msg);
cu2 = fromwire_channel_update(ctx, msg, &len);
cu2 = fromwire_struct_channel_update(ctx, msg, &len);
assert(len == 0);
assert(channel_update_eq(&cu, cu2));
test_corruption(&cu, cu2, channel_update);
@ -401,18 +1007,18 @@ int main(void)
set_pubkey(&ac.delayed_payment_basepoint);
set_pubkey(&ac.first_per_commitment_point);
msg = towire_accept_channel(ctx, &ac);
msg = towire_struct_accept_channel(ctx, &ac);
len = tal_count(msg);
ac2 = fromwire_accept_channel(ctx, msg, &len);
ac2 = fromwire_struct_accept_channel(ctx, msg, &len);
assert(len == 0);
assert(accept_channel_eq(&ac, ac2));
test_corruption(&ac, ac2, accept_channel);
memset(&uah, 2, sizeof(uah));
msg = towire_update_add_htlc(ctx, &uah);
msg = towire_struct_update_add_htlc(ctx, &uah);
len = tal_count(msg);
uah2 = fromwire_update_add_htlc(ctx, msg, &len);
uah2 = fromwire_struct_update_add_htlc(ctx, msg, &len);
assert(len == 0);
assert(update_add_htlc_eq(&uah, uah2));
test_corruption(&uah, uah2, update_add_htlc);
@ -420,9 +1026,9 @@ int main(void)
memset(&na, 2, sizeof(na));
set_pubkey(&na.node_id);
msg = towire_node_announcement(ctx, &na);
msg = towire_struct_node_announcement(ctx, &na);
len = tal_count(msg);
na2 = fromwire_node_announcement(ctx, msg, &len);
na2 = fromwire_struct_node_announcement(ctx, msg, &len);
assert(len == 0);
assert(node_announcement_eq(&na, na2));
test_corruption(&na, na2, node_announcement);

2
wire/towire.c

@ -80,7 +80,7 @@ void towire_u8_array(u8 **pptr, const u8 *arr, size_t num)
towire(pptr, arr, num);
}
void towire_pad_array(u8 **pptr, const u8 *arr, size_t num)
void towire_pad(u8 **pptr, size_t num)
{
/* Simply insert zeros. */
size_t oldsize = tal_count(*pptr);

5
wire/wire.h

@ -28,9 +28,9 @@ void towire_u8(u8 **pptr, u8 v);
void towire_u16(u8 **pptr, u16 v);
void towire_u32(u8 **pptr, u32 v);
void towire_u64(u8 **pptr, u64 v);
void towire_pad(u8 **pptr, size_t num);
void towire_u8_array(u8 **pptr, const u8 *arr, size_t num);
void towire_pad_array(u8 **pptr, const u8 *arr, size_t num);
void towire_signature_array(u8 **pptr, const struct signature *arr, size_t num);
@ -46,11 +46,10 @@ void fromwire_channel_id(const u8 **cursor, size_t *max,
struct channel_id *channel_id);
void fromwire_sha256(const u8 **cursor, size_t *max, struct sha256 *sha256);
void fromwire_ipv6(const u8 **cursor, size_t *max, struct ipv6 *ipv6);
void fromwire_pad(const u8 **cursor, size_t *max, size_t num);
void fromwire_u8_array(const u8 **cursor, size_t *max,
u8 *arr, size_t num);
void fromwire_pad_array(const u8 **cursor, size_t *max,
u8 *arr, size_t num);
void fromwire_signature_array(const u8 **cursor, size_t *max,
struct signature *arr, size_t num);

Loading…
Cancel
Save