Skip to content

Commit 523520f

Browse files
committed
Merge bitcoin/bitcoin#30866: descriptor: Add proper Clone function to miniscript::Node
66d21d0 qa: check parsed multipath descriptors dont share references (Antoine Poinsot) 09a1875 miniscript: Make NodeRef a unique_ptr (Ava Chow) 9ccb46f miniscript: Ensure there is no NodeRef copy constructor or assignment operator (Ava Chow) 6d11c9c descriptor: Add proper Clone function to miniscript::Node (Ava Chow) Pull request description: 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. Fixes #30864 ACKs for top commit: darosior: re-ACK 66d21d0 hodlinator: re-ACK 66d21d0 🚀 brunoerg: reACK 66d21d0 Tree-SHA512: bea017497ed3cc0b2da2df7e3ccae1fa4a324769b7da1065963da131235bd8bfdcdfe337a3fabbb3ab4d3822611211fca6a9772e18e2ee1cb3d853e831ff6f88
2 parents 5691fa9 + 66d21d0 commit 523520f

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

src/script/descriptor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@ class MiniscriptDescriptor final : public DescriptorImpl
13531353
for (const auto& arg : m_pubkey_args) {
13541354
providers.push_back(arg->Clone());
13551355
}
1356-
return std::make_unique<MiniscriptDescriptor>(std::move(providers), miniscript::MakeNodeRef<uint32_t>(*m_node));
1356+
return std::make_unique<MiniscriptDescriptor>(std::move(providers), m_node->Clone());
13571357
}
13581358
};
13591359

@@ -2143,7 +2143,7 @@ std::vector<std::unique_ptr<DescriptorImpl>> ParseScript(uint32_t& key_exp_index
21432143
for (auto& pub : parser.m_keys) {
21442144
pubs.emplace_back(std::move(pub.at(i)));
21452145
}
2146-
ret.emplace_back(std::make_unique<MiniscriptDescriptor>(std::move(pubs), node));
2146+
ret.emplace_back(std::make_unique<MiniscriptDescriptor>(std::move(pubs), node->Clone()));
21472147
}
21482148
return ret;
21492149
}

src/script/miniscript.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ inline consteval Type operator""_mst(const char* c, size_t l)
189189
using Opcode = std::pair<opcodetype, std::vector<unsigned char>>;
190190

191191
template<typename Key> struct Node;
192-
template<typename Key> using NodeRef = std::shared_ptr<const Node<Key>>;
192+
template<typename Key> using NodeRef = std::unique_ptr<const Node<Key>>;
193193

194-
//! Construct a miniscript node as a shared_ptr.
194+
//! Construct a miniscript node as a unique_ptr.
195195
template<typename Key, typename... Args>
196-
NodeRef<Key> MakeNodeRef(Args&&... args) { return std::make_shared<const Node<Key>>(std::forward<Args>(args)...); }
196+
NodeRef<Key> MakeNodeRef(Args&&... args) { return std::make_unique<const Node<Key>>(std::forward<Args>(args)...); }
197197

198198
//! The different node types in miniscript.
199199
enum class Fragment {
@@ -528,6 +528,20 @@ struct Node {
528528
}
529529
}
530530

531+
NodeRef<Key> Clone() const
532+
{
533+
// Use TreeEval() to avoid a stack-overflow due to recursion
534+
auto upfn = [](const Node& node, Span<NodeRef<Key>> children) {
535+
std::vector<NodeRef<Key>> new_subs;
536+
for (auto child = children.begin(); child != children.end(); ++child) {
537+
new_subs.emplace_back(std::move(*child));
538+
}
539+
// std::make_unique (and therefore MakeNodeRef) doesn't work on private constructors
540+
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}};
541+
};
542+
return TreeEval<NodeRef<Key>>(upfn);
543+
}
544+
531545
private:
532546
//! Cached ops counts.
533547
const internal::Ops ops;
@@ -546,6 +560,11 @@ struct Node {
546560
//! for all subnodes as well.
547561
mutable std::optional<bool> has_duplicate_keys;
548562

563+
// Constructor which takes all of the data that a Node could possibly contain.
564+
// This is kept private as no valid fragment has all of these arguments.
565+
// Only used by Clone()
566+
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)
567+
: 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()) {}
549568

