Skip to content

Commit

Permalink
wire: use common/bigsize routines
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jul 31, 2019
1 parent 2241e25 commit cc70b9c
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 44 deletions.
6 changes: 6 additions & 0 deletions channeld/test/run-commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ static bool print_superverbose;
/*#define DEBUG */

/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for status_fmt */
void status_fmt(enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...)

Expand Down
6 changes: 6 additions & 0 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
#include <wally_core.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for status_failed */
void status_failed(enum status_failreason code UNNEEDED,
const char *fmt UNNEEDED, ...)
Expand Down
7 changes: 7 additions & 0 deletions cli/test/run-large-input.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "config.h"
#include <assert.h>
#include <common/amount.h>
#include <common/bigsize.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/stat.h>
Expand All @@ -26,6 +27,12 @@ int test_printf(const char *format, ...);
#undef main

/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for version_and_exit */
char *version_and_exit(const void *unused UNNEEDED)
{ fprintf(stderr, "version_and_exit called!\n"); abort(); }
Expand Down
1 change: 1 addition & 0 deletions closingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ $(LIGHTNINGD_CLOSING_OBJS): $(LIGHTNINGD_HEADERS)
CLOSINGD_COMMON_OBJS := \
common/amount.o \
common/base32.o \
common/bigsize.o \
common/bip32.o \
common/close_tx.o \
common/crypto_state.o \
Expand Down
1 change: 1 addition & 0 deletions connectd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ CONNECTD_COMMON_OBJS := \
common/base32.o \
common/bech32.o \
common/bech32_util.o \
common/bigsize.o \
common/bip32.o \
common/crypto_state.o \
common/cryptomsg.o \
Expand Down
1 change: 1 addition & 0 deletions gossipd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ GOSSIPD_COMMON_OBJS := \
common/base32.o \
common/bech32.o \
common/bech32_util.o \
common/bigsize.o \
common/bip32.o \
common/crypto_state.o \
common/cryptomsg.o \
Expand Down
1 change: 1 addition & 0 deletions hsmd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LIGHTNINGD_HSM_OBJS := $(LIGHTNINGD_HSM_SRC:.c=.o)
# Common source we use.
HSMD_COMMON_OBJS := \
common/amount.o \
common/bigsize.o \
common/bip32.o \
common/daemon.o \
common/daemon_conn.o \
Expand Down
6 changes: 6 additions & 0 deletions lightningd/test/run-find_my_abspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ char *add_plugin_dir(struct plugins *plugins UNNEEDED, const char *dir UNNEEDED,
/* Generated stub for begin_topology */
void begin_topology(struct chain_topology *topo UNNEEDED)
{ fprintf(stderr, "begin_topology called!\n"); abort(); }
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for channel_notify_new_block */
void channel_notify_new_block(struct lightningd *ld UNNEEDED,
u32 block_height UNNEEDED)
Expand Down
6 changes: 6 additions & 0 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
bool deprecated_apis = false;

/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for bitcoind_gettxout */
void bitcoind_gettxout(struct bitcoind *bitcoind UNNEEDED,
const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED,
Expand Down
6 changes: 6 additions & 0 deletions lightningd/test/run-jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include "../json.c"

/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for db_begin_transaction_ */
void db_begin_transaction_(struct db *db UNNEEDED, const char *location UNNEEDED)
{ fprintf(stderr, "db_begin_transaction_ called!\n"); abort(); }
Expand Down
1 change: 1 addition & 0 deletions onchaind/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ $(LIGHTNINGD_ONCHAIN_OBJS): $(LIGHTNINGD_HEADERS)
# Common source we use.
ONCHAIND_COMMON_OBJS := \
common/amount.o \
common/bigsize.o \
common/bip32.o \
common/daemon.o \
common/daemon_conn.o \
Expand Down
1 change: 1 addition & 0 deletions openingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ LIGHTNINGD_HEADERS_NOGEN += $(LIGHTNINGD_OPENING_HEADERS_NOGEN)
OPENINGD_COMMON_OBJS := \
common/amount.o \
common/base32.o \
common/bigsize.o \
common/bip32.o \
common/channel_config.o \
common/crypto_state.o \
Expand Down
1 change: 1 addition & 0 deletions plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ PLUGIN_COMMON_OBJS := \
common/amount.o \
common/bech32.o \
common/bech32_util.o \
common/bigsize.o \
common/bolt11.o \
common/daemon.o \
common/hash_u5.o \
Expand Down
6 changes: 6 additions & 0 deletions wallet/test/run-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, bool ca
#include <unistd.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

static char *db_err;
Expand Down
6 changes: 6 additions & 0 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ const struct chainparams *get_chainparams(const struct lightningd *ld)
}

/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for bitcoind_gettxout */
void bitcoind_gettxout(struct bitcoind *bitcoind UNNEEDED,
const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED,
Expand Down
37 changes: 9 additions & 28 deletions wire/fromwire.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,37 +163,18 @@ bool fromwire_bool(const u8 **cursor, size_t *max)
return ret;
}

u64 fromwire_bigsize(const u8 **cursor, size_t *max)
bigsize_t fromwire_bigsize(const u8 **cursor, size_t *max)
{
u8 flag = fromwire_u8(cursor, max);
u64 ret;
bigsize_t v;
size_t len = bigsize_get(*cursor, *max, &v);

switch(flag) {
case 0xff:
ret = fromwire_u64(cursor, max);
if ((ret >> 32) == 0) {
SUPERVERBOSE("not minimal encoded");
fromwire_fail(cursor, max);
}
break;
case 0xfe:
ret = fromwire_u32(cursor, max);
if ((ret >> 16) == 0) {
SUPERVERBOSE("not minimal encoded");
fromwire_fail(cursor, max);
}
break;
case 0xfd:
ret = fromwire_u16(cursor, max);
if (ret < 0xfd) {
SUPERVERBOSE("not minimal encoded");
fromwire_fail(cursor, max);
}
break;
default:
ret = flag;
if (len == 0) {
fromwire_fail(cursor, max);
return 0;
}
return ret;
assert(len <= *max);
fromwire(cursor, max, NULL, len);
return v;
}

void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey)
Expand Down
6 changes: 6 additions & 0 deletions wire/test/run-peer-wire.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
secp256k1_context *secp256k1_ctx;

/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

/* memsetting pubkeys doesn't work */
Expand Down
4 changes: 4 additions & 0 deletions wire/test/run-tlvstream.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include <ccan/array_size/array_size.h>
#include <ccan/str/hex/hex.h>
#include <common/utils.h>
#include <stdio.h>
#include <wally_core.h>

#include <common/bigsize.c>

static const char *reason;
#undef SUPERVERBOSE
#define SUPERVERBOSE(r) do { reason = (r); } while(0)

#include <wire/tlvstream.c>
Expand Down
21 changes: 7 additions & 14 deletions wire/towire.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,13 @@ void towire_bool(u8 **pptr, bool v)
towire(pptr, &val, sizeof(val));
}

