Skip to content

Commit 65b9d8f

Browse files
committed
Avoid copying CWalletTx in LoadToWallet
The change in walletdb.cpp is easier to review ignoring whitespace. This change is need to get rid of CWalletTx copy constructor.
1 parent bd2fbc7 commit 65b9d8f

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

src/wallet/wallet.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -879,32 +879,33 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
879879
return &wtx;
880880
}
881881

882-
void CWallet::LoadToWallet(CWalletTx& wtxIn)
882+
bool CWallet::LoadToWallet(const uint256& hash, const UpdateWalletTxFn& fill_wtx)
883883
{
884+
const auto& ins = mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(hash), std::forward_as_tuple(this, nullptr));
885+
CWalletTx& wtx = ins.first->second;
886+
if (!fill_wtx(wtx, ins.second)) {
887+
return false;
888+
}
884889
// If wallet doesn't have a chain (e.g wallet-tool), don't bother to update txn.
885890
if (HaveChain()) {
886-
Optional<int> block_height = chain().getBlockHeight(wtxIn.m_confirm.hashBlock);
891+
Optional<int> block_height = chain().getBlockHeight(wtx.m_confirm.hashBlock);
887892
if (block_height) {
888893
// Update cached block height variable since it not stored in the
889894
// serialized transaction.
890-
wtxIn.m_confirm.block_height = *block_height;
891-
} else if (wtxIn.isConflicted() || wtxIn.isConfirmed()) {
895+
wtx.m_confirm.block_height = *block_height;
896+
} else if (wtx.isConflicted() || wtx.isConfirmed()) {
892897
// If tx block (or conflicting block) was reorged out of chain
893898
// while the wallet was shutdown, change tx status to UNCONFIRMED
894899
// and reset block height, hash, and index. ABANDONED tx don't have
895900
// associated blocks and don't need to be updated. The case where a
896901
// transaction was reorged out while online and then reconfirmed
897902
// while offline is covered by the rescan logic.
898-
wtxIn.setUnconfirmed();
899-
wtxIn.m_confirm.hashBlock = uint256();
900-
wtxIn.m_confirm.block_height = 0;
901-
wtxIn.m_confirm.nIndex = 0;
903+
wtx.setUnconfirmed();
904+
wtx.m_confirm.hashBlock = uint256();
905+
wtx.m_confirm.block_height = 0;
906+
wtx.m_confirm.nIndex = 0;
902907
}
903908
}
904-
uint256 hash = wtxIn.GetHash();
905-
const auto& ins = mapWallet.emplace(hash, wtxIn);
906-
CWalletTx& wtx = ins.first->second;
907-
wtx.BindWallet(this);
908909
if (/* insertion took place */ ins.second) {
909910
wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));
910911
}
@@ -918,6 +919,7 @@ void CWallet::LoadToWallet(CWalletTx& wtxIn)
918919
}
919920
}
920921
}
922+
return true;
921923
}
922924

923925
bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, CWalletTx::Confirmation confirm, bool fUpdate)

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
896896
using UpdateWalletTxFn = std::function<bool(CWalletTx& wtx, bool new_tx)>;
897897

898898
CWalletTx* AddToWallet(CTransactionRef tx, const CWalletTx::Confirmation& confirm, const UpdateWalletTxFn& update_wtx=nullptr, bool fFlushOnClose=true);
899-
void LoadToWallet(CWalletTx& wtxIn) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
899+
bool LoadToWallet(const uint256& hash, const UpdateWalletTxFn& fill_wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
900900
void transactionAddedToMempool(const CTransactionRef& tx) override;
901901
void blockConnected(const CBlock& block, int height) override;
902902
void blockDisconnected(const CBlock& block, int height) override;

src/wallet/walletdb.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -272,36 +272,43 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
272272
} else if (strType == DBKeys::TX) {
273273
uint256 hash;
274274
ssKey >> hash;
275-
CWalletTx wtx(nullptr /* pwallet */, MakeTransactionRef());
276-
ssValue >> wtx;
277-
if (wtx.GetHash() != hash)
278-
return false;
275+
// LoadToWallet call below creates a new CWalletTx that fill_wtx
276+
// callback fills with transaction metadata.
277+
auto fill_wtx = [&](CWalletTx& wtx, bool new_tx) {
278+
assert(new_tx);
279+
ssValue >> wtx;
280+
if (wtx.GetHash() != hash)
281+
return false;
279282

280-
// Undo serialize changes in 31600
281-
if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
282-
{
283-
if (!ssValue.empty())
283+
// Undo serialize changes in 31600
284+
if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
284285
{
285-
char fTmp;
286-
char fUnused;
287-
std::string unused_string;
288-
ssValue >> fTmp >> fUnused >> unused_string;
289-
strErr = strprintf("LoadWallet() upgrading tx ver=%d %d %s",
290-
wtx.fTimeReceivedIsTxTime, fTmp, hash.ToString());
291-
wtx.fTimeReceivedIsTxTime = fTmp;
286+
if (!ssValue.empty())
287+
{
288+
char fTmp;
289+
char fUnused;
290+
std::string unused_string;
291+
ssValue >> fTmp >> fUnused >> unused_string;
292+
strErr = strprintf("LoadWallet() upgrading tx ver=%d %d %s",
293+
wtx.fTimeReceivedIsTxTime, fTmp, hash.ToString());
294+
wtx.fTimeReceivedIsTxTime = fTmp;
295+
}
296+
else
297+
{
298+
strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString());
299+
wtx.fTimeReceivedIsTxTime = 0;
300+
}
301+
wss.vWalletUpgrade.push_back(hash);
292302
}
293-
else
294-
{
295-
strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString());
296-
wtx.fTimeReceivedIsTxTime = 0;
297-
}
298-
wss.vWalletUpgrade.push_back(hash);
299-
}
300303

301-
if (wtx.nOrderPos == -1)
302-
wss.fAnyUnordered = true;
304+
if (wtx.nOrderPos == -1)
305+
wss.fAnyUnordered = true;
303306

304-
pwallet->LoadToWallet(wtx);
307+
return true;
308+
};
309+
if (!pwallet->LoadToWallet(hash, fill_wtx)) {
310+
return false;
311+
}
305312
} else if (strType == DBKeys::WATCHS) {
306313
wss.nWatchKeys++;
307314
CScript script;

0 commit comments

Comments
 (0)