From fbe50e087a7e2113a053dd8397428b048bf8c4f3 Mon Sep 17 00:00:00 2001 From: niftynei Date: Fri, 15 May 2020 17:13:22 -0500 Subject: [PATCH] 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 --- bitcoin/test/Makefile | 2 +- bitcoin/test/run-bitcoin_block_from_hex.c | 6 ++- bitcoin/test/run-tx-encode.c | 6 ++- channeld/Makefile | 1 + channeld/test/Makefile | 7 ++-- channeld/test/run-commit_tx.c | 12 +++--- channeld/test/run-full_channel.c | 11 ++--- closingd/Makefile | 1 + common/Makefile | 1 + common/daemon.c | 40 ++---------------- common/setup.c | 51 +++++++++++++++++++++++ common/setup.h | 7 ++++ common/test/Makefile | 1 + common/test/run-funding_tx.c | 12 ++---- connectd/Makefile | 1 + gossipd/Makefile | 1 + hsmd/Makefile | 1 + lightningd/Makefile | 1 + onchaind/Makefile | 1 + onchaind/test/Makefile | 1 + onchaind/test/run-grind_feerate-bug.c | 11 ++--- onchaind/test/run-grind_feerate.c | 9 ++-- openingd/Makefile | 1 + plugins/Makefile | 1 + wallet/test/Makefile | 1 + wallet/test/run-wallet.c | 11 ++--- 26 files changed, 111 insertions(+), 87 deletions(-) create mode 100644 common/setup.c create mode 100644 common/setup.h diff --git a/bitcoin/test/Makefile b/bitcoin/test/Makefile index d79186fff..799d16f54 100644 --- a/bitcoin/test/Makefile +++ b/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) diff --git a/bitcoin/test/run-bitcoin_block_from_hex.c b/bitcoin/test/run-bitcoin_block_from_hex.c index 6b7401754..7a88a423e 100644 --- a/bitcoin/test/run-bitcoin_block_from_hex.c +++ b/bitcoin/test/run-bitcoin_block_from_hex.c @@ -4,6 +4,7 @@ #include "../tx.c" #include "../varint.c" #include +#include /* 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; } diff --git a/bitcoin/test/run-tx-encode.c b/bitcoin/test/run-tx-encode.c index d11778a16..7336c11cc 100644 --- a/bitcoin/test/run-tx-encode.c +++ b/bitcoin/test/run-tx-encode.c @@ -4,6 +4,7 @@ #include #include #include +#include #include /* 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; } diff --git a/channeld/Makefile b/channeld/Makefile index fc563c9cb..e9cc0dc11 100644 --- a/channeld/Makefile +++ b/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 \ diff --git a/channeld/test/Makefile b/channeld/test/Makefile index ca74b9042..d143338fe 100644 --- a/channeld/test/Makefile +++ b/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/%) diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index 511ac8cc3..eb97787ea 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -15,6 +15,7 @@ static bool print_superverbose; #include #include #include +#include #include /* 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; diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 508046466..5a96fa09f 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -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; diff --git a/closingd/Makefile b/closingd/Makefile index dd709a355..8a36cf679 100644 --- a/closingd/Makefile +++ b/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 \ diff --git a/common/Makefile b/common/Makefile index a374bda05..bcce39002 100644 --- a/common/Makefile +++ b/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 \ diff --git a/common/daemon.c b/common/daemon.c index 29059bb59..4b76f9b65 100644 --- a/common/daemon.c +++ b/common/daemon.c @@ -7,18 +7,15 @@ #include #include #include +#include #include #include #include #include -#include #include #include #include #include -#include - -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[]) diff --git a/common/setup.c b/common/setup.c new file mode 100644 index 000000000..fa0462e95 --- /dev/null +++ b/common/setup.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include + +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); +} diff --git a/common/setup.h b/common/setup.h new file mode 100644 index 000000000..54dd86571 --- /dev/null +++ b/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 */ diff --git a/common/test/Makefile b/common/test/Makefile index aa52a2f7c..c30036686 100644 --- a/common/test/Makefile +++ b/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) diff --git a/common/test/run-funding_tx.c b/common/test/run-funding_tx.c index cdb7378fd..b826cbd6d 100644 --- a/common/test/run-funding_tx.c +++ b/common/test/run-funding_tx.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -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; } diff --git a/connectd/Makefile b/connectd/Makefile index e57178a9c..114a5ed8a 100644 --- a/connectd/Makefile +++ b/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 \ diff --git a/gossipd/Makefile b/gossipd/Makefile index c8df85f8a..f87405a63 100644 --- a/gossipd/Makefile +++ b/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 \ diff --git a/hsmd/Makefile b/hsmd/Makefile index ee89860e7..a5ab61eb3 100644 --- a/hsmd/Makefile +++ b/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 \ diff --git a/lightningd/Makefile b/lightningd/Makefile index 846af1314..77aa6438c 100644 --- a/lightningd/Makefile +++ b/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 \ diff --git a/onchaind/Makefile b/onchaind/Makefile index 23f31a350..274cb035c 100644 --- a/onchaind/Makefile +++ b/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 \ diff --git a/onchaind/test/Makefile b/onchaind/test/Makefile index 7d29d5250..ac7e5ae8d 100644 --- a/onchaind/test/Makefile +++ b/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 diff --git a/onchaind/test/run-grind_feerate-bug.c b/onchaind/test/run-grind_feerate-bug.c index 84c0c50bc..93c0f5f79 100644 --- a/onchaind/test/run-grind_feerate-bug.c +++ b/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 +#include #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(); } diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index 164c4bc3c..770e61438 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -1,5 +1,6 @@ #include #include +#include #include #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; } diff --git a/openingd/Makefile b/openingd/Makefile index f209ab523..425b8f804 100644 --- a/openingd/Makefile +++ b/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 \ diff --git a/plugins/Makefile b/plugins/Makefile index e4569e84b..8ddf63024 100644 --- a/plugins/Makefile +++ b/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 \ diff --git a/wallet/test/Makefile b/wallet/test/Makefile index c594429f4..f57ae8f70 100644 --- a/wallet/test/Makefile +++ b/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 \ diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 4b3e92df9..b101e1bf2 100644 --- a/wallet/test/run-wallet.c +++ b/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 #include #include +#include #include #include #include @@ -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; }