550569
//! Compute the length of the script for this miniscript (including children).
551570
size_t CalcScriptLen() const {
@@ -1663,6 +1682,10 @@ struct Node {
16631682
: Node(internal::NoDupCheck{}, ctx.MsContext(), nt, std::move(sub), val) { DuplicateKeyCheck(ctx); }
16641683
template <typename Ctx> Node(const Ctx& ctx, Fragment nt, uint32_t val = 0)
16651684
: Node(internal::NoDupCheck{}, ctx.MsContext(), nt, val) { DuplicateKeyCheck(ctx); }
1685+
1686+
// Delete copy constructor and assignment operator, use Clone() instead
1687+
Node(const Node&) = delete;
1688+
Node& operator=(const Node&) = delete;
16661689
};
16671690

16681691
namespace internal {

src/test/descriptor_tests.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,20 @@ void CheckMultipath(const std::string& prv,
448448
/*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, /*preimages=*/{},
449449
expanded_prvs.at(i), expanded_pubs.at(i), i);
450450
}
451+
452+
// The descriptor for each path must be standalone. They should not share common references. Test this
453+
// by parsing a multipath descriptor expression, deallocating all but one of the descriptors and making
454+
// sure we can perform operations on it.
455+
FlatSigningProvider prov, out;
456+
std::string error;
457+
const auto desc{[&](){
458+
auto parsed{Parse(pub, prov, error)};
459+
assert(parsed.size() > 1);
460+
return std::move(parsed.at(0));
461+
}()};
462+
desc->ToString();
463+
std::vector<CScript> out_scripts;
464+
desc->Expand(0, prov, out_scripts, out);
451465
}
452466

