| 1037 | } |
| 1038 | |
| 1039 | void Node::export_static_sync_blocks(const BlockChainState &block_chain, const std::string &folder) { |
| 1040 | if (block_chain.get_tip_height() < block_chain.get_currency().last_hard_checkpoint().height) |
| 1041 | throw std::runtime_error("Daemon must be synced at least to last hard checkpoint"); |
| 1042 | if (!platform::folder_exists(folder)) |
| 1043 | throw std::runtime_error("Folder for static sync_blocks must exist " + folder); |
| 1044 | api::cnd::SyncBlocks::Request req; |
| 1045 | req.max_size = 1024 * 1024; |
| 1046 | req.max_count = 1000; |
| 1047 | req.need_redundant_data = false; |
| 1048 | std::vector<Hash> all_chain(block_chain.get_currency().last_hard_checkpoint().height + 1); |
| 1049 | std::cout << "Reading chain, can take a minute or two..." << std::endl; |
| 1050 | for (Height i = 0; i != block_chain.get_currency().last_hard_checkpoint().height + 1; ++i) |
| 1051 | invariant(block_chain.get_chain(i, &all_chain.at(i)), ""); |
| 1052 | Height ha = 0; |
| 1053 | api::cnd::SyncBlocks::ResponseCompact res; |
| 1054 | size_t max_total_count = 0; |
| 1055 | size_t min_total_count = std::numeric_limits<size_t>::max(); |
| 1056 | size_t sum_total_size_real = 0; |
| 1057 | size_t sum_total_size_link = 0; |
| 1058 | while (ha <= block_chain.get_currency().last_hard_checkpoint().height) { |
| 1059 | res.blocks.clear(); |
| 1060 | size_t total_size = 0; |
| 1061 | while (ha + res.blocks.size() <= block_chain.get_currency().last_hard_checkpoint().height) { |
| 1062 | res.blocks.push_back(block_chain.fill_sync_block_compact(all_chain.at(ha + res.blocks.size()))); |
| 1063 | total_size += seria::binary_size(res.blocks.back()); |
| 1064 | if (total_size >= req.max_size) |
| 1065 | break; |
| 1066 | } |
| 1067 | invariant(!res.blocks.empty(), ""); |
| 1068 | auto ba = json_rpc::create_binary_response_body(res, common::JsonValue(nullptr)); |
| 1069 | std::cout << "Saving chunk start=" << ha << ", count=" << res.blocks.size() << ", size=" << ba.size() |
| 1070 | << std::endl; |
| 1071 | sum_total_size_real += ba.size(); |
| 1072 | for (size_t d = 0; d != res.blocks.size(); ++d) { |
| 1073 | if (d == 1) { |
| 1074 | ba = common::to_string(ha); |
| 1075 | sum_total_size_link += ba.size() * (res.blocks.size() - 1); |
| 1076 | } |
| 1077 | std::string subfolder; |
| 1078 | auto file_name = api::cnd::SyncBlocks::get_filename(Height(ha + d), &subfolder); |
| 1079 | invariant( |
| 1080 | platform::create_folders_if_necessary(folder + api::cnd::SyncBlocks::url_prefix() + "/" + subfolder), |
| 1081 | ""); |
| 1082 | invariant(platform::save_file( |
| 1083 | folder + api::cnd::SyncBlocks::url_prefix() + "/" + file_name, ba.data(), ba.size()), |
| 1084 | ""); |
| 1085 | } |
| 1086 | ha += res.blocks.size(); |
| 1087 | max_total_count = std::max(max_total_count, res.blocks.size()); |
| 1088 | min_total_count = std::min(min_total_count, res.blocks.size()); |
| 1089 | } |
| 1090 | std::cout << "min_total_count=" << min_total_count << " max_total_count=" << max_total_count << std::endl; |
| 1091 | std::cout << "sum_total_size_real=" << sum_total_size_real << " sum_total_size_link=" << sum_total_size_link |
| 1092 | << std::endl; |
| 1093 | } |
nothing calls this directly
no test coverage detected