| 128 | } |
| 129 | |
| 130 | void 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)) { |
nothing calls this directly
no test coverage detected