MCPcopy Create free account
hub / github.com/ElementsProject/elements / ThreadSync

Method ThreadSync

src/index/base.cpp:130–200  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128}
129
130void BaseIndex::ThreadSync()
131{
132 SetSyscallSandboxPolicy(SyscallSandboxPolicy::TX_INDEX);
133 const CBlockIndex* pindex = m_best_block_index.load();
134 if (!m_synced) {
135 auto& consensus_params = Params().GetConsensus();
136
137 int64_t last_log_time = 0;
138 int64_t last_locator_write_time = 0;
139 while (true) {
140 if (m_interrupt) {
141 m_best_block_index = pindex;
142 // No need to handle errors in Commit. If it fails, the error will be already be
143 // logged. The best way to recover is to continue, as index cannot be corrupted by
144 // a missed commit to disk for an advanced index state.
145 Commit();
146 return;
147 }
148
149 {
150 LOCK(cs_main);
151 const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain);
152 if (!pindex_next) {
153 m_best_block_index = pindex;
154 // No need to handle errors in Commit. See rationale above.
155 Commit();
156 m_synced = true;
157 break;
158 }
159 if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
160 FatalError("%s: Failed to rewind index %s to a previous chain tip",
161 __func__, GetName());
162 return;
163 }
164 pindex = pindex_next;
165 }
166
167 int64_t current_time = GetTime();
168 if (last_log_time + SYNC_LOG_INTERVAL < current_time) {
169 LogPrintf("Syncing %s with block chain from height %d\n",
170 GetName(), pindex->nHeight);
171 last_log_time = current_time;
172 }
173
174 if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
175 m_best_block_index = pindex;
176 last_locator_write_time = current_time;
177 // No need to handle errors in Commit. See rationale above.
178 Commit();
179 }
180
181 CBlock block;
182 if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
183 FatalError("%s: Failed to read block %s from disk",
184 __func__, pindex->GetBlockHash().ToString());
185 return;
186 }
187 if (!WriteBlock(block, pindex)) {

Callers

nothing calls this directly

Calls 8

SetSyscallSandboxPolicyFunction · 0.85
FatalErrorFunction · 0.85
GetTimeFunction · 0.85
ReadBlockFromDiskFunction · 0.85
ParamsClass · 0.50
loadMethod · 0.45
ToStringMethod · 0.45
GetBlockHashMethod · 0.45

Tested by

no test coverage detected