| 5064 | } |
| 5065 | |
| 5066 | bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function, bool skip_file_commit) |
| 5067 | { |
| 5068 | int64_t start = GetTimeMicros(); |
| 5069 | |
| 5070 | std::map<uint256, CAmount> mapDeltas; |
| 5071 | std::vector<TxMempoolInfo> vinfo; |
| 5072 | std::set<uint256> unbroadcast_txids; |
| 5073 | |
| 5074 | static Mutex dump_mutex; |
| 5075 | LOCK(dump_mutex); |
| 5076 | |
| 5077 | { |
| 5078 | LOCK(pool.cs); |
| 5079 | for (const auto &i : pool.mapDeltas) { |
| 5080 | mapDeltas[i.first] = i.second; |
| 5081 | } |
| 5082 | vinfo = pool.infoAll(); |
| 5083 | unbroadcast_txids = pool.GetUnbroadcastTxs(); |
| 5084 | } |
| 5085 | |
| 5086 | int64_t mid = GetTimeMicros(); |
| 5087 | |
| 5088 | try { |
| 5089 | FILE* filestr{mockable_fopen_function(gArgs.GetDataDirNet() / "mempool.dat.new", "wb")}; |
| 5090 | if (!filestr) { |
| 5091 | return false; |
| 5092 | } |
| 5093 | |
| 5094 | CAutoFile file(filestr, SER_DISK, CLIENT_VERSION); |
| 5095 | |
| 5096 | uint64_t version = MEMPOOL_DUMP_VERSION; |
| 5097 | file << version; |
| 5098 | |
| 5099 | file << (uint64_t)vinfo.size(); |
| 5100 | for (const auto& i : vinfo) { |
| 5101 | file << *(i.tx); |
| 5102 | file << int64_t{count_seconds(i.m_time)}; |
| 5103 | file << int64_t{i.nFeeDelta}; |
| 5104 | mapDeltas.erase(i.tx->GetHash()); |
| 5105 | } |
| 5106 | |
| 5107 | file << mapDeltas; |
| 5108 | |
| 5109 | LogPrintf("Writing %d unbroadcast transactions to disk.\n", unbroadcast_txids.size()); |
| 5110 | file << unbroadcast_txids; |
| 5111 | |
| 5112 | if (!skip_file_commit && !FileCommit(file.Get())) |
| 5113 | throw std::runtime_error("FileCommit failed"); |
| 5114 | file.fclose(); |
| 5115 | if (!RenameOver(gArgs.GetDataDirNet() / "mempool.dat.new", gArgs.GetDataDirNet() / "mempool.dat")) { |
| 5116 | throw std::runtime_error("Rename failed"); |
| 5117 | } |
| 5118 | int64_t last = GetTimeMicros(); |
| 5119 | LogPrintf("Dumped mempool: %gs to copy, %gs to dump\n", (mid-start)*MICRO, (last-mid)*MICRO); |
| 5120 | } catch (const std::exception& e) { |
| 5121 | LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what()); |
| 5122 | return false; |
| 5123 | } |