Skip to content

Commit 2318aff

Browse files
committed
MOVEONLY: Move BaseIndex to its own file.
1 parent f376a49 commit 2318aff

File tree

5 files changed

+345
-326
lines changed

5 files changed

+345
-326
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ BITCOIN_CORE_H = \
107107
fs.h \
108108
httprpc.h \
109109
httpserver.h \
110+
index/base.h \
110111
index/txindex.h \
111112
indirectmap.h \
112113
init.h \
@@ -208,6 +209,7 @@ libbitcoin_server_a_SOURCES = \
208209
consensus/tx_verify.cpp \
209210
httprpc.cpp \
210211
httpserver.cpp \
212+
index/base.cpp \
211213
index/txindex.cpp \
212214
init.cpp \
213215
dbwrapper.cpp \

src/index/base.cpp

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
// Copyright (c) 2017-2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <chainparams.h>
6+
#include <index/base.h>
7+
#include <init.h>
8+
#include <tinyformat.h>
9+
#include <ui_interface.h>
10+
#include <util.h>
11+
#include <validation.h>
12+
#include <warnings.h>
13+
14+
constexpr int64_t SYNC_LOG_INTERVAL = 30; // seconds
15+
constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL = 30; // seconds
16+
17+
template<typename... Args>
18+
static void FatalError(const char* fmt, const Args&... args)
19+
{
20+
std::string strMessage = tfm::format(fmt, args...);
21+
SetMiscWarning(strMessage);
22+
LogPrintf("*** %s\n", strMessage);
23+
uiInterface.ThreadSafeMessageBox(
24+
"Error: A fatal internal error occurred, see debug.log for details",
25+
"", CClientUIInterface::MSG_ERROR);
26+
StartShutdown();
27+
}
28+
29+
BaseIndex::~BaseIndex()
30+
{
31+
Interrupt();
32+
Stop();
33+
}
34+
35+
bool BaseIndex::Init()
36+
{
37+
CBlockLocator locator;
38+
if (!GetDB().ReadBestBlock(locator)) {
39+
locator.SetNull();
40+
}
41+
42+
LOCK(cs_main);
43+
m_best_block_index = FindForkInGlobalIndex(chainActive, locator);
44+
m_synced = m_best_block_index.load() == chainActive.Tip();
45+
return true;
46+
}
47+
48+
static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev)
49+
{
50+
AssertLockHeld(cs_main);
51+
52+
if (!pindex_prev) {
53+
return chainActive.Genesis();
54+
}
55+
56+
const CBlockIndex* pindex = chainActive.Next(pindex_prev);
57+
if (pindex) {
58+
return pindex;
59+
}
60+
61+
return chainActive.Next(chainActive.FindFork(pindex_prev));
62+
}
63+
64+
void BaseIndex::ThreadSync()
65+
{
66+
const CBlockIndex* pindex = m_best_block_index.load();
67+
if (!m_synced) {
68+
auto& consensus_params = Params().GetConsensus();
69+
70+
int64_t last_log_time = 0;
71+
int64_t last_locator_write_time = 0;
72+
while (true) {
73+
if (m_interrupt) {
74+
WriteBestBlock(pindex);
75+
return;
76+
}
77+
78+
{
79+
LOCK(cs_main);
80+
const CBlockIndex* pindex_next = NextSyncBlock(pindex);
81+
if (!pindex_next) {
82+
WriteBestBlock(pindex);
83+
m_best_block_index = pindex;
84+
m_synced = true;
85+
break;
86+
}
87+
pindex = pindex_next;
88+
}
89+
90+
int64_t current_time = GetTime();
91+
if (last_log_time + SYNC_LOG_INTERVAL < current_time) {
92+
LogPrintf("Syncing %s with block chain from height %d\n",
93+
GetName(), pindex->nHeight);
94+
last_log_time = current_time;
95+
}
96+
97+
if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
98+
WriteBestBlock(pindex);
99+
last_locator_write_time = current_time;
100+
}
101+
102+
CBlock block;
103+
if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
104+
FatalError("%s: Failed to read block %s from disk",
105+
__func__, pindex->GetBlockHash().ToString());
106+
return;
107+
}
108+
if (!WriteBlock(block, pindex)) {
109+
FatalError("%s: Failed to write block %s to index database",
110+
__func__, pindex->GetBlockHash().ToString());
111+
return;
112+
}
113+
}
114+
}
115+
116+
if (pindex) {
117+
LogPrintf("%s is enabled at height %d\n", GetName(), pindex->nHeight);
118+
} else {
119+
LogPrintf("%s is enabled\n", GetName());
120+
}
121+
}
122+
123+
bool BaseIndex::WriteBestBlock(const CBlockIndex* block_index)
124+
{
125+
LOCK(cs_main);
126+
if (!GetDB().WriteBestBlock(chainActive.GetLocator(block_index))) {
127+
return error("%s: Failed to write locator to disk", __func__);
128+
}
129+
return true;
130+
}
131+
132+
void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex,
133+
const std::vector<CTransactionRef>& txn_conflicted)
134+
{
135+
if (!m_synced) {
136+
return;
137+
}
138+
139+
const CBlockIndex* best_block_index = m_best_block_index.load();
140+
if (!best_block_index) {
141+
if (pindex->nHeight != 0) {
142+
FatalError("%s: First block connected is not the genesis block (height=%d)",
143+
__func__, pindex->nHeight);
144+
return;
145+
}
146+
} else {
147+
// Ensure block connects to an ancestor of the current best block. This should be the case
148+
// most of the time, but may not be immediately after the sync thread catches up and sets
149+
// m_synced. Consider the case where there is a reorg and the blocks on the stale branch are
150+
// in the ValidationInterface queue backlog even after the sync thread has caught up to the
151+
// new chain tip. In this unlikely event, log a warning and let the queue clear.
152+
if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) {
153+
LogPrintf("%s: WARNING: Block %s does not connect to an ancestor of " /* Continued */
154+
"known best chain (tip=%s); not updating index\n",
155+
__func__, pindex->GetBlockHash().ToString(),
156+
best_block_index->GetBlockHash().ToString());
157+
return;
158+
}
159+
}
160+
161+
if (WriteBlock(*block, pindex)) {
162+
m_best_block_index = pindex;
163+
} else {
164+
FatalError("%s: Failed to write block %s to index",
165+
__func__, pindex->GetBlockHash().ToString());
166+
return;
167+
}
168+
}
169+
170+
void BaseIndex::ChainStateFlushed(const CBlockLocator& locator)
171+
{
172+
if (!m_synced) {
173+
return;
174+
}
175+
176+
const uint256& locator_tip_hash = locator.vHave.front();
177+
const CBlockIndex* locator_tip_index;
178+
{
179+
LOCK(cs_main);
180+
locator_tip_index = LookupBlockIndex(locator_tip_hash);
181+
}
182+
183+
if (!locator_tip_index) {
184+
FatalError("%s: First block (hash=%s) in locator was not found",
185+
__func__, locator_tip_hash.ToString());
186+
return;
187+
}
188+
189+
// This checks that ChainStateFlushed callbacks are received after BlockConnected. The check may fail
190+
// immediately after the sync thread catches up and sets m_synced. Consider the case where
191+
// there is a reorg and the blocks on the stale branch are in the ValidationInterface queue
192+
// backlog even after the sync thread has caught up to the new chain tip. In this unlikely
193+
// event, log a warning and let the queue clear.
194+
const CBlockIndex* best_block_index = m_best_block_index.load();
195+
if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) {
196+
LogPrintf("%s: WARNING: Locator contains block (hash=%s) not on known best " /* Continued */
197+
"chain (tip=%s); not writing index locator\n",
198+
__func__, locator_tip_hash.ToString(),
199+
best_block_index->GetBlockHash().ToString());
200+
return;
201+
}
202+
203+
if (!GetDB().WriteBestBlock(locator)) {
204+
error("%s: Failed to write locator to disk", __func__);
205+
}
206+
}
207+
208+
bool BaseIndex::BlockUntilSyncedToCurrentChain()
209+
{
210+
AssertLockNotHeld(cs_main);
211+
212+
if (!m_synced) {
213+
return false;
214+
}
215+
216+
{
217+
// Skip the queue-draining stuff if we know we're caught up with
218+
// chainActive.Tip().
219+
LOCK(cs_main);
220+
const CBlockIndex* chain_tip = chainActive.Tip();
221+
const CBlockIndex* best_block_index = m_best_block_index.load();
222+
if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) {
223+
return true;
224+
}
225+
}
226+
227+
LogPrintf("%s: %s is catching up on block notifications\n", __func__, GetName());
228+
SyncWithValidationInterfaceQueue();
229+
return true;
230+
}
231+
232+
void BaseIndex::Interrupt()
233+
{
234+
m_interrupt();
235+
}
236+
237+
void BaseIndex::Start()
238+
{
239+
// Need to register this ValidationInterface before running Init(), so that
240+
// callbacks are not missed if Init sets m_synced to true.
241+
RegisterValidationInterface(this);
242+
if (!Init()) {
243+
FatalError("%s: %s failed to initialize", __func__, GetName());
244+
return;
245+
}
246+
247+
m_thread_sync = std::thread(&TraceThread<std::function<void()>>, GetName(),
248+
std::bind(&BaseIndex::ThreadSync, this));
249+
}
250+
251+
void BaseIndex::Stop()
252+
{
253+
UnregisterValidationInterface(this);
254+
255+
if (m_thread_sync.joinable()) {
256+
m_thread_sync.join();
257+
}
258+
}

