Skip to content

Commit 4226779

Browse files
author
Antoine Riard
committed
refactor: replace isPotentialtip/waitForNotifications by higher method
Add GUARDED_BY(cs_wallet) annotation to m_last_block_processed, given that its now guarded by cs_wallet instead of cs_main
1 parent edfe943 commit 4226779

File tree

4 files changed

+19
-37
lines changed

4 files changed

+19
-37
lines changed

src/interfaces/chain.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,6 @@ class LockImpl : public Chain::Lock
136136
}
137137
return nullopt;
138138
}
139-
bool isPotentialTip(const uint256& hash) override
140-
{
141-
if (::chainActive.Tip()->GetBlockHash() == hash) return true;
142-
CBlockIndex* block = LookupBlockIndex(hash);
143-
return block && block->GetAncestor(::chainActive.Height()) == ::chainActive.Tip();
144-
}
145139
CBlockLocator getTipLocator() override { return ::chainActive.GetLocator(); }
146140
Optional<int> findLocatorFork(const CBlockLocator& locator) override
147141
{
@@ -358,7 +352,16 @@ class ChainImpl : public Chain
358352
{
359353
return MakeUnique<NotificationsHandlerImpl>(*this, notifications);
360354
}
361-
void waitForNotifications() override { SyncWithValidationInterfaceQueue(); }
355+
void waitForNotificationsIfNewBlocksConnected(const uint256& old_tip) override
356+
{
357+
if (!old_tip.IsNull()) {
358+
LOCK(::cs_main);
359+
if (old_tip == ::chainActive.Tip()->GetBlockHash()) return;
360+
CBlockIndex* block = LookupBlockIndex(old_tip);
361+
if (block && block->GetAncestor(::chainActive.Height()) == ::chainActive.Tip()) return;
362+
}
363+
SyncWithValidationInterfaceQueue();
364+
}
362365
std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override
363366
{
364367
return MakeUnique<RpcHandlerImpl>(command);

src/interfaces/chain.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ class Wallet;
4343
//! asynchronously
4444
//! (https://github.com/bitcoin/bitcoin/pull/10973#issuecomment-380101269).
4545
//!
46-
//! * The isPotentialTip() and waitForNotifications() methods are too low-level
47-
//! and should be replaced with a higher level
48-
//! waitForNotificationsUpTo(block_hash) method that the wallet can call
49-
//! instead
50-
//! (https://github.com/bitcoin/bitcoin/pull/10973#discussion_r266995234).
51-
//!
5246
//! * The relayTransactions() and submitToMemoryPool() methods could be replaced
5347
//! with a higher-level broadcastTransaction method
5448
//! (https://github.com/bitcoin/bitcoin/pull/14978#issuecomment-459373984).
@@ -132,11 +126,6 @@ class Chain
132126
//! information is desired).
133127
virtual Optional<int> findFork(const uint256& hash, Optional<int>* height) = 0;
134128

135-
//! Return true if block hash points to the current chain tip, or to a
136-
//! possible descendant of the current chain tip that isn't currently
137-
//! connected.
138-
virtual bool isPotentialTip(const uint256& hash) = 0;
139-
140129
//! Get locator for the current chain tip.
141130
virtual CBlockLocator getTipLocator() = 0;
142131

@@ -271,8 +260,10 @@ class Chain
271260
//! Register handler for notifications.
272261
virtual std::unique_ptr<Handler> handleNotifications(Notifications& notifications) = 0;
273262

274-
//! Wait for pending notifications to be handled.
275-
virtual void waitForNotifications() = 0;
263+
//! Wait for pending notifications to be processed unless block hash points to the current
264+
//! chain tip, or to a possible descendant of the current chain tip that isn't currently
265+
//! connected.
266+
virtual void waitForNotificationsIfNewBlocksConnected(const uint256& old_tip) = 0;
276267

277268
//! Register handler for RPC. Command is not copied, so reference
278269
//! needs to remain valid until Handler is disconnected.

src/wallet/wallet.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,24 +1288,12 @@ void CWallet::UpdatedBlockTip()
12881288

12891289
void CWallet::BlockUntilSyncedToCurrentChain() {
12901290
AssertLockNotHeld(cs_wallet);
1291-
1292-
{
1293-
// Skip the queue-draining stuff if we know we're caught up with
1294-
// chainActive.Tip()...
1295-
// We could also take cs_wallet here, and call m_last_block_processed
1296-
// protected by cs_wallet instead of cs_main, but as long as we need
1297-
// cs_main here anyway, it's easier to just call it cs_main-protected.
1298-
auto locked_chain = chain().lock();
1299-
1300-
if (!m_last_block_processed.IsNull() && locked_chain->isPotentialTip(m_last_block_processed)) {
1301-
return;
1302-
}
1303-
}
1304-
1305-
// ...otherwise put a callback in the validation interface queue and wait
1291+
// Skip the queue-draining stuff if we know we're caught up with
1292+
// chainActive.Tip(), otherwise put a callback in the validation interface queue and wait
13061293
// for the queue to drain enough to execute it (indicating we are caught up
13071294
// at least with the time we entered this function).
1308-
chain().waitForNotifications();
1295+
uint256 last_block_hash = WITH_LOCK(cs_wallet, return m_last_block_processed);
1296+
chain().waitForNotificationsIfNewBlocksConnected(last_block_hash);
13091297
}
13101298

13111299

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
720720
* to have seen all transactions in the chain, but is only used to track
721721
* live BlockConnected callbacks.
722722
*/
723-
uint256 m_last_block_processed;
723+
uint256 m_last_block_processed GUARDED_BY(cs_wallet);
724724

725725
public:
726726
/*

0 commit comments

Comments
 (0)