Skip to content

Commit 9700fcb

Browse files
author
Antoine Riard
committed
Replace CWalletTx::SetConf by Confirmation initialization list
1 parent 5aacc3e commit 9700fcb

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

src/wallet/rpcdump.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ UniValue importprunedfunds(const JSONRPCRequest& request)
382382
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
383383
}
384384

385-
wtx.SetConf(CWalletTx::Status::CONFIRMED, merkleBlock.header.GetHash(), txnIndex);
385+
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, merkleBlock.header.GetHash(), txnIndex);
386+
wtx.m_confirm = confirm;
386387

387388
auto locked_chain = pwallet->chain().lock();
388389
LOCK(pwallet->cs_wallet);

src/wallet/test/wallet_tests.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
276276
AssertLockHeld(spk_man->cs_wallet);
277277
wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
278278

279-
wtx.SetConf(CWalletTx::Status::CONFIRMED, ::ChainActive().Tip()->GetBlockHash(), 0);
279+
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, ::ChainActive().Tip()->GetBlockHash(), 0);
280+
wtx.m_confirm = confirm;
280281

281282
// Call GetImmatureCredit() once before adding the key to the wallet to
282283
// cache the current immature credit amount, which is 0.
@@ -317,7 +318,8 @@ static int64_t AddTx(CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64
317318
wallet.AddToWallet(wtx);
318319
}
319320
if (block) {
320-
wtx.SetConf(CWalletTx::Status::CONFIRMED, block->GetBlockHash(), 0);
321+
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, block->GetBlockHash(), 0);
322+
wtx.m_confirm = confirm;
321323
}
322324
wallet.AddToWallet(wtx);
323325
return wallet.mapWallet.at(wtx.GetHash()).nTimeSmart;
@@ -497,7 +499,8 @@ class ListCoinsTestingSetup : public TestChain100Setup
497499
wallet->SetLastBlockProcessed(wallet->GetLastBlockHeight() + 1, ::ChainActive().Tip()->GetBlockHash());
498500
auto it = wallet->mapWallet.find(tx->GetHash());
499501
BOOST_CHECK(it != wallet->mapWallet.end());
500-
it->second.SetConf(CWalletTx::Status::CONFIRMED, ::ChainActive().Tip()->GetBlockHash(), 1);
502+
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, ::ChainActive().Tip()->GetBlockHash(), 1);
503+
it->second.m_confirm = confirm;
501504
return it->second;
502505
}
503506

src/wallet/wallet.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -848,19 +848,19 @@ void CWallet::LoadToWallet(CWalletTx& wtxIn)
848848
}
849849
}
850850