src/index/base.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) 2017-2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_INDEX_BASE_H
6+
#define BITCOIN_INDEX_BASE_H
7+
8+
#include <primitives/block.h>
9+
#include <primitives/transaction.h>
10+
#include <threadinterrupt.h>
11+
#include <txdb.h>
12+
#include <uint256.h>
13+
#include <validationinterface.h>
14+
15+
class CBlockIndex;
16+
17+
/**
18+
* Base class for indices of blockchain data. This implements
19+
* CValidationInterface and ensures blocks are indexed sequentially according
20+
* to their position in the active chain.
21+
*/
22+
class BaseIndex : public CValidationInterface
23+
{
24+
private:
25+
/// Whether the index is in sync with the main chain. The flag is flipped
26+
/// from false to true once, after which point this starts processing
27+
/// ValidationInterface notifications to stay in sync.
28+
std::atomic<bool> m_synced{false};
29+
30+
/// The last block in the chain that the index is in sync with.
31+
std::atomic<const CBlockIndex*> m_best_block_index{nullptr};
32+
33+
std::thread m_thread_sync;
34+
CThreadInterrupt m_interrupt;
35+
36+
/// Sync the index with the block index starting from the current best block.
37+
/// Intended to be run in its own thread, m_thread_sync, and can be
38+
/// interrupted with m_interrupt. Once the index gets in sync, the m_synced
39+
/// flag is set and the BlockConnected ValidationInterface callback takes
40+
/// over and the sync thread exits.
41+
void ThreadSync();
42+
43+
/// Write the current chain block locator to the DB.
44+
bool WriteBestBlock(const CBlockIndex* block_index);
45+
46+
protected:
47+
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex,
48+
const std::vector<CTransactionRef>& txn_conflicted) override;
49+
50+
void ChainStateFlushed(const CBlockLocator& locator) override;
51+
52+
/// Initialize internal state from the database and block index.
53+
virtual bool Init();
54+
55+
/// Write update index entries for a newly connected block.
56+
virtual bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) { return true; }
57+
58+
virtual BaseIndexDB& GetDB() const = 0;
59+
60+
/// Get the name of the index for display in logs.
61+
virtual const char* GetName() const = 0;
62+
63+
public:
64+
/// Destructor interrupts sync thread if running and blocks until it exits.
65+
virtual ~BaseIndex();
66+
67+
/// Blocks the current thread until the index is caught up to the current
68+
/// state of the block chain. This only blocks if the index has gotten in
69+
/// sync once and only needs to process blocks in the ValidationInterface
70+
/// queue. If the index is catching up from far behind, this method does
71+
/// not block and immediately returns false.
72+
bool BlockUntilSyncedToCurrentChain();
73+
74+
void Interrupt();
75+
76+
/// Start initializes the sync state and registers the instance as a
77+
/// ValidationInterface so that it stays in sync with blockchain updates.
78+
void Start();
79+
80+
/// Stops the instance from staying in sync with blockchain updates.
81+
void Stop();
82+
};
83+
84+
#endif // BITCOIN_INDEX_BASE_H

0 commit comments

Comments
 (0)