Skip to content

Commit d25d58b

Browse files
darosiorsipa
andcommitted
miniscript: add a helper to find the first insane sub with no child
This is helpful for finer grained descriptor parsing error: when there are multiple errors to report in a Miniscript descriptor start with the "smallest" fragments: the ones closer to be a leaf. Co-Authored-By: Pieter Wuille <[email protected]>
1 parent c38c7c5 commit d25d58b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/script/miniscript.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,21 @@ struct Node {
429429
));
430430
}
431431

432+
/** Like TreeEval, but without downfn or State type.
433+
* upfn takes (const Node&, Span<Result>) and returns Result. */
434+
template<typename Result, typename UpFn>
435+
Result TreeEval(UpFn upfn) const
436+
{
437+
struct DummyState {};
438+
return std::move(*TreeEvalMaybe<Result>(DummyState{},
439+
[](DummyState, const Node&, size_t) { return DummyState{}; },
440+
[&upfn](DummyState, const Node& node, Span<Result> subs) {
441+
Result res{upfn(node, subs)};
442+
return std::optional<Result>(std::move(res));
443+
}
444+
));
445+
}
446+
432447
/** Compare two miniscript subtrees, using a non-recursive algorithm. */
433448
friend int Compare(const Node<Key>& node1, const Node<Key>& node2)
434449
{
@@ -818,6 +833,15 @@ struct Node {
818833
//! Return the expression type.
819834
Type GetType() const { return typ; }
820835

836+
//! Find an insane subnode which has no insane children. Nullptr if there is none.
837+
const Node* FindInsaneSub() const {
838+
return TreeEval<const Node*>([](const Node& node, Span<const Node*> subs) -> const Node* {
839+
for (auto& sub: subs) if (sub) return sub;
840+
if (!node.IsSaneSubexpression()) return &node;
841+
return nullptr;
842+
});
843+
}
844+
821845
//! Check whether this node is valid at all.
822846
bool IsValid() const { return !(GetType() == ""_mst) && ScriptSize() <= MAX_STANDARD_P2WSH_SCRIPT_SIZE; }
823847

src/test/miniscript_tests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ struct KeyConverter {
111111
assert(it != g_testdata->pkmap.end());
112112
return it->second;
113113
}
114+
115+
std::optional<std::string> ToString(const Key& key) const {
116+
return HexStr(ToPKBytes(key));
117+
}
114118
};
115119

116120
//! Singleton instance of KeyConverter.
@@ -290,6 +294,15 @@ BOOST_AUTO_TEST_CASE(fixed_tests)
290294
// Same when the duplicates are on different levels in the tree
291295
const auto ms_dup4 = miniscript::FromString("thresh(2,pkh(03d30199d74fb5a22d47b6e054e2f378cedacffcb89904a61d75d0dbd407143e65),s:pk(03fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556),a:and_b(dv:older(1),s:pk(03d30199d74fb5a22d47b6e054e2f378cedacffcb89904a61d75d0dbd407143e65)))", CONVERTER);
292296
BOOST_CHECK(ms_dup4 && !ms_dup4->IsSane() && !ms_dup4->CheckDuplicateKey());
297+
// Test we find the first insane sub closer to be a leaf node. This fragment is insane for two reasons:
298+
// 1. It can be spent without a signature
299+
// 2. It contains timelock mixes
300+
// We'll report the timelock mix error, as it's "deeper" (closer to be a leaf node) than the "no 's' property"
301+
// error is.
302+
const auto ms_ins = miniscript::FromString("or_i(and_b(after(1),a:after(1000000000)),pk(03cdabb7f2dce7bfbd8a0b9570c6fd1e712e5d64045e9d6b517b3d5072251dc204))", CONVERTER);
303+
BOOST_CHECK(ms_ins && ms_ins->IsValid() && !ms_ins->IsSane());
304+
const auto insane_sub = ms_ins->FindInsaneSub();
305+
BOOST_CHECK(insane_sub && *insane_sub->ToString(CONVERTER) == "and_b(after(1),a:after(1000000000))");
293306

294307
// Timelock tests
295308
Test("after(100)", "?", TESTMODE_VALID | TESTMODE_NONMAL); // only heightlock

0 commit comments

Comments
 (0)