Skip to content

Commit

Permalink
setup: create a common setup which will handle the wally-context
Browse files Browse the repository at this point in the history
Since we now over-write the wally malloc/free functions, we need to do
so for tests as well. Here we pull up all of the common setup/teardown
logic into a separate place, and update the tests that use libwally to
use the new common_setup core

Changelog-None
  • Loading branch information
niftynei authored and rustyrussell committed May 19, 2020
1 parent c5a0d8c commit fbe50e0
Show file tree
Hide file tree
Showing 26 changed files with 111 additions and 87 deletions.
2 changes: 1 addition & 1 deletion bitcoin/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ BITCOIN_TEST_SRC := $(wildcard bitcoin/test/run-*.c)
BITCOIN_TEST_OBJS := $(BITCOIN_TEST_SRC:.c=.o)
BITCOIN_TEST_PROGRAMS := $(BITCOIN_TEST_OBJS:.o=)

BITCOIN_TEST_COMMON_OBJS := common/utils.o
BITCOIN_TEST_COMMON_OBJS := common/utils.o common/setup.o

$(BITCOIN_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_TEST_COMMON_OBJS) bitcoin/chainparams.o
$(BITCOIN_TEST_OBJS): $(CCAN_HEADERS) $(BITCOIN_HEADERS) $(BITCOIN_SRC)
Expand Down
6 changes: 4 additions & 2 deletions bitcoin/test/run-bitcoin_block_from_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../tx.c"
#include "../varint.c"
#include <assert.h>
#include <common/setup.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for amount_asset_is_main */
Expand Down Expand Up @@ -105,14 +106,14 @@ static const char block[] =

STRUCTEQ_DEF(sha256_double, 0, sha);

int main(void)
int main(int argc, const char *argv[])
{
struct bitcoin_blkid prev;
struct sha256_double merkle;
struct bitcoin_txid txid, expected_txid;
struct bitcoin_block *b;

setup_locale();
common_setup(argv[0]);
chainparams = chainparams_for_network("bitcoin");
b = bitcoin_block_from_hex(NULL, chainparams,
block, strlen(block));
Expand Down Expand Up @@ -146,5 +147,6 @@ int main(void)
assert(bitcoin_txid_eq(&txid, &expected_txid));

tal_free(b);
common_shutdown();
return 0;
}
6 changes: 4 additions & 2 deletions bitcoin/test/run-tx-encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <bitcoin/tx.c>
#include <bitcoin/varint.c>
#include <ccan/str/hex/hex.h>
#include <common/setup.h>
#include <common/utils.h>

/* AUTOGENERATED MOCKS START */
Expand Down Expand Up @@ -75,9 +76,9 @@ static void hexeq(const void *p, size_t len, const char *hex)
tal_free(tmphex);
}

int main(void)
int main(int argc, const char *argv[])
{
setup_locale();
common_setup(argv[0]);
chainparams = chainparams_for_network("bitcoin");

struct bitcoin_tx *tx;
Expand Down Expand Up @@ -118,5 +119,6 @@ int main(void)
"3b");

tal_free(tx);
common_shutdown();
return 0;
}
1 change: 1 addition & 0 deletions channeld/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ CHANNELD_COMMON_OBJS := \
common/ping.o \
common/pseudorand.o \
common/read_peer_msg.o \
common/setup.o \
common/sphinx.o \
common/status.o \
common/status_wire.o \
Expand Down
7 changes: 4 additions & 3 deletions channeld/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ CHANNELD_TEST_COMMON_OBJS := \
common/htlc_tx.o \
common/initial_commit_tx.o \
common/key_derive.o \
common/pseudorand.o \
common/msg_queue.o \
common/utils.o \
common/permute_tx.o \
common/pseudorand.o \
common/setup.o \
common/type_to_string.o \
common/permute_tx.o
common/utils.o

update-mocks: $(CHANNELD_TEST_SRC:%=update-mocks/%)

Expand Down
12 changes: 5 additions & 7 deletions channeld/test/run-commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static bool print_superverbose;
#include <common/amount.h>
#include <common/channel_id.h>
#include <common/key_derive.h>
#include <common/setup.h>
#include <common/status.h>

/* Turn this on to brute-force fee values */
Expand Down Expand Up @@ -447,9 +448,9 @@ static const struct htlc **invert_htlcs(const struct htlc **htlcs)
return inv;
}

