/* This file was generated by generate-wire.py */
/* Do not modify this file! Modify the .csv file it was generated from. */
/* Original template can be found at tools/gen/impl_template */

#include <wire/peer_wiregen.h>
#include <assert.h>
#include <ccan/array_size/array_size.h>
#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <stdio.h>

#ifndef SUPERVERBOSE
#define SUPERVERBOSE(...)
#endif


const char *peer_wire_name(int e)
{
	static char invalidbuf[sizeof("INVALID ") + STR_MAX_CHARS(e)];

	switch ((enum peer_wire)e) {
	case WIRE_INIT: return "WIRE_INIT";
	case WIRE_ERROR: return "WIRE_ERROR";
	case WIRE_PING: return "WIRE_PING";
	case WIRE_PONG: return "WIRE_PONG";
	case WIRE_OPEN_CHANNEL: return "WIRE_OPEN_CHANNEL";
	case WIRE_ACCEPT_CHANNEL: return "WIRE_ACCEPT_CHANNEL";
	case WIRE_FUNDING_CREATED: return "WIRE_FUNDING_CREATED";
	case WIRE_FUNDING_SIGNED: return "WIRE_FUNDING_SIGNED";
	case WIRE_FUNDING_LOCKED: return "WIRE_FUNDING_LOCKED";
	case WIRE_SHUTDOWN: return "WIRE_SHUTDOWN";
	case WIRE_CLOSING_SIGNED: return "WIRE_CLOSING_SIGNED";
	case WIRE_UPDATE_ADD_HTLC: return "WIRE_UPDATE_ADD_HTLC";
	case WIRE_UPDATE_FULFILL_HTLC: return "WIRE_UPDATE_FULFILL_HTLC";
	case WIRE_UPDATE_FAIL_HTLC: return "WIRE_UPDATE_FAIL_HTLC";
	case WIRE_UPDATE_FAIL_MALFORMED_HTLC: return "WIRE_UPDATE_FAIL_MALFORMED_HTLC";
	case WIRE_COMMITMENT_SIGNED: return "WIRE_COMMITMENT_SIGNED";
	case WIRE_REVOKE_AND_ACK: return "WIRE_REVOKE_AND_ACK";
	case WIRE_UPDATE_FEE: return "WIRE_UPDATE_FEE";
	case WIRE_CHANNEL_REESTABLISH: return "WIRE_CHANNEL_REESTABLISH";
	case WIRE_ANNOUNCEMENT_SIGNATURES: return "WIRE_ANNOUNCEMENT_SIGNATURES";
	case WIRE_CHANNEL_ANNOUNCEMENT: return "WIRE_CHANNEL_ANNOUNCEMENT";
	case WIRE_NODE_ANNOUNCEMENT: return "WIRE_NODE_ANNOUNCEMENT";
	case WIRE_CHANNEL_UPDATE: return "WIRE_CHANNEL_UPDATE";
	case WIRE_QUERY_SHORT_CHANNEL_IDS: return "WIRE_QUERY_SHORT_CHANNEL_IDS";
	case WIRE_REPLY_SHORT_CHANNEL_IDS_END: return "WIRE_REPLY_SHORT_CHANNEL_IDS_END";
	case WIRE_QUERY_CHANNEL_RANGE: return "WIRE_QUERY_CHANNEL_RANGE";
	case WIRE_REPLY_CHANNEL_RANGE: return "WIRE_REPLY_CHANNEL_RANGE";
	case WIRE_GOSSIP_TIMESTAMP_FILTER: return "WIRE_GOSSIP_TIMESTAMP_FILTER";
	}

	snprintf(invalidbuf, sizeof(invalidbuf), "INVALID %i", e);
	return invalidbuf;
}

bool peer_wire_is_defined(u16 type)
{
	switch ((enum peer_wire)type) {
	case WIRE_INIT:;
	case WIRE_ERROR:;
	case WIRE_PING:;
	case WIRE_PONG:;
	case WIRE_OPEN_CHANNEL:;
	case WIRE_ACCEPT_CHANNEL:;
	case WIRE_FUNDING_CREATED:;
	case WIRE_FUNDING_SIGNED:;
	case WIRE_FUNDING_LOCKED:;
	case WIRE_SHUTDOWN:;
	case WIRE_CLOSING_SIGNED:;
	case WIRE_UPDATE_ADD_HTLC:;
	case WIRE_UPDATE_FULFILL_HTLC:;
	case WIRE_UPDATE_FAIL_HTLC:;
	case WIRE_UPDATE_FAIL_MALFORMED_HTLC:;
	case WIRE_COMMITMENT_SIGNED:;
	case WIRE_REVOKE_AND_ACK:;
	case WIRE_UPDATE_FEE:;
	case WIRE_CHANNEL_REESTABLISH:;
	case WIRE_ANNOUNCEMENT_SIGNATURES:;
	case WIRE_CHANNEL_ANNOUNCEMENT:;
	case WIRE_NODE_ANNOUNCEMENT:;
	case WIRE_CHANNEL_UPDATE:;
	case WIRE_QUERY_SHORT_CHANNEL_IDS:;
	case WIRE_REPLY_SHORT_CHANNEL_IDS_END:;
	case WIRE_QUERY_CHANNEL_RANGE:;
	case WIRE_REPLY_CHANNEL_RANGE:;
	case WIRE_GOSSIP_TIMESTAMP_FILTER:;
	      return true;
	}
	return false;
}




/* SUBTYPE: CHANNEL_UPDATE_CHECKSUMS */
void towire_channel_update_checksums(u8 **p, const struct channel_update_checksums *channel_update_checksums)
{

	towire_u32(p, channel_update_checksums->checksum_node_id_1);
	towire_u32(p, channel_update_checksums->checksum_node_id_2);
}
void fromwire_channel_update_checksums(const u8 **cursor, size_t *plen, struct channel_update_checksums *channel_update_checksums)
{

 	channel_update_checksums->checksum_node_id_1 = fromwire_u32(cursor, plen);
 	channel_update_checksums->checksum_node_id_2 = fromwire_u32(cursor, plen);
}

/* SUBTYPE: CHANNEL_UPDATE_TIMESTAMPS */
void towire_channel_update_timestamps(u8 **p, const struct channel_update_timestamps *channel_update_timestamps)
{

	towire_u32(p, channel_update_timestamps->timestamp_node_id_1);
	towire_u32(p, channel_update_timestamps->timestamp_node_id_2);
}
void fromwire_channel_update_timestamps(const u8 **cursor, size_t *plen, struct channel_update_timestamps *channel_update_timestamps)
{

 	channel_update_timestamps->timestamp_node_id_1 = fromwire_u32(cursor, plen);
 	channel_update_timestamps->timestamp_node_id_2 = fromwire_u32(cursor, plen);
}


struct tlv_init_tlvs *tlv_init_tlvs_new(const tal_t *ctx)
{
	/* Initialize everything to NULL. (Quiet, C pedants!) */
	struct tlv_init_tlvs *inst = talz(ctx, struct tlv_init_tlvs);

	/* Initialized the fields to an empty array. */
	inst->fields = tal_arr(inst, struct tlv_field, 0);
	return inst;
}

/* INIT_TLVS MSG: networks */
static u8 *towire_tlv_init_tlvs_networks(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_init_tlvs *r = vrecord;
	u8 *ptr;

	if (!r->networks)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

		for (size_t i = 0; i < tal_count(r->networks); i++)
		towire_bitcoin_blkid(&ptr, r->networks + i);
	return ptr;
}
static void fromwire_tlv_init_tlvs_networks(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_init_tlvs *r = vrecord;

	r->networks = *plen ? tal_arr(r, struct bitcoin_blkid, 0) : NULL;
	for (size_t i = 0; *plen != 0; i++) {
		struct bitcoin_blkid tmp;
		fromwire_bitcoin_blkid(cursor, plen, &tmp);
		tal_arr_expand(&r->networks, tmp);
	}
}

static const struct tlv_record_type tlvs_init_tlvs[] = {
	{ 1, towire_tlv_init_tlvs_networks, fromwire_tlv_init_tlvs_networks },
};

void towire_init_tlvs(u8 **pptr,
			const void *record)
{
	size_t num_types = 1;
	const struct tlv_record_type *types = tlvs_init_tlvs;
	if (!record)
		return;

	for (size_t i = 0; i < num_types; i++) {
		u8 *val;
		if (i != 0)
			assert(types[i].type > types[i-1].type);
		val = types[i].towire(NULL, record);
		if (!val)
			continue;

		/* BOLT #1:
		 *
		 * The sending node:
		 ...
		 *  - MUST minimally encode `type` and `length`.
		 */
		towire_bigsize(pptr, types[i].type);
		towire_bigsize(pptr, tal_bytelen(val));
		towire(pptr, val, tal_bytelen(val));
		tal_free(val);
	}
}


bool fromwire_init_tlvs(const u8 **cursor, size_t *max, struct tlv_init_tlvs *record)
{
	size_t num_types = 1;
	const struct tlv_record_type *types = tlvs_init_tlvs;
	while (*max > 0) {
		struct tlv_field field;

		/* BOLT #1:
		 *
		 * The `type` is encoded using the BigSize format.
		 */
		field.numtype = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("type");
			goto fail;
		}
		field.length = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("length");
			goto fail;
		}

		/* BOLT #1:
		 *  - if `length` exceeds the number of bytes remaining in the
		 *    message:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (field.length > *max) {
			SUPERVERBOSE("value");
			goto fail;
		}
		field.value = tal_dup_arr(record, u8, *cursor, field.length, 0);

		/* BOLT #1:
		 * - if `type` is known:
		 *   - MUST decode the next `length` bytes using the known
		 *     encoding for `type`.
		 */
		field.meta = NULL;
		for (size_t i = 0; i < num_types; i++) {
			if (types[i].type == field.numtype)
				field.meta = &types[i];
		}

		if (field.meta) {
			/* Length of message can't exceed 16 bits anyway. */
			size_t tlvlen = field.length;
			field.meta->fromwire(cursor, &tlvlen, record);

			if (!*cursor)
				goto fail;

			/* BOLT #1:
			 *  - if `length` is not exactly equal to that required
			 *    for the known encoding for `type`:
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (tlvlen != 0) {
				SUPERVERBOSE("greater than encoding length");
				goto fail;
			}
		} else {
			/* We didn't read from *cursor through a fromwire, so
			 * update manually. */
			*cursor += field.length;
		}
		/* We've read bytes in ->fromwire, so update max */
		*max -= field.length;
		tal_arr_expand(&record->fields, field);
	}
	return true;
fail:
	fromwire_fail(cursor, max);
	return false;
}

