diff options
author | Antoine Poinsot <darosior@protonmail.com> | 2022-11-25 10:45:55 +0100 |
---|---|---|
committer | Antoine Poinsot <darosior@protonmail.com> | 2023-08-25 12:40:12 +0200 |
commit | 10546a569c6c96a5ec1b9708abf9ff5c8644f669 (patch) | |
tree | 51942798a796e8d8b2c8c6c0f61d7b9bb0e3c3d7 /src/script | |
parent | 9b7ec393b82ca9d7ada77d06e0835df0386a8b85 (diff) |
wallet: accurately account for the size of the witness stack
When estimating the maximum size of an input, we were assuming the
number of elements on the witness stack could be encode in a single
byte. This is a valid approximation for all the descriptors we support
(including P2WSH Miniscript ones), but may not hold anymore once we
support Miniscript within Taproot descriptors (since the max standard
witness stack size of 100 gets lifted).
It's a low-hanging fruit to account for it correctly, so just do it now.
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/descriptor.cpp | 36 | ||||
-rw-r--r-- | src/script/descriptor.h | 3 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index 727cf6118f..2f3f2c7a1d 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -718,6 +718,8 @@ public: virtual std::optional<int64_t> MaxSatSize(bool use_max_sig) const { return {}; } std::optional<int64_t> MaxSatisfactionWeight(bool) const override { return {}; } + + std::optional<int64_t> MaxSatisfactionElems() const override { return {}; } }; /** A parsed addr(A) descriptor. */ @@ -795,6 +797,8 @@ public: std::optional<int64_t> MaxSatisfactionWeight(bool use_max_sig) const override { return *MaxSatSize(use_max_sig) * WITNESS_SCALE_FACTOR; } + + std::optional<int64_t> MaxSatisfactionElems() const override { return 1; } }; /** A parsed pkh(P) descriptor. */ @@ -822,6 +826,8 @@ public: std::optional<int64_t> MaxSatisfactionWeight(bool use_max_sig) const override { return *MaxSatSize(use_max_sig) * WITNESS_SCALE_FACTOR; } + + std::optional<int64_t> MaxSatisfactionElems() const override { return 2; } }; /** A parsed wpkh(P) descriptor. */ @@ -849,6 +855,8 @@ public: std::optional<int64_t> MaxSatisfactionWeight(bool use_max_sig) const override { return MaxSatSize(use_max_sig); } + + std::optional<int64_t> MaxSatisfactionElems() const override { return 2; } }; /** A parsed combo(P) descriptor. */ @@ -909,6 +917,8 @@ public: std::optional<int64_t> MaxSatisfactionWeight(bool use_max_sig) const override { return *MaxSatSize(use_max_sig) * WITNESS_SCALE_FACTOR; } + + std::optional<int64_t> MaxSatisfactionElems() const override { return 1 + m_threshold; } }; /** A parsed (sorted)multi_a(...) descriptor. Always uses x-only pubkeys. */ @@ -943,6 +953,8 @@ public: std::optional<int64_t> MaxSatSize(bool use_max_sig) const override { return (1 + 65) * m_threshold + (m_pubkey_args.size() - m_threshold); } + + std::optional<int64_t> MaxSatisfactionElems() const override { return m_pubkey_args.size(); } }; /** A parsed sh(...) descriptor. */ @@ -983,6 +995,11 @@ public: } return {}; } + + std::optional<int64_t> MaxSatisfactionElems() const override { + if (const auto sub_elems = m_subdescriptor_args[0]->MaxSatisfactionElems()) return 1 + *sub_elems; + return {}; + } }; /** A parsed wsh(...) descriptor. */ @@ -1014,6 +1031,11 @@ public: std::optional<int64_t> MaxSatisfactionWeight(bool use_max_sig) const override { return MaxSatSize(use_max_sig); } + + std::optional<int64_t> MaxSatisfactionElems() const override { + if (const auto sub_elems = m_subdescriptor_args[0]->MaxSatisfactionElems()) return 1 + *sub_elems; + return {}; + } }; /** A parsed tr(...) descriptor. */ @@ -1074,6 +1096,11 @@ public: // FIXME: We assume keypath spend, which can lead to very large underestimations. return 1 + 65; } + + std::optional<int64_t> MaxSatisfactionElems() const override { + // FIXME: See above, we assume keypath spend. + return 1; + } }; /* We instantiate Miniscript here with a simple integer as key type. @@ -1164,6 +1191,10 @@ public: // For Miniscript we always assume high-R ECDSA signatures. return m_node->GetWitnessSize(); } + + std::optional<int64_t> MaxSatisfactionElems() const override { + return m_node->GetStackSize(); + } }; /** A parsed rawtr(...) descriptor. */ @@ -1189,6 +1220,11 @@ public: // We can't know whether there is a script path, so assume key path spend. return 1 + 65; } + + std::optional<int64_t> MaxSatisfactionElems() const override { + // See above, we assume keypath spend. + return 1; + } }; //////////////////////////////////////////////////////////////////////////// diff --git a/src/script/descriptor.h b/src/script/descriptor.h index 32f01c9154..caa5d1608d 100644 --- a/src/script/descriptor.h +++ b/src/script/descriptor.h @@ -155,6 +155,9 @@ struct Descriptor { * @param use_max_sig Whether to assume ECDSA signatures will have a high-r. */ virtual std::optional<int64_t> MaxSatisfactionWeight(bool use_max_sig) const = 0; + + /** Get the maximum size number of stack elements for satisfying this descriptor. */ + virtual std::optional<int64_t> MaxSatisfactionElems() const = 0; }; /** Parse a `descriptor` string. Included private keys are put in `out`. |