Skip to content

Commit 390878c

Browse files
committed
PSBT: tal_wally updates and docs
Default wally_tal_ctx to NULL, add extra asserts and tal_checks, and documentation explaning the usage of tal_wally_start/end. Changelog-None
1 parent d7f8fe9 commit 390878c

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

common/setup.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
static void *cln_wally_tal(size_t size)
1212
{
1313
assert(wally_tal_ctx);
14+
assert(tal_check(wally_tal_ctx, "cln_wally_tal ctx check"));
1415
return tal_arr_label(wally_tal_ctx, u8, size, "cln_wally_tal");
1516
}
1617

common/utils.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <errno.h>
1111
#include <locale.h>
1212

13-
const tal_t *wally_tal_ctx;
13+
const tal_t *wally_tal_ctx = NULL;
1414
secp256k1_context *secp256k1_ctx;
1515
const tal_t *tmpctx;
1616

@@ -35,8 +35,14 @@ void tal_wally_start(void)
3535
void tal_wally_end(const tal_t *parent)
3636
{
3737
tal_t *p;
38+
if (!wally_tal_ctx) {
39+
/* This makes valgrind show us backtraces! */
40+
*(u8 *)wally_tal_ctx = '\0';
41+
abort();
42+
}
3843
while ((p = tal_first(wally_tal_ctx)) != NULL) {
39-
/* Refuse to make a loop! */
44+
/* Refuse to make a loop!
45+
* Did you mean to use tal_wally_end_onto? */
4046
assert(p != parent);
4147
/* Don't steal backtrace from wally_tal_ctx! */
4248
if (tal_name(p) && streq(tal_name(p), "backtrace")) {
@@ -45,6 +51,7 @@ void tal_wally_end(const tal_t *parent)
4551
}
4652
tal_steal(parent, p);
4753
}
54+
assert(tal_check(wally_tal_ctx, "tal_wally_end ctx check"));
4855
wally_tal_ctx = tal_free(wally_tal_ctx);
4956
}
5057

common/utils.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,32 @@ void clean_tmpctx(void);
112112
/* Call this before any libwally function which allocates. */
113113
void tal_wally_start(void);
114114

115-
/* Then call this to reparent everything onto this parent */
115+
/* Then call this to reparent everything onto this parent.
116+
*
117+
* This method should *not* be used for creation of psbt objects,
118+
* tal_wally_end_onto should be used instead!
119+
*
120+
* Typical usage is to end on the PSBT being modified.
121+
*/
116122
void tal_wally_end(const tal_t *parent);
117123

118124
/* ... or this if you want to reparent onto something which is
119125
* allocated by libwally here. Fixes up this from_wally obj to have a
120-
* proper tal_name, too! */
126+
* proper tal_name, too!
127+
*
128+
* This results in a tal hierarchy like so:
129+
* parent -> from_wally -> [everything wally allocated]
130+
*
131+
* This is typically used when allocating a new psbt and ensuring all the psbt's
132+
* member objects are children of the PSBT itself.
133+
*
134+
* For example:
135+
* struct wally_psbt *clone;
136+
* tal_wally_start();
137+
* if (wally_psbt_clone_alloc(psbt, 0, &clone) != WALLY_OK)
138+
* abort();
139+
* tal_wally_end_onto(ctx, clone, struct wally_psbt);
140+
*/
121141
#define tal_wally_end_onto(parent, from_wally, type) \
122142
tal_wally_end_onto_( \
123143
(parent), (from_wally), \

0 commit comments

Comments
 (0)