bool init_tlvs_is_valid(const struct tlv_init_tlvs *record, size_t *err_index)
{
	size_t numfields = tal_count(record->fields);
	bool first = true;
	u64 prev_type = 0;
	for (int i=0; i<numfields; i++) {
		struct tlv_field *f = &record->fields[i];
		if (f->numtype % 2 == 0 && f->meta == NULL) {
			/* BOLT #1:
			 * - otherwise, if `type` is unknown:
			 *   - if `type` is even:
			 *     - MUST fail to parse the `tlv_stream`.
			 *   - otherwise, if `type` is odd:
			 *     - MUST discard the next `length` bytes.
			 */
			SUPERVERBOSE("unknown even");
			if (err_index != NULL)
				*err_index = i;
			return false;
		} else if (!first && f->numtype <= prev_type) {
			/* BOLT #1:
			 *  - if decoded `type`s are not strictly-increasing
			 *    (including situations when two or more occurrences
			 *    of the same `type` are met):
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (f->numtype == prev_type)
				SUPERVERBOSE("duplicate tlv type");
			else
				SUPERVERBOSE("invalid ordering");
			if (err_index != NULL)
				*err_index = i;
			return false;
		}
		first = false;
		prev_type = f->numtype;
	}
	return true;
}


struct tlv_n1 *tlv_n1_new(const tal_t *ctx)
{
	/* Initialize everything to NULL. (Quiet, C pedants!) */
	struct tlv_n1 *inst = talz(ctx, struct tlv_n1);

	/* Initialized the fields to an empty array. */
	inst->fields = tal_arr(inst, struct tlv_field, 0);
	return inst;
}

/* N1 MSG: tlv1 */
static u8 *towire_tlv_n1_tlv1(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_n1 *r = vrecord;
	u8 *ptr;

	if (!r->tlv1)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

	towire_tu64(&ptr, *r->tlv1);
	return ptr;
}
static void fromwire_tlv_n1_tlv1(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_n1 *r = vrecord;

	    r->tlv1 = tal(r, u64);

*r->tlv1 = fromwire_tu64(cursor, plen);
}
/* N1 MSG: tlv2 */
static u8 *towire_tlv_n1_tlv2(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_n1 *r = vrecord;
	u8 *ptr;

	if (!r->tlv2)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

	towire_short_channel_id(&ptr, r->tlv2);
	return ptr;
}
static void fromwire_tlv_n1_tlv2(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_n1 *r = vrecord;

	    r->tlv2 = tal(r, struct short_channel_id);

fromwire_short_channel_id(cursor, plen, &*r->tlv2);
}
/* N1 MSG: tlv3 */
static u8 *towire_tlv_n1_tlv3(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_n1 *r = vrecord;
	u8 *ptr;

	if (!r->tlv3)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

	towire_pubkey(&ptr, &r->tlv3->node_id);

	towire_amount_msat(&ptr, r->tlv3->amount_msat_1);

	towire_amount_msat(&ptr, r->tlv3->amount_msat_2);
	return ptr;
}
static void fromwire_tlv_n1_tlv3(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_n1 *r = vrecord;

	r->tlv3 = tal(r, struct tlv_n1_tlv3);
	fromwire_pubkey(cursor, plen, &r->tlv3->node_id);
	r->tlv3->amount_msat_1 = fromwire_amount_msat(cursor, plen);
	r->tlv3->amount_msat_2 = fromwire_amount_msat(cursor, plen);
}
/* N1 MSG: tlv4 */
static u8 *towire_tlv_n1_tlv4(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_n1 *r = vrecord;
	u8 *ptr;

	if (!r->tlv4)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

	towire_u16(&ptr, *r->tlv4);
	return ptr;
}
static void fromwire_tlv_n1_tlv4(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_n1 *r = vrecord;

	    r->tlv4 = tal(r, u16);

*r->tlv4 = fromwire_u16(cursor, plen);
}

const struct tlv_record_type tlvs_n1[] = {
	{ 1, towire_tlv_n1_tlv1, fromwire_tlv_n1_tlv1 },
	{ 2, towire_tlv_n1_tlv2, fromwire_tlv_n1_tlv2 },
	{ 3, towire_tlv_n1_tlv3, fromwire_tlv_n1_tlv3 },
	{ 254, towire_tlv_n1_tlv4, fromwire_tlv_n1_tlv4 },
};

void towire_n1(u8 **pptr,
			const void *record)
{
	size_t num_types = 4;
	const struct tlv_record_type *types = tlvs_n1;
	if (!record)
		return;

	for (size_t i = 0; i < num_types; i++) {
		u8 *val;
		if (i != 0)
			assert(types[i].type > types[i-1].type);
		val = types[i].towire(NULL, record);
		if (!val)
			continue;

		/* BOLT #1:
		 *
		 * The sending node:
		 ...
		 *  - MUST minimally encode `type` and `length`.
		 */
		towire_bigsize(pptr, types[i].type);
		towire_bigsize(pptr, tal_bytelen(val));
		towire(pptr, val, tal_bytelen(val));
		tal_free(val);
	}
}


bool fromwire_n1(const u8 **cursor, size_t *max, struct tlv_n1 *record)
{
	size_t num_types = 4;
	const struct tlv_record_type *types = tlvs_n1;
	while (*max > 0) {
		struct tlv_field field;

		/* BOLT #1:
		 *
		 * The `type` is encoded using the BigSize format.
		 */
		field.numtype = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("type");
			goto fail;
		}
		field.length = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("length");
			goto fail;
		}

		/* BOLT #1:
		 *  - if `length` exceeds the number of bytes remaining in the
		 *    message:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (field.length > *max) {
			SUPERVERBOSE("value");
			goto fail;
		}
		field.value = tal_dup_arr(record, u8, *cursor, field.length, 0);

		/* BOLT #1:
		 * - if `type` is known:
		 *   - MUST decode the next `length` bytes using the known
		 *     encoding for `type`.
		 */
		field.meta = NULL;
		for (size_t i = 0; i < num_types; i++) {
			if (types[i].type == field.numtype)
				field.meta = &types[i];
		}

		if (field.meta) {
			/* Length of message can't exceed 16 bits anyway. */
			size_t tlvlen = field.length;
			field.meta->fromwire(cursor, &tlvlen, record);

			if (!*cursor)
				goto fail;

			/* BOLT #1:
			 *  - if `length` is not exactly equal to that required
			 *    for the known encoding for `type`:
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (tlvlen != 0) {
				SUPERVERBOSE("greater than encoding length");
				goto fail;
			}
		} else {
			/* We didn't read from *cursor through a fromwire, so
			 * update manually. */
			*cursor += field.length;
		}
		/* We've read bytes in ->fromwire, so update max */
		*max -= field.length;
		tal_arr_expand(&record->fields, field);
	}
	return true;
fail:
	fromwire_fail(cursor, max);
	return false;
}

bool n1_is_valid(const struct tlv_n1 *record, size_t *err_index)
{
	size_t numfields = tal_count(record->fields);
	bool first = true;
	u64 prev_type = 0;
	for (int i=0; i<numfields; i++) {
		struct tlv_field *f = &record->fields[i];
		if (f->numtype % 2 == 0 && f->meta == NULL) {
			/* BOLT #1:
			 * - otherwise, if `type` is unknown:
			 *   - if `type` is even:
			 *     - MUST fail to parse the `tlv_stream`.
			 *   - otherwise, if `type` is odd:
			 *     - MUST discard the next `length` bytes.
			 */
			SUPERVERBOSE("unknown even");
			if (err_index != NULL)
				*err_index = i;
			return false;
		} else if (!first && f->numtype <= prev_type) {
			/* BOLT #1:
			 *  - if decoded `type`s are not strictly-increasing
			 *    (including situations when two or more occurrences
			 *    of the same `type` are met):
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (f->numtype == prev_type)
				SUPERVERBOSE("duplicate tlv type");
			else
				SUPERVERBOSE("invalid ordering");
			if (err_index != NULL)
				*err_index = i;
			return false;
		}
		first = false;
		prev_type = f->numtype;
	}
	return true;
}


struct tlv_n2 *tlv_n2_new(const tal_t *ctx)
{
	/* Initialize everything to NULL. (Quiet, C pedants!) */
	struct tlv_n2 *inst = talz(ctx, struct tlv_n2);

	/* Initialized the fields to an empty array. */
	inst->fields = tal_arr(inst, struct tlv_field, 0);
	return inst;
}

/* N2 MSG: tlv1 */
static u8 *towire_tlv_n2_tlv1(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_n2 *r = vrecord;
	u8 *ptr;

	if (!r->tlv1)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

	towire_tu64(&ptr, *r->tlv1);
	return ptr;
}
static void fromwire_tlv_n2_tlv1(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_n2 *r = vrecord;

	    r->tlv1 = tal(r, u64);

*r->tlv1 = fromwire_tu64(cursor, plen);
}
/* N2 MSG: tlv2 */
static u8 *towire_tlv_n2_tlv2(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_n2 *r = vrecord;
	u8 *ptr;

	if (!r->tlv2)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

	towire_tu32(&ptr, *r->tlv2);
	return ptr;
}
static void fromwire_tlv_n2_tlv2(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_n2 *r = vrecord;

	    r->tlv2 = tal(r, u32);

*r->tlv2 = fromwire_tu32(cursor, plen);
}

const struct tlv_record_type tlvs_n2[] = {
	{ 0, towire_tlv_n2_tlv1, fromwire_tlv_n2_tlv1 },
	{ 11, towire_tlv_n2_tlv2, fromwire_tlv_n2_tlv2 },
};

void towire_n2(u8 **pptr,
			const void *record)
{
	size_t num_types = 2;
	const struct tlv_record_type *types = tlvs_n2;
	if (!record)
		return;

	for (size_t i = 0; i < num_types; i++) {
		u8 *val;
		if (i != 0)
			assert(types[i].type > types[i-1].type);
		val = types[i].towire(NULL, record);
		if (!val)
			continue;

		/* BOLT #1:
		 *
		 * The sending node:
		 ...
		 *  - MUST minimally encode `type` and `length`.
		 */
		towire_bigsize(pptr, types[i].type);
		towire_bigsize(pptr, tal_bytelen(val));
		towire(pptr, val, tal_bytelen(val));
		tal_free(val);
	}
}


bool fromwire_n2(const u8 **cursor, size_t *max, struct tlv_n2 *record)
{
	size_t num_types = 2;
	const struct tlv_record_type *types = tlvs_n2;
	while (*max > 0) {
		struct tlv_field field;

		/* BOLT #1:
		 *
		 * The `type` is encoded using the BigSize format.
		 */
		field.numtype = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("type");
			goto fail;
		}
		field.length = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("length");
			goto fail;
		}

		/* BOLT #1:
		 *  - if `length` exceeds the number of bytes remaining in the
		 *    message:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (field.length > *max) {
			SUPERVERBOSE("value");
			goto fail;
		}
		field.value = tal_dup_arr(record, u8, *cursor, field.length, 0);

		/* BOLT #1:
		 * - if `type` is known:
		 *   - MUST decode the next `length` bytes using the known
		 *     encoding for `type`.
		 */
		field.meta = NULL;
		for (size_t i = 0; i < num_types; i++) {
			if (types[i].type == field.numtype)
				field.meta = &types[i];
		}

		if (field.meta) {
			/* Length of message can't exceed 16 bits anyway. */
			size_t tlvlen = field.length;
			field.meta->fromwire(cursor, &tlvlen, record);

			if (!*cursor)
				goto fail;

			/* BOLT #1:
			 *  - if `length` is not exactly equal to that required
			 *    for the known encoding for `type`:
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (tlvlen != 0) {
				SUPERVERBOSE("greater than encoding length");
				goto fail;
			}
		} else {
			/* We didn't read from *cursor through a fromwire, so
			 * update manually. */
			*cursor += field.length;
		}
		/* We've read bytes in ->fromwire, so update max */
		*max -= field.length;
		tal_arr_expand(&record->fields, field);
	}
	return true;
fail:
	fromwire_fail(cursor, max);
	return false;
}

