Browse Source

setup: create a common setup which will handle the wally-context

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
nifty/pset-pre
niftynei 5 years ago
committed by Rusty Russell
parent
commit
fbe50e087a
  1. 2
      bitcoin/test/Makefile
  2. 6
      bitcoin/test/run-bitcoin_block_from_hex.c
  3. 6
      bitcoin/test/run-tx-encode.c
  4. 1
      channeld/Makefile
  5. 7
      channeld/test/Makefile
  6. 12
      channeld/test/run-commit_tx.c
  7. 11
      channeld/test/run-full_channel.c
  8. 1
      closingd/Makefile
  9. 1
      common/Makefile
  10. 40
      common/daemon.c
  11. 51
      common/setup.c
  12. 7
      common/setup.h
  13. 1
      common/test/Makefile
  14. 12
      common/test/run-funding_tx.c
  15. 1
      connectd/Makefile
  16. 1
      gossipd/Makefile
  17. 1
      hsmd/Makefile
  18. 1
      lightningd/Makefile
  19. 1
      onchaind/Makefile
  20. 1
      onchaind/test/Makefile
  21. 11
      onchaind/test/run-grind_feerate-bug.c
  22. 9
      onchaind/test/run-grind_feerate.c
  23. 1
      openingd/Makefile
  24. 1
      plugins/Makefile
  25. 1
      wallet/test/Makefile
  26. 11
      wallet/test/run-wallet.c

2
bitcoin/test/Makefile

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

6
bitcoin/test/run-bitcoin_block_from_hex.c

@ -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 */
@ -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));
@ -146,5 +147,6 @@ int main(void)
assert(bitcoin_txid_eq(&txid, &expected_txid));
tal_free(b);
common_shutdown();
return 0;
}

6
bitcoin/test/run-tx-encode.c

@ -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 */
@ -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;
@ -118,5 +119,6 @@ int main(void)
"3b");
tal_free(tx);
common_shutdown();
return 0;
}

1
channeld/Makefile

@ -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 \

7
channeld/test/Makefile

@ -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/%)

12
channeld/test/run-commit_tx.c

@ -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 */
@ -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;
@ -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);
@ -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;

11
channeld/test/run-full_channel.c

@ -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>
@ -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. */
@ -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);
@ -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;

1
closingd/Makefile

@ -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 \

1
common/Makefile

@ -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 \

40
common/daemon.c

@ -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);
@ -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;
@ -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[])

51
common/setup.c

@ -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
common/setup.h

@ -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
common/test/Makefile

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

12
common/test/run-funding_tx.c

@ -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>
@ -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;
@ -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:
@ -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
connectd/Makefile

@ -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 \

1
gossipd/Makefile

@ -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 \

1
hsmd/Makefile

@ -28,6 +28,7 @@ HSMD_COMMON_OBJS := \
common/msg_queue.o \
common/node_id.o \
common/permute_tx.o \
common/setup.o \
common/status.o \
common/status_wire.o \
common/subdaemon.o \

1
lightningd/Makefile

@ -60,6 +60,7 @@ LIGHTNINGD_COMMON_OBJS := \
common/per_peer_state.o \
common/permute_tx.o \
common/pseudorand.o \
common/setup.o \
common/sphinx.o \
common/status_wire.o \
common/timeout.o \

1
onchaind/Makefile

@ -68,6 +68,7 @@ ONCHAIND_COMMON_OBJS := \
common/onionreply.o \
common/peer_billboard.o \
common/permute_tx.o \
common/setup.o \
common/status.o \
common/status_wire.o \
common/subdaemon.o \

1
onchaind/test/Makefile

@ -10,6 +10,7 @@ ONCHAIND_TEST_COMMON_OBJS := \
common/amount.o \
common/features.o \
common/pseudorand.o \
common/setup.o \
common/type_to_string.o \
common/utils.o

11
onchaind/test/run-grind_feerate-bug.c

