From cf28de7e12010fe18c6eb76aa80cc942de37ed19 Mon Sep 17 00:00:00 2001 From: Salvatore Ingala <6681844+bigspider@users.noreply.github.com> Date: Wed, 10 Jan 2024 14:27:20 +0100 Subject: [PATCH] Refactor: added generic macro to define types and helpers for relative pointers --- src/common/wallet.c | 184 +++++++++++++++++---------------------- src/common/wallet.h | 137 +++++++++++++++-------------- src/handler/lib/policy.c | 81 +++++++++-------- 3 files changed, 192 insertions(+), 210 deletions(-) diff --git a/src/common/wallet.c b/src/common/wallet.c index f8e7810c6..50f907713 100644 --- a/src/common/wallet.c +++ b/src/common/wallet.c @@ -508,7 +508,7 @@ static int parse_script(buffer_t *in_buf, static int parse_child_scripts(buffer_t *in_buf, buffer_t *out_buf, size_t depth, - ptr_rel_node_t child_scripts[], + rptr_policy_node_t child_scripts[], int n_children, int version, unsigned int context_flags) { @@ -517,7 +517,7 @@ static int parse_child_scripts(buffer_t *in_buf, for (int child_index = 0; child_index < n_children; child_index++) { buffer_alloc(out_buf, 0, true); // ensure alignment of current pointer - init_relative_node_ptr(&child_scripts[child_index], buffer_get_cur(out_buf)); + i_policy_node(&child_scripts[child_index], buffer_get_cur(out_buf)); if (0 > parse_script(in_buf, out_buf, version, depth + 1, context_flags)) { // failed while parsing internal script @@ -616,7 +616,7 @@ static int parse_script(buffer_t *in_buf, } if (inner_wrapper != NULL) { - init_relative_node_ptr(&inner_wrapper->script, node); + i_policy_node(&inner_wrapper->script, node); } inner_wrapper = node; } @@ -738,7 +738,7 @@ static int parse_script(buffer_t *in_buf, // the internal script is recursively parsed (if successful) in the current location // of the output buffer buffer_alloc(out_buf, 0, true); // ensure alignment of current pointer - init_relative_node_ptr(&node->script, buffer_get_cur(out_buf)); + i_policy_node(&node->script, buffer_get_cur(out_buf)); if (0 > parse_script(in_buf, out_buf, version, depth + 1, inner_context_flags)) { // failed while parsing internal script @@ -822,7 +822,7 @@ static int parse_script(buffer_t *in_buf, } for (int i = 0; i < 3; i++) { - if (!resolve_node_ptr(&node->scripts[i])->flags.is_miniscript) { + if (!r_policy_node(&node->scripts[i])->flags.is_miniscript) { return WITH_ERROR(-1, "children of andor must be miniscript"); } } @@ -830,9 +830,9 @@ static int parse_script(buffer_t *in_buf, // andor(X, Y, Z) // X is Bdu; Y and Z are both B, K, or V - const policy_node_t *X = resolve_node_ptr(&node->scripts[0]); - const policy_node_t *Y = resolve_node_ptr(&node->scripts[1]); - const policy_node_t *Z = resolve_node_ptr(&node->scripts[2]); + const policy_node_t *X = r_policy_node(&node->scripts[0]); + const policy_node_t *Y = r_policy_node(&node->scripts[1]); + const policy_node_t *Z = r_policy_node(&node->scripts[2]); if (X->flags.miniscript_type != MINISCRIPT_TYPE_B || !X->flags.miniscript_mod_d || !X->flags.miniscript_mod_u) { @@ -885,13 +885,13 @@ static int parse_script(buffer_t *in_buf, return -1; } - if (!resolve_node_ptr(&node->scripts[0])->flags.is_miniscript || - !resolve_node_ptr(&node->scripts[1])->flags.is_miniscript) { + if (!r_policy_node(&node->scripts[0])->flags.is_miniscript || + !r_policy_node(&node->scripts[1])->flags.is_miniscript) { return WITH_ERROR(-1, "children of and_v must be miniscript"); } - const policy_node_t *X = resolve_node_ptr(&node->scripts[0]); - const policy_node_t *Y = resolve_node_ptr(&node->scripts[1]); + const policy_node_t *X = r_policy_node(&node->scripts[0]); + const policy_node_t *Y = r_policy_node(&node->scripts[1]); // and_v(X,Y) // X is V; Y is B, K, or V @@ -944,13 +944,13 @@ static int parse_script(buffer_t *in_buf, return -1; } - if (!resolve_node_ptr(&node->scripts[0])->flags.is_miniscript || - !resolve_node_ptr(&node->scripts[1])->flags.is_miniscript) { + if (!r_policy_node(&node->scripts[0])->flags.is_miniscript || + !r_policy_node(&node->scripts[1])->flags.is_miniscript) { return WITH_ERROR(-1, "children of and_b must be miniscript"); } - const policy_node_t *X = resolve_node_ptr(&node->scripts[0]); - const policy_node_t *Y = resolve_node_ptr(&node->scripts[1]); + const policy_node_t *X = r_policy_node(&node->scripts[0]); + const policy_node_t *Y = r_policy_node(&node->scripts[1]); // and_b(X,Y) // X is B; Y is W @@ -1000,16 +1000,16 @@ static int parse_script(buffer_t *in_buf, return -1; } - if (!resolve_node_ptr(&node->scripts[0])->flags.is_miniscript || - !resolve_node_ptr(&node->scripts[1])->flags.is_miniscript) { + if (!r_policy_node(&node->scripts[0])->flags.is_miniscript || + !r_policy_node(&node->scripts[1])->flags.is_miniscript) { return WITH_ERROR(-1, "children of and_n must be miniscript"); } // and_n(X, Y) is equivalent to andor(X, Y, 0) // X is Bdu; Y is B - const policy_node_t *X = resolve_node_ptr(&node->scripts[0]); - const policy_node_t *Y = resolve_node_ptr(&node->scripts[1]); + const policy_node_t *X = r_policy_node(&node->scripts[0]); + const policy_node_t *Y = r_policy_node(&node->scripts[1]); if (X->flags.miniscript_type != MINISCRIPT_TYPE_B || !X->flags.miniscript_mod_d || !X->flags.miniscript_mod_u) { @@ -1055,16 +1055,16 @@ static int parse_script(buffer_t *in_buf, return -1; } - if (!resolve_node_ptr(&node->scripts[0])->flags.is_miniscript || - !resolve_node_ptr(&node->scripts[1])->flags.is_miniscript) { + if (!r_policy_node(&node->scripts[0])->flags.is_miniscript || + !r_policy_node(&node->scripts[1])->flags.is_miniscript) { return WITH_ERROR(-1, "children of or_b must be miniscript"); } // or_b(X, Z) // X is Bd; Z is Wd - const policy_node_t *X = resolve_node_ptr(&node->scripts[0]); - const policy_node_t *Z = resolve_node_ptr(&node->scripts[1]); + const policy_node_t *X = r_policy_node(&node->scripts[0]); + const policy_node_t *Z = r_policy_node(&node->scripts[1]); if (X->flags.miniscript_type != MINISCRIPT_TYPE_B || !X->flags.miniscript_mod_d) { return WITH_ERROR(-1, "invalid type"); @@ -1111,16 +1111,16 @@ static int parse_script(buffer_t *in_buf, return -1; } - if (!resolve_node_ptr(&node->scripts[0])->flags.is_miniscript || - !resolve_node_ptr(&node->scripts[1])->flags.is_miniscript) { + if (!r_policy_node(&node->scripts[0])->flags.is_miniscript || + !r_policy_node(&node->scripts[1])->flags.is_miniscript) { return WITH_ERROR(-1, "children of or_c must be miniscript"); } // or_c(X, Z) // X is Bdu; Z is V - const policy_node_t *X = resolve_node_ptr(&node->scripts[0]); - const policy_node_t *Z = resolve_node_ptr(&node->scripts[1]); + const policy_node_t *X = r_policy_node(&node->scripts[0]); + const policy_node_t *Z = r_policy_node(&node->scripts[1]); if (X->flags.miniscript_type != MINISCRIPT_TYPE_B || !X->flags.miniscript_mod_d || !X->flags.miniscript_mod_u) { @@ -1165,16 +1165,16 @@ static int parse_script(buffer_t *in_buf, return -1; } - if (!resolve_node_ptr(&node->scripts[0])->flags.is_miniscript || - !resolve_node_ptr(&node->scripts[1])->flags.is_miniscript) { + if (!r_policy_node(&node->scripts[0])->flags.is_miniscript || + !r_policy_node(&node->scripts[1])->flags.is_miniscript) { return WITH_ERROR(-1, "children of or_d must be miniscript"); } // or_d(X, Z) // X is Bdu; Z is B - const policy_node_t *X = resolve_node_ptr(&node->scripts[0]); - const policy_node_t *Z = resolve_node_ptr(&node->scripts[1]); + const policy_node_t *X = r_policy_node(&node->scripts[0]); + const policy_node_t *Z = r_policy_node(&node->scripts[1]); if (X->flags.miniscript_type != MINISCRIPT_TYPE_B || !X->flags.miniscript_mod_d || !X->flags.miniscript_mod_u) { @@ -1219,16 +1219,16 @@ static int parse_script(buffer_t *in_buf, return -1; } - if (!resolve_node_ptr(&node->scripts[0])->flags.is_miniscript || - !resolve_node_ptr(&node->scripts[1])->flags.is_miniscript) { + if (!r_policy_node(&node->scripts[0])->flags.is_miniscript || + !r_policy_node(&node->scripts[1])->flags.is_miniscript) { return WITH_ERROR(-1, "children of or_i must be miniscript"); } // or_i(X, Z) // both are B, K, or V - const policy_node_t *X = resolve_node_ptr(&node->scripts[0]); - const policy_node_t *Z = resolve_node_ptr(&node->scripts[1]); + const policy_node_t *X = r_policy_node(&node->scripts[0]); + const policy_node_t *Z = r_policy_node(&node->scripts[1]); if (X->flags.miniscript_type == MINISCRIPT_TYPE_W) { return WITH_ERROR(-1, "invalid type"); // must be B, K or V @@ -1294,26 +1294,24 @@ static int parse_script(buffer_t *in_buf, ++node->n; // parse a script into cur->script buffer_alloc(out_buf, 0, true); // ensure alignment of current pointer - init_relative_node_ptr(&cur->script, buffer_get_cur(out_buf)); + i_policy_node(&cur->script, buffer_get_cur(out_buf)); if (0 > parse_script(in_buf, out_buf, version, depth + 1, context_flags)) { // failed while parsing internal script return -1; } - if (!resolve_node_ptr(&cur->script)->flags.is_miniscript) { + if (!r_policy_node(&cur->script)->flags.is_miniscript) { return WITH_ERROR(-1, "children of thresh must be miniscript"); } if (node->n == 1) { // the first child's type must be B - if (resolve_node_ptr(&cur->script)->flags.miniscript_type != - MINISCRIPT_TYPE_B) { + if (r_policy_node(&cur->script)->flags.miniscript_type != MINISCRIPT_TYPE_B) { return WITH_ERROR(-1, "the first children of thresh must be of type B"); } } else { // every other child's type must be W - if (resolve_node_ptr(&cur->script)->flags.miniscript_type != - MINISCRIPT_TYPE_W) { + if (r_policy_node(&cur->script)->flags.miniscript_type != MINISCRIPT_TYPE_W) { return WITH_ERROR( -1, "each child of thresh (except the first) must be of type W"); @@ -1321,15 +1319,15 @@ static int parse_script(buffer_t *in_buf, } // all children must have properties du - if (!resolve_node_ptr(&cur->script)->flags.miniscript_mod_d || - !resolve_node_ptr(&cur->script)->flags.miniscript_mod_u) { + if (!r_policy_node(&cur->script)->flags.miniscript_mod_d || + !r_policy_node(&cur->script)->flags.miniscript_mod_u) { return WITH_ERROR(-1, "each child of thresh must have properties d and u"); } - if (resolve_node_ptr(&cur->script)->flags.miniscript_mod_z) { + if (r_policy_node(&cur->script)->flags.miniscript_mod_z) { ++count_z; } - if (resolve_node_ptr(&cur->script)->flags.miniscript_mod_o) { + if (r_policy_node(&cur->script)->flags.miniscript_mod_o) { ++count_o; } @@ -1385,7 +1383,7 @@ static int parse_script(buffer_t *in_buf, if (key_placeholder == NULL) { return WITH_ERROR(-1, "Out of memory"); } - init_relative_node_key_placeholder_ptr(&node->key_placeholder, key_placeholder); + i_policy_node_key_placeholder(&node->key_placeholder, key_placeholder); if (token == TOKEN_WPKH) { if (depth > 0 && ((context_flags & CONTEXT_WITHIN_SH) == 0)) { @@ -1465,7 +1463,7 @@ static int parse_script(buffer_t *in_buf, if (key_placeholder == NULL) { return WITH_ERROR(-1, "Out of memory"); } - init_relative_node_key_placeholder_ptr(&node->key_placeholder, key_placeholder); + i_policy_node_key_placeholder(&node->key_placeholder, key_placeholder); if (0 > parse_placeholder(in_buf, version, key_placeholder)) { return WITH_ERROR(-1, "Couldn't parse key placeholder"); @@ -1582,8 +1580,7 @@ static int parse_script(buffer_t *in_buf, // We allocate the array of key indices at the current position in the output buffer // (on success) buffer_alloc(out_buf, 0, true); // ensure alignment of current pointer - init_relative_node_key_placeholder_ptr(&node->key_placeholders, - buffer_get_cur(out_buf)); + i_policy_node_key_placeholder(&node->key_placeholders, buffer_get_cur(out_buf)); node->n = 0; while (true) { @@ -1660,7 +1657,7 @@ static int parse_script(buffer_t *in_buf, // if there was one or more wrappers, the script of the most internal node must point // to the parsed node if (inner_wrapper != NULL) { - init_relative_node_ptr(&inner_wrapper->script, parsed_node); + i_policy_node(&inner_wrapper->script, parsed_node); } // Validate and compute the flags (miniscript type and modifiers) for all the wrapper, if any @@ -1672,14 +1669,14 @@ static int parse_script(buffer_t *in_buf, // find the actual node by traversing the list policy_node_with_script_t *node = (policy_node_with_script_t *) outermost_node; for (int j = 0; j < i; j++) { - node = (policy_node_with_script_t *) resolve_node_ptr(&node->script); + node = (policy_node_with_script_t *) r_policy_node(&node->script); } - if (!resolve_node_ptr(&node->script)->flags.is_miniscript) { + if (!r_policy_node(&node->script)->flags.is_miniscript) { return WITH_ERROR(-1, "wrappers can only be applied to miniscript"); } - const policy_node_t *X = resolve_node_ptr(&node->script); + const policy_node_t *X = r_policy_node(&node->script); uint8_t X_type = X->flags.miniscript_type; @@ -1866,7 +1863,7 @@ static int parse_tree(buffer_t *in_buf, buffer_t *out_buf, int version, size_t d tree_node->is_leaf = true; buffer_alloc(out_buf, 0, true); // ensure alignment of current pointer - init_relative_node_ptr(&tree_node->script, buffer_get_cur(out_buf)); + i_policy_node(&tree_node->script, buffer_get_cur(out_buf)); if (0 > parse_script(in_buf, out_buf, version, depth + 1, CONTEXT_WITHIN_TR)) { return -1; } @@ -1922,7 +1919,7 @@ int get_policy_segwit_version(const policy_node_t *policy) { return 1; } else if (policy->type == TOKEN_SH) { const policy_node_t *inner = - resolve_node_ptr(&((const policy_node_with_script_t *) policy)->script); + r_policy_node(&((const policy_node_with_script_t *) policy)->script); if (inner->type == TOKEN_WPKH || inner->type == TOKEN_WSH) { return 0; // wrapped segwit } else { @@ -1991,8 +1988,7 @@ static int compute_thresh_ops(const policy_node_thresh_t *node, while (cur != NULL) { policy_node_ext_info_t t; - if (0 > compute_miniscript_policy_ext_info(resolve_node_ptr(&cur->script), &t, ctx)) - return -1; + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&cur->script), &t, ctx)) return -1; out->count += t.ops.count + 1; @@ -2038,8 +2034,7 @@ static int compute_thresh_stacksize(const policy_node_thresh_t *node, while (cur != NULL) { policy_node_ext_info_t t; - if (0 > compute_miniscript_policy_ext_info(resolve_node_ptr(&cur->script), &t, ctx)) - return -1; + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&cur->script), &t, ctx)) return -1; next_sats[0] = sumcheck(sats[0], t.ss.dsat); for (int j = 1; j < sats_size; j++) { @@ -2234,14 +2229,11 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, policy_node_ext_info_t y; policy_node_ext_info_t z; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[0]), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[0]), &x, ctx)) return -1; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[1]), &y, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[1]), &y, ctx)) return -1; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[2]), &z, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[2]), &z, ctx)) return -1; out->s = z.s & (x.s | y.s); @@ -2277,11 +2269,9 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, policy_node_ext_info_t x; policy_node_ext_info_t y; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[0]), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[0]), &x, ctx)) return -1; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[1]), &y, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[1]), &y, ctx)) return -1; out->s = x.s | y.s; @@ -2313,11 +2303,9 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, policy_node_ext_info_t x; policy_node_ext_info_t y; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[0]), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[0]), &x, ctx)) return -1; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[1]), &y, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[1]), &y, ctx)) return -1; out->s = x.s | y.s; @@ -2351,11 +2339,9 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, policy_node_ext_info_t x; policy_node_ext_info_t y; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[0]), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[0]), &x, ctx)) return -1; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[1]), &y, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[1]), &y, ctx)) return -1; out->s = x.s | y.s; @@ -2386,11 +2372,9 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, policy_node_ext_info_t x; policy_node_ext_info_t z; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[0]), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[0]), &x, ctx)) return -1; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[1]), &z, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[1]), &z, ctx)) return -1; out->s = x.s & z.s; @@ -2422,11 +2406,9 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, policy_node_ext_info_t x; policy_node_ext_info_t z; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[0]), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[0]), &x, ctx)) return -1; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[1]), &z, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[1]), &z, ctx)) return -1; out->s = x.s & z.s; @@ -2456,11 +2438,9 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, policy_node_ext_info_t x; policy_node_ext_info_t z; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[0]), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[0]), &x, ctx)) return -1; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[1]), &z, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[1]), &z, ctx)) return -1; out->s = x.s & z.s; @@ -2491,11 +2471,9 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, policy_node_ext_info_t x; policy_node_ext_info_t z; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[0]), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[0]), &x, ctx)) return -1; - if (0 > - compute_miniscript_policy_ext_info(resolve_node_ptr(&node->scripts[1]), &z, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->scripts[1]), &z, ctx)) return -1; out->s = x.s & z.s; @@ -2536,7 +2514,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, ++n_children; policy_node_ext_info_t t; - if (0 > compute_miniscript_policy_ext_info(resolve_node_ptr(&cur->script), &t, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&cur->script), &t, ctx)) return -1; if (t.e) { @@ -2586,7 +2564,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, const policy_node_with_script_t *node = (const policy_node_with_script_t *) policy_node; policy_node_ext_info_t x; - if (0 > compute_miniscript_policy_ext_info(resolve_node_ptr(&node->script), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->script), &x, ctx)) return -1; out->s = x.s; @@ -2613,7 +2591,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, const policy_node_with_script_t *node = (const policy_node_with_script_t *) policy_node; policy_node_ext_info_t x; - if (0 > compute_miniscript_policy_ext_info(resolve_node_ptr(&node->script), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->script), &x, ctx)) return -1; out->s = x.s; @@ -2641,7 +2619,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, const policy_node_with_script_t *node = (const policy_node_with_script_t *) policy_node; policy_node_ext_info_t x; - if (0 > compute_miniscript_policy_ext_info(resolve_node_ptr(&node->script), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->script), &x, ctx)) return -1; out->s = 1; @@ -2671,7 +2649,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, const policy_node_with_script_t *node = (const policy_node_with_script_t *) policy_node; policy_node_ext_info_t x; - if (0 > compute_miniscript_policy_ext_info(resolve_node_ptr(&node->script), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->script), &x, ctx)) return -1; out->s = x.s; @@ -2696,7 +2674,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, const policy_node_with_script_t *node = (const policy_node_with_script_t *) policy_node; policy_node_ext_info_t x; - if (0 > compute_miniscript_policy_ext_info(resolve_node_ptr(&node->script), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->script), &x, ctx)) return -1; out->s = x.s; @@ -2722,7 +2700,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, const policy_node_with_script_t *node = (const policy_node_with_script_t *) policy_node; policy_node_ext_info_t x; - if (0 > compute_miniscript_policy_ext_info(resolve_node_ptr(&node->script), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->script), &x, ctx)) return -1; out->s = x.s; @@ -2747,7 +2725,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, const policy_node_with_script_t *node = (const policy_node_with_script_t *) policy_node; policy_node_ext_info_t x; - if (0 > compute_miniscript_policy_ext_info(resolve_node_ptr(&node->script), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->script), &x, ctx)) return -1; out->s = x.s; @@ -2773,7 +2751,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node, const policy_node_with_script_t *node = (const policy_node_with_script_t *) policy_node; policy_node_ext_info_t x; - if (0 > compute_miniscript_policy_ext_info(resolve_node_ptr(&node->script), &x, ctx)) + if (0 > compute_miniscript_policy_ext_info(r_policy_node(&node->script), &x, ctx)) return -1; out->s = x.s; diff --git a/src/common/wallet.h b/src/common/wallet.h index 040ee4104..3c1a7e49e 100644 --- a/src/common/wallet.h +++ b/src/common/wallet.h @@ -164,6 +164,65 @@ typedef enum { #define MINISCRIPT_TYPE_K 2 #define MINISCRIPT_TYPE_W 3 +// The various structures used to represent the wallet policy abstract syntax tree contain a lot +// pointers; using a regular pointer would make each of them 4 bytes long, moreover causing +// additional loss of memory due to padding. Instead, we use a 2-bytes relative pointer to point to +// policy_nodes, representing a non-negative offset from the position of the structure itself. +// This reduces the memory utilization of those pointers, and moreover it allows to reduce padding +// in other structures, as they no longer contain 32-bit pointers. +typedef struct ptr_rel_s { + uint16_t offset; +} ptr_rel_t; + +// TODO: remove the generic versions +// Converts a relative pointer to the corresponding pointer to the corresponding absolute pointer +static inline const void *resolve_ptr(const ptr_rel_t *ptr) { + return (const void *) ((const uint8_t *) ptr + ptr->offset); +} + +// Initializes a relative pointer so that it points to node. +// IMPORTANT: the assumption is that node is located in memory at an address larger than +// relative_ptr, and at an offset smaller than 65536. No error is detected otherwise, therefore this +// is potentially dangerous to use. +static inline void init_relative_ptr(ptr_rel_t *relative_ptr, void *node) { + relative_ptr->offset = (uint16_t) ((uint8_t *) node - (uint8_t *) relative_ptr); +} + +// Defines a relative pointer type for name##t, and the conversion functions to/from a relative +// pointer and a pointer to name##_t. +// Relative pointers use an uint16_t to represent the offset; therefore, the offset must be at most. +// 65536. Bounds are not checked, therefore it needs to be handled with care. +#define DEFINE_REL_PTR(name, type) \ + /* \ + * Relative pointer structure for `type`. \ + * \ + * This structure holds an offset that is used to calculate the actual pointer \ + * to a `type` object. \ + */ \ + typedef struct rptr_##name##_s { \ + uint16_t offset; \ + } rptr_##name##_t; \ + \ + /* \ + * Resolve a relative pointer to a `type` object. \ + * \ + * @param ptr A pointer to the relative pointer structure. \ + * @return A constant pointer to the `type` object. \ + */ \ + static inline const type *r_##name(const rptr_##name##_t *ptr) { \ + return (const type *) ((const uint8_t *) ptr + ptr->offset); \ + } \ + \ + /* \ + * Initialize a relative pointer to a `type` object. \ + * \ + * @param relative_ptr A pointer to the relative pointer structure to be initialized. \ + * @param node A pointer to the `type` object. \ + */ \ + static inline void i_##name(rptr_##name##_t *relative_ptr, void *node) { \ + relative_ptr->offset = (uint16_t) ((uint8_t *) node - (uint8_t *) relative_ptr); \ + } + // 2 bytes typedef struct policy_node_s { PolicyNodeType type; @@ -178,6 +237,8 @@ typedef struct policy_node_s { } flags; // 1 byte } policy_node_t; +DEFINE_REL_PTR(policy_node, policy_node_t) + typedef struct miniscript_ops_s { uint16_t count; // non-push opcodes int16_t sat; // number of keys in possibly executed OP_CHECKMULTISIG(VERIFY)s to satisfy (-1 @@ -213,28 +274,6 @@ typedef struct policy_node_ext_info_s { unsigned int x : 1; // the last opcode is not EQUAL, CHECKSIG, or CHECKMULTISIG } policy_node_ext_info_t; -// The various structures used to represent the wallet policy abstract syntax tree contain a lot -// pointers; using a regular pointer would make each of them 4 bytes long, moreover causing -// additional loss of memory due to padding. Instead, we use a 2-bytes relative pointer to point to -// policy_nodes, representing a non-negative offset from the position of the structure itself. -// This reduces the memory utilization of those pointers, and moreover it allows to reduce padding -// in other structures, as they no longer contain 32-bit pointers. -typedef struct ptr_rel_s { - uint16_t offset; -} ptr_rel_t; - -// Typed versions for each of the kinds of pointers - -// relative pointer to policy_node_s -typedef struct ptr_rel_node_s { - uint16_t offset; -} ptr_rel_node_t; - -// relative pointer to policy_node_key_placeholder_s -typedef struct ptr_rel_node_key_placeholder_s { - uint16_t offset; -} ptr_rel_node_key_placeholder_t; - #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcomment" // The compiler doesn't like /** inside a block comment, so we disable this warning temporarily. @@ -257,6 +296,8 @@ typedef struct { int16_t key_index; // index of the key } policy_node_key_placeholder_t; +DEFINE_REL_PTR(policy_node_key_placeholder, policy_node_key_placeholder_t) + // 4 bytes typedef struct { struct policy_node_s base; @@ -265,19 +306,19 @@ typedef struct { // 4 bytes typedef struct { struct policy_node_s base; - ptr_rel_node_t script; + rptr_policy_node_t script; } policy_node_with_script_t; // 6 bytes typedef struct { struct policy_node_s base; - ptr_rel_node_t scripts[2]; + rptr_policy_node_t scripts[2]; } policy_node_with_script2_t; // 8 bytes typedef struct { struct policy_node_s base; - ptr_rel_node_t scripts[3]; + rptr_policy_node_t scripts[3]; } policy_node_with_script3_t; // generic type with pointer for up to 3 (but constant) number of child scripts @@ -286,7 +327,7 @@ typedef policy_node_with_script3_t policy_node_with_scripts_t; // 4 bytes typedef struct { struct policy_node_s base; - ptr_rel_node_key_placeholder_t key_placeholder; + rptr_policy_node_key_placeholder_t key_placeholder; } policy_node_with_key_t; // 8 bytes @@ -300,7 +341,7 @@ typedef struct { struct policy_node_s base; // type is TOKEN_MULTI or TOKEN_SORTEDMULTI int16_t k; // threshold int16_t n; // number of keys - ptr_rel_node_key_placeholder_t + rptr_policy_node_key_placeholder_t key_placeholders; // pointer to array of exactly n key placeholders } policy_node_multisig_t; @@ -308,7 +349,7 @@ typedef struct { typedef struct policy_node_scriptlist_s { // TODO: change to relative pointers struct policy_node_scriptlist_s *next; - ptr_rel_node_t script; + rptr_policy_node_t script; } policy_node_scriptlist_t; // 12 bytes, (+ 8 bytes for every script) @@ -336,7 +377,7 @@ typedef struct policy_node_tree_s { bool is_leaf; // if this is a leaf, then it contains a pointer to a SCRIPT; // otherwise, it contains two pointers to TREE expressions. union { - ptr_rel_node_t script; // pointer to a policy_node_with_script_t + rptr_policy_node_t script; // pointer to a policy_node_with_script_t struct { ptr_rel_t left_tree; // pointer to a policy_node_tree_s ptr_rel_t right_tree; // pointer to a policy_node_tree_s @@ -346,47 +387,11 @@ typedef struct policy_node_tree_s { typedef struct { struct policy_node_s base; - ptr_rel_node_key_placeholder_t key_placeholder; + rptr_policy_node_key_placeholder_t key_placeholder; // TODO: change to relative pointers policy_node_tree_t *tree; // NULL if tr(KP) } policy_node_tr_t; -// The following helpers function simplifies dealing with relative pointers to scripts - -// Converts a relative pointer to the corresponding pointer to the corresponding absolute pointer -static inline const void *resolve_ptr(const ptr_rel_t *ptr) { - return (const void *) ((const uint8_t *) ptr + ptr->offset); -} - -// Syntactic sugar for resolve_ptr when the return value is a pointer to policy_node_t -static inline const policy_node_t *resolve_node_ptr(const ptr_rel_node_t *ptr) { - return (const policy_node_t *) ((const uint8_t *) ptr + ptr->offset); -} - -// Syntactic sugar for resolve_ptr when the return value is a pointer to policy_node_t -static inline const policy_node_key_placeholder_t *resolve_node_key_placeholder_ptr( - const ptr_rel_node_key_placeholder_t *ptr) { - return (const policy_node_key_placeholder_t *) ((const uint8_t *) ptr + ptr->offset); -} - -// Initializes a relative pointer so that it points to node. -// IMPORTANT: the assumption is that node is located in memory at an address larger than -// relative_ptr, and at an offset smaller than 65536. No error is detected otherwise, therefore this -// is potentially dangerous to use. -static inline void init_relative_ptr(ptr_rel_t *relative_ptr, void *node) { - relative_ptr->offset = (uint16_t) ((uint8_t *) node - (uint8_t *) relative_ptr); -} - -static inline void init_relative_node_ptr(ptr_rel_node_t *relative_ptr, void *node) { - relative_ptr->offset = (uint16_t) ((uint8_t *) node - (uint8_t *) relative_ptr); -} - -static inline void init_relative_node_key_placeholder_ptr( - ptr_rel_node_key_placeholder_t *relative_ptr, - void *node) { - relative_ptr->offset = (uint16_t) ((uint8_t *) node - (uint8_t *) relative_ptr); -} - /** * Parses the string in the `buffer` as a serialized policy map into `header` * diff --git a/src/handler/lib/policy.c b/src/handler/lib/policy.c index 94ef61e7d..8cd64242d 100644 --- a/src/handler/lib/policy.c +++ b/src/handler/lib/policy.c @@ -568,7 +568,7 @@ __attribute__((warn_unused_result)) static int process_generic_node(policy_parse if (-1 == get_derived_pubkey(state->dispatcher_context, state->wdi, - resolve_node_key_placeholder_ptr(&policy->key_placeholder), + r_policy_node_key_placeholder(&policy->key_placeholder), compressed_pubkey)) { return -1; } @@ -590,7 +590,7 @@ __attribute__((warn_unused_result)) static int process_generic_node(policy_parse if (-1 == get_derived_pubkey(state->dispatcher_context, state->wdi, - resolve_node_key_placeholder_ptr(&policy->key_placeholder), + r_policy_node_key_placeholder(&policy->key_placeholder), compressed_pubkey)) { return -1; } @@ -628,7 +628,7 @@ __attribute__((warn_unused_result)) static int process_generic_node(policy_parse case CMD_CODE_PROCESS_CHILD: { const policy_node_with_scripts_t *policy = (const policy_node_with_scripts_t *) node->policy_node; - if (0 > state_stack_push(state, resolve_node_ptr(&policy->scripts[cmd_data]), 0)) { + if (0 > state_stack_push(state, r_policy_node(&policy->scripts[cmd_data]), 0)) { return -1; } break; @@ -637,7 +637,7 @@ __attribute__((warn_unused_result)) static int process_generic_node(policy_parse const policy_node_with_scripts_t *policy = (const policy_node_with_scripts_t *) node->policy_node; if (0 > state_stack_push(state, - resolve_node_ptr(&policy->scripts[cmd_data]), + r_policy_node(&policy->scripts[cmd_data]), node->flags)) { return -1; } @@ -647,7 +647,7 @@ __attribute__((warn_unused_result)) static int process_generic_node(policy_parse const policy_node_with_scripts_t *policy = (const policy_node_with_scripts_t *) node->policy_node; if (0 > state_stack_push(state, - resolve_node_ptr(&policy->scripts[cmd_data]), + r_policy_node(&policy->scripts[cmd_data]), node->flags | PROCESSOR_FLAG_V)) { return -1; } @@ -680,7 +680,7 @@ __attribute__((warn_unused_result)) static int process_pkh_wpkh_node(policy_pars if (-1 == get_derived_pubkey(state->dispatcher_context, state->wdi, - resolve_node_key_placeholder_ptr(&policy->key_placeholder), + r_policy_node_key_placeholder(&policy->key_placeholder), compressed_pubkey)) { return -1; } else if (policy->base.type == TOKEN_PKH) { @@ -762,7 +762,7 @@ __attribute__((warn_unused_result)) static int process_thresh_node(policy_parser update_output_u8(state, OP_ADD); } - if (-1 == state_stack_push(state, resolve_node_ptr(&cur->script), 0)) { + if (-1 == state_stack_push(state, r_policy_node(&cur->script), 0)) { return -1; } ++node->step; @@ -808,7 +808,7 @@ __attribute__((warn_unused_result)) static int process_multi_sortedmulti_node( if (-1 == get_derived_pubkey(state->dispatcher_context, state->wdi, - &resolve_node_key_placeholder_ptr(&policy->key_placeholders)[i], + &r_policy_node_key_placeholder(&policy->key_placeholders)[i], compressed_pubkey)) { return -1; } @@ -834,7 +834,7 @@ __attribute__((warn_unused_result)) static int process_multi_sortedmulti_node( if (-1 == get_derived_pubkey( state->dispatcher_context, state->wdi, - &resolve_node_key_placeholder_ptr(&policy->key_placeholders)[j], + &r_policy_node_key_placeholder(&policy->key_placeholders)[j], cur_pubkey)) { return -1; } @@ -886,7 +886,7 @@ __attribute__((warn_unused_result)) static int process_multi_a_sortedmulti_a_nod if (-1 == get_derived_pubkey(state->dispatcher_context, state->wdi, - &resolve_node_key_placeholder_ptr(&policy->key_placeholders)[i], + &r_policy_node_key_placeholder(&policy->key_placeholders)[i], compressed_pubkey)) { return -1; } @@ -902,7 +902,7 @@ __attribute__((warn_unused_result)) static int process_multi_a_sortedmulti_a_nod if (-1 == get_derived_pubkey( state->dispatcher_context, state->wdi, - &resolve_node_key_placeholder_ptr(&policy->key_placeholders)[j], + &r_policy_node_key_placeholder(&policy->key_placeholders)[j], cur_pubkey)) { return -1; } @@ -988,7 +988,7 @@ __attribute__((noinline)) int compute_taptree_hash(dispatcher_context_t *dc, const policy_node_tree_t *tree, uint8_t out[static 32]) { if (tree->is_leaf) - return compute_tapleaf_hash(dc, wdi, resolve_node_ptr(&tree->script), out); + return compute_tapleaf_hash(dc, wdi, r_policy_node(&tree->script), out); else return compute_and_combine_taptree_child_hashes(dc, wdi, tree, out); } @@ -1011,7 +1011,7 @@ int get_wallet_script(dispatcher_context_t *dispatcher_context, policy_node_with_key_t *pkh_policy = (policy_node_with_key_t *) policy; if (0 > get_derived_pubkey(dispatcher_context, wdi, - resolve_node_key_placeholder_ptr(&pkh_policy->key_placeholder), + r_policy_node_key_placeholder(&pkh_policy->key_placeholder), compressed_pubkey)) { return -1; } @@ -1030,7 +1030,7 @@ int get_wallet_script(dispatcher_context_t *dispatcher_context, policy_node_with_key_t *wpkh_policy = (policy_node_with_key_t *) policy; if (0 > get_derived_pubkey(dispatcher_context, wdi, - resolve_node_key_placeholder_ptr(&wpkh_policy->key_placeholder), + r_policy_node_key_placeholder(&wpkh_policy->key_placeholder), compressed_pubkey)) { return -1; } @@ -1044,18 +1044,17 @@ int get_wallet_script(dispatcher_context_t *dispatcher_context, const policy_node_t *core_policy; if (policy->type == TOKEN_SH) { const policy_node_t *child = - resolve_node_ptr(&((const policy_node_with_script_t *) policy)->script); + r_policy_node(&((const policy_node_with_script_t *) policy)->script); if (child->type == TOKEN_WSH) { script_type = WRAPPED_SCRIPT_TYPE_SH_WSH; - core_policy = - resolve_node_ptr(&((const policy_node_with_script_t *) child)->script); + core_policy = r_policy_node(&((const policy_node_with_script_t *) child)->script); } else { script_type = WRAPPED_SCRIPT_TYPE_SH; core_policy = child; } } else { // if (policy->type == TOKEN_WSH script_type = WRAPPED_SCRIPT_TYPE_WSH; - core_policy = resolve_node_ptr(&((const policy_node_with_script_t *) policy)->script); + core_policy = r_policy_node(&((const policy_node_with_script_t *) policy)->script); } if (0 > get_wallet_internal_script_hash(dispatcher_context, @@ -1110,7 +1109,7 @@ int get_wallet_script(dispatcher_context_t *dispatcher_context, if (0 > get_derived_pubkey(dispatcher_context, wdi, - resolve_node_key_placeholder_ptr(&tr_policy->key_placeholder), + r_policy_node_key_placeholder(&tr_policy->key_placeholder), compressed_pubkey)) { return -1; } @@ -1334,23 +1333,23 @@ static int get_bip44_purpose(const policy_node_t *descriptor_template) { int purpose = -1; switch (descriptor_template->type) { case TOKEN_PKH: - kp = resolve_node_key_placeholder_ptr( + kp = r_policy_node_key_placeholder( &((const policy_node_with_key_t *) descriptor_template)->key_placeholder); purpose = 44; // legacy break; case TOKEN_WPKH: - kp = resolve_node_key_placeholder_ptr( + kp = r_policy_node_key_placeholder( &((const policy_node_with_key_t *) descriptor_template)->key_placeholder); purpose = 84; // native segwit break; case TOKEN_SH: { - const policy_node_t *inner = resolve_node_ptr( - &((const policy_node_with_script_t *) descriptor_template)->script); + const policy_node_t *inner = + r_policy_node(&((const policy_node_with_script_t *) descriptor_template)->script); if (inner->type != TOKEN_WPKH) { return -1; } - kp = resolve_node_key_placeholder_ptr( + kp = r_policy_node_key_placeholder( &((const policy_node_with_key_t *) inner)->key_placeholder); purpose = 49; // nested segwit break; @@ -1360,7 +1359,7 @@ static int get_bip44_purpose(const policy_node_t *descriptor_template) { return -1; } - kp = resolve_node_key_placeholder_ptr( + kp = r_policy_node_key_placeholder( &((const policy_node_tr_t *) descriptor_template)->key_placeholder); purpose = 86; // standard single-key P2TR break; @@ -1503,9 +1502,9 @@ static int get_key_placeholder_by_index_in_tree(const policy_node_tree_t *tree, policy_node_key_placeholder_t *out_placeholder) { if (tree->is_leaf) { int ret = - get_key_placeholder_by_index(resolve_node_ptr(&tree->script), i, NULL, out_placeholder); + get_key_placeholder_by_index(r_policy_node(&tree->script), i, NULL, out_placeholder); if (ret >= 0 && out_tapleaf_ptr != NULL && i < (unsigned) ret) { - *out_tapleaf_ptr = resolve_node_ptr(&tree->script); + *out_tapleaf_ptr = r_policy_node(&tree->script); } return ret; } else { @@ -1560,7 +1559,7 @@ int get_key_placeholder_by_index(const policy_node_t *policy, if (i == 0) { policy_node_with_key_t *wpkh = (policy_node_with_key_t *) policy; memcpy(out_placeholder, - resolve_node_key_placeholder_ptr(&wpkh->key_placeholder), + r_policy_node_key_placeholder(&wpkh->key_placeholder), sizeof(policy_node_key_placeholder_t)); } return 1; @@ -1569,7 +1568,7 @@ int get_key_placeholder_by_index(const policy_node_t *policy, if (i == 0) { policy_node_tr_t *tr = (policy_node_tr_t *) policy; memcpy(out_placeholder, - resolve_node_key_placeholder_ptr(&tr->key_placeholder), + r_policy_node_key_placeholder(&tr->key_placeholder), sizeof(policy_node_key_placeholder_t)); } if (((policy_node_tr_t *) policy)->tree != NULL) { @@ -1597,7 +1596,7 @@ int get_key_placeholder_by_index(const policy_node_t *policy, if (i < (unsigned int) node->n) { policy_node_key_placeholder_t *placeholders = - resolve_node_key_placeholder_ptr(&node->key_placeholders); + r_policy_node_key_placeholder(&node->key_placeholders); memcpy(out_placeholder, &placeholders[i], sizeof(policy_node_key_placeholder_t)); } @@ -1618,7 +1617,7 @@ int get_key_placeholder_by_index(const policy_node_t *policy, case TOKEN_L: case TOKEN_U: { return get_key_placeholder_by_index( - resolve_node_ptr(&((const policy_node_with_script_t *) policy)->script), + r_policy_node(&((const policy_node_with_script_t *) policy)->script), i, out_tapleaf_ptr, out_placeholder); @@ -1633,14 +1632,14 @@ int get_key_placeholder_by_index(const policy_node_t *policy, case TOKEN_OR_D: case TOKEN_OR_I: { const policy_node_with_script2_t *node = (const policy_node_with_script2_t *) policy; - int ret1 = get_key_placeholder_by_index(resolve_node_ptr(&node->scripts[0]), + int ret1 = get_key_placeholder_by_index(r_policy_node(&node->scripts[0]), i, out_tapleaf_ptr, out_placeholder); if (ret1 < 0) return -1; bool found = i < (unsigned int) ret1; - int ret2 = get_key_placeholder_by_index(resolve_node_ptr(&node->scripts[1]), + int ret2 = get_key_placeholder_by_index(r_policy_node(&node->scripts[1]), found ? 0 : i - ret1, found ? NULL : out_tapleaf_ptr, found ? NULL : out_placeholder); @@ -1652,21 +1651,21 @@ int get_key_placeholder_by_index(const policy_node_t *policy, // nodes with exactly three child scripts case TOKEN_ANDOR: { const policy_node_with_script3_t *node = (const policy_node_with_script3_t *) policy; - int ret1 = get_key_placeholder_by_index(resolve_node_ptr(&node->scripts[0]), + int ret1 = get_key_placeholder_by_index(r_policy_node(&node->scripts[0]), i, out_tapleaf_ptr, out_placeholder); if (ret1 < 0) return -1; bool found = i < (unsigned int) ret1; - int ret2 = get_key_placeholder_by_index(resolve_node_ptr(&node->scripts[1]), + int ret2 = get_key_placeholder_by_index(r_policy_node(&node->scripts[1]), found ? 0 : i - ret1, found ? NULL : out_tapleaf_ptr, found ? NULL : out_placeholder); if (ret2 < 0) return -1; found = i < (unsigned int) (ret1 + ret2); - int ret3 = get_key_placeholder_by_index(resolve_node_ptr(&node->scripts[2]), + int ret3 = get_key_placeholder_by_index(r_policy_node(&node->scripts[2]), found ? 0 : i - ret1 - ret2, found ? NULL : out_tapleaf_ptr, found ? NULL : out_placeholder); @@ -1682,7 +1681,7 @@ int get_key_placeholder_by_index(const policy_node_t *policy, policy_node_scriptlist_t *cur_child = node->scriptlist; for (int script_idx = 0; script_idx < node->n; script_idx++) { found = i < (unsigned int) ret; - int ret_partial = get_key_placeholder_by_index(resolve_node_ptr(&cur_child->script), + int ret_partial = get_key_placeholder_by_index(r_policy_node(&cur_child->script), found ? 0 : i - ret, found ? NULL : out_tapleaf_ptr, found ? NULL : out_placeholder); @@ -1815,16 +1814,16 @@ static int is_taptree_miniscript_sane(const policy_node_tree_t *taptree) { // Recurse until leaves are found, then check sanity if they contain miniscript. // No check is performed on leaves not containing miniscript. if (taptree->is_leaf) { - const policy_node_t *script = resolve_node_ptr(&taptree->script); + const policy_node_t *script = r_policy_node(&taptree->script); if (script->flags.is_miniscript && // only check for miniscript leaves 0 > is_miniscript_sane(script, MINISCRIPT_CONTEXT_TAPSCRIPT)) { return -1; } } else { - if (0 > is_taptree_miniscript_sane(resolve_node_ptr(&taptree->left_tree))) { + if (0 > is_taptree_miniscript_sane(r_policy_node(&taptree->left_tree))) { return -1; } - if (0 > is_taptree_miniscript_sane(resolve_node_ptr(&taptree->right_tree))) { + if (0 > is_taptree_miniscript_sane(r_policy_node(&taptree->right_tree))) { return -1; } } @@ -1839,7 +1838,7 @@ int is_policy_sane(dispatcher_context_t *dispatcher_context, uint32_t n_keys) { if (policy->type == TOKEN_WSH) { const policy_node_t *inner = - resolve_node_ptr(&((const policy_node_with_script_t *) policy)->script); + r_policy_node(&((const policy_node_with_script_t *) policy)->script); if (inner->flags.is_miniscript) { if (0 > is_miniscript_sane(inner, MINISCRIPT_CONTEXT_P2WSH)) { return -1;