From f90c3a62f506d1bc0fe26972b312f07152c79b2e Mon Sep 17 00:00:00 2001 From: Jim Posen Date: Fri, 8 Dec 2017 11:20:10 -0800 Subject: [index] TxIndex method to wait until caught up. In order to preserve getrawtransaction RPC behavior, there needs to be a way for a thread to ensure the transaction index is in sync with the current state of the blockchain. --- src/index/txindex.cpp | 24 ++++++++++++++++++++++++ src/index/txindex.h | 6 ++++++ 2 files changed, 30 insertions(+) (limited to 'src/index') diff --git a/src/index/txindex.cpp b/src/index/txindex.cpp index 82798fbcc1..484526a6d9 100644 --- a/src/index/txindex.cpp +++ b/src/index/txindex.cpp @@ -228,6 +228,30 @@ void TxIndex::SetBestChain(const CBlockLocator& locator) } } +bool TxIndex::BlockUntilSyncedToCurrentChain() +{ + AssertLockNotHeld(cs_main); + + if (!m_synced) { + return false; + } + + { + // Skip the queue-draining stuff if we know we're caught up with + // chainActive.Tip(). + LOCK(cs_main); + const CBlockIndex* chain_tip = chainActive.Tip(); + const CBlockIndex* best_block_index = m_best_block_index.load(); + if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) { + return true; + } + } + + LogPrintf("%s: txindex is catching up on block notifications\n", __func__); + SyncWithValidationInterfaceQueue(); + return true; +} + bool TxIndex::FindTx(const uint256& txid, CDiskTxPos& pos) const { return m_db->ReadTxPos(txid, pos); diff --git a/src/index/txindex.h b/src/index/txindex.h index 633aee46ce..e1f1b17676 100644 --- a/src/index/txindex.h +++ b/src/index/txindex.h @@ -63,6 +63,12 @@ public: /// Destructor interrupts sync thread if running and blocks until it exits. ~TxIndex(); + /// Blocks the current thread until the transaction index is caught up to + /// the current state of the block chain. This only blocks if the index has gotten in sync once + /// and only needs to process blocks in the ValidationInterface queue. If the index is catching + /// up from far behind, this method does not block and immediately returns false. + bool BlockUntilSyncedToCurrentChain(); + /// Look up the on-disk location of a transaction by hash. bool FindTx(const uint256& txid, CDiskTxPos& pos) const; -- cgit v1.2.3