Skip to content

Commit

Permalink
wallet: fully fix tx credit calculation issues introduced in merge of b…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdorfman committed Oct 27, 2023
1 parent 1d9de10 commit ab8ad42
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
58 changes: 29 additions & 29 deletions src/wallet/receive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,35 @@ bool AllInputsMine(const CWallet& wallet, const CTransaction& tx, const isminefi
}

CAmountMap OutputGetCredit(const CWallet& wallet, const CTransaction& tx, const size_t out_index, const isminefilter& filter) {
std::map<uint256, CWalletTx>::const_iterator mi = wallet.mapWallet.find(tx.GetHash());
if (mi != wallet.mapWallet.end())
{
const CWalletTx& wtx = (*mi).second;
if (out_index < wtx.tx->vout.size() && wallet.IsMine(wtx.tx->vout[out_index]) & filter) {
CAmountMap amounts;
amounts[wtx.GetOutputAsset(wallet, out_index)] = std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, out_index));
return amounts;
}
}
return CAmountMap();
}

CAmountMap TxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter) {
CAmountMap nCredit;
if (wallet.IsMine(tx.vout[out_index]) & filter) {
CWalletTx wtx(MakeTransactionRef(std::move(tx)), TxStateInactive{});
CAmount credit = std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, out_index));
if (!MoneyRange(credit))
throw std::runtime_error(std::string(__func__) + ": value out of range");

nCredit[wtx.GetOutputAsset(wallet, out_index)] += credit;
if (!MoneyRange(nCredit))
throw std::runtime_error(std::string(__func__) + ": value out of range");
{
LOCK(wallet.cs_wallet);

for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
if (wallet.IsMine(wtx.tx->vout[i]) & filter) {
CAmount credit = std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, i));
if (!MoneyRange(credit))
throw std::runtime_error(std::string(__func__) + ": value out of range");

nCredit[wtx.GetOutputAsset(wallet, i)] += credit;
if (!MoneyRange(nCredit))
throw std::runtime_error(std::string(__func__) + ": value out of range");
}
}
}
return nCredit;
}
Expand Down Expand Up @@ -126,7 +145,7 @@ static CAmountMap GetCachableAmount(const CWallet& wallet, const CWalletTx& wtx,
{
auto& amount = wtx.m_amounts[type];
if (recalculate || !amount.m_cached[filter]) {
amount.Set(filter, type == CWalletTx::DEBIT ? wallet.GetDebit(*wtx.tx, filter) : TxGetCredit(wallet, *wtx.tx, filter));
amount.Set(filter, type == CWalletTx::DEBIT ? wallet.GetDebit(*wtx.tx, filter) : TxGetCredit(wallet, wtx, filter));
wtx.m_is_cache_empty = false;
}
return amount.m_value[filter];
Expand All @@ -149,25 +168,6 @@ CAmountMap CachedTxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const
return credit;
}

CAmountMap TxGetCredit(const CWallet& wallet, const CTransaction& tx, const isminefilter& filter)
{
CAmountMap amounts;
{
LOCK(wallet.cs_wallet);
std::map<uint256, CWalletTx>::const_iterator mi = wallet.mapWallet.find(tx.GetHash());
if (mi != wallet.mapWallet.end())
{
const CWalletTx& wtx = (*mi).second;
for (size_t i = 0; i < wtx.tx->vout.size(); ++i) {
if (wallet.IsMine(wtx.tx->vout[i]) & filter) {
amounts[wtx.GetOutputAsset(wallet, i)] = std::max<CAmount>(0, wtx.GetOutputValueOut(wallet, i));
}
}
}
}
return amounts;
}

CAmountMap CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter)
{
if (wtx.tx->vin.empty())
Expand Down
3 changes: 1 addition & 2 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,7 @@ BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
// credit amount is calculated.
wtx.MarkDirty(wallet);
AddKey(wallet, coinbaseKey);
// ELEMENTS: FIXME failing
// BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx)[CAsset()], 50*COIN);
BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx)[CAsset()], 50*COIN);
}

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

0 comments on commit ab8ad42

Please sign in to comment.