Skip to content

Commit dc0526b

Browse files
committed
Fix qt usages of txout-based GetCredit
1 parent 08327e5 commit dc0526b

File tree

5 files changed

+28
-22
lines changed

5 files changed

+28
-22
lines changed

src/interfaces/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,11 @@ class WalletImpl : public Wallet
430430
LOCK(m_wallet.cs_wallet);
431431
return m_wallet.GetDebit(txin, filter);
432432
}
433-
CAmountMap getCredit(const CTxOut& txout, isminefilter filter) override
433+
CAmountMap getCredit(const CTransaction& tx, const size_t out_index, isminefilter filter) override
434434
{
435435
auto locked_chain = m_wallet.chain().lock();
436436
LOCK(m_wallet.cs_wallet);
437-
return m_wallet.GetCredit(txout, filter);
437+
return m_wallet.GetCredit(tx, out_index, filter);
438438
}
439439
CoinsList listCoins() override
440440
{

src/interfaces/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class Wallet
214214
virtual CAmountMap getDebit(const CTxIn& txin, isminefilter filter) = 0;
215215

216216
//! Return credit amount if transaction input belongs to wallet.
217-
virtual CAmountMap getCredit(const CTxOut& txout, isminefilter filter) = 0;
217+
virtual CAmountMap getCredit(const CTransaction& tx, const size_t out_index, isminefilter filter) = 0;
218218

219219
//! Return AvailableCoins + LockedCoins grouped by wallet address.
220220
//! (put change in one group with wallet address)

src/qt/transactiondesc.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wall
136136
// Coinbase
137137
//
138138
CAmount nUnmatured = 0;
139-
for (const CTxOut& txout : wtx.tx->vout)
140-
nUnmatured += valueFor(wallet.getCredit(txout, ISMINE_ALL), ::policyAsset);
139+
for (size_t nOut = 0; nOut < wtx.tx->vout.size(); nOut++)
140+
nUnmatured += valueFor(wallet.getCredit(*(wtx.tx), nOut, ISMINE_ALL), ::policyAsset);
141141
strHTML += "<b>" + tr("Credit") + ":</b> ";
142142
if (status.is_in_main_chain)
143143
strHTML += BitcoinUnits::formatHtmlWithUnit(unit, nUnmatured)+ " (" + tr("matures in %n more block(s)", "", status.blocks_to_maturity) + ")";
@@ -233,9 +233,9 @@ QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wall
233233
}
234234
}
235235
mine = wtx.txout_is_mine.begin();
236-
for (const CTxOut& txout : wtx.tx->vout) {
236+
for (size_t nOut = 0; nOut < wtx.tx->vout.size(); nOut++) {
237237
if (*(mine++)) {
238-
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, valueFor(wallet.getCredit(txout, ISMINE_ALL), ::policyAsset)) + "<br>";
238+
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, valueFor(wallet.getCredit(*(wtx.tx), nOut, ISMINE_ALL), ::policyAsset)) + "<br>";
239239
}
240240
}
241241
}
@@ -293,9 +293,12 @@ QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wall
293293
for (const CTxIn& txin : wtx.tx->vin)
294294
if(wallet.txinIsMine(txin))
295295
strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -valueFor(wallet.getDebit(txin, ISMINE_ALL), ::policyAsset)) + "<br>";
296-
for (const CTxOut& txout : wtx.tx->vout)
297-
if(wallet.txoutIsMine(txout))
298-
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, valueFor(wallet.getCredit(txout, ISMINE_ALL), ::policyAsset)) + "<br>";
296+
for (size_t nOut = 0; nOut < wtx.tx->vout.size(); nOut++) {
297+
const CTxOut& txout = wtx.tx->vout[nOut];
298+
if(wallet.txoutIsMine(txout)) {
299+
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, valueFor(wallet.getCredit(*(wtx->tx), nOut, ISMINE_ALL), ::policyAsset)) + "<br>";
300+
}
301+
}
299302

300303
strHTML += "<br><b>" + tr("Transaction") + ":</b><br>";
301304
strHTML += GUIUtil::HtmlEscape(wtx.tx->ToString(), true);

src/wallet/wallet.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,19 +1269,22 @@ isminetype CWallet::IsMine(const CTxOut& txout) const
12691269
return ::IsMine(*this, txout.scriptPubKey);
12701270
}
12711271

1272-
CAmountMap CWallet::GetCredit(const CTxOut& txout, const isminefilter& filter) const
1272+
CAmountMap CWallet::GetCredit(const CTransaction& tx, const size_t out_index, const isminefilter& filter) const
12731273
{
1274-
assert(false && "CWallet::GetCredit(const CTxOut&, const isminefilter&): this method should not be used anymore");
1275-
1276-
CAmountMap credit;
1277-
if (txout.nAsset.IsExplicit() && txout.nValue.IsExplicit()) {
1278-
credit[txout.nAsset.GetAsset()] = txout.nValue.GetAmount();
1279-
} else {
1280-
WalletLogPrintf("WARNING: Calculating credit of blinded transaction.\n");
1274+
{
1275+
LOCK(cs_wallet);
1276+
std::map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(tx.GetHash());
1277+
if (mi != mapWallet.end())
1278+
{
1279+
const CWalletTx& wtx = (*mi).second;
1280+
if (out_index < wtx.tx->vout.size() && IsMine(wtx.tx->vout[out_index]) & filter) {
1281+
CAmountMap amounts;
1282+
amounts[wtx.GetOutputAsset(out_index)] = std::max<CAmount>(0, wtx.GetOutputValueOut(out_index));
1283+
return amounts;
1284+
}
1285+
}
12811286
}
1282-
if (!MoneyRange(credit))
1283-
throw std::runtime_error(std::string(__func__) + ": value out of range");
1284-
return ((IsMine(txout) & filter) ? credit : CAmountMap());
1287+
return CAmountMap();
12851288
}
12861289

12871290
bool CWallet::IsChange(const CTxOut& txout) const

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
11021102
*/
11031103
CAmountMap GetDebit(const CTxIn& txin, const isminefilter& filter) const;
11041104
isminetype IsMine(const CTxOut& txout) const;
1105-
CAmountMap GetCredit(const CTxOut& txout, const isminefilter& filter) const;
1105+
CAmountMap GetCredit(const CTransaction& tx, const size_t out_index, const isminefilter& filter) const;
11061106
bool IsChange(const CTxOut& txout) const;
11071107
bool IsChange(const CScript& script) const;
11081108
CAmountMap GetChange(const CTxOut& txout) const;

0 commit comments

Comments
 (0)