bool n2_is_valid(const struct tlv_n2 *record, size_t *err_index)
{
	size_t numfields = tal_count(record->fields);
	bool first = true;
	u64 prev_type = 0;
	for (int i=0; i<numfields; i++) {
		struct tlv_field *f = &record->fields[i];
		if (f->numtype % 2 == 0 && f->meta == NULL) {
			/* BOLT #1:
			 * - otherwise, if `type` is unknown:
			 *   - if `type` is even:
			 *     - MUST fail to parse the `tlv_stream`.
			 *   - otherwise, if `type` is odd:
			 *     - MUST discard the next `length` bytes.
			 */
			SUPERVERBOSE("unknown even");
			if (err_index != NULL)
				*err_index = i;
			return false;
		} else if (!first && f->numtype <= prev_type) {
			/* BOLT #1:
			 *  - if decoded `type`s are not strictly-increasing
			 *    (including situations when two or more occurrences
			 *    of the same `type` are met):
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (f->numtype == prev_type)
				SUPERVERBOSE("duplicate tlv type");
			else
				SUPERVERBOSE("invalid ordering");
			if (err_index != NULL)
				*err_index = i;
			return false;
		}
		first = false;
		prev_type = f->numtype;
	}
	return true;
}


struct tlv_open_channel_tlvs *tlv_open_channel_tlvs_new(const tal_t *ctx)
{
	/* Initialize everything to NULL. (Quiet, C pedants!) */
	struct tlv_open_channel_tlvs *inst = talz(ctx, struct tlv_open_channel_tlvs);

	/* Initialized the fields to an empty array. */
	inst->fields = tal_arr(inst, struct tlv_field, 0);
	return inst;
}

/* OPEN_CHANNEL_TLVS MSG: upfront_shutdown_script */
static u8 *towire_tlv_open_channel_tlvs_upfront_shutdown_script(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_open_channel_tlvs *r = vrecord;
	u8 *ptr;

	if (!r->upfront_shutdown_script)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

	towire_u8_array(&ptr, r->upfront_shutdown_script, tal_count(r->upfront_shutdown_script));
	return ptr;
}
static void fromwire_tlv_open_channel_tlvs_upfront_shutdown_script(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_open_channel_tlvs *r = vrecord;

	r->upfront_shutdown_script = *plen ? tal_arr(r, u8, *plen) : NULL;
fromwire_u8_array(cursor, plen, r->upfront_shutdown_script, *plen);
}

static const struct tlv_record_type tlvs_open_channel_tlvs[] = {
	{ 0, towire_tlv_open_channel_tlvs_upfront_shutdown_script, fromwire_tlv_open_channel_tlvs_upfront_shutdown_script },
};

void towire_open_channel_tlvs(u8 **pptr,
			const void *record)
{
	size_t num_types = 1;
	const struct tlv_record_type *types = tlvs_open_channel_tlvs;
	if (!record)
		return;

	for (size_t i = 0; i < num_types; i++) {
		u8 *val;
		if (i != 0)
			assert(types[i].type > types[i-1].type);
		val = types[i].towire(NULL, record);
		if (!val)
			continue;

		/* BOLT #1:
		 *
		 * The sending node:
		 ...
		 *  - MUST minimally encode `type` and `length`.
		 */
		towire_bigsize(pptr, types[i].type);
		towire_bigsize(pptr, tal_bytelen(val));
		towire(pptr, val, tal_bytelen(val));
		tal_free(val);
	}
}


bool fromwire_open_channel_tlvs(const u8 **cursor, size_t *max, struct tlv_open_channel_tlvs *record)
{
	size_t num_types = 1;
	const struct tlv_record_type *types = tlvs_open_channel_tlvs;
	while (*max > 0) {
		struct tlv_field field;

		/* BOLT #1:
		 *
		 * The `type` is encoded using the BigSize format.
		 */
		field.numtype = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("type");
			goto fail;
		}
		field.length = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("length");
			goto fail;
		}

		/* BOLT #1:
		 *  - if `length` exceeds the number of bytes remaining in the
		 *    message:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (field.length > *max) {
			SUPERVERBOSE("value");
			goto fail;
		}
		field.value = tal_dup_arr(record, u8, *cursor, field.length, 0);

		/* BOLT #1:
		 * - if `type` is known:
		 *   - MUST decode the next `length` bytes using the known
		 *     encoding for `type`.
		 */
		field.meta = NULL;
		for (size_t i = 0; i < num_types; i++) {
			if (types[i].type == field.numtype)
				field.meta = &types[i];
		}

		if (field.meta) {
			/* Length of message can't exceed 16 bits anyway. */
			size_t tlvlen = field.length;
			field.meta->fromwire(cursor, &tlvlen, record);

			if (!*cursor)
				goto fail;

			/* BOLT #1:
			 *  - if `length` is not exactly equal to that required
			 *    for the known encoding for `type`:
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (tlvlen != 0) {
				SUPERVERBOSE("greater than encoding length");
				goto fail;
			}
		} else {
			/* We didn't read from *cursor through a fromwire, so
			 * update manually. */
			*cursor += field.length;
		}
		/* We've read bytes in ->fromwire, so update max */
		*max -= field.length;
		tal_arr_expand(&record->fields, field);
	}
	return true;
fail:
	fromwire_fail(cursor, max);
	return false;
}

bool open_channel_tlvs_is_valid(const struct tlv_open_channel_tlvs *record, size_t *err_index)
{
	size_t numfields = tal_count(record->fields);
	bool first = true;
	u64 prev_type = 0;
	for (int i=0; i<numfields; i++) {
		struct tlv_field *f = &record->fields[i];
		if (f->numtype % 2 == 0 && f->meta == NULL) {
			/* BOLT #1:
			 * - otherwise, if `type` is unknown:
			 *   - if `type` is even:
			 *     - MUST fail to parse the `tlv_stream`.
			 *   - otherwise, if `type` is odd:
			 *     - MUST discard the next `length` bytes.
			 */
			SUPERVERBOSE("unknown even");
			if (err_index != NULL)
				*err_index = i;
			return false;
		} else if (!first && f->numtype <= prev_type) {
			/* BOLT #1:
			 *  - if decoded `type`s are not strictly-increasing
			 *    (including situations when two or more occurrences
			 *    of the same `type` are met):
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (f->numtype == prev_type)
				SUPERVERBOSE("duplicate tlv type");
			else
				SUPERVERBOSE("invalid ordering");
			if (err_index != NULL)
				*err_index = i;
			return false;
		}
		first = false;
		prev_type = f->numtype;
	}
	return true;
}


struct tlv_accept_channel_tlvs *tlv_accept_channel_tlvs_new(const tal_t *ctx)
{
	/* Initialize everything to NULL. (Quiet, C pedants!) */
	struct tlv_accept_channel_tlvs *inst = talz(ctx, struct tlv_accept_channel_tlvs);

	/* Initialized the fields to an empty array. */
	inst->fields = tal_arr(inst, struct tlv_field, 0);
	return inst;
}

/* ACCEPT_CHANNEL_TLVS MSG: upfront_shutdown_script */
static u8 *towire_tlv_accept_channel_tlvs_upfront_shutdown_script(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_accept_channel_tlvs *r = vrecord;
	u8 *ptr;

	if (!r->upfront_shutdown_script)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

	towire_u8_array(&ptr, r->upfront_shutdown_script, tal_count(r->upfront_shutdown_script));
	return ptr;
}
static void fromwire_tlv_accept_channel_tlvs_upfront_shutdown_script(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_accept_channel_tlvs *r = vrecord;

	r->upfront_shutdown_script = *plen ? tal_arr(r, u8, *plen) : NULL;
fromwire_u8_array(cursor, plen, r->upfront_shutdown_script, *plen);
}

static const struct tlv_record_type tlvs_accept_channel_tlvs[] = {
	{ 0, towire_tlv_accept_channel_tlvs_upfront_shutdown_script, fromwire_tlv_accept_channel_tlvs_upfront_shutdown_script },
};

void towire_accept_channel_tlvs(u8 **pptr,
			const void *record)
{
	size_t num_types = 1;
	const struct tlv_record_type *types = tlvs_accept_channel_tlvs;
	if (!record)
		return;

	for (size_t i = 0; i < num_types; i++) {
		u8 *val;
		if (i != 0)
			assert(types[i].type > types[i-1].type);
		val = types[i].towire(NULL, record);
		if (!val)
			continue;

		/* BOLT #1:
		 *
		 * The sending node:
		 ...
		 *  - MUST minimally encode `type` and `length`.
		 */
		towire_bigsize(pptr, types[i].type);
		towire_bigsize(pptr, tal_bytelen(val));
		towire(pptr, val, tal_bytelen(val));
		tal_free(val);
	}
}


bool fromwire_accept_channel_tlvs(const u8 **cursor, size_t *max, struct tlv_accept_channel_tlvs *record)
{
	size_t num_types = 1;
	const struct tlv_record_type *types = tlvs_accept_channel_tlvs;
	while (*max > 0) {
		struct tlv_field field;

		/* BOLT #1:
		 *
		 * The `type` is encoded using the BigSize format.
		 */
		field.numtype = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("type");
			goto fail;
		}
		field.length = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("length");
			goto fail;
		}

		/* BOLT #1:
		 *  - if `length` exceeds the number of bytes remaining in the
		 *    message:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (field.length > *max) {
			SUPERVERBOSE("value");
			goto fail;
		}
		field.value = tal_dup_arr(record, u8, *cursor, field.length, 0);

		/* BOLT #1:
		 * - if `type` is known:
		 *   - MUST decode the next `length` bytes using the known
		 *     encoding for `type`.
		 */
		field.meta = NULL;
		for (size_t i = 0; i < num_types; i++) {
			if (types[i].type == field.numtype)
				field.meta = &types[i];
		}

		if (field.meta) {
			/* Length of message can't exceed 16 bits anyway. */
			size_t tlvlen = field.length;
			field.meta->fromwire(cursor, &tlvlen, record);

			if (!*cursor)
				goto fail;

			/* BOLT #1:
			 *  - if `length` is not exactly equal to that required
			 *    for the known encoding for `type`:
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (tlvlen != 0) {
				SUPERVERBOSE("greater than encoding length");
				goto fail;
			}
		} else {
			/* We didn't read from *cursor through a fromwire, so
			 * update manually. */
			*cursor += field.length;
		}
		/* We've read bytes in ->fromwire, so update max */
		*max -= field.length;
		tal_arr_expand(&record->fields, field);
	}
	return true;
fail:
	fromwire_fail(cursor, max);
	return false;
}

