From a71a7341b9dd3603c95e5b8c0a86c357cf85f78b Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 21 Nov 2019 17:00:22 +0000 Subject: [PATCH] Merge tree and boolean fields in ChainTip API The trees were being unnecessarily fetched during DisconnectTip. --- src/main.cpp | 9 ++------- src/validationinterface.cpp | 4 ++-- src/validationinterface.h | 4 ++-- src/wallet/wallet.cpp | 6 ++---- src/wallet/wallet.h | 5 ++++- src/zcbenchmarks.cpp | 8 ++++---- 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 74b5cdcb1..3adf9b082 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3014,18 +3014,13 @@ bool static DisconnectTip(CValidationState &state, const CChainParams& chainpara // Update chainActive and related variables. UpdateTip(pindexDelete->pprev, chainparams); - // Get the current commitment tree - SproutMerkleTree newSproutTree; - SaplingMerkleTree newSaplingTree; - assert(pcoinsTip->GetSproutAnchorAt(pcoinsTip->GetBestAnchor(SPROUT), newSproutTree)); - assert(pcoinsTip->GetSaplingAnchorAt(pcoinsTip->GetBestAnchor(SAPLING), newSaplingTree)); // Let wallets know transactions went from 1-confirmed to // 0-confirmed or conflicted: BOOST_FOREACH(const CTransaction &tx, block.vtx) { SyncWithWallets(tx, NULL); } // Update cached incremental witnesses - GetMainSignals().ChainTip(pindexDelete, &block, newSproutTree, newSaplingTree, false); + GetMainSignals().ChainTip(pindexDelete, &block, boost::none); return true; } @@ -3100,7 +3095,7 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, SyncWithWallets(tx, pblock); } // Update cached incremental witnesses - GetMainSignals().ChainTip(pindexNew, pblock, oldSproutTree, oldSaplingTree, true); + GetMainSignals().ChainTip(pindexNew, pblock, std::make_pair(oldSproutTree, oldSaplingTree)); EnforceNodeDeprecation(pindexNew->nHeight); diff --git a/src/validationinterface.cpp b/src/validationinterface.cpp index f6b691cd6..07da61122 100644 --- a/src/validationinterface.cpp +++ b/src/validationinterface.cpp @@ -25,7 +25,7 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn) { g_signals.SyncTransaction.connect(boost::bind(&CValidationInterface::SyncTransaction, pwalletIn, _1, _2)); 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, _4, _5)); + g_signals.ChainTip.connect(boost::bind(&CValidationInterface::ChainTip, pwalletIn, _1, _2, _3)); g_signals.SetBestChain.connect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1)); g_signals.Inventory.connect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1)); g_signals.Broadcast.connect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1)); @@ -40,7 +40,7 @@ void UnregisterValidationInterface(CValidationInterface* pwalletIn) { g_signals.BlockChecked.disconnect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2)); g_signals.Broadcast.disconnect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1)); g_signals.Inventory.disconnect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1)); - g_signals.ChainTip.disconnect(boost::bind(&CValidationInterface::ChainTip, pwalletIn, _1, _2, _3, _4, _5)); + g_signals.ChainTip.disconnect(boost::bind(&CValidationInterface::ChainTip, pwalletIn, _1, _2, _3)); 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)); diff --git a/src/validationinterface.h b/src/validationinterface.h index 96307c373..1fbfe3837 100644 --- a/src/validationinterface.h +++ b/src/validationinterface.h @@ -36,7 +36,7 @@ protected: virtual void UpdatedBlockTip(const CBlockIndex *pindex) {} virtual void SyncTransaction(const CTransaction &tx, const CBlock *pblock) {} virtual void EraseFromWallet(const uint256 &hash) {} - virtual void ChainTip(const CBlockIndex *pindex, const CBlock *pblock, SproutMerkleTree sproutTree, SaplingMerkleTree saplingTree, bool added) {} + virtual void ChainTip(const CBlockIndex *pindex, const CBlock *pblock, boost::optional> added) {} virtual void SetBestChain(const CBlockLocator &locator) {} virtual void UpdatedTransaction(const uint256 &hash) {} virtual void Inventory(const uint256 &hash) {} @@ -59,7 +59,7 @@ struct CMainSignals { /** Notifies listeners of an updated transaction without new data (for now: a coinbase potentially becoming visible). */ boost::signals2::signal UpdatedTransaction; /** Notifies listeners of a change to the tip of the active block chain. */ - boost::signals2::signal ChainTip; + boost::signals2::signal>)> ChainTip; /** Notifies listeners of a new active block chain. */ boost::signals2::signal SetBestChain; /** Notifies listeners about an inventory item being seen on the network. */ diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 4d8a0e1f9..f09894db4 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -574,12 +574,10 @@ void CWallet::ChainTipAdded(const CBlockIndex *pindex, void CWallet::ChainTip(const CBlockIndex *pindex, const CBlock *pblock, - SproutMerkleTree sproutTree, - SaplingMerkleTree saplingTree, - bool added) + boost::optional> added) { if (added) { - ChainTipAdded(pindex, pblock, sproutTree, saplingTree); + ChainTipAdded(pindex, pblock, added->first, added->second); // Prevent migration transactions from being created when node is syncing after launch, // and also when node wakes up from suspension/hibernation and incoming blocks are old. if (!IsInitialBlockDownload(Params()) && diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index bfacdc494..78ac5632c 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1195,7 +1195,10 @@ public: CAmount GetDebit(const CTransaction& tx, const isminefilter& filter) const; CAmount GetCredit(const CTransaction& tx, const isminefilter& filter) const; CAmount GetChange(const CTransaction& tx) const; - void ChainTip(const CBlockIndex *pindex, const CBlock *pblock, SproutMerkleTree sproutTree, SaplingMerkleTree saplingTree, bool added); + void ChainTip( + const CBlockIndex *pindex, + const CBlock *pblock, + boost::optional> added); void RunSaplingMigration(int blockHeight); void AddPendingSaplingMigrationTx(const CTransaction& tx); /** Saves witness caches and best block locator to disk. */ diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index e064dd586..391bf5847 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -350,7 +350,7 @@ double benchmark_increment_sprout_note_witnesses(size_t nTxs) index1.nHeight = 1; // Increment to get transactions witnessed - wallet.ChainTip(&index1, &block1, sproutTree, saplingTree, true); + wallet.ChainTip(&index1, &block1, std::make_pair(sproutTree, saplingTree)); // Second block CBlock block2; @@ -366,7 +366,7 @@ double benchmark_increment_sprout_note_witnesses(size_t nTxs) struct timeval tv_start; timer_start(tv_start); - wallet.ChainTip(&index2, &block2, sproutTree, saplingTree, true); + wallet.ChainTip(&index2, &block2, std::make_pair(sproutTree, saplingTree)); return timer_stop(tv_start); } @@ -412,7 +412,7 @@ double benchmark_increment_sapling_note_witnesses(size_t nTxs) index1.nHeight = 1; // Increment to get transactions witnessed - wallet.ChainTip(&index1, &block1, sproutTree, saplingTree, true); + wallet.ChainTip(&index1, &block1, std::make_pair(sproutTree, saplingTree)); // Second block CBlock block2; @@ -428,7 +428,7 @@ double benchmark_increment_sapling_note_witnesses(size_t nTxs) struct timeval tv_start; timer_start(tv_start); - wallet.ChainTip(&index2, &block2, sproutTree, saplingTree, true); + wallet.ChainTip(&index2, &block2, std::make_pair(sproutTree, saplingTree)); return timer_stop(tv_start); }