diff options
author | MarcoFalke <falke.marco@gmail.com> | 2019-05-01 14:59:55 -0400 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2019-05-01 15:02:31 -0400 |
commit | 0936f35f65ad778c1308f00b42b6182c7e64270f (patch) | |
tree | 3b7c1eb93f23232797fb8f06694d2fcca892c232 /src | |
parent | 12aa2ac988d0ccb21019a20b109e018cf31b78cf (diff) | |
parent | 422677963a7b41e340b911b4cd53d29dd8d63f21 (diff) |
Merge #15842: refactor: replace isPotentialtip/waitForNotifications by higher method
422677963a refactor: replace isPotentialtip/waitForNotifications by higher method (Antoine Riard)
edfe9438ca Add WITH_LOCK macro: run code while locking a mutex (Antoine Riard)
Pull request description:
In Chain interface, instead of a isPotentialTip and a WaitForNotifications method, both used only once in CWallet::BlockUntilSyncedToCurrentChain, combine them in a higher WaitForNotificationsUpToTip method. Semantic should be unchanged, wallet wait for pending notifications to be processed unless block hash points to the current chain tip or a descendant.
ACKs for commit 422677:
jnewbery:
ACK 422677963a7b41e340b911b4cd53d29dd8d63f21
ryanofsky:
utACK 422677963a7b41e340b911b4cd53d29dd8d63f21. Only change is adding the cs_wallet lock annotation.
Tree-SHA512: 2834ff0218795ef607543fae822e5cce25d759c1a9cfcb1f896a4af03071faed5276fbe0966e0c6ed65dc0e88af161899c5b2ca358a2d24fe70969a550000bf2
Diffstat (limited to 'src')
-rw-r--r-- | src/interfaces/chain.cpp | 17 | ||||
-rw-r--r-- | src/interfaces/chain.h | 17 | ||||
-rw-r--r-- | src/sync.h | 10 | ||||
-rw-r--r-- | src/wallet/wallet.cpp | 20 | ||||
-rw-r--r-- | src/wallet/wallet.h | 2 |
5 files changed, 29 insertions, 37 deletions
diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index 839af650bb..617be3ca71 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -122,12 +122,6 @@ class LockImpl : public Chain::Lock } return nullopt; } - bool isPotentialTip(const uint256& hash) override - { - if (::chainActive.Tip()->GetBlockHash() == hash) return true; - CBlockIndex* block = LookupBlockIndex(hash); - return block && block->GetAncestor(::chainActive.Height()) == ::chainActive.Tip(); - } CBlockLocator getTipLocator() override { return ::chainActive.GetLocator(); } Optional<int> findLocatorFork(const CBlockLocator& locator) override { @@ -343,7 +337,16 @@ public: { return MakeUnique<NotificationsHandlerImpl>(*this, notifications); } - void waitForNotifications() override { SyncWithValidationInterfaceQueue(); } + void waitForNotificationsIfNewBlocksConnected(const uint256& old_tip) override + { + if (!old_tip.IsNull()) { + LOCK(::cs_main); + if (old_tip == ::chainActive.Tip()->GetBlockHash()) return; + CBlockIndex* block = LookupBlockIndex(old_tip); + if (block && block->GetAncestor(::chainActive.Height()) == ::chainActive.Tip()) return; + } + SyncWithValidationInterfaceQueue(); + } std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override { return MakeUnique<RpcHandlerImpl>(command); diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index 7564ad26ac..0b7249a5ab 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -43,12 +43,6 @@ class Wallet; //! asynchronously //! (https://github.com/bitcoin/bitcoin/pull/10973#issuecomment-380101269). //! -//! * The isPotentialTip() and waitForNotifications() methods are too low-level -//! and should be replaced with a higher level -//! waitForNotificationsUpTo(block_hash) method that the wallet can call -//! instead -//! (https://github.com/bitcoin/bitcoin/pull/10973#discussion_r266995234). -//! //! * The relayTransactions() and submitToMemoryPool() methods could be replaced //! with a higher-level broadcastTransaction method //! (https://github.com/bitcoin/bitcoin/pull/14978#issuecomment-459373984). @@ -123,11 +117,6 @@ public: //! information is desired). virtual Optional<int> findFork(const uint256& hash, Optional<int>* height) = 0; - //! Return true if block hash points to the current chain tip, or to a - //! possible descendant of the current chain tip that isn't currently - //! connected. - virtual bool isPotentialTip(const uint256& hash) = 0; - //! Get locator for the current chain tip. virtual CBlockLocator getTipLocator() = 0; @@ -256,8 +245,10 @@ public: //! Register handler for notifications. virtual std::unique_ptr<Handler> handleNotifications(Notifications& notifications) = 0; - //! Wait for pending notifications to be handled. - virtual void waitForNotifications() = 0; + //! Wait for pending notifications to be processed unless block hash points to the current + //! chain tip, or to a possible descendant of the current chain tip that isn't currently + //! connected. + virtual void waitForNotificationsIfNewBlocksConnected(const uint256& old_tip) = 0; //! Register handler for RPC. Command is not copied, so reference //! needs to remain valid until Handler is disconnected. diff --git a/src/sync.h b/src/sync.h index 3857eda56b..2667fb52f9 100644 --- a/src/sync.h +++ b/src/sync.h @@ -198,6 +198,16 @@ using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove LeaveCritical(); \ } +//! Run code while locking a mutex. +//! +//! Examples: +//! +//! WITH_LOCK(cs, shared_val = shared_val + 1); +//! +//! int val = WITH_LOCK(cs, return shared_val); +//! +#define WITH_LOCK(cs, code) [&] { LOCK(cs); code; }() + class CSemaphore { private: diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 4ab94f0c2c..071ae23e1b 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1287,24 +1287,12 @@ void CWallet::UpdatedBlockTip() void CWallet::BlockUntilSyncedToCurrentChain() { AssertLockNotHeld(cs_wallet); - - { - // Skip the queue-draining stuff if we know we're caught up with - // chainActive.Tip()... - // We could also take cs_wallet here, and call m_last_block_processed - // protected by cs_wallet instead of cs_main, but as long as we need - // cs_main here anyway, it's easier to just call it cs_main-protected. - auto locked_chain = chain().lock(); - - if (!m_last_block_processed.IsNull() && locked_chain->isPotentialTip(m_last_block_processed)) { - return; - } - } - - // ...otherwise put a callback in the validation interface queue and wait + // Skip the queue-draining stuff if we know we're caught up with + // chainActive.Tip(), otherwise put a callback in the validation interface queue and wait // for the queue to drain enough to execute it (indicating we are caught up // at least with the time we entered this function). - chain().waitForNotifications(); + uint256 last_block_hash = WITH_LOCK(cs_wallet, return m_last_block_processed); + chain().waitForNotificationsIfNewBlocksConnected(last_block_hash); } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 900af75f4f..510f4b8c04 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -693,7 +693,7 @@ private: * to have seen all transactions in the chain, but is only used to track * live BlockConnected callbacks. */ - uint256 m_last_block_processed; + uint256 m_last_block_processed GUARDED_BY(cs_wallet); public: /* |