bool accept_channel_tlvs_is_valid(const struct tlv_accept_channel_tlvs *record, size_t *err_index)
{
	size_t numfields = tal_count(record->fields);
	bool first = true;
	u64 prev_type = 0;
	for (int i=0; i<numfields; i++) {
		struct tlv_field *f = &record->fields[i];
		if (f->numtype % 2 == 0 && f->meta == NULL) {
			/* BOLT #1:
			 * - otherwise, if `type` is unknown:
			 *   - if `type` is even:
			 *     - MUST fail to parse the `tlv_stream`.
			 *   - otherwise, if `type` is odd:
			 *     - MUST discard the next `length` bytes.
			 */
			SUPERVERBOSE("unknown even");
			if (err_index != NULL)
				*err_index = i;
			return false;
		} else if (!first && f->numtype <= prev_type) {
			/* BOLT #1:
			 *  - if decoded `type`s are not strictly-increasing
			 *    (including situations when two or more occurrences
			 *    of the same `type` are met):
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (f->numtype == prev_type)
				SUPERVERBOSE("duplicate tlv type");
			else
				SUPERVERBOSE("invalid ordering");
			if (err_index != NULL)
				*err_index = i;
			return false;
		}
		first = false;
		prev_type = f->numtype;
	}
	return true;
}


struct tlv_query_short_channel_ids_tlvs *tlv_query_short_channel_ids_tlvs_new(const tal_t *ctx)
{
	/* Initialize everything to NULL. (Quiet, C pedants!) */
	struct tlv_query_short_channel_ids_tlvs *inst = talz(ctx, struct tlv_query_short_channel_ids_tlvs);

	/* Initialized the fields to an empty array. */
	inst->fields = tal_arr(inst, struct tlv_field, 0);
	return inst;
}

/* QUERY_SHORT_CHANNEL_IDS_TLVS MSG: query_flags */
static u8 *towire_tlv_query_short_channel_ids_tlvs_query_flags(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_query_short_channel_ids_tlvs *r = vrecord;
	u8 *ptr;

	if (!r->query_flags)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

	towire_u8(&ptr, r->query_flags->encoding_type);

	towire_u8_array(&ptr, r->query_flags->encoded_query_flags, tal_count(r->query_flags->encoded_query_flags));
	return ptr;
}
static void fromwire_tlv_query_short_channel_ids_tlvs_query_flags(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_query_short_channel_ids_tlvs *r = vrecord;

	r->query_flags = tal(r, struct tlv_query_short_channel_ids_tlvs_query_flags);
	r->query_flags->encoding_type = fromwire_u8(cursor, plen);
	r->query_flags->encoded_query_flags = *plen ? tal_arr(r->query_flags, u8, *plen) : NULL;
fromwire_u8_array(cursor, plen, r->query_flags->encoded_query_flags, *plen);
}

static const struct tlv_record_type tlvs_query_short_channel_ids_tlvs[] = {
	{ 1, towire_tlv_query_short_channel_ids_tlvs_query_flags, fromwire_tlv_query_short_channel_ids_tlvs_query_flags },
};

void towire_query_short_channel_ids_tlvs(u8 **pptr,
			const void *record)
{
	size_t num_types = 1;
	const struct tlv_record_type *types = tlvs_query_short_channel_ids_tlvs;
	if (!record)
		return;

	for (size_t i = 0; i < num_types; i++) {
		u8 *val;
		if (i != 0)
			assert(types[i].type > types[i-1].type);
		val = types[i].towire(NULL, record);
		if (!val)
			continue;

		/* BOLT #1:
		 *
		 * The sending node:
		 ...
		 *  - MUST minimally encode `type` and `length`.
		 */
		towire_bigsize(pptr, types[i].type);
		towire_bigsize(pptr, tal_bytelen(val));
		towire(pptr, val, tal_bytelen(val));
		tal_free(val);
	}
}


bool fromwire_query_short_channel_ids_tlvs(const u8 **cursor, size_t *max, struct tlv_query_short_channel_ids_tlvs *record)
{
	size_t num_types = 1;
	const struct tlv_record_type *types = tlvs_query_short_channel_ids_tlvs;
	while (*max > 0) {
		struct tlv_field field;

		/* BOLT #1:
		 *
		 * The `type` is encoded using the BigSize format.
		 */
		field.numtype = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("type");
			goto fail;
		}
		field.length = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("length");
			goto fail;
		}

		/* BOLT #1:
		 *  - if `length` exceeds the number of bytes remaining in the
		 *    message:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (field.length > *max) {
			SUPERVERBOSE("value");
			goto fail;
		}
		field.value = tal_dup_arr(record, u8, *cursor, field.length, 0);

		/* BOLT #1:
		 * - if `type` is known:
		 *   - MUST decode the next `length` bytes using the known
		 *     encoding for `type`.
		 */
		field.meta = NULL;
		for (size_t i = 0; i < num_types; i++) {
			if (types[i].type == field.numtype)
				field.meta = &types[i];
		}

		if (field.meta) {
			/* Length of message can't exceed 16 bits anyway. */
			size_t tlvlen = field.length;
			field.meta->fromwire(cursor, &tlvlen, record);

			if (!*cursor)
				goto fail;

			/* BOLT #1:
			 *  - if `length` is not exactly equal to that required
			 *    for the known encoding for `type`:
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (tlvlen != 0) {
				SUPERVERBOSE("greater than encoding length");
				goto fail;
			}
		} else {
			/* We didn't read from *cursor through a fromwire, so
			 * update manually. */
			*cursor += field.length;
		}
		/* We've read bytes in ->fromwire, so update max */
		*max -= field.length;
		tal_arr_expand(&record->fields, field);
	}
	return true;
fail:
	fromwire_fail(cursor, max);
	return false;
}

bool query_short_channel_ids_tlvs_is_valid(const struct tlv_query_short_channel_ids_tlvs *record, size_t *err_index)
{
	size_t numfields = tal_count(record->fields);
	bool first = true;
	u64 prev_type = 0;
	for (int i=0; i<numfields; i++) {
		struct tlv_field *f = &record->fields[i];
		if (f->numtype % 2 == 0 && f->meta == NULL) {
			/* BOLT #1:
			 * - otherwise, if `type` is unknown:
			 *   - if `type` is even:
			 *     - MUST fail to parse the `tlv_stream`.
			 *   - otherwise, if `type` is odd:
			 *     - MUST discard the next `length` bytes.
			 */
			SUPERVERBOSE("unknown even");
			if (err_index != NULL)
				*err_index = i;
			return false;
		} else if (!first && f->numtype <= prev_type) {
			/* BOLT #1:
			 *  - if decoded `type`s are not strictly-increasing
			 *    (including situations when two or more occurrences
			 *    of the same `type` are met):
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (f->numtype == prev_type)
				SUPERVERBOSE("duplicate tlv type");
			else
				SUPERVERBOSE("invalid ordering");
			if (err_index != NULL)
				*err_index = i;
			return false;
		}
		first = false;
		prev_type = f->numtype;
	}
	return true;
}


struct tlv_query_channel_range_tlvs *tlv_query_channel_range_tlvs_new(const tal_t *ctx)
{
	/* Initialize everything to NULL. (Quiet, C pedants!) */
	struct tlv_query_channel_range_tlvs *inst = talz(ctx, struct tlv_query_channel_range_tlvs);

	/* Initialized the fields to an empty array. */
	inst->fields = tal_arr(inst, struct tlv_field, 0);
	return inst;
}

/* QUERY_CHANNEL_RANGE_TLVS MSG: query_option */
static u8 *towire_tlv_query_channel_range_tlvs_query_option(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_query_channel_range_tlvs *r = vrecord;
	u8 *ptr;

	if (!r->query_option)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

	towire_bigsize(&ptr, *r->query_option);
	return ptr;
}
static void fromwire_tlv_query_channel_range_tlvs_query_option(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_query_channel_range_tlvs *r = vrecord;

	    r->query_option = tal(r, bigsize);

*r->query_option = fromwire_bigsize(cursor, plen);
}

static const struct tlv_record_type tlvs_query_channel_range_tlvs[] = {
	{ 1, towire_tlv_query_channel_range_tlvs_query_option, fromwire_tlv_query_channel_range_tlvs_query_option },
};

void towire_query_channel_range_tlvs(u8 **pptr,
			const void *record)
{
	size_t num_types = 1;
	const struct tlv_record_type *types = tlvs_query_channel_range_tlvs;
	if (!record)
		return;

	for (size_t i = 0; i < num_types; i++) {
		u8 *val;
		if (i != 0)
			assert(types[i].type > types[i-1].type);
		val = types[i].towire(NULL, record);
		if (!val)
			continue;

		/* BOLT #1:
		 *
		 * The sending node:
		 ...
		 *  - MUST minimally encode `type` and `length`.
		 */
		towire_bigsize(pptr, types[i].type);
		towire_bigsize(pptr, tal_bytelen(val));
		towire(pptr, val, tal_bytelen(val));
		tal_free(val);
	}
}


bool fromwire_query_channel_range_tlvs(const u8 **cursor, size_t *max, struct tlv_query_channel_range_tlvs *record)
{
	size_t num_types = 1;
	const struct tlv_record_type *types = tlvs_query_channel_range_tlvs;
	while (*max > 0) {
		struct tlv_field field;

		/* BOLT #1:
		 *
		 * The `type` is encoded using the BigSize format.
		 */
		field.numtype = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("type");
			goto fail;
		}
		field.length = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("length");
			goto fail;
		}

		/* BOLT #1:
		 *  - if `length` exceeds the number of bytes remaining in the
		 *    message:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (field.length > *max) {
			SUPERVERBOSE("value");
			goto fail;
		}
		field.value = tal_dup_arr(record, u8, *cursor, field.length, 0);

		/* BOLT #1:
		 * - if `type` is known:
		 *   - MUST decode the next `length` bytes using the known
		 *     encoding for `type`.
		 */
		field.meta = NULL;
		for (size_t i = 0; i < num_types; i++) {
			if (types[i].type == field.numtype)
				field.meta = &types[i];
		}

		if (field.meta) {
			/* Length of message can't exceed 16 bits anyway. */
			size_t tlvlen = field.length;
			field.meta->fromwire(cursor, &tlvlen, record);

			if (!*cursor)
				goto fail;

			/* BOLT #1:
			 *  - if `length` is not exactly equal to that required
			 *    for the known encoding for `type`:
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (tlvlen != 0) {
				SUPERVERBOSE("greater than encoding length");
				goto fail;
			}
		} else {
			/* We didn't read from *cursor through a fromwire, so
			 * update manually. */
			*cursor += field.length;
		}
		/* We've read bytes in ->fromwire, so update max */
		*max -= field.length;
		tal_arr_expand(&record->fields, field);
	}
	return true;
fail:
	fromwire_fail(cursor, max);
	return false;
}

