This commit is contained in:
l0rinc 2025-04-28 15:08:13 -07:00 committed by GitHub
commit cbf73002c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -297,28 +297,29 @@ using miniscript::operator""_mst;
using Node = miniscript::Node<CPubKey>; using Node = miniscript::Node<CPubKey>;
/** Compute all challenges (pubkeys, hashes, timelocks) that occur in a given Miniscript. */ /** Compute all challenges (pubkeys, hashes, timelocks) that occur in a given Miniscript. */
// NOLINTNEXTLINE(misc-no-recursion) std::set<Challenge> FindChallenges(const Node* root)
std::set<Challenge> FindChallenges(const NodeRef& ref) { {
std::set<Challenge> chal; std::set<Challenge> chal;
for (std::vector stack{root}; !stack.empty();) {
const auto* ref{stack.back()};
stack.pop_back();
for (const auto& key : ref->keys) { for (const auto& key : ref->keys) {
chal.emplace(ChallengeType::PK, ChallengeNumber(key)); chal.emplace(ChallengeType::PK, ChallengeNumber(key));
} }
if (ref->fragment == miniscript::Fragment::OLDER) { switch (ref->fragment) {
chal.emplace(ChallengeType::OLDER, ref->k); case Fragment::OLDER: chal.emplace(ChallengeType::OLDER, ref->k); break;
} else if (ref->fragment == miniscript::Fragment::AFTER) { case Fragment::AFTER: chal.emplace(ChallengeType::AFTER, ref->k); break;
chal.emplace(ChallengeType::AFTER, ref->k); case Fragment::SHA256: chal.emplace(ChallengeType::SHA256, ChallengeNumber(ref->data)); break;
} else if (ref->fragment == miniscript::Fragment::SHA256) { case Fragment::RIPEMD160: chal.emplace(ChallengeType::RIPEMD160, ChallengeNumber(ref->data)); break;
chal.emplace(ChallengeType::SHA256, ChallengeNumber(ref->data)); case Fragment::HASH256: chal.emplace(ChallengeType::HASH256, ChallengeNumber(ref->data)); break;
} else if (ref->fragment == miniscript::Fragment::RIPEMD160) { case Fragment::HASH160: chal.emplace(ChallengeType::HASH160, ChallengeNumber(ref->data)); break;
chal.emplace(ChallengeType::RIPEMD160, ChallengeNumber(ref->data)); default: break;
} else if (ref->fragment == miniscript::Fragment::HASH256) {
chal.emplace(ChallengeType::HASH256, ChallengeNumber(ref->data));
} else if (ref->fragment == miniscript::Fragment::HASH160) {
chal.emplace(ChallengeType::HASH160, ChallengeNumber(ref->data));
} }
for (const auto& sub : ref->subs) { for (const auto& sub : ref->subs) {
auto sub_chal = FindChallenges(sub); stack.push_back(sub.get());
chal.insert(sub_chal.begin(), sub_chal.end()); }
} }
return chal; return chal;
} }
@ -347,7 +348,7 @@ struct MiniScriptTest : BasicTestingSetup {
/** Run random satisfaction tests. */ /** Run random satisfaction tests. */
void TestSatisfy(const KeyConverter& converter, const std::string& testcase, const NodeRef& node) { void TestSatisfy(const KeyConverter& converter, const std::string& testcase, const NodeRef& node) {
auto script = node->ToScript(converter); auto script = node->ToScript(converter);
auto challenges = FindChallenges(node); // Find all challenges in the generated miniscript. const auto challenges{FindChallenges(node.get())}; // Find all challenges in the generated miniscript.
std::vector<Challenge> challist(challenges.begin(), challenges.end()); std::vector<Challenge> challist(challenges.begin(), challenges.end());
for (int iter = 0; iter < 3; ++iter) { for (int iter = 0; iter < 3; ++iter) {
std::shuffle(challist.begin(), challist.end(), m_rng); std::shuffle(challist.begin(), challist.end(), m_rng);