void towire_bigsize(u8 **pptr, const u64 val)
{
if (val < 0xfd) {
towire_u8(pptr, val);
} else if (val <= 0xffff) {
towire_u8(pptr, 0xfd);
towire_u16(pptr, val);
} else if (val <= 0xffffffff) {
towire_u8(pptr, 0xfe);
towire_u32(pptr, val);
} else {
towire_u8(pptr, 0xff);
towire_u64(pptr, val);
}
void towire_bigsize(u8 **pptr, const bigsize_t val)
{
u8 buf[BIGSIZE_MAX_LEN];
size_t len;

len = bigsize_put(buf, val);
towire(pptr, buf, len);
}

void towire_pubkey(u8 **pptr, const struct pubkey *pubkey)
Expand Down
4 changes: 2 additions & 2 deletions wire/wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void towire_tu64(u8 **pptr, u64 v);
void towire_double(u8 **pptr, const double *v);
void towire_pad(u8 **pptr, size_t num);
void towire_bool(u8 **pptr, bool v);
void towire_bigsize(u8 **pptr, const u64 val);
void towire_bigsize(u8 **pptr, const bigsize_t val);

void towire_u8_array(u8 **pptr, const u8 *arr, size_t num);

Expand All @@ -99,7 +99,7 @@ u32 fromwire_tu32(const u8 **cursor, size_t *max);
u64 fromwire_tu64(const u8 **cursor, size_t *max);
void fromwire_double(const u8 **cursor, size_t *max, double *v);
bool fromwire_bool(const u8 **cursor, size_t *max);
u64 fromwire_bigsize(const u8 **cursor, size_t *max);
bigsize_t fromwire_bigsize(const u8 **cursor, size_t *max);
void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret);
void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey);
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey);
Expand Down

0 comments on commit cc70b9c

Please sign in to comment.