bool query_channel_range_tlvs_is_valid(const struct tlv_query_channel_range_tlvs *record, size_t *err_index)
{
	size_t numfields = tal_count(record->fields);
	bool first = true;
	u64 prev_type = 0;
	for (int i=0; i<numfields; i++) {
		struct tlv_field *f = &record->fields[i];
		if (f->numtype % 2 == 0 && f->meta == NULL) {
			/* BOLT #1:
			 * - otherwise, if `type` is unknown:
			 *   - if `type` is even:
			 *     - MUST fail to parse the `tlv_stream`.
			 *   - otherwise, if `type` is odd:
			 *     - MUST discard the next `length` bytes.
			 */
			SUPERVERBOSE("unknown even");
			if (err_index != NULL)
				*err_index = i;
			return false;
		} else if (!first && f->numtype <= prev_type) {
			/* BOLT #1:
			 *  - if decoded `type`s are not strictly-increasing
			 *    (including situations when two or more occurrences
			 *    of the same `type` are met):
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (f->numtype == prev_type)
				SUPERVERBOSE("duplicate tlv type");
			else
				SUPERVERBOSE("invalid ordering");
			if (err_index != NULL)
				*err_index = i;
			return false;
		}
		first = false;
		prev_type = f->numtype;
	}
	return true;
}


struct tlv_reply_channel_range_tlvs *tlv_reply_channel_range_tlvs_new(const tal_t *ctx)
{
	/* Initialize everything to NULL. (Quiet, C pedants!) */
	struct tlv_reply_channel_range_tlvs *inst = talz(ctx, struct tlv_reply_channel_range_tlvs);

	/* Initialized the fields to an empty array. */
	inst->fields = tal_arr(inst, struct tlv_field, 0);
	return inst;
}

/* REPLY_CHANNEL_RANGE_TLVS MSG: timestamps_tlv */
static u8 *towire_tlv_reply_channel_range_tlvs_timestamps_tlv(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_reply_channel_range_tlvs *r = vrecord;
	u8 *ptr;

	if (!r->timestamps_tlv)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

	towire_u8(&ptr, r->timestamps_tlv->encoding_type);

	towire_u8_array(&ptr, r->timestamps_tlv->encoded_timestamps, tal_count(r->timestamps_tlv->encoded_timestamps));
	return ptr;
}
static void fromwire_tlv_reply_channel_range_tlvs_timestamps_tlv(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_reply_channel_range_tlvs *r = vrecord;

	r->timestamps_tlv = tal(r, struct tlv_reply_channel_range_tlvs_timestamps_tlv);
	r->timestamps_tlv->encoding_type = fromwire_u8(cursor, plen);
	r->timestamps_tlv->encoded_timestamps = *plen ? tal_arr(r->timestamps_tlv, u8, *plen) : NULL;
fromwire_u8_array(cursor, plen, r->timestamps_tlv->encoded_timestamps, *plen);
}
/* REPLY_CHANNEL_RANGE_TLVS MSG: checksums_tlv */
static u8 *towire_tlv_reply_channel_range_tlvs_checksums_tlv(const tal_t *ctx, const void *vrecord)
{
	const struct tlv_reply_channel_range_tlvs *r = vrecord;
	u8 *ptr;

	if (!r->checksums_tlv)
		return NULL;


	ptr = tal_arr(ctx, u8, 0);

		for (size_t i = 0; i < tal_count(r->checksums_tlv); i++)
		towire_channel_update_checksums(&ptr, r->checksums_tlv + i);
	return ptr;
}
static void fromwire_tlv_reply_channel_range_tlvs_checksums_tlv(const u8 **cursor, size_t *plen, void *vrecord)
{
	struct tlv_reply_channel_range_tlvs *r = vrecord;

	r->checksums_tlv = *plen ? tal_arr(r, struct channel_update_checksums, 0) : NULL;
	for (size_t i = 0; *plen != 0; i++) {
		struct channel_update_checksums tmp;
		fromwire_channel_update_checksums(cursor, plen, &tmp);
		tal_arr_expand(&r->checksums_tlv, tmp);
	}
}

static const struct tlv_record_type tlvs_reply_channel_range_tlvs[] = {
	{ 1, towire_tlv_reply_channel_range_tlvs_timestamps_tlv, fromwire_tlv_reply_channel_range_tlvs_timestamps_tlv },
	{ 3, towire_tlv_reply_channel_range_tlvs_checksums_tlv, fromwire_tlv_reply_channel_range_tlvs_checksums_tlv },
};

void towire_reply_channel_range_tlvs(u8 **pptr,
			const void *record)
{
	size_t num_types = 2;
	const struct tlv_record_type *types = tlvs_reply_channel_range_tlvs;
	if (!record)
		return;

	for (size_t i = 0; i < num_types; i++) {
		u8 *val;
		if (i != 0)
			assert(types[i].type > types[i-1].type);
		val = types[i].towire(NULL, record);
		if (!val)
			continue;

		/* BOLT #1:
		 *
		 * The sending node:
		 ...
		 *  - MUST minimally encode `type` and `length`.
		 */
		towire_bigsize(pptr, types[i].type);
		towire_bigsize(pptr, tal_bytelen(val));
		towire(pptr, val, tal_bytelen(val));
		tal_free(val);
	}
}


bool fromwire_reply_channel_range_tlvs(const u8 **cursor, size_t *max, struct tlv_reply_channel_range_tlvs *record)
{
	size_t num_types = 2;
	const struct tlv_record_type *types = tlvs_reply_channel_range_tlvs;
	while (*max > 0) {
		struct tlv_field field;

		/* BOLT #1:
		 *
		 * The `type` is encoded using the BigSize format.
		 */
		field.numtype = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("type");
			goto fail;
		}
		field.length = fromwire_bigsize(cursor, max);

		/* BOLT #1:
		 *  - if a `type` or `length` is not minimally encoded:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (!*cursor) {
			SUPERVERBOSE("length");
			goto fail;
		}

		/* BOLT #1:
		 *  - if `length` exceeds the number of bytes remaining in the
		 *    message:
		 *    - MUST fail to parse the `tlv_stream`.
		 */
		if (field.length > *max) {
			SUPERVERBOSE("value");
			goto fail;
		}
		field.value = tal_dup_arr(record, u8, *cursor, field.length, 0);

		/* BOLT #1:
		 * - if `type` is known:
		 *   - MUST decode the next `length` bytes using the known
		 *     encoding for `type`.
		 */
		field.meta = NULL;
		for (size_t i = 0; i < num_types; i++) {
			if (types[i].type == field.numtype)
				field.meta = &types[i];
		}

		if (field.meta) {
			/* Length of message can't exceed 16 bits anyway. */
			size_t tlvlen = field.length;
			field.meta->fromwire(cursor, &tlvlen, record);

			if (!*cursor)
				goto fail;

			/* BOLT #1:
			 *  - if `length` is not exactly equal to that required
			 *    for the known encoding for `type`:
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (tlvlen != 0) {
				SUPERVERBOSE("greater than encoding length");
				goto fail;
			}
		} else {
			/* We didn't read from *cursor through a fromwire, so
			 * update manually. */
			*cursor += field.length;
		}
		/* We've read bytes in ->fromwire, so update max */
		*max -= field.length;
		tal_arr_expand(&record->fields, field);
	}
	return true;
fail:
	fromwire_fail(cursor, max);
	return false;
}

bool reply_channel_range_tlvs_is_valid(const struct tlv_reply_channel_range_tlvs *record, size_t *err_index)
{
	size_t numfields = tal_count(record->fields);
	bool first = true;
	u64 prev_type = 0;
	for (int i=0; i<numfields; i++) {
		struct tlv_field *f = &record->fields[i];
		if (f->numtype % 2 == 0 && f->meta == NULL) {
			/* BOLT #1:
			 * - otherwise, if `type` is unknown:
			 *   - if `type` is even:
			 *     - MUST fail to parse the `tlv_stream`.
			 *   - otherwise, if `type` is odd:
			 *     - MUST discard the next `length` bytes.
			 */
			SUPERVERBOSE("unknown even");
			if (err_index != NULL)
				*err_index = i;
			return false;
		} else if (!first && f->numtype <= prev_type) {
			/* BOLT #1:
			 *  - if decoded `type`s are not strictly-increasing
			 *    (including situations when two or more occurrences
			 *    of the same `type` are met):
			 *    - MUST fail to parse the `tlv_stream`.
			 */
			if (f->numtype == prev_type)
				SUPERVERBOSE("duplicate tlv type");
			else
				SUPERVERBOSE("invalid ordering");
			if (err_index != NULL)
				*err_index = i;
			return false;
		}
		first = false;
		prev_type = f->numtype;
	}
	return true;
}


/* WIRE: INIT */
u8 *towire_init(const tal_t *ctx, const u8 *globalfeatures, const u8 *features, const struct tlv_init_tlvs *tlvs)
{
	u16 gflen = tal_count(globalfeatures);
	u16 flen = tal_count(features);
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_INIT);
	towire_u16(&p, gflen);
	towire_u8_array(&p, globalfeatures, gflen);
	towire_u16(&p, flen);
	towire_u8_array(&p, features, flen);
	towire_init_tlvs(&p, tlvs);

	return memcheck(p, tal_count(p));
}
bool fromwire_init(const tal_t *ctx, const void *p, u8 **globalfeatures, u8 **features, struct tlv_init_tlvs *tlvs)
{
	u16 gflen;
	u16 flen;

	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_INIT)
		return false;
 	gflen = fromwire_u16(&cursor, &plen);
 	// 2nd case globalfeatures
	*globalfeatures = gflen ? tal_arr(ctx, u8, gflen) : NULL;
	fromwire_u8_array(&cursor, &plen, *globalfeatures, gflen);
 	flen = fromwire_u16(&cursor, &plen);
 	// 2nd case features
	*features = flen ? tal_arr(ctx, u8, flen) : NULL;
	fromwire_u8_array(&cursor, &plen, *features, flen);
 	fromwire_init_tlvs(&cursor, &plen, tlvs);
	return cursor != NULL;
}

/* WIRE: ERROR */
u8 *towire_error(const tal_t *ctx, const struct channel_id *channel_id, const u8 *data)
{
	u16 len = tal_count(data);
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_ERROR);
	towire_channel_id(&p, channel_id);
	towire_u16(&p, len);
	towire_u8_array(&p, data, len);

	return memcheck(p, tal_count(p));
}
bool fromwire_error(const tal_t *ctx, const void *p, struct channel_id *channel_id, u8 **data)
{
	u16 len;

	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_ERROR)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	len = fromwire_u16(&cursor, &plen);
 	// 2nd case data
	*data = len ? tal_arr(ctx, u8, len) : NULL;
	fromwire_u8_array(&cursor, &plen, *data, len);
	return cursor != NULL;
}

/* WIRE: PING */
u8 *towire_ping(const tal_t *ctx, u16 num_pong_bytes, const u8 *ignored)
{
	u16 byteslen = tal_count(ignored);
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_PING);
	towire_u16(&p, num_pong_bytes);
	towire_u16(&p, byteslen);
	towire_u8_array(&p, ignored, byteslen);

	return memcheck(p, tal_count(p));
}
bool fromwire_ping(const tal_t *ctx, const void *p, u16 *num_pong_bytes, u8 **ignored)
{
	u16 byteslen;

	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_PING)
		return false;
 	*num_pong_bytes = fromwire_u16(&cursor, &plen);
 	byteslen = fromwire_u16(&cursor, &plen);
 	// 2nd case ignored
	*ignored = byteslen ? tal_arr(ctx, u8, byteslen) : NULL;
	fromwire_u8_array(&cursor, &plen, *ignored, byteslen);
	return cursor != NULL;
}

/* WIRE: PONG */
u8 *towire_pong(const tal_t *ctx, const u8 *ignored)
{
	u16 byteslen = tal_count(ignored);
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_PONG);
	towire_u16(&p, byteslen);
	towire_u8_array(&p, ignored, byteslen);

	return memcheck(p, tal_count(p));
}
bool fromwire_pong(const tal_t *ctx, const void *p, u8 **ignored)
{
	u16 byteslen;

	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_PONG)
		return false;
 	byteslen = fromwire_u16(&cursor, &plen);
 	// 2nd case ignored
	*ignored = byteslen ? tal_arr(ctx, u8, byteslen) : NULL;
	fromwire_u8_array(&cursor, &plen, *ignored, byteslen);
	return cursor != NULL;
}

/* WIRE: OPEN_CHANNEL */
u8 *towire_open_channel(const tal_t *ctx, const struct bitcoin_blkid *chain_hash, const struct channel_id *temporary_channel_id, struct amount_sat funding_satoshis, struct amount_msat push_msat, struct amount_sat dust_limit_satoshis, struct amount_msat max_htlc_value_in_flight_msat, struct amount_sat channel_reserve_satoshis, struct amount_msat htlc_minimum_msat, u32 feerate_per_kw, u16 to_self_delay, u16 max_accepted_htlcs, const struct pubkey *funding_pubkey, const struct pubkey *revocation_basepoint, const struct pubkey *payment_basepoint, const struct pubkey *delayed_payment_basepoint, const struct pubkey *htlc_basepoint, const struct pubkey *first_per_commitment_point, u8 channel_flags, const struct tlv_open_channel_tlvs *tlvs)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_OPEN_CHANNEL);
	towire_bitcoin_blkid(&p, chain_hash);
	towire_channel_id(&p, temporary_channel_id);
	towire_amount_sat(&p, funding_satoshis);
	towire_amount_msat(&p, push_msat);
	towire_amount_sat(&p, dust_limit_satoshis);
	towire_amount_msat(&p, max_htlc_value_in_flight_msat);
	towire_amount_sat(&p, channel_reserve_satoshis);
	towire_amount_msat(&p, htlc_minimum_msat);
	towire_u32(&p, feerate_per_kw);
	towire_u16(&p, to_self_delay);
	towire_u16(&p, max_accepted_htlcs);
	towire_pubkey(&p, funding_pubkey);
	towire_pubkey(&p, revocation_basepoint);
	towire_pubkey(&p, payment_basepoint);
	towire_pubkey(&p, delayed_payment_basepoint);
	towire_pubkey(&p, htlc_basepoint);
	towire_pubkey(&p, first_per_commitment_point);
	towire_u8(&p, channel_flags);
	towire_open_channel_tlvs(&p, tlvs);

	return memcheck(p, tal_count(p));
}
bool fromwire_open_channel(const void *p, struct bitcoin_blkid *chain_hash, struct channel_id *temporary_channel_id, struct amount_sat *funding_satoshis, struct amount_msat *push_msat, struct amount_sat *dust_limit_satoshis, struct amount_msat *max_htlc_value_in_flight_msat, struct amount_sat *channel_reserve_satoshis, struct amount_msat *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 *htlc_basepoint, struct pubkey *first_per_commitment_point, u8 *channel_flags, struct tlv_open_channel_tlvs *tlvs)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_OPEN_CHANNEL)
		return false;
 	fromwire_bitcoin_blkid(&cursor, &plen, chain_hash);
 	fromwire_channel_id(&cursor, &plen, temporary_channel_id);
 	*funding_satoshis = fromwire_amount_sat(&cursor, &plen);
 	*push_msat = fromwire_amount_msat(&cursor, &plen);
 	*dust_limit_satoshis = fromwire_amount_sat(&cursor, &plen);
 	*max_htlc_value_in_flight_msat = fromwire_amount_msat(&cursor, &plen);
 	*channel_reserve_satoshis = fromwire_amount_sat(&cursor, &plen);
 	*htlc_minimum_msat = fromwire_amount_msat(&cursor, &plen);
 	*feerate_per_kw = fromwire_u32(&cursor, &plen);
 	*to_self_delay = fromwire_u16(&cursor, &plen);
 	*max_accepted_htlcs = fromwire_u16(&cursor, &plen);
 	fromwire_pubkey(&cursor, &plen, funding_pubkey);
 	fromwire_pubkey(&cursor, &plen, revocation_basepoint);
 	fromwire_pubkey(&cursor, &plen, payment_basepoint);
 	fromwire_pubkey(&cursor, &plen, delayed_payment_basepoint);
 	fromwire_pubkey(&cursor, &plen, htlc_basepoint);
 	fromwire_pubkey(&cursor, &plen, first_per_commitment_point);
 	*channel_flags = fromwire_u8(&cursor, &plen);
 	fromwire_open_channel_tlvs(&cursor, &plen, tlvs);
	return cursor != NULL;
}

