core-lightning/common/setup.c
Rusty Russell d5cb0d85b5 utils: use a cleaner pattern to capture wally allocations.
We force use of tal_wally_start/tal_wally_end around every wally
allocation, and with "end" make the caller choose where to reparent
everything.

This is particularly powerful where we allocate a tx or a psbt: we
want that tx or psbt to be the parent of the other allocations, so
this way we can reparent the tx or psbt, then reparent everything
else onto it.

Implementing psbt_finalize (which uses a behavior flag antipattern)
was tricky, so I ended up splitting that into 'psbt_finalize' and
'psbt_final_tx', which I think also makes the callers clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-09-23 14:45:53 +02:00

51 lines
1.0 KiB
C

#include <assert.h>
#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 void *wally_tal(size_t size)
{
assert(wally_tal_ctx);
return tal_arr_label(wally_tal_ctx, u8, size, "wally_tal");
}
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_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);
}