@ -3,6 +3,7 @@
No valid signature found for 3 htlc_timeout_txs feerate 10992-15370, last tx 0200000001a02a38c6ec5541963704a2a035b3094b18d69cc25cc7419d75e02894618329720000000000000000000191ea3000000000002200208bfadb3554f41cc06f00de0ec2e2f91e36ee45b5006a1f606146784755356ba532f10800, input 3215967sat, signature 3045022100917efdc8577e8578aef5e513fad25edbb55921466e8ffccb05ce8bb05a54ae6902205c2fded9d7bfc290920821bfc828720bc24287f3dad9a62fb4f806e2404ed0f401, cltvs 585998/585998/586034 wscripts 76a914f454b1fe5b95428d6beec58ed3131a6ea611b2fa8763ac672103f83ca95b22920e71487736a7284696dd52443fd8f7ce683153ac31d1d1db7da67c820120876475527c21026ebaa1d08757b86110e40e3f4a081803eec694e23ec75ee0bfd753589df896e752ae67a9148dbcec4a5d782dd87588801607ea7dfc8874ffee88ac6868/76a914f454b1fe5b95428d6beec58ed3131a6ea611b2fa8763ac672103f83ca95b22920e71487736a7284696dd52443fd8f7ce683153ac31d1d1db7da67c820120876475527c21026ebaa1d08757b86110e40e3f4a081803eec694e23ec75ee0bfd753589df896e752ae67a9148dbcec4a5d782dd87588801607ea7dfc8874ffee88ac6868/76a914f454b1fe5b95428d6beec58ed3131a6ea611b2fa8763ac672103f83ca95b22920e71487736a7284696dd52443fd8f7ce683153ac31d1d1db7da67c820120876475527c21026ebaa1d08757b86110e40e3f4a081803eec694e23ec75ee0bfd753589df896e752ae67a9148dbcec4a5d782dd87588801607ea7dfc8874ffee88ac6868 (version v0.7.1-57-gb3215a8)"
*/
#include <ccan/str/hex/hex.h>
#include <common/setup.h>
#define main test_main
int test_main(int argc, char *argv[]);
@ -352,7 +353,7 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
return tx;
}
int main(void)
int main(int argc, char *argv[])
{
struct bitcoin_signature remotesig;
struct tracked_output *out;
@ -361,10 +362,7 @@ int main(void)
struct htlc_stub htlcs[3];
u8 *htlc_scripts[3];
setup_locale();
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN);
setup_tmpctx();
common_setup(argv[0]);
chainparams = chainparams_for_network("bitcoin");
htlcs[0].cltv_expiry = 585998;
@ -409,6 +407,5 @@ int main(void)
false);
assert(ret == 2);
take_cleanup();
tal_free(tmpctx);
secp256k1_context_destroy(secp256k1_ctx);
common_shutdown();
}

9
onchaind/test/run-grind_feerate.c

@ -1,5 +1,6 @@
#include <ccan/array_size/array_size.h>
#include <ccan/time/time.h>
#include <common/setup.h>
#include <common/status.h>
#undef status_debug
@ -308,7 +309,7 @@ bool wire_sync_write(int fd UNNEEDED, const void *msg TAKES UNNEEDED)
int main(int argc, char *argv[])
{
setup_locale();
common_setup(argv[0]);
struct bitcoin_tx *tx;
struct bitcoin_signature sig;
@ -319,9 +320,6 @@ int main(int argc, char *argv[])
struct timeabs start, end;
int iterations = 1000;
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN);
setup_tmpctx();
chainparams = chainparams_for_network("bitcoin");
tx = bitcoin_tx_from_hex(tmpctx, "0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000",
strlen("0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000"));
@ -360,7 +358,6 @@ int main(int argc, char *argv[])
time_to_msec(time_between(end, start)),
time_to_nsec(time_divide(time_between(end, start), iterations)));
tal_free(tmpctx);
secp256k1_context_destroy(secp256k1_ctx);
common_shutdown();
return 0;
}

1
openingd/Makefile

@ -72,6 +72,7 @@ OPENINGD_COMMON_OBJS := \
common/permute_tx.o \
common/pseudorand.o \
common/read_peer_msg.o \
common/setup.o \
common/status.o \
common/status_wire.o \
common/subdaemon.o \

1
plugins/Makefile

@ -45,6 +45,7 @@ PLUGIN_COMMON_OBJS := \
common/node_id.o \
common/param.o \
common/pseudorand.o \
common/setup.o \
common/type_to_string.o \
common/utils.o \
common/version.o \

1
wallet/test/Makefile

@ -15,6 +15,7 @@ WALLET_TEST_COMMON_OBJS := \
common/onionreply.o \
common/key_derive.o \
common/pseudorand.o \
common/setup.o \
common/timeout.o \
common/utils.o \
common/wireaddr.o \

11
wallet/test/run-wallet.c

@ -22,6 +22,7 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const s
#include <common/amount.h>
#include <common/errcode.h>
#include <common/memleak.h>
#include <common/setup.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
@ -1467,17 +1468,14 @@ static bool test_wallet_payment_status_enum(void)
return true;
}
int main(void)
int main(int argc, const char *argv[])
{
setup_locale();
common_setup(argv[0]);
chainparams = chainparams_for_network("bitcoin");
bool ok = true;
struct lightningd *ld;
setup_tmpctx();
wally_init(0);
secp256k1_ctx = wally_get_secp_context();
ld = tal(tmpctx, struct lightningd);
ld->config = test_config;
@ -1499,9 +1497,8 @@ int main(void)
/* Do not clean up in the case of an error, we might want to debug the
* database. */
if (ok) {
tal_free(tmpctx);
take_cleanup();
common_shutdown();
}
wally_cleanup(0);
return !ok;
}

Loading…
Cancel
Save