Skip to content

Commit

Permalink
Merge bitcoin#10368: [wallet] Remove helper conversion operator from …
Browse files Browse the repository at this point in the history
…wallet

5a5e4e9 [wallet] Remove CTransaction&() helper conversion operator from wallet implementation. (Karl-Johan Alm)

Pull request description:

  The `CTransaction&()` operator in `CMerkleTx` makes conversion into `CTransaction`s transparent, but was marked as to-be-removed in favor of explicitly getting the `tx` ivar, presumably as the operator can lead to ambiguous behavior and makes the code harder to follow.

  This PR removes the operator and adapts callers. This includes some cases of `static_cast<CTransaction>(wtx)` → `*wtx.tx`, which is definitely an improvement.

Tree-SHA512: 95856fec7194d6a79615ea1c322abfcd6bcedf6ffd0cfa89bbdd332ce13035fa52dd4b828d20df673072dde1be64b79c513529a6f422dd5f0961ce722a32d56a
  • Loading branch information
laanwj authored and PastaPastaPasta committed Jan 12, 2020
1 parent 660d380 commit 03e4213
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/qt/transactiondesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
QString TransactionDesc::FormatTxStatus(const CWalletTx& wtx)
{
AssertLockHeld(cs_main);
if (!CheckFinalTx(wtx))
if (!CheckFinalTx(*wtx.tx))
{
if (wtx.tx->nLockTime < LOCKTIME_THRESHOLD)
return tr("Open for %n more block(s)", "", wtx.tx->nLockTime - chainActive.Height());
Expand Down
2 changes: 1 addition & 1 deletion src/qt/transactionrecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void TransactionRecord::updateStatus(const CWalletTx &wtx, int chainLockHeight)
status.label = QString::fromStdString(addrBookIt->second.name);
}

if (!CheckFinalTx(wtx))
if (!CheckFinalTx(*wtx.tx))
{
if (wtx.tx->nLockTime < LOCKTIME_THRESHOLD)
{
Expand Down
2 changes: 1 addition & 1 deletion src/qt/transactiontablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class TransactionTablePriv
std::map<uint256, CWalletTx>::iterator mi = wallet->mapWallet.find(rec->hash);
if(mi != wallet->mapWallet.end())
{
std::string strHex = EncodeHexTx(static_cast<CTransaction>(mi->second));
std::string strHex = EncodeHexTx(*mi->second.tx);
return QString::fromStdString(strHex);
}
return QString();
Expand Down
2 changes: 1 addition & 1 deletion src/qt/walletmodeltransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ CWalletTx *WalletModelTransaction::getTransaction() const

unsigned int WalletModelTransaction::getTransactionSize()
{
return (!walletTransaction ? 0 : (::GetSerializeSize(walletTransaction->tx, SER_NETWORK, PROTOCOL_VERSION)));
return (!walletTransaction ? 0 : (::GetSerializeSize(*walletTransaction->tx, SER_NETWORK, PROTOCOL_VERSION)));
}

CAmount WalletModelTransaction::getTransactionFee() const
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ UniValue importprunedfunds(const JSONRPCRequest& request)

LOCK2(cs_main, pwallet->cs_wallet);

if (pwallet->IsMine(wtx)) {
if (pwallet->IsMine(*wtx.tx)) {
pwallet->AddToWallet(wtx, false);
return NullUniValue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,7 @@ UniValue gettransaction(const JSONRPCRequest& request)
ListTransactions(pwallet, wtx, "*", 0, false, details, filter);
entry.push_back(Pair("details", details));

std::string strHex = EncodeHexTx(static_cast<CTransaction>(wtx));
std::string strHex = EncodeHexTx(*wtx.tx);
entry.push_back(Pair("hex", strHex));

return entry;
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/test/accounting_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)

wtx.mapValue["comment"] = "y";
{
CMutableTransaction tx(wtx);
CMutableTransaction tx(*wtx.tx);
--tx.nLockTime; // Just to change the hash :)
wtx.SetTx(MakeTransactionRef(std::move(tx)));
}
Expand All @@ -93,7 +93,7 @@ BOOST_AUTO_TEST_CASE(acc_orderupgrade)

wtx.mapValue["comment"] = "x";
{
CMutableTransaction tx(wtx);
CMutableTransaction tx(*wtx.tx);
--tx.nLockTime; // Just to change the hash :)
wtx.SetTx(MakeTransactionRef(std::move(tx)));
}
Expand Down
23 changes: 12 additions & 11 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1494,14 +1494,15 @@ int CWallet::GetRealOutpointPrivateSendRounds(const COutPoint& outpoint, int nRo
uint256 hash = outpoint.hash;
unsigned int nout = outpoint.n;

// TODO wtx should refer to a CWalletTx object, not a pointer, based on surrounding code
const CWalletTx* wtx = GetWalletTx(hash);
if(wtx != nullptr)
{
std::map<uint256, CMutableTransaction>::const_iterator mdwi = mDenomWtxes.find(hash);
if (mdwi == mDenomWtxes.end()) {
// not known yet, let's add it
LogPrint(BCLog::PRIVATESEND, "GetRealOutpointPrivateSendRounds INSERTING %s\n", hash.ToString());
mDenomWtxes[hash] = CMutableTransaction(*wtx);
mDenomWtxes[hash] = CMutableTransaction(*wtx->tx);
} else if(mDenomWtxes[hash].vout[nout].nRounds != -10) {
// found and it's not an initial value, just return it
return mDenomWtxes[hash].vout[nout].nRounds;
Expand Down Expand Up @@ -2044,7 +2045,7 @@ bool CWalletTx::RelayWalletTransaction(CConnman* connman)
LogPrintf("Relaying wtx %s\n", hash.ToString());

if (connman) {
connman->RelayTransaction((CTransaction)*this);
connman->RelayTransaction(*tx);
return true;
}
}
Expand Down Expand Up @@ -2076,7 +2077,7 @@ CAmount CWalletTx::GetDebit(const isminefilter& filter) const
debit += nDebitCached;
else
{
nDebitCached = pwallet->GetDebit(*this, ISMINE_SPENDABLE);
nDebitCached = pwallet->GetDebit(*tx, ISMINE_SPENDABLE);
fDebitCached = true;
debit += nDebitCached;
}
Expand All @@ -2087,7 +2088,7 @@ CAmount CWalletTx::GetDebit(const isminefilter& filter) const
debit += nWatchDebitCached;
else
{
nWatchDebitCached = pwallet->GetDebit(*this, ISMINE_WATCH_ONLY);
nWatchDebitCached = pwallet->GetDebit(*tx, ISMINE_WATCH_ONLY);
fWatchDebitCached = true;
debit += nWatchDebitCached;
}
Expand All @@ -2109,7 +2110,7 @@ CAmount CWalletTx::GetCredit(const isminefilter& filter) const
credit += nCreditCached;
else
{
nCreditCached = pwallet->GetCredit(*this, ISMINE_SPENDABLE);
nCreditCached = pwallet->GetCredit(*tx, ISMINE_SPENDABLE);
fCreditCached = true;
credit += nCreditCached;
}
Expand All @@ -2120,7 +2121,7 @@ CAmount CWalletTx::GetCredit(const isminefilter& filter) const
credit += nWatchCreditCached;
else
{
nWatchCreditCached = pwallet->GetCredit(*this, ISMINE_WATCH_ONLY);
nWatchCreditCached = pwallet->GetCredit(*tx, ISMINE_WATCH_ONLY);
fWatchCreditCached = true;
credit += nWatchCreditCached;
}
Expand All @@ -2134,7 +2135,7 @@ CAmount CWalletTx::GetImmatureCredit(bool fUseCache) const
{
if (fUseCache && fImmatureCreditCached)
return nImmatureCreditCached;
nImmatureCreditCached = pwallet->GetCredit(*this, ISMINE_SPENDABLE);
nImmatureCreditCached = pwallet->GetCredit(*tx, ISMINE_SPENDABLE);
fImmatureCreditCached = true;
return nImmatureCreditCached;
}
Expand Down Expand Up @@ -2178,7 +2179,7 @@ CAmount CWalletTx::GetImmatureWatchOnlyCredit(const bool& fUseCache) const
{
if (fUseCache && fImmatureWatchCreditCached)
return nImmatureWatchCreditCached;
nImmatureWatchCreditCached = pwallet->GetCredit(*this, ISMINE_WATCH_ONLY);
nImmatureWatchCreditCached = pwallet->GetCredit(*tx, ISMINE_WATCH_ONLY);
fImmatureWatchCreditCached = true;
return nImmatureWatchCreditCached;
}
Expand Down Expand Up @@ -2298,7 +2299,7 @@ CAmount CWalletTx::GetChange() const
{
if (fChangeCached)
return nChangeCached;
nChangeCached = pwallet->GetChange(*this);
nChangeCached = pwallet->GetChange(*tx);
fChangeCached = true;
return nChangeCached;
}
Expand All @@ -2312,7 +2313,7 @@ bool CWalletTx::InMempool() const
bool CWalletTx::IsTrusted() const
{
// Quick answer in most cases
if (!CheckFinalTx(*this))
if (!CheckFinalTx(*tx))
return false;
int nDepth = GetDepthInMainChain();
if (nDepth >= 1)
Expand Down Expand Up @@ -2678,7 +2679,7 @@ void CWallet::AvailableCoins(std::vector<COutput> &vCoins, bool fOnlySafe, const
for (auto pcoin : GetSpendableTXs()) {
const uint256& wtxid = pcoin->GetHash();

if (!CheckFinalTx(*pcoin))
if (!CheckFinalTx(*pcoin->tx))
continue;

if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
Expand Down
4 changes: 0 additions & 4 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,6 @@ class CMerkleTx
Init();
}

/** Helper conversion operator to allow passing CMerkleTx where CTransaction is expected.
* TODO: adapt callers and remove this operator. */
operator const CTransaction&() const { return *tx; }

void Init()
{
hashBlock = uint256();
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
CWalletTx wtx;
ssValue >> wtx;
CValidationState state;
if (!(CheckTransaction(wtx, state) && (wtx.GetHash() == hash) && state.IsValid()))
if (!(CheckTransaction(*wtx.tx, state) && (wtx.GetHash() == hash) && state.IsValid()))
return false;

// Undo serialize changes in 31600
Expand Down

0 comments on commit 03e4213

Please sign in to comment.