bool CMasternodeDB::Write(const CMasternodeMan& mnodemanToSave) { int64_t nStart = GetTimeMillis(); // serialize addresses, checksum data up to that point, then append csum CDataStream ssMasternodes(SER_DISK, CLIENT_VERSION); ssMasternodes << FLATDATA(Params().MessageStart()); ssMasternodes << mnodemanToSave; uint256 hash = Hash(ssMasternodes.begin(), ssMasternodes.end()); ssMasternodes << hash; // open output file, and associate with CAutoFile FILE *file = fopen(pathMN.string().c_str(), "wb"); CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION); if (!fileout) return error("%s : Failed to open file %s", __func__, pathMN.string()); // Write and commit header, data try { fileout << ssMasternodes; } catch (std::exception &e) { return error("%s : Serialize or I/O error - %s", __func__, e.what()); } FileCommit(fileout); fileout.fclose(); LogPrintf("Written info to mncache.dat %dms\n", GetTimeMillis() - nStart); LogPrintf(" %s\n", mnodemanToSave.ToString()); return true; }
void CMempoolComponent::DumpMempool(void) { int64_t start = GetTimeMicros(); std::map<uint256, CAmount> mapDeltas; std::vector<TxMempoolInfo> vinfo; { LOCK(cs); for (const auto &i : mempool.mapDeltas) { mapDeltas[i.first] = i.second; } vinfo = mempool.infoAll(); } int64_t mid = GetTimeMicros(); try { FILE *filestr = fsbridge::fopen(Args().GetDataDir() / "mempool.dat.new", "wb"); if (!filestr) { return; } CAutoFile file(filestr, SER_DISK, CLIENT_VERSION); uint64_t version = MEMPOOL_DUMP_VERSION; file << version; file << (uint64_t)vinfo.size(); for (const auto &i : vinfo) { file << *(i.tx); file << (int64_t)i.nTime; file << (int64_t)i.nFeeDelta; mapDeltas.erase(i.tx->GetHash()); } file << mapDeltas; FileCommit(file.Get()); file.fclose(); RenameOver(Args().GetDataDir() / "mempool.dat.new", Args().GetDataDir() / "mempool.dat"); int64_t last = GetTimeMicros(); NLogFormat("Dumped mempool: %gs to copy, %gs to dump", (mid - start) * 0.000001, (last - mid) * 0.000001); } catch (const std::exception &e) { ELogFormat("Failed to dump mempool: %s. Continuing anyway", e.what()); } }
bool CBanDB::Write(const banmap_t& banSet) { // Generate random temporary filename unsigned short randv = 0; GetRandBytes((unsigned char*)&randv, sizeof(randv)); std::string tmpfn = strprintf("banlist.dat.%04x", randv); // serialize banlist, checksum data up to that point, then append csum CDataStream ssBanlist(SER_DISK, CLIENT_VERSION); ssBanlist << FLATDATA(Params().MessageStart()); ssBanlist << banSet; uint256 hash = Hash(ssBanlist.begin(), ssBanlist.end()); ssBanlist << hash; // open temp output file, and associate with CAutoFile boost::filesystem::path pathTmp = GetDataDir() / tmpfn; FILE *file = fopen(pathTmp.string().c_str(), "wb"); CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); if (fileout.IsNull()) return error("%s: Failed to open file %s", __func__, pathTmp.string()); // Write and commit header, data try { fileout << ssBanlist; } catch (const std::exception& e) { return error("%s: Serialize or I/O error - %s", __func__, e.what()); } FileCommit(fileout.Get()); fileout.fclose(); // replace existing banlist.dat, if any, with new banlist.dat.XXXX if (!RenameOver(pathTmp, pathBanlist)) return error("%s: Rename-into-place failed", __func__); return true; }