453467
void CheckInferDescriptor(const std::string& script_hex, const std::string& expected_desc, const std::vector<std::string>& hex_scripts, const std::vector<std::pair<std::string, std::string>>& origin_pubkeys)
@@ -851,6 +865,31 @@ BOOST_AUTO_TEST_CASE(descriptor_test)
851865
{{3}, {}},
852866
}
853867
);
868+
CheckMultipath("tr(xprv9yYge4PS54XkYT9KiLfCRwc8Jeuz8DucxQGtuEecJZYhKNiqbPxYHTPzXtskmzWBqdqkRAGsghNmZzNsfU2wstaB3XjDQFPv567aQSSuPyo/<2;3>,l:pk(xprvA1ADjaN8H3HGnZSmt4VF7YdWoV9oNq8jhqhurxsrYycBAFK555cECoaY22KWt6BTRNLuvobW5VQTF89PN3iA485LAg7epazevPyjCa4xTzd))",
869+
"tr(xpub6CY33ZvKuS63kwDnpNCCo5YrrgkUXgdUKdCVhd4Dru5gCB3z8wGnqFiUP98Za5pYSYF5KmvBHTY3Ra8FAJGggzBjuHS69WzN8gscPupuZwK/<2;3>,l:pk(xpub6E9a95u27Qqa13XEz62FUgaFMWzHnHrb54dWfMHU7K9A33eDccvUkbu1sHYoByHAgJdR326rWqn9pGZgZHz1afDprW5gGwS4gUX8Ri6aGPZ))",
870+
{
871+
"tr(xprv9yYge4PS54XkYT9KiLfCRwc8Jeuz8DucxQGtuEecJZYhKNiqbPxYHTPzXtskmzWBqdqkRAGsghNmZzNsfU2wstaB3XjDQFPv567aQSSuPyo/2,l:pk(xprvA1ADjaN8H3HGnZSmt4VF7YdWoV9oNq8jhqhurxsrYycBAFK555cECoaY22KWt6BTRNLuvobW5VQTF89PN3iA485LAg7epazevPyjCa4xTzd))",
872+
"tr(xprv9yYge4PS54XkYT9KiLfCRwc8Jeuz8DucxQGtuEecJZYhKNiqbPxYHTPzXtskmzWBqdqkRAGsghNmZzNsfU2wstaB3XjDQFPv567aQSSuPyo/3,l:pk(xprvA1ADjaN8H3HGnZSmt4VF7YdWoV9oNq8jhqhurxsrYycBAFK555cECoaY22KWt6BTRNLuvobW5VQTF89PN3iA485LAg7epazevPyjCa4xTzd))",
873+
},
874+
{
875+
"tr(xpub6CY33ZvKuS63kwDnpNCCo5YrrgkUXgdUKdCVhd4Dru5gCB3z8wGnqFiUP98Za5pYSYF5KmvBHTY3Ra8FAJGggzBjuHS69WzN8gscPupuZwK/2,l:pk(xpub6E9a95u27Qqa13XEz62FUgaFMWzHnHrb54dWfMHU7K9A33eDccvUkbu1sHYoByHAgJdR326rWqn9pGZgZHz1afDprW5gGwS4gUX8Ri6aGPZ))",
876+
"tr(xpub6CY33ZvKuS63kwDnpNCCo5YrrgkUXgdUKdCVhd4Dru5gCB3z8wGnqFiUP98Za5pYSYF5KmvBHTY3Ra8FAJGggzBjuHS69WzN8gscPupuZwK/3,l:pk(xpub6E9a95u27Qqa13XEz62FUgaFMWzHnHrb54dWfMHU7K9A33eDccvUkbu1sHYoByHAgJdR326rWqn9pGZgZHz1afDprW5gGwS4gUX8Ri6aGPZ))",
877+
},
878+
{
879+
"tr(xpub6CY33ZvKuS63kwDnpNCCo5YrrgkUXgdUKdCVhd4Dru5gCB3z8wGnqFiUP98Za5pYSYF5KmvBHTY3Ra8FAJGggzBjuHS69WzN8gscPupuZwK/2,l:pk(xpub6E9a95u27Qqa13XEz62FUgaFMWzHnHrb54dWfMHU7K9A33eDccvUkbu1sHYoByHAgJdR326rWqn9pGZgZHz1afDprW5gGwS4gUX8Ri6aGPZ))",
880+
"tr(xpub6CY33ZvKuS63kwDnpNCCo5YrrgkUXgdUKdCVhd4Dru5gCB3z8wGnqFiUP98Za5pYSYF5KmvBHTY3Ra8FAJGggzBjuHS69WzN8gscPupuZwK/3,l:pk(xpub6E9a95u27Qqa13XEz62FUgaFMWzHnHrb54dWfMHU7K9A33eDccvUkbu1sHYoByHAgJdR326rWqn9pGZgZHz1afDprW5gGwS4gUX8Ri6aGPZ))",
881+
},
882+
XONLY_KEYS,
883+
{
884+
{{"51200e3c14456bfa30f9f0bed6e55f35e1e9ca17c835e9f71b25bac0dfaab38ff2cd"}},
885+
{{"51202bdda29337ecaf8fcd5aa395febac6f99b8a866a0e8fb3f7bde2e24b1a7df2ba"}},
886+
},
887+
OutputType::BECH32M,
888+
{
889+
{{2}, {}},
890+
{{3}, {}},
891+
}
892+
);
854893
CheckUnparsable("pkh(xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U/<0;1>/<2;3>)", "pkh(xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/<0;1>/<2;3>)", "pkh(): Multiple multipath key path specifiers found");
855894
CheckUnparsable("pkh([deadbeef/<0;1>]xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U/0)", "pkh([deadbeef/<0;1>]xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/0)", "pkh(): Key path value \'<0;1>\' specifies multipath in a section where multipath is not allowed");
856895
CheckUnparsable("tr(xprv9s21ZrQH143K2Zu2kTVKcQi9nKhfgJUkYqG73wXsHuhATm1wkt6kcSZeTYEw2PL7krZtJopEYDvBdYWdAai3n3TWUTCVfHvPHqTYJv7smYe/6/*,{pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc/<1;2;3>/0/*),pk(xprv9s21ZrQH143K3jUwNHoqQNrtzJnJmx4Yup8NkNLdVQCymYbPbJXnPhwkfTfxZfptcs3rLAPUXS39oDLgrNKQGwbGsEmJJ8BU3RzQuvShEG4/0/0/<3;4>/*)})", "tr(xpub6B4sSbNr8XFYXqqKB7PeUemqgEaVtCLjgd5Lf2VYtezSHozC7ffCvVNCyu9TCgHntRQdimjV3tHbxmNfocxtuh6saNtZEw91gjXLRhQ3Yar/6/*,{pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/<1;2;3>/0/*),pk(xpub6AhFhZJJGt9YB8i85RfrJ8jT3T2FF5EejDCXqXfm1DAczFEXkk8HD3CXTg2TmKM8wTbSnSw3wPg5JuyLitUrpRmkjn2BQXyZnqJx16AGy94/0/0/<3;4>/*)})", "tr(): Multipath subscripts have mismatched lengths");

0 commit comments

Comments
 (0)