Skip to content

Commit 198fcca

Browse files
committed
wallet: refactor, include 'FeeCalculation' inside 'CreatedTransactionResult'
1 parent 7a45c33 commit 198fcca

File tree

8 files changed

+15
-25
lines changed

8 files changed

+15
-25
lines changed

src/wallet/feebumper.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ Result CreateRateBumpTransaction(CWallet& wallet, const uint256& txid, const CCo
220220

221221
constexpr int RANDOM_CHANGE_POSITION = -1;
222222
bilingual_str fail_reason;
223-
FeeCalculation fee_calc_out;
224-
std::optional<CreatedTransactionResult> txr = CreateTransaction(wallet, recipients, RANDOM_CHANGE_POSITION, fail_reason, new_coin_control, fee_calc_out, false);
223+
std::optional<CreatedTransactionResult> txr = CreateTransaction(wallet, recipients, RANDOM_CHANGE_POSITION, fail_reason, new_coin_control, false);
225224
if (!txr) {
226225
errors.push_back(Untranslated("Unable to create transaction.") + Untranslated(" ") + fail_reason);
227226
return Result::WALLET_ERROR;

src/wallet/interfaces.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,8 @@ class WalletImpl : public Wallet
258258
bilingual_str& fail_reason) override
259259
{
260260
LOCK(m_wallet->cs_wallet);
261-
FeeCalculation fee_calc_out;
262261
std::optional<CreatedTransactionResult> txr = CreateTransaction(*m_wallet, recipients, change_pos,
263-
fail_reason, coin_control, fee_calc_out, sign);
262+
fail_reason, coin_control, sign);
264263
if (!txr) return {};
265264
fee = txr->fee;
266265
change_pos = txr->change_pos;

src/wallet/rpc/spend.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ UniValue SendMoney(CWallet& wallet, const CCoinControl &coin_control, std::vecto
157157
// Send
158158
constexpr int RANDOM_CHANGE_POSITION = -1;
159159
bilingual_str error;
160-
FeeCalculation fee_calc_out;
161-
std::optional<CreatedTransactionResult> txr = CreateTransaction(wallet, recipients, RANDOM_CHANGE_POSITION, error, coin_control, fee_calc_out, true);
160+
std::optional<CreatedTransactionResult> txr = CreateTransaction(wallet, recipients, RANDOM_CHANGE_POSITION, error, coin_control, true);
162161
if (!txr) {
163162
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, error.original);
164163
}
@@ -167,7 +166,7 @@ UniValue SendMoney(CWallet& wallet, const CCoinControl &coin_control, std::vecto
167166
if (verbose) {
168167
UniValue entry(UniValue::VOBJ);
169168
entry.pushKV("txid", tx->GetHash().GetHex());
170-
entry.pushKV("fee_reason", StringForFeeReason(fee_calc_out.reason));
169+
entry.pushKV("fee_reason", StringForFeeReason(txr->fee_calc.reason));
171170
return entry;
172171
}
173172
return tx->GetHash().GetHex();

src/wallet/spend.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,6 @@ static std::optional<CreatedTransactionResult> CreateTransactionInternal(
662662
int change_pos,
663663
bilingual_str& error,
664664
const CCoinControl& coin_control,
665-
FeeCalculation& fee_calc_out,
666665
bool sign) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
667666
{
668667
AssertLockHeld(wallet.cs_wallet);
@@ -954,7 +953,6 @@ static std::optional<CreatedTransactionResult> CreateTransactionInternal(
954953
// Before we return success, we assume any change key will be used to prevent
955954
// accidental re-use.
956955
reservedest.KeepDestination();
957-
fee_calc_out = feeCalc;
958956

959957
wallet.WalletLogPrintf("Fee Calculation: Fee:%d Bytes:%u Tgt:%d (requested %d) Reason:\"%s\" Decay %.5f: Estimation: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out) Fail: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out)\n",
960958
nFeeRet, nBytes, feeCalc.returnedTarget, feeCalc.desiredTarget, StringForFeeReason(feeCalc.reason), feeCalc.est.decay,
@@ -964,7 +962,7 @@ static std::optional<CreatedTransactionResult> CreateTransactionInternal(
964962
feeCalc.est.fail.start, feeCalc.est.fail.end,
965963
(feeCalc.est.fail.totalConfirmed + feeCalc.est.fail.inMempool + feeCalc.est.fail.leftMempool) > 0.0 ? 100 * feeCalc.est.fail.withinTarget / (feeCalc.est.fail.totalConfirmed + feeCalc.est.fail.inMempool + feeCalc.est.fail.leftMempool) : 0.0,
966964
feeCalc.est.fail.withinTarget, feeCalc.est.fail.totalConfirmed, feeCalc.est.fail.inMempool, feeCalc.est.fail.leftMempool);
967-
return CreatedTransactionResult(tx, nFeeRet, nChangePosInOut);
965+
return CreatedTransactionResult(tx, nFeeRet, nChangePosInOut, feeCalc);
968966
}
969967

970968
std::optional<CreatedTransactionResult> CreateTransaction(
@@ -973,7 +971,6 @@ std::optional<CreatedTransactionResult> CreateTransaction(
973971
int change_pos,
974972
bilingual_str& error,
975973
const CCoinControl& coin_control,
976-
FeeCalculation& fee_calc_out,
977974
bool sign)
978975
{
979976
if (vecSend.empty()) {
@@ -988,7 +985,7 @@ std::optional<CreatedTransactionResult> CreateTransaction(
988985

989986
LOCK(wallet.cs_wallet);
990987

991-
std::optional<CreatedTransactionResult> txr_ungrouped = CreateTransactionInternal(wallet, vecSend, change_pos, error, coin_control, fee_calc_out, sign);
988+
std::optional<CreatedTransactionResult> txr_ungrouped = CreateTransactionInternal(wallet, vecSend, change_pos, error, coin_control, sign);
992989
TRACE4(coin_selection, normal_create_tx_internal, wallet.GetName().c_str(), txr_ungrouped.has_value(),
993990
txr_ungrouped.has_value() ? txr_ungrouped->fee : 0, txr_ungrouped.has_value() ? txr_ungrouped->change_pos : 0);
994991
if (!txr_ungrouped) return std::nullopt;
@@ -998,7 +995,7 @@ std::optional<CreatedTransactionResult> CreateTransaction(
998995
CCoinControl tmp_cc = coin_control;
999996
tmp_cc.m_avoid_partial_spends = true;
1000997
bilingual_str error2; // fired and forgotten; if an error occurs, we discard the results
1001-
std::optional<CreatedTransactionResult> txr_grouped = CreateTransactionInternal(wallet, vecSend, change_pos, error2, tmp_cc, fee_calc_out, sign);
998+
std::optional<CreatedTransactionResult> txr_grouped = CreateTransactionInternal(wallet, vecSend, change_pos, error2, tmp_cc, sign);
1002999
// if fee of this alternative one is within the range of the max fee, we use this one
10031000
const bool use_aps{txr_grouped.has_value() ? (txr_grouped->fee <= txr_ungrouped->fee + wallet.m_max_aps_fee) : false};
10041001
TRACE5(coin_selection, aps_create_tx_internal, wallet.GetName().c_str(), use_aps, txr_grouped.has_value(),
@@ -1045,8 +1042,7 @@ bool FundTransaction(CWallet& wallet, CMutableTransaction& tx, CAmount& nFeeRet,
10451042
}
10461043
}
10471044

1048-
FeeCalculation fee_calc_out;
1049-
std::optional<CreatedTransactionResult> txr = CreateTransaction(wallet, vecSend, nChangePosInOut, error, coinControl, fee_calc_out, false);
1045+
std::optional<CreatedTransactionResult> txr = CreateTransaction(wallet, vecSend, nChangePosInOut, error, coinControl, false);
10501046
if (!txr) return false;
10511047
CTransactionRef tx_new = txr->tx;
10521048
nFeeRet = txr->fee;

src/wallet/spend.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_WALLET_SPEND_H
77

88
#include <consensus/amount.h>
9+
#include <policy/fees.h> // for FeeCalculation
910
#include <wallet/coinselection.h>
1011
#include <wallet/transaction.h>
1112
#include <wallet/wallet.h>
@@ -107,18 +108,19 @@ struct CreatedTransactionResult
107108
{
108109
CTransactionRef tx;
109110
CAmount fee;
111+
FeeCalculation fee_calc;
110112
int change_pos;
111113

112-
CreatedTransactionResult(CTransactionRef tx, CAmount fee, int change_pos)
113-
: tx(tx), fee(fee), change_pos(change_pos) {}
114+
CreatedTransactionResult(CTransactionRef _tx, CAmount _fee, int _change_pos, const FeeCalculation& _fee_calc)
115+
: tx(_tx), fee(_fee), fee_calc(_fee_calc), change_pos(_change_pos) {}
114116
};
115117

116118
/**
117119
* Create a new transaction paying the recipients with a set of coins
118120
* selected by SelectCoins(); Also create the change output, when needed
119121
* @note passing change_pos as -1 will result in setting a random position
120122
*/
121-
std::optional<CreatedTransactionResult> CreateTransaction(CWallet& wallet, const std::vector<CRecipient>& vecSend, int change_pos, bilingual_str& error, const CCoinControl& coin_control, FeeCalculation& fee_calc_out, bool sign = true);
123+
std::optional<CreatedTransactionResult> CreateTransaction(CWallet& wallet, const std::vector<CRecipient>& vecSend, int change_pos, bilingual_str& error, const CCoinControl& coin_control, bool sign = true);
122124

123125
/**
124126
* Insert additional inputs into the transaction by

src/wallet/test/spend_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ BOOST_FIXTURE_TEST_CASE(SubtractFee, TestChain100Setup)
3434
coin_control.fOverrideFeeRate = true;
3535
// We need to use a change type with high cost of change so that the leftover amount will be dropped to fee instead of added as a change output
3636
coin_control.m_change_type = OutputType::LEGACY;
37-
FeeCalculation fee_calc;
38-
std::optional<CreatedTransactionResult> txr = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, error, coin_control, fee_calc);
37+
std::optional<CreatedTransactionResult> txr = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, error, coin_control);
3938
BOOST_CHECK(txr.has_value());
4039
BOOST_CHECK_EQUAL(txr->tx->vout.size(), 1);
4140
BOOST_CHECK_EQUAL(txr->tx->vout[0].nValue, recipient.nAmount + leftover_input_amount - txr->fee);

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <wallet/wallet.h>
66

7-
#include <any>
87
#include <future>
98
#include <memory>
109
#include <stdint.h>
@@ -13,7 +12,6 @@
1312
#include <interfaces/chain.h>
1413
#include <key_io.h>
1514
#include <node/blockstorage.h>
16-
#include <node/context.h>
1715
#include <policy/policy.h>
1816
#include <rpc/server.h>
1917
#include <test/util/logging.h>
@@ -523,10 +521,9 @@ class ListCoinsTestingSetup : public TestChain100Setup
523521
CTransactionRef tx;
524522
bilingual_str error;
525523
CCoinControl dummy;
526-
FeeCalculation fee_calc_out;
527524
{
528525
constexpr int RANDOM_CHANGE_POSITION = -1;
529-
std::optional<CreatedTransactionResult> txr = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, error, dummy, fee_calc_out);
526+
std::optional<CreatedTransactionResult> txr = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, error, dummy);
530527
BOOST_CHECK(txr.has_value());
531528
tx = txr->tx;
532529
}

src/wallet/wallet.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ using LoadWalletFn = std::function<void(std::unique_ptr<interfaces::Wallet> wall
4545

4646
class CScript;
4747
enum class FeeEstimateMode;
48-
struct FeeCalculation;
4948
struct bilingual_str;
5049

5150
namespace wallet {

0 commit comments

Comments
 (0)