851-
bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, CWalletTx::Status status, const uint256& block_hash, int posInBlock, bool fUpdate)
851+
bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, CWalletTx::Confirmation confirm, bool fUpdate)
852852
{
853853
const CTransaction& tx = *ptx;
854854
{
855855
AssertLockHeld(cs_wallet);
856856

857-
if (!block_hash.IsNull()) {
857+
if (!confirm.hashBlock.IsNull()) {
858858
for (const CTxIn& txin : tx.vin) {
859859
std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range = mapTxSpends.equal_range(txin.prevout);
860860
while (range.first != range.second) {
861861
if (range.first->second != tx.GetHash()) {
862-
WalletLogPrintf("Transaction %s (in block %s) conflicts with wallet transaction %s (both spend %s:%i)\n", tx.GetHash().ToString(), block_hash.ToString(), range.first->second.ToString(), range.first->first.hash.ToString(), range.first->first.n);
863-
MarkConflicted(block_hash, range.first->second);
862+
WalletLogPrintf("Transaction %s (in block %s) conflicts with wallet transaction %s (both spend %s:%i)\n", tx.GetHash().ToString(), confirm.hashBlock.ToString(), range.first->second.ToString(), range.first->first.hash.ToString(), range.first->first.n);
863+
MarkConflicted(confirm.hashBlock, range.first->second);
864864
}
865865
range.first++;
866866
}
@@ -888,7 +888,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, CWalletTx::St
888888

889889
// Block disconnection override an abandoned tx as unconfirmed
890890
// which means user may have to call abandontransaction again
891-
wtx.SetConf(status, block_hash, posInBlock);
891+
wtx.m_confirm = confirm;
892892

893893
return AddToWallet(wtx, false);
894894
}
@@ -1022,9 +1022,9 @@ void CWallet::MarkConflicted(const uint256& hashBlock, const uint256& hashTx)
10221022
}
10231023
}
10241024

1025-
void CWallet::SyncTransaction(const CTransactionRef& ptx, CWalletTx::Status status, const uint256& block_hash, int posInBlock, bool update_tx)
1025+
void CWallet::SyncTransaction(const CTransactionRef& ptx, CWalletTx::Confirmation confirm, bool update_tx)
10261026
{
1027-
if (!AddToWalletIfInvolvingMe(ptx, status, block_hash, posInBlock, update_tx))
1027+
if (!AddToWalletIfInvolvingMe(ptx, confirm, update_tx))
10281028
return; // Not one of ours
10291029

10301030
// If a transaction changes 'conflicted' state, that changes the balance
@@ -1036,7 +1036,8 @@ void CWallet::SyncTransaction(const CTransactionRef& ptx, CWalletTx::Status stat
10361036
void CWallet::TransactionAddedToMempool(const CTransactionRef& ptx) {
10371037
auto locked_chain = chain().lock();
10381038
LOCK(cs_wallet);
1039-
SyncTransaction(ptx, CWalletTx::Status::UNCONFIRMED, {} /* block hash */, 0 /* position in block */);
1039+
CWalletTx::Confirmation confirm(CWalletTx::Status::UNCONFIRMED, {}, 0);
1040+
SyncTransaction(ptx, confirm);
10401041

10411042
auto it = mapWallet.find(ptx->GetHash());
10421043
if (it != mapWallet.end()) {
@@ -1061,7 +1062,8 @@ void CWallet::BlockConnected(const CBlock& block, const std::vector<CTransaction
10611062
m_last_block_processed_height = height;
10621063
m_last_block_processed = block_hash;
10631064
for (size_t i = 0; i < block.vtx.size(); i++) {
1064-
SyncTransaction(block.vtx[i], CWalletTx::Status::CONFIRMED, block_hash, i);
1065+
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, m_last_block_processed, i);
1066+
SyncTransaction(block.vtx[i], confirm);
10651067
TransactionRemovedFromMempool(block.vtx[i]);
10661068
}
10671069
for (const CTransactionRef& ptx : vtxConflicted) {
@@ -1081,7 +1083,8 @@ void CWallet::BlockDisconnected(const CBlock& block, int height)
10811083
m_last_block_processed_height = height - 1;
10821084
m_last_block_processed = block.hashPrevBlock;
10831085
for (const CTransactionRef& ptx : block.vtx) {
1084-
SyncTransaction(ptx, CWalletTx::Status::UNCONFIRMED, {} /* block hash */, 0 /* position in block */);
1086+
CWalletTx::Confirmation confirm(CWalletTx::Status::UNCONFIRMED, {}, 0);
1087+
SyncTransaction(ptx, confirm);
10851088
}
10861089
}
10871090

@@ -1627,7 +1630,8 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
16271630
break;
16281631
}
16291632
for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
1630-
SyncTransaction(block.vtx[posInBlock], CWalletTx::Status::CONFIRMED, block_hash, posInBlock, fUpdate);
1633+
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, block_hash, posInBlock);
1634+
SyncTransaction(block.vtx[posInBlock], confirm, fUpdate);
16311635
}
16321636
// scan succeeded, record block as most recent successfully scanned
16331637
result.last_scanned_block = block_hash;
@@ -3918,18 +3922,6 @@ CKeyPool::CKeyPool(const CPubKey& vchPubKeyIn, bool internalIn)
39183922
m_pre_split = false;
39193923
}
39203924

3921-
void CWalletTx::SetConf(Status status, const uint256& block_hash, int posInBlock)
3922-
{
3923-
// Update tx status
3924-
m_confirm.status = status;
3925-
3926-
// Update the tx's hashBlock
3927-
m_confirm.hashBlock = block_hash;
3928-
3929-
// set the position of the transaction in the block
3930-
m_confirm.nIndex = posInBlock;
3931-
}
3932-
39333925
int CWalletTx::GetDepthInMainChain(interfaces::Chain::Lock& locked_chain) const
39343926
{
39353927
if (isUnconfirmed() || isAbandoned()) return 0;

src/wallet/wallet.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,10 @@ class CWalletTx
361361
* where they instead point to block hash and index of the deepest conflicting tx.
362362
*/
363363
struct Confirmation {
364-
Status status = UNCONFIRMED;
365-
uint256 hashBlock = uint256();
366-
int nIndex = 0;
364+
Status status;
365+
uint256 hashBlock;
366+
int nIndex;
367+
Confirmation(Status s = UNCONFIRMED, uint256 h = uint256(), int i = 0) : status(s), hashBlock(h), nIndex(i) {}
367368
};
368369

369370
Confirmation m_confirm;
@@ -491,8 +492,6 @@ class CWalletTx
491492
// in place.
492493
std::set<uint256> GetConflicts() const NO_THREAD_SAFETY_ANALYSIS;
493494

494-
void SetConf(Status status, const uint256& block_hash, int posInBlock);
495-
496495
/**
497496
* Return depth of transaction in blockchain:
498497
* <0 : conflicts with a transaction this deep in the blockchain
@@ -642,7 +641,7 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
642641
* Abandoned state should probably be more carefully tracked via different
643642
* posInBlock signals or by checking mempool presence when necessary.
644643
*/
645-
bool AddToWalletIfInvolvingMe(const CTransactionRef& tx, CWalletTx::Status status, const uint256& block_hash, int posInBlock, bool fUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
644+
bool AddToWalletIfInvolvingMe(const CTransactionRef& tx, CWalletTx::Confirmation confirm, bool fUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
646645

647646
/* Mark a transaction (and its in-wallet descendants) as conflicting with a particular block. */
648647
void MarkConflicted(const uint256& hashBlock, const uint256& hashTx);
@@ -654,7 +653,7 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
654653

655654
/* Used by TransactionAddedToMemorypool/BlockConnected/Disconnected/ScanForWalletTransactions.
656655
* Should be called with non-zero block_hash and posInBlock if this is for a transaction that is included in a block. */
657-
void SyncTransaction(const CTransactionRef& tx, CWalletTx::Status status, const uint256& block_hash, int posInBlock = 0, bool update_tx = true) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
656+
void SyncTransaction(const CTransactionRef& tx, CWalletTx::Confirmation confirm, bool update_tx = true) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
658657

659658
std::atomic<uint64_t> m_wallet_flags{0};
660659

0 commit comments

Comments
 (0)