| 180 | } |
| 181 | |
| 182 | bool LegacyBlockChainReader::import_blockchain2(const std::string &index_file_name, const std::string &item_file_name, |
| 183 | BlockChainState *block_chain, Height max_height) { |
| 184 | auto idea_start = std::chrono::high_resolution_clock::now(); |
| 185 | Height start_block = 0; |
| 186 | try { |
| 187 | LegacyBlockChainReader reader(block_chain->get_currency(), index_file_name, item_file_name); |
| 188 | if (reader.get_block_count() == 0) |
| 189 | return true; |
| 190 | const size_t import_height = std::min(max_height, reader.get_block_count() - 1); |
| 191 | if (block_chain->get_tip_height() >= import_height) { |
| 192 | // std::cout << "Skipping block chain import - we have more blocks than " |
| 193 | // "blocks.bin tip_height=" |
| 194 | // << block_chain->get_tip_height() << " bs_count=" << bs_count << std::endl; |
| 195 | return true; |
| 196 | } |
| 197 | std::cout << "Importing blocks up to height " << import_height << std::endl; |
| 198 | start_block = block_chain->get_tip_height(); |
| 199 | // api::BlockHeader prev_info; |
| 200 | while (block_chain->get_tip_height() < import_height) { |
| 201 | boost::variant<ConsensusError, PreparedBlock> result = |
| 202 | reader.get_prepared_block_by_index(block_chain->get_tip_height() + 1); |
| 203 | if (const ConsensusError *err = boost::get<ConsensusError>(&result)) |
| 204 | throw *err; |
| 205 | const PreparedBlock &pb = boost::get<PreparedBlock>(result); |
| 206 | api::BlockHeader info; |
| 207 | if (!block_chain->add_block(pb, &info, false, "blocks_file")) { |
| 208 | std::cout << "block_chain.add_block !BROADCAST_ALL block=" << block_chain->get_tip_height() + 1 |
| 209 | << std::endl; |
| 210 | block_chain->db_commit(); |
| 211 | return false; |
| 212 | } |
| 213 | // if (block_chain->get_tip_height() % 50000 == 0) |
| 214 | // block_chain->db_commit(); |
| 215 | // ts_file << info.timestamp << "\t" << info.timestamp_median << "\t" << |
| 216 | // info.timestamp_unlock << "\t" |
| 217 | // << int64_t(info.timestamp) - |
| 218 | // int64_t(prev_info.timestamp) << "\t" |
| 219 | // << int64_t(info.timestamp_median) - |
| 220 | // int64_t(prev_info.timestamp_median) << "\t" |
| 221 | // << int64_t(info.timestamp) - |
| 222 | // int64_t(info.timestamp_median) << std::endl; |
| 223 | // prev_info = info; |
| 224 | // if (block_chain->get_tip_height() == 1370000) // 1370000 |
| 225 | // break; |
| 226 | } |
| 227 | } catch (const std::exception &ex) { |
| 228 | std::cout << "Exception while importing blockchain file, what=" << common::what(ex) << std::endl; |
| 229 | return false; |
| 230 | } catch (...) { |
| 231 | std::cout << "Unknown exception while importing blockchain file" << std::endl; |
| 232 | return false; |
| 233 | } |
| 234 | block_chain->db_commit(); |
| 235 | auto idea_ms = |
| 236 | std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - idea_start); |
| 237 | std::cout << "Import blocks " << start_block << ":" << block_chain->get_tip_height() |
| 238 | << " seconds=" << double(idea_ms.count()) / 1000 << std::endl; |
| 239 | return true; |
nothing calls this directly
no test coverage detected