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.
This commit is contained in:
parent
bd2fbc7cdb
commit
65b9d8f8dd
3 changed files with 50 additions and 41 deletions
|
@ -879,32 +879,33 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
|
||||||
return &wtx;
|
return &wtx;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CWallet::LoadToWallet(CWalletTx& wtxIn)
|
bool CWallet::LoadToWallet(const uint256& hash, const UpdateWalletTxFn& fill_wtx)
|
||||||
{
|
{
|
||||||
|
const auto& ins = mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(hash), std::forward_as_tuple(this, nullptr));
|
||||||
|
CWalletTx& wtx = ins.first->second;
|
||||||
|
if (!fill_wtx(wtx, ins.second)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
// If wallet doesn't have a chain (e.g wallet-tool), don't bother to update txn.
|
// If wallet doesn't have a chain (e.g wallet-tool), don't bother to update txn.
|
||||||
if (HaveChain()) {
|
if (HaveChain()) {
|
||||||
Optional<int> block_height = chain().getBlockHeight(wtxIn.m_confirm.hashBlock);
|
Optional<int> block_height = chain().getBlockHeight(wtx.m_confirm.hashBlock);
|
||||||
if (block_height) {
|
if (block_height) {
|
||||||
// Update cached block height variable since it not stored in the
|
// Update cached block height variable since it not stored in the
|
||||||
// serialized transaction.
|
// serialized transaction.
|
||||||
wtxIn.m_confirm.block_height = *block_height;
|
wtx.m_confirm.block_height = *block_height;
|
||||||
} else if (wtxIn.isConflicted() || wtxIn.isConfirmed()) {
|
} else if (wtx.isConflicted() || wtx.isConfirmed()) {
|
||||||
// If tx block (or conflicting block) was reorged out of chain
|
// If tx block (or conflicting block) was reorged out of chain
|
||||||
// while the wallet was shutdown, change tx status to UNCONFIRMED
|
// while the wallet was shutdown, change tx status to UNCONFIRMED
|
||||||
// and reset block height, hash, and index. ABANDONED tx don't have
|
// and reset block height, hash, and index. ABANDONED tx don't have
|
||||||
// associated blocks and don't need to be updated. The case where a
|
// associated blocks and don't need to be updated. The case where a
|
||||||
// transaction was reorged out while online and then reconfirmed
|
// transaction was reorged out while online and then reconfirmed
|
||||||
// while offline is covered by the rescan logic.
|
// while offline is covered by the rescan logic.
|
||||||
wtxIn.setUnconfirmed();
|
wtx.setUnconfirmed();
|
||||||
wtxIn.m_confirm.hashBlock = uint256();
|
wtx.m_confirm.hashBlock = uint256();
|
||||||
wtxIn.m_confirm.block_height = 0;
|
wtx.m_confirm.block_height = 0;
|
||||||
wtxIn.m_confirm.nIndex = 0;
|
wtx.m_confirm.nIndex = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
uint256 hash = wtxIn.GetHash();
|
|
||||||
const auto& ins = mapWallet.emplace(hash, wtxIn);
|
|
||||||
CWalletTx& wtx = ins.first->second;
|
|
||||||
wtx.BindWallet(this);
|
|
||||||
if (/* insertion took place */ ins.second) {
|
if (/* insertion took place */ ins.second) {
|
||||||
wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));
|
wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));
|
||||||
}
|
}
|
||||||
|
@ -918,6 +919,7 @@ void CWallet::LoadToWallet(CWalletTx& wtxIn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, CWalletTx::Confirmation confirm, bool fUpdate)
|
bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, CWalletTx::Confirmation confirm, bool fUpdate)
|
||||||
|
|
|
@ -896,7 +896,7 @@ public:
|
||||||
using UpdateWalletTxFn = std::function<bool(CWalletTx& wtx, bool new_tx)>;
|
using UpdateWalletTxFn = std::function<bool(CWalletTx& wtx, bool new_tx)>;
|
||||||
|
|
||||||
CWalletTx* AddToWallet(CTransactionRef tx, const CWalletTx::Confirmation& confirm, const UpdateWalletTxFn& update_wtx=nullptr, bool fFlushOnClose=true);
|
CWalletTx* AddToWallet(CTransactionRef tx, const CWalletTx::Confirmation& confirm, const UpdateWalletTxFn& update_wtx=nullptr, bool fFlushOnClose=true);
|
||||||
void LoadToWallet(CWalletTx& wtxIn) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
bool LoadToWallet(const uint256& hash, const UpdateWalletTxFn& fill_wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||||
void transactionAddedToMempool(const CTransactionRef& tx) override;
|
void transactionAddedToMempool(const CTransactionRef& tx) override;
|
||||||
void blockConnected(const CBlock& block, int height) override;
|
void blockConnected(const CBlock& block, int height) override;
|
||||||
void blockDisconnected(const CBlock& block, int height) override;
|
void blockDisconnected(const CBlock& block, int height) override;
|
||||||
|
|
|
@ -272,7 +272,10 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
|
||||||
} else if (strType == DBKeys::TX) {
|
} else if (strType == DBKeys::TX) {
|
||||||
uint256 hash;
|
uint256 hash;
|
||||||
ssKey >> hash;
|
ssKey >> hash;
|
||||||
CWalletTx wtx(nullptr /* pwallet */, MakeTransactionRef());
|
// LoadToWallet call below creates a new CWalletTx that fill_wtx
|
||||||
|
// callback fills with transaction metadata.
|
||||||
|
auto fill_wtx = [&](CWalletTx& wtx, bool new_tx) {
|
||||||
|
assert(new_tx);
|
||||||
ssValue >> wtx;
|
ssValue >> wtx;
|
||||||
if (wtx.GetHash() != hash)
|
if (wtx.GetHash() != hash)
|
||||||
return false;
|
return false;
|
||||||
|
@ -301,7 +304,11 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
|
||||||
if (wtx.nOrderPos == -1)
|
if (wtx.nOrderPos == -1)
|
||||||
wss.fAnyUnordered = true;
|
wss.fAnyUnordered = true;
|
||||||
|
|
||||||
pwallet->LoadToWallet(wtx);
|
return true;
|
||||||
|
};
|
||||||
|
if (!pwallet->LoadToWallet(hash, fill_wtx)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} else if (strType == DBKeys::WATCHS) {
|
} else if (strType == DBKeys::WATCHS) {
|
||||||
wss.nWatchKeys++;
|
wss.nWatchKeys++;
|
||||||
CScript script;
|
CScript script;
|
||||||
|
|
Loading…
Reference in a new issue