int main(void)
int main(int argc, const char *argv[])
{
setup_locale();
common_setup(argv[0]);

struct bitcoin_txid funding_txid;
struct amount_sat funding_amount, dust_limit;
Expand Down Expand Up @@ -482,9 +483,6 @@ int main(void)
struct amount_msat to_local, to_remote;
const struct htlc **htlcs, **htlc_map, **htlc_map2, **inv_htlcs;

secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN);
setup_tmpctx();
chainparams = chainparams_for_network("bitcoin");

htlcs = setup_htlcs(tmpctx);
Expand Down Expand Up @@ -1008,9 +1006,9 @@ int main(void)
}

/* No memory leaks please */
secp256k1_context_destroy(secp256k1_ctx);
take_cleanup();
tal_free(tmpctx);
common_shutdown();


/* FIXME: Do BOLT comparison! */
return 0;
Expand Down
11 changes: 4 additions & 7 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <ccan/str/hex/hex.h>
#include <common/amount.h>
#include <common/channel_id.h>
#include <common/setup.h>
#include <common/sphinx.h>
#include <common/type_to_string.h>
#include <stdio.h>
Expand Down Expand Up @@ -351,9 +352,9 @@ static void update_feerate(struct channel *channel, u32 feerate)
assert(channel_feerate(channel, REMOTE) == feerate);
}

int main(void)
int main(int argc, const char *argv[])
{
setup_locale();
common_setup(argv[0]);

struct bitcoin_txid funding_txid;
/* We test from both sides. */
Expand All @@ -373,9 +374,6 @@ int main(void)
const u8 *funding_wscript, *funding_wscript_alt;
size_t i;

wally_init(0);
secp256k1_ctx = wally_get_secp_context();
setup_tmpctx();
chainparams = chainparams_for_network("bitcoin");

feerate_per_kw = tal_arr(tmpctx, u32, NUM_SIDES);
Expand Down Expand Up @@ -670,9 +668,8 @@ int main(void)
}

/* No memory leaks please */
wally_cleanup(0);
take_cleanup();
tal_free(tmpctx);
common_shutdown();

/* FIXME: Do BOLT comparison! */
return 0;
Expand Down
1 change: 1 addition & 0 deletions closingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ CLOSINGD_COMMON_OBJS := \
common/permute_tx.o \
common/pseudorand.o \
common/read_peer_msg.o \
common/setup.o \
common/socket_close.o \
common/status.o \
common/status_wire.o \
Expand Down
1 change: 1 addition & 0 deletions common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ COMMON_SRC_NOGEN := \
common/ping.c \
common/pseudorand.c \
common/read_peer_msg.c \
common/setup.c \
common/socket_close.c \
common/sphinx.c \
common/status.c \
Expand Down
40 changes: 3 additions & 37 deletions common/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
#include <ccan/tal/str/str.h>
#include <common/daemon.h>
#include <common/memleak.h>
#include <common/setup.h>
#include <common/status.h>
#include <common/utils.h>
#include <common/version.h>
#include <signal.h>
#include <sodium.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <wally_core.h>

static const tal_t *wally_tal_ctx;

#if BACKTRACE_SUPPORTED
static void (*bt_print)(const char *fmt, ...) PRINTF_FMT(1,2);
Expand Down Expand Up @@ -121,27 +118,11 @@ static void add_steal_notifiers(const tal_t *root)
}
#endif

static void *wally_tal(size_t size)
{
return tal_arr_label(wally_tal_ctx, u8, size, "wally_notleak");
}

static void wally_free(void *ptr)
{
tal_free(ptr);
}

static struct wally_operations wally_tal_ops = {
.malloc_fn = wally_tal,
.free_fn = wally_free,
};

