descriptor: Add proper Clone function to miniscript::Node

Multipath descriptors requires performing a deep copy, so a Clone
function that does that is added to miniscript::Node instead of the
current shallow copy.

Co-Authored-By: Antoine Poinsot <darosior@protonmail.com>
This commit is contained in:
Ava Chow 2024-09-10 15:50:08 -04:00
parent 5691fa93c4
commit 6d11c9c60b
2 changed files with 21 additions and 2 deletions

View file

@ -1353,7 +1353,7 @@ public:
for (const auto& arg : m_pubkey_args) {
providers.push_back(arg->Clone());
}
return std::make_unique<MiniscriptDescriptor>(std::move(providers), miniscript::MakeNodeRef<uint32_t>(*m_node));
return std::make_unique<MiniscriptDescriptor>(std::move(providers), m_node->Clone());
}
};
@ -2143,7 +2143,7 @@ std::vector<std::unique_ptr<DescriptorImpl>> ParseScript(uint32_t& key_exp_index
for (auto& pub : parser.m_keys) {
pubs.emplace_back(std::move(pub.at(i)));
}
ret.emplace_back(std::make_unique<MiniscriptDescriptor>(std::move(pubs), node));
ret.emplace_back(std::make_unique<MiniscriptDescriptor>(std::move(pubs), node->Clone()));
}
return ret;
}

View file

@ -528,6 +528,20 @@ struct Node {
}
}
NodeRef<Key> Clone() const
{
// Use TreeEval() to avoid a stack-overflow due to recursion
auto upfn = [](const Node& node, Span<NodeRef<Key>> children) {
std::vector<NodeRef<Key>> new_subs;
for (auto child = children.begin(); child != children.end(); ++child) {
new_subs.emplace_back(std::move(*child));
}
// std::make_unique (and therefore MakeNodeRef) doesn't work on private constructors
return std::unique_ptr<Node>{new Node{internal::NoDupCheck{}, node.m_script_ctx, node.fragment, std::move(new_subs), node.keys, node.data, node.k}};
};
return TreeEval<NodeRef<Key>>(upfn);
}
private:
//! Cached ops counts.
const internal::Ops ops;
@ -546,6 +560,11 @@ private:
//! for all subnodes as well.
mutable std::optional<bool> has_duplicate_keys;
// Constructor which takes all of the data that a Node could possibly contain.
// This is kept private as no valid fragment has all of these arguments.
// Only used by Clone()
Node(internal::NoDupCheck, MiniscriptContext script_ctx, Fragment nt, std::vector<NodeRef<Key>> sub, std::vector<Key> key, std::vector<unsigned char> arg, uint32_t val)
: fragment(nt), k(val), keys(key), data(std::move(arg)), subs(std::move(sub)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
//! Compute the length of the script for this miniscript (including children).
size_t CalcScriptLen() const {