Skip to content

Commit 2c372e9

Browse files
committed
MERGEBACK: descriptor: More test coverage + simplification
1 parent 0ef7f9c commit 2c372e9

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

src/ctest/test_descriptor.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,50 @@ static const struct descriptor_err_test {
960960
"descriptor - raw - any parent",
961961
"pk(raw(000102030405060708090a0b0c0d0e0f))",
962962
WALLY_NETWORK_BITCOIN_MAINNET
963+
},{
964+
"descriptor - after - non number child",
965+
"wsh(after(key_1)",
966+
WALLY_NETWORK_BITCOIN_MAINNET
967+
},{
968+
"descriptor - after - zero delay",
969+
"wsh(after(0)",
970+
WALLY_NETWORK_BITCOIN_MAINNET
971+
},{
972+
"descriptor - after - negative delay",
973+
"wsh(after(-1)",
974+
WALLY_NETWORK_BITCOIN_MAINNET
975+
},{
976+
"descriptor - after - delay too large",
977+
"wsh(after(2147483648)",
978+
WALLY_NETWORK_BITCOIN_MAINNET
979+
},{
980+
"descriptor - older - non number child",
981+
"wsh(older(key_1)",
982+
WALLY_NETWORK_BITCOIN_MAINNET
983+
},{
984+
"descriptor - older - zero delay",
985+
"wsh(older(0)",
986+
WALLY_NETWORK_BITCOIN_MAINNET
987+
},{
988+
"descriptor - older - negative delay",
989+
"wsh(older(-1)",
990+
WALLY_NETWORK_BITCOIN_MAINNET
991+
},{
992+
"descriptor - older - delay too large",
993+
"wsh(older(2147483648)",
994+
WALLY_NETWORK_BITCOIN_MAINNET
995+
},{
996+
"miniscript - thresh - zero required",
997+
"wsh(thresh(0,c:pk_k(key_1),sc:pk_k(key_2),sc:pk_k(key_3)))",
998+
WALLY_NETWORK_BITCOIN_MAINNET
999+
},{
1000+
"miniscript - thresh - not enough children",
1001+
"wsh(thresh(2,c:pk_k(key_1),sc:pk_k(key_2)))",
1002+
WALLY_NETWORK_BITCOIN_MAINNET
1003+
},{
1004+
"miniscript - thresh - require more than available children",
1005+
"wsh(thresh(4,c:pk_k(key_1),sc:pk_k(key_2),sc:pk_k(key_3)))",
1006+
WALLY_NETWORK_BITCOIN_MAINNET
9631007
}
9641008
/* TODO: Add more tests for verify_x cases */
9651009
};

src/descriptor.c

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -587,35 +587,17 @@ static int verify_or_i(ms_node *node)
587587

588588
static int verify_thresh(ms_node *node)
589589
{
590-
ms_node *top = NULL;
591-
ms_node *child = NULL;
592-
uint32_t count = 0;
593-
uint32_t k = 0;
594-
bool all_e = true;
595-
bool all_m = true;
596-
uint32_t args = 0;
597-
uint32_t num_s = 0;
598-
599-
if (node_get_child_count(node) < 4)
600-
return WALLY_EINVAL;
590+
ms_node *top = top = node->child, *child;
591+
int64_t count = 0, num_s = 0, args = 0;
592+
bool all_e = true, all_m = true;
601593

602-
top = node->child;
603-
if (top->builtin || top->kind != KIND_NUMBER || top->number < 0)
594+
if (!top || top->builtin || top->kind != KIND_NUMBER)
604595
return WALLY_EINVAL;
605596

606-
k = (uint32_t)top->number;
607-
if (k < 1)
608-
return WALLY_EINVAL;
597+
for (child = top->next; child; child = child->next) {
598+
const uint32_t expected_type = count ? TYPE_W : TYPE_B;
609599

610-
child = top->next;
611-
while (child) {
612-
if (!child->builtin)
613-
return WALLY_EINVAL;
614-
615-
if (!count) {
616-
if (~child->type_properties & (TYPE_B | PROP_D | PROP_U))
617-
return WALLY_EINVAL;
618-
} else if (~child->type_properties & (TYPE_W | PROP_D | PROP_U))
600+
if (!child->builtin || (~child->type_properties & (expected_type | PROP_D | PROP_U)))
619601
return WALLY_EINVAL;
620602

621603
if (~child->type_properties & PROP_E)
@@ -628,10 +610,8 @@ static int verify_thresh(ms_node *node)
628610
args += (~child->type_properties & PROP_O) ? 2 : 1;
629611

630612
++count;
631-
child = child->next;
632613
}
633-
634-
if (k > count)
614+
if (count < 3 || top->number < 1 || top->number >= count)
635615
return WALLY_EINVAL;
636616

637617
node->type_properties = TYPE_B | PROP_D | PROP_U;
@@ -641,9 +621,9 @@ static int verify_thresh(ms_node *node)
641621
node->type_properties |= PROP_O;
642622
if (all_e && num_s == count)
643623
node->type_properties |= PROP_E;
644-
if (all_e && all_m && num_s >= count - k)
624+
if (all_e && all_m && num_s >= count - top->number)
645625
node->type_properties |= PROP_M;
646-
if (num_s >= count - k + 1)
626+
if (num_s >= count - top->number + 1)
647627
node->type_properties |= PROP_S;
648628

649629
return WALLY_OK;

0 commit comments

Comments
 (0)