Skip to content

Commit 39be0f0

Browse files
committed
wallet: fix tx credit calculation issues introduced in merge of bitcoin/bitcoin#22100 in 3e939cc and 103069c
1 parent 04cba29 commit 39be0f0

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

src/wallet/receive.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,35 @@ bool AllInputsMine(const CWallet& wallet, const CTransaction& tx, const isminefi
5252
}
5353

5454
CAmountMap OutputGetCredit(const CWallet& wallet, const CTransaction& tx, const size_t out_index, const isminefilter& filter) {
55+
std::map<uint256, CWalletTx>::const_iterator mi = wallet.mapWallet.find(tx.GetHash());
56+
if (mi != wallet.mapWallet.end())
57+
{
58+
const CWalletTx& wtx = (*mi).second;
59+
if (out_index < wtx.tx->vout.size() && wallet.IsMine(wtx.tx->vout[out_index]) & filter) {
60+
CAmountMap amounts;
61+
amounts[wtx.GetOutputAsset(wallet, out_index)] = std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, out_index));
62+
return amounts;
63+
}
64+
}
65+
return CAmountMap();
66+
}
67+
68+
CAmountMap TxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter) {
5569
CAmountMap nCredit;
56-
if (wallet.IsMine(tx.vout[out_index]) & filter) {
57-
CWalletTx wtx(MakeTransactionRef(std::move(tx)), TxStateInactive{});
58-
CAmount credit = std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, out_index));
59-
if (!MoneyRange(credit))
60-
throw std::runtime_error(std::string(__func__) + ": value out of range");
61-
62-
nCredit[wtx.GetOutputAsset(wallet, out_index)] += credit;
63-
if (!MoneyRange(nCredit))
64-
throw std::runtime_error(std::string(__func__) + ": value out of range");
70+
{
71+
LOCK(wallet.cs_wallet);
72+
73+
for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
74+
if (wallet.IsMine(wtx.tx->vout[i]) & filter) {
75+
CAmount credit = std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, i));
76+
if (!MoneyRange(credit))
77+
throw std::runtime_error(std::string(__func__) + ": value out of range");
78+
79+
nCredit[wtx.GetOutputAsset(wallet, i)] += credit;
80+
if (!MoneyRange(nCredit))
81+
throw std::runtime_error(std::string(__func__) + ": value out of range");
82+
}
83+
}
6584
}
6685
return nCredit;
6786
}
@@ -126,7 +145,7 @@ static CAmountMap GetCachableAmount(const CWallet& wallet, const CWalletTx& wtx,
126145
{
127146
auto& amount = wtx.m_amounts[type];
128147
if (recalculate || !amount.m_cached[filter]) {
129-
amount.Set(filter, type == CWalletTx::DEBIT ? wallet.GetDebit(*wtx.tx, filter) : TxGetCredit(wallet, *wtx.tx, filter));
148+
amount.Set(filter, type == CWalletTx::DEBIT ? wallet.GetDebit(*wtx.tx, filter) : TxGetCredit(wallet, wtx, filter));
130149
wtx.m_is_cache_empty = false;
131150
}
132151
return amount.m_value[filter];
@@ -149,26 +168,6 @@ CAmountMap CachedTxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const
149168
return credit;
150169
}
151170

152-
CAmountMap TxGetCredit(const CWallet& wallet, const CTransaction& tx, const isminefilter& filter)
153-
{
154-
{
155-
LOCK(wallet.cs_wallet);
156-
std::map<uint256, CWalletTx>::const_iterator mi = wallet.mapWallet.find(tx.GetHash());
157-
if (mi != wallet.mapWallet.end())
158-
{
159-
const CWalletTx& wtx = (*mi).second;
160-
for (size_t i = 0; i < wtx.tx->vout.size(); ++i) {
161-
if (wallet.IsMine(wtx.tx->vout[i]) & filter) {
162-
CAmountMap amounts;
163-
amounts[wtx.GetOutputAsset(wallet, i)] = std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, i));
164-
return amounts;
165-
}
166-
}
167-
}
168-
}
169-
return CAmountMap();
170-
}
171-
172171
CAmountMap CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter)
173172
{
174173
if (wtx.tx->vin.empty())

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,7 @@ BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
359359
// credit amount is calculated.
360360
wtx.MarkDirty(wallet);
361361
AddKey(wallet, coinbaseKey);
362-
// ELEMENTS: FIXME failing
363-
// BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx)[CAsset()], 50*COIN);
362+
BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx)[CAsset()], 50*COIN);
364363
}
365364

366365
static int64_t AddTx(ChainstateManager& chainman, CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)

0 commit comments

Comments
 (0)