Skip to content

Commit 51e2cd3

Browse files
committed
Have CalculateMaximumSignedTxSize also compute tx weight
1 parent 81d5af4 commit 51e2cd3

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

src/wallet/feebumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ Result CreateRateBumpTransaction(CWallet& wallet, const uint256& txid, const CCo
190190
if (coin_control.m_feerate) {
191191
// The user provided a feeRate argument.
192192
// We calculate this here to avoid compiler warning on the cs_wallet lock
193-
const int64_t maxTxSize = CalculateMaximumSignedTxSize(*wtx.tx, &wallet);
193+
const int64_t maxTxSize = CalculateMaximumSignedTxSize(*wtx.tx, &wallet).first;
194194
Result res = CheckFeeRate(wallet, wtx, *new_coin_control.m_feerate, maxTxSize, errors);
195195
if (res != Result::OK) {
196196
return res;

src/wallet/wallet.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,14 +1594,15 @@ bool CWallet::ImportScriptPubKeys(const std::string& label, const std::set<CScri
15941594
return true;
15951595
}
15961596

1597-
int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, bool use_max_sig)
1597+
// Returns pair of vsize and weight
1598+
std::pair<int64_t, int64_t> CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, bool use_max_sig)
15981599
{
15991600
std::vector<CTxOut> txouts;
16001601
for (const CTxIn& input : tx.vin) {
16011602
const auto mi = wallet->mapWallet.find(input.prevout.hash);
16021603
// Can not estimate size without knowing the input details
16031604
if (mi == wallet->mapWallet.end()) {
1604-
return -1;
1605+
return std::make_pair(-1, -1);
16051606
}
16061607
assert(input.prevout.n < mi->second.tx->vout.size());
16071608
txouts.emplace_back(mi->second.tx->vout[input.prevout.n]);
@@ -1610,13 +1611,16 @@ int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wall
16101611
}
16111612

16121613
// txouts needs to be in the order of tx.vin
1613-
int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector<CTxOut>& txouts, bool use_max_sig)
1614+
std::pair<int64_t, int64_t> CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector<CTxOut>& txouts, bool use_max_sig)
16141615
{
16151616
CMutableTransaction txNew(tx);
16161617
if (!wallet->DummySignTx(txNew, txouts, use_max_sig)) {
1617-
return -1;
1618+
return std::make_pair(-1, -1);
16181619
}
1619-
return GetVirtualTransactionSize(CTransaction(txNew));
1620+
CTransaction ctx(txNew);
1621+
int64_t vsize = GetVirtualTransactionSize(ctx);
1622+
int64_t weight = GetTransactionWeight(ctx);
1623+
return std::make_pair(vsize, weight);
16201624
}
16211625

16221626
int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* wallet, bool use_max_sig)
@@ -2770,6 +2774,7 @@ bool CWallet::CreateTransactionInternal(
27702774
CMutableTransaction txNew;
27712775
FeeCalculation feeCalc;
27722776
CAmount nFeeNeeded;
2777+
std::pair<int64_t, int64_t> tx_sizes;
27732778
int nBytes;
27742779
{
27752780
std::set<CInputCoin> setCoins;
@@ -2952,7 +2957,8 @@ bool CWallet::CreateTransactionInternal(
29522957
txNew.vin.push_back(CTxIn(coin.outpoint,CScript()));
29532958
}
29542959

2955-
nBytes = CalculateMaximumSignedTxSize(CTransaction(txNew), this, coin_control.fAllowWatchOnly);
2960+
tx_sizes = CalculateMaximumSignedTxSize(CTransaction(txNew), this, coin_control.fAllowWatchOnly);
2961+
nBytes = tx_sizes.first;
29562962
if (nBytes < 0) {
29572963
error = _("Signing transaction failed");
29582964
return false;

src/wallet/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,8 +1321,8 @@ class WalletRescanReserver
13211321
// Use DummySignatureCreator, which inserts 71 byte signatures everywhere.
13221322
// NOTE: this requires that all inputs must be in mapWallet (eg the tx should
13231323
// be IsAllFromMe).
1324-
int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, bool use_max_sig = false) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet);
1325-
int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector<CTxOut>& txouts, bool use_max_sig = false);
1324+
std::pair<int64_t, int64_t> CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, bool use_max_sig = false) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet);
1325+
std::pair<int64_t, int64_t> CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector<CTxOut>& txouts, bool use_max_sig = false);
13261326

13271327
//! Add wallet name to persistent configuration so it will be loaded on startup.
13281328
bool AddWalletSetting(interfaces::Chain& chain, const std::string& wallet_name);

0 commit comments

Comments
 (0)