merge bitcoin#15842: replace isPotentialtip/waitForNotifications by higher method

This commit is contained in:
Kittywhiskers Van Gogh 2022-04-03 15:23:22 +05:30
parent 2b02a1b03e
commit 1d02804ce2
4 changed files with 19 additions and 32 deletions

View File

@ -130,13 +130,6 @@ class LockImpl : public Chain::Lock, public UniqueLock<CCriticalSection>
}
return nullopt;
}
bool isPotentialTip(const uint256& hash) override
{
LockAnnotation lock(::cs_main);
if (::ChainActive().Tip()->GetBlockHash() == hash) return true;
CBlockIndex* block = LookupBlockIndex(hash);
return block && block->GetAncestor(::ChainActive().Height()) == ::ChainActive().Tip();
}
CBlockLocator getTipLocator() override
{
LockAnnotation lock(::cs_main);
@ -355,7 +348,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);

View File

@ -123,11 +123,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;
@ -247,8 +242,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.

View File

@ -1595,24 +1595,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);
}

View File

@ -696,7 +696,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);
/** Pulled from wallet DB ("ps_salt") and used when mixing a random number of rounds.
* This salt is needed to prevent an attacker from learning how many extra times