/* WIRE: ACCEPT_CHANNEL */
u8 *towire_accept_channel(const tal_t *ctx, const struct channel_id *temporary_channel_id, struct amount_sat dust_limit_satoshis, struct amount_msat max_htlc_value_in_flight_msat, struct amount_sat channel_reserve_satoshis, struct amount_msat htlc_minimum_msat, u32 minimum_depth, u16 to_self_delay, u16 max_accepted_htlcs, const struct pubkey *funding_pubkey, const struct pubkey *revocation_basepoint, const struct pubkey *payment_basepoint, const struct pubkey *delayed_payment_basepoint, const struct pubkey *htlc_basepoint, const struct pubkey *first_per_commitment_point, const struct tlv_accept_channel_tlvs *tlvs)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_ACCEPT_CHANNEL);
	towire_channel_id(&p, temporary_channel_id);
	towire_amount_sat(&p, dust_limit_satoshis);
	towire_amount_msat(&p, max_htlc_value_in_flight_msat);
	towire_amount_sat(&p, channel_reserve_satoshis);
	towire_amount_msat(&p, htlc_minimum_msat);
	towire_u32(&p, minimum_depth);
	towire_u16(&p, to_self_delay);
	towire_u16(&p, max_accepted_htlcs);
	towire_pubkey(&p, funding_pubkey);
	towire_pubkey(&p, revocation_basepoint);
	towire_pubkey(&p, payment_basepoint);
	towire_pubkey(&p, delayed_payment_basepoint);
	towire_pubkey(&p, htlc_basepoint);
	towire_pubkey(&p, first_per_commitment_point);
	towire_accept_channel_tlvs(&p, tlvs);

	return memcheck(p, tal_count(p));
}
bool fromwire_accept_channel(const void *p, struct channel_id *temporary_channel_id, struct amount_sat *dust_limit_satoshis, struct amount_msat *max_htlc_value_in_flight_msat, struct amount_sat *channel_reserve_satoshis, struct amount_msat *htlc_minimum_msat, u32 *minimum_depth, 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 *htlc_basepoint, struct pubkey *first_per_commitment_point, struct tlv_accept_channel_tlvs *tlvs)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_ACCEPT_CHANNEL)
		return false;
 	fromwire_channel_id(&cursor, &plen, temporary_channel_id);
 	*dust_limit_satoshis = fromwire_amount_sat(&cursor, &plen);
 	*max_htlc_value_in_flight_msat = fromwire_amount_msat(&cursor, &plen);
 	*channel_reserve_satoshis = fromwire_amount_sat(&cursor, &plen);
 	*htlc_minimum_msat = fromwire_amount_msat(&cursor, &plen);
 	*minimum_depth = fromwire_u32(&cursor, &plen);
 	*to_self_delay = fromwire_u16(&cursor, &plen);
 	*max_accepted_htlcs = fromwire_u16(&cursor, &plen);
 	fromwire_pubkey(&cursor, &plen, funding_pubkey);
 	fromwire_pubkey(&cursor, &plen, revocation_basepoint);
 	fromwire_pubkey(&cursor, &plen, payment_basepoint);
 	fromwire_pubkey(&cursor, &plen, delayed_payment_basepoint);
 	fromwire_pubkey(&cursor, &plen, htlc_basepoint);
 	fromwire_pubkey(&cursor, &plen, first_per_commitment_point);
 	fromwire_accept_channel_tlvs(&cursor, &plen, tlvs);
	return cursor != NULL;
}

/* WIRE: FUNDING_CREATED */
u8 *towire_funding_created(const tal_t *ctx, const struct channel_id *temporary_channel_id, const struct bitcoin_txid *funding_txid, u16 funding_output_index, const secp256k1_ecdsa_signature *signature)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_FUNDING_CREATED);
	towire_channel_id(&p, temporary_channel_id);
	towire_bitcoin_txid(&p, funding_txid);
	towire_u16(&p, funding_output_index);
	towire_secp256k1_ecdsa_signature(&p, signature);

	return memcheck(p, tal_count(p));
}
bool fromwire_funding_created(const void *p, struct channel_id *temporary_channel_id, struct bitcoin_txid *funding_txid, u16 *funding_output_index, secp256k1_ecdsa_signature *signature)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_FUNDING_CREATED)
		return false;
 	fromwire_channel_id(&cursor, &plen, temporary_channel_id);
 	fromwire_bitcoin_txid(&cursor, &plen, funding_txid);
 	*funding_output_index = fromwire_u16(&cursor, &plen);
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, signature);
	return cursor != NULL;
}

/* WIRE: FUNDING_SIGNED */
u8 *towire_funding_signed(const tal_t *ctx, const struct channel_id *channel_id, const secp256k1_ecdsa_signature *signature)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_FUNDING_SIGNED);
	towire_channel_id(&p, channel_id);
	towire_secp256k1_ecdsa_signature(&p, signature);

	return memcheck(p, tal_count(p));
}
bool fromwire_funding_signed(const void *p, struct channel_id *channel_id, secp256k1_ecdsa_signature *signature)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_FUNDING_SIGNED)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, signature);
	return cursor != NULL;
}

/* WIRE: FUNDING_LOCKED */
u8 *towire_funding_locked(const tal_t *ctx, const struct channel_id *channel_id, const struct pubkey *next_per_commitment_point)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_FUNDING_LOCKED);
	towire_channel_id(&p, channel_id);
	towire_pubkey(&p, next_per_commitment_point);

	return memcheck(p, tal_count(p));
}
bool fromwire_funding_locked(const void *p, struct channel_id *channel_id, struct pubkey *next_per_commitment_point)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_FUNDING_LOCKED)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	fromwire_pubkey(&cursor, &plen, next_per_commitment_point);
	return cursor != NULL;
}

/* WIRE: SHUTDOWN */
u8 *towire_shutdown(const tal_t *ctx, const struct channel_id *channel_id, const u8 *scriptpubkey)
{
	u16 len = tal_count(scriptpubkey);
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_SHUTDOWN);
	towire_channel_id(&p, channel_id);
	towire_u16(&p, len);
	towire_u8_array(&p, scriptpubkey, len);

	return memcheck(p, tal_count(p));
}
bool fromwire_shutdown(const tal_t *ctx, const void *p, struct channel_id *channel_id, u8 **scriptpubkey)
{
	u16 len;

	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_SHUTDOWN)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	len = fromwire_u16(&cursor, &plen);
 	// 2nd case scriptpubkey
	*scriptpubkey = len ? tal_arr(ctx, u8, len) : NULL;
	fromwire_u8_array(&cursor, &plen, *scriptpubkey, len);
	return cursor != NULL;
}

/* WIRE: CLOSING_SIGNED */
u8 *towire_closing_signed(const tal_t *ctx, const struct channel_id *channel_id, struct amount_sat fee_satoshis, const secp256k1_ecdsa_signature *signature)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_CLOSING_SIGNED);
	towire_channel_id(&p, channel_id);
	towire_amount_sat(&p, fee_satoshis);
	towire_secp256k1_ecdsa_signature(&p, signature);

	return memcheck(p, tal_count(p));
}
bool fromwire_closing_signed(const void *p, struct channel_id *channel_id, struct amount_sat *fee_satoshis, secp256k1_ecdsa_signature *signature)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_CLOSING_SIGNED)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	*fee_satoshis = fromwire_amount_sat(&cursor, &plen);
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, signature);
	return cursor != NULL;
}

/* WIRE: UPDATE_ADD_HTLC */
u8 *towire_update_add_htlc(const tal_t *ctx, const struct channel_id *channel_id, u64 id, struct amount_msat amount_msat, const struct sha256 *payment_hash, u32 cltv_expiry, const u8 onion_routing_packet[1366])
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_UPDATE_ADD_HTLC);
	towire_channel_id(&p, channel_id);
	towire_u64(&p, id);
	towire_amount_msat(&p, amount_msat);
	towire_sha256(&p, payment_hash);
	towire_u32(&p, cltv_expiry);
	towire_u8_array(&p, onion_routing_packet, 1366);

	return memcheck(p, tal_count(p));
}
bool fromwire_update_add_htlc(const void *p, struct channel_id *channel_id, u64 *id, struct amount_msat *amount_msat, struct sha256 *payment_hash, u32 *cltv_expiry, u8 onion_routing_packet[1366])
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_UPDATE_ADD_HTLC)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	*id = fromwire_u64(&cursor, &plen);
 	*amount_msat = fromwire_amount_msat(&cursor, &plen);
 	fromwire_sha256(&cursor, &plen, payment_hash);
 	*cltv_expiry = fromwire_u32(&cursor, &plen);
 	fromwire_u8_array(&cursor, &plen, onion_routing_packet, 1366);
	return cursor != NULL;
}

/* WIRE: UPDATE_FULFILL_HTLC */
u8 *towire_update_fulfill_htlc(const tal_t *ctx, const struct channel_id *channel_id, u64 id, const struct preimage *payment_preimage)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_UPDATE_FULFILL_HTLC);
	towire_channel_id(&p, channel_id);
	towire_u64(&p, id);
	towire_preimage(&p, payment_preimage);

	return memcheck(p, tal_count(p));
}
bool fromwire_update_fulfill_htlc(const void *p, struct channel_id *channel_id, u64 *id, struct preimage *payment_preimage)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_UPDATE_FULFILL_HTLC)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	*id = fromwire_u64(&cursor, &plen);
 	fromwire_preimage(&cursor, &plen, payment_preimage);
	return cursor != NULL;
}