void daemon_setup(const char *argv0,
void (*backtrace_print)(const char *fmt, ...),
void (*backtrace_exit)(void))
{
err_set_progname(argv0);

common_setup(argv0);
#if BACKTRACE_SUPPORTED
bt_print = backtrace_print;
bt_exit = backtrace_exit;
Expand All @@ -162,30 +143,15 @@ void daemon_setup(const char *argv0,
memleak_init();
#endif

/* We rely on libsodium for some of the crypto stuff, so we'd better
* not start if it cannot do its job correctly. */
if (sodium_init() == -1)
errx(1, "Could not initialize libsodium. Maybe not enough entropy"
" available ?");

/* We handle write returning errors! */
signal(SIGPIPE, SIG_IGN);

/* We set up Wally, the bitcoin wallet lib */
wally_tal_ctx = tal_label(NULL, char, "wally_ctx_notleak");
wally_init(0);
wally_set_operations(&wally_tal_ops);
secp256k1_ctx = wally_get_secp_context();

setup_tmpctx();
io_poll_override(daemon_poll);
}

void daemon_shutdown(void)
{
tal_free(tmpctx);
wally_cleanup(0);
wally_free(wally_tal_ctx);
common_shutdown();
}

void daemon_maybe_debug(char *argv[])
Expand Down
51 changes: 51 additions & 0 deletions common/setup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <ccan/ccan/err/err.h>
#include <common/memleak.h>
#include <common/setup.h>
#include <common/utils.h>
#include <sodium.h>
#include <wally_core.h>

static const tal_t *wally_tal_ctx;

static void *wally_tal(size_t size)
{
return tal_arr_label(wally_tal_ctx, u8, size, "wally_notleak");
}

static void wally_free(void *ptr)
{
tal_free(ptr);
}

static struct wally_operations wally_tal_ops = {
.malloc_fn = wally_tal,
.free_fn = wally_free,
};


void common_setup(const char *argv0)
{
setup_locale();
err_set_progname(argv0);

/* We rely on libsodium for some of the crypto stuff, so we'd better
* not start if it cannot do its job correctly. */
if (sodium_init() == -1)
errx(1, "Could not initialize libsodium. Maybe not enough entropy"
" available ?");

/* We set up Wally, the bitcoin wallet lib */
wally_tal_ctx = tal_label(NULL, char, "wally_ctx_notleak");
wally_init(0);
wally_set_operations(&wally_tal_ops);
secp256k1_ctx = wally_get_secp_context();

setup_tmpctx();
}

void common_shutdown(void)
{
tal_free(tmpctx);
wally_cleanup(0);
tal_free(wally_tal_ctx);
}
7 changes: 7 additions & 0 deletions common/setup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef LIGHTNING_COMMON_SETUP_H
#define LIGHTNING_COMMON_SETUP_H
#include "config.h"

void common_setup(const char *argv0);
void common_shutdown(void);
#endif /* LIGHTNING_COMMON_SETUP_H */
1 change: 1 addition & 0 deletions common/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ COMMON_TEST_OBJS := $(COMMON_TEST_SRC:.c=.o)
COMMON_TEST_PROGRAMS := $(COMMON_TEST_OBJS:.o=)

COMMON_TEST_COMMON_OBJS := \
common/setup.o \
common/utils.o

$(COMMON_TEST_PROGRAMS): $(COMMON_TEST_COMMON_OBJS) $(BITCOIN_OBJS)
Expand Down
12 changes: 4 additions & 8 deletions common/test/run-funding_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <bitcoin/privkey.h>
#include <bitcoin/pubkey.h>
#include <ccan/str/hex/hex.h>
#include <common/setup.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <inttypes.h>
Expand Down Expand Up @@ -112,9 +113,9 @@ static struct privkey privkey_from_hex(const char *hex)
}
#endif

int main(void)
int main(int argc, const char *argv[])
{
setup_locale();
common_setup(argv[0]);

struct bitcoin_tx *input, *funding;
struct amount_sat fee, change;
Expand All @@ -132,9 +133,6 @@ int main(void)
struct amount_sat tmpamt;
struct amount_asset asset;

secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN);
setup_tmpctx();
chainparams = chainparams_for_network("bitcoin");

/* BOLT #3:
Expand Down Expand Up @@ -225,8 +223,6 @@ int main(void)
tal_hex(tmpctx, linearize_tx(tmpctx, funding)));

/* No memory leaks please */
secp256k1_context_destroy(secp256k1_ctx);
tal_free(tmpctx);

common_shutdown();
return 0;
}
1 change: 1 addition & 0 deletions connectd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ CONNECTD_COMMON_OBJS := \
common/onionreply.o \
common/per_peer_state.o \
common/pseudorand.o \
common/setup.o \
common/status.o \
common/status_wire.o \
common/subdaemon.o \
Expand Down
1 change: 1 addition & 0 deletions gossipd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ GOSSIPD_COMMON_OBJS := \
common/per_peer_state.o \
common/ping.o \
common/pseudorand.o \
common/setup.o \
common/status.o \
common/status_wire.o \
common/subdaemon.o \
Expand Down
Loading

0 comments on commit fbe50e0

Please sign in to comment.