Skip to content

Commit 1329ef1

Browse files
committed
Merge #13651: [moveonly] Extract CWallet::MarkInputsDirty, and privatize AddToWalletIfInvolvingMe
17e6aa8 Privatize CWallet::AddToWalletIfInvolvingMe (Ben Woosley) b7f5650 Extract CWallet::MarkInputsDirty (Ben Woosley) Pull request description: Thus reducing code and surface area of CWallet. Tree-SHA512: 31a99acc77ef3438ef9b95d60030972b707bd69d6e7b1498a5f776b219d9aabc83464f75bfec7bad5cb635d0b2d686c389914e5cc57a4bb0b93c47bd82ca608c
2 parents 9a1ad2c + 17e6aa8 commit 1329ef1

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

src/wallet/wallet.cpp

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,19 +1028,6 @@ bool CWallet::LoadToWallet(const CWalletTx& wtxIn)
10281028
return true;
10291029
}
10301030

1031-
/**
1032-
* Add a transaction to the wallet, or update it. pIndex and posInBlock should
1033-
* be set when the transaction was known to be included in a block. When
1034-
* pIndex == nullptr, then wallet state is not updated in AddToWallet, but
1035-
* notifications happen and cached balances are marked dirty.
1036-
*
1037-
* If fUpdate is true, existing transactions will be updated.
1038-
* TODO: One exception to this is that the abandoned state is cleared under the
1039-
* assumption that any further notification of a transaction that was considered
1040-
* abandoned is an indication that it is not safe to be considered abandoned.
1041-
* Abandoned state should probably be more carefully tracked via different
1042-
* posInBlock signals or by checking mempool presence when necessary.
1043-
*/
10441031
bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, const CBlockIndex* pIndex, int posInBlock, bool fUpdate)
10451032
{
10461033
const CTransaction& tx = *ptx;
@@ -1107,6 +1094,16 @@ bool CWallet::TransactionCanBeAbandoned(const uint256& hashTx) const
11071094
return wtx && !wtx->isAbandoned() && wtx->GetDepthInMainChain() == 0 && !wtx->InMempool();
11081095
}
11091096

1097+
void CWallet::MarkInputsDirty(const CTransactionRef& tx)
1098+
{
1099+
for (const CTxIn& txin : tx->vin) {
1100+
auto it = mapWallet.find(txin.prevout.hash);
1101+
if (it != mapWallet.end()) {
1102+
it->second.MarkDirty();
1103+
}
1104+
}
1105+
}
1106+
11101107
bool CWallet::AbandonTransaction(const uint256& hashTx)
11111108
{
11121109
LOCK2(cs_main, cs_wallet);
@@ -1155,13 +1152,7 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)
11551152
}
11561153
// If a transaction changes 'conflicted' state, that changes the balance
11571154
// available of the outputs it spends. So force those to be recomputed
1158-
for (const CTxIn& txin : wtx.tx->vin)
1159-
{
1160-
auto it = mapWallet.find(txin.prevout.hash);
1161-
if (it != mapWallet.end()) {
1162-
it->second.MarkDirty();
1163-
}
1164-
}
1155+
MarkInputsDirty(wtx.tx);
11651156
}
11661157
}
11671158

@@ -1217,31 +1208,19 @@ void CWallet::MarkConflicted(const uint256& hashBlock, const uint256& hashTx)
12171208
}
12181209
// If a transaction changes 'conflicted' state, that changes the balance
12191210
// available of the outputs it spends. So force those to be recomputed
1220-
for (const CTxIn& txin : wtx.tx->vin) {
1221-
auto it = mapWallet.find(txin.prevout.hash);
1222-
if (it != mapWallet.end()) {
1223-
it->second.MarkDirty();
1224-
}
1225-
}
1211+
MarkInputsDirty(wtx.tx);
12261212
}
12271213
}
12281214
}
12291215

12301216
void CWallet::SyncTransaction(const CTransactionRef& ptx, const CBlockIndex *pindex, int posInBlock, bool update_tx) {
1231-
const CTransaction& tx = *ptx;
1232-
12331217
if (!AddToWalletIfInvolvingMe(ptx, pindex, posInBlock, update_tx))
12341218
return; // Not one of ours
12351219

12361220
// If a transaction changes 'conflicted' state, that changes the balance
12371221
// available of the outputs it spends. So force those to be
12381222
// recomputed, also:
1239-
for (const CTxIn& txin : tx.vin) {
1240-
auto it = mapWallet.find(txin.prevout.hash);
1241-
if (it != mapWallet.end()) {
1242-
it->second.MarkDirty();
1243-
}
1244-
}
1223+
MarkInputsDirty(ptx);
12451224
}
12461225

12471226
void CWallet::TransactionAddedToMempool(const CTransactionRef& ptx) {

src/wallet/wallet.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,27 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
701701
void AddToSpends(const COutPoint& outpoint, const uint256& wtxid);
702702
void AddToSpends(const uint256& wtxid);
703703

704+
/**
705+
* Add a transaction to the wallet, or update it. pIndex and posInBlock should
706+
* be set when the transaction was known to be included in a block. When
707+
* pIndex == nullptr, then wallet state is not updated in AddToWallet, but
708+
* notifications happen and cached balances are marked dirty.
709+
*
710+
* If fUpdate is true, existing transactions will be updated.
711+
* TODO: One exception to this is that the abandoned state is cleared under the
712+
* assumption that any further notification of a transaction that was considered
713+
* abandoned is an indication that it is not safe to be considered abandoned.
714+
* Abandoned state should probably be more carefully tracked via different
715+
* posInBlock signals or by checking mempool presence when necessary.
716+
*/
717+
bool AddToWalletIfInvolvingMe(const CTransactionRef& tx, const CBlockIndex* pIndex, int posInBlock, bool fUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
718+
704719
/* Mark a transaction (and its in-wallet descendants) as conflicting with a particular block. */
705720
void MarkConflicted(const uint256& hashBlock, const uint256& hashTx);
706721

722+
/* Mark a transaction's inputs dirty, thus forcing the outputs to be recomputed */
723+
void MarkInputsDirty(const CTransactionRef& tx);
724+
707725
void SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator>);
708726

709727
/* Used by TransactionAddedToMemorypool/BlockConnected/Disconnected/ScanForWalletTransactions.
@@ -932,7 +950,6 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
932950
void TransactionAddedToMempool(const CTransactionRef& tx) override;
933951
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex *pindex, const std::vector<CTransactionRef>& vtxConflicted) override;
934952
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock) override;
935-
bool AddToWalletIfInvolvingMe(const CTransactionRef& tx, const CBlockIndex* pIndex, int posInBlock, bool fUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
936953
int64_t RescanFromTime(int64_t startTime, const WalletRescanReserver& reserver, bool update);
937954
CBlockIndex* ScanForWalletTransactions(CBlockIndex* pindexStart, CBlockIndex* pindexStop, const WalletRescanReserver& reserver, bool fUpdate = false);
938955
void TransactionRemovedFromMempool(const CTransactionRef &ptx) override;

0 commit comments

Comments
 (0)