Branch data Line data Source code
1 : : // Copyright (c) 2017-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef BITCOIN_WALLET_FEEBUMPER_H
6 : : #define BITCOIN_WALLET_FEEBUMPER_H
7 : :
8 : : #include <consensus/consensus.h>
9 : : #include <primitives/transaction.h>
10 : : #include <script/interpreter.h>
11 : : #include <util/check.h>
12 : :
13 : : class uint256;
14 : : enum class FeeEstimateMode;
15 : : struct bilingual_str;
16 : :
17 : : namespace wallet {
18 : : class CCoinControl;
19 : : class CWallet;
20 : : class CWalletTx;
21 : :
22 : : namespace feebumper {
23 : :
24 : : enum class Result
25 : : {
26 : : OK,
27 : : INVALID_ADDRESS_OR_KEY,
28 : : INVALID_REQUEST,
29 : : INVALID_PARAMETER,
30 : : WALLET_ERROR,
31 : : MISC_ERROR,
32 : : };
33 : :
34 : : //! Return whether transaction can be bumped.
35 : : bool TransactionCanBeBumped(const CWallet& wallet, const Txid& txid);
36 : :
37 : : /** Create bumpfee transaction based on feerate estimates.
38 : : *
39 : : * @param[in] wallet The wallet to use for this bumping
40 : : * @param[in] txid The txid of the transaction to bump
41 : : * @param[in] coin_control A CCoinControl object which provides feerates and other information used for coin selection
42 : : * @param[out] errors Errors
43 : : * @param[out] old_fee The fee the original transaction pays
44 : : * @param[out] new_fee the fee that the bump transaction pays
45 : : * @param[out] mtx The bump transaction itself
46 : : * @param[in] require_mine Whether the original transaction must consist of inputs that can be spent by the wallet
47 : : * @param[in] outputs Vector of new outputs to replace the bumped transaction's outputs
48 : : * @param[in] original_change_index The position of the change output to deduct the fee from in the transaction being bumped
49 : : */
50 : : Result CreateRateBumpTransaction(CWallet& wallet,
51 : : const Txid& txid,
52 : : const CCoinControl& coin_control,
53 : : std::vector<bilingual_str>& errors,
54 : : CAmount& old_fee,
55 : : CAmount& new_fee,
56 : : CMutableTransaction& mtx,
57 : : bool require_mine,
58 : : const std::vector<CTxOut>& outputs,
59 : : std::optional<uint32_t> original_change_index = std::nullopt);
60 : :
61 : : //! Sign the new transaction,
62 : : //! @return false if the tx couldn't be found or if it was
63 : : //! impossible to create the signature(s)
64 : : bool SignTransaction(CWallet& wallet, CMutableTransaction& mtx);
65 : :
66 : : //! Commit the bumpfee transaction.
67 : : //! @return success in case of CWallet::CommitTransaction was successful,
68 : : //! but sets errors if the tx could not be added to the mempool (will try later)
69 : : //! or if the old transaction could not be marked as replaced.
70 : : Result CommitTransaction(CWallet& wallet,
71 : : const Txid& txid,
72 : : CMutableTransaction&& mtx,
73 : : std::vector<bilingual_str>& errors,
74 : : Txid& bumped_txid);
75 : :
76 : : struct SignatureWeights
77 : : {
78 : : private:
79 : : int m_sigs_count{0};
80 : : int64_t m_sigs_weight{0};
81 : :
82 : : public:
83 : 4 : void AddSigWeight(const size_t weight, const SigVersion sigversion)
84 : : {
85 [ + + - - ]: 4 : switch (sigversion) {
86 : 1 : case SigVersion::BASE:
87 : 1 : m_sigs_weight += weight * WITNESS_SCALE_FACTOR;
88 : 1 : m_sigs_count += 1 * WITNESS_SCALE_FACTOR;
89 : 1 : break;
90 : 3 : case SigVersion::WITNESS_V0:
91 : 3 : m_sigs_weight += weight;
92 : 3 : m_sigs_count++;
93 : 3 : break;
94 : 0 : case SigVersion::TAPROOT:
95 : 0 : case SigVersion::TAPSCRIPT:
96 : 0 : AssertUnreachable();
97 : : }
98 : 4 : }
99 : :
100 : 4 : int64_t GetWeightDiffToMax() const
101 : : {
102 : : // Note: the witness scaling factor is already accounted for because the count is multiplied by it.
103 [ + - ]: 4 : return (/* max signature size=*/ 72 * m_sigs_count) - m_sigs_weight;
104 : : }
105 : : };
106 : :
107 : 4 : class SignatureWeightChecker : public DeferringSignatureChecker
108 : : {
109 : : private:
110 : : SignatureWeights& m_weights;
111 : :
112 : : public:
113 [ + - ]: 4 : SignatureWeightChecker(SignatureWeights& weights, const BaseSignatureChecker& checker) : DeferringSignatureChecker(checker), m_weights(weights) {}
114 : :
115 : 4 : bool CheckECDSASignature(const std::vector<unsigned char>& sig, const std::vector<unsigned char>& pubkey, const CScript& script, SigVersion sigversion) const override
116 : : {
117 [ + - ]: 4 : if (m_checker.CheckECDSASignature(sig, pubkey, script, sigversion)) {
118 [ - + ]: 4 : m_weights.AddSigWeight(sig.size(), sigversion);
119 : 4 : return true;
120 : : }
121 : : return false;
122 : : }
123 : : };
124 : :
125 : : } // namespace feebumper
126 : : } // namespace wallet
127 : :
128 : : #endif // BITCOIN_WALLET_FEEBUMPER_H
|