/* WIRE: UPDATE_FAIL_HTLC */
u8 *towire_update_fail_htlc(const tal_t *ctx, const struct channel_id *channel_id, u64 id, const u8 *reason)
{
	u16 len = tal_count(reason);
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_UPDATE_FAIL_HTLC);
	towire_channel_id(&p, channel_id);
	towire_u64(&p, id);
	towire_u16(&p, len);
	towire_u8_array(&p, reason, len);

	return memcheck(p, tal_count(p));
}
bool fromwire_update_fail_htlc(const tal_t *ctx, const void *p, struct channel_id *channel_id, u64 *id, u8 **reason)
{
	u16 len;

	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_UPDATE_FAIL_HTLC)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	*id = fromwire_u64(&cursor, &plen);
 	len = fromwire_u16(&cursor, &plen);
 	// 2nd case reason
	*reason = len ? tal_arr(ctx, u8, len) : NULL;
	fromwire_u8_array(&cursor, &plen, *reason, len);
	return cursor != NULL;
}

/* WIRE: UPDATE_FAIL_MALFORMED_HTLC */
u8 *towire_update_fail_malformed_htlc(const tal_t *ctx, const struct channel_id *channel_id, u64 id, const struct sha256 *sha256_of_onion, u16 failure_code)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_UPDATE_FAIL_MALFORMED_HTLC);
	towire_channel_id(&p, channel_id);
	towire_u64(&p, id);
	towire_sha256(&p, sha256_of_onion);
	towire_u16(&p, failure_code);

	return memcheck(p, tal_count(p));
}
bool fromwire_update_fail_malformed_htlc(const void *p, struct channel_id *channel_id, u64 *id, struct sha256 *sha256_of_onion, u16 *failure_code)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_UPDATE_FAIL_MALFORMED_HTLC)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	*id = fromwire_u64(&cursor, &plen);
 	fromwire_sha256(&cursor, &plen, sha256_of_onion);
 	*failure_code = fromwire_u16(&cursor, &plen);
	return cursor != NULL;
}

/* WIRE: COMMITMENT_SIGNED */
u8 *towire_commitment_signed(const tal_t *ctx, const struct channel_id *channel_id, const secp256k1_ecdsa_signature *signature, const secp256k1_ecdsa_signature *htlc_signature)
{
	u16 num_htlcs = tal_count(htlc_signature);
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_COMMITMENT_SIGNED);
	towire_channel_id(&p, channel_id);
	towire_secp256k1_ecdsa_signature(&p, signature);
	towire_u16(&p, num_htlcs);
	for (size_t i = 0; i < num_htlcs; i++)
		towire_secp256k1_ecdsa_signature(&p, htlc_signature + i);

	return memcheck(p, tal_count(p));
}
bool fromwire_commitment_signed(const tal_t *ctx, const void *p, struct channel_id *channel_id, secp256k1_ecdsa_signature *signature, secp256k1_ecdsa_signature **htlc_signature)
{
	u16 num_htlcs;

	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_COMMITMENT_SIGNED)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, signature);
 	num_htlcs = fromwire_u16(&cursor, &plen);
 	// 2nd case htlc_signature
	*htlc_signature = num_htlcs ? tal_arr(ctx, secp256k1_ecdsa_signature, num_htlcs) : NULL;
	for (size_t i = 0; i < num_htlcs; i++)
		fromwire_secp256k1_ecdsa_signature(&cursor, &plen, *htlc_signature + i);
	return cursor != NULL;
}

/* WIRE: REVOKE_AND_ACK */
u8 *towire_revoke_and_ack(const tal_t *ctx, const struct channel_id *channel_id, const struct secret *per_commitment_secret, const struct pubkey *next_per_commitment_point)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_REVOKE_AND_ACK);
	towire_channel_id(&p, channel_id);
	towire_secret(&p, per_commitment_secret);
	towire_pubkey(&p, next_per_commitment_point);

	return memcheck(p, tal_count(p));
}
bool fromwire_revoke_and_ack(const void *p, struct channel_id *channel_id, struct secret *per_commitment_secret, struct pubkey *next_per_commitment_point)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_REVOKE_AND_ACK)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	fromwire_secret(&cursor, &plen, per_commitment_secret);
 	fromwire_pubkey(&cursor, &plen, next_per_commitment_point);
	return cursor != NULL;
}

/* WIRE: UPDATE_FEE */
u8 *towire_update_fee(const tal_t *ctx, const struct channel_id *channel_id, u32 feerate_per_kw)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_UPDATE_FEE);
	towire_channel_id(&p, channel_id);
	towire_u32(&p, feerate_per_kw);

	return memcheck(p, tal_count(p));
}
bool fromwire_update_fee(const void *p, struct channel_id *channel_id, u32 *feerate_per_kw)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_UPDATE_FEE)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	*feerate_per_kw = fromwire_u32(&cursor, &plen);
	return cursor != NULL;
}

/* WIRE: CHANNEL_REESTABLISH */
u8 *towire_channel_reestablish(const tal_t *ctx, const struct channel_id *channel_id, u64 next_commitment_number, u64 next_revocation_number, const struct secret *your_last_per_commitment_secret, const struct pubkey *my_current_per_commitment_point)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_CHANNEL_REESTABLISH);
	towire_channel_id(&p, channel_id);
	towire_u64(&p, next_commitment_number);
	towire_u64(&p, next_revocation_number);
	towire_secret(&p, your_last_per_commitment_secret);
	towire_pubkey(&p, my_current_per_commitment_point);

	return memcheck(p, tal_count(p));
}
bool fromwire_channel_reestablish(const void *p, struct channel_id *channel_id, u64 *next_commitment_number, u64 *next_revocation_number, struct secret *your_last_per_commitment_secret, struct pubkey *my_current_per_commitment_point)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_CHANNEL_REESTABLISH)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	*next_commitment_number = fromwire_u64(&cursor, &plen);
 	*next_revocation_number = fromwire_u64(&cursor, &plen);
 	fromwire_secret(&cursor, &plen, your_last_per_commitment_secret);
 	fromwire_pubkey(&cursor, &plen, my_current_per_commitment_point);
	return cursor != NULL;
}

/* WIRE: ANNOUNCEMENT_SIGNATURES */
u8 *towire_announcement_signatures(const tal_t *ctx, const struct channel_id *channel_id, const struct short_channel_id *short_channel_id, const secp256k1_ecdsa_signature *node_signature, const secp256k1_ecdsa_signature *bitcoin_signature)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_ANNOUNCEMENT_SIGNATURES);
	towire_channel_id(&p, channel_id);
	towire_short_channel_id(&p, short_channel_id);
	towire_secp256k1_ecdsa_signature(&p, node_signature);
	towire_secp256k1_ecdsa_signature(&p, bitcoin_signature);

	return memcheck(p, tal_count(p));
}
bool fromwire_announcement_signatures(const void *p, struct channel_id *channel_id, struct short_channel_id *short_channel_id, secp256k1_ecdsa_signature *node_signature, secp256k1_ecdsa_signature *bitcoin_signature)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_ANNOUNCEMENT_SIGNATURES)
		return false;
 	fromwire_channel_id(&cursor, &plen, channel_id);
 	fromwire_short_channel_id(&cursor, &plen, short_channel_id);
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, node_signature);
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, bitcoin_signature);
	return cursor != NULL;
}

/* WIRE: CHANNEL_ANNOUNCEMENT */
u8 *towire_channel_announcement(const tal_t *ctx, const secp256k1_ecdsa_signature *node_signature_1, const secp256k1_ecdsa_signature *node_signature_2, const secp256k1_ecdsa_signature *bitcoin_signature_1, const secp256k1_ecdsa_signature *bitcoin_signature_2, const u8 *features, const struct bitcoin_blkid *chain_hash, const struct short_channel_id *short_channel_id, const struct node_id *node_id_1, const struct node_id *node_id_2, const struct pubkey *bitcoin_key_1, const struct pubkey *bitcoin_key_2)
{
	u16 len = tal_count(features);
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_CHANNEL_ANNOUNCEMENT);
	towire_secp256k1_ecdsa_signature(&p, node_signature_1);
	towire_secp256k1_ecdsa_signature(&p, node_signature_2);
	towire_secp256k1_ecdsa_signature(&p, bitcoin_signature_1);
	towire_secp256k1_ecdsa_signature(&p, bitcoin_signature_2);
	towire_u16(&p, len);
	towire_u8_array(&p, features, len);
	towire_bitcoin_blkid(&p, chain_hash);
	towire_short_channel_id(&p, short_channel_id);
	towire_node_id(&p, node_id_1);
	towire_node_id(&p, node_id_2);
	towire_pubkey(&p, bitcoin_key_1);
	towire_pubkey(&p, bitcoin_key_2);

	return memcheck(p, tal_count(p));
}
bool fromwire_channel_announcement(const tal_t *ctx, const void *p, secp256k1_ecdsa_signature *node_signature_1, secp256k1_ecdsa_signature *node_signature_2, secp256k1_ecdsa_signature *bitcoin_signature_1, secp256k1_ecdsa_signature *bitcoin_signature_2, u8 **features, struct bitcoin_blkid *chain_hash, struct short_channel_id *short_channel_id, struct node_id *node_id_1, struct node_id *node_id_2, struct pubkey *bitcoin_key_1, struct pubkey *bitcoin_key_2)
{
	u16 len;

	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_CHANNEL_ANNOUNCEMENT)
		return false;
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, node_signature_1);
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, node_signature_2);
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, bitcoin_signature_1);
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, bitcoin_signature_2);
 	len = fromwire_u16(&cursor, &plen);
 	// 2nd case features
	*features = len ? tal_arr(ctx, u8, len) : NULL;
	fromwire_u8_array(&cursor, &plen, *features, len);
 	fromwire_bitcoin_blkid(&cursor, &plen, chain_hash);
 	fromwire_short_channel_id(&cursor, &plen, short_channel_id);
 	fromwire_node_id(&cursor, &plen, node_id_1);
 	fromwire_node_id(&cursor, &plen, node_id_2);
 	fromwire_pubkey(&cursor, &plen, bitcoin_key_1);
 	fromwire_pubkey(&cursor, &plen, bitcoin_key_2);
	return cursor != NULL;
}

/* WIRE: NODE_ANNOUNCEMENT */
u8 *towire_node_announcement(const tal_t *ctx, const secp256k1_ecdsa_signature *signature, const u8 *features, u32 timestamp, const struct node_id *node_id, const u8 rgb_color[3], const u8 alias[32], const u8 *addresses)
{
	u16 flen = tal_count(features);
	u16 addrlen = tal_count(addresses);
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_NODE_ANNOUNCEMENT);
	towire_secp256k1_ecdsa_signature(&p, signature);
	towire_u16(&p, flen);
	towire_u8_array(&p, features, flen);
	towire_u32(&p, timestamp);
	towire_node_id(&p, node_id);
	towire_u8_array(&p, rgb_color, 3);
	towire_u8_array(&p, alias, 32);
	towire_u16(&p, addrlen);
	towire_u8_array(&p, addresses, addrlen);

	return memcheck(p, tal_count(p));
}
bool fromwire_node_announcement(const tal_t *ctx, const void *p, secp256k1_ecdsa_signature *signature, u8 **features, u32 *timestamp, struct node_id *node_id, u8 rgb_color[3], u8 alias[32], u8 **addresses)
{
	u16 flen;
	u16 addrlen;

	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_NODE_ANNOUNCEMENT)
		return false;
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, signature);
 	flen = fromwire_u16(&cursor, &plen);
 	// 2nd case features
	*features = flen ? tal_arr(ctx, u8, flen) : NULL;
	fromwire_u8_array(&cursor, &plen, *features, flen);
 	*timestamp = fromwire_u32(&cursor, &plen);
 	fromwire_node_id(&cursor, &plen, node_id);
 	fromwire_u8_array(&cursor, &plen, rgb_color, 3);
 	fromwire_u8_array(&cursor, &plen, alias, 32);
 	addrlen = fromwire_u16(&cursor, &plen);
 	// 2nd case addresses
	*addresses = addrlen ? tal_arr(ctx, u8, addrlen) : NULL;
	fromwire_u8_array(&cursor, &plen, *addresses, addrlen);
	return cursor != NULL;
}

/* WIRE: CHANNEL_UPDATE */
u8 *towire_channel_update(const tal_t *ctx, const secp256k1_ecdsa_signature *signature, const struct bitcoin_blkid *chain_hash, const struct short_channel_id *short_channel_id, u32 timestamp, u8 message_flags, u8 channel_flags, u16 cltv_expiry_delta, struct amount_msat htlc_minimum_msat, u32 fee_base_msat, u32 fee_proportional_millionths)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_CHANNEL_UPDATE);
	towire_secp256k1_ecdsa_signature(&p, signature);
	towire_bitcoin_blkid(&p, chain_hash);
	towire_short_channel_id(&p, short_channel_id);
	towire_u32(&p, timestamp);
	towire_u8(&p, message_flags);
	towire_u8(&p, channel_flags);
	towire_u16(&p, cltv_expiry_delta);
	towire_amount_msat(&p, htlc_minimum_msat);
	towire_u32(&p, fee_base_msat);
	towire_u32(&p, fee_proportional_millionths);

	return memcheck(p, tal_count(p));
}
bool fromwire_channel_update(const void *p, secp256k1_ecdsa_signature *signature, struct bitcoin_blkid *chain_hash, struct short_channel_id *short_channel_id, u32 *timestamp, u8 *message_flags, u8 *channel_flags, u16 *cltv_expiry_delta, struct amount_msat *htlc_minimum_msat, u32 *fee_base_msat, u32 *fee_proportional_millionths)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_CHANNEL_UPDATE)
		return false;
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, signature);
 	fromwire_bitcoin_blkid(&cursor, &plen, chain_hash);
 	fromwire_short_channel_id(&cursor, &plen, short_channel_id);
 	*timestamp = fromwire_u32(&cursor, &plen);
 	*message_flags = fromwire_u8(&cursor, &plen);
 	*channel_flags = fromwire_u8(&cursor, &plen);
 	*cltv_expiry_delta = fromwire_u16(&cursor, &plen);
 	*htlc_minimum_msat = fromwire_amount_msat(&cursor, &plen);
 	*fee_base_msat = fromwire_u32(&cursor, &plen);
 	*fee_proportional_millionths = fromwire_u32(&cursor, &plen);
	return cursor != NULL;
}

/* WIRE: QUERY_SHORT_CHANNEL_IDS */
u8 *towire_query_short_channel_ids(const tal_t *ctx, const struct bitcoin_blkid *chain_hash, const u8 *encoded_short_ids, const struct tlv_query_short_channel_ids_tlvs *tlvs)
{
	u16 len = tal_count(encoded_short_ids);
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_QUERY_SHORT_CHANNEL_IDS);
	towire_bitcoin_blkid(&p, chain_hash);
	towire_u16(&p, len);
	towire_u8_array(&p, encoded_short_ids, len);
	towire_query_short_channel_ids_tlvs(&p, tlvs);

	return memcheck(p, tal_count(p));
}
bool fromwire_query_short_channel_ids(const tal_t *ctx, const void *p, struct bitcoin_blkid *chain_hash, u8 **encoded_short_ids, struct tlv_query_short_channel_ids_tlvs *tlvs)
{
	u16 len;

	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_QUERY_SHORT_CHANNEL_IDS)
		return false;
 	fromwire_bitcoin_blkid(&cursor, &plen, chain_hash);
 	len = fromwire_u16(&cursor, &plen);
 	// 2nd case encoded_short_ids
	*encoded_short_ids = len ? tal_arr(ctx, u8, len) : NULL;
	fromwire_u8_array(&cursor, &plen, *encoded_short_ids, len);
 	fromwire_query_short_channel_ids_tlvs(&cursor, &plen, tlvs);
	return cursor != NULL;
}

/* WIRE: REPLY_SHORT_CHANNEL_IDS_END */
u8 *towire_reply_short_channel_ids_end(const tal_t *ctx, const struct bitcoin_blkid *chain_hash, u8 full_information)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_REPLY_SHORT_CHANNEL_IDS_END);
	towire_bitcoin_blkid(&p, chain_hash);
	towire_u8(&p, full_information);

	return memcheck(p, tal_count(p));
}
bool fromwire_reply_short_channel_ids_end(const void *p, struct bitcoin_blkid *chain_hash, u8 *full_information)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_REPLY_SHORT_CHANNEL_IDS_END)
		return false;
 	fromwire_bitcoin_blkid(&cursor, &plen, chain_hash);
 	*full_information = fromwire_u8(&cursor, &plen);
	return cursor != NULL;
}

/* WIRE: QUERY_CHANNEL_RANGE */
u8 *towire_query_channel_range(const tal_t *ctx, const struct bitcoin_blkid *chain_hash, u32 first_blocknum, u32 number_of_blocks, const struct tlv_query_channel_range_tlvs *tlvs)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_QUERY_CHANNEL_RANGE);
	towire_bitcoin_blkid(&p, chain_hash);
	towire_u32(&p, first_blocknum);
	towire_u32(&p, number_of_blocks);
	towire_query_channel_range_tlvs(&p, tlvs);

	return memcheck(p, tal_count(p));
}
bool fromwire_query_channel_range(const void *p, struct bitcoin_blkid *chain_hash, u32 *first_blocknum, u32 *number_of_blocks, struct tlv_query_channel_range_tlvs *tlvs)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_QUERY_CHANNEL_RANGE)
		return false;
 	fromwire_bitcoin_blkid(&cursor, &plen, chain_hash);
 	*first_blocknum = fromwire_u32(&cursor, &plen);
 	*number_of_blocks = fromwire_u32(&cursor, &plen);
 	fromwire_query_channel_range_tlvs(&cursor, &plen, tlvs);
	return cursor != NULL;
}

/* WIRE: REPLY_CHANNEL_RANGE */
u8 *towire_reply_channel_range(const tal_t *ctx, const struct bitcoin_blkid *chain_hash, u32 first_blocknum, u32 number_of_blocks, u8 full_information, const u8 *encoded_short_ids, const struct tlv_reply_channel_range_tlvs *tlvs)
{
	u16 len = tal_count(encoded_short_ids);
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_REPLY_CHANNEL_RANGE);
	towire_bitcoin_blkid(&p, chain_hash);
	towire_u32(&p, first_blocknum);
	towire_u32(&p, number_of_blocks);
	towire_u8(&p, full_information);
	towire_u16(&p, len);
	towire_u8_array(&p, encoded_short_ids, len);
	towire_reply_channel_range_tlvs(&p, tlvs);

	return memcheck(p, tal_count(p));
}
bool fromwire_reply_channel_range(const tal_t *ctx, const void *p, struct bitcoin_blkid *chain_hash, u32 *first_blocknum, u32 *number_of_blocks, u8 *full_information, u8 **encoded_short_ids, struct tlv_reply_channel_range_tlvs *tlvs)
{
	u16 len;

	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_REPLY_CHANNEL_RANGE)
		return false;
 	fromwire_bitcoin_blkid(&cursor, &plen, chain_hash);
 	*first_blocknum = fromwire_u32(&cursor, &plen);
 	*number_of_blocks = fromwire_u32(&cursor, &plen);
 	*full_information = fromwire_u8(&cursor, &plen);
 	len = fromwire_u16(&cursor, &plen);
 	// 2nd case encoded_short_ids
	*encoded_short_ids = len ? tal_arr(ctx, u8, len) : NULL;
	fromwire_u8_array(&cursor, &plen, *encoded_short_ids, len);
 	fromwire_reply_channel_range_tlvs(&cursor, &plen, tlvs);
	return cursor != NULL;
}

/* WIRE: GOSSIP_TIMESTAMP_FILTER */
u8 *towire_gossip_timestamp_filter(const tal_t *ctx, const struct bitcoin_blkid *chain_hash, u32 first_timestamp, u32 timestamp_range)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_GOSSIP_TIMESTAMP_FILTER);
	towire_bitcoin_blkid(&p, chain_hash);
	towire_u32(&p, first_timestamp);
	towire_u32(&p, timestamp_range);

	return memcheck(p, tal_count(p));
}
bool fromwire_gossip_timestamp_filter(const void *p, struct bitcoin_blkid *chain_hash, u32 *first_timestamp, u32 *timestamp_range)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_GOSSIP_TIMESTAMP_FILTER)
		return false;
 	fromwire_bitcoin_blkid(&cursor, &plen, chain_hash);
 	*first_timestamp = fromwire_u32(&cursor, &plen);
 	*timestamp_range = fromwire_u32(&cursor, &plen);
	return cursor != NULL;
}

/* WIRE: CHANNEL_UPDATE_OPTION_CHANNEL_HTLC_MAX */
u8 *towire_channel_update_option_channel_htlc_max(const tal_t *ctx, const secp256k1_ecdsa_signature *signature, const struct bitcoin_blkid *chain_hash, const struct short_channel_id *short_channel_id, u32 timestamp, u8 message_flags, u8 channel_flags, u16 cltv_expiry_delta, struct amount_msat htlc_minimum_msat, u32 fee_base_msat, u32 fee_proportional_millionths, struct amount_msat htlc_maximum_msat)
{
	u8 *p = tal_arr(ctx, u8, 0);

	towire_u16(&p, WIRE_CHANNEL_UPDATE);
	towire_secp256k1_ecdsa_signature(&p, signature);
	towire_bitcoin_blkid(&p, chain_hash);
	towire_short_channel_id(&p, short_channel_id);
	towire_u32(&p, timestamp);
	towire_u8(&p, message_flags);
	towire_u8(&p, channel_flags);
	towire_u16(&p, cltv_expiry_delta);
	towire_amount_msat(&p, htlc_minimum_msat);
	towire_u32(&p, fee_base_msat);
	towire_u32(&p, fee_proportional_millionths);
	towire_amount_msat(&p, htlc_maximum_msat);

	return memcheck(p, tal_count(p));
}
bool fromwire_channel_update_option_channel_htlc_max(const void *p, secp256k1_ecdsa_signature *signature, struct bitcoin_blkid *chain_hash, struct short_channel_id *short_channel_id, u32 *timestamp, u8 *message_flags, u8 *channel_flags, u16 *cltv_expiry_delta, struct amount_msat *htlc_minimum_msat, u32 *fee_base_msat, u32 *fee_proportional_millionths, struct amount_msat *htlc_maximum_msat)
{
	const u8 *cursor = p;
	size_t plen = tal_count(p);

	if (fromwire_u16(&cursor, &plen) != WIRE_CHANNEL_UPDATE)
		return false;
 	fromwire_secp256k1_ecdsa_signature(&cursor, &plen, signature);
 	fromwire_bitcoin_blkid(&cursor, &plen, chain_hash);
 	fromwire_short_channel_id(&cursor, &plen, short_channel_id);
 	*timestamp = fromwire_u32(&cursor, &plen);
 	*message_flags = fromwire_u8(&cursor, &plen);
 	*channel_flags = fromwire_u8(&cursor, &plen);
 	*cltv_expiry_delta = fromwire_u16(&cursor, &plen);
 	*htlc_minimum_msat = fromwire_amount_msat(&cursor, &plen);
 	*fee_base_msat = fromwire_u32(&cursor, &plen);
 	*fee_proportional_millionths = fromwire_u32(&cursor, &plen);
 	*htlc_maximum_msat = fromwire_amount_msat(&cursor, &plen);
	return cursor != NULL;
}
// SHA256STAMP:c950ebb221bc14a97d377d804daa828196be344fa4addae32351ec8a135ed45a
