From 1020254b6ad87c586e8c54534b3345f6cc072306 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Sat, 4 Jul 2020 23:33:23 +0800 Subject: [PATCH] Pass nHeight instead of pindex to AddToWalletIfInvolvingMe() Co-authored by Jack Grigg (jack@electriccoin.co) and Sean Bowe (ewillbefull@gmail.com) --- src/amqp/amqpnotificationinterface.cpp | 2 +- src/amqp/amqpnotificationinterface.h | 2 +- src/validationinterface.cpp | 16 ++++++++-------- src/validationinterface.h | 4 ++-- src/wallet/wallet.cpp | 18 +++++------------- src/wallet/wallet.h | 4 ++-- src/zmq/zmqnotificationinterface.cpp | 2 +- src/zmq/zmqnotificationinterface.h | 2 +- 8 files changed, 21 insertions(+), 29 deletions(-) diff --git a/src/amqp/amqpnotificationinterface.cpp b/src/amqp/amqpnotificationinterface.cpp index 0dfae00b9..27f98f690 100644 --- a/src/amqp/amqpnotificationinterface.cpp +++ b/src/amqp/amqpnotificationinterface.cpp @@ -122,7 +122,7 @@ void AMQPNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindex) } } -void AMQPNotificationInterface::SyncTransaction(const CTransaction &tx, const CBlock *pblock) +void AMQPNotificationInterface::SyncTransaction(const CTransaction &tx, const CBlock *pblock, const int nHeight) { for (std::list::iterator i = notifiers.begin(); i != notifiers.end(); ) { AMQPAbstractNotifier *notifier = *i; diff --git a/src/amqp/amqpnotificationinterface.h b/src/amqp/amqpnotificationinterface.h index 77495a85c..ef0ac7f34 100644 --- a/src/amqp/amqpnotificationinterface.h +++ b/src/amqp/amqpnotificationinterface.h @@ -24,7 +24,7 @@ protected: void Shutdown(); // CValidationInterface - void SyncTransaction(const CTransaction &tx, const CBlock *pblock); + void SyncTransaction(const CTransaction &tx, const CBlock *pblock, const int nHeight); void UpdatedBlockTip(const CBlockIndex *pindex); private: diff --git a/src/validationinterface.cpp b/src/validationinterface.cpp index 49773aee8..db9f8e47e 100644 --- a/src/validationinterface.cpp +++ b/src/validationinterface.cpp @@ -25,7 +25,7 @@ CMainSignals& GetMainSignals() void RegisterValidationInterface(CValidationInterface* pwalletIn) { g_signals.UpdatedBlockTip.connect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1)); - g_signals.SyncTransaction.connect(boost::bind(&CValidationInterface::SyncTransaction, pwalletIn, _1, _2)); + g_signals.SyncTransaction.connect(boost::bind(&CValidationInterface::SyncTransaction, pwalletIn, _1, _2, _3)); g_signals.EraseTransaction.connect(boost::bind(&CValidationInterface::EraseFromWallet, pwalletIn, _1)); g_signals.UpdatedTransaction.connect(boost::bind(&CValidationInterface::UpdatedTransaction, pwalletIn, _1)); g_signals.ChainTip.connect(boost::bind(&CValidationInterface::ChainTip, pwalletIn, _1, _2, _3)); @@ -47,7 +47,7 @@ void UnregisterValidationInterface(CValidationInterface* pwalletIn) { g_signals.SetBestChain.disconnect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1)); g_signals.UpdatedTransaction.disconnect(boost::bind(&CValidationInterface::UpdatedTransaction, pwalletIn, _1)); g_signals.EraseTransaction.disconnect(boost::bind(&CValidationInterface::EraseFromWallet, pwalletIn, _1)); - g_signals.SyncTransaction.disconnect(boost::bind(&CValidationInterface::SyncTransaction, pwalletIn, _1, _2)); + g_signals.SyncTransaction.disconnect(boost::bind(&CValidationInterface::SyncTransaction, pwalletIn, _1, _2, _3)); g_signals.UpdatedBlockTip.disconnect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1)); } @@ -65,8 +65,8 @@ void UnregisterAllValidationInterfaces() { g_signals.UpdatedBlockTip.disconnect_all_slots(); } -void SyncWithWallets(const CTransaction &tx, const CBlock *pblock) { - g_signals.SyncTransaction(tx, pblock); +void SyncWithWallets(const CTransaction &tx, const CBlock *pblock, const int nHeight) { + g_signals.SyncTransaction(tx, pblock, nHeight); } struct CachedBlockData { @@ -187,7 +187,7 @@ void ThreadNotifyWallets(CBlockIndex *pindexLastTip) // Let wallets know transactions went from 1-confirmed to // 0-confirmed or conflicted: for (const CTransaction &tx : block.vtx) { - SyncWithWallets(tx, NULL); + SyncWithWallets(tx, NULL, pindexLastTip->nHeight); } // Update cached incremental witnesses GetMainSignals().ChainTip(pindexLastTip, &block, boost::none); @@ -214,11 +214,11 @@ void ThreadNotifyWallets(CBlockIndex *pindexLastTip) // Tell wallet about transactions that went from mempool // to conflicted: for (const CTransaction &tx : blockData.txConflicted) { - SyncWithWallets(tx, NULL); + SyncWithWallets(tx, NULL, blockData.pindex->nHeight + 1); } // ... and about transactions that got confirmed: for (const CTransaction &tx : block.vtx) { - SyncWithWallets(tx, &block); + SyncWithWallets(tx, &block, blockData.pindex->nHeight); } // Update cached incremental witnesses GetMainSignals().ChainTip(blockData.pindex, &block, blockData.oldTrees); @@ -230,7 +230,7 @@ void ThreadNotifyWallets(CBlockIndex *pindexLastTip) // Notify transactions in the mempool for (auto tx : recentlyAdded.first) { try { - SyncWithWallets(tx, NULL); + SyncWithWallets(tx, NULL, pindexLastTip->nHeight + 1); } catch (const boost::thread_interrupted&) { throw; } catch (const std::exception& e) { diff --git a/src/validationinterface.h b/src/validationinterface.h index 8dfdd6eb4..f1694a313 100644 --- a/src/validationinterface.h +++ b/src/validationinterface.h @@ -33,7 +33,7 @@ void UnregisterAllValidationInterfaces(); class CValidationInterface { protected: virtual void UpdatedBlockTip(const CBlockIndex *pindex) {} - virtual void SyncTransaction(const CTransaction &tx, const CBlock *pblock) {} + virtual void SyncTransaction(const CTransaction &tx, const CBlock *pblock, const int nHeight) {} virtual void EraseFromWallet(const uint256 &hash) {} virtual void ChainTip(const CBlockIndex *pindex, const CBlock *pblock, boost::optional> added) {} virtual void SetBestChain(const CBlockLocator &locator) {} @@ -52,7 +52,7 @@ struct CMainSignals { /** Notifies listeners of updated block chain tip */ boost::signals2::signal UpdatedBlockTip; /** Notifies listeners of updated transaction data (transaction, and optionally the block it is found in. */ - boost::signals2::signal SyncTransaction; + boost::signals2::signal SyncTransaction; /** Notifies listeners of an erased transaction (currently disabled, requires transaction replacement). */ boost::signals2::signal EraseTransaction; /** Notifies listeners of an updated transaction without new data (for now: a coinbase potentially becoming visible). */ diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index e7120cdf1..6e20ae6d4 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1711,22 +1711,14 @@ bool CWallet::UpdatedNoteData(const CWalletTx& wtxIn, CWalletTx& wtx) * updated; instead, the transaction being in the mempool or conflicted is determined on * the fly in CMerkleTx::GetDepthInMainChain(). */ -bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, const CBlockIndex* pindex, bool fUpdate) +bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, const int nHeight, bool fUpdate) { { AssertLockHeld(cs_wallet); bool fExisted = mapWallet.count(tx.GetHash()) != 0; if (fExisted && !fUpdate) return false; auto sproutNoteData = FindMySproutNotes(tx); - int height; - if (pblock) { - height = pindex->nHeight; - } else { - // assume tx is in mempool - LOCK(cs_main); - height = chainActive.Height(); - } - auto saplingNoteDataAndAddressesToAdd = FindMySaplingNotes(tx, height); + auto saplingNoteDataAndAddressesToAdd = FindMySaplingNotes(tx, nHeight); auto saplingNoteData = saplingNoteDataAndAddressesToAdd.first; auto addressesToAdd = saplingNoteDataAndAddressesToAdd.second; for (const auto &addressToAdd : addressesToAdd) { @@ -1760,10 +1752,10 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pbl return false; } -void CWallet::SyncTransaction(const CTransaction& tx, const CBlock* pblock, const CBlockIndex* pindex) +void CWallet::SyncTransaction(const CTransaction& tx, const CBlock* pblock, const int nHeight) { LOCK(cs_wallet); - if (!AddToWalletIfInvolvingMe(tx, pblock, pindex, true)) + if (!AddToWalletIfInvolvingMe(tx, pblock, nHeight, true)) return; // Not one of ours MarkAffectedTransactionsDirty(tx); @@ -2744,7 +2736,7 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate) ReadBlockFromDisk(block, pindex, Params().GetConsensus()); BOOST_FOREACH(CTransaction& tx, block.vtx) { - if (AddToWalletIfInvolvingMe(tx, &block, pindex, fUpdate)) { + if (AddToWalletIfInvolvingMe(tx, &block, pindex->nHeight, fUpdate)) { myTxHashes.push_back(tx.GetHash()); ret++; } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 9c0bc2a26..c41116119 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1162,8 +1162,8 @@ public: void UpdateSaplingNullifierNoteMapWithTx(CWalletTx& wtx); void UpdateSaplingNullifierNoteMapForBlock(const CBlock* pblock); bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb); - void SyncTransaction(const CTransaction& tx, const CBlock* pblock, const CBlockIndex* pindex); - bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, const CBlockIndex* pindex, bool fUpdate); + void SyncTransaction(const CTransaction& tx, const CBlock* pblock, const int nHeight); + bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, const int nHeight, bool fUpdate); void EraseFromWallet(const uint256 &hash); void WitnessNoteCommitment( std::vector commitments, diff --git a/src/zmq/zmqnotificationinterface.cpp b/src/zmq/zmqnotificationinterface.cpp index e427e6b97..22c3b3901 100644 --- a/src/zmq/zmqnotificationinterface.cpp +++ b/src/zmq/zmqnotificationinterface.cpp @@ -163,7 +163,7 @@ void CZMQNotificationInterface::BlockChecked(const CBlock& block, const CValidat } } -void CZMQNotificationInterface::SyncTransaction(const CTransaction &tx, const CBlock *pblock) +void CZMQNotificationInterface::SyncTransaction(const CTransaction &tx, const CBlock *pblock, const int nHeight) { for (std::list::iterator i = notifiers.begin(); i!=notifiers.end(); ) { diff --git a/src/zmq/zmqnotificationinterface.h b/src/zmq/zmqnotificationinterface.h index 1dc7211d5..fedff60ef 100644 --- a/src/zmq/zmqnotificationinterface.h +++ b/src/zmq/zmqnotificationinterface.h @@ -25,7 +25,7 @@ protected: void Shutdown(); // CValidationInterface - void SyncTransaction(const CTransaction &tx, const CBlock *pblock); + void SyncTransaction(const CTransaction &tx, const CBlock *pblock, const int nHeight); void UpdatedBlockTip(const CBlockIndex *pindex); void BlockChecked(const CBlock